gno
gno copied to clipboard
runtime panic with shadowed loop variable inside closure
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 := ishadows the outer loop variable. - The closure mutates the shadowed variable.
- No runtime or compile error.
i think this is the one targeting this issue. thank you for reporting this.
https://github.com/gnolang/gno/pull/3990