-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: correctly init variables assigned from function call
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.
- Loading branch information
Showing
5 changed files
with
122 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
type monkey struct { | ||
test func() int | ||
} | ||
|
||
func main() { | ||
input := []string{"1", "2", "3"} | ||
|
||
var monkeys []*monkey | ||
|
||
for _, v := range input { | ||
kong := monkey{} | ||
divisor, err := strconv.Atoi(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Print(divisor, " ") | ||
kong.test = func() int { | ||
return divisor | ||
} | ||
monkeys = append(monkeys, &kong) | ||
} | ||
|
||
for _, mk := range monkeys { | ||
fmt.Print(mk.test(), " ") | ||
} | ||
} | ||
|
||
// Output: | ||
// 1 2 3 1 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type monkey struct { | ||
test func() int | ||
} | ||
|
||
func getk(k int) (int, error) { return k, nil } | ||
|
||
func main() { | ||
input := []string{"1", "2", "3"} | ||
|
||
var monkeys []*monkey | ||
|
||
for k := range input { | ||
kong := monkey{} | ||
divisor, _ := getk(k) | ||
fmt.Print(divisor, " ") | ||
kong.test = func() int { | ||
return divisor | ||
} | ||
monkeys = append(monkeys, &kong) | ||
} | ||
|
||
for _, mk := range monkeys { | ||
fmt.Print(mk.test(), " ") | ||
} | ||
} | ||
|
||
// Output: | ||
// 0 1 2 0 1 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters