Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect output when closure is set in a loop #1594

Closed
mvertes opened this issue Sep 21, 2023 · 0 comments · Fixed by #1595
Closed

incorrect output when closure is set in a loop #1594

mvertes opened this issue Sep 21, 2023 · 0 comments · Fixed by #1595
Labels
area/core bug Something isn't working
Milestone

Comments

@mvertes
Copy link
Member

mvertes commented Sep 21, 2023

The following program sample.go triggers an unexpected result

package main

func main() {
	var fns []func()
	for _, v := range []int{1, 2, 3} {
		x := v*100 + v
		fns = append(fns, func() { println(x) })
	}
	for _, fn := range fns {
		fn()
	}
}

Expected result

$ go run ./sample.go
101
202
303

Got

$ yaegi ./sample.go
303
303
303

Yaegi Version

v0.15.1

Additional Notes

The same issue as in gnolang/gno#1135

@mvertes mvertes added bug Something isn't working area/core labels Sep 21, 2023
@mvertes mvertes modified the milestone: v0.15.x Sep 21, 2023
mvertes added a commit to mvertes/yaegi that referenced this issue Sep 21, 2023
@traefiker traefiker added this to the v0.15.x milestone Sep 21, 2023
traefiker pushed a commit that referenced this issue Sep 21, 2023
Avoid a spurious optimisation which forces a variable to be reused instead of redefined for assignment operation. This ensures that a variable defined in a loop is re-allocated, preserving the previous instance when used by a closure for example.

Fix #1594
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants