Skip to content

Commit a86a588

Browse files
committed
resolve #1212
1 parent a93effc commit a86a588

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
print(x) -- `x` is `string` here
3535
end
3636
```
37+
* `CHG` infer type by `>`/`<`/`>=`/`<=`
3738
* `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file.
3839
* `FIX` [#1204](https://github.com/sumneko/lua-language-server/issues/1204)
3940
* `FIX` [#1208](https://github.com/sumneko/lua-language-server/issues/1208)

script/vm/compiler.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,36 @@ local compilerSwitch = util.switch()
17861786
return
17871787
end
17881788
end
1789+
if source.op.type == '>'
1790+
or source.op.type == '<'
1791+
or source.op.type == '>='
1792+
or source.op.type == '<=' then
1793+
local a = vm.getNumber(source[1])
1794+
local b = vm.getNumber(source[2])
1795+
if a and b then
1796+
local result
1797+
if source.op.type == '>' then
1798+
result = a > b
1799+
elseif source.op.type == '<' then
1800+
result = a < b
1801+
elseif source.op.type == '>=' then
1802+
result = a >= b
1803+
elseif source.op.type == '<=' then
1804+
result = a <= b
1805+
end
1806+
vm.setNode(source, {
1807+
type = 'boolean',
1808+
start = source.start,
1809+
finish = source.finish,
1810+
parent = source,
1811+
[1] = result,
1812+
})
1813+
return
1814+
else
1815+
vm.setNode(source, vm.declareGlobal('type', 'boolean'))
1816+
return
1817+
end
1818+
end
17891819
end)
17901820

17911821
---@param source vm.object

test/type_inference/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ TEST 'integer' [[
154154
<?x?> = ~ 1
155155
]]
156156

157+
TEST 'boolean' [[
158+
<?x?> = 1 < 2
159+
]]
160+
157161
TEST 'integer' [[
158162
local a = true
159163
local b = 1

0 commit comments

Comments
 (0)