Skip to content

Commit 5fa5ff7

Browse files
committed
RequirementMachine: Update tests for protocol typealiases
1 parent 7ee83a0 commit 5fa5ff7

File tree

9 files changed

+83
-25
lines changed

9 files changed

+83
-25
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public protocol P1 {
2+
typealias X = Y.Z
3+
associatedtype Y : P2
4+
}
5+
6+
public protocol P2 {
7+
associatedtype Z
8+
typealias C = Array<Z>
9+
}

test/Generics/protocol_type_aliases.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// RUN: %target-typecheck-verify-swift
2-
// RUN: %target-typecheck-verify-swift -debug-generic-signatures > %t.dump 2>&1
1+
// FIXME: Once the Requirement Machine can emit diagnostics, run this test with
2+
// it enabled unconditionally.
3+
4+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=off
5+
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on > %t.dump 2>&1
36
// RUN: %FileCheck %s < %t.dump
47

58

@@ -54,6 +57,9 @@ protocol Q3_1: P3, P3_1 {} // expected-error{{generic signature requires types '
5457

5558
// Subprotocols can force associated types in their parents to be concrete, and
5659
// this should be understood for types constrained by the subprotocols.
60+
61+
// CHECK-LABEL: .Q4@
62+
// CHECK: Requirement signature: <Self where Self : P, Self.[P]A == Int>
5763
protocol Q4: P {
5864
typealias A = Int // expected-warning{{typealias overriding associated type 'A' from protocol 'P'}}
5965
}
@@ -70,22 +76,21 @@ func getP_X<T: P>(_: T.Type) -> T.X.Type { return T.X.self }
7076
func checkQ4_A<T: Q4>(x: T.Type) { sameType(getP_A(x), Int.self) }
7177
func checkQ4_X<T: Q4>(x: T.Type) { sameType(getP_X(x), Int.self) }
7278

73-
// FIXME: these do not work, seemingly mainly due to the 'recursive decl validation'
74-
// FIXME in GenericSignatureBuilder.cpp.
75-
/*
7679
func checkQ5_A<T: Q5>(x: T.Type) { sameType(getP_A(x), Int.self) }
7780
func checkQ5_X<T: Q5>(x: T.Type) { sameType(getP_X(x), Int.self) }
78-
*/
7981

8082

81-
// Typealiases happen to allow imposing same type requirements between parent
82-
// protocols
83+
84+
// Typealiases allow imposing same type requirements between parent protocols
8385
protocol P6_1 {
8486
associatedtype A // expected-note{{'A' declared here}}
8587
}
8688
protocol P6_2 {
8789
associatedtype B
8890
}
91+
92+
// CHECK-LABEL: .Q6@
93+
// CHECK-NEXT: Requirement signature: <Self where Self : P6_1, Self : P6_2, Self.[P6_1]A == Self.[P6_2]B>
8994
protocol Q6: P6_1, P6_2 {
9095
typealias A = B // expected-warning{{typealias overriding associated type}}
9196
}
@@ -101,7 +106,10 @@ protocol P7 {
101106
typealias A = Int
102107
}
103108

109+
// CHECK-LABEL: .P7a@
110+
// CHECK: Requirement signature: <Self where Self : P7>
104111
protocol P7a : P7 {
105112
associatedtype A // expected-warning{{associated type 'A' is redundant with type 'A' declared in inherited protocol 'P7'}}
106113
}
107114

115+
func testP7A<T : P7a>(_: T, a: T.A) -> Int { return a }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=on
2+
3+
protocol P1 {
4+
typealias T = Array<U>
5+
associatedtype U
6+
}
7+
8+
protocol P2 {
9+
typealias T = Array<Int>
10+
}
11+
12+
func foo<T : P1 & P2>(_: T, u: T.U) -> Int { return u }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on 2>&1 | %FileCheck %s
2+
3+
// CHECK-LABEL: .P@
4+
// CHECK-NEXT: Requirement signature: <Self>
5+
protocol P {
6+
typealias T = Int
7+
associatedtype T
8+
}

test/Generics/protocol_typealias_same_type_requirement.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ protocol P7 {
4141
}
4242

4343
// CHECK-LABEL: protocol_typealias_same_type_requirement.(file).P8@
44-
// CHECK-LABEL: Requirement signature: <Self where Self : P6, Self : P7, Self.[P7]X == Int>
44+
// CHECK-LABEL: Requirement signature: <Self where Self : P6, Self : P7>
4545
protocol P8 : P6, P7 {}
46+
47+
func testP8<T : P8>(_: T, x: T.X) -> Int { return x }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module %S/Inputs/protocol_typealias_other.swift -emit-module-path %t/protocol_typealias_other.swiftmodule -module-name protocol_typealias_other -requirement-machine-protocol-signatures=on
3+
// RUN: %target-swift-frontend -typecheck %s -I %t -debug-generic-signatures -requirement-machine-inferred-signatures=on 2>&1 | %FileCheck %s
4+
5+
import protocol_typealias_other
6+
7+
// CHECK-LABEL: .useP1@
8+
// CHECK-NEXT: <T where T : P1, T.[P1]Y.[P2]Z == Int>
9+
func useP1<T : P1>(_: T) where T.X == Int {}
10+
11+
// CHECK-LABEL: .useP2@
12+
// CHECK-NEXT: <T where T : P2, T.[P2]Z == Int>
13+
func useP2<T : P2>(_: T) where T.C == Array<Int> {}
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
2-
3-
// References to associated type from 'where' clause should be
4-
// ambiguous when there's a typealias with the same name in another
5-
// unrelated protocol.
2+
// RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-inferred-signatures=on -requirement-machine-protocol-signatures=on 2>&1 | %FileCheck %s
63

74
protocol P1 {
85
typealias T = Int // expected-note 3{{found this candidate}}
@@ -12,35 +9,44 @@ protocol P2 {
129
associatedtype T // expected-note 3{{found this candidate}}
1310
}
1411

12+
// CHECK: ExtensionDecl line={{.*}} base=P1
13+
// CHECK-NEXT: Generic signature: <Self where Self : P1, Self : P2>
1514
extension P1 where Self : P2, T == Int { // expected-error {{'T' is ambiguous for type lookup in this context}}
1615
func takeT11(_: T) {} // expected-error {{'T' is ambiguous for type lookup in this context}}
1716
func takeT12(_: Self.T) {}
1817
}
1918

20-
extension P1 where Self : P2 {
21-
// FIXME: This doesn't make sense -- either both should
22-
// succeed, or both should be ambiguous.
23-
func takeT21(_: T) {} // expected-error {{'T' is ambiguous for type lookup in this context}}
19+
// CHECK: ExtensionDecl line={{.*}} base=P1
20+
// CHECK-NEXT: Generic signature: <Self where Self : P1, Self : P2>
21+
extension P1 where Self : P2, Self.T == Int {
22+
func takeT21(_: T) {}
2423
func takeT22(_: Self.T) {}
2524
}
2625

26+
extension P1 where Self : P2 {
27+
func takeT31(_: T) {} // expected-error {{'T' is ambiguous for type lookup in this context}}
28+
func takeT32(_: Self.T) {}
29+
}
30+
2731
// Same as above, but now we have two visible associated types with the same
2832
// name.
2933
protocol P3 {
3034
associatedtype T
3135
}
3236

37+
// CHECK: ExtensionDecl line={{.*}} base=P2
38+
// CHECK-NEXT: Generic signature: <Self where Self : P2, Self : P3, Self.[P2]T == Int>
3339
extension P2 where Self : P3, T == Int {
34-
func takeT31(_: T) {}
35-
func takeT32(_: Self.T) {}
40+
func takeT41(_: T) {}
41+
func takeT52(_: Self.T) {}
3642
}
3743

3844
extension P2 where Self : P3 {
39-
func takeT41(_: T) {}
40-
func takeT42(_: Self.T) {}
45+
func takeT61(_: T) {}
46+
func takeT62(_: Self.T) {}
4147
}
4248

4349
protocol P4 : P2, P3 {
44-
func takeT51(_: T)
45-
func takeT52(_: Self.T)
50+
func takeT71(_: T)
51+
func takeT72(_: Self.T)
4652
}

validation-test/Sema/type_checker_crashers_fixed/sr13856.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify
22

33
protocol OptionalType {
44
associatedtype Wrapped

validation-test/compiler_crashers_2_fixed/0142-rdar36549499.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend %s -emit-ir -o /dev/null
1+
// RUN: %target-swift-frontend %s -emit-ir -o /dev/null -requirement-machine-protocol-signatures=verify -requirement-machine-inferred-signatures=verify
22
protocol S {
33
associatedtype I: IteratorProtocol
44
typealias E = I.Element

0 commit comments

Comments
 (0)