Skip to content

Commit 25bd69b

Browse files
committed
Account for ref and ptr objects
1 parent 23c5cc1 commit 25bd69b

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

compiler/sigmatch.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,8 +1704,12 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
17041704

17051705
# Fallback for computed generic type aliases (bug #25340):
17061706
# Check if the aliased types (last elements) have inheritance relationship
1707-
let aLast = roota.last
1708-
let fLast = rootf.last
1707+
var aLast = roota.last
1708+
var fLast = rootf.last
1709+
# Handle ref/ptr types - unwrap to get the underlying object
1710+
if aLast.kind == fLast.kind and aLast.kind in {tyRef, tyPtr}:
1711+
aLast = aLast.elementType
1712+
fLast = fLast.elementType
17091713
if aLast.kind == tyObject and fLast.kind == tyObject and trIsOutParam notin flags:
17101714
let depth = isObjectSubtype(c, aLast, fLast, nil)
17111715
if depth > 0:

tests/typerel/t25340.nim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,38 @@ block:
7070
assert JSExternObj[HTMLElement] is JSExternObj[JSObj]
7171
assert JSExternObj[Document] is JSExternObj[JSObj]
7272

73+
block: # Refs
74+
type
75+
JSObj {.inheritable.} = object
76+
JSExternObjBase {.inheritable, pure.} = ref object
77+
HTMLElement = object of JSObj
78+
Document = object of HTMLElement
79+
80+
macro parent(t: typedesc): untyped =
81+
let n = t.getType()[1]
82+
if $n == "JSObj":
83+
return ident"JSExternObjBase"
84+
else:
85+
let imp = n.getTypeImpl()
86+
imp.expectKind(nnkObjectTy)
87+
let base = imp[1][0]
88+
result = newTree(nnkRefTy,
89+
newTree(nnkObjectTy,
90+
newEmptyNode(),
91+
newTree(nnkOfInherit,
92+
newTree(nnkBracketExpr, ident"JSExternObj", base)),
93+
newEmptyNode()))
94+
95+
type
96+
JSExternObj[T] = parent(T)
97+
98+
var a: JSExternObj[JSObj]
99+
var d: JSExternObj[Document]
100+
a = d
101+
proc p(a: JSExternObj[JSObj]) =
102+
discard
103+
p(d)
104+
105+
assert JSExternObj[Document] is JSExternObj[HTMLElement]
106+
assert JSExternObj[HTMLElement] is JSExternObj[JSObj]
107+
assert JSExternObj[Document] is JSExternObj[JSObj]

0 commit comments

Comments
 (0)