summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--meta/template/basic.lua2
-rw-r--r--script/vm/sign.lua18
-rw-r--r--test/type_inference/init.lua36
3 files changed, 51 insertions, 5 deletions
diff --git a/meta/template/basic.lua b/meta/template/basic.lua
index b4913cc2..4509258e 100644
--- a/meta/template/basic.lua
+++ b/meta/template/basic.lua
@@ -6,7 +6,7 @@ arg = {}
---#DES 'assert'
---@generic T
----@param v T
+---@param v? T
---@param message? any
---@return T
function assert(v, message) end
diff --git a/script/vm/sign.lua b/script/vm/sign.lua
index 5c17aa3d..0f5962fd 100644
--- a/script/vm/sign.lua
+++ b/script/vm/sign.lua
@@ -122,13 +122,20 @@ function mt:resolve(uri, args, removeGeneric)
-- remove un-generic type
---@param argNode vm.node
+ ---@param sign vm.node
---@param knownTypes table<string, true>
---@return vm.node
- local function buildArgNode(argNode, knownTypes)
+ local function buildArgNode(argNode, sign, knownTypes)
local newArgNode = vm.createNode()
+ local needRemoveNil = sign:hasFalsy()
for n in argNode:eachObject() do
- if argNode:hasFalsy() then
- goto CONTINUE
+ if needRemoveNil then
+ if n.type == 'nil' then
+ goto CONTINUE
+ end
+ if n.type == 'global' and n.cate == 'type' and n.name == 'nil' then
+ goto CONTINUE
+ end
end
local view = vm.viewObject(n, uri)
if knownTypes[view] then
@@ -137,6 +144,9 @@ function mt:resolve(uri, args, removeGeneric)
newArgNode:merge(n)
::CONTINUE::
end
+ if not needRemoveNil and argNode:isOptional() then
+ newArgNode:addOptional()
+ end
return newArgNode
end
@@ -158,7 +168,7 @@ function mt:resolve(uri, args, removeGeneric)
local argNode = vm.compileNode(arg)
local knownTypes, genericNames = getSignInfo(sign)
if not isAllResolved(genericNames) then
- local newArgNode = buildArgNode(argNode, knownTypes)
+ local newArgNode = buildArgNode(argNode,sign, knownTypes)
for n in sign:eachObject() do
resolve(n, newArgNode)
end
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 9bd66761..5fa70f5f 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -2799,3 +2799,39 @@ m.x = {} --> 4th
print(m.<?x?>)
]]
+
+TEST 'boolean?' [[
+---@generic T
+---@param x T
+---@return T
+local function echo(x) end
+
+---@type boolean?
+local b
+
+local <?x?> = echo(b)
+]]
+
+TEST 'boolean' [[
+---@generic T
+---@param x T?
+---@return T
+local function echo(x) end
+
+---@type boolean?
+local b
+
+local <?x?> = echo(b)
+]]
+
+TEST 'boolean' [[
+---@generic T
+---@param x? T
+---@return T
+local function echo(x) end
+
+---@type boolean?
+local b
+
+local <?x?> = echo(b)
+]]