-
Notifications
You must be signed in to change notification settings - Fork 351
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
double assignment + closure, in a for loop, reuses the same variable, instead of assigning to a new one. #1497
Comments
Alright, here's the actual bug, which actually has nothing to do with what I was suspecting: package main
import "strconv"
type monkey struct {
test func() int
}
func main() {
input := []string{"1", "2", "3"}
var monkeys []*monkey
// for k, v := range input { // <-- no problem if we assign k to divisor, for example.
for _, v := range input {
kong := monkey{}
// divisor := k <-- no problem
divisor, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
// divisor, _ := := strconv.Atoi(v) <-- also buggy
println(divisor)
kong.test = func() int {
return divisor
}
monkeys = append(monkeys, &kong)
}
for _, mk := range monkeys {
println(mk.test())
}
} % go run ./issues/1497/main.go
1
2
3
1
2
3
%
%
%
% yaegi run ./issues/1497/main.go
1
2
3
3
3
3 So I believe it's the combination of the double return/assignment (from |
mpl
changed the title
temporary title: objects in slice not properly updated when iterating on them
double assignment + closure, in a for loop, reuses the same variable, instead of assigning to a new one.
Dec 26, 2022
mvertes
added a commit
that referenced
this issue
Mar 22, 2023
In the case of a Go short definition (i.e. `a, b := f()`), the new defined variables must be (re-)created in order to preserve the previous value (if in a loop) which can be still in use in the context of a closure. This must not apply to redeclared variables which simply see their value reassigned. The problem was both occuring in callBin() for runtime functions and assignFromCall() for functions created in the interpreter. Fixes #1497.
traefiker
pushed a commit
that referenced
this issue
Mar 24, 2023
In the case of a Go short definition (i.e. `a, b := f()`), the new defined variables must be (re-)created in order to preserve the previous value (if in a loop) which can be still in use in the context of a closure. This must not apply to redeclared variables which simply see their value reassigned. The problem was both occuring in callBin() for runtime functions and assignFromCall() for functions created in the interpreter. Fixes #1497.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following program
sample.go
triggers an unexpected resultExpected result
Got
Yaegi Version
eee72d1
Additional Notes
I was solving the 11th advent of code: https://adventofcode.com/2022/day/11
and I noticed the result given by yaegi is different from the one with the go compiler.
I haven't had time to narrow it down yet, and to write a small repro. However, I strongly suspect the bug is connected to:
monkeys[monkeyDest].items = append(monkeys[monkeyDest].items, worry)
as the rest of the code is completely trivial.
I will update this issue with a better repro and title later.
The text was updated successfully, but these errors were encountered: