Skip to content

Incorrect closure capture of loop variables in Yaegi interpreter #1713

@Aryan-Bagale

Description

@Aryan-Bagale

The following program sample.go triggers an unexpected result

package main

import "fmt"

type F func() int

func main() {
	var a, b, c F

	for i := 1; i <= 3; i++ {
		v := i
		switch i {
		case 1:
			a = F(func() int { return v })
		case 2:
			b = F(func() int { return v })
		case 3:
			c = F(func() int { return v })
		}
	}

	fmt.Println(a())
	fmt.Println(b())
	fmt.Println(c())
}

Expected result

$ go run ./sample.go
1
2
3

Got

$ yaegi ./sample.go
3
3
3

Yaegi Version

v0.16.1

Additional Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions