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

"Goto reference" stops working if there's an error in a line of code of instance method #1048

Open
fireplusteam opened this issue Feb 4, 2024 · 4 comments

Comments

@fireplusteam
Copy link

"Go To Reference" stops working if there's an error in a line of code of instance method.
This issue appears to only be reproduced when used as an instance method in Package.swift project.. There is no problem with global methods.

For example:

struct Foo {
    func callMethod() {
        bla(B: "", d: 10) // THIS "Goto references" doesn't work
    }

    func bla( // this cannot goto references
        B: String,
        d: String  
    ) {

    }
}
func callMethod() {
    bla(B: "", d: 10) // THIS "Goto references" works fine even though it has an error in a line
}

func bla( // this can goto references
    B: String,
    d: String  
) {

}

Can be reproduced with VS Code, swift extension

@ahoppen
Copy link
Collaborator

ahoppen commented Feb 4, 2024

Tracked in Apple’s issue tracker as rdar://122261868

@ahoppen
Copy link
Collaborator

ahoppen commented Feb 5, 2024

Reduced test case

func testReferences1() async throws {
  let ws = try await IndexedSingleSwiftFileWorkspace(
    """
    struct Foo {
      func callMethod() {
        1️⃣bla(B: "", d: 10)
      }

      func bla(B: String, d: String) {}
    }
    """,
    allowBuildFailure: true
  )

  let response = try await ws.testClient.send(
    ReferencesRequest(
      textDocument: TextDocumentIdentifier(ws.fileURI),
      position: ws.positions["1️⃣"],
      context: ReferencesContext(includeDeclaration: true)
    )
  )
  XCTAssertEqual(response.count, 2)
}

When removing struct Foo { and the final }, the test passes.

@ahoppen
Copy link
Collaborator

ahoppen commented Feb 27, 2024

Thinking about this, I think we should not report a reference in either case because bla never unambiguously references the function or method.

The reason why we do report the reference to the top-level function currently is that it gets type checked into a DeclRefExpr, which apparently resolves to the top-level function, while in the member case it gets type checked to a UnresolvedDotExpr, ie. a member access on self and that doesn’t get resolved during type checking.

@fireplusteam
Copy link
Author

@ahoppen , That works in xCode though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants