1
- // RUN: %target-typecheck-verify-swift -disable-availability-checking
1
+ // RUN: %target-typecheck-verify-swift -target %target-swift-5.7-abi-triple -dump-ast | %FileCheck %s
2
+
3
+ /// Used to verify the type of an expression. Use like this:
4
+ /// ```
5
+ /// var types = SwiftTypePair(typeOf: expr, type2: SwiftType<Int>.self)
6
+ /// types.assertTypesAreEqual()
7
+ /// ```
8
+ struct SwiftType < T> { }
9
+ struct SwiftTypePair < T1, T2> {
10
+ init ( typeOf: T1 , type2: SwiftType < T2 > . Type ) { }
11
+
12
+ mutating func assertTypesAreEqual( ) where T1 == T2 { }
13
+ }
2
14
3
15
protocol Q { }
4
16
@@ -26,16 +38,20 @@ func acceptCollection<C: Collection>(_ c: C) -> C.Element { c.first! }
26
38
27
39
// --- Simple opening of existential values
28
40
func testSimpleExistentialOpening( p: any P , pq: any P & Q , c: any Collection ) {
41
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
29
42
let pa = acceptGeneric ( p)
30
43
let _: Int = pa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
31
44
32
45
var vp = p
46
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
33
47
let vpa = acceptGeneric ( vp)
34
48
let _: Int = vpa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
35
49
50
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
36
51
let pqa = acceptGeneric ( pq)
37
52
let _: Int = pqa // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
38
53
54
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
39
55
let element = acceptCollection ( c)
40
56
let _: Int = element // expected-error{{cannot convert value of type 'Any' to specified type 'Int'}}
41
57
}
@@ -50,6 +66,7 @@ func takeCollectionOfPs<C: Collection>(_: C) -> C.Element.A?
50
66
}
51
67
52
68
func testCollectionOfPs( cp: any CollectionOfPs ) {
69
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
53
70
let e = takeCollectionOfPs ( cp)
54
71
let _: Int = e // expected-error{{cannot convert value of type '(any Q)?' to specified type 'Int'}}
55
72
}
@@ -62,9 +79,11 @@ extension P {
62
79
}
63
80
64
81
func testMultipleOpened( a: any P , b: any P & Q ) {
82
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
65
83
let r1 = takeTwoGenerics ( a, b)
66
84
let _: Int = r1 // expected-error{{cannot convert value of type '(any P, any P & Q)' to specified type 'Int'}}
67
85
86
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
68
87
let r2 = a. combineThePs ( b)
69
88
let _: Int = r2 // expected-error{{cannot convert value of type '(any Q, any Q)?' to specified type 'Int'}}
70
89
}
@@ -75,6 +94,7 @@ func conjureValue<T: P>(of type: T.Type) -> T? {
75
94
}
76
95
77
96
func testMagic( pt: any P . Type ) {
97
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
78
98
let pOpt = conjureValue ( of: pt)
79
99
let _: Int = pOpt // expected-error{{cannot convert value of type '(any P)?' to specified type 'Int'}}
80
100
}
@@ -162,13 +182,15 @@ func takesInOut<T: P>(_ value: inout T) { }
162
182
163
183
func passesInOut( i: Int ) {
164
184
var p : any P = i
185
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
165
186
takesInOut ( & p)
166
187
}
167
188
168
189
func takesOptional< T: P > ( _ value: T ? ) { }
169
190
// expected-note@-1{{required by global function 'takesOptional' where 'T' = 'any P'}}
170
191
171
192
func passesToOptional( p: any P , pOpt: ( any P ) ? ) {
193
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
172
194
takesOptional ( p) // okay
173
195
takesOptional ( pOpt) // expected-error{{type 'any P' cannot conform to 'P'}}
174
196
// expected-note@-1{{only concrete types such as structs, enums and classes can conform to protocols}}
@@ -177,20 +199,24 @@ func passesToOptional(p: any P, pOpt: (any P)?) {
177
199
178
200
@available ( SwiftStdlib 5 . 1 , * )
179
201
func testReturningOpaqueTypes( p: any P ) {
202
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
180
203
let q = p. getQ ( )
181
204
let _: Int = q // expected-error{{cannot convert value of type 'any Q' to specified type 'Int'}}
182
205
183
206
p. getCollectionOf ( ) // expected-error{{member 'getCollectionOf' cannot be used on value of type 'any P'; consider using a generic constraint instead}}
184
207
208
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
185
209
let q2 = getPQ ( p)
186
210
let _: Int = q2 // expected-error{{cannot convert value of type 'any Q' to specified type 'Int'}}
187
211
188
212
getCollectionOfP ( p) // expected-error{{type 'any P' cannot conform to 'P'}}
189
213
// expected-note@-1{{only concrete types such as structs, enums and classes can conform to protocols}}
190
214
215
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
191
216
let fi = funnyIdentity ( p)
192
217
let _: Int = fi // expected-error{{cannot convert value of type '(any P)?' to specified type 'Int'}}
193
218
219
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
194
220
_ = arrayOfOne ( p) // okay, arrays are covariant in their argument
195
221
196
222
_ = createX ( p) // expected-error{{type 'any P' cannot conform to 'P'}}
@@ -210,13 +236,20 @@ func overloadedGenericFunctionTakingP<T: P>(_: T) { }
210
236
211
237
func testTakeValueAndClosure( p: any P ) {
212
238
// Type-erase when not provided with a generic function.
239
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
213
240
takeValueAndClosure ( p) { x in
214
- print ( x)
215
- let _: Int = x // expected-error{{cannot convert value of type 'any P' to specified type 'Int'}}
241
+ var types = SwiftTypePair ( typeOf: x, type2: SwiftType < any P > . self)
242
+ types. assertTypesAreEqual ( )
243
+
244
+ return ( )
216
245
}
217
246
218
247
// Do not erase when referring to a generic function.
248
+ // FIXME:
249
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
219
250
takeValueAndClosure ( p, body: genericFunctionTakingP)
251
+ // FIXME:
252
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
220
253
takeValueAndClosure ( p, body: overloadedGenericFunctionTakingP)
221
254
takeValueAndClosure ( p, body: genericFunctionTakingPQ) // expected-error{{global function 'genericFunctionTakingPQ' requires that 'T' conform to 'Q'}}
222
255
@@ -255,32 +288,47 @@ func testExplicitCoercionRequirement(v: any B, otherV: any B & D) {
255
288
func overloaded< T: B > ( _: T ) -> ( x: Int , y: T . C ) { fatalError ( ) }
256
289
func overloaded< T: P > ( _: T ) -> Int { 42 }
257
290
291
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
258
292
_ = getC ( v) // Ok
293
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
259
294
_ = getC ( v) as any P // Ok
260
-
295
+
296
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
261
297
_ = getE ( v) // Ok
298
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
262
299
_ = getE ( v) as any P1 < Double > // Ok
263
-
300
+
301
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
264
302
_ = getTuple ( v) // Ok
303
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
265
304
_ = getTuple ( v) as ( any B , any P ) // Ok
266
305
// Ok because T.C.A == Double
306
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
267
307
_ = getNoError ( v)
268
308
309
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
269
310
_ = getComplex ( v) // Ok
311
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
270
312
_ = getComplex ( v) as ( [ ( x: ( a: any P , b: Int ) , y: Int ) ] , [ Int : any P ] ) // Ok
271
313
314
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
272
315
_ = overloaded ( v) // Ok
273
316
274
317
func acceptsAny< T> ( _: T ) { }
275
318
319
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
276
320
acceptsAny ( getC ( v) ) // Ok
321
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
277
322
acceptsAny ( getC ( v) as any P ) // Ok
278
323
324
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
279
325
acceptsAny ( getComplex ( v) ) // Ok
326
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
280
327
acceptsAny ( getComplex ( v) as ( [ ( x: ( a: any P , b: Int ) , y: Int ) ] , [ Int : any P ] ) )
281
328
282
329
func getAssocNoRequirements< T: B > ( _: T ) -> ( Int , [ T . D ] ) { fatalError ( ) }
283
330
331
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
284
332
_ = getAssocNoRequirements ( v) // Ok, `D` doesn't have any requirements
285
333
286
334
// Test existential opening from protocol extension access
@@ -292,24 +340,35 @@ func testExplicitCoercionRequirement(v: any B, otherV: any B & D) {
292
340
293
341
func getF< T: D > ( _: T ) -> T . E { fatalError ( ) }
294
342
343
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
295
344
_ = getF ( otherV) // Ok `E` doesn't have a `where` clause
296
345
297
346
func getSelf< T: B > ( _: T ) -> T { fatalError ( ) } // expected-note {{found this candidate}}
298
347
func getSelf< T: D > ( _: T ) -> T { fatalError ( ) } // expected-note {{found this candidate}}
299
348
349
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
300
350
_ = getSelf ( v) // Ok
351
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
301
352
_ = getSelf ( v) as any B // Ok
302
353
_ = getSelf ( otherV) as any B & D // expected-error {{ambiguous use of 'getSelf'}}
303
354
304
355
func getBDSelf< T: D > ( _: T ) -> T { fatalError ( ) }
356
+
357
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
305
358
_ = getBDSelf ( otherV) // Ok
359
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
306
360
_ = getBDSelf ( otherV) as any B & D // Ok
307
361
308
362
func getP< T: P > ( _: T ) { }
363
+
364
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
309
365
getP ( getC ( v) ) // Ok
366
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
310
367
getP ( v. getC ( ) ) // Ok
311
368
369
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
312
370
getP ( ( getC ( v) as any P ) ) // Ok - parens avoid opening suppression
371
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
313
372
getP ( ( v. getC ( ) as any P ) ) // Ok - parens avoid opening suppression
314
373
}
315
374
@@ -417,9 +476,25 @@ struct S<T, U> {
417
476
}
418
477
}
419
478
420
- func nestedMetatypeCallee< T> ( _ t: T ) { }
479
+ do {
480
+ func nestedMetatypeCallee< T> ( _ t: T ) { }
421
481
422
- func nestedMetatypeCaller( ) {
423
482
let t = String . Type. Type. Type. self as ( any Q . Type . Type . Type . Type )
483
+ // CHECK-NOT: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
424
484
nestedMetatypeCallee ( t)
425
485
}
486
+
487
+ do {
488
+ protocol P { }
489
+
490
+ func foo< T: P > ( _ m: inout T . Type ) { }
491
+
492
+ // expected-note@+1 {{change 'let' to 'var' to make it mutable}}
493
+ let rValueP : P . Type
494
+ var lValueP : P . Type
495
+
496
+ // expected-error@+1 {{cannot pass immutable value as inout argument: 'rValueP' is a 'let' constant}}
497
+ foo ( & rValueP)
498
+ // CHECK: open_existential_expr {{.*}} location={{.*}}:[[@LINE+1]]:{{[0-9]+}} range=
499
+ foo ( & lValueP)
500
+ }
0 commit comments