diff options
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/diagnostics/assign-type-mismatch.lua | 47 | ||||
-rw-r--r-- | script/core/diagnostics/init.lua | 13 | ||||
-rw-r--r-- | script/core/diagnostics/type-check-assign.lua | 20 |
3 files changed, 54 insertions, 26 deletions
diff --git a/script/core/diagnostics/assign-type-mismatch.lua b/script/core/diagnostics/assign-type-mismatch.lua new file mode 100644 index 00000000..38cfc674 --- /dev/null +++ b/script/core/diagnostics/assign-type-mismatch.lua @@ -0,0 +1,47 @@ +local files = require 'files' +local lang = require 'language' +local guide = require 'parser.guide' +local vm = require 'vm' +local await = require 'await' + +local checkTypes = { + 'setlocal', + 'setglobal', + 'setfield', + 'setindex', + 'setmethod', + 'tablefield', + 'tableindex' +} + +---@async +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + ---@async + guide.eachSourceTypes(state.ast, checkTypes, function (source) + local value = source.value + if not value then + return + end + await.delay() + local varNode = vm.compileNode(source) + local valueNode = vm.compileNode(value) + if vm.getInfer(varNode):hasUnknown(uri) 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 + end) +end diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index e9af8ea3..746052b0 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -7,12 +7,13 @@ local util = require 'utility' -- 把耗时最长的诊断放到最后面 local diagSort = { - ['redundant-value'] = 100, - ['not-yieldable'] = 101, - ['deprecated'] = 102, - ['undefined-field'] = 103, - ['redundant-parameter'] = 104, - ['cast-local-type'] = 105, + ['redundant-value'] = 100, + ['not-yieldable'] = 101, + ['deprecated'] = 102, + ['undefined-field'] = 103, + ['redundant-parameter'] = 104, + ['cast-local-type'] = 105, + ['assign-type-mismatch'] = 106, } local diagList = {} diff --git a/script/core/diagnostics/type-check-assign.lua b/script/core/diagnostics/type-check-assign.lua deleted file mode 100644 index 6b3c73b3..00000000 --- a/script/core/diagnostics/type-check-assign.lua +++ /dev/null @@ -1,20 +0,0 @@ -local files = require 'files' -local lang = require 'language' -local guide = require 'parser.guide' -local vm = require 'vm' - -return function (uri, callback) - local state = files.getState(uri) - if not state then - return - end - - guide.eachSourceType(state.ast, 'setlocal', function (source) - local value = source.value - if not value then - return - end - local locNode = vm.compileNode(source) - local valueNode = vm.compileNode(value) - end) -end |