Skip to content

Commit bfdafe6

Browse files
committed
Sema: Fix logic error in associated type inference solver
If matching a candidate value witness against a protocol requirement produced non-viable bindings, then don't consider the witness in the solver; it can never lead to a valid solution.
1 parent c999a87 commit bfdafe6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,11 @@ void AssociatedTypeInference::findSolutionsRec(
31163116
// looking for solutions involving each one.
31173117
const auto &inferredReq = inferred[reqDepth];
31183118
for (const auto &witnessReq : inferredReq.second) {
3119+
// If this witness had invalid bindings, don't consider it since it can
3120+
// never lead to a valid solution.
3121+
if (!witnessReq.NonViable.empty())
3122+
continue;
3123+
31193124
llvm::SaveAndRestore<unsigned> savedNumTypeWitnesses(numTypeWitnesses);
31203125

31213126
// If we had at least one tautological witness, we must consider the
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-typecheck-verify-swift -disable-experimental-associated-type-inference
2+
// RUN: %target-typecheck-verify-swift -enable-experimental-associated-type-inference
3+
4+
protocol P {
5+
associatedtype A: Q = Int
6+
7+
func f(_: A)
8+
}
9+
10+
protocol Q {}
11+
12+
extension Int: Q {}
13+
extension String: Q {}
14+
15+
struct S: P {
16+
// The presence of this overload where Float does not conform to Q would confuse
17+
// the solver and it would find two ambiguous solutions, A := Int and A := String.
18+
func f(_: Float) {}
19+
func f(_: String) {}
20+
}

0 commit comments

Comments
 (0)