Skip to content

Commit

Permalink
fixes #22866; fixes #19998; ensure destruction for Object constructio…
Browse files Browse the repository at this point in the history
…n with custom destructors (#22901)

fixes #22866;
fixes #19998
  • Loading branch information
ringabout authored Nov 2, 2023
1 parent 40e33de commit b68e0aa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 3 additions & 1 deletion compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,9 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
result[i][1] = p(n[i][1], c, s, m)
else:
result[i] = p(n[i], c, s, m)
if mode == normal and isRefConstr:
if mode == normal and (isRefConstr or (hasDestructor(c, t) and
getAttachedOp(c.graph, t, attachedDestructor) != nil and
sfOverridden in getAttachedOp(c.graph, t, attachedDestructor).flags)):
result = ensureDestruction(result, n, c, s)
of nkCallKinds:
if n[0].kind == nkSym and n[0].sym.magic == mEnsureMove:
Expand Down
15 changes: 9 additions & 6 deletions tests/arc/tcomputedgoto.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
discard """
cmd: '''nim c --gc:arc $file'''
output: '''2
2'''
cmd: '''nim c --mm:arc $file'''
output: '''
2
2
destroyed
'''
"""

type
ObjWithDestructor = object
a: int
proc `=destroy`(self: var ObjWithDestructor) =
proc `=destroy`(self: ObjWithDestructor) =
echo "destroyed"

proc `=`(self: var ObjWithDestructor, other: ObjWithDestructor) =
proc `=copy`(self: var ObjWithDestructor, other: ObjWithDestructor) =
echo "copied"

proc test(a: range[0..1], arg: ObjWithDestructor) =
Expand Down Expand Up @@ -38,4 +41,4 @@ proc test(a: range[0..1], arg: ObjWithDestructor) =
if iteration == 2:
break

test(1, ObjWithDestructor())
test(1, ObjWithDestructor())
11 changes: 7 additions & 4 deletions tests/arc/tcomputedgotocopy.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
discard """
cmd: '''nim c --gc:arc $file'''
output: '''2
2'''
cmd: '''nim c --mm:arc $file'''
output: '''
2
2
destroyed
'''
"""

type
ObjWithDestructor = object
a: int
proc `=destroy`(self: var ObjWithDestructor) =
proc `=destroy`(self: ObjWithDestructor) =
echo "destroyed"

proc `=copy`(self: var ObjWithDestructor, other: ObjWithDestructor) =
Expand Down

0 comments on commit b68e0aa

Please sign in to comment.