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

"Go to Declaration" does not work when the type was asserted to a different type #782

Open
sonnguyen9800 opened this issue Sep 25, 2024 · 1 comment
Labels
blocked/external Blocked on external factor bug Something isn't working

Comments

@sonnguyen9800
Copy link

sonnguyen9800 commented Sep 25, 2024

Problem

When using the Luau Language Server (johnymorgan) with VSCode, I cannot use the "Go to Declaration" functionality for functions defined on tables with metatables and type annotations. This occurs in cases where functions are defined for a table using a type that employs setmetatable for object-like behavior.

Reproduction

Here’s an example:

type AccountImpl = {
    __index: AccountImpl,
    new: (name: string, balance: number) -> AccountType,
    deposit: (self: AccountType, credit: number) -> (),
    withdraw: (self: AccountType, debit: number) -> (),
}

type AccountType = typeof(setmetatable({} :: { name: string, balance: number }, {} :: AccountImpl))

local MyAccount: AccountImpl = {} :: AccountImpl
MyAccount.__index = MyAccount

function MyAccount.new(name: string, balance: number): AccountType
    local self = {}
    self.name = name
    self.balance = balance
    return setmetatable(self, MyAccount)
end

function MyAccount:deposit(credit: number)
    self.balance += credit
end

function MyAccount:withdraw(debit: number)
    self.balance -= debit
end

local account = MyAccount.new("Alexander", 500)
account:withdraw(100) -- cannot go to declaration

Expected Behavior

When right-clicking account:withdraw(100) or MyAccount.new(...) and selecting "Go to Declaration," the LSP should navigate to the respective function definitions (MyAccount:withdraw or MyAccount.new).

Observed Behavior

"Go to Declaration" does not work for either the method (withdraw) or the function (new).

Additional Information

Luau Language Server version: 1.32.4
VSCode version: 1.93.1
Reproduction environment: Window

P/S

  • I have tried adding explicit type annotations, but the issue persists.
  • If I use Nightrains's Roblox-LSP then the issue no longer persist

Update

  • I also cannot find "references" & "go to definitions".

Update #2

So basically I cannot find "references" or "go to definition" if tables assigned to any custom type

The code below is working

local Sample = {}
Sample.__index = Sample

function Sample.HelloWorld(): () 
    print("LOL")
end

Sample.HelloWorld() -- Can go to the definition
return Sample

While the other is not

local Sample = {} :: ACustomType
Sample.__index = Sample

function Sample.HelloWorld(): () 
    print("LOL")
end

Sample.HelloWorld() -- Can't go to the definition
return Sample
@JohnnyMorganz JohnnyMorganz added the bug Something isn't working label Sep 27, 2024
@JohnnyMorganz
Copy link
Owner

We rely on Luau's type tracking for function definitions and go to declaration.

When you apply a type assertion, then the Luau type checker doesn't ever care / track your definition of Sample.HelloWorld anymore when you call the function. It is going to try and find the definition of HelloWorld inside of ACustomType.

I don't think there is much we can do here to solve this, because really it is working as intended

@JohnnyMorganz JohnnyMorganz changed the title Issue: Cannot "Go to Declaration" for functions on tables of a type using metatables "Go to Declaration" does not work when the type was asserted to a different type Dec 28, 2024
@JohnnyMorganz JohnnyMorganz added the blocked/external Blocked on external factor label Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked/external Blocked on external factor bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants