From: Jake Bailey Date: Sat, 6 Sep 2025 01:13:03 +0000 (-0700) Subject: cmd/compile/internal/inline: ignore superfluous slicing X-Git-Tag: go1.26rc1~931 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a67977da5e;p=gostls13.git cmd/compile/internal/inline: ignore superfluous slicing When slicing, ignore expressions which could be elided, as in slicing starting at 0 or ending at len(v). Fixes #75278 Change-Id: I9c18e29c3d4da9bef89bd25bb261d3cb60e66392 Reviewed-on: https://go-review.googlesource.com/c/go/+/701216 LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Reviewed-by: Keith Randall Auto-Submit: Keith Randall Reviewed-by: Mark Freeman --- diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index b39710548e..9f03f6404a 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -738,6 +738,17 @@ opSwitch: if n.X.Op() == ir.OINDEX && isIndexingCoverageCounter(n.X) { return false } + + case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR: + n := n.(*ir.SliceExpr) + + // Ignore superfluous slicing. + if n.Low != nil && n.Low.Op() == ir.OLITERAL && ir.Int64Val(n.Low) == 0 { + v.budget++ + } + if n.High != nil && n.High.Op() == ir.OLEN && n.High.(*ir.UnaryExpr).X == n.X { + v.budget += 2 + } } v.budget-- diff --git a/src/cmd/compile/internal/test/inl_test.go b/src/cmd/compile/internal/test/inl_test.go index 4e51d7d9d1..3bea11dbb1 100644 --- a/src/cmd/compile/internal/test/inl_test.go +++ b/src/cmd/compile/internal/test/inl_test.go @@ -234,6 +234,7 @@ func TestIntendedInlining(t *testing.T) { "(*B).Loop", }, "path": { + "Base", "scanChunk", }, "path/filepath": { diff --git a/test/fixedbugs/issue75278.go b/test/fixedbugs/issue75278.go new file mode 100644 index 0000000000..bcf5dc3076 --- /dev/null +++ b/test/fixedbugs/issue75278.go @@ -0,0 +1,25 @@ +// errorcheck -0 -m=2 + +// 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 p + +var a, b []int + +func NoIndices() { // ERROR "can inline NoIndices with cost 4 as:.*" + b = a[:] +} + +func LowIndex() { // ERROR "can inline LowIndex with cost 4 as:.*" + b = a[0:] +} + +func HighIndex() { // ERROR "can inline HighIndex with cost 4 as:.*" + b = a[:len(a)] +} + +func BothIndices() { // ERROR "can inline BothIndices with cost 4 as:.*" + b = a[0:len(a)] +}