summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/vm/node.lua13
-rw-r--r--script/vm/runner.lua5
-rw-r--r--test/type_inference/init.lua9
3 files changed, 27 insertions, 0 deletions
diff --git a/script/vm/node.lua b/script/vm/node.lua
index 76e61192..5086f66d 100644
--- a/script/vm/node.lua
+++ b/script/vm/node.lua
@@ -2,6 +2,7 @@ local files = require 'files'
---@class vm
local vm = require 'vm.vm'
local ws = require 'workspace.workspace'
+local guide = require 'parser.guide'
---@type table<vm.object, vm.node>
vm.nodeCache = {}
@@ -105,6 +106,18 @@ function mt:hasFalsy()
return false
end
+function mt:hasKnownType()
+ for _, c in ipairs(self) do
+ if c.type == 'global' and c.cate == 'type' then
+ return true
+ end
+ if guide.isLiteral(c) then
+ return true
+ end
+ end
+ return false
+end
+
---@return boolean
function mt:isNullable()
if self.optional then
diff --git a/script/vm/runner.lua b/script/vm/runner.lua
index 0ec26bef..a9c38a87 100644
--- a/script/vm/runner.lua
+++ b/script/vm/runner.lua
@@ -132,6 +132,7 @@ function mt:_lookInto(action, topNode, outNode)
self:_launchBlock(action, blockNode:copy())
topNode = mainNode
elseif action.type == 'if' then
+ local hasElse
local mainNode = topNode:copy()
local blockNodes = {}
for _, subBlock in ipairs(action) do
@@ -140,6 +141,7 @@ function mt:_lookInto(action, topNode, outNode)
blockNode, mainNode = self:_lookInto(subBlock.filter, blockNode, mainNode)
self:_fastWard(subBlock.filter.finish, blockNode)
else
+ hasElse = true
mainNode:clear()
end
blockNode = self:_launchBlock(subBlock, blockNode:copy())
@@ -150,6 +152,9 @@ function mt:_lookInto(action, topNode, outNode)
blockNodes[#blockNodes+1] = blockNode
end
end
+ if not hasElse and not topNode:hasKnownType() then
+ mainNode:merge(vm.declareGlobal('type', 'unknown'))
+ end
for _, blockNode in ipairs(blockNodes) do
mainNode:merge(blockNode)
end
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index a194ce2d..efcc5fea 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -2482,3 +2482,12 @@ t = t or {}
print(<?t?>)
]]
+
+TEST 'unknown|nil' [[
+local x
+
+if x == nil then
+end
+
+print(<?x?>)
+]]