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

If seq is the sink parameter, it can be changed from the function #12340

Open
alex65536 opened this issue Oct 2, 2019 · 4 comments · May be fixed by #23360
Open

If seq is the sink parameter, it can be changed from the function #12340

alex65536 opened this issue Oct 2, 2019 · 4 comments · May be fixed by #23360
Assignees
Labels

Comments

@alex65536
Copy link
Contributor

If I pass a seq[T] as a sink parameter into the function and change it inside the function, the seq[T] is changed in outer scope. According to https://nim-lang.org/docs/destructors.html, it should be copied to the function, so no change should occur.

Maybe I just don't understand the behavior of sink correctly, but it still remains a vaild bug, because I can change the variables declared with let (see the example below).

Example

func consume(x: sink seq[int]) =
  x[0] += 5

let x = @[1, 2, 3, 4]
consume x
echo x

Current Output

@[6, 2, 3, 4]

Expected Output

@[1, 2, 3, 4]

Additional Information

$ nim -v
Nim Compiler Version 1.0.0 [Linux: amd64]
Compiled at 2019-10-24
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release

The version of Nim I am using is from Debian unstable repos.

@Araq
Copy link
Member

Araq commented Oct 2, 2019

According to https://nim-lang.org/docs/destructors.html, it should be copied to the function, so no change should occur

Agreed.

@mratsim
Copy link
Collaborator

mratsim commented Nov 11, 2019

Quoting myself from #12641

It seems like sink parameter can be mutated.

This is actually very useful to me as for message passing a proc might want to take ownership of a message, update it and sink it into a channel. A var param would be needed otherwise and wouldn't be able to express the sink semantics.

i.e. this compiles

proc foo(x: sink int) =

  x += 1
  echo x

proc main() =
  var z = 3
  foo(z)


main()

However, this mutability of sink parameter is not clearly specified in the destructors spec.
If it's intentional, it should be added, if not it should be removed (and a sink var would be useful).

@Araq
Copy link
Member

Araq commented Nov 12, 2019

It's intentional.

@ringabout
Copy link
Member

ringabout commented Mar 2, 2021

It works with --gc:arc or --gc:destructors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants