Skip to content

Commit 4d73939

Browse files
committed
fix incorrect type check for generic with nil
1 parent e7f8bc7 commit 4d73939

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# changelog
22

33
## 3.6.2
4+
* `FIX` incorrect type check for generic with nil
45
* `FIX` [#1676]
6+
57
[#1676]: https://github.com/sumneko/lua-language-server/issues/1676
68

79
## 3.6.1

script/vm/generic.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ local function cloneObject(source, resolved)
3636
end
3737
if source.type == 'doc.type' then
3838
local newType = {
39-
type = source.type,
40-
start = source.start,
41-
finish = source.finish,
42-
parent = source.parent,
43-
types = {},
39+
type = source.type,
40+
start = source.start,
41+
finish = source.finish,
42+
parent = source.parent,
43+
optional = source.optional,
44+
types = {},
4445
}
4546
for i, typeUnit in ipairs(source.types) do
4647
local newObj = cloneObject(typeUnit, resolved)

script/vm/type.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ function vm.isSubType(uri, child, parent, mark, errs)
355355
return true
356356
end
357357
end
358+
if n.type == 'doc.generic.name' then
359+
return true
360+
end
358361
end
359362
if parent:isOptional() then
360363
if vm.isSubType(uri, child, 'nil', mark, errs) == true then

test/diagnostics/type-check.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,32 @@ end
10881088
func('hello', 'world')
10891089
]]
10901090

1091+
TEST [[
1092+
---@generic T1, T2, T3, T4, T5
1093+
---@param f fun(): T1?, T2?, T3?, T4?, T5?
1094+
---@return T1?, T2?, T3?, T4?, T5?
1095+
local function foo(f)
1096+
return f()
1097+
end
1098+
1099+
local a, b = foo(function()
1100+
return 1
1101+
end)
1102+
]]
1103+
1104+
TEST [[
1105+
---@generic T1, T2, T3, T4, T5
1106+
---@param f fun(): T1|nil, T2|nil, T3|nil, T4|nil, T5|nil
1107+
---@return T1?, T2?, T3?, T4?, T5?
1108+
local function foo(f)
1109+
return f()
1110+
end
1111+
1112+
local a, b = foo(function()
1113+
return 1
1114+
end)
1115+
]]
1116+
10911117
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
10921118
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
10931119
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)