Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong code generation for synthesized object hooks #1338

Merged
Merged
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
7 changes: 6 additions & 1 deletion compiler/sem/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
23 changes: 23 additions & 0 deletions tests/lang_objects/destructor/tsuper_type_destructor_bug.nim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that we should either add a {.passC.} or a config file to turn on -Werror=incompatible-pointer-types for this test

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config file approach is already used by another test, but it's brittle and I'd rather not embrace it further.

I'll make a separate PR that adds debug functionality for pretty-printing the initial MIR, and then this test can make sure the MIR for the destroy hook is sensible.

Original file line number Diff line number Diff line change
@@ -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()
Loading