Skip to content

Revert optional breakage #6835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 50 additions & 39 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,37 +1193,6 @@ static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
return false;
}

static Optional<ConversionRestrictionKind>
selectOptionalConversionRestriction(Type type1, Type type2,
ConstraintKind kind) {
OptionalTypeKind optionalKind1 = OTK_None;
if (auto boundGeneric1 = type1->getAs<BoundGenericType>())
optionalKind1 = boundGeneric1->getDecl()->classifyAsOptionalType();

OptionalTypeKind optionalKind2 = OTK_None;
if (auto boundGeneric2 = type2->getAs<BoundGenericType>())
optionalKind2 = boundGeneric2->getDecl()->classifyAsOptionalType();

if (optionalKind2 == OTK_None) {
if (optionalKind1 == OTK_ImplicitlyUnwrappedOptional &&
kind >= ConstraintKind::Conversion)
return ConversionRestrictionKind::ForceUnchecked;

return None;
}

if (optionalKind1 == OTK_None)
return ConversionRestrictionKind::ValueToOptional;

if (optionalKind1 == OTK_Optional &&
optionalKind2 == OTK_ImplicitlyUnwrappedOptional &&
kind < ConstraintKind::Conversion)
return None;

return ConversionRestrictionKind::OptionalToOptional;
}


ConstraintSystem::SolutionKind
ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
TypeMatchOptions flags,
Expand Down Expand Up @@ -2016,15 +1985,57 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
}
}

// A value of type T! can be converted to type U if T is convertible
// to U by force-unwrapping the source value.
// A value of type T, T?, or T! can be converted to type U? or U! if
// T is convertible to U.
if (concrete && kind >= ConstraintKind::Subtype)
if (auto restriction =
selectOptionalConversionRestriction(type1, type2, kind))
conversionsOrFixes.push_back(restriction.getValue());
// A value of type T can be converted to type U? if T is convertible to U.
// A value of type T? can be converted to type U? if T is convertible to U.
// The above conversions also apply to implicitly unwrapped optional types,
// except that there is no implicit conversion from T? to T!.
{
BoundGenericType *boundGenericType2;

if (concrete && kind >= ConstraintKind::Subtype &&
(boundGenericType2 = type2->getAs<BoundGenericType>())) {
auto decl2 = boundGenericType2->getDecl();
if (auto optionalKind2 = decl2->classifyAsOptionalType()) {
assert(boundGenericType2->getGenericArgs().size() == 1);

BoundGenericType *boundGenericType1 = type1->getAs<BoundGenericType>();
if (boundGenericType1) {
auto decl1 = boundGenericType1->getDecl();
if (decl1 == decl2) {
assert(boundGenericType1->getGenericArgs().size() == 1);
conversionsOrFixes.push_back(
ConversionRestrictionKind::OptionalToOptional);
} else if (optionalKind2 == OTK_Optional &&
decl1 == TC.Context.getImplicitlyUnwrappedOptionalDecl()) {
assert(boundGenericType1->getGenericArgs().size() == 1);
conversionsOrFixes.push_back(
ConversionRestrictionKind::OptionalToOptional);
} else if (optionalKind2 == OTK_ImplicitlyUnwrappedOptional &&
kind >= ConstraintKind::Conversion &&
decl1 == TC.Context.getOptionalDecl()) {
assert(boundGenericType1->getGenericArgs().size() == 1);
conversionsOrFixes.push_back(
ConversionRestrictionKind::OptionalToOptional);
}
}

conversionsOrFixes.push_back(
ConversionRestrictionKind::ValueToOptional);
}
}
}

// A value of type T! can be (unsafely) forced to U if T
// is convertible to U.
{
Type objectType1;
if (concrete && kind >= ConstraintKind::Conversion &&
(objectType1 = lookThroughImplicitlyUnwrappedOptionalType(type1))) {
conversionsOrFixes.push_back(
ConversionRestrictionKind::ForceUnchecked);
}
}

// Allow '() -> T' to '() -> ()' and '() -> Never' to '() -> T' for closure
// literals.
if (auto elt = locator.last()) {
Expand Down
12 changes: 12 additions & 0 deletions test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,15 @@ SR_3248().callback("test") // expected-error {{cannot convert value of type 'St

_? = nil // expected-error {{'nil' requires a contextual type}}
_?? = nil // expected-error {{'nil' requires a contextual type}}


infix operator !====
func !====(_ lhs: AnyObject?, _ rhs: AnyObject?) -> Bool { return false }

func calleeRdar29977523(_ lhs: AnyObject?, _ rhs: AnyObject?) { }

func rdar29977523(lhs: AnyObject?!, rhs: AnyObject?) {
if lhs !==== rhs { }

calleeRdar29977523(lhs, rhs)
}