Skip to content

Commit e88c7d0

Browse files
committed
resolve #1154 resolve #1165
infer type by `t and t.x`
1 parent 96b4e0b commit e88c7d0

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

changelog.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
```
1111
* `CHG` infer type by `error`
1212
```lua
13-
---@type integer?
13+
---@type integer|nil
1414
local n
1515

1616
if not n then
@@ -19,6 +19,13 @@
1919

2020
print(n) -- `n` is `integer` here
2121
```
22+
* `CHG` infer type by `t and t.x`
23+
```lua
24+
---@type table|nil
25+
local t
26+
27+
local s = t and t.x or 1 -- `t` in `t.x` is `table`
28+
```
2229
* `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file.
2330
* `FIX` [#1204](https://github.com/sumneko/lua-language-server/issues/1204)
2431
* `FIX` [#1208](https://github.com/sumneko/lua-language-server/issues/1208)

script/vm/runner.lua

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local guide = require 'parser.guide'
88
---@field _loc parser.object
99
---@field _objs parser.object[]
1010
---@field _callback vm.runner.callback
11+
---@field _mark table
1112
local mt = {}
1213
mt.__index = mt
1314
mt._index = 1
@@ -117,6 +118,17 @@ function mt:_lookInto(action, topNode, outNode)
117118
if not action then
118119
return topNode, outNode
119120
end
121+
if self._mark[action] then
122+
return
123+
end
124+
self._mark[action] = true
125+
local top = self._objs[self._index]
126+
if not top then
127+
return topNode, outNode
128+
end
129+
if not guide.isInRange(action, top.finish) then
130+
return topNode, outNode
131+
end
120132
local set
121133
local value = vm.getObjectValue(action)
122134
if value then
@@ -232,10 +244,10 @@ function mt:_lookInto(action, topNode, outNode)
232244
self:_lookInto(arg, topNode)
233245
end
234246
end
235-
elseif action.type == 'return' then
236-
for _, rtn in ipairs(action) do
237-
self:_lookInto(rtn, topNode)
238-
end
247+
else
248+
guide.eachSourceContain(action, top.finish, function(source)
249+
self:_lookInto(source, topNode)
250+
end)
239251
end
240252
::RETURN::
241253
topNode = self:_fastWard(action.finish, topNode)
@@ -275,6 +287,7 @@ function vm.launchRunner(loc, callback)
275287
local self = setmetatable({
276288
_loc = loc,
277289
_objs = {},
290+
_mark = {},
278291
_callback = callback,
279292
}, mt)
280293

test/type_inference/init.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,3 +2559,24 @@ end
25592559
25602560
print(<?n?>)
25612561
]]
2562+
2563+
TEST 'table' [[
2564+
---@type table?
2565+
local n
2566+
2567+
print((n and <?n?>.x))
2568+
]]
2569+
2570+
TEST 'table' [[
2571+
---@type table?
2572+
local n
2573+
2574+
n = n and <?n?>.x or 1
2575+
]]
2576+
2577+
TEST 'table' [[
2578+
---@type table?
2579+
local n
2580+
2581+
n = ff[n and <?n?>.x]
2582+
]]

0 commit comments

Comments
 (0)