diff options
| author | Robert Griesemer <gri@golang.org> | 2021-10-05 11:45:22 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2021-10-05 19:28:36 +0000 |
| commit | 7ae83c8f38bc79e999a8657da443d815036c7d72 (patch) | |
| tree | 537edbebf2ecbf4a4e4994f495292b7da422c759 /src | |
| parent | c1a0aa300a1003ab823d5633e521e064a063271c (diff) | |
| download | go-7ae83c8f38bc79e999a8657da443d815036c7d72.tar.xz | |
go/types: implement generic slice expressions
This is a clean port of CL 354070 from types2 to go/types.
Change-Id: I44de1b8e6c0177e2a33e7f36a82465dc520c35aa
Reviewed-on: https://go-review.googlesource.com/c/go/+/354092
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/go/types/index.go | 7 | ||||
| -rw-r--r-- | src/go/types/testdata/check/typeparams.go2 | 8 |
2 files changed, 7 insertions, 8 deletions
diff --git a/src/go/types/index.go b/src/go/types/index.go index ca04072f7a..613f6292e4 100644 --- a/src/go/types/index.go +++ b/src/go/types/index.go @@ -207,7 +207,7 @@ func (check *Checker) sliceExpr(x *operand, e *ast.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.Slice3 { @@ -246,11 +246,6 @@ func (check *Checker) sliceExpr(x *operand, e *ast.SliceExpr) { case *Slice: valid = true // x.typ doesn't change - - case *TypeParam: - check.errorf(x, _Todo, "generic slice expressions not yet implemented") - x.mode = invalid - return } if !valid { diff --git a/src/go/types/testdata/check/typeparams.go2 b/src/go/types/testdata/check/typeparams.go2 index bfacb3e1e7..10f84314fd 100644 --- a/src/go/types/testdata/check/typeparams.go2 +++ b/src/go/types/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 |
