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 #13659 #13674

Merged
merged 11 commits into from
Mar 17, 2020
4 changes: 2 additions & 2 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,10 @@ proc p(n: PNode; c: var Con; mode: ProcessMode): PNode =
if n[0].kind in {nkDotExpr, nkCheckedFieldExpr}:
cycleCheck(n, c)
assert n[1].kind notin {nkAsgn, nkFastAsgn}
result = moveOrCopy(n[0], n[1], c)
result = moveOrCopy(p(n[0], c, mode), n[1], c)
else:
result = copyNode(n)
result.add copyTree(n[0])
result.add p(n[0], c, mode)
result.add p(n[1], c, consumed)
of nkRaiseStmt:
if optOwnedRefs in c.graph.config.globalOptions and n[0].kind != nkEmpty:
Expand Down
26 changes: 25 additions & 1 deletion tests/destructor/tselect.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
discard """
output: '''abcsuffix
xyzsuffix'''
xyzsuffix
destroy foo 2
destroy foo 1
'''
cmd: '''nim c --gc:arc $file'''
"""

Expand All @@ -24,3 +27,24 @@ proc test(param: string; cond: bool) =

test("suffix", true)
test("suffix", false)



#--------------------------------------------------------------------
# issue #13659

type
Foo = ref object
data: int
parent: Foo

proc `=destroy`(self: var type(Foo()[])) =
echo "destroy foo ", self.data
for i in self.fields: i.reset

proc getParent(self: Foo): Foo = self.parent

var foo1 = Foo(data: 1)
var foo2 = Foo(data: 2, parent: foo1)

foo2.getParent.data = 1