Skip to content

runtime panic with shadowed loop variable inside closure #4945

@omarsy

Description

@omarsy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions