- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathopaque.swift
618 lines (507 loc) · 20.1 KB
/
opaque.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// RUN: %target-swift-frontend -disable-availability-checking -typecheck -verify %s
protocolP{
func paul()
mutatingfunc priscilla()
}
protocolQ{func quinn()}
extensionInt:P,Q{func paul(){}; mutatingfunc priscilla(){}; func quinn(){}}
extensionString:P,Q{func paul(){}; mutatingfunc priscilla(){}; func quinn(){}}
extensionArray:P,Q{func paul(){}; mutatingfunc priscilla(){}; func quinn(){}}
classC{}
classD:C,P,Q{func paul(){}; func priscilla(){}; func quinn(){}; func d(){}}
letproperty:someP=1
letdeflessLet:someP // expected-error{{has no initializer}} {{documentation-file=opaque-type-inference}}
vardeflessVar:someP // expected-error{{has no initializer}}
structGenericProperty<T:P>{
varx:T
varproperty:someP{
return x
}
}
let(bim, bam):someP=(1,2) // expected-error{{'some' type can only be declared on a single property declaration}}
varcomputedProperty:someP{
get{return1}
set{ _ = newValue +1} // expected-error{{cannot convert value of type 'some P' to expected argument type 'Int'}}
}
structSubscriptTest{
subscript(_ x:Int)->someP{
return x
}
}
func bar()->someP{
return1
}
func bas()->someP&Q{
return1
}
func zim()->someC{
returnD()
}
func zang()->someC&P&Q{
returnD()
}
func zung()->someAnyObject{
returnD()
}
func zoop()->someAny{
returnD()
}
func zup()->someAny&P{
returnD()
}
func zip()->someAnyObject&P{
returnD()
}
func zorp()->someAny&C&P{
returnD()
}
func zlop()->someC&AnyObject&P{
returnD()
}
// Don't allow opaque types to propagate by inference into other global decls'
// types
structTest{
letinferredOpaque=bar() // expected-error{{inferred type}}
letinferredOpaqueStructural=Optional(bar()) // expected-error{{inferred type}}
letinferredOpaqueStructural2=(bar(),bas()) // expected-error{{inferred type}}
}
letzingle={()->somePin1} // expected-error{{'some' types are only permitted}}
func twoOpaqueTypes()->(someP,someP){return(1,2)}
func asArrayElem()->[someP]{return[1]}
// Invalid positions
typealiasFoo=someP // expected-error{{'some' types are only permitted}}
func blibble(blobble:someP){}
func blib()->P&someQ{return1} // expected-error{{'some' should appear at the beginning}}
func blab()->someP?{return1} // expected-error{{must specify only}} expected-note{{did you mean to write an optional of an 'some' type?}}
func blorb<T:someP>(_:T){} // expected-error{{'some' types are only permitted}}
func blub<T>()->Twhere T ==someP{return1} // expected-error{{'some' types are only permitted}}
protocolOP:someP{} // expected-error{{'some' types are only permitted}}
func foo()->someP{
letx=(someP).self // expected-error*{{}}
return1
}
// Invalid constraints
letzug:someInt=1 // FIXME expected-error{{must specify only}}
letzwang:some()=() // FIXME expected-error{{must specify only}}
letzwoggle:some(()->())={} // FIXME expected-error{{must specify only}}
// Type-checking of expressions of opaque type
func alice()->someP{return1}
func bob()->someP{return1}
func grace<T:P>(_ x:T)->someP{return x }
func typeIdentity(){
do{
vara=alice()
a =alice()
a =bob() // expected-error{{}}
a =grace(1) // expected-error{{}}
a =grace("two") // expected-error{{}}
}
do{
varaf= alice
af = alice
af = bob // expected-error{{}}
af = grace // expected-error{{generic parameter 'T' could not be inferred}}
// expected-error@-1 {{cannot assign value of type '(T) -> some P' to type '() -> some P'}}
}
do{
varb=bob()
b =alice() // expected-error{{}}
b =bob()
b =grace(1) // expected-error{{}}
b =grace("two") // expected-error{{}}
}
do{
vargi=grace(1)
gi =alice() // expected-error{{}}
gi =bob() // expected-error{{}}
gi =grace(2)
gi =grace("three") // expected-error{{}}
}
do{
vargs=grace("one")
gs =alice() // expected-error{{}}
gs =bob() // expected-error{{}}
gs =grace(2) // expected-error{{}}
gs =grace("three")
}
// The opaque type should conform to its constraining protocols
do{
letgs=grace("one")
varggs=grace(gs)
ggs =grace(gs)
}
// The opaque type should expose the members implied by its protocol
// constraints
do{
vara=alice()
a.paul()
a.priscilla()
}
}
func recursion(x:Int)->someP{
if x ==0{
return0
}
returnrecursion(x: x -1)
}
func noReturnStmts()->someP{} // expected-error {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}} {{documentation-file=opaque-type-inference}}
func returnUninhabited()->someP{ // expected-note {{opaque return type declared here}}
fatalError() // expected-error{{return type of global function 'returnUninhabited()' requires that 'Never' conform to 'P'}}
}
func mismatchedReturnTypes(_ x:Bool, _ y:Int, _ z:String)->someP{ // expected-error{{do not have matching underlying types}} {{documentation-file=opaque-type-inference}}
if x {
return y // expected-note{{underlying type 'Int'}}
}else{
return z // expected-note{{underlying type 'String'}}
}
}
varmismatchedReturnTypesProperty:someP{ // expected-error{{do not have matching underlying types}}
iftrue{
return0 // expected-note{{underlying type 'Int'}}
}else{
return"" // expected-note{{underlying type 'String'}}
}
}
structMismatchedReturnTypesSubscript{
subscript(x:Bool, y:Int, z:String)->someP{ // expected-error{{do not have matching underlying types}}
if x {
return y // expected-note{{underlying type 'Int'}}
}else{
return z // expected-note{{underlying type 'String'}}
}
}
}
func jan()->someP{
return[marcia(),marcia(),marcia()]
}
func marcia()->someP{
return[marcia(),marcia(),marcia()] // expected-error{{defines the opaque type in terms of itself}} {{documentation-file=opaque-type-inference}}
}
protocolR{
associatedtypeS:P, Q // expected-note*{{}}
func r_out()->S
func r_in(_:S)
}
extensionInt:R{
func r_out()->String{
return""
}
func r_in(_:String){}
}
func candace()->someR{
return0
}
func doug()->someR{
return0
}
func gary<T:R>(_ x:T)->someR{
return x
}
func sameType<T>(_:T, _:T){}
func associatedTypeIdentity(){
letc=candace()
letd=doug()
varcr= c.r_out()
cr =candace().r_out()
cr =doug().r_out() // expected-error{{}}
vardr= d.r_out()
dr =candace().r_out() // expected-error{{}}
dr =doug().r_out()
c.r_in(cr)
c.r_in(c.r_out())
c.r_in(dr) // expected-error{{}}
c.r_in(d.r_out()) // expected-error{{}}
d.r_in(cr) // expected-error{{}}
d.r_in(c.r_out()) // expected-error{{}}
d.r_in(dr)
d.r_in(d.r_out())
cr.paul()
cr.priscilla()
cr.quinn()
dr.paul()
dr.priscilla()
dr.quinn()
sameType(cr, c.r_out())
sameType(dr, d.r_out())
sameType(cr, dr) // expected-error {{conflicting arguments to generic parameter 'T' ('(some R).S' (result type of 'candace') vs. '(some R).S' (result type of 'doug'))}}
sameType(gary(candace()).r_out(),gary(candace()).r_out())
sameType(gary(doug()).r_out(),gary(doug()).r_out())
sameType(gary(doug()).r_out(),gary(candace()).r_out())
// expected-error@-1 {{conflicting arguments to generic parameter 'T' ('some R' (result type of 'doug') vs. 'some R' (result type of 'candace'))}}
// expected-error@-2 {{conflicting arguments to generic parameter 'T' ('some R' (result type of 'doug') vs. 'some R' (result type of 'candace'))}}
}
func redeclaration()->someP{return0} // expected-note 2{{previously declared}}
func redeclaration()->someP{return0} // expected-error{{redeclaration}}
func redeclaration()->someQ{return0} // expected-error{{redeclaration}}
func redeclaration()->P{return0}
func redeclaration()->Any{return0}
varredeclaredProp:someP{return0} // expected-note 3{{previously declared}}
varredeclaredProp:someP{return0} // expected-error{{redeclaration}}
varredeclaredProp:someQ{return0} // expected-error{{redeclaration}}
varredeclaredProp:P{return0} // expected-error{{redeclaration}}
structRedeclarationTest{
func redeclaration()->someP{return0} // expected-note 2{{previously declared}}
func redeclaration()->someP{return0} // expected-error{{redeclaration}}
func redeclaration()->someQ{return0} // expected-error{{redeclaration}}
func redeclaration()->P{return0}
varredeclaredProp:someP{return0} // expected-note 3{{previously declared}}
varredeclaredProp:someP{return0} // expected-error{{redeclaration}}
varredeclaredProp:someQ{return0} // expected-error{{redeclaration}}
varredeclaredProp:P{return0} // expected-error{{redeclaration}}
subscript(redeclared _:Int)->someP{return0} // expected-note 2{{previously declared}}
subscript(redeclared _:Int)->someP{return0} // expected-error{{redeclaration}}
subscript(redeclared _:Int)->someQ{return0} // expected-error{{redeclaration}}
subscript(redeclared _:Int)->P{return0}
}
func diagnose_requirement_failures(){
structS{
varfoo:someP{returnS()} // expected-note {{declared here}}
// expected-error@-1 {{return type of property 'foo' requires that 'S' conform to 'P'}}
subscript(_:Int)->someP{ // expected-note {{declared here}}
returnS()
// expected-error@-1 {{return type of subscript 'subscript(_:)' requires that 'S' conform to 'P'}}
}
func bar()->someP{ // expected-note {{declared here}}
returnS()
// expected-error@-1 {{return type of instance method 'bar()' requires that 'S' conform to 'P'}}
}
staticfunc baz(x:String)->someP{ // expected-note {{declared here}}
returnS()
// expected-error@-1 {{return type of static method 'baz(x:)' requires that 'S' conform to 'P'}}
}
}
func fn()->someP{ // expected-note {{declared here}}
returnS()
// expected-error@-1 {{return type of local function 'fn()' requires that 'S' conform to 'P'}}
}
}
func global_function_with_requirement_failure()->someP{ // expected-note {{declared here}}
return42asDouble
// expected-error@-1 {{return type of global function 'global_function_with_requirement_failure()' requires that 'Double' conform to 'P'}}
}
func recursive_func_is_invalid_opaque(){
func rec(x:Int)->someP{
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
if x ==0{
returnrec(x:0)
}
returnrec(x: x -1)
}
}
func closure()->someP{
_ ={
return"test"
}
return42
}
protocolHasAssocType{
associatedtypeAssoc
func assoc()->Assoc
}
structGenericWithOpaqueAssoc<T>:HasAssocType{
func assoc()->someAny{return0}
}
structOtherGeneric<X, Y, Z>{
varx:GenericWithOpaqueAssoc<X>.Assoc
vary:GenericWithOpaqueAssoc<Y>.Assoc
varz:GenericWithOpaqueAssoc<Z>.Assoc
}
protocolP_51641323{
associatedtypeT
varfoo:Self.T{get}
}
func rdar_51641323(){
structFoo:P_51641323{
varfoo:someP_51641323{ // expected-note {{required by opaque return type of property 'foo'}}
{} // expected-error {{type '() -> ()' cannot conform to 'P_51641323'}} expected-note {{only concrete types such as structs, enums and classes can conform to protocols}}
}
}
}
// Protocol requirements cannot have opaque return types
protocolOpaqueProtocolRequirement{
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: P\n}}{{21-27=<#AssocType#>}}
func method1()->someP
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: C & P & Q\n}}{{21-35=<#AssocType#>}}
func method2()->someC&P&Q
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: Nonsense\n}}{{21-34=<#AssocType#>}}
func method3()->someNonsense
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: P\n}}{{13-19=<#AssocType#>}}
varprop:someP{get}
// expected-error@+1 {{cannot be the return type of a protocol requirement}}{{3-3=associatedtype <#AssocType#>: P\n}}{{18-24=<#AssocType#>}}
subscript()->someP{get}
}
func testCoercionDiagnostics(){
varopaque=foo()
opaque =bar() // expected-error {{cannot assign value of type 'some P' (result of 'bar()') to type 'some P' (result of 'foo()')}} {{none}}
opaque =() // expected-error {{cannot assign value of type '()' to type 'some P'}} {{none}}
opaque = computedProperty // expected-error {{cannot assign value of type 'some P' (type of 'computedProperty') to type 'some P' (result of 'foo()')}} {{none}}
opaque =SubscriptTest()[0] // expected-error {{cannot assign value of type 'some P' (result of 'SubscriptTest.subscript(_:)') to type 'some P' (result of 'foo()')}} {{none}}
varopaqueOpt:Optional= opaque
opaqueOpt =bar() // expected-error {{cannot assign value of type 'some P' (result of 'bar()') to type 'some P' (result of 'foo()')}} {{none}}
opaqueOpt =() // expected-error {{cannot assign value of type '()' to type 'some P'}} {{none}}
}
varglobalVar:someP=17
letglobalLet:someP=38
structFoo{
staticvarstaticVar:someP=17
staticletstaticLet:someP=38
varinstanceVar:someP=17
letinstanceLet:someP=38
}
protocolP_52528543{
init()
associatedtypeA:Q_52528543
vara:A{get}
}
protocolQ_52528543{
associatedtypeB // expected-note 2 {{associated type 'B'}}
varb:B{get}
}
extensionP_52528543{
func frob(a_b:A.B)->someP_52528543{returnself}
}
func foo<T:P_52528543>(x:T)->someP_52528543{
return x
.frob(a_b: x.a.b)
.frob(a_b: x.a.b) // expected-error {{cannot convert}}
}
structGenericFoo<T:P_52528543, U:P_52528543>{
letx:someP_52528543=T()
lety:someP_52528543=U()
mutatingfunc bump(){
varxab=f_52528543(x: x)
xab =f_52528543(x: y) // expected-error{{cannot assign}}
}
}
func f_52528543<T:P_52528543>(x:T)->T.A.B{return x.a.b }
func opaque_52528543<T:P_52528543>(x:T)->someP_52528543{return x }
func invoke_52528543<T:P_52528543, U:P_52528543>(x:T, y:U){
letx2=opaque_52528543(x: x)
lety2=opaque_52528543(x: y)
varxab=f_52528543(x: x2)
xab =f_52528543(x: y2) // expected-error{{cannot assign}}
}
protocolProto{}
structI:Proto{}
dynamicfunc foo<S>(_ s:S)->someProto{
returnI()
}
@_dynamicReplacement(for: foo)
func foo_repl<S>(_ s:S)->someProto{
returnI()
}
protocolSomeProtocolA{}
protocolSomeProtocolB{}
protocolSomeProtocolC{}
structSomeStructC:SomeProtocolA,SomeProtocolB,SomeProtocolC{}
letsomeProperty:SomeProtocolA&someSomeProtocolB=SomeStructC() // expected-error {{'some' should appear at the beginning of a composition}}{{35-40=}}{{19-19=some }}
letsomeOtherProperty:someSomeProtocolA&someSomeProtocolB=SomeStructC() // expected-error {{'some' should appear at the beginning of a composition}}{{45-50=}}
letsomeThirdProperty:someSomeProtocolA&SomeProtocolB&someSomeProtocolC=SomeStructC() // expected-error {{'some' should appear at the beginning of a composition}}{{61-66=}}
// An opaque result type on a protocol extension member effectively
// contains an invariant reference to 'Self', and therefore cannot
// be referenced on an existential type.
protocolOpaqueProtocol{}
extensionOpaqueProtocol{
varasSome:someOpaqueProtocol{returnself}
func getAsSome()->someOpaqueProtocol{returnself}
subscript(_:Int)->someOpaqueProtocol{returnself}
}
func takesOpaqueProtocol(existential:OpaqueProtocol){
// These are okay because we erase to the opaque type bound
leta= existential.asSome
let _:Int= a // expected-error{{cannot convert value of type 'any OpaqueProtocol' to specified type 'Int'}}
_ = existential.getAsSome()
_ =existential[0]
}
func takesOpaqueProtocol<T :OpaqueProtocol>(generic:T){
// these are all OK:
_ = generic.asSome
_ = generic.getAsSome()
_ =generic[0]
}
func opaquePlaceholderFunc()->some_{1} // expected-error {{type placeholder not allowed here}}
varopaquePlaceholderVar:some_=1 // expected-error {{type placeholder not allowed here}}
// rdar://90456579 - crash in `OpaqueUnderlyingTypeChecker`
func test_diagnostic_with_contextual_generic_params(){
structS{
func test<T:Q>(t:T)->someQ{
// expected-error@-1 {{function declares an opaque return type 'some Q', but the return statements in its body do not have matching underlying types}}
iftrue{
return t // expected-note {{return statement has underlying type 'T'}}
}
return"" // String conforms to `Q`
// expected-note@-1 {{return statement has underlying type 'String'}}
}
}
}
// https://github.com/apple/swift/issues/53378
// Suggest `return` when the last statement of a multi-statement function body
// would be a valid return value
protocolP1{
}
protocolP2{
}
do{
func test()->someNumeric{
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
letx=0
x // expected-note {{did you mean to return the last expression?}} {{5-5=return }}
// expected-warning@-1 {{expression of type 'Int' is unused}}
}
func test2()->someNumeric{
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
letx="s"
x // expected-warning {{expression of type 'String' is unused}}
}
structS1:P1,P2{
}
structS2:P1{
}
func test3()->someP1&P2{
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
letx=S1()
x // expected-note {{did you mean to return the last expression?}} {{5-5=return }}
// expected-warning@-1 {{expression of type 'S1' is unused}}
}
func test4()->someP1&P2{
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
letx=S2()
x // expected-warning {{expression of type 'S2' is unused}}
}
func test5()->someP1{
// expected-error@-1 {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
letx= invalid // expected-error {{cannot find 'invalid' in scope}}
x
}
}
// https://github.com/apple/swift/issues/62787
func f62787()->Optional<someCollection<Int>>{
returnnil // expected-error{{underlying type for opaque result type 'Optional<some Collection<Int>>' could not be inferred from return expression}}
}
func f62787_1(x:Bool)->Optional<someCollection<Int>>{
if x {
returnnil // expected-error{{underlying type for opaque result type 'Optional<some Collection<Int>>' could not be inferred from return expression}}
}
returnnil // expected-error{{underlying type for opaque result type 'Optional<some Collection<Int>>' could not be inferred from return expression}}
}
// rdar://124482122 - Make sure that constraints are respected by opaque types
protocolP3<A>{
associatedtypeA:P1
}
do{
structG<A:P1>:P3{}
structS:P1{}
classA{}
func test1()->someP3<Int>{ // expected-note {{opaque return type declared here}}
returnG<S>()
// expected-error@-1 {{return type of local function 'test1()' requires the types 'S' and 'Int' be equivalent}}
}
func test2()->someP3<G<S>>{ // expected-note {{opaque return type declared here}}
returnG<S>()
// expected-error@-1 {{return type of local function 'test2()' requires the types 'S' and 'G<S>' be equivalent}}
}
func test3()->someP1&A{ // expected-note {{opaque return type declared here}}
S()
// expected-error@-1 {{return type of local function 'test3()' requires that 'S' inherit from 'A'}}
}
}