Skip to content

[SR-510] case Foo = {!@#$!@#$} compiles and drops the case #43127

Closed
@lilyball

Description

@lilyball
mannequin
Previous ID SR-510
Radar None
Original Reporter @lilyball
Type Bug
Status Resolved
Resolution Done
Environment

Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
Apple Swift version 2.2-dev (LLVM 3ebdbb2c7e, Clang f66c5bb67b, Swift 54dcd16)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @dduan
Priority Medium

md5: 33eddeb732f3259b9a84f18bc9c2048f

Issue Description:

The following compiles just fine:

enum Testing: String {
    case Thing = "thing"
    case Bob = {"test"}
}

but the actual case Bob is dropped from the AST entirely, as seen by swiftc -print-ast:

internal enum Testing : String {
  case Thing
  internal typealias RawValue = String
  internal var hashValue: Int { get }
  internal init?(rawValue: String)
  internal var rawValue: String { get }
}

Even more interestingly, it doesn't matter what's inside the brackets. The following behaves identically:

enum Testing: String {
    case Thing = "thing"
    case Bob = {!@#$!@#%!@}
}

And in fact, you can actually write this case anywhere, not just inside of enums:

case Bob = {!@#!@#}

The only requirements seem to be that the characters inside the braces must be valid Swift source characters, so e.g. you can't put an apostrophe in there. And whatever's inside must pass the tokenizer, so e.g. string literals must be valid, and floating-point literals must be as well.

It also appears that the rest of the line past the closing brace is also discarded after tokenizing. Unless there's an open brace, which causes it to emit a few errors about closure expressions. And if this case is at the top-level, it actually discards the rest of the file as well, although a few keywords like break and if can trigger errors:

print("hello")
case X = { blah blah blah };

Two households, both alike in dignity,
In fair Verona, where we lay our scene,
//From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean
From forth the fatal loins of these two foes
A pair of star-cross`d lovers take their life;
Whose misadventured piteous overthrows
Do with their death bury their parents` strife
The fearful passage of their death-mark`d love,
And the continuance of their parents` rage,
Which, but their children`s end, nought could remove,
Is now the two hours` traffic of our stage;
//The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend

This issue was first discovered by nuclearace (JIRA User), and a lot of the subsequent info was found by @mikeash.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions