Skip to content

Commit 6c82c0f

Browse files
committed
fix #1463
1 parent 680ffb8 commit 6c82c0f

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* `FIX` [#1446](https://github.com/sumneko/lua-language-server/issues/1446)
1111
* `FIX` [#1451](https://github.com/sumneko/lua-language-server/issues/1451)
1212
* `FIX` [#1461](https://github.com/sumneko/lua-language-server/issues/1461)
13+
* `FIX` [#1463](https://github.com/sumneko/lua-language-server/issues/1463)
1314

1415
## 3.5.2
1516
`2022-8-1`

script/vm/type.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ function vm.isSubType(uri, child, parent, mark)
252252
return false
253253
end
254254

255+
---@param node string|vm.node|vm.object
256+
function vm.isUnknown(node)
257+
if type(node) == 'string' then
258+
return node == 'unknown'
259+
end
260+
if node.type == 'vm.node' then
261+
return not node:hasKnownType()
262+
end
263+
return false
264+
end
265+
255266
---@param uri uri
256267
---@param tnode vm.node
257268
---@param knode vm.node|string
@@ -280,6 +291,9 @@ function vm.getTableValue(uri, tnode, knode, inversion)
280291
result:merge(vm.compileNode(tn.node))
281292
end
282293
if tn.type == 'table' then
294+
if vm.isUnknown(knode) then
295+
goto CONTINUE
296+
end
283297
for _, field in ipairs(tn) do
284298
if field.type == 'tableindex'
285299
and field.value then
@@ -315,6 +329,7 @@ function vm.getTableValue(uri, tnode, knode, inversion)
315329
end
316330
end
317331
end
332+
::CONTINUE::
318333
end
319334
if result:isEmpty() then
320335
return nil
@@ -350,6 +365,9 @@ function vm.getTableKey(uri, tnode, vnode, reverse)
350365
result:merge(vm.declareGlobal('type', 'integer'))
351366
end
352367
if tn.type == 'table' then
368+
if vm.isUnknown(tnode) then
369+
goto CONTINUE
370+
end
353371
for _, field in ipairs(tn) do
354372
if field.type == 'tableindex' then
355373
if field.index then
@@ -364,6 +382,7 @@ function vm.getTableKey(uri, tnode, vnode, reverse)
364382
end
365383
end
366384
end
385+
::CONTINUE::
367386
end
368387
if result:isEmpty() then
369388
return nil

test/diagnostics/type-check.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,15 @@ f(1)
775775
f(<!3!>)
776776
]]
777777

778+
TEST [[
779+
local t = {
780+
x = 1,
781+
}
782+
783+
local x
784+
t[x] = true
785+
]]
786+
778787
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
779788
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
780789
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

test/type_inference/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,3 +3773,13 @@ t.x = t.x
37733773
37743774
print(t.<?x?>)
37753775
]]
3776+
3777+
TEST 'unknown' [[
3778+
local t = {
3779+
x = 1,
3780+
}
3781+
3782+
local x
3783+
3784+
local <?v?> = t[x]
3785+
]]

0 commit comments

Comments
 (0)