Skip to content

Commit 76b8cf3

Browse files
committed
limit error message for type dismatch
resolve #1838
1 parent 4a9ab5b commit 76b8cf3

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
## 3.6.8
44
* `NEW` command `lua.exportDocument` . VSCode will display this command in the right-click menu
55
* `FIX` [#1831]
6+
* `FIX` [#1838]
7+
68
[#1831]: https://github.com/sumneko/lua-language-server/issues/1831
9+
[#1838]: https://github.com/sumneko/lua-language-server/issues/1838
710

811
## 3.6.7
912
`2023-1-20`

script/vm/infer.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local vm = require 'vm.vm'
1111
---@field _lastView? string
1212
---@field _lastViewUri? uri
1313
---@field _lastViewDefault? any
14+
---@field _subViews? string[]
1415
local mt = {}
1516
mt.__index = mt
1617
mt._hasTable = false
@@ -413,6 +414,7 @@ function mt:view(uri, default)
413414
end
414415

415416
local array = {}
417+
self._subViews = array
416418
for view in pairs(self.views) do
417419
if not self._drop[view] then
418420
array[#array+1] = view
@@ -471,6 +473,13 @@ function mt:eachView(uri)
471473
return next, self.views
472474
end
473475

476+
---@param uri uri
477+
---@return string[]
478+
function mt:getSubViews(uri)
479+
self:view(uri)
480+
return self._subViews
481+
end
482+
474483
---@return string?
475484
function mt:viewLiterals()
476485
if not self.node then

script/vm/ref.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ simpleSwitch = util.switch()
2929

3030
---@async
3131
local function searchInAllFiles(suri, searcher, notify)
32+
await.delay()
33+
3234
searcher(suri)
35+
await.delay()
3336

3437
local uris = {}
3538
for uri in files.eachFile(suri) do

script/vm/type.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ function vm.isSubType(uri, child, parent, mark, errs)
301301
end
302302
end
303303
if hasKnownType > 0 then
304-
if errs and hasKnownType > 1 then
304+
if errs
305+
and hasKnownType > 1
306+
and #vm.getInfer(child):getSubViews(uri) > 1 then
305307
errs[#errs+1] = 'TYPE_ERROR_CHILD_ALL_DISMATCH'
306308
errs[#errs+1] = child
307309
errs[#errs+1] = parent
@@ -376,7 +378,9 @@ function vm.isSubType(uri, child, parent, mark, errs)
376378
end
377379
end
378380
if hasKnownType > 0 then
379-
if errs and hasKnownType > 1 then
381+
if errs
382+
and hasKnownType > 1
383+
and #vm.getInfer(parent):getSubViews(uri) > 1 then
380384
errs[#errs+1] = 'TYPE_ERROR_PARENT_ALL_DISMATCH'
381385
errs[#errs+1] = child
382386
errs[#errs+1] = parent
@@ -703,6 +707,7 @@ local ErrorMessageMap = {
703707
---@return string
704708
function vm.viewTypeErrorMessage(uri, errs)
705709
local lines = {}
710+
local mark = {}
706711
local index = 1
707712
while true do
708713
local name = errs[index]
@@ -741,8 +746,17 @@ function vm.viewTypeErrorMessage(uri, errs)
741746
index = index + 1
742747
end
743748
local line = lang.script(name, lparams)
744-
lines[#lines+1] = '- ' .. line
749+
if not mark[line] then
750+
mark[line] = true
751+
lines[#lines+1] = '- ' .. line
752+
end
745753
end
746754
util.revertTable(lines)
747-
return table.concat(lines, '\n')
755+
if #lines > 15 then
756+
lines[13] = ('...(+%d)'):format(#lines - 15)
757+
table.move(lines, #lines - 2, #lines, 14)
758+
return table.concat(lines, '\n', 1, 16)
759+
else
760+
return table.concat(lines, '\n')
761+
end
748762
end

test/diagnostics/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function TEST(script, ...)
4747
end
4848

4949
files.remove(TESTURI)
50+
51+
return function (callback)
52+
callback(origins)
53+
end
5054
end
5155

5256
require 'diagnostics.common'

test/diagnostics/type-check.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,58 @@ local t = {
11281128
}
11291129
]]
11301130

1131+
TEST [[
1132+
local x
1133+
1134+
if X then
1135+
x = 'A'
1136+
elseif X then
1137+
x = 'B'
1138+
else
1139+
x = 'C'
1140+
end
1141+
1142+
local y = x
1143+
1144+
<!y!> = nil
1145+
]]
1146+
(function (diags)
1147+
local diag = diags[1]
1148+
assert(diag.message == [[
1149+
已显式定义变量的类型为 `string` ,不能再将其类型转换为 `nil`。
1150+
- `nil` 无法匹配 `string`
1151+
- 类型 `nil` 无法匹配 `string`]])
1152+
end)
1153+
1154+
1155+
TEST [[
1156+
---@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'
1157+
local x
1158+
1159+
<!x!> = nil
1160+
]]
1161+
(function (diags)
1162+
local diag = diags[1]
1163+
assert(diag.message == [[
1164+
已显式定义变量的类型为 `'A'|'B'|'C'|'D'|'E'...(+21)` ,不能再将其类型转换为 `nil`。
1165+
- `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)`
1166+
- `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)` 中的任何子类
1167+
- 类型 `nil` 无法匹配 `'Z'`
1168+
- 类型 `nil` 无法匹配 `'Y'`
1169+
- 类型 `nil` 无法匹配 `'X'`
1170+
- 类型 `nil` 无法匹配 `'W'`
1171+
- 类型 `nil` 无法匹配 `'V'`
1172+
- 类型 `nil` 无法匹配 `'U'`
1173+
- 类型 `nil` 无法匹配 `'T'`
1174+
- 类型 `nil` 无法匹配 `'S'`
1175+
- 类型 `nil` 无法匹配 `'R'`
1176+
- 类型 `nil` 无法匹配 `'Q'`
1177+
...(+13)
1178+
- 类型 `nil` 无法匹配 `'C'`
1179+
- 类型 `nil` 无法匹配 `'B'`
1180+
- 类型 `nil` 无法匹配 `'A'`]])
1181+
end)
1182+
11311183
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
11321184
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
11331185
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)