Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` Recognize a class inheriting a generic class as a child of that generic's own parent [#3441](https://github.com/LuaLS/lua-language-server/pull/3441)
* `NEW` Support type inference for `@field` and `@type` function declarations in method overrides [#3367](https://github.com/LuaLS/lua-language-server/issues/3367)
* `FIX` Deduplicate documentation bindings for parameters
* `FIX` Correct `math.type` meta return annotation to use `nil` instead of the string literal `'nil'`
Expand Down
18 changes: 15 additions & 3 deletions script/vm/type.lua
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,21 @@ function vm.isSubType(uri, child, parent, mark, errs)
for _, set in ipairs(childClass:getSets(uri)) do
if set.type == 'doc.class' and set.extends then
for _, ext in ipairs(set.extends) do
if ext.type == 'doc.extends.name'
and (not isBasicType or guide.isBasicType(ext[1]))
and vm.isSubType(uri, ext[1], parent, mark, errs) == true then
-- A parent given with arguments (`---@class A : B<string>`) is
-- parsed into `doc.type.sign`, keeping the name one level down.
-- Without looking inside, such a parent is skipped entirely and
-- the class is not recognized as its child.
local extName
if ext.type == 'doc.extends.name' then
extName = ext[1]
elseif ext.type == 'doc.type.sign'
and ext.node
and ext.node.type == 'doc.extends.name' then
extName = ext.node[1]
end
if extName
and (not isBasicType or guide.isBasicType(extName))
and vm.isSubType(uri, extName, parent, mark, errs) == true then
mark[childName] = nil
return true
end
Expand Down
19 changes: 19 additions & 0 deletions test/diagnostics/param-type-mismatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,23 @@ local function f(y) end
f(x)
]]

-- A class inheriting a generic one is still a child of that generic's own parent: the
-- parent given with arguments is parsed one level down and used to be skipped.
TEST [[
---@class Base

---@class Middle<T> : Base
---@field Value T

---@class Child : Middle<boolean>

---@param value Base
local function f(value) end

---@type Child
local child

f(child)
]]

config.set(nil, 'Lua.type.checkTableShape', false)
Loading