]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: implement copy for generic argument types
authorRobert Griesemer <gri@golang.org>
Thu, 7 Oct 2021 02:38:15 +0000 (19:38 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 7 Oct 2021 03:41:40 +0000 (03:41 +0000)
This is a port of CL 354432 from types2 to go/types
with minor adjustments:
- an error message has a different position
- the constraint literals are wrapped in interfaces
  because the interface-free notation has not been
  ported yet

Change-Id: I167094b57b39027566f2b7ce3aa97a071bae4da5
Reviewed-on: https://go-review.googlesource.com/c/go/+/354489
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/go/types/builtins.go
src/go/types/testdata/check/builtins.go2

index d805e46666dab798dd732192a281c789b749a343..3e2c994b090afd6f73c47793ff5abc996c6800ad 100644 (file)
@@ -341,15 +341,13 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                        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, _Todo, "copy on generic operands not yet implemented")
                }
 
                if dst == nil || src == nil {
index 1c773cc70b17e741b1ac71882d3d80f9b977c0ef..fb912a191822089b5ab675165d37448f7b58c7a5 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 interface{~[]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 /* ERROR different element types */ , x3)
+}
+
+func _[T interface{~[]E}, E any](x T, y []E) {
+       copy(x, y)
+       copy(x /* ERROR different element types */ , "foo")
+}
+
+func _[T interface{~string}](x []byte, y T) {
+       copy(x, y)
+       copy(y /* ERROR expects slice arguments */ , x)
+}
+
+func _[T interface{~[]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 }