1
0
mirror of https://github.com/golang/go synced 2024-09-29 21:34:28 -06:00

cmd/compile/internal/types2: implement copy for generic argument types

For now, the underlying types of the the argument types' constraints
must be a single type that is a slice (the source operand may also
be a string).

Change-Id: I9e705e3349c9242f126b9c3e0af65e9ffb25fe6e
Reviewed-on: https://go-review.googlesource.com/c/go/+/354432
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Robert Griesemer 2021-10-06 18:23:06 -07:00
parent 812a33dc7d
commit 39bbf08e71
2 changed files with 38 additions and 3 deletions

View File

@ -336,15 +336,13 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
return
}
var src Type
switch t := under(y.typ).(type) {
switch t := optype(y.typ).(type) {
case *Basic:
if isString(y.typ) {
src = universeByte
}
case *Slice:
src = t.elem
case *TypeParam:
check.error(x, "copy on generic operands not yet implemented")
}
if dst == nil || src == nil {

View File

@ -45,6 +45,43 @@ func _[T C5[X], X any](ch T) {
close(ch)
}
// copy
func _[T any](x, y T) {
copy(x /* ERROR copy expects slice arguments */ , y)
}
func _[T ~[]byte](x, y T) {
copy(x, y)
copy(x, "foo")
copy("foo" /* ERROR expects slice arguments */ , y)
var x2 []byte
copy(x2, y) // element types are identical
copy(y, x2) // element types are identical
type myByte byte
var x3 []myByte
copy(x3 /* ERROR different element types */ , y)
copy(y, x3 /* ERROR different element types */ )
}
func _[T ~[]E, E any](x T, y []E) {
copy(x, y)
copy(x /* ERROR different element types */ , "foo")
}
func _[T ~string](x []byte, y T) {
copy(x, y)
copy(y /* ERROR expects slice arguments */ , x)
}
func _[T ~[]byte|~string](x T, y []byte) {
copy(x /* ERROR expects slice arguments */ , y)
// TODO(gri) should this be valid?
copy(y /* ERROR expects slice arguments */ , x)
}
// delete
type M0 interface{ int }