Skip to content

Commit a35dc74

Browse files
committed
AST: Fix failure to diagnose with redundant same-type constraints
This test case used to crash because the source-location-based order here did not handle invalid source locations, but Doug fixed it already in #21656. However, fixing it to sort invalid locations after valid locations meant that the diagnostic code would pick the invalid location to emit the diagnostic on, so no diagnostic was emitted. This is not a big deal since the diagnostics in question are warnings, but we do want to emit these redundant constraint warnings to avoid confusing users if possible so let's fix it to do the right thing. More completely fixes rdar://problem/46848889 (but it was already not crashing after Doug's fix).
1 parent 83b3fcb commit a35dc74

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6840,7 +6840,7 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
68406840
// Put invalid locations after valid ones.
68416841
if (locA.isInvalid() || locB.isInvalid()) {
68426842
if (locA.isInvalid() != locB.isInvalid())
6843-
return locA.isInvalid() ? 1 : -1;
6843+
return locA.isValid() ? 1 : -1;
68446844

68456845
return 0;
68466846
}

test/Constraints/same_types.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,14 @@ func testSameTypeCommutativity5<U, T: P1>(_ t: T, _ u: U)
309309
func testSameTypeCommutativity6<U, T: P1>(_ t: T, _ u: U)
310310
where U & P3 == T.Assoc { } // Equivalent to T.Assoc == U & P3
311311
// expected-error@-1 2 {{non-protocol, non-class type 'U' cannot be used within a protocol-constrained type}}
312+
313+
// rdar;//problem/46848889
314+
struct Foo<A: P1, B: P1, C: P1> where A.Assoc == B.Assoc, A.Assoc == C.Assoc {}
315+
316+
struct Bar<A: P1, B: P1> where A.Assoc == B.Assoc {
317+
func f<C: P1>(with other: C) -> Foo<A, B, C> where A.Assoc == C.Assoc {
318+
// expected-note@-1 {{previous same-type constraint 'B.Assoc' == 'C.Assoc' inferred from type here}}
319+
// expected-warning@-2 {{redundant same-type constraint 'A.Assoc' == 'C.Assoc'}}
320+
fatalError()
321+
}
322+
}

0 commit comments

Comments
 (0)