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-5324] Better diagnostic when instance member of outer type is referenced from nested type #47898

Closed
jckarter opened this issue Jun 27, 2017 · 6 comments
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement

Comments

@jckarter
Copy link
Member

Previous ID SR-5324
Radar rdar://problem/33015870
Original Reporter @jckarter
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug
Assignee kacperh (JIRA)
Priority Medium

md5: 89ed2369cd8bd72c149a19b747b77277

Issue Description:

If you attempt something like this:

struct Outer {
  var outer: Int

  struct Inner {
    var inner: Int

    func sum() -> Int {
      return inner + outer
    }
  }
}

then you get a somewhat confusing error message:

foo.swift:8:22: error: instance member 'outer' cannot be used on type 'Outer'
      return inner + outer
                     ^~~~~

since the member isn't really being used on the outer type, but on an instance of the inner type. A specialized diagnostic that said "instance member 'outer' of type 'Outer' cannot be accessed on instance of nested type 'Outer.Inner'" would be easier to understand.

@swift-ci
Copy link
Collaborator

Comment by KacperHarasim (JIRA)

Hi @jckarter ,

I've tried to tackle this issue and came up with this to check this condition:

      // Check whether the instance member is declared on parent context and if so
      // provide more specialized message
      auto memberTypeContext = member->getDeclContext()->getInnermostTypeContext();
      auto currentTypeContext = CS.DC->getInnermostTypeContext();
      if (memberTypeContext->getSyntacticDepth() < currentTypeContext->getSyntacticDepth()) {
           //Diagnose...
      }

It's my first ever look at the Swift compiler. Does it make any sense? It's surely working 🙂 I don't want to make a PR since I'm not sure whether it's clean solution in terms of compiler.

Thanks for the help!

@jckarter
Copy link
Member Author

Thanks for looking into this! @xedin or @rudkx would be better judges of the approach here. Don't be afraid to start a PR on Github; even if you have an incomplete approach, it's still a good environment for reviewing and discussing the implementation.

@xedin
Copy link
Member

xedin commented Aug 24, 2017

kacperh (JIRA User) I concur with @jckarter here, thank for looking at this and please open a PR and we'll discuss there, please /cc me there! 🙂

@swift-ci
Copy link
Collaborator

Comment by KacperHarasim (JIRA)

Will do, thanks! 🙂

@swift-ci
Copy link
Collaborator

Comment by KacperHarasim (JIRA)

PR opened to implement this: #11609

@xedin
Copy link
Member

xedin commented Sep 19, 2017

Resolved by #11609

@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
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

3 participants