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

NimVM reuses default-value sequences, differing from Nim and from NimVM with explicit defaults #20985

Closed
jrfondren opened this issue Dec 1, 2022 · 2 comments · Fixed by #21351

Comments

@jrfondren
Copy link
Contributor

Description

The following program with identical code blocks (but for let v. const) results in a != b. This isn't the case when the group and groups variables are explicitly initialized to @[].

let a = block:
  var groups: seq[seq[int]]
  for i in 0 ..< 3:
    var group: seq[int]
    for j in 0 ..< 3:
      group.add j
    groups.add group
  groups

const b = block:
  var groups: seq[seq[int]]
  for i in 0 ..< 3:
    var group: seq[int]
    for j in 0 ..< 3:
      group.add j
    groups.add group
  groups

echo a
echo b

Nim Version

Nim Compiler Version 1.6.10 [Linux: amd64]

and:

Nim Compiler Version 1.7.3 [Linux: amd64]
Compiled at 2022-12-01
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: b36f511

Current Output

@[@[0, 1, 2], @[0, 1, 2], @[0, 1, 2]]
@[@[0, 1, 2], @[0, 1, 2, 0, 1, 2], @[0, 1, 2, 0, 1, 2, 0, 1, 2]]

Expected Output

@[@[0, 1, 2], @[0, 1, 2], @[0, 1, 2]]
@[@[0, 1, 2], @[0, 1, 2], @[0, 1, 2]]

Possible Solution

No response

Additional Information

Workaround: don't trust https://nim-lang.org/docs/manual.html#statements-and-expressions-var-statement in const blocks:

let a = block:
  var groups: seq[seq[int]] = @[]
  for i in 0 ..< 3:
    var group: seq[int] = @[]
    for j in 0 ..< 3:
      group.add j
    groups.add group
  groups

const b = block:
  var groups: seq[seq[int]] = @[]
  for i in 0 ..< 3:
    var group: seq[int] = @[]
    for j in 0 ..< 3:
      group.add j
    groups.add group
  groups

echo a
echo b
@metagn
Copy link
Collaborator

metagn commented Dec 1, 2022

Related/same #10938

ringabout added a commit that referenced this issue Dec 6, 2022
@ringabout
Copy link
Member

Duplicate of #10938

@ringabout ringabout marked this as a duplicate of #10938 Dec 14, 2022
Araq pushed a commit that referenced this issue Mar 1, 2023
…lizes global variables with null values in VM (#21351)

* fixes #10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
nim-lang#20985; always initializes global variables with null values in VM (nim-lang#21351)

* fixes nim-lang#10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests
narimiran pushed a commit that referenced this issue Apr 26, 2023
…lizes global variables with null values in VM (#21351)

* fixes #10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests

(cherry picked from commit 1b1412f)
narimiran pushed a commit that referenced this issue Apr 26, 2023
…lizes global variables with null values in VM (#21351)

* fixes #10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests

(cherry picked from commit 1b1412f)
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
nim-lang#20985; always initializes global variables with null values in VM (nim-lang#21351)

* fixes nim-lang#10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants