Skip to content

Commit 31c83e0

Browse files
committed
fix incorrect param-type-mismatch diagnostic for optional fields
1 parent 4411cdb commit 31c83e0

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

changelog.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<!-- Add all new changes here. They will be moved under a version at release -->
55
* `CHG` Update Love2d version
66
* `CHG` Improve type infer of `table.unpack` and `unpack`
7-
* `FIX` missing-fields diagnostic now warns about missing inherited fields
7+
* `FIX` `missing-fields` diagnostic now warns about missing inherited fields
8+
* `FIX` Incorrect `param-type-mismatch` diagnostic for optional fields
89

910
## 3.13.2
1011
`2024-11-21`

script/vm/type.lua

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ local function checkValue(parent, child, mark, errs)
244244
local knode = vm.compileNode(pfield.name)
245245
local cvalues = vm.getTableValue(uri, tnode, knode, true)
246246
if not cvalues then
247+
if pfield.optional then
248+
goto continue
249+
end
247250
if errs then
248251
errs[#errs+1] = 'TYPE_ERROR_TABLE_NO_FIELD'
249252
errs[#errs+1] = pfield.name
@@ -260,6 +263,7 @@ local function checkValue(parent, child, mark, errs)
260263
end
261264
return false
262265
end
266+
::continue::
263267
end
264268
end
265269
return true

test/diagnostics/param-type-mismatch.lua

+10
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,14 @@ local k = t[i].int
384384
f(k)
385385
]]
386386

387+
TEST [[
388+
---@type [1, 2, 3]
389+
local x
390+
391+
---@param y { [1]?: 1, [2]?: 2, [3]?: 3, [4]?: 4}
392+
local function f(y) end
393+
394+
f(x)
395+
]]
396+
387397
config.set(nil, 'Lua.type.checkTableShape', false)

0 commit comments

Comments
 (0)