diff --git a/changelog.md b/changelog.md index c51fdd878..0a32cb623 100644 --- a/changelog.md +++ b/changelog.md @@ -4,7 +4,8 @@ * `CHG` Update Love2d version * `CHG` Improve type infer of `table.unpack` and `unpack` -* `FIX` missing-fields diagnostic now warns about missing inherited fields +* `FIX` `missing-fields` diagnostic now warns about missing inherited fields +* `FIX` Incorrect `param-type-mismatch` diagnostic for optional fields ## 3.13.2 `2024-11-21` diff --git a/script/vm/type.lua b/script/vm/type.lua index b6e50e363..a8ac3bcee 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -244,6 +244,9 @@ local function checkValue(parent, child, mark, errs) local knode = vm.compileNode(pfield.name) local cvalues = vm.getTableValue(uri, tnode, knode, true) if not cvalues then + if pfield.optional then + goto continue + end if errs then errs[#errs+1] = 'TYPE_ERROR_TABLE_NO_FIELD' errs[#errs+1] = pfield.name @@ -260,6 +263,7 @@ local function checkValue(parent, child, mark, errs) end return false end + ::continue:: end end return true diff --git a/test/diagnostics/param-type-mismatch.lua b/test/diagnostics/param-type-mismatch.lua index c67d42b0b..58acba2b2 100644 --- a/test/diagnostics/param-type-mismatch.lua +++ b/test/diagnostics/param-type-mismatch.lua @@ -384,4 +384,14 @@ local k = t[i].int f(k) ]] +TEST [[ +---@type [1, 2, 3] +local x + +---@param y { [1]?: 1, [2]?: 2, [3]?: 3, [4]?: 4} +local function f(y) end + +f(x) +]] + config.set(nil, 'Lua.type.checkTableShape', false)