diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/vm/compiler.lua | 10 | ||||
-rw-r--r-- | script/vm/value.lua | 18 | ||||
-rw-r--r-- | test/type_inference/init.lua | 20 |
4 files changed, 31 insertions, 18 deletions
diff --git a/changelog.md b/changelog.md index a228acbc..7c5295b4 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 3.4.1 * `NEW` settings: * `type.weakNilCheck` +* `FIX` [#1256](https://github.com/sumneko/lua-language-server/issues/1256) * `FIX` [#1257](https://github.com/sumneko/lua-language-server/issues/1257) ## 3.4.0 diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index bf0cc44b..1ba20785 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -1033,13 +1033,14 @@ local binarySwich = util.switch() : call(function (source) local node1 = vm.compileNode(source[1]) local node2 = vm.compileNode(source[2]) - local r1 = vm.testCondition(source[1]) + local r1 = vm.testCondition(source[1]) if r1 == true then vm.setNode(source, node2) elseif r1 == false then vm.setNode(source, node1) else - vm.setNode(source, node2) + local node = node1:copy():setFalsy():merge(node2) + vm.setNode(source, node) end end) : case 'or' @@ -1052,9 +1053,8 @@ local binarySwich = util.switch() elseif r1 == false then vm.setNode(source, node2) else - vm.getNode(source):merge(node1) - vm.getNode(source):setTruthy() - vm.getNode(source):merge(node2) + local node = node1:copy():setTruthy():merge(node2) + vm.setNode(source, node) end end) : case '==' diff --git a/script/vm/value.lua b/script/vm/value.lua index 83265603..c4f8e96d 100644 --- a/script/vm/value.lua +++ b/script/vm/value.lua @@ -22,24 +22,16 @@ function vm.testCondition(source) if n[1] == false then hasFalse = true end - end - if n.type == 'global' and n.cate == 'type' then - if n.name == 'true' then - hasTrue = true - end + elseif n.type == 'global' and n.cate == 'type' then if n.name == 'false' or n.name == 'nil' then hasFalse = true + else + hasTrue = true end - end - if n.type == 'nil' then + elseif n.type == 'nil' then hasFalse = true - end - if n.type == 'string' - or n.type == 'number' - or n.type == 'integer' - or n.type == 'table' - or n.type == 'function' then + elseif guide.isLiteral(n) then hasTrue = true end end diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 624cf4b9..0c9be21e 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -3250,3 +3250,23 @@ local function f() end for x, <?y?> in f do end ]] + +TEST 'number|nil' [[ +---@type table|nil +local a + +---@type number|nil +local b + +local <?c?> = a and b +]] + +TEST 'number|table|nil' [[ +---@type table|nil +local a + +---@type number|nil +local b + +local <?c?> = a or b +]] |