Skip to content

Commit

Permalink
fixes #24504; fixes ensureMove for refs (#24505)
Browse files Browse the repository at this point in the history
fixes #24504

(cherry picked from commit d0288d3)
  • Loading branch information
ringabout authored and narimiran committed Dec 8, 2024
1 parent 9f43f9f commit 15e5ddc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
36 changes: 14 additions & 22 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -795,15 +795,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
Expand Down Expand Up @@ -878,12 +870,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
Expand All @@ -900,13 +886,19 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
isDangerous = true

result = shallowCopy(n)
for i in 1..<n.len:
if i < L and isCompileTimeOnly(parameters[i]):
result[i] = n[i]
elif i < L and (isSinkTypeForParam(parameters[i]) or inSpawn > 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..<n.len:
if i < L and isCompileTimeOnly(parameters[i]):
result[i] = n[i]
elif i < L and (isSinkTypeForParam(parameters[i]) or inSpawn > 0):
result[i] = p(n[i], c, s, sinkArg)
else:
result[i] = p(n[i], c, s, normal)

when false:
if isDangerous:
Expand Down
47 changes: 46 additions & 1 deletion tests/destructor/tnewruntime_strutils.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 15e5ddc

Please sign in to comment.