aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunwei Zuo <zuojunwei.2023@bytedance.com>2023-04-12 18:53:51 +0800
committerMatthew Dempsky <mdempsky@google.com>2023-04-12 20:25:00 +0000
commitabb86e6e82ec67f7b7cecd8c6225466b25ca2814 (patch)
tree5586e3612199dd77ab08d04a25b799a4c4b73d75
parent22c1d18a276a6edadfb140ad7013ed74b7ed792b (diff)
downloadgo-abb86e6e82ec67f7b7cecd8c6225466b25ca2814.tar.gz
go-abb86e6e82ec67f7b7cecd8c6225466b25ca2814.zip
[release-branch.go1.19] cmd/compile: fix ir.StaticValue for ORANGE
Range statement will mutate the key and value, so we should treat them as reassigned. Fixes #59579 Change-Id: I9c6b67d938760a0c6a1d9739f2737c67af4a3a10 Reviewed-on: https://go-review.googlesource.com/c/go/+/483855 Run-TryBot: Wayne Zuo <wdvxdr@golangcn.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> (cherry picked from commit 89567a35c11c343cf765d6fb1270e1250e50d83f) Reviewed-on: https://go-review.googlesource.com/c/go/+/484135 Run-TryBot: Matthew Dempsky <mdempsky@google.com>
-rw-r--r--src/cmd/compile/internal/ir/expr.go5
-rw-r--r--test/fixedbugs/issue59572.go30
-rw-r--r--test/fixedbugs/issue59572.out3
3 files changed, 38 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/ir/expr.go b/src/cmd/compile/internal/ir/expr.go
index 8ac7e7f4f7..e945cc5655 100644
--- a/src/cmd/compile/internal/ir/expr.go
+++ b/src/cmd/compile/internal/ir/expr.go
@@ -912,6 +912,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) {
diff --git a/test/fixedbugs/issue59572.go b/test/fixedbugs/issue59572.go
new file mode 100644
index 0000000000..a16817aec0
--- /dev/null
+++ b/test/fixedbugs/issue59572.go
@@ -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")
+ },
+}
diff --git a/test/fixedbugs/issue59572.out b/test/fixedbugs/issue59572.out
new file mode 100644
index 0000000000..01e79c32a8
--- /dev/null
+++ b/test/fixedbugs/issue59572.out
@@ -0,0 +1,3 @@
+1
+2
+3