Skip to content

Commit

Permalink
sink tuples by values (nim-lang#24731)
Browse files Browse the repository at this point in the history
A reduced case
```nim
type AnObject = tuple
  a: string
  b: int
  c: int

proc mutate(a: sink AnObject) =
  `=wasMoved`(a)
  echo 1

# echo "Value is: ", obj.value
proc bar =
  mutate(("1.2", 0, 0))

bar()
```
  • Loading branch information
ringabout authored Feb 27, 2025
1 parent 0cb4cd1 commit 7e8a650
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/ccgutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ proc ccgIntroducedPtr*(conf: ConfigRef; s: PSym, retType: PType): bool =
result = true # ordinary objects are always passed by reference,
# otherwise casting doesn't work
of tyTuple:
result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options)
if s.typ.kind == tySink:
# it's a sink, so we pass it by value
result = false
else:
result = (getSize(conf, pt) > conf.target.floatSize*3) or (optByRef in s.options)
else:
result = false
# first parameter and return type is 'lent T'? --> use pass by pointer
Expand Down
16 changes: 16 additions & 0 deletions tests/arc/tarcmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ copying
123
42
@["", "d", ""]
mutate: 1
ok
destroying variable: 20
destroying variable: 10
Expand Down Expand Up @@ -882,3 +883,18 @@ proc test_18070() = # bug #18070
doAssert msg == "", "expected empty string but got: " & $msg

test_18070()

type AnObject = tuple
a: string
b: int
c: int

proc mutate(a: sink AnObject) =
`=wasMoved`(a)
echo "mutate: 1"

# echo "Value is: ", obj.value
proc bar =
mutate(("1.2", 0, 0))

bar()

0 comments on commit 7e8a650

Please sign in to comment.