apple / sourcekit-lsp

Language Server Protocol implementation for Swift and C-based languages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

fireplusteam opened this issue · comments

"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

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

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.

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.

@ahoppen , That works in xCode though