diff --git a/src/cmd/compile/internal/types2/range.go b/src/cmd/compile/internal/types2/range.go index ecda53d14bad45f768d5de715fc3fc05db6e63e1..dc0d81d05b7bec11bb0d55866e14b436ed0d672f 100644 --- a/src/cmd/compile/internal/types2/range.go +++ b/src/cmd/compile/internal/types2/range.go @@ -37,6 +37,16 @@ func (check *Checker) rangeStmt(inner stmtContext, rangeStmt *syntax.ForStmt, no if isTypes2 && x.mode != invalid && sValue == nil && !check.hasCallOrRecv { if t, ok := arrayPtrDeref(under(x.typ)).(*Array); ok { + for { + // Put constant info on the thing inside parentheses. + // That's where (*../noder/writer).expr expects it. + // See issue 73476. + p, ok := rangeVar.(*syntax.ParenExpr) + if !ok { + break + } + rangeVar = p.X + } // Override type of rangeVar to be a constant // (and thus side-effects will not be computed // by the backend). diff --git a/src/go/types/range.go b/src/go/types/range.go index 91149c1426fe8c59663131f1290b2553664a8dac..ed7d83283c5fb6ca2aa7395c181801642bb2792a 100644 --- a/src/go/types/range.go +++ b/src/go/types/range.go @@ -40,6 +40,16 @@ func (check *Checker) rangeStmt(inner stmtContext, rangeStmt *ast.RangeStmt, noN if isTypes2 && x.mode != invalid && sValue == nil && !check.hasCallOrRecv { if t, ok := arrayPtrDeref(under(x.typ)).(*Array); ok { + for { + // Put constant info on the thing inside parentheses. + // That's where (*../noder/writer).expr expects it. + // See issue 73476. + p, ok := rangeVar.(*ast.ParenExpr) + if !ok { + break + } + rangeVar = p.X + } // Override type of rangeVar to be a constant // (and thus side-effects will not be computed // by the backend). diff --git a/test/fixedbugs/issue73476.go b/test/fixedbugs/issue73476.go new file mode 100644 index 0000000000000000000000000000000000000000..cc54d9c1e4498cc8e0c1739dc204d23f386ae70d --- /dev/null +++ b/test/fixedbugs/issue73476.go @@ -0,0 +1,17 @@ +// run + +// Copyright 2025 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 + +//go:noinline +func f(p *[4]int) { + for i := range (*p) { // Note the parentheses! gofmt wants to remove them - don't let it! + println(i) + } +} +func main() { + f(nil) +} diff --git a/test/fixedbugs/issue73476.out b/test/fixedbugs/issue73476.out new file mode 100644 index 0000000000000000000000000000000000000000..bc856dafab0941942ccea4202bfa3a5b02bf4371 --- /dev/null +++ b/test/fixedbugs/issue73476.out @@ -0,0 +1,4 @@ +0 +1 +2 +3