Skip to content

Returned value of class methods not detected properly #1418

@kenzocarneiro

Description

@kenzocarneiro

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions