Skip to content

[ConstraintSystem] Favor resolving closures over any disjunction bind… #29700

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
Feb 7, 2020
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
9 changes: 8 additions & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,14 @@ bool ConstraintSystem::PotentialBindings::favoredOverDisjunction(
// if it's something like a collection (where it has to pick
// between a conversion and bridging conversion) or concrete
// type let's prefer the disjunction.
return boundType->is<TypeVariableType>();
//
// We are looking through optionals here because it could be
// a situation where disjunction is formed to match optionals
// either as deep equality or optional-to-optional conversion.
// Such type variables might be connected to closure as well
// e.g. when result type is optional, so it makes sense to
// open closure before attempting such disjunction.
return boundType->lookThroughAllOptionalTypes()->is<TypeVariableType>();
}

return !InvolvesTypeVariables;
Expand Down
18 changes: 18 additions & 0 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -953,3 +953,21 @@ class Foo<State: StateType> {
func test_explicit_variadic_is_interpreted_correctly() {
_ = { (T: String...) -> String in T[0] + "" } // Ok
}

// rdar://problem/59208419 - closure result type is incorrectly inferred to be a supertype
func test_correct_inference_of_closure_result_in_presence_of_optionals() {
class A {}
class B : A {}

func foo(_: B) -> Int? { return 42 }

func bar<T: A>(_: (A) -> T?) -> T? {
return .none
}

guard let v = bar({ $0 as? B }),
let _ = foo(v) // Ok, v is inferred as `B`
else {
return;
}
}