Skip to content

Commit 25034af

Browse files
authored
Merge pull request #21702 from slavapestov/gsb-same-type-order-5.0
Fix crash with redundant same-type constraints [5.0]
2 parents fd38e72 + a35dc74 commit 25034af

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6837,6 +6837,14 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
68376837
auto locA = (*a)->constraint.source->getLoc();
68386838
auto locB = (*b)->constraint.source->getLoc();
68396839

6840+
// Put invalid locations after valid ones.
6841+
if (locA.isInvalid() || locB.isInvalid()) {
6842+
if (locA.isInvalid() != locB.isInvalid())
6843+
return locA.isValid() ? 1 : -1;
6844+
6845+
return 0;
6846+
}
6847+
68406848
auto bufferA = sourceMgr.findBufferContainingLoc(locA);
68416849
auto bufferB = sourceMgr.findBufferContainingLoc(locB);
68426850

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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend -typecheck %s
2+
3+
protocol P1 {
4+
associatedtype A1
5+
}
6+
7+
protocol P2 {
8+
associatedtype A2
9+
}
10+
11+
struct S1<G1: P1, G2: P1>: P1 where G1.A1 == G2.A1 {
12+
typealias A1 = G1.A1
13+
}
14+
15+
struct S2<G1: P1, G2: P2>: P2 where G1.A1 == G2.A2 {
16+
typealias A2 = G2.A2
17+
}
18+
19+
struct S3<G1: P1, G2: P2> where G1.A1 == G2.A2 {
20+
func f<G: P1>(_: G) -> S3<S1<G, G1>, S2<G, G2>> {
21+
fatalError()
22+
}
23+
}

0 commit comments

Comments
 (0)