summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/vm/compiler.lua38
-rw-r--r--test/type_inference/init.lua14
2 files changed, 47 insertions, 5 deletions
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index d4309cc9..dc22f6b9 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -511,7 +511,7 @@ local function getReturnOfSetMetaTable(args)
node:merge(vm.compileNode(tbl))
end
if mt then
- vm.compileByParentNode(mt, '__index', function (src)
+ vm.compileByParentNodeAll(mt, '__index', function (src)
for n in vm.compileNode(src):eachObject() do
if n.type == 'global'
or n.type == 'local'
@@ -637,8 +637,9 @@ end
---@param source parser.object | vm.variable
---@param key string|vm.global|vm.ANY
----@param pushResult fun(source: parser.object)
-function vm.compileByParentNode(source, key, pushResult)
+---@return parser.object[] docedResults
+---@return parser.object[] commonResults
+function vm.getNodesOfParentNode(source, key)
local parentNode = vm.compileNode(source)
local docedResults = {}
local commonResults = {}
@@ -691,6 +692,16 @@ function vm.compileByParentNode(source, key, pushResult)
end)
end
+ return docedResults, commonResults
+end
+
+-- 遍历所有字段(按照优先级)
+---@param source parser.object | vm.variable
+---@param key string|vm.global|vm.ANY
+---@param pushResult fun(source: parser.object)
+function vm.compileByParentNode(source, key, pushResult)
+ local docedResults, commonResults = vm.getNodesOfParentNode(source, key)
+
if #docedResults > 0 then
for _, res in ipairs(docedResults) do
pushResult(res)
@@ -703,6 +714,21 @@ function vm.compileByParentNode(source, key, pushResult)
end
end
+-- 遍历所有字段(无视优先级)
+---@param source parser.object | vm.variable
+---@param key string|vm.global|vm.ANY
+---@param pushResult fun(source: parser.object)
+function vm.compileByParentNodeAll(source, key, pushResult)
+ local docedResults, commonResults = vm.getNodesOfParentNode(source, key)
+
+ for _, res in ipairs(docedResults) do
+ pushResult(res)
+ end
+ for _, res in ipairs(commonResults) do
+ pushResult(res)
+ end
+end
+
---@param list parser.object[]
---@param index integer
---@return vm.node
@@ -1341,8 +1367,10 @@ local compilerSwitch = util.switch()
end)
end
- if not hasMarkDoc and source.value then
- vm.setNode(source, vm.compileNode(source.value))
+ if source.value then
+ if not hasMarkDoc or source.value.type == 'table' then
+ vm.setNode(source, vm.compileNode(source.value))
+ end
end
end)
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 12a8e2ab..6a8ba588 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -4278,3 +4278,17 @@ local x ---@type Some
print(<?x?>)
]]
+
+TEST 'integer' [[
+---@class metatable : table
+---@field __index table
+
+---@param table table
+---@param metatable? metatable
+---@return table
+function setmetatable(table, metatable) end
+
+local m = setmetatable({},{ __index = { a = 1 } })
+
+m.<?a?>
+]]