Skip to content

Commit

Permalink
limit error message for type dismatch
Browse files Browse the repository at this point in the history
resolve #1838
  • Loading branch information
sumneko committed Jan 30, 2023
1 parent 4a9ab5b commit 76b8cf3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
## 3.6.8
* `NEW` command `lua.exportDocument` . VSCode will display this command in the right-click menu
* `FIX` [#1831]
* `FIX` [#1838]

[#1831]: https://github.com/sumneko/lua-language-server/issues/1831
[#1838]: https://github.com/sumneko/lua-language-server/issues/1838

## 3.6.7
`2023-1-20`
Expand Down
9 changes: 9 additions & 0 deletions script/vm/infer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local vm = require 'vm.vm'
---@field _lastView? string
---@field _lastViewUri? uri
---@field _lastViewDefault? any
---@field _subViews? string[]
local mt = {}
mt.__index = mt
mt._hasTable = false
Expand Down Expand Up @@ -413,6 +414,7 @@ function mt:view(uri, default)
end

local array = {}
self._subViews = array
for view in pairs(self.views) do
if not self._drop[view] then
array[#array+1] = view
Expand Down Expand Up @@ -471,6 +473,13 @@ function mt:eachView(uri)
return next, self.views
end

---@param uri uri
---@return string[]
function mt:getSubViews(uri)
self:view(uri)
return self._subViews
end

---@return string?
function mt:viewLiterals()
if not self.node then
Expand Down
3 changes: 3 additions & 0 deletions script/vm/ref.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ simpleSwitch = util.switch()

---@async
local function searchInAllFiles(suri, searcher, notify)
await.delay()

searcher(suri)
await.delay()

local uris = {}
for uri in files.eachFile(suri) do
Expand Down
22 changes: 18 additions & 4 deletions script/vm/type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ function vm.isSubType(uri, child, parent, mark, errs)
end
end
if hasKnownType > 0 then
if errs and hasKnownType > 1 then
if errs
and hasKnownType > 1
and #vm.getInfer(child):getSubViews(uri) > 1 then
errs[#errs+1] = 'TYPE_ERROR_CHILD_ALL_DISMATCH'
errs[#errs+1] = child
errs[#errs+1] = parent
Expand Down Expand Up @@ -376,7 +378,9 @@ function vm.isSubType(uri, child, parent, mark, errs)
end
end
if hasKnownType > 0 then
if errs and hasKnownType > 1 then
if errs
and hasKnownType > 1
and #vm.getInfer(parent):getSubViews(uri) > 1 then
errs[#errs+1] = 'TYPE_ERROR_PARENT_ALL_DISMATCH'
errs[#errs+1] = child
errs[#errs+1] = parent
Expand Down Expand Up @@ -703,6 +707,7 @@ local ErrorMessageMap = {
---@return string
function vm.viewTypeErrorMessage(uri, errs)
local lines = {}
local mark = {}
local index = 1
while true do
local name = errs[index]
Expand Down Expand Up @@ -741,8 +746,17 @@ function vm.viewTypeErrorMessage(uri, errs)
index = index + 1
end
local line = lang.script(name, lparams)
lines[#lines+1] = '- ' .. line
if not mark[line] then
mark[line] = true
lines[#lines+1] = '- ' .. line
end
end
util.revertTable(lines)
return table.concat(lines, '\n')
if #lines > 15 then
lines[13] = ('...(+%d)'):format(#lines - 15)
table.move(lines, #lines - 2, #lines, 14)
return table.concat(lines, '\n', 1, 16)
else
return table.concat(lines, '\n')
end
end
4 changes: 4 additions & 0 deletions test/diagnostics/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ function TEST(script, ...)
end

files.remove(TESTURI)

return function (callback)
callback(origins)
end
end

require 'diagnostics.common'
Expand Down
52 changes: 52 additions & 0 deletions test/diagnostics/type-check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,58 @@ local t = {
}
]]

TEST [[
local x
if X then
x = 'A'
elseif X then
x = 'B'
else
x = 'C'
end
local y = x
<!y!> = nil
]]
(function (diags)
local diag = diags[1]
assert(diag.message == [[
已显式定义变量的类型为 `string` ,不能再将其类型转换为 `nil`。
- `nil` 无法匹配 `string`
- 类型 `nil` 无法匹配 `string`]])
end)


TEST [[
---@type 'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z'
local x
<!x!> = nil
]]
(function (diags)
local diag = diags[1]
assert(diag.message == [[
已显式定义变量的类型为 `'A'|'B'|'C'|'D'|'E'...(+21)` ,不能再将其类型转换为 `nil`。
- `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)`
- `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)` 中的任何子类
- 类型 `nil` 无法匹配 `'Z'`
- 类型 `nil` 无法匹配 `'Y'`
- 类型 `nil` 无法匹配 `'X'`
- 类型 `nil` 无法匹配 `'W'`
- 类型 `nil` 无法匹配 `'V'`
- 类型 `nil` 无法匹配 `'U'`
- 类型 `nil` 无法匹配 `'T'`
- 类型 `nil` 无法匹配 `'S'`
- 类型 `nil` 无法匹配 `'R'`
- 类型 `nil` 无法匹配 `'Q'`
...(+13)
- 类型 `nil` 无法匹配 `'C'`
- 类型 `nil` 无法匹配 `'B'`
- 类型 `nil` 无法匹配 `'A'`]])
end)

config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')
Expand Down

0 comments on commit 76b8cf3

Please sign in to comment.