From: Junwei Zuo Date: Wed, 12 Apr 2023 10:53:51 +0000 (+0800) Subject: [release-branch.go1.20] cmd/compile: fix ir.StaticValue for ORANGE X-Git-Tag: go1.20.4~11 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1dbbac7d796041eac4791fc0caa9315bd50ea964;p=gostls13.git [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 TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky Auto-Submit: Keith Randall Reviewed-by: Keith Randall Reviewed-by: Keith Randall (cherry picked from commit 89567a35c11c343cf765d6fb1270e1250e50d83f) Reviewed-on: https://go-review.googlesource.com/c/go/+/484136 Run-TryBot: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/ir/expr.go b/src/cmd/compile/internal/ir/expr.go index a481b14f8b..e549c08d0c 100644 --- a/src/cmd/compile/internal/ir/expr.go +++ b/src/cmd/compile/internal/ir/expr.go @@ -959,6 +959,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