1
0
mirror of https://github.com/golang/go synced 2024-11-24 13:20:15 -07:00

cmd/compile/internal/types2: implement generic slice expressions

For now, the constraint's underlying type set must be a single
type that is sliceable.

Change-Id: I08b6a2e88fe35e8238a95b3f40dc969689021a0f
Reviewed-on: https://go-review.googlesource.com/c/go/+/354070
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Robert Griesemer 2021-10-05 10:29:21 -07:00
parent 097a82f54d
commit 7e69c5decf
2 changed files with 7 additions and 8 deletions

View File

@ -207,7 +207,7 @@ func (check *Checker) sliceExpr(x *operand, e *syntax.SliceExpr) {
valid := false
length := int64(-1) // valid if >= 0
switch typ := under(x.typ).(type) {
switch typ := optype(x.typ).(type) {
case *Basic:
if isString(typ) {
if e.Full {
@ -246,11 +246,6 @@ func (check *Checker) sliceExpr(x *operand, e *syntax.SliceExpr) {
case *Slice:
valid = true
// x.typ doesn't change
case *TypeParam:
check.error(x, "generic slice expressions not yet implemented")
x.mode = invalid
return
}
if !valid {

View File

@ -115,9 +115,13 @@ func _[T interface{ [10]byte | string }](x T, i int) { _ = x[i]; _ = x[9]; _ = x
func _[T interface{ [10]int | *[20]int | []int }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR out of bounds */ ] }
// slicing
// TODO(gri) implement this
func _[T interface{ ~string }] (x T, i, j, k int) { _ = x /* ERROR generic slice expressions not yet implemented */ [i:j:k] }
func _[T interface{ ~[10]E }, E any] (x T, i, j, k int) { var _ []E = x[i:j] }
func _[T interface{ ~[10]E }, E any] (x T, i, j, k int) { var _ []E = x[i:j:k] }
func _[T interface{ ~[]byte }] (x T, i, j, k int) { var _ T = x[i:j] }
func _[T interface{ ~[]byte }] (x T, i, j, k int) { var _ T = x[i:j:k] }
func _[T interface{ ~string }] (x T, i, j, k int) { var _ T = x[i:j] }
func _[T interface{ ~string }] (x T, i, j, k int) { var _ T = x /* ERROR 3-index slice of string */ [i:j:k] }
// len/cap built-ins