diff options
-rw-r--r-- | compiler/injectdestructors.nim | 6 | ||||
-rw-r--r-- | tests/destructor/tnewruntime_misc.nim | 17 |
2 files changed, 22 insertions, 1 deletions
diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 4f96d236f..87f09da59 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -796,7 +796,11 @@ proc p(n: PNode; c: var Con): PNode = result = n of nkAsgn, nkFastAsgn: if hasDestructor(n[0].typ) and n[1].kind notin {nkProcDef, nkDo, nkLambda}: - result = moveOrCopy(n[0], n[1], c) + # rule (self-assignment-removal): + if n[1].kind == nkSym and n[0].kind == nkSym and n[0].sym == n[1].sym: + result = newNodeI(nkEmpty, n.info) + else: + result = moveOrCopy(n[0], n[1], c) else: result = copyNode(n) recurse(n, result) diff --git a/tests/destructor/tnewruntime_misc.nim b/tests/destructor/tnewruntime_misc.nim index 1fb5b0151..0e9541e2e 100644 --- a/tests/destructor/tnewruntime_misc.nim +++ b/tests/destructor/tnewruntime_misc.nim @@ -3,6 +3,7 @@ discard """ output: '''(field: "value") Indeed axc +(v: 10) 0 new: 0''' """ @@ -58,5 +59,21 @@ proc test(p: owned proc()) = test(proc() = discard) +# bug #10689 + +type + O = object + v: int + +proc `=sink`(d: var O, s: O) = + d.v = s.v + +proc selfAssign = + var o = O(v: 10) + o = o + echo o + +selfAssign() + let (a, d) = allocCounters() discard cprintf("%ld new: %ld\n", a - unpairedEnvAllocs() - d, allocs) |