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-5789] Fixit when failed reference to member named subscript could refer to subscript declaration #48359

Closed
jckarter opened this issue Aug 29, 2017 · 3 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

@jckarter
Copy link
Member

Previous ID SR-5789
Radar None
Original Reporter @jckarter
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug, TypeChecker
Assignee @AnthonyLatsis
Priority Medium

md5: fa1f5c2c3f8ea6e0eb85b218ce540463

Issue Description:

Someone learning the language could expect this to work:

struct Foo {
  subscript(x: Int) -> Int { return x }
}

let foo = Foo()
let bar = foo.subscript(0)

The foo.subscript syntax looks for a member named func `subscript` or var `subscript` instead of a subscript declaration, which will fail, and give a confusing error message:

/Users/jgroff/foo.swift:6:11: error: value of type 'Foo' has no member 'subscript'
let bar = foo.subscript(0)
          ^~~ ~~~~~~~~~

It would be helpful to improve the error message to specifically say that Foo has no method, property, or nested type named `subscript`, and/or to recognize that Foo does have a subscript declaration and suggest that as an alternative with a fixit:

/Users/jgroff/foo.swift:6:11: error: value of type 'Foo' has no member property or method named 'subscript'
let bar = foo.subscript(0)
          ^~~ ~~~~~~~~~

/Users/jgroff/foo.swift:6:11: note: did you mean to use the subscript operator?
let bar = foo.subscript(0)
              ~~~~~~~~~~~~
              [0]
@belkadan
Copy link
Contributor

Note that you'll need to be testing this with a compiler built from the master branch. Swift 4.0 still crashes on this code.

@AnthonyLatsis
Copy link
Collaborator

PR - #16431

@AnthonyLatsis
Copy link
Collaborator

did you mean the subscript operator if there are subscripts, fixits if the inputs are compatible, an additional declared here note if there is only one candidate.

@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

3 participants