diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-06-21 17:05:04 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-06-21 17:05:04 +0800 |
commit | f76cd50992dab57a57c61e8e6f5f788745544da9 (patch) | |
tree | 03b49b1b32f8044ca4c7d1dc16d01a068c354166 | |
parent | 8af599790936f81cddaecb31d36eede70ad3739c (diff) | |
download | lua-language-server-f76cd50992dab57a57c61e8e6f5f788745544da9.zip |
resolve #1221 generic with optional
-rw-r--r-- | meta/template/basic.lua | 2 | ||||
-rw-r--r-- | script/vm/sign.lua | 18 | ||||
-rw-r--r-- | test/type_inference/init.lua | 36 |
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) +]] |