Skip to content

Commit 1dbbac7

Browse files
wdvxdr1123mdempsky
authored andcommitted
[release-branch.go1.20] cmd/compile: fix ir.StaticValue for ORANGE
Range statement will mutate the key and value, so we should treat them as reassigned. Fixes #59580 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]> (cherry picked from commit 89567a3) Reviewed-on: https://go-review.googlesource.com/c/go/+/484136 Run-TryBot: Matthew Dempsky <[email protected]>
1 parent 99001c4 commit 1dbbac7

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
@@ -959,6 +959,11 @@ func reassigned(name *Name) bool {
959959
if isName(OuterValue(n.X)) {
960960
return true
961961
}
962+
case ORANGE:
963+
n := n.(*RangeStmt)
964+
if isName(n.Key) || isName(n.Value) {
965+
return true
966+
}
962967
case OCLOSURE:
963968
n := n.(*ClosureExpr)
964969
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)