diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-06-18 01:58:48 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-06-18 01:58:48 +0800 |
commit | 53d376ce281906fd856fac42073a072906b2628e (patch) | |
tree | d72c05a6100b9f8da4650106653a85ec2a55c044 /script | |
parent | bc4e205a4953a7f3a99bafc9fc88295f929beab1 (diff) | |
download | lua-language-server-53d376ce281906fd856fac42073a072906b2628e.zip |
update
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/assign-type-mismatch.lua | 20 | ||||
-rw-r--r-- | script/core/diagnostics/cast-local-type.lua | 41 | ||||
-rw-r--r-- | script/provider/provider.lua | 2 | ||||
-rw-r--r-- | script/vm/type.lua | 34 |
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 |