mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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 <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>
This commit is contained in:
parent
a5fa5ea51c
commit
a67977da5e
3 changed files with 37 additions and 0 deletions
|
|
@ -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--
|
||||
|
|
|
|||
|
|
@ -234,6 +234,7 @@ func TestIntendedInlining(t *testing.T) {
|
|||
"(*B).Loop",
|
||||
},
|
||||
"path": {
|
||||
"Base",
|
||||
"scanChunk",
|
||||
},
|
||||
"path/filepath": {
|
||||
|
|
|
|||
25
test/fixedbugs/issue75278.go
Normal file
25
test/fixedbugs/issue75278.go
Normal file
|
|
@ -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)]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue