I'm looking at https://github.com/nim-lang/Nim/blob/devel/tests/init/tzeroarray.nim which has a variable declared in a loop:
for i in 0 .. 1:
var a: array[0..4, int]
if a[0] != 0: quit "bug"
a[0] = 6
In the AST, the variable a gets tagged sfGlobal, and in the generated c code, it does indeed get declared in the global c scope - why? I'd expect the lifetime of the variable to be for the duration of the loop as it cannot be accessed outside, and thus not be created at the global scope (in fact, the c gen goes on to add a special case globals in for loops in order to clear the variable for each iteration, instead of handling it like any local).
I guess this has to do with the for loop using an iterator instead of a plain "while" loop. If you use a while loop instead the array is created locally.
I made up this as alternative example which does use a local var for the array.
template forx(n: untyped, s, e: int, code: untyped) {.immediate.} =
var n = s
while(n < e):
inc n
code
proc main() =
forx i, 0, 1:
var a: array[0..4, int]
if a[i] != 0: quit "bug"
a[i] = 6
main()
Yeah... sorry :(
What you say is what I was expecting at first but then I was going for and back between versions and misinterpreted the global typedef with a global variable declaration at some point.
But then it makes "sense" to me that a global var always is in global scope. I guess thats just handled special and there is no "stack" available.