summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
Diffstat (limited to 'script')
-rw-r--r--script/core/diagnostics/cast-local-type.lua18
-rw-r--r--script/vm/node.lua1
2 files changed, 16 insertions, 3 deletions
diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua
index 4b7f7e84..65cc0368 100644
--- a/script/core/diagnostics/cast-local-type.lua
+++ b/script/core/diagnostics/cast-local-type.lua
@@ -24,13 +24,25 @@ return function (uri, callback)
if vm.getInfer(loc):hasUnknown(uri) then
return
end
- local canSetNil = vm.getInfer(loc):hasClass(uri)
- or vm.getInfer(loc):hasType(uri, 'table')
+
+ -- allow `local x = {};x = nil`,
+ -- but not allow `local x ---@type table;x = nil`
+ local allowNil = vm.getInfer(loc):hasType(uri, 'table')
+ and not locNode:hasType 'table'
+
+ -- allow `local x = 0;x = 1.0`,
+ -- but not allow `local x ---@type integer;x = 1.0`
+ local allowNumber = vm.getInfer(loc):hasType(uri, 'integer')
+ and not locNode:hasType 'integer'
+
for _, ref in ipairs(loc.ref) do
if ref.type == 'setlocal' then
await.delay()
local refNode = vm.compileNode(ref)
- if canSetNil and vm.getInfer(ref):view(uri) == 'nil' then
+ if allowNil and vm.isSubType(uri, refNode, 'nil') then
+ goto CONTINUE
+ end
+ if allowNumber and vm.isSubType(uri, refNode, 'number') then
goto CONTINUE
end
if vm.isSubType(uri, refNode, locNode) then
diff --git a/script/vm/node.lua b/script/vm/node.lua
index 1ff229fa..26f8e761 100644
--- a/script/vm/node.lua
+++ b/script/vm/node.lua
@@ -106,6 +106,7 @@ function mt:hasFalsy()
return false
end
+---@return boolean
function mt:hasKnownType()
for _, c in ipairs(self) do
if c.type == 'global' and c.cate == 'type' then