LuaLS / LuaLS/lua-language-server
Union table|class doesn't accept a generic table and only accepts a class instance (causing false missing-fields diagnostic)
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
### How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
### Which OS are you using?
Windows
### What is the issue affecting?
Type Checking, Diagnostics/Syntax Checking
### Expected Behaviour
When a function is declared as accepting both `table` and a custom class type, it should accept getting passed a generic table.
### Actual Behaviour
LLS insists on a table matching the class being passed (with all the fields set) and reports the missing-fields diagnostic when passing a generic table.
### Reproduction steps
LLS definition file contains the following:
```lua
---@meta
---@class TestClass
---@field TestField number A test field in test class
TestClass = {}
-- TestFunction1
---@param generic_table_or_testclass_instance table|TestClass
function TestFunction1(generic_table_or_testclass_instance) end
-- TestFunction2
---@param generic_table table
---@param testclass_instance TestClass
---@overload fun(generic_table: table)
---@overload fun(testclass_instance: TestClass)
function TestFunction2(generic_table, testclass_instance) end
-- TestFunction3
---@param generic_table table
function TestFunction3(generic_table) end
-- TestFunction3
---@param testclass_instance TestClass
function TestFunction3(testclass_instance) end
```
And then a lua code file contains a call to either test function like
```lua
TestFunction1({1,2,3})
TestFunction2({1,2,3})
TestFunction3({1,2,3})
```
LLS reports `Missing required fields in type ``TestClass``: ``TestField`` Lua Diagnostics.(missing-fields)` for all 3 calls. Instead it should accept the table as input parameter for the 3 functions.
### Additional Notes
_No response_
### Log File
_No response_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Lua and type annotations in the reproduction, then trace the type-checking path that produces the missing-fields diagnostic for union and overload parameters. Done means generic tables are accepted for all three calls without reporting TestClass.TestField as missing; no source files or tests are named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100