Skip to content

Sema: Explicitly allow Optional-to-IUO when converting functions. #7153

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 1 commit into from
Jan 31, 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
10 changes: 6 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ static bool isPotentiallyMoreOptionalThan(Type objType1,
/// Enumerate all of the applicable optional conversion restrictions
static void enumerateOptionalConversionRestrictions(
Type type1, Type type2,
ConstraintKind kind,
ConstraintKind kind, ConstraintLocatorBuilder locator,
llvm::function_ref<void(ConversionRestrictionKind)> fn) {
SmallVector<Type, 2> optionals1;
Type objType1 = type1->lookThroughAllAnyOptionalTypes(optionals1);
Expand All @@ -1251,6 +1251,7 @@ static void enumerateOptionalConversionRestrictions(
// Break cyclic conversions between T? and U! by only allowing it for
// conversion constraints.
if (kind >= ConstraintKind::Conversion ||
locator.isFunctionConversion() ||
!(optionalKind1 == OTK_Optional &&
optionalKind2 == OTK_ImplicitlyUnwrappedOptional))
fn(ConversionRestrictionKind::OptionalToOptional);
Expand Down Expand Up @@ -2055,9 +2056,10 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// 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) {
enumerateOptionalConversionRestrictions(type1, type2, kind,
[&](ConversionRestrictionKind restriction) {
conversionsOrFixes.push_back(restriction);
enumerateOptionalConversionRestrictions(
type1, type2, kind, locator,
[&](ConversionRestrictionKind restriction) {
conversionsOrFixes.push_back(restriction);
});
}

Expand Down
5 changes: 5 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,8 @@ func sr3497_unfold<A, B>(_ a0: A, next: (inout A) -> B) {}
func sr3497() {
let _ = sr3497_unfold((0, 0)) { s in 0 } // ok
}

// SR-3758: Swift 3.1 fails to compile 3.0 code involving closures and IUOs
let _: ((Any?) -> Void) = { (arg: Any!) in }
// This example was rejected in 3.0 as well, but accepting it is correct.
let _: ((Int?) -> Void) = { (arg: Int!) in }
11 changes: 11 additions & 0 deletions test/SILGen/implicitly_unwrapped_optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ func return_any() -> AnyObject! { return nil }
func bind_any() {
let object : AnyObject? = return_any()
}

// CHECK-LABEL: sil hidden @_T029implicitly_unwrapped_optional6sr3758yyF
func sr3758() {
// Verify that there are no additional reabstractions introduced.
// CHECK: [[CLOSURE:%.+]] = function_ref @_T029implicitly_unwrapped_optional6sr3758yyFySQyypGcfU_ : $@convention(thin) (@in Optional<Any>) -> ()
// CHECK: [[F:%.+]] = thin_to_thick_function [[CLOSURE]] : $@convention(thin) (@in Optional<Any>) -> () to $@callee_owned (@in Optional<Any>) -> ()
// CHECK: [[CALLEE:%.+]] = copy_value [[F]] : $@callee_owned (@in Optional<Any>) -> ()
// CHECK: = apply [[CALLEE]]({{%.+}}) : $@callee_owned (@in Optional<Any>) -> ()
let f: ((Any?) -> Void) = { (arg: Any!) in }
f(nil)
} // CHECK: end sil function '_T029implicitly_unwrapped_optional6sr3758yyF'