summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--changelog.md2
-rw-r--r--script/vm/generic.lua11
-rw-r--r--script/vm/type.lua3
-rw-r--r--test/diagnostics/type-check.lua26
4 files changed, 37 insertions, 5 deletions
diff --git a/changelog.md b/changelog.md
index 19e8d5c9..4c9ba5cb 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,7 +1,9 @@
# changelog
## 3.6.2
+* `FIX` incorrect type check for generic with nil
* `FIX` [#1676]
+
[#1676]: https://github.com/sumneko/lua-language-server/issues/1676
## 3.6.1
diff --git a/script/vm/generic.lua b/script/vm/generic.lua
index e6b47392..4ac7374d 100644
--- a/script/vm/generic.lua
+++ b/script/vm/generic.lua
@@ -36,11 +36,12 @@ local function cloneObject(source, resolved)
end
if source.type == 'doc.type' then
local newType = {
- type = source.type,
- start = source.start,
- finish = source.finish,
- parent = source.parent,
- types = {},
+ type = source.type,
+ start = source.start,
+ finish = source.finish,
+ parent = source.parent,
+ optional = source.optional,
+ types = {},
}
for i, typeUnit in ipairs(source.types) do
local newObj = cloneObject(typeUnit, resolved)
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 57a6444f..32b8e50b 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -355,6 +355,9 @@ function vm.isSubType(uri, child, parent, mark, errs)
return true
end
end
+ if n.type == 'doc.generic.name' then
+ return true
+ end
end
if parent:isOptional() then
if vm.isSubType(uri, child, 'nil', mark, errs) == true then
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index 434cf5bc..ff2eddea 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -1088,6 +1088,32 @@ end
func('hello', 'world')
]]
+TEST [[
+---@generic T1, T2, T3, T4, T5
+---@param f fun(): T1?, T2?, T3?, T4?, T5?
+---@return T1?, T2?, T3?, T4?, T5?
+local function foo(f)
+ return f()
+end
+
+local a, b = foo(function()
+ return 1
+end)
+]]
+
+TEST [[
+---@generic T1, T2, T3, T4, T5
+---@param f fun(): T1|nil, T2|nil, T3|nil, T4|nil, T5|nil
+---@return T1?, T2?, T3?, T4?, T5?
+local function foo(f)
+ return f()
+end
+
+local a, b = foo(function()
+ return 1
+end)
+]]
+
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')