gno icon indicating copy to clipboard operation
gno copied to clipboard

runtime panic with shadowed loop variable inside closure

Open omarsy opened this issue 3 weeks ago • 1 comments

Summary

When a loop variable is shadowed inside a for loop and then captured by an immediately-invoked function literal, the Gno VM panics with:

unexpected NameExpr type for GetPointerToMaybeHeapDefine

This does not happen in Go, where the program compiles and runs fine.

Reproduction

package main

import "fmt"

func main() {
    for i := 0; i < 3; i++ {
        i := i // shadow loop variable
        func() {
            i += 1
        }()
    }
    fmt.Println("Hello, 世界")
}

Running this in Gno produces:

unexpected panic: unexpected NameExpr type for GetPointerToMaybeHeapDefine
stacktrace:
main<VPBlock(1,0)>()
    main/for25.gno:7
...
panic: unexpected NameExpr type for GetPointerToMaybeHeapDefine

Expected Behavior

  • The inner i := i shadows the outer loop variable.
  • The closure mutates the shadowed variable.
  • No runtime or compile error.

omarsy avatar Dec 07 '25 23:12 omarsy

i think this is the one targeting this issue. thank you for reporting this.

https://github.com/gnolang/gno/pull/3990

ltzmaxwell avatar Dec 08 '25 10:12 ltzmaxwell