2
2
// RUN: %target-typecheck-verify-swift -typecheck -debug-generic-signatures %s > %t.dump 2>&1
3
3
// RUN: %FileCheck %s < %t.dump
4
4
5
+
6
+ func sameType< T> ( _: T . Type , _: T . Type ) { }
7
+
5
8
protocol P {
6
9
associatedtype A
7
10
typealias X = A
@@ -12,9 +15,9 @@ protocol Q {
12
15
13
16
// CHECK-LABEL: .requirementOnNestedTypeAlias@
14
17
// CHECK-NEXT: Requirements:
15
- // CHECK-NEXT: τ_0_0 : Q [Explicit @ 19 :51]
16
- // CHECK-NEXT: τ_0_0[.Q].B : P [Explicit @ 19 :51 -> Protocol requirement (Q)]
17
- // CHECK-NEXT: τ_0_0[.Q].B[.P].A == Int [Explicit @ 19 :62]
18
+ // CHECK-NEXT: τ_0_0 : Q [Explicit @ 22 :51]
19
+ // CHECK-NEXT: τ_0_0[.Q].B : P [Explicit @ 22 :51 -> Protocol requirement (Q)]
20
+ // CHECK-NEXT: τ_0_0[.Q].B[.P].A == Int [Explicit @ 22 :62]
18
21
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : Q, τ_0_0.B.A == Int>
19
22
func requirementOnNestedTypeAlias< T> ( _: T ) where T: Q , T. B. X == Int { }
20
23
@@ -31,18 +34,83 @@ protocol Q2 {
31
34
32
35
// CHECK-LABEL: .requirementOnConcreteNestedTypeAlias@
33
36
// CHECK-NEXT: Requirements:
34
- // CHECK-NEXT: τ_0_0 : Q2 [Explicit @ 39 :59]
35
- // CHECK-NEXT: τ_0_0[.Q2].B : P2 [Explicit @ 39 :59 -> Protocol requirement (Q2)]
36
- // CHECK-NEXT: τ_0_0[.Q2].C == S<T.B.A> [Explicit @ 39 :69]
37
+ // CHECK-NEXT: τ_0_0 : Q2 [Explicit @ 42 :59]
38
+ // CHECK-NEXT: τ_0_0[.Q2].B : P2 [Explicit @ 42 :59 -> Protocol requirement (Q2)]
39
+ // CHECK-NEXT: τ_0_0[.Q2].C == S<T.B.A> [Explicit @ 42 :69]
37
40
// CHECK-NEXT: τ_0_0[.Q2].B[.P2].X == S<T.B.A> [Nested type match]
38
41
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : Q2, τ_0_0.C == S<τ_0_0.B.A>>
39
42
func requirementOnConcreteNestedTypeAlias< T> ( _: T ) where T: Q2 , T. C == T . B . X { }
40
43
41
44
// CHECK-LABEL: .concreteRequirementOnConcreteNestedTypeAlias@
42
45
// CHECK-NEXT: Requirements:
43
- // CHECK-NEXT: τ_0_0 : Q2 [Explicit @ 48 :67]
44
- // CHECK-NEXT: τ_0_0[.Q2].B : P2 [Explicit @ 48 :67 -> Protocol requirement (Q2)]
46
+ // CHECK-NEXT: τ_0_0 : Q2 [Explicit @ 51 :67]
47
+ // CHECK-NEXT: τ_0_0[.Q2].B : P2 [Explicit @ 51 :67 -> Protocol requirement (Q2)]
45
48
// CHECK-NEXT: τ_0_0[.Q2].C == τ_0_0[.Q2].B[.P2].A [Explicit]
46
49
// CHECK-NEXT: τ_0_0[.Q2].B[.P2].X == S<T.B.A> [Nested type match]
47
50
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : Q2, τ_0_0.C == τ_0_0.B.A>
48
51
func concreteRequirementOnConcreteNestedTypeAlias< T> ( _: T ) where T: Q2 , S < T . C > == T . B . X { }
52
+
53
+
54
+ // Incompatable concrete typealias types are flagged as such
55
+ protocol P3 {
56
+ typealias T = Int // expected-error{{typealias 'T' requires types 'Int' and 'Float' to be the same}}
57
+ }
58
+ protocol Q3 : P3 {
59
+ typealias T = Float
60
+ }
61
+
62
+ protocol P3_1 {
63
+ typealias T = Float // expected-error{{typealias 'T' requires types 'Float' and 'Int' to be the same}}
64
+ }
65
+ protocol Q3_1 : P3 , P3_1 { }
66
+
67
+ // FIXME: these shouldn't be necessary to trigger the errors above, but are, due to
68
+ // the 'recusive decl validation' FIXME in GenericSignatureBuilder.cpp.
69
+ func useTypealias< T: Q3 > ( _: T , _: T . T ) { }
70
+ func useTypealias1< T: Q3_1 > ( _: T , _: T . T ) { }
71
+
72
+ // Subprotocols can force associated types in their parents to be concrete, and
73
+ // this should be understood for types constrained by the subprotocols.
74
+ protocol Q4 : P {
75
+ typealias A = Int
76
+ }
77
+ protocol Q5 : P {
78
+ typealias X = Int
79
+ }
80
+
81
+ // fully generic functions that manipulate the archetypes in a P
82
+ func getP_A< T: P > ( _: T . Type ) -> T . A . Type { return T . A. self }
83
+ func getP_X< T: P > ( _: T . Type ) -> T . X . Type { return T . X. self }
84
+
85
+ // ... which we use to check if the compiler is following through the concrete
86
+ // same-type constraints implied by the subprotocols.
87
+ func checkQ4_A< T: Q4 > ( x: T . Type ) { sameType ( getP_A ( x) , Int . self) }
88
+ func checkQ4_X< T: Q4 > ( x: T . Type ) { sameType ( getP_X ( x) , Int . self) }
89
+
90
+ // FIXME: these do not work, seemingly mainly due to the 'recursive decl validation'
91
+ // FIXME in GenericSignatureBuilder.cpp.
92
+ /*
93
+ func checkQ5_A<T: Q5>(x: T.Type) { sameType(getP_A(x), Int.self) }
94
+ func checkQ5_X<T: Q5>(x: T.Type) { sameType(getP_X(x), Int.self) }
95
+ */
96
+
97
+
98
+ // Typealiases happen to allow imposing same type requirements between parent
99
+ // protocols
100
+ protocol P6_1 {
101
+ associatedtype A
102
+ }
103
+ protocol P6_2 {
104
+ associatedtype B
105
+ }
106
+ protocol Q6 : P6_1 , P6_2 {
107
+ typealias A = B
108
+ }
109
+
110
+ func getP6_1_A< T: P6_1 > ( _: T . Type ) -> T . A . Type { return T . A. self }
111
+ func getP6_2_B< T: P6_2 > ( _: T . Type ) -> T . B . Type { return T . B. self }
112
+
113
+ func checkQ6< T: Q6 > ( x: T . Type ) {
114
+ sameType ( getP6_1_A ( x) , getP6_2_B ( x) )
115
+ }
116
+
0 commit comments