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

Const block variable memory being reused unexpectedly #13918

Closed
guzba opened this issue Apr 7, 2020 · 1 comment · Fixed by #21351 · May be fixed by timotheecour/Nim#795
Closed

Const block variable memory being reused unexpectedly #13918

guzba opened this issue Apr 7, 2020 · 1 comment · Fixed by #21351 · May be fixed by timotheecour/Nim#795

Comments

@guzba
Copy link
Contributor

guzba commented Apr 7, 2020

When using a block to initialize a const at compile time, I am seeing variables keep values from the previous scope / not be re-initialized as expected.

Run with nim c -r test.nim

const test = block:
  for i in 1 .. 5:
    var arr: array[3, int]
    var val: int
    echo arr, " ", val
    for j in 0 ..< len(arr):
      arr[j] = i
      val = i
  0

Current Output

[0, 0, 0] 0
[1, 1, 1] 1
[2, 2, 2] 2
[3, 3, 3] 3
[4, 4, 4] 4

Expected Output

[0, 0, 0] 0
[0, 0, 0] 0
[0, 0, 0] 0
[0, 0, 0] 0
[0, 0, 0] 0

Additional Information

  • If you change the const to let the output is as expected.
$ nim -v
Nim Compiler Version 1.2.0 [Windows: amd64]
Compiled at 2020-04-03
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 7e83adff84be5d0c401a213eccb61e321a3fb1ff
active boot switches: -d:release
@metagn
Copy link
Collaborator

metagn commented Apr 25, 2020

Same issue as #10938

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