-
-
Notifications
You must be signed in to change notification settings - Fork 410
Description
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Linux
What is the issue affecting?
Type Inference
Expected Behaviour
When a child class overrides a parent class method that was declared using @field or @type annotations, the parameter types should be correctly inferred from the parent class definition.
---@class Buff
local mt = {}
---@type (fun(self: Buff, target: Buff): boolean)?
mt.on_cover = nil
---@class Buff.CommandAura : Buff
local tpl = {}
function tpl:on_cover(target)
-- Expected: target type should be Buff
return self.level > target.level
endActual Behaviour
The parameter types are inferred as any instead of the types defined in the parent class.
---@class Buff
local mt = {}
---@type (fun(self: Buff, target: Buff): boolean)?
mt.on_cover = nil
---@class Buff.CommandAura : Buff
local tpl = {}
function tpl:on_cover(target)
-- Actual: target type is any ❌
return self.level > target.level
endThis also affects @field declarations:
---@class Animal
---@field can_eat (fun(self: Animal, other: Animal): boolean)?
local base = {}
---@class Dog : Animal
local dog = {}
function dog:can_eat(other)
-- Actual: other type is any ❌
return true
endReproduction steps
- Define a parent class with a method using
@typeor@fieldannotation - Create a child class that inherits from the parent
- Override the method in the child class
- Check the parameter types - they will be
anyinstead of the parent's types
Additional Notes
This issue is related to #2569, but specifically focuses on field-style function declarations (@field and @type) rather than method declarations (function Class:method()).
PR #2859 successfully implemented type inference for method declaration style overrides, but did not cover field-style declarations.
Root cause: In script/vm/compiler.lua, the compileFunctionParam function only checks for 'doc.type.function' and 'function' node types, but @field nodes have type 'doc.field' with the actual function type stored in the extends property.
Log File
No response