Skip to content

Accessing compileTime variable at runtime returns zeroed data. #13375

Open
@ftsf

Description

@ftsf

After doing anything runtime specific, eg. looping, compileTime variables return zeroed data.

The manual states

"compileTime variables are available at runtime too. This simplifies certain idioms where variables are filled at compile-time (for example, lookup tables) but accessed at runtime:"

But this does not appear to work except before doing any runtime operations.

Example taken from manual

import macros

var nameToProc {.compileTime.}: seq[(string, proc (): string {.nimcall.})]

macro registerProc(p: untyped): untyped =
  result = newTree(nnkStmtList, p)
  
  let procName = p[0]
  let procNameAsStr = $p[0]
  result.add quote do:
    nameToProc.add((`procNameAsStr`, `procName`))

proc foo: string {.registerProc.} = "foo"
proc bar: string {.registerProc.} = "bar"
proc baz: string {.registerProc.} = "baz"

# this works as expected
echo nameToProc.len
doAssert nameToProc[2][1]() == "baz"

for i in 0..<1:
  echo nameToProc.len # problem begins here, this returns 0

# this also doesn't work
echo nameToProc.len
doAssert nameToProc[2][1]() == "baz"

Current Output

3
0
0
/usercode/in.nim(26)     in
/playground/nim/lib/system/fatal.nim(39) sysFatal
Error: unhandled exception: index out of bounds, the container is empty [IndexError]

Expected Output

3
3
3
$ nim -v
Nim Compiler Version 1.0.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions