From 3452d80da3cf4f08da0f5905b1aa19cec475936e Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Thu, 24 Apr 2025 08:38:56 -0700 Subject: 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: Cuong Manh Le LUCI-TryBot-Result: Go LUCI Auto-Submit: Keith Randall Reviewed-by: Junyang Shao Reviewed-by: Keith Randall --- src/cmd/compile/internal/walk/range.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/cmd/compile/internal/walk/range.go b/src/cmd/compile/internal/walk/range.go index a1e5442a69..3d3547b84b 100644 --- a/src/cmd/compile/internal/walk/range.go +++ b/src/cmd/compile/internal/walk/range.go @@ -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" 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) } -- cgit v1.3-5-g9baa