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
|
// RUN: %target-typecheck-verify-swift
enum Empty {}
enum Boolish {
case falsy
case truthy
init() { self = .falsy }
}
var b = Boolish.falsy
b = .truthy
enum Optionable<T> {
case Nought
case Mere(T)
}
var o = Optionable<Int>.Nought
o = .Mere(0)
enum Color { case Red, Green, Grayscale(Int), Blue }
var c = Color.Red
c = .Green
c = .Grayscale(255)
c = .Blue
let partialApplication = Color.Grayscale
// Cases are excluded from non-enums.
case FloatingCase // expected-error{{enum 'case' is not allowed outside of an enum}}
struct SomeStruct {
case StructCase // expected-error{{enum 'case' is not allowed outside of an enum}}
}
class SomeClass {
case ClassCase // expected-error{{enum 'case' is not allowed outside of an enum}}
}
enum EnumWithExtension1 {
case A1
}
extension EnumWithExtension1 {
case A2 // expected-error{{enum 'case' is not allowed outside of an enum}}
}
// Attributes for enum cases.
enum EnumCaseAttributes {
@xyz case EmptyAttributes // expected-error {{unknown attribute 'xyz'}}
}
// Recover when a switch 'case' label is spelled inside an enum (or outside).
enum SwitchEnvy {
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 (_, var x, 0): // expected-error{{'case' label can only appear inside a 'switch' statement}}
}
enum HasMethodsPropertiesAndCtors {
case TweedleDee
case TweedleDum
func method() {}
func staticMethod() {}
init() {}
subscript(x:Int) -> Int {
return 0
}
var property : Int {
return 0
}
}
enum ImproperlyHasIVars {
case Flopsy
case Mopsy
var ivar : Int // expected-error{{enums must not contain stored properties}}
}
// We used to crash on this. rdar://14678675
enum rdar14678675 {
case U1, // expected-error{{expected identifier after comma in enum 'case' declaration}}
case U2
case U3
}
enum Recovery1 {
case: // expected-error {{'case' label can only appear inside a 'switch' statement}} expected-error {{expected pattern}}
}
enum Recovery2 {
case UE1: // expected-error {{'case' label can only appear inside a 'switch' statement}}
}
enum Recovery3 {
case UE2(Void): // expected-error {{'case' label can only appear inside a 'switch' statement}}
}
enum Recovery4 { // 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=}}
}
enum Recovery6 {
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}}
}
enum RawTypeEmpty : Int {} // expected-error {{an enum with no cases cannot declare a raw type}}
// expected-error@-1{{'RawTypeEmpty' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
enum Raw : Int {
case Ankeny, Burnside
}
enum MultiRawType : Int64, Int32 { // expected-error {{multiple enum raw types 'Int64' and 'Int32'}}
case Couch, Davis
}
protocol RawTypeNotFirstProtocol {}
enum RawTypeNotFirst : RawTypeNotFirstProtocol, Int { // expected-error {{raw type 'Int' must appear first in the enum inheritance clause}} {{24-24=Int, }} {{47-52=}}
case E
}
enum ExpressibleByRawTypeNotLiteral : Array<Int> { // expected-error {{raw type 'Array<Int>' is not expressible by a string, integer, or floating-point literal}}
// 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
}
enum RawTypeCircularityA : RawTypeCircularityB, ExpressibleByIntegerLiteral { // expected-error {{'RawTypeCircularityA' has a raw type that depends on itself}}
case Morrison, Belmont, Madison, Hawthorne
init(integerLiteral value: Int) {
self = .Morrison
}
}
enum RawTypeCircularityB : RawTypeCircularityA, ExpressibleByIntegerLiteral { // expected-note {{enum 'RawTypeCircularityB' declared here}}
case Willamette, Columbia, Sandy, Multnomah
init(integerLiteral value: Int) {
self = .Willamette
}
}
struct ExpressibleByFloatLiteralOnly : ExpressibleByFloatLiteral {
init(floatLiteral: Double) {}
}
enum ExpressibleByRawTypeNotIntegerLiteral : 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}}
case Everett // expected-error {{enum cases require explicit raw values when the raw type is not expressible by integer or string literal}}
case Flanders
}
enum RawTypeWithIntValues : Int {
case Glisan = 17, Hoyt = 219, Irving, Johnson = 97209
}
enum RawTypeWithNegativeValues : Int {
case Glisan = -17, Hoyt = -219, Irving, Johnson = -97209
case AutoIncAcrossZero = -1, Zero, One
}
enum RawTypeWithUnicodeScalarValues : UnicodeScalar { // expected-error {{'RawTypeWithUnicodeScalarValues' declares raw type 'UnicodeScalar' (aka 'Unicode.Scalar'), but does not conform to RawRepresentable and conformance could not be synthesized}}
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"
}
enum RawTypeWithCharacterValues : Character { // expected-error {{'RawTypeWithCharacterValues' declares raw type 'Character', but does not conform to RawRepresentable and conformance could not be synthesized}}
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 = "ใฏ"
}
enum RawTypeWithCharacterValues_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
}
enum RawTypeWithCharacterValues_Error1 : Character { // expected-error {{'RawTypeWithCharacterValues_Error1' declares raw type 'Character', but does not conform to RawRepresentable and conformance could not be synthesized}}
case First = "abc" // expected-error {{cannot convert value of type 'String' to raw type 'Character'}}
}
enum RawTypeWithStringValues : String {
case Primrose // okay
case Quimby = "Lucky Lab"
case Raleigh // okay
case Savier = "McMenamin's", Thurman = "Kenny and Zuke's"
}
enum RawValuesWithoutRawType {
case Upshur = 22 // expected-error {{enum case cannot have a raw value if the enum does not have a raw type}}
}
enum RawTypeWithRepeatValues : Int {
case Vaughn = 22 // expected-note {{raw value previously used here}}
case Wilson = 22 // expected-error {{raw value for enum case is not unique}}
}
enum RawTypeWithRepeatValuesString : String {
case Vaughn = "XYZ" // expected-note {{raw value previously used here}}
case Wilson = "XYZ" // expected-error {{raw value for enum case is not unique}}
}
enum RawTypeWithRepeatValuesAutoInc1 : String {
case A = "B" // expected-note {{raw value previously used here}}
case B // expected-error {{raw value for enum case is not unique}}
}
enum RawTypeWithRepeatValuesAutoInc2 : String {
case A // expected-note {{raw value previously used here}}
case B = "A" // expected-error {{raw value for enum case is not unique}}
}
enum RawTypeWithRepeatValuesAutoInc3 : 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}}
}
enum NonliteralRawValue : Int {
case Yeon = 100 + 20 + 3 // expected-error {{raw value for enum case must be a literal}}
}
enum RawTypeWithPayload : 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}}
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}}
}
enum RawTypeMismatch : Int { // expected-error {{'RawTypeMismatch' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
case Barbur = "foo" // expected-error {{}}
}
enum DuplicateMembers1 {
case Foo // expected-note {{'Foo' previously declared here}}
case Foo // expected-error {{invalid redeclaration of 'Foo'}}
}
enum DuplicateMembers2 {
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'}}
}
enum DuplicateMembers3 {
case Foo // expected-note {{'Foo' previously declared here}}
case Foo(Int) // expected-error {{invalid redeclaration of 'Foo'}}
}
enum DuplicateMembers4 : Int { // expected-error {{'DuplicateMembers4' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
case Foo = 1 // expected-note {{'Foo' previously declared here}}
case Foo = 2 // expected-error {{invalid redeclaration of 'Foo'}}
}
enum DuplicateMembers5 : Int { // expected-error {{'DuplicateMembers5' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
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}}
}
enum DuplicateMembers6 {
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'}}
}
enum DuplicateMembers7 : String { // expected-error {{'DuplicateMembers7' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
case Foo // expected-note {{'Foo' previously declared here}}
case Foo = "Bar" // expected-error {{invalid redeclaration of 'Foo'}}
}
enum DuplicateMembers8 : String { // expected-error {{'DuplicateMembers8' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
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}}
}
enum DuplicateMembers9 : String { // expected-error {{'DuplicateMembers9' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
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}}
}
enum DuplicateMembers10 : 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 {
let x: DuplicateMembers1 = .Foo
switch x {
case .Foo:
return "Foo"
}
}
enum PlaygroundRepresentation : 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
static func fromByte(byte : UInt8) -> PlaygroundRepresentation {
let repr = PlaygroundRepresentation(rawValue: byte)
if repr == .none { return .Unknown } else { return repr! }
}
}
struct ManyLiteralable : ExpressibleByIntegerLiteral, ExpressibleByStringLiteral, Equatable {
init(stringLiteral: String) {}
init(integerLiteral: Int) {}
init(unicodeScalarLiteral: String) {}
init(extendedGraphemeClusterLiteral: String) {}
}
func ==(lhs: ManyLiteralable, rhs: ManyLiteralable) -> Bool { return true }
enum ManyLiteralA : 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}}
}
enum ManyLiteralB : ManyLiteralable { // expected-error {{'ManyLiteralB' declares raw type 'ManyLiteralable', but does not conform to RawRepresentable and conformance could not be synthesized}}
case A = "abc"
case B // expected-error {{enum case must declare a raw value when the preceding raw value is not an integer}}
}
enum ManyLiteralC : ManyLiteralable {
case A
case B = "0"
}
enum foo : String { // expected-error {{'foo' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}}
case bar = nil // expected-error {{cannot convert 'nil' to raw type 'String'}}
}
// Static member lookup from instance methods
struct EmptyStruct {}
enum EnumWithStaticMember {
static let staticVar = EmptyStruct()
func foo() {
let _ = staticVar // expected-error {{static member 'staticVar' cannot be used on instance of type 'EnumWithStaticMember'}}
}
}
// SE-0036:
struct SE0036_Auxiliary {}
enum SE0036 {
case A
case B(SE0036_Auxiliary)
case C(SE0036_Auxiliary)
static func 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
}
static func staticReferenceInSwitchInStaticMethod() {
switch SE0036.A {
case A: break
case B(_): break
case C(let x): _ = x; break
}
}
func staticReferenceInSwitchInInstanceMethod() {
switch self {
case A: break // expected-error {{enum case 'A' cannot be used as an instance member}} {{10-10=.}}
case B(_): break // expected-error {{'_' can only appear in a pattern or on the left side of an assignment}}
case C(let x): _ = x; break // expected-error {{enum case 'C' cannot be used as an instance member}} {{10-10=.}}
}
}
func explicitReferenceInSwitch() {
switch SE0036.A {
case SE0036.A: break
case SE0036.B(_): break
case SE0036.C(let x): _ = x; break
}
}
func dotReferenceInSwitchInInstanceMethod() {
switch self {
case .A: break
case .B(_): break
case .C(let x): _ = x; break
}
}
static func dotReferenceInSwitchInStaticMethod() {
switch SE0036.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())
}
}
enum SE0036_Generic<T> {
case A(x: T)
func foo() {
switch self {
case A(_): break // expected-error {{'_' can only appear in a pattern or on the left side of an assignment}}
}
switch self {
case .A(let a): print(a)
}
switch self {
case SE0036_Generic.A(let a): print(a)
}
}
}
enum switch {} // 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`}}
enum SE0155 {
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
enum E_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
}
enum E_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
}
enum E_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
}
enum E_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`}}
}
enum E_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`}}
}
enum E_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}}
}
enum CasesWithMissingElement: Int {
// expected-error@-1 {{'CasesWithMissingElement' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}}
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'}}
}
|