Skip to content

Commit 67e0681

Browse files
randall77gopherbot
authored andcommitted
cmd/compile: put constant value on node inside parentheses
That's where the unified IR writer expects it. Fixes #73476 Change-Id: Ic22bd8dee5be5991e6d126ae3f6eccb2acdc0b19 Reviewed-on: https://go-review.googlesource.com/c/go/+/667415 Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 3672a09 commit 67e0681

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/cmd/compile/internal/types2/range.go

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ func (check *Checker) rangeStmt(inner stmtContext, rangeStmt *syntax.ForStmt, no
3737

3838
if isTypes2 && x.mode != invalid && sValue == nil && !check.hasCallOrRecv {
3939
if t, ok := arrayPtrDeref(under(x.typ)).(*Array); ok {
40+
for {
41+
// Put constant info on the thing inside parentheses.
42+
// That's where (*../noder/writer).expr expects it.
43+
// See issue 73476.
44+
p, ok := rangeVar.(*syntax.ParenExpr)
45+
if !ok {
46+
break
47+
}
48+
rangeVar = p.X
49+
}
4050
// Override type of rangeVar to be a constant
4151
// (and thus side-effects will not be computed
4252
// by the backend).

src/go/types/range.go

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue73476.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run
2+
3+
// Copyright 2025 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+
//go:noinline
10+
func f(p *[4]int) {
11+
for i := range (*p) { // Note the parentheses! gofmt wants to remove them - don't let it!
12+
println(i)
13+
}
14+
}
15+
func main() {
16+
f(nil)
17+
}

test/fixedbugs/issue73476.out

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

0 commit comments

Comments
 (0)