Skip to content

Commit 5038ce8

Browse files
committed
cmd/compile: add missing OASOP case in mayModifyPkgVar
CL 395541 made staticopy safe, stop applying the optimization once seeing an expression that may modify global variables. However, it misses the case for OASOP expression, causing the static init mis-recognizes the modification and think it's safe. Fixing this by adding missing OASOP case. Fixes #66585 Change-Id: I603cec018d3b5a09825c14e1f066a0e16f8bde23 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/575216 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Than McIntosh <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 973befe commit 5038ce8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/cmd/compile/internal/staticinit/sched.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,12 @@ func mayModifyPkgVar(n ir.Node) bool {
899899
case ir.OAPPEND, ir.OCLEAR, ir.OCOPY:
900900
return true // could mutate a global array
901901

902+
case ir.OASOP:
903+
n := n.(*ir.AssignOpStmt)
904+
if !safeLHS(n.X) {
905+
return true
906+
}
907+
902908
case ir.OAS:
903909
n := n.(*ir.AssignStmt)
904910
if !safeLHS(n.X) {

test/fixedbugs/issue66585.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run
2+
3+
// Copyright 2024 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+
var x = 0
10+
var a = foo()
11+
var b = x
12+
13+
func foo() int {
14+
x++
15+
return x
16+
}
17+
18+
func main() {
19+
if a != 1 {
20+
panic("unexpected a value")
21+
}
22+
if b != 1 {
23+
panic("unexpected b value")
24+
}
25+
}

0 commit comments

Comments
 (0)