Skip to content

Commit 89567a3

Browse files
wdvxdr1123gopherbot
authored andcommitted
cmd/compile: fix ir.StaticValue for ORANGE
Range statement will mutate the key and value, so we should treat them as reassigned. Fixes golang#59572 Change-Id: I9c6b67d938760a0c6a1d9739f2737c67af4a3a10 Reviewed-on: https://go-review.googlesource.com/c/go/+/483855 Run-TryBot: Wayne Zuo <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 134af2e commit 89567a3

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/cmd/compile/internal/ir/expr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,11 @@ func reassigned(name *Name) bool {
958958
if isName(OuterValue(n.X)) {
959959
return true
960960
}
961+
case ORANGE:
962+
n := n.(*RangeStmt)
963+
if isName(n.Key) || isName(n.Value) {
964+
return true
965+
}
961966
case OCLOSURE:
962967
n := n.(*ClosureExpr)
963968
if Any(n.Func, do) {

test/fixedbugs/issue59572.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// run
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func foo() {
10+
println("foo")
11+
}
12+
13+
func main() {
14+
fn := foo
15+
for _, fn = range list {
16+
fn()
17+
}
18+
}
19+
20+
var list = []func(){
21+
func() {
22+
println("1")
23+
},
24+
func() {
25+
println("2")
26+
},
27+
func() {
28+
println("3")
29+
},
30+
}

test/fixedbugs/issue59572.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
2
3+
3

0 commit comments

Comments
 (0)