LuaLS / LuaLS/lua-language-server

@generic 不验证类型是否一致

Open
#3,454 1 comment 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

假设有这样的代码:

---@class TestBase
---@class TestDerived1 : TestBase
---@class TestDerived2 : TestBase

---@generic T:TestBase
---@param v1 T
---@param v2 T
local function foo(v1, v2) end

我给 foov1, v2 标注了同一个 T,因此期望它们类型是一致的

local t1 ---@type TestDerived1
local t2 ---@type TestDerived2

foo(t1, t2)

这应该报错

Actual Behaviour

可实际上没有。因为泛型只对每一个参数进行独立的多态类型检查,都继承同一个父类自然没有报错。

当然,同一个泛型的一致性是完全一致还是多态一致,可以归结为一种设计考虑。当前的逻辑也许不算太坏。

但是,如果再加上返回值,结果就会变得很奇怪。

Image

返回值同样是 T,可是却变成第一个参数的类型了!无论从哪个角度想都不合理。

Reproduction steps

复制上述代码查看悬停类型提示即可

Additional Notes

建议:

校验由 @generic 标注的同一个泛型必须是同一类型

或者:在类型不同时不要推断为第一个类型而是基类类型

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

No source file or test is named. Reproduce the issue with the provided Lua snippet and inspect the generic type-checking and return-type inference paths, then trace how one generic parameter is bound across multiple arguments. Done means the chosen same-type behavior is reflected consistently in diagnostics and hover results, with regression coverage if the project has an applicable test location.

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
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.