From f004178a811fdc5e0e83a43d2d70ae405ef9b6bf Mon Sep 17 00:00:00 2001 From: cooldome Date: Tue, 17 Mar 2020 14:26:08 +0000 Subject: [PATCH] fixes #13659 --- compiler/injectdestructors.nim | 4 ++-- tests/destructor/tselect.nim | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 161cb7652449..48faa7e84c66 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -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: diff --git a/tests/destructor/tselect.nim b/tests/destructor/tselect.nim index 9262b47d498d..c22bf7203b50 100644 --- a/tests/destructor/tselect.nim +++ b/tests/destructor/tselect.nim @@ -1,6 +1,9 @@ discard """ output: '''abcsuffix -xyzsuffix''' +xyzsuffix +destroy foo 2 +destroy foo 1 +''' cmd: '''nim c --gc:arc $file''' """ @@ -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 \ No newline at end of file