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-9679] Spurious "enum element cannot be referenced as an instance member" error #52122

Closed
mattneub opened this issue Jan 16, 2019 · 7 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers type checker Area → compiler: Semantic analysis

Comments

@mattneub
Copy link

Previous ID SR-9679
Radar rdar://problem/47686317
Original Reporter @mattneub
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug, TypeChecker
Assignee @mattneub
Priority Medium

md5: 3caa66360ed0b6baa10819c536ffca98

Issue Description:

func f1() {
    let okAction = UIAlertAction(
        title: "Undo All", style: .default, handler: {
            _ in self.doUndoAll(nil)
        }
    )
}
func f2(_ sender: Any) { }

This results in "Enum element 'default' cannot be referenced as an instance member". But the problem is not in the enum element; it's in the `nil` parameter inside the trailing closure.

This may be related to https://bugs.swift.org/browse/SR-5069. The latter is marked closed, but perhaps it should not be.

@belkadan
Copy link
Contributor

Self-contained version:

class FakeUIAlertAction {
  enum Style {
    case `default`
  }
  init(title: String, style: Style, handler: (Any) -> Void) {}
}

func f1() {
    let okAction = FakeUIAlertAction(
        title: "Undo All", style: .default, handler: {
            _ in f2(nil)
        }
    )
}
func f2(_ sender: Any) { }

Even if I write the full FakeUIAlertAction.Style.default I get "expression type 'FakeUIAlertAction' is ambiguous without more context", so the type checker's clearly quite confused.

@xedin, have you seen this before?

@mattneub
Copy link
Author

By the way, another way to get this diagnostic is to use `=` instead of `==` in comparison, e.g.

        if self.game = .ended {

That results in "Enum element 'ended' cannot be referenced as an instance member". That seems an unhelpful statement of the mistake; it wouldn't give a beginner any clue as to what's really wrong.

@xedin
Copy link
Member

xedin commented Jan 31, 2019

Thanks for reporting, @mattneub! It looks like the problem here is related to `f2(nil)` because `nil` can't be an argument for `Any` parameter. Diagnostics could definitely use some work...

@mayoff
Copy link

mayoff commented Jun 7, 2019

The swiftc included with Xcode 11 beta 1 emits the following improved diagnostic for Jordan's FakeUIAlertAction test:

/tmp/test.swift:11:21: error: 'nil' is not compatible with expected argument type 'Any'
            _ in f2(nil)
                    ^ 

@xedin
Copy link
Member

xedin commented Jun 7, 2019

That’s fortunate 🙂

@xedin
Copy link
Member

xedin commented Apr 17, 2020

Could you please test with Xcode 11.4 and let me know whether this problem still exists? It looks like reduced example results in a correct diagnostic.

@mattneub
Copy link
Author

@xedin I can't reproduce in 11.4. Let's close it. Thanks!

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants