Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1660] Enum with String rawValue and escaped init identifier causes compile error #44269

Closed
swift-ci opened this issue Jun 2, 2016 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 2, 2016

Previous ID SR-1660
Radar None
Original Reporter Wheerd (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, Parser
Assignee None
Priority Medium

md5: 5665fa3f90e5789f42eca1cf46a3fc49

is duplicated by:

  • SR-6959 can't use init as a enum case with associated values

relates to:

  • SR-6557 init case of enum can be used implicitly

Issue Description:

The following program results in a compile time error, because the generated rawValue initializer interferes with the `init` case:

enum Test : String {
    case `init`
}

The compiler gives the following error message:

<unknown>:0: error: invalid redeclaration of 'init(rawValue:)'
INFO at line 5, col 10: 'init' previously declared here
    case `init`
         ^

From my understanding, this enum should work perfectly fine, as that is what the backtick escapes are supposed to prevent.

@belkadan
Copy link
Contributor

belkadan commented Jun 2, 2016

Yep. This is caused by our representation of the keyword init as "init" rather than some non-identifier name.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 7, 2016

Comment by Manuel Krebber (JIRA)

Does that mean it won't be fixed? It is not really a pressing issue, as one can simply use a different identifier, but I guess other problem could also arise from mixing the initializers with the normal namespace.

@belkadan
Copy link
Contributor

belkadan commented Jun 7, 2016

Well, it's definitely a bug, and something I'd like to fix, but given that you usually can use a different identifier I don't expect it to be prioritized.

@swift-ci
Copy link
Collaborator Author

Comment by Richard Venable (JIRA)

I also ran into this bug, so then I tested other keywords and verified that it is just `init` that has this problem:

enum Keyword: String {
    case `associatedtype`
    case `class`
    case `deinit`
    case `enum`
    case `extension`
    case `fileprivate`
    case `func`
    case `import`
    //case `init`
    case `inout`
    case `internal`
    case `let`
    case `open`
    case `operator`
    case `private`
    case `protocol`
    case `public`
    case `static`
    case `struct`
    case `subscript`
    case `typealias`
    case `var`
    case `break`
    case `case`
    case `continue`
    case `default`
    case `defer`
    case `do`
    case `else`
    case `fallthrough`
    case `for`
    case `guard`
    case `if`
    case `in`
    case `repeat`
    case `return`
    case `switch`
    case `where`
    case `while`
    case `as`
    case `Any`
    case `catch`
    case `false`
    case `is`
    case `nil`
    case `rethrows`
    case `super`
    case `self`
    case `Self`
    case `throw`
    case `throws`
    case `true`
    case `try`
    case `associativity`
    case `convenience`
    case `dynamic`
    case `didSet`
    case `final`
    case `get`
    case `infix`
    case `indirect`
    case `lazy`
    case `left`
    case `mutating`
    case `none`
    case `nonmutating`
    case `optional`
    case `override`
    case `postfix`
    case `precedence`
    case `prefix`
    case `Protocol`
    case `required`
    case `right`
    case `set`
    case `Type`
    case `unowned`
    case `weak`
    case `willSet`
}

let keyword = Keyword(rawValue: "func")

@belkadan
Copy link
Contributor

belkadan commented Feb 9, 2018

The right fix for this is to make init a "special declaration name" like @ahoppen did with subscript. That's not inherently difficult but it's going to require a huge pile of mechanical changes all across the compiler.

@slavapestov
Copy link
Member

#15201

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

3 participants