Skip to content
Snippets Groups Projects
Commit 3452d80d authored by Keith Randall's avatar Keith Randall Committed by Gopher Robot
Browse files

cmd/compile: add cast in range loop final value computation

When replacing a loop where the iteration variable has a named type,
we need to compute the last iteration value as i = T(len(a)-1), not
just i = len(a)-1.

Fixes #73491

Change-Id: Ic1cc3bdf8571a40c10060f929a9db8a888de2b70
Reviewed-on: https://go-review.googlesource.com/c/go/+/667815


Reviewed-by: default avatarCuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@google.com>
Reviewed-by: default avatarJunyang Shao <shaojunyang@google.com>
Reviewed-by: default avatarKeith Randall <khr@google.com>
parent 3009566a
No related branches found
No related tags found
No related merge requests found
...@@ -605,7 +605,7 @@ func arrayClear(wbPos src.XPos, a ir.Node, nrange *ir.RangeStmt) ir.Node { ...@@ -605,7 +605,7 @@ func arrayClear(wbPos src.XPos, a ir.Node, nrange *ir.RangeStmt) ir.Node {
// For array range clear, also set "i = len(a) - 1" // For array range clear, also set "i = len(a) - 1"
if nrange != nil { if nrange != nil {
idx := ir.NewAssignStmt(base.Pos, nrange.Key, ir.NewBinaryExpr(base.Pos, ir.OSUB, ir.NewUnaryExpr(base.Pos, ir.OLEN, a), ir.NewInt(base.Pos, 1))) idx := ir.NewAssignStmt(base.Pos, nrange.Key, typecheck.Conv(ir.NewBinaryExpr(base.Pos, ir.OSUB, ir.NewUnaryExpr(base.Pos, ir.OLEN, a), ir.NewInt(base.Pos, 1)), nrange.Key.Type()))
n.Body.Append(idx) n.Body.Append(idx)
} }
......
// build
// 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
type T int
const K T = 5
type P struct {
a [K]*byte
}
//go:noinline
func f(p *P) {
for i := range K {
p.a[i] = nil
}
}
func main() {
f(nil)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment