diff options
-rw-r--r-- | changelog.md | 9 | ||||
-rw-r--r-- | script/vm/runner.lua | 21 | ||||
-rw-r--r-- | test/type_inference/init.lua | 21 |
3 files changed, 46 insertions, 5 deletions
diff --git a/changelog.md b/changelog.md index 3b2e2dc0..01cb797f 100644 --- a/changelog.md +++ b/changelog.md @@ -10,7 +10,7 @@ ``` * `CHG` infer type by `error` ```lua - ---@type integer? + ---@type integer|nil local n if not n then @@ -19,6 +19,13 @@ print(n) -- `n` is `integer` here ``` +* `CHG` infer type by `t and t.x` + ```lua + ---@type table|nil + local t + + local s = t and t.x or 1 -- `t` in `t.x` is `table` + ``` * `FIX` with clients that support LSP 3.17 (VSCode), workspace diagnostics are triggered every time when opening a file. * `FIX` [#1204](https://github.com/sumneko/lua-language-server/issues/1204) * `FIX` [#1208](https://github.com/sumneko/lua-language-server/issues/1208) diff --git a/script/vm/runner.lua b/script/vm/runner.lua index 5d561677..05dcbb7a 100644 --- a/script/vm/runner.lua +++ b/script/vm/runner.lua @@ -8,6 +8,7 @@ local guide = require 'parser.guide' ---@field _loc parser.object ---@field _objs parser.object[] ---@field _callback vm.runner.callback +---@field _mark table local mt = {} mt.__index = mt mt._index = 1 @@ -117,6 +118,17 @@ function mt:_lookInto(action, topNode, outNode) if not action then return topNode, outNode end + if self._mark[action] then + return + end + self._mark[action] = true + local top = self._objs[self._index] + if not top then + return topNode, outNode + end + if not guide.isInRange(action, top.finish) then + return topNode, outNode + end local set local value = vm.getObjectValue(action) if value then @@ -232,10 +244,10 @@ function mt:_lookInto(action, topNode, outNode) self:_lookInto(arg, topNode) end end - elseif action.type == 'return' then - for _, rtn in ipairs(action) do - self:_lookInto(rtn, topNode) - end + else + guide.eachSourceContain(action, top.finish, function(source) + self:_lookInto(source, topNode) + end) end ::RETURN:: topNode = self:_fastWard(action.finish, topNode) @@ -275,6 +287,7 @@ function vm.launchRunner(loc, callback) local self = setmetatable({ _loc = loc, _objs = {}, + _mark = {}, _callback = callback, }, mt) diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 5ed572dd..a12e702f 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -2559,3 +2559,24 @@ end print(<?n?>) ]] + +TEST 'table' [[ +---@type table? +local n + +print((n and <?n?>.x)) +]] + +TEST 'table' [[ +---@type table? +local n + +n = n and <?n?>.x or 1 +]] + +TEST 'table' [[ +---@type table? +local n + +n = ff[n and <?n?>.x] +]] |