Closed mpl closed 1 year ago
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 strconv.Atoi
in that case), and the use in a closure, that leads to the bug. If the assignment to divisor does not come from a func with a double return, then there's no problem.
The following program
sample.go
triggers an unexpected resultExpected result
Got
Yaegi Version
eee72d1aae664bf6627ef955215d886dc7b105c0
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.