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-13608] Swift 5.5 and 5.3 compilers confuse enum case names with struct name #56043

Open
haikusw opened this issue Sep 25, 2020 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@haikusw
Copy link
Contributor

haikusw commented Sep 25, 2020

Previous ID SR-13608
Radar rdar://problem/69549219
Original Reporter @haikusw
Type Bug
Environment

macOS 10.15.16
Xcode 12.0 GM (12A7209)

And Xcode 12.2 beta (12B5018i)

~ % xcode-select -p
/Applications/Xcode.app/Contents/Developer
~ % swift --version
Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1)
Target: x86_64-apple-darwin19.6.0

and

~ % xcode-select -p
/Applications/Xcode-beta.app/Contents/Developer

~ % swift --version
Apple Swift version 5.3.1 (swiftlang-1200.0.35 clang-1200.0.32.4)
Target: x86_64-apple-darwin19.6.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: bb11ef562b5bc556ff9f9532474e9b69

Issue Description:

If you have a struct with the same name as the enum value in an enum and then have a computed property in the enum that references that struct then the compiler erroneously thinks the struct name is an enum case even though it doesn't have a . in front of it.

import Cocoa 
protocol Activity {
    var id: String { get set }
}

struct Share: Activity {
    var id: String
}

struct Favorite: Activity {
    var id: String
}

enum ActivityType {
    case Share
    case Favorite
    
    var type: Activity {
        switch self {
        case .Share: return Share(id: "1")             // ERROR
        case .Favorite: return Favorite(id: "2")     // ERROR
        }
    }
}
error: MyPlayground.playground:2313:23: error: enum case 'Share' cannot be used as an instance member
 case .Share: return Share(id: "1")
                     ^~~~~
                     ActivityType.
 
 error: MyPlayground.playground:2313:23: error: enum case 'Share' has no associated values
 case .Share: return Share(id: "1")
                     ^
 
 error: MyPlayground.playground:2314:26: error: enum case 'Favorite' has no associated values
 case .Favorite: return Favorite(id: "2")
                        ^
 
 error: MyPlayground.playground:2314:26: error: enum case 'Favorite' cannot be used as an instance member
 case .Favorite: return Favorite(id: "2")
                        ^~~~~~~~
                        ActivityType.
@haikusw
Copy link
Contributor Author

haikusw commented Sep 25, 2020

  • Putting the computed property into an extension on the enum doesn't change the result.

  • Using:

    {code:swift}
    if self == .Share { return Share.self }

@typesanitizer
Copy link

You're right, this does seem like a bug.

@swift-ci create

@slavapestov
Copy link
Member

It looks like this is not a regression at least. Name lookup will find enum cases when performing an unqualified lookup, and the filtering happens later.

We could probably change this behavior without breaking source though.

In the meantime, a possible workaround is to module-qualify the Share struct, which will always find the top-level name and not the innermost one.

@haikusw
Copy link
Contributor Author

haikusw commented Oct 1, 2020

Thank you for the reply. Glad it's not a regression.

Is there a module name for an Xcode Swift playground? I'm not aware of how to reference the current module in an Xcode Swift playground.

Are there other places, besides the case declaration in the enum, where the name of an enum case element is valid without the "." in front of it? I don't understand how the compiler/type-checker thinks there's any possibility that this is the enum case symbol.

Thank you.

@haikusw
Copy link
Contributor Author

haikusw commented Sep 24, 2021

This continues to be a problem in Xcode 13.0 / Swift 5.5 fwiw.

Copied the code above into an Xcode 13.0 macOS playground and get the same error exactly.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants