Skip to content

Const block variable memory being reused unexpectedly #13918

Closed
timotheecour/Nim
#795
@guzba

Description

@guzba

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions