Skip to content

Commit cc9bf96

Browse files
committed
infer type by local tp = type(x)
1 parent c61eefc commit cc9bf96

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
if type(x) == 'string' then
3434
print(x) -- `x` is `string` here
3535
end
36+
37+
local tp = type(x)
38+
39+
if tp == 'boolean' then
40+
print(x) -- `x` is `boolean` here
41+
end
3642
```
3743
* `CHG` infer type by `>`/`<`/`>=`/`<=`
3844
* `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file.

script/vm/runner.lua

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ function mt:_lookInto(action, topNode, outNode)
119119
return topNode, outNode
120120
end
121121
if self._mark[action] then
122-
return
122+
return topNode, outNode
123123
end
124124
self._mark[action] = true
125125
local top = self._objs[self._index]
126126
if not top then
127127
return topNode, outNode
128128
end
129-
if not guide.isInRange(action, top.finish) then
129+
if not guide.isInRange(action, top.finish)
130+
-- trick for `local tp = type(x);if tp == 'string' then`
131+
and action.type ~= 'binary' then
130132
return topNode, outNode
131133
end
132134
local set
@@ -220,6 +222,7 @@ function mt:_lookInto(action, topNode, outNode)
220222
end
221223
if exp.type == 'getlocal'
222224
and exp.node == self._loc then
225+
-- if x == y then
223226
self:_fastWard(exp.finish, topNode:copy())
224227
local checkerNode = vm.compileNode(checker)
225228
if action.op.type == '==' then
@@ -240,6 +243,7 @@ function mt:_lookInto(action, topNode, outNode)
240243
and exp.args[1]
241244
and exp.args[1].type == 'getlocal'
242245
and exp.args[1].node == self._loc then
246+
-- if type(x) == 'string' then
243247
self:_fastWard(exp.finish, topNode:copy())
244248
if action.op.type == '==' then
245249
topNode:narrow(checker[1])
@@ -252,6 +256,34 @@ function mt:_lookInto(action, topNode, outNode)
252256
outNode:narrow(checker[1])
253257
end
254258
end
259+
elseif exp.type == 'getlocal'
260+
and checker.type == 'string' then
261+
local nodeValue = vm.getObjectValue(exp.node)
262+
if nodeValue
263+
and nodeValue.type == 'select'
264+
and nodeValue.sindex == 1 then
265+
local call = nodeValue.vararg
266+
if call
267+
and call.type == 'call'
268+
and call.node.special == 'type'
269+
and call.args
270+
and call.args[1]
271+
and call.args[1].type == 'getlocal'
272+
and call.args[1].node == self._loc then
273+
-- `local tp = type(x);if tp == 'string' then`
274+
if action.op.type == '==' then
275+
topNode:narrow(checker[1])
276+
if outNode then
277+
outNode:remove(checker[1])
278+
end
279+
else
280+
topNode:remove(checker[1])
281+
if outNode then
282+
outNode:narrow(checker[1])
283+
end
284+
end
285+
end
286+
end
255287
end
256288
end
257289
elseif action.type == 'call' then

test/type_inference/init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,3 +2618,12 @@ if type(x) == 'function' then
26182618
print(<?x?>)
26192619
end
26202620
]]
2621+
2622+
TEST 'integer' [[
2623+
local x
2624+
local tp = type(x)
2625+
2626+
if tp == 'integer' then
2627+
print(<?x?>)
2628+
end
2629+
]]

0 commit comments

Comments
 (0)