Skip to content

Commit 608b1b4

Browse files
committed
[ConstraintSystem] Allow solving same-type requirements for associated types
where the base does not conform to the associated type's protocol. We can only reach this case when the solver has already applied a fix for the conformance failure.
1 parent 5428060 commit 608b1b4

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,10 +3331,13 @@ bool ConstraintSystem::repairFailures(
33313331
if (lhs->hasHole() || rhs->hasHole())
33323332
return true;
33333333

3334-
// If dependent members are present here it's because
3335-
// base doesn't conform to associated type's protocol.
3336-
if (lhs->hasDependentMember() || rhs->hasDependentMember())
3337-
break;
3334+
// If dependent members are present here it's because the base doesn't
3335+
// conform to the associated type's protocol. We can only get here if we
3336+
// already applied a fix for the conformance failure.
3337+
if (lhs->hasDependentMember() || rhs->hasDependentMember()) {
3338+
increaseScore(SK_Fix);
3339+
return true;
3340+
}
33383341

33393342
// If requirement is something like `T == [Int]` let's let
33403343
// type matcher a chance to match generic parameters before
@@ -3949,12 +3952,14 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
39493952
llvm_unreachable("type variables should have already been handled by now");
39503953

39513954
case TypeKind::DependentMember: {
3952-
// If one of the dependent member types has no type variables,
3953-
// this comparison is effectively illformed, because dependent
3954-
// member couldn't be simplified down to the actual type, and
3955-
// we wouldn't be able to solve this constraint, so let's just fail.
3956-
if (!desugar1->hasTypeVariable() || !desugar2->hasTypeVariable())
3957-
return getTypeMatchFailure(locator);
3955+
// If one of the dependent member types has no type variables, the
3956+
// dependent member can't be simplified because the base doesn't conform
3957+
// to the associated type's protocol. We can only get here if we already
3958+
// applied a fix for the conformance failure.
3959+
if (!desugar1->hasTypeVariable() || !desugar2->hasTypeVariable()) {
3960+
increaseScore(SK_Fix);
3961+
return getTypeMatchSuccess();
3962+
}
39583963

39593964
// Nothing we can solve yet, since we need to wait until
39603965
// type variables will get resolved.

test/Constraints/same_types.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,25 @@ class R<T>: P7 where T: P7, T.A == T.Type { // expected-note {{'T' declared as p
333333

334334
R.fn(args: R.self) // expected-error {{generic parameter 'T' could not be inferred}}
335335
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}}
336+
337+
// rdar://problem/58607155
338+
protocol AssocType1 { associatedtype A }
339+
protocol AssocType2 { associatedtype A }
340+
341+
func rdar58607155() {
342+
func f<T1: AssocType1, T2: AssocType2>(t1: T1, t2: T2) where T1.A == T2.A {}
343+
// expected-note@-1 2 {{where 'T2' = 'MissingConformance'}}
344+
// expected-note@-2 2 {{where 'T1' = 'MissingConformance'}}
345+
346+
class Conformance: AssocType1, AssocType2 { typealias A = Int }
347+
class MissingConformance {}
348+
349+
// One generic argument has a conformance failure
350+
f(t1: MissingConformance(), t2: Conformance()) // expected-error {{local function 'f(t1:t2:)' requires that 'MissingConformance' conform to 'AssocType1'}}
351+
f(t1: Conformance(), t2: MissingConformance()) // expected-error {{local function 'f(t1:t2:)' requires that 'MissingConformance' conform to 'AssocType2'}}
352+
353+
// Both generic arguments have a conformance failure
354+
f(t1: MissingConformance(), t2: MissingConformance())
355+
// expected-error@-1 {{local function 'f(t1:t2:)' requires that 'MissingConformance' conform to 'AssocType1'}}
356+
// expected-error@-2 {{local function 'f(t1:t2:)' requires that 'MissingConformance' conform to 'AssocType2'}}
357+
}

0 commit comments

Comments
 (0)