]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: implement copy for generic argument types
authorRobert Griesemer <gri@golang.org>
Thu, 7 Oct 2021 01:23:06 +0000 (18:23 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 7 Oct 2021 02:30:47 +0000 (02:30 +0000)
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>
src/cmd/compile/internal/types2/builtins.go
src/cmd/compile/internal/types2/testdata/check/builtins.go2

index 3b8d85859a7eb70741f88d72cbd2b947e9e3b344..cb4d93c6c41609a10763803a7817f969c9496395 100644 (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 {
index 0cfea93bf6d500a9a7883ed3baffd04fc702e9e0..243e888ff7c97930900643797551ca94f3a90254 100644 (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 }