]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/inline: ignore superfluous slicing
authorJake Bailey <jacob.b.bailey@gmail.com>
Sat, 6 Sep 2025 01:13:03 +0000 (18:13 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 9 Sep 2025 19:10:10 +0000 (12:10 -0700)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Mark Freeman <markfreeman@google.com>
src/cmd/compile/internal/inline/inl.go
src/cmd/compile/internal/test/inl_test.go
test/fixedbugs/issue75278.go [new file with mode: 0644]

index b39710548ebafadd1fd903f117804018c13c9b27..9f03f6404a5f69f4b5d4e4a7e52987e69671e66d 100644 (file)
@@ -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--
index 4e51d7d9d135437971770af174c56d0935067cec..3bea11dbb17b125e26f8fa100167cc69bc1b6eae 100644 (file)
@@ -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 (file)
index 0000000..bcf5dc3
--- /dev/null
@@ -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)]
+}