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

Returned value of class methods not detected properly #1418

Closed
kenzocarneiro opened this issue Jul 31, 2022 · 1 comment
Closed

Returned value of class methods not detected properly #1418

kenzocarneiro opened this issue Jul 31, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@kenzocarneiro
Copy link

Describe the bug
I have a variable "v" of type "Vector". When I use the method "normalized" on the Vector, the extension doesn't detect the return value properly.

Here is the code:

--- Class representing Vectors.
--- @class Vector
--- @field x number
--- @field y number
Vector = {x = 0; y = 0}

--- Constructor of Vector.
--- @param x number
--- @param y number
--- @return Vector
function Vector:new(x, y)
    local e = {}
    setmetatable(e, self)
    self.__index = self
    e.x = x or self.x
    e.y = y or self.y
    return e
end

--- Normalize the Vector.
--- @return Vector
function Vector:normalized()
    local len = math.sqrt(self.x^2 + self.y^2)
    return Vector:new(self.x / len, self.y / len)
end

local v = Vector:new(1, 1)
v = v:normalized()
print(v.x)

Before v:normalized, v is detected properly:
image

But after the assignment to the method result, v isn't recognized anymore (however, the description of the class is still there...):
image

The weird thing about this bug is that when using a temporary variable, "v" is detected properly:

local v = Vector:new(1, 1)
local v2 = v:normalized()
v = v2
print(v.x)

Result:
image

To reproduce

  • Create a class Vector
  • Add a constructor and other methods
  • Add a method "normalized" that returns a Vector
  • Create a Vector "v"
  • Re-assign it to v:normalized()

This bug doesn't happen everytime, but reloading and/or copying the code triggers it again.

Expected behavior
v should always be detected as a Vector, even after v:normalized(), since this method returns a Vector.

Environment:

  • OS: Windows 10 x64
  • Is WSL remote? No
  • Client: VSCode
@carsakiller
Copy link
Collaborator

Interesting… I was unable to replicate initially, but after reloading VS Code, I did start seeing that print(v.x) was reporting v as you showed in your second image. I was able to work around this though by marking v = v:normalized() as local:

local v = Vector:new(1, 1)
local v = v:normalized()
print(v.x)

However, I don't recommend overwriting your local variables, and the server will also warn against this.

This appears to be something acting up when reassigning a local variable. There are a few other ways to do this that can avoid this problem, depending on your situation:

local v = Vector:new(1, 1):normalized()
print(v.x)
local v = Vector:new(1, 1)
local vNorm = v:normalized()
print(vNorm.x)

@sumneko sumneko added the bug Something isn't working label Aug 1, 2022
@sumneko sumneko closed this as completed in c234004 Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants