Skip to content

Infer function parameter types when overriding the same-named class function in an instance of that class - #2859

Merged
sumneko merged 4 commits into
LuaLS:masterfrom
tomlau10:feat/param_infer_for_override
Sep 18, 2024
Merged

Infer function parameter types when overriding the same-named class function in an instance of that class#2859
sumneko merged 4 commits into
LuaLS:masterfrom
tomlau10:feat/param_infer_for_override

Conversation

@tomlau10

@tomlau10tomlau10 commented Sep 18, 2024

Copy link
Copy Markdown
Contributor

resolves#2158, #2569 (comment)

The problem

Sometimes we will want to define interface for overriding in instance object of that class, and retain the param types defined in the interface while overriding. But currently no way to do so as described in #2158 and #2569 (comment)

---@classAlocalA= {}
---@returnAfunctionA.new() end---@paramxnumberfunctionA:user_callback(x) endlocala=A.new()
functiona:user_callback(x) end-- no type infer for `x`, also throwing `duplicate-set-field` warning

Proposed change

Detailed explanation here: #2569 (comment)

Expected Result

---@classAlocalA= {}
---@returnAfunctionA.new() end---@paramxnumberfunctionA:user_callback(x) endlocala=A.new()
functiona:user_callback(x)
-- x -> number-- and no more `duplicate-set-field` warning :)end

Additional notes

  • The way that I modified duplicate-set-field diagnostic is allowing an instance variable to override any of its class function/method.
    i.e. all function/methods from a class can be overriden in variable of that type, without any duplicate-set-field warning.
  • This auto infer only works in a class variable / instance variable pair ✅
    i.e. it doesn't work when the namespace is just a global variable ❌
--- file aglobal_namespace= {}
---@paramxnumberfunctionglobal_namespace.user_callback(x) end--- file bfunctionglobal_namespace.user_callback(x)
-- this will not work, because this is not in `class/type` variable pairend
  • I believe a @override will be definitely needed for that case, as otherwise no way to distinguish between setting duplicate field or really doing override 😕

中文版

  • 支持在 instance variable 下 override 其 class function 時做 auto param type infer
  • 同時忽略在這種 use case 下的 duplicate-set-field warning
  • 這個只針對 class/type variable 組合下的 override 場景 (例子見上邊)
    • 並不處理 define 成 global namespace 後再做 override 的使用場景
    • 該種 use case 似必需等待 @override 語法支持了,否則不能區分是出現 duplicate 還是真的想 override

@tomlau10tomlau10 changed the title Infer function parameter types when overriding the same-named class function or method in a type variableInfer function parameter types when overriding the same-named class function in an instance of that classSep 18, 2024
@tomlau10
tomlau10force-pushed the feat/param_infer_for_override branch from 58fa120 to 75cf4cfCompareSeptember 18, 2024 06:11
@sumneko
sumneko merged commit 8f96025 into LuaLS:masterSep 18, 2024
@sumneko

Copy link
Copy Markdown
Collaborator

Thank you!

@tomlau10
tomlau10 deleted the feat/param_infer_for_override branch September 19, 2024 02:50
@tomlau10tomlau10 mentioned this pull request Sep 10, 2025
ChouUn added a commit to ChouUn/lua-language-server that referenced this pull request Mar 5, 2026
…ns in method overrides
When a child class overrides a parent class method that was declared using
@field or @type annotations (instead of function declarations), the parameter
types are now correctly inferred from the parent class.
This extends the existing method override type inference (PR LuaLS#2859) to support
field-style function declarations.
Example:
```lua
---@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)
-- target type is now correctly inferred as Buff (was any before)
return self.level > target.level
end
```
Changes:
- Modified compileFunctionParam in script/vm/compiler.lua to extract function
type from doc.field nodes by accessing their extends property
- Added support for doc.type.function in parent class field lookup
- Added comprehensive test cases for @field and @type method overrides
FixesLuaLS#3367
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for overriding functions

2 participants

@tomlau10@sumneko