Skip to content

Commit 170dc8a

Browse files
committed
Do not penalize binding or equality constraints involving Any.
We currently have an element in the solution score related to whether we had a binding or equality constraint involving Any. Doing this yields some strange results, e.g. if overload resolution results in a property declared as Any we end up discarding that solution in favor of solutions that involve other overloads that are not declared as Any but are also not actually better solutions (e.g. overloads that are declared as function types). We really want to retain both solutions in this case and allow the ranking step of the solver to decide on the better choice. Fixes rdar://problem/29374163, rdar://problem/29691909.
1 parent 66824df commit 170dc8a

File tree

6 files changed

+24
-12
lines changed

6 files changed

+24
-12
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ void ConstraintSystem::increaseScore(ScoreKind kind) {
7474
case SK_ScalarPointerConversion:
7575
log << "scalar-to-pointer conversion";
7676
break;
77-
case SK_EmptyExistentialConversion:
78-
log << "empty-existential conversion";
79-
break;
8077
}
8178
log << ")\n";
8279
}

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,12 +1356,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
13561356
}
13571357

13581358
assignFixedType(typeVar1, type2);
1359-
1360-
// For symmetry with overload resolution, penalize conversions to empty
1361-
// existentials.
1362-
if (type2->isAny())
1363-
increaseScore(ScoreKind::SK_EmptyExistentialConversion);
1364-
1359+
13651360
return SolutionKind::Solved;
13661361
}
13671362

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,8 @@ enum ScoreKind {
406406
SK_ScalarPointerConversion,
407407
/// A conversion from an array to a pointer of matching element type.
408408
SK_ArrayPointerConversion,
409-
/// A conversion to an empty existential type ('Any' or '{}').
410-
SK_EmptyExistentialConversion,
411409

412-
SK_LastScoreKind = SK_EmptyExistentialConversion,
410+
SK_LastScoreKind = SK_ArrayPointerConversion,
413411
};
414412

415413
/// The number of score kinds.

test/Constraints/Inputs/overload.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@interface Rdar29374163Itf
2+
@property (nonnull, readonly, retain) id foo;
3+
- (_Nonnull id) fooWithRdar29374163Itf:(Rdar29374163Itf * _Nonnull) rdar29374163itf;
4+
@end

test/Constraints/diag_ambiguities.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ C(g) // expected-error{{ambiguous use of 'g'}}
2828

2929
func h<T>(_ x: T) -> () {}
3030
C(h) // expected-error{{ambiguous use of 'init'}}
31+
32+
func rdar29691909_callee(_ o: AnyObject?) -> Any? { return o } // expected-note {{found this candidate}}
33+
func rdar29691909_callee(_ o: AnyObject) -> Any { return o } // expected-note {{found this candidate}}
34+
35+
func rdar29691909(o: AnyObject) -> Any? {
36+
return rdar29691909_callee(o) // expected-error{{ambiguous use of 'rdar29691909_callee'}}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-typecheck-verify-swift -import-objc-header %S/Inputs/overload.h
2+
// REQUIRES: objc_interop
3+
protocol rdar29374163Proto {}
4+
5+
class rdar29374163 {
6+
init(_ itf: Rdar29374163Itf) { self.itf = itf }
7+
var itf: Rdar29374163Itf
8+
func bar() -> Int {
9+
return itf.foo as! Int
10+
}
11+
}

0 commit comments

Comments
 (0)