mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 09:54:49 +00:00
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:
@@ -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)
|
||||
|
||||
18
tests/destructor/tmove.nim
Normal file
18
tests/destructor/tmove.nim
Normal 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()
|
||||
Reference in New Issue
Block a user