golang/go

cmd/compile: unnecessary bounds check when slicing after using `slices.Grow`

開放

#73,145 建立於 2025年4月3日

 (2 則留言) (1 個反應) (0 位負責人)Go (19,008 個分叉)batch import

倉庫指標

星標
 (133,883 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

Go version

go version go1.24.1 linux/amd64

Output of go env in your module/workspace:

-

What did you do?

package pkg

import "slices"

func Foo() {
	var xs []int
	xs = slices.Grow(xs, 4)[:len(xs)+4]
}

What did you see happen?

Slicing after growing inserts a check for the capacity and a conditional call to runtime.panicSliceAcap

What did you expect to see?

The compiler should be able to tell that we just guaranteed that the slice's capacity is at least len(xs)+4 and shouldn't insert another check.

貢獻者指南