Skip to content

Commit 45d7b2c

Browse files
RomanSpectorclaude
andcommitted
Recognize a class inheriting a generic one as a child of its parent
A parent given with arguments (`---@Class A : B<string>`) is parsed into `doc.type.sign`, which keeps the name one level down. The inheritance walk only looked at plain names, so such a parent was skipped entirely: `A` was not recognized as a child of whatever `B` itself inherits, and passing it where the base type is expected reported a mismatch. The name is now taken from the sign node as well. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent a9aa09a commit 45d7b2c

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

script/vm/type.lua

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,21 @@ function vm.isSubType(uri, child, parent, mark, errs)
589589
for _, set in ipairs(childClass:getSets(uri)) do
590590
if set.type == 'doc.class' and set.extends then
591591
for _, ext in ipairs(set.extends) do
592-
if ext.type == 'doc.extends.name'
593-
and (not isBasicType or guide.isBasicType(ext[1]))
594-
and vm.isSubType(uri, ext[1], parent, mark, errs) == true then
592+
-- A parent given with arguments (`---@class A : B<string>`) is
593+
-- parsed into `doc.type.sign`, keeping the name one level down.
594+
-- Without looking inside, such a parent is skipped entirely and
595+
-- the class is not recognized as its child.
596+
local extName
597+
if ext.type == 'doc.extends.name' then
598+
extName = ext[1]
599+
elseif ext.type == 'doc.type.sign'
600+
and ext.node
601+
and ext.node.type == 'doc.extends.name' then
602+
extName = ext.node[1]
603+
end
604+
if extName
605+
and (not isBasicType or guide.isBasicType(extName))
606+
and vm.isSubType(uri, extName, parent, mark, errs) == true then
595607
mark[childName] = nil
596608
return true
597609
end

test/diagnostics/param-type-mismatch.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,23 @@ local function f(y) end
394394
f(x)
395395
]]
396396

397+
-- A class inheriting a generic one is still a child of that generic's own parent: the
398+
-- parent given with arguments is parsed one level down and used to be skipped.
399+
TEST [[
400+
---@class Base
401+
402+
---@class Middle<T> : Base
403+
---@field Value T
404+
405+
---@class Child : Middle<boolean>
406+
407+
---@param value Base
408+
local function f(value) end
409+
410+
---@type Child
411+
local child
412+
413+
f(child)
414+
]]
415+
397416
config.set(nil, 'Lua.type.checkTableShape', false)

0 commit comments

Comments
 (0)