Skip to content
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

fixes #23759; rework move for refc #23764

Merged
merged 4 commits into from
Jun 29, 2024
Merged
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
11 changes: 8 additions & 3 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
of tyString:
if optSeqDestructors in p.config.globalOptions:
genGenericAsgn(p, dest, src, flags)
elif (needToCopy notin flags and src.storage != OnStatic) or canMove(p, src.lode, dest):
elif ({needToCopy, needToCopySinkParam} * flags == {} and src.storage != OnStatic) or canMove(p, src.lode, dest):
genRefAssign(p, dest, src)
else:
if (dest.storage == OnStack and p.config.selectedGC != gcGo) or not usesWriteBarrier(p.config):
Expand Down Expand Up @@ -2345,8 +2345,13 @@ proc genMove(p: BProc; n: PNode; d: var TLoc) =
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)
if n[1].kind == nkSym and isSinkParam(n[1].sym):
var tmp = getTemp(p, n[1].typ.skipTypes({tySink}))
genAssignment(p, tmp, a, {needToCopySinkParam})
genAssignment(p, d, tmp, {})
resetLoc(p, tmp)
else:
genAssignment(p, d, a, {})
resetLoc(p, a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you always reset a then you mutate const data, potentially. What you need to do is to shadow the sink parameter with a variable of the same name.


proc genDestroy(p: BProc; n: PNode) =
Expand Down
1 change: 1 addition & 0 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ proc rdCharLoc(a: TLoc): Rope =
type
TAssignmentFlag = enum
needToCopy
needToCopySinkParam
needTempForOpenArray
TAssignmentFlags = set[TAssignmentFlag]

Expand Down
9 changes: 9 additions & 0 deletions tests/refc/tsinkbug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ var obj = AnObject(value: 42)
echo "Value is: ", obj.value
mutate(obj)
echo "Value is: ", obj.value

proc p(x: sink string) =
var y = move(x)
doAssert x.len == 0
doAssert y.len == 4

p("1234")
var s = "oooo"
p(s)
Loading