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

[VM] Immutable proc argument is incorrectly modified #19464

Closed
konsumlamm opened this issue Jan 28, 2022 · 0 comments · Fixed by #19902
Closed

[VM] Immutable proc argument is incorrectly modified #19464

konsumlamm opened this issue Jan 28, 2022 · 0 comments · Fixed by #19902
Labels
VM see also `const` label

Comments

@konsumlamm
Copy link
Contributor

Example

type
  Wrapper = object
    inner: int

proc assign(r: var Wrapper, a: Wrapper) =
  r = a

proc myEcho(a: Wrapper) =
  var tmp = a
  assign(tmp, Wrapper(inner: 0)) # this shouldn't modify `a`
  echo a

static:
  var result: Wrapper
  assign(result, Wrapper(inner: 1))
  myEcho(result)

Current Output

(inner: 0)

Expected Output

(inner: 1)

Additional Information

The problem seems to be that myEcho directly modifies a, instead of just the tmp copy.

This doesn't occur if

  • the code isn't run at compile time
  • no wrapper type is used
  • normal assignment is used instead of assign
$ nim -v
Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2022-01-27
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 4b723c0f5350c5bfb9722dace7a40c66a4630a68
active boot switches: -d:release

(This example may seem contrived, but that's because I tried to reduce it as much as possible. I originally encountered this bug while adding debug echos in nim-lang/bigints.)

@ringabout ringabout added the VM see also `const` label label Jun 13, 2022
Araq pushed a commit that referenced this issue Jun 22, 2022
 (#19902) [backport]

* revert #12217 since the root problem seems to have been fixed; fix #15974;fix #12551; fix #19464

* fix #16020; fix #16780

* fix tests and #16613

* fix #14553

* fix #19909; skip skipRegisterAddr

* fix #18641
narimiran pushed a commit that referenced this issue Jun 23, 2022
 (#19902) [backport]

* revert #12217 since the root problem seems to have been fixed; fix #15974;fix #12551; fix #19464

* fix #16020; fix #16780

* fix tests and #16613

* fix #14553

* fix #19909; skip skipRegisterAddr

* fix #18641

(cherry picked from commit 3cb2d7a)
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VM see also `const` label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants