diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index e6b6d5b42a..013ab1c506 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -802,15 +802,7 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing result = passCopyToSink(n, c, s) elif n.kind in {nkBracket, nkObjConstr, nkTupleConstr, nkClosure, nkNilLit} + nkCallKinds + nkLiterals: - if n.kind in nkCallKinds and n[0].kind == nkSym: - if n[0].sym.magic == mEnsureMove: - inc c.inEnsureMove - result = p(n[1], c, s, sinkArg) - dec c.inEnsureMove - else: - result = p(n, c, s, consumed) - else: - result = p(n, c, s, consumed) + result = p(n, c, s, consumed) elif ((n.kind == nkSym and isSinkParam(n.sym)) or isAnalysableFieldAccess(n, c.owner)) and isLastRead(n, c, s) and not (n.kind == nkSym and isCursor(n)): # Sinked params can be consumed only once. We need to reset the memory @@ -886,12 +878,6 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing if mode == normal and (isRefConstr or hasCustomDestructor(c, t)): result = ensureDestruction(result, n, c, s) of nkCallKinds: - if n[0].kind == nkSym and n[0].sym.magic == mEnsureMove: - inc c.inEnsureMove - result = p(n[1], c, s, sinkArg) - dec c.inEnsureMove - return - let inSpawn = c.inSpawn if n[0].kind == nkSym and n[0].sym.magic == mSpawn: c.inSpawn.inc @@ -909,13 +895,19 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing isDangerous = true result = shallowCopy(n) - for i in 1.. 0): - result[i] = p(n[i], c, s, sinkArg) - else: - result[i] = p(n[i], c, s, normal) + + if n[0].kind == nkSym and n[0].sym.magic == mEnsureMove: + inc c.inEnsureMove + result[1] = p(n[1], c, s, sinkArg) + dec c.inEnsureMove + else: + for i in 1.. 0): + result[i] = p(n[i], c, s, sinkArg) + else: + result[i] = p(n[i], c, s, normal) when false: if isDangerous: diff --git a/tests/destructor/tnewruntime_strutils.nim b/tests/destructor/tnewruntime_strutils.nim index 9c8d419734..7e4074218f 100644 --- a/tests/destructor/tnewruntime_strutils.nim +++ b/tests/destructor/tnewruntime_strutils.nim @@ -1,6 +1,6 @@ discard """ valgrind: true - cmd: '''nim c -d:nimAllocStats --gc:arc -d:useMalloc $file''' + cmd: '''nim c -d:nimAllocStats --mm:arc -d:useMalloc $file''' output: ''' @[(input: @["KXSC", "BGMC"]), (input: @["PXFX"]), (input: @["WXRQ", "ZSCZD"])] 14 @@ -252,3 +252,48 @@ proc main = main() + +block: + block: + type + JsonNode = ref object + + proc foo(d: JsonNode) = + discard + + proc test_something()= + var a = JsonNode() + foo ensureMove(a) + + test_something() + + block: + type + JsonNode = object + data: int + + proc foo(d: JsonNode) = + discard + + proc test_something()= + var a = JsonNode() + foo ensureMove(a) + + test_something() + + block: + type + JsonNode = object + data: int + + proc `=destroy`(x: JsonNode) = discard + + proc foo(d: JsonNode) = + discard + + proc test_something()= + var a = JsonNode() + foo ensureMove(a) + + test_something() +