Skip to content

Commit

Permalink
cmd/compile: fix ir.StaticValue for ORANGE
Browse files Browse the repository at this point in the history
Range statement will mutate the key and value, so we should treat them as reassigned.

Fixes #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]>
  • Loading branch information
wdvxdr1123 authored and gopherbot committed Apr 12, 2023
1 parent 134af2e commit 89567a3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cmd/compile/internal/ir/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,11 @@ func reassigned(name *Name) bool {
if isName(OuterValue(n.X)) {
return true
}
case ORANGE:
n := n.(*RangeStmt)
if isName(n.Key) || isName(n.Value) {
return true
}
case OCLOSURE:
n := n.(*ClosureExpr)
if Any(n.Func, do) {
Expand Down
30 changes: 30 additions & 0 deletions test/fixedbugs/issue59572.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// run

// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

func foo() {
println("foo")
}

func main() {
fn := foo
for _, fn = range list {
fn()
}
}

var list = []func(){
func() {
println("1")
},
func() {
println("2")
},
func() {
println("3")
},
}
3 changes: 3 additions & 0 deletions test/fixedbugs/issue59572.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
2
3

0 comments on commit 89567a3

Please sign in to comment.