Skip to content

Commit

Permalink
fix #22173 sink paramers not moved into closure (refc) (#22359)
Browse files Browse the repository at this point in the history
* use genRefAssign when assign to sink string

* add test case
  • Loading branch information
bung87 authored Aug 2, 2023
1 parent 825a0e7 commit b40da81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 9 additions & 6 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,15 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
if (dest.storage == OnStack and p.config.selectedGC != gcGo) or not usesWriteBarrier(p.config):
linefmt(p, cpsStmts, "$1 = #copyString($2);$n", [dest.rdLoc, src.rdLoc])
elif dest.storage == OnHeap:
# we use a temporary to care for the dreaded self assignment:
var tmp: TLoc
getTemp(p, ty, tmp)
linefmt(p, cpsStmts, "$3 = $1; $1 = #copyStringRC1($2);$n",
[dest.rdLoc, src.rdLoc, tmp.rdLoc])
linefmt(p, cpsStmts, "if ($1) #nimGCunrefNoCycle($1);$n", [tmp.rdLoc])
if dest.lode.typ.kind == tySink:
genRefAssign(p, dest, src)
else:
# we use a temporary to care for the dreaded self assignment:
var tmp: TLoc
getTemp(p, ty, tmp)
linefmt(p, cpsStmts, "$3 = $1; $1 = #copyStringRC1($2);$n",
[dest.rdLoc, src.rdLoc, tmp.rdLoc])
linefmt(p, cpsStmts, "if ($1) #nimGCunrefNoCycle($1);$n", [tmp.rdLoc])
else:
linefmt(p, cpsStmts, "#unsureAsgnRef((void**) $1, #copyString($2));$n",
[addrLoc(p.config, dest), rdLoc(src)])
Expand Down
20 changes: 20 additions & 0 deletions tests/gc/t22173.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
discard """
cmd: '''nim c --gc:refc -r $file'''
"""
const Memo = 100 * 1024

proc fff(v: sink string): iterator(): char =
return iterator(): char =
for c in v:
yield c

var tmp = newString(Memo)

let iter = fff(move(tmp))

while true:
let v = iter()
if finished(iter):
break

doAssert getOccupiedMem() < Memo * 3

0 comments on commit b40da81

Please sign in to comment.