Skip to content

Commit 2493072

Browse files
committed
cmd/compile: avoid assignment conversion in append(a, b...)
There's no need for a and b to match types. The typechecker already ensured that a and b are both slices with the same base type, or a and b are (possibly named) []byte and string. The optimization to treat append(b, make([], ...)) as a zeroing slice extension doesn't fire when there's a OCONVNOP wrapping the make. Fixes #53888 Change-Id: Ied871ed0bbb8e4a4b35d280c71acbab8103691bc Reviewed-on: https://go-review.googlesource.com/c/go/+/418475 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]>
1 parent c3833a5 commit 2493072

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

src/cmd/compile/internal/noder/transform.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,11 @@ func transformAppend(n *ir.CallExpr) ir.Node {
730730
assert(t.IsSlice())
731731

732732
if n.IsDDD {
733-
if t.Elem().IsKind(types.TUINT8) && args[1].Type().IsString() {
734-
return n
735-
}
736-
737-
args[1] = assignconvfn(args[1], t.Underlying())
733+
// assignconvfn is of args[1] not required here, as the
734+
// types of args[0] and args[1] don't need to match
735+
// (They will both have an underlying type which are
736+
// slices of indentical base types, or be []byte and string.)
737+
// See issue 53888.
738738
return n
739739
}
740740

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !race
6+
7+
package test
8+
9+
import (
10+
"testing"
11+
)
12+
13+
func TestAppendOfMake(t *testing.T) {
14+
for n := 32; n < 33; n++ { // avoid stack allocation of make()
15+
b := make([]byte, n)
16+
f := func() {
17+
b = append(b[:0], make([]byte, n)...)
18+
}
19+
if n := testing.AllocsPerRun(10, f); n > 0 {
20+
t.Errorf("got %f allocs, want 0", n)
21+
}
22+
type S []byte
23+
24+
s := make(S, n)
25+
g := func() {
26+
s = append(s[:0], make(S, n)...)
27+
}
28+
if n := testing.AllocsPerRun(10, g); n > 0 {
29+
t.Errorf("got %f allocs, want 0", n)
30+
}
31+
h := func() {
32+
s = append(s[:0], make([]byte, n)...)
33+
}
34+
if n := testing.AllocsPerRun(10, h); n > 0 {
35+
t.Errorf("got %f allocs, want 0", n)
36+
}
37+
i := func() {
38+
b = append(b[:0], make(S, n)...)
39+
}
40+
if n := testing.AllocsPerRun(10, i); n > 0 {
41+
t.Errorf("got %f allocs, want 0", n)
42+
}
43+
}
44+
}

src/cmd/compile/internal/typecheck/func.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ func tcAppend(n *ir.CallExpr) ir.Node {
465465
return n
466466
}
467467

468-
args[1] = AssignConv(args[1], t.Underlying(), "append")
468+
// AssignConv is of args[1] not required here, as the
469+
// types of args[0] and args[1] don't need to match
470+
// (They will both have an underlying type which are
471+
// slices of indentical base types, or be []byte and string.)
472+
// See issue 53888.
469473
return n
470474
}
471475

src/runtime/slice.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func growslice(et *_type, old slice, cap int) slice {
194194
}
195195

196196
if cap < old.cap {
197-
panic(errorString("growslice: cap out of range"))
197+
panic(errorString("growslice: len out of range"))
198198
}
199199

200200
if et.size == 0 {
@@ -284,7 +284,7 @@ func growslice(et *_type, old slice, cap int) slice {
284284
// print(len(s), "\n")
285285
// }
286286
if overflow || capmem > maxAlloc {
287-
panic(errorString("growslice: cap out of range"))
287+
panic(errorString("growslice: len out of range"))
288288
}
289289

290290
var p unsafe.Pointer

test/fixedbugs/issue29190.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ const maxInt = int(^uint(0) >> 1)
1616

1717
func main() {
1818
s := make([]T, maxInt)
19-
shouldPanic("cap out of range", func() { s = append(s, T{}) })
19+
shouldPanic("len out of range", func() { s = append(s, T{}) })
2020
var oneElem = make([]T, 1)
21-
shouldPanic("cap out of range", func() { s = append(s, oneElem...) })
21+
shouldPanic("len out of range", func() { s = append(s, oneElem...) })
2222
}
2323

2424
func shouldPanic(str string, f func()) {

0 commit comments

Comments
 (0)