summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/core/diagnostics/assign-type-mismatch.lua20
-rw-r--r--script/core/diagnostics/cast-local-type.lua41
-rw-r--r--script/provider/provider.lua2
-rw-r--r--script/vm/type.lua34
4 files changed, 54 insertions, 43 deletions
diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua
index d301faed..168106a6 100644
--- a/script/core/diagnostics/assign-type-mismatch.lua
+++ b/script/core/diagnostics/assign-type-mismatch.lua
@@ -36,18 +36,16 @@ return function (uri, callback)
end
local varNode = vm.compileNode(source)
local valueNode = vm.compileNode(value)
- if vm.getInfer(varNode):hasUnknown(uri) then
+ if vm.canCastType(uri, varNode, valueNode) then
return
end
- if not vm.isSubType(uri, valueNode, varNode) then
- callback {
- start = source.start,
- finish = source.finish,
- message = lang.script('DIAG_ASSIGN_TYPE_MISMATCH', {
- loc = vm.getInfer(varNode):view(uri),
- ref = vm.getInfer(valueNode):view(uri),
- }),
- }
- end
+ callback {
+ start = source.start,
+ finish = source.finish,
+ message = lang.script('DIAG_ASSIGN_TYPE_MISMATCH', {
+ loc = vm.getInfer(varNode):view(uri),
+ ref = vm.getInfer(valueNode):view(uri),
+ }),
+ }
end)
end
diff --git a/script/core/diagnostics/cast-local-type.lua b/script/core/diagnostics/cast-local-type.lua
index 65cc0368..ea14ffb6 100644
--- a/script/core/diagnostics/cast-local-type.lua
+++ b/script/core/diagnostics/cast-local-type.lua
@@ -21,43 +21,22 @@ return function (uri, callback)
if not locNode:getData 'hasDefined' then
return
end
- if vm.getInfer(loc):hasUnknown(uri) then
- return
- end
-
- -- 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 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
- goto CONTINUE
+
+ if not vm.canCastType(uri, locNode, refNode) then
+ callback {
+ start = ref.start,
+ finish = ref.finish,
+ message = lang.script('DIAG_CAST_LOCAL_TYPE', {
+ loc = vm.getInfer(locNode):view(uri),
+ ref = vm.getInfer(refNode):view(uri),
+ }),
+ }
end
- callback {
- start = ref.start,
- finish = ref.finish,
- message = lang.script('DIAG_CAST_LOCAL_TYPE', {
- loc = vm.getInfer(locNode):view(uri),
- ref = vm.getInfer(refNode):view(uri),
- }),
- }
end
- ::CONTINUE::
end
end)
end
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index 6a1bde60..e2531abf 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -1018,7 +1018,7 @@ m.register '$/status/click' {
for _, scp in ipairs(workspace.folders) do
diagnostic.diagnosticsScope(scp.uri, true)
end
- elseif result == 'Restart server' then
+ elseif result == 'Restart Server' then
os.exit(0, true)
end
end
diff --git a/script/vm/type.lua b/script/vm/type.lua
index db378878..ebea8c30 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -202,3 +202,37 @@ function vm.getTableKey(uri, tnode, vnode)
end
return result
end
+
+---@param uri uri
+---@param defNode vm.node
+---@param refNode vm.node
+---@return boolean
+function vm.canCastType(uri, defNode, refNode)
+ local defInfer = vm.getInfer(defNode)
+
+ if defInfer:hasUnknown(uri) then
+ return true
+ end
+
+ -- allow `local x = {};x = nil`,
+ -- but not allow `local x ---@type table;x = nil`
+ local allowNil = defInfer:hasType(uri, 'table')
+ and not defNode:hasType 'table'
+
+ -- allow `local x = 0;x = 1.0`,
+ -- but not allow `local x ---@type integer;x = 1.0`
+ local allowNumber = defInfer:hasType(uri, 'integer')
+ and not defNode:hasType 'integer'
+
+ if allowNil and vm.isSubType(uri, refNode, 'nil') then
+ return true
+ end
+ if allowNumber and vm.isSubType(uri, refNode, 'number') then
+ return true
+ end
+ if vm.isSubType(uri, refNode, defNode) then
+ return true
+ end
+
+ return false
+end