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

fixes #22866; fixes #19998; ensure destruction for Object construction with custom destructors #22901

Merged
merged 6 commits into from
Nov 2, 2023
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
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
Loading