Skip to content

fixes #24806; generates =wasMoved for seqs self-assignment to reset the buffer #24830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions compiler/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,14 @@ proc forallElements(c: var TLiftCtx; t: PType; body, x, y: PNode) =
body.sons.setLen counterIdx

proc checkSelfAssignment(c: var TLiftCtx; t: PType; body, x, y: PNode) =
let addrX = newTreeIT(nkAddr, c.info, makePtrType(c.fn, x.typ, c.idgen), x)
let addrY = newTreeIT(nkAddr, c.info, makePtrType(c.fn, y.typ, c.idgen), y)
var cond = callCodegenProc(c.g, "sameSeqPayload", c.info,
newTreeIT(nkAddr, c.info, makePtrType(c.fn, x.typ, c.idgen), x),
newTreeIT(nkAddr, c.info, makePtrType(c.fn, y.typ, c.idgen), y)
addrX, addrY
)
cond.typ() = getSysType(c.g, c.info, tyBool)
body.add genIf(c, cond, newTreeI(nkReturnStmt, c.info, newNodeI(nkEmpty, c.info)))
body.add genIf(c, cond,
genBuiltin(c, mWasMoved, "`=wasMoved`", addrX))

proc fillSeqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
case c.kind
Expand All @@ -590,10 +592,10 @@ proc fillSeqOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
of attachedAsgn, attachedDeepCopy:
# we generate:
# if x.p == y.p:
# return
# setLen(dest, y.len)
# wasMoved(x)
# setLen(x, y.len)
# var i = 0
# while i < y.len: dest[i] = y[i]; inc(i)
# while i < y.len: x[i] = y[i]; inc(i)
# This is usually more efficient than a destroy/create pair.
checkSelfAssignment(c, t, body, x, y)
body.add setLenSeqCall(c, t, x, y)
Expand Down
48 changes: 48 additions & 0 deletions tests/arc/tmalloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,51 @@ block: # bug #22058
V(v: foo())

doAssert bar().v == @[byte(0)]

type
GlobFilter* = object
incl*: bool
glob*: string

GlobState* = object
one: int
two: int

proc aa() =
let filters = @[GlobFilter(incl: true, glob: "**")]
var wbg = newSeqOfCap[GlobState](1)
wbg.add GlobState()
var
dirc = @[wbg]
while true:
wbg = dirc[^1]
dirc.add wbg
break

var handlerLocs = newSeq[string]()
handlerLocs.add "sammich"
aa()
aa()

block: # bug #24806
block:
proc aa() =
var
a = @[0]
b = @[a]
block:
a = b[0]
b.add a

aa()

block:
proc aa() =
var
a = @[0]
b = @[a]
block:
a = b[^1]
b.add a

aa()