diff --git a/compiler/sem/liftdestructors.nim b/compiler/sem/liftdestructors.nim index 202f532eb6c..5d7e48031ea 100644 --- a/compiler/sem/liftdestructors.nim +++ b/compiler/sem/liftdestructors.nim @@ -196,7 +196,12 @@ proc fillBodyObj(c: var TLiftCtx; n, body, x, y: PNode; enforceDefaultOp: bool) proc fillBodyObjTImpl(c: var TLiftCtx; t: PType, body, x, y: PNode) = if t.len > 0 and t[0] != nil: - fillBody(c, skipTypes(t[0], abstractPtrs), body, x, y) + # also apply the operation to the super type. An up-conversion is required + # for proper typing + let + base = skipTypes(t[0], abstractPtrs) + obj = newTreeIT(nkObjUpConv, x.info, base, x) + fillBody(c, base, body, obj, y) fillBodyObj(c, t.n, body, x, y, enforceDefaultOp = false) proc fillBodyObjT(c: var TLiftCtx; t: PType, body, x, y: PNode) = diff --git a/tests/lang_objects/destructor/tsuper_type_destructor_bug.nim b/tests/lang_objects/destructor/tsuper_type_destructor_bug.nim new file mode 100644 index 00000000000..dd2851bc0d1 --- /dev/null +++ b/tests/lang_objects/destructor/tsuper_type_destructor_bug.nim @@ -0,0 +1,23 @@ +discard """ + description: ''' + Regression test for an internal typing issue with synthesized hooks for + objects using inheritance + ''' + targets: c js vm +""" + +type + Parent = object of RootObj + x: int + Sub = object of Parent + +proc `=destroy`(x: var Parent) = + # ensure that the parameter is at least accessible at run-time + doAssert x.x == 1 + +# the destroy hook for `Sub` is synthesized by the compiler + +proc test() = + var x = Sub(x: 1) + +test()