Skip to content

Commit 8691625

Browse files
committed
special searching for __index
#2106
1 parent 3bf4d7d commit 8691625

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

script/vm/compiler.lua

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ local function getReturnOfSetMetaTable(args)
511511
node:merge(vm.compileNode(tbl))
512512
end
513513
if mt then
514-
vm.compileByParentNode(mt, '__index', function (src)
514+
vm.compileByParentNodeAll(mt, '__index', function (src)
515515
for n in vm.compileNode(src):eachObject() do
516516
if n.type == 'global'
517517
or n.type == 'local'
@@ -637,8 +637,9 @@ end
637637

638638
---@param source parser.object | vm.variable
639639
---@param key string|vm.global|vm.ANY
640-
---@param pushResult fun(source: parser.object)
641-
function vm.compileByParentNode(source, key, pushResult)
640+
---@return parser.object[] docedResults
641+
---@return parser.object[] commonResults
642+
function vm.getNodesOfParentNode(source, key)
642643
local parentNode = vm.compileNode(source)
643644
local docedResults = {}
644645
local commonResults = {}
@@ -691,6 +692,16 @@ function vm.compileByParentNode(source, key, pushResult)
691692
end)
692693
end
693694

695+
return docedResults, commonResults
696+
end
697+
698+
-- 遍历所有字段(按照优先级)
699+
---@param source parser.object | vm.variable
700+
---@param key string|vm.global|vm.ANY
701+
---@param pushResult fun(source: parser.object)
702+
function vm.compileByParentNode(source, key, pushResult)
703+
local docedResults, commonResults = vm.getNodesOfParentNode(source, key)
704+
694705
if #docedResults > 0 then
695706
for _, res in ipairs(docedResults) do
696707
pushResult(res)
@@ -703,6 +714,21 @@ function vm.compileByParentNode(source, key, pushResult)
703714
end
704715
end
705716

717+
-- 遍历所有字段(无视优先级)
718+
---@param source parser.object | vm.variable
719+
---@param key string|vm.global|vm.ANY
720+
---@param pushResult fun(source: parser.object)
721+
function vm.compileByParentNodeAll(source, key, pushResult)
722+
local docedResults, commonResults = vm.getNodesOfParentNode(source, key)
723+
724+
for _, res in ipairs(docedResults) do
725+
pushResult(res)
726+
end
727+
for _, res in ipairs(commonResults) do
728+
pushResult(res)
729+
end
730+
end
731+
706732
---@param list parser.object[]
707733
---@param index integer
708734
---@return vm.node
@@ -1341,8 +1367,10 @@ local compilerSwitch = util.switch()
13411367
end)
13421368
end
13431369

1344-
if not hasMarkDoc and source.value then
1345-
vm.setNode(source, vm.compileNode(source.value))
1370+
if source.value then
1371+
if not hasMarkDoc or source.value.type == 'table' then
1372+
vm.setNode(source, vm.compileNode(source.value))
1373+
end
13461374
end
13471375

13481376
end)

test/type_inference/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,3 +4278,17 @@ local x ---@type Some
42784278
42794279
print(<?x?>)
42804280
]]
4281+
4282+
TEST 'integer' [[
4283+
---@class metatable : table
4284+
---@field __index table
4285+
4286+
---@param table table
4287+
---@param metatable? metatable
4288+
---@return table
4289+
function setmetatable(table, metatable) end
4290+
4291+
local m = setmetatable({},{ __index = { a = 1 } })
4292+
4293+
m.<?a?>
4294+
]]

0 commit comments

Comments
 (0)