aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-10-05 10:29:21 -0700
committerRobert Griesemer <gri@golang.org>2021-10-05 18:39:53 +0000
commit7e69c5decf0b95ce2856769b9798c3a1cf3078ee (patch)
tree41caef4b1622c9a16125b62fd21192697295bf57 /src
parent097a82f54d20cb722b8712025caac44c357c1b13 (diff)
downloadgo-7e69c5decf0b95ce2856769b9798c3a1cf3078ee.tar.xz
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>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/types2/index.go7
-rw-r--r--src/cmd/compile/internal/types2/testdata/check/typeparams.go28
2 files changed, 7 insertions, 8 deletions
diff --git a/src/cmd/compile/internal/types2/index.go b/src/cmd/compile/internal/types2/index.go
index 848a70dea8..47a5e50f62 100644
--- a/src/cmd/compile/internal/types2/index.go
+++ b/src/cmd/compile/internal/types2/index.go
@@ -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 {
diff --git a/src/cmd/compile/internal/types2/testdata/check/typeparams.go2 b/src/cmd/compile/internal/types2/testdata/check/typeparams.go2
index 69b6925b9f..29c25b0bb4 100644
--- a/src/cmd/compile/internal/types2/testdata/check/typeparams.go2
+++ b/src/cmd/compile/internal/types2/testdata/check/typeparams.go2
@@ -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