fixes move sideeffects issues [backport] (#22439)

* fixes move sideeffects issues [backport]

* fix openarray

* fixes openarray

(cherry picked from commit faf1c91e6a)
This commit is contained in:
ringabout
2023-08-11 00:04:29 +08:00
committed by narimiran
parent 665480372e
commit 6f00b46c4b
2 changed files with 34 additions and 3 deletions

View File

@@ -2368,9 +2368,22 @@ proc genMove(p: BProc; n: PNode; d: var TLoc) =
if op == nil:
resetLoc(p, a)
else:
let addrExp = makeAddr(n[1], p.module.idgen)
let wasMovedCall = newTreeI(nkCall, n.info, newSymNode(op), addrExp)
genCall(p, wasMovedCall, d)
var b: TLoc
initLocExpr(p, newSymNode(op), b)
case skipTypes(a.t, abstractVar+{tyStatic}).kind
of tyOpenArray, tyVarargs: # todo fixme generated `wasMoved` hooks for
# openarrays, but it probably shouldn't?
var s: string
if reifiedOpenArray(a.lode):
if a.t.kind in {tyVar, tyLent}:
s = "$1->Field0, $1->Field1" % [rdLoc(a)]
else:
s = "$1.Field0, $1.Field1" % [rdLoc(a)]
else:
s = "$1, $1Len_0" % [rdLoc(a)]
linefmt(p, cpsStmts, "$1($2);$n", [rdLoc(b), s])
else:
linefmt(p, cpsStmts, "$1($2);$n", [rdLoc(b), byRefLoc(p, a)])
else:
let flags = if not canMove(p, n[1], d): {needToCopy} else: {}
genAssignment(p, d, a, flags)

View File

@@ -0,0 +1,18 @@
discard """
targets: "c cpp"
"""
block:
var called = 0
proc bar(a: var int): var int =
inc called
result = a
proc foo =
var a = 2
var s = move bar(a)
doAssert called == 1
doAssert s == 2
foo()