- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathenum.swift
527 lines (425 loc) · 20.3 KB
/
enum.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
// RUN: %target-typecheck-verify-swift
enumEmpty{}
enumBoolish{
case falsy
case truthy
init(){self=.falsy }
}
varb=Boolish.falsy
b =.truthy
enumOptionable<T>{
case Nought
case Mere(T)
}
varo= Optionable<Int>.Nought
o =.Mere(0)
enumColor{case Red, Green, Grayscale(Int), Blue }
varc=Color.Red
c =.Green
c =.Grayscale(255)
c =.Blue
letpartialApplication=Color.Grayscale
// Cases are excluded from non-enums.
case FloatingCase // expected-error{{enum 'case' is not allowed outside of an enum}}
structSomeStruct{
case StructCase // expected-error{{enum 'case' is not allowed outside of an enum}}
}
classSomeClass{
case ClassCase // expected-error{{enum 'case' is not allowed outside of an enum}}
}
enumEnumWithExtension1{
case A1
}
extensionEnumWithExtension1{
case A2 // expected-error{{enum 'case' is not allowed outside of an enum}}
}
// Attributes for enum cases.
enumEnumCaseAttributes{
@xyzcase EmptyAttributes // expected-error {{unknown attribute 'xyz'}}
}
// Recover when a switch 'case' label is spelled inside an enum (or outside).
enumSwitchEnvy{
case X: // expected-error{{'case' label can only appear inside a 'switch' statement}}
case X(Y): // expected-error{{'case' label can only appear inside a 'switch' statement}}
case X, Y: // expected-error{{'case' label can only appear inside a 'switch' statement}}
case X where true: // expected-error{{'case' label can only appear inside a 'switch' statement}}
case X(Y), Z(W): // expected-error{{'case' label can only appear inside a 'switch' statement}}
case X(Y)where true: // expected-error{{'case' label can only appear inside a 'switch' statement}}
case 0: // expected-error{{'case' label can only appear inside a 'switch' statement}}
case _: // expected-error{{'case' label can only appear inside a 'switch' statement}}
case(_,varx,0): // expected-error{{'case' label can only appear inside a 'switch' statement}}
}
enumHasMethodsPropertiesAndCtors{
case TweedleDee
case TweedleDum
func method(){}
func staticMethod(){}
init(){}
subscript(x:Int)->Int{
return0
}
varproperty:Int{
return0
}
}
enumImproperlyHasIVars{
case Flopsy
case Mopsy
varivar:Int // expected-error{{enums must not contain stored properties}}
}
// We used to crash on this. rdar://14678675
enumrdar14678675{
case U1, // expected-error{{expected identifier after comma in enum 'case' declaration}}
case U2
case U3
}
enumRecovery1{
case: // expected-error {{'case' label can only appear inside a 'switch' statement}} expected-error {{expected pattern}}
}
enumRecovery2{
case UE1: // expected-error {{'case' label can only appear inside a 'switch' statement}}
}
enumRecovery3{
case UE2(Void): // expected-error {{'case' label can only appear inside a 'switch' statement}}
}
enumRecovery4{ // expected-note {{in declaration of 'Recovery4'}}
case Self Self // expected-error {{keyword 'Self' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{8-12=`Self`}} expected-error {{consecutive declarations on a line must be separated by ';'}} {{12-12=;}} expected-error {{expected declaration}}
}
enum Recovery5 {
case .UE3 // expected-error {{extraneous '.' in enum 'case' declaration}} {{8-9=}}
case .UE4,.UE5
// expected-error@-1{{extraneous '.' in enum 'case' declaration}} {{8-9=}}
// expected-error@-2{{extraneous '.' in enum 'case' declaration}} {{14-15=}}
}
enumRecovery6{
case Snout, _; // expected-error {{keyword '_' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{15-16=`_`}} expected-note {{'_' previously declared here}}
case _; // expected-error {{keyword '_' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{8-9=`_`}} expected-error {{invalid redeclaration of '_'}}
case Tusk, // expected-error {{expected identifier after comma in enum 'case' declaration}}
}
enumRawTypeEmpty:Int{} // expected-error {{an enum with no cases cannot declare a raw type}} expected-note {{add stubs for conformance}}
// expected-error@-1{{'RawTypeEmpty' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
enumRaw:Int{
case Ankeny, Burnside
}
enumMultiRawType:Int64,Int32{ // expected-error {{multiple enum raw types 'Int64' and 'Int32'}}
case Couch, Davis
}
protocolRawTypeNotFirstProtocol{}
enumRawTypeNotFirst:RawTypeNotFirstProtocol,Int{ // expected-error {{raw type 'Int' must appear first in the enum inheritance clause}} {{24-24=Int, }} {{47-52=}}
case E
}
enumExpressibleByRawTypeNotLiteral:Array<Int>{ // expected-error {{raw type 'Array<Int>' is not expressible by a string, integer, or floating-point literal}} expected-note {{add stubs for conformance}}
// expected-error@-1{{'ExpressibleByRawTypeNotLiteral' declares raw type 'Array<Int>', but does not conform to RawRepresentable and conformance could not be synthesized}}
case Ladd, Elliott, Sixteenth, Harrison
}
enumRawTypeCircularityA:RawTypeCircularityB,ExpressibleByIntegerLiteral{ // expected-error {{'RawTypeCircularityA' has a raw type that depends on itself}}
case Morrison, Belmont, Madison, Hawthorne
init(integerLiteral value:Int){
self=.Morrison
}
}
enumRawTypeCircularityB:RawTypeCircularityA,ExpressibleByIntegerLiteral{ // expected-note {{enum 'RawTypeCircularityB' declared here}}
case Willamette, Columbia, Sandy, Multnomah
init(integerLiteral value:Int){
self=.Willamette
}
}
structExpressibleByFloatLiteralOnly:ExpressibleByFloatLiteral{
init(floatLiteral:Double){}
}
enumExpressibleByRawTypeNotIntegerLiteral:ExpressibleByFloatLiteralOnly{ // expected-error {{'ExpressibleByRawTypeNotIntegerLiteral' declares raw type 'ExpressibleByFloatLiteralOnly', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-error {{RawRepresentable conformance cannot be synthesized because raw type 'ExpressibleByFloatLiteralOnly' is not Equatable}} expected-note {{add stubs for conformance}}
case Everett // expected-error {{enum cases require explicit raw values when the raw type is not expressible by integer or string literal}}
case Flanders
}
enumRawTypeWithIntValues:Int{
case Glisan =17, Hoyt =219, Irving, Johnson =97209
}
enumRawTypeWithNegativeValues:Int{
case Glisan =-17, Hoyt =-219, Irving, Johnson =-97209
case AutoIncAcrossZero =-1, Zero, One
}
enumRawTypeWithUnicodeScalarValues:UnicodeScalar{ // expected-error {{'RawTypeWithUnicodeScalarValues' declares raw type 'UnicodeScalar' (aka 'Unicode.Scalar'), but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Kearney ="K"
case Lovejoy // expected-error {{enum cases require explicit raw values when the raw type is not expressible by integer or string literal}}
case Marshall ="M"
}
enumRawTypeWithCharacterValues:Character{ // expected-error {{'RawTypeWithCharacterValues' declares raw type 'Character', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case First ="い"
case Second // expected-error {{enum cases require explicit raw values when the raw type is not expressible by integer or string literal}}
case Third ="は"
}
enumRawTypeWithCharacterValues_Correct:Character{
case First ="😅" // ok
case Second ="👩👩👧👦" // ok
case Third ="👋🏽" // ok
case Fourth ="\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}" // ok
}
enumRawTypeWithCharacterValues_Error1:Character{ // expected-error {{'RawTypeWithCharacterValues_Error1' declares raw type 'Character', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case First ="abc" // expected-error {{cannot convert value of type 'String' to raw type 'Character'}}
}
enumRawTypeWithStringValues:String{
case Primrose // okay
case Quimby ="Lucky Lab"
case Raleigh // okay
case Savier ="McMenamin's", Thurman ="Kenny and Zuke's"
}
enumRawValuesWithoutRawType{
case Upshur =22 // expected-error {{enum case cannot have a raw value if the enum does not have a raw type}}
}
enumRawTypeWithRepeatValues:Int{
case Vaughn =22 // expected-note {{raw value previously used here}}
case Wilson =22 // expected-error {{raw value for enum case is not unique}}
}
enumRawTypeWithRepeatValuesString:String{
case Vaughn ="XYZ" // expected-note {{raw value previously used here}}
case Wilson ="XYZ" // expected-error {{raw value for enum case is not unique}}
}
enumRawTypeWithRepeatValuesAutoInc1:String{
case A ="B" // expected-note {{raw value previously used here}}
case B // expected-error {{raw value for enum case is not unique}}
}
enumRawTypeWithRepeatValuesAutoInc2:String{
case A // expected-note {{raw value previously used here}}
case B ="A" // expected-error {{raw value for enum case is not unique}}
}
enumRawTypeWithRepeatValuesAutoInc3:String{
case A
case B // expected-note {{raw value previously used here}}
case C ="B" // expected-error {{raw value for enum case is not unique}}
}
enumNonliteralRawValue:Int{
case Yeon =100+20+3 // expected-error {{raw value for enum case must be a literal}}
}
enumRawTypeWithPayload:Int{ // expected-error {{'RawTypeWithPayload' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{declared raw type 'Int' here}} expected-note {{declared raw type 'Int' here}} expected-note {{add stubs for conformance}}
case Powell(Int) // expected-error {{enum with raw type cannot have cases with arguments}}
case Terwilliger(Int)=17 // expected-error {{enum with raw type cannot have cases with arguments}}
}
enumRawTypeMismatch:Int{ // expected-error {{'RawTypeMismatch' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Barbur ="foo" // expected-error {{}}
}
enumDuplicateMembers1{
case Foo // expected-note {{'Foo' previously declared here}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
}
enumDuplicateMembers2{
case Foo, Bar // expected-note {{'Foo' previously declared here}} expected-note {{'Bar' previously declared here}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
case Bar // expected-error {{invalid redeclaration of 'Bar'}}
}
enumDuplicateMembers3{
case Foo // expected-note {{'Foo' previously declared here}}
case Foo(Int) // expected-error {{invalid redeclaration of 'Foo'}}
}
enumDuplicateMembers4:Int{ // expected-error {{'DuplicateMembers4' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Foo =1 // expected-note {{'Foo' previously declared here}}
case Foo =2 // expected-error {{invalid redeclaration of 'Foo'}}
}
enumDuplicateMembers5:Int{ // expected-error {{'DuplicateMembers5' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Foo =1 // expected-note {{'Foo' previously declared here}}
case Foo =1+1 // expected-error {{invalid redeclaration of 'Foo'}} expected-error {{raw value for enum case must be a literal}}
}
enumDuplicateMembers6{
case Foo // expected-note 2{{'Foo' previously declared here}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
}
enumDuplicateMembers7:String{ // expected-error {{'DuplicateMembers7' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Foo // expected-note {{'Foo' previously declared here}}
case Foo ="Bar" // expected-error {{invalid redeclaration of 'Foo'}}
}
enumDuplicateMembers8:String{ // expected-error {{'DuplicateMembers8' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Foo // expected-note {{'Foo' previously declared here}}
// expected-note@-1 {{raw value previously used here}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
// expected-error@-1 {{raw value for enum case is not unique}}
}
enumDuplicateMembers9:String{ // expected-error {{'DuplicateMembers9' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case Foo ="Foo" // expected-note {{'Foo' previously declared here}}
// expected-note@-1 {{raw value previously used here}}
case Foo ="Foo"// expected-error {{invalid redeclaration of 'Foo'}}
// expected-error@-1 {{raw value for enum case is not unique}}
}
enumDuplicateMembers10:String{
case Foo // expected-note {{raw value previously used here}}
case Bar ="Foo" // expected-error {{raw value for enum case is not unique}}
}
// Refs to duplicated enum cases shouldn't crash the compiler.
// rdar://problem/20922401
func check20922401()->String{
letx:DuplicateMembers1=.Foo
switch x {
case.Foo:
return"Foo"
}
}
enumPlaygroundRepresentation:UInt8{
case Class =1
case Struct =2
case Tuple =3
case Enum =4
case Aggregate =5
case Container =6
case IDERepr =7
case Gap =8
case ScopeEntry =9
case ScopeExit =10
case Error =11
case IndexContainer =12
case KeyContainer =13
case MembershipContainer =14
case Unknown =0xFF
staticfunc fromByte(byte :UInt8)->PlaygroundRepresentation{
letrepr=PlaygroundRepresentation(rawValue: byte)
if repr ==.none {return.Unknown }else{return repr! }
}
}
structManyLiteralable:ExpressibleByIntegerLiteral,ExpressibleByStringLiteral,Equatable{
init(stringLiteral:String){}
init(integerLiteral:Int){}
init(unicodeScalarLiteral:String){}
init(extendedGraphemeClusterLiteral:String){}
}
func==(lhs:ManyLiteralable, rhs:ManyLiteralable)->Bool{returntrue}
enumManyLiteralA:ManyLiteralable{
case A // expected-note {{raw value previously used here}} expected-note {{raw value implicitly auto-incremented from zero}}
case B =0 // expected-error {{raw value for enum case is not unique}}
}
enumManyLiteralB:ManyLiteralable{ // expected-error {{'ManyLiteralB' declares raw type 'ManyLiteralable', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case A ="abc"
case B // expected-error {{enum case must declare a raw value when the preceding raw value is not an integer}}
}
enumManyLiteralC:ManyLiteralable{
case A
case B ="0"
}
enumfoo:String{ // expected-error {{'foo' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}}
case bar =nil // expected-error {{cannot convert 'nil' to raw type 'String'}}
}
// Static member lookup from instance methods
structEmptyStruct{}
enumEnumWithStaticMember{
staticletstaticVar=EmptyStruct()
func foo(){
let _ = staticVar // expected-error {{static member 'staticVar' cannot be used on instance of type 'EnumWithStaticMember'}}
}
}
// SE-0036:
structSE0036_Auxiliary{}
enumSE0036{
case A
case B(SE0036_Auxiliary)
case C(SE0036_Auxiliary)
staticfunc staticReference(){
_ = A
_ =self.A
_ =SE0036.A
}
func staticReferenceInInstanceMethod(){
_ = A // expected-error {{enum case 'A' cannot be used as an instance member}} {{9-9=SE0036.}}
_ =self.A // expected-error {{enum case 'A' cannot be used as an instance member}} {{9-13=SE0036}}
_ =SE0036.A
}
staticfunc staticReferenceInSwitchInStaticMethod(){
switchSE0036.A {
case A:break
caseB(_):break
caseC(let x): _ = x; break
}
}
func staticReferenceInSwitchInInstanceMethod(){
switchself{
case A:break // expected-error {{enum case 'A' cannot be used as an instance member}} {{10-10=.}}
caseB(_):break // expected-error {{'_' can only appear in a pattern or on the left side of an assignment}}
caseC(let x): _ = x; break // expected-error {{enum case 'C' cannot be used as an instance member}} {{10-10=.}}
}
}
func explicitReferenceInSwitch(){
switchSE0036.A {
caseSE0036.A:break
caseSE0036.B(_):break
caseSE0036.C(let x): _ = x; break
}
}
func dotReferenceInSwitchInInstanceMethod(){
switchself{
case.A:break
case.B(_):break
case.C(let x): _ = x; break
}
}
staticfunc dotReferenceInSwitchInStaticMethod(){
switchSE0036.A {
case.A:break
case.B(_):break
case.C(let x): _ = x; break
}
}
init(){
self=.A
self= A // expected-error {{enum case 'A' cannot be used as an instance member}} {{12-12=SE0036.}}
self=SE0036.A
self=.B(SE0036_Auxiliary())
self=B(SE0036_Auxiliary()) // expected-error {{enum case 'B' cannot be used as an instance member}} {{12-12=SE0036.}}
self=SE0036.B(SE0036_Auxiliary())
}
}
enumSE0036_Generic<T>{
case A(x:T)
func foo(){
switchself{
caseA(_):break // expected-error {{'_' can only appear in a pattern or on the left side of an assignment}}
}
switchself{
case.A(let a):print(a)
}
switchself{
caseSE0036_Generic.A(let a):print(a)
}
}
}
enumswitch{} // expected-error {{keyword 'switch' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{6-12=`switch`}}
enumSE0155{
case emptyArgs() // expected-warning {{enum element with associated values must have at least one associated value}}
// expected-note@-1 {{did you mean to remove the empty associated value list?}} {{17-19=}}
// expected-note@-2 {{did you mean to explicitly add a 'Void' associated value?}} {{18-18=Void}}
}
// https://github.com/apple/swift/issues/53662
enumE_53662{
case identifier
case operator // expected-error {{keyword 'operator' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{8-16=`operator`}}
case identifier2
}
enumE_53662_var{
case identifier
case var // expected-error {{keyword 'var' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{8-11=`var`}}
case identifier2
}
enumE_53662_underscore{
case identifier
case _ // expected-error {{keyword '_' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{8-9=`_`}}
case identifier2
}
enumE_53662_Comma{
case a, b, c, func, d // expected-error {{keyword 'func' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{17-21=`func`}}
}
enumE_53662_Newline{
case identifier1
case identifier2
case
case identifier // expected-error {{keyword 'case' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{3-7=`case`}}
}
enumE_53662_Newline2{
case
func foo(){} // expected-error {{keyword 'func' cannot be used as an identifier here}} expected-note {{if this name is unavoidable, use backticks to escape it}} {{3-7=`func`}}
}
enum E_53662_PatternMatching {
case let .foo(x,y): // expected-error {{'case' label can only appear inside a 'switch' statement}}
}
enumCasesWithMissingElement:Int{
// expected-error@-1 {{'CasesWithMissingElement' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
// expected-note@-2 {{add stubs for conformance}}
case a ="hello", // expected-error{{expected identifier after comma in enum 'case' declaration}}
// expected-error@-1 {{cannot convert value of type 'String' to raw type 'Int'}}
case b ="hello", // expected-error{{expected identifier after comma in enum 'case' declaration}}
// expected-error@-1 {{cannot convert value of type 'String' to raw type 'Int'}}
}