LuaLS / LuaLS/lua-language-server

False negative for param-type-mismatch when using a variable as argument

Open
#2,734 0 comments 0 reactions 0 assignees View on GitHub

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

### Expected Behaviour

```lua
---@class (exact) Foo
---@field FooField integer
local Foo

---@param FooParam Foo
local function test(FooParam) end

local x = {z = 1}
test(x)

test({z = 1})
```

In the example above, Lua LS should report a param-type-mismatch diagnostic for `test(x)`. It has enough information to know that x cannot be of type Foo, at the very least because it is missing `FooField`.
We see that test({z=1}) correctly reports the param-type-mismatch diagnostic.

### Actual Behaviour

No error is reported.
![image](https://github.com/LuaLS/lua-language-server/assets/36710691/5bd6c68a-21ab-4235-b4e3-2f5c7f9a87de)

### Reproduction steps

In any editor with Lua LS installed, write the provided code. Observe that there are no squiggles reported.

### Additional Notes

I would say this is a pretty serious bug. I would have expected Lua LS's type checking to be robust enough to handle checking the types of variables. It seems that all it is doing is checking that the type is a table.

IS this because there is no narrowing of variables once they become tables? This is only done for table literals?

### Log File

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Lua reproduction in Lua LS and trace the type-checking path for the parameter passed to test, comparing the variable argument with the table literal. Done means the same param-type-mismatch diagnostic is reported for test(x), including the missing FooField case.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.