Skip to content

Commit

Permalink
fix #1256
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 1, 2022
1 parent 342ff9a commit ab1838e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 '=='
Expand Down
18 changes: 5 additions & 13 deletions script/vm/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/type_inference/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
]]

0 comments on commit ab1838e

Please sign in to comment.