File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -246,7 +246,14 @@ bool ConstraintSystem::PotentialBindings::favoredOverDisjunction(
246
246
// if it's something like a collection (where it has to pick
247
247
// between a conversion and bridging conversion) or concrete
248
248
// type let's prefer the disjunction.
249
- return boundType->is <TypeVariableType>();
249
+ //
250
+ // We are looking through optionals here because it could be
251
+ // a situation where disjunction is formed to match optionals
252
+ // either as deep equality or optional-to-optional conversion.
253
+ // Such type variables might be connected to closure as well
254
+ // e.g. when result type is optional, so it makes sense to
255
+ // open closure before attempting such disjunction.
256
+ return boundType->lookThroughAllOptionalTypes ()->is <TypeVariableType>();
250
257
}
251
258
252
259
return !InvolvesTypeVariables;
Original file line number Diff line number Diff line change @@ -953,3 +953,21 @@ class Foo<State: StateType> {
953
953
func test_explicit_variadic_is_interpreted_correctly( ) {
954
954
_ = { ( T: String... ) - > String in T [ 0 ] + " " } // Ok
955
955
}
956
+
957
+ // rdar://problem/59208419 - closure result type is incorrectly inferred to be a supertype
958
+ func test_correct_inference_of_closure_result_in_presence_of_optionals( ) {
959
+ class A { }
960
+ class B : A { }
961
+
962
+ func foo( _: B ) -> Int ? { return 42 }
963
+
964
+ func bar< T: A > ( _: ( A ) -> T ? ) -> T ? {
965
+ return . none
966
+ }
967
+
968
+ guard let v = bar ( { $0 as? B } ) ,
969
+ let _ = foo ( v) // Ok, v is inferred as `B`
970
+ else {
971
+ return ;
972
+ }
973
+ }
You can’t perform that action at this time.
0 commit comments