aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2025-03-06 15:44:01 -0800
committerGopher Robot <gobot@golang.org>2025-03-10 21:30:51 -0700
commit2d097e363a6fce725802ecbde6d0d1b90f45290d (patch)
treef3ad51c33c79341df181d296655eab51880041bf /src/internal
parente3ea8e68fb91bdc510cb7702981609ce5a9da12e (diff)
downloadgo-2d097e363a6fce725802ecbde6d0d1b90f45290d.tar.xz
go/types, types2: better error messages for copy built-in
Rather than relying on coreString, use the new commonUnder function to determine the argument slice element types. Factor out this functionality, which is shared for append and copy, into a new helper function sliceElem (similar to chanElem). Use sliceElem for both the append and copy implementation. As a result, the error messages for invalid copy calls are now more detailed. While at it, handle the special cases for append and copy first because they don't need the slice element computation. Finally, share the same type recording code for the special and general cases. As an aside, in commonUnder, be clearer in the code that the result is either a nil type and an error, or a non-nil type and a nil error. This matches in style what we do in sliceElem. Change-Id: I318bafc0d2d31df04f33b1b464ad50d581918671 Reviewed-on: https://go-review.googlesource.com/c/go/+/655675 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/types/testdata/check/builtins0.go12
-rw-r--r--src/internal/types/testdata/check/builtins1.go11
-rw-r--r--src/internal/types/testdata/fixedbugs/issue49735.go6
3 files changed, 15 insertions, 14 deletions
diff --git a/src/internal/types/testdata/check/builtins0.go b/src/internal/types/testdata/check/builtins0.go
index 62759d1e9c..ea30fbcbe7 100644
--- a/src/internal/types/testdata/check/builtins0.go
+++ b/src/internal/types/testdata/check/builtins0.go
@@ -260,9 +260,9 @@ func complex2() {
func copy1() {
copy() // ERROR "not enough arguments"
copy("foo") // ERROR "not enough arguments"
- copy([ /* ERROR "copy expects slice arguments" */ ...]int{}, []int{})
- copy([ /* ERROR "copy expects slice arguments" */ ]int{}, [...]int{})
- copy([ /* ERROR "different element types" */ ]int8{}, "foo")
+ copy([ /* ERROR "invalid copy: argument must be a slice; have [...]int{} (value of type [0]int)" */ ...]int{}, []int{})
+ copy([]int{}, [ /* ERROR "invalid copy: argument must be a slice; have [...]int{} (value of type [0]int)" */ ...]int{})
+ copy([ /* ERROR "invalid copy: arguments []int8{} (value of type []int8) and \"foo\" (untyped string constant) have different element types int8 and byte" */ ]int8{}, "foo")
// spec examples
var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
@@ -275,9 +275,9 @@ func copy1() {
var t [][]int
copy(t, t)
- copy(t /* ERROR "copy expects slice arguments" */ , nil)
- copy(nil /* ERROR "copy expects slice arguments" */ , t)
- copy(nil /* ERROR "copy expects slice arguments" */ , nil)
+ copy(t, nil /* ERROR "invalid copy: argument must be a slice; have untyped nil" */ )
+ copy(nil /* ERROR "invalid copy: argument must be a slice; have untyped nil" */ , t)
+ copy(nil /* ERROR "invalid copy: argument must be a slice; have untyped nil" */ , nil)
copy(t... /* ERROR "invalid use of ..." */ )
}
diff --git a/src/internal/types/testdata/check/builtins1.go b/src/internal/types/testdata/check/builtins1.go
index 498dd4b463..25610c1379 100644
--- a/src/internal/types/testdata/check/builtins1.go
+++ b/src/internal/types/testdata/check/builtins1.go
@@ -62,13 +62,13 @@ func _[T C5[X], X any](ch T) {
// copy
func _[T any](x, y T) {
- copy(x /* ERROR "copy expects slice arguments" */ , y)
+ copy(x /* ERROR "invalid copy: argument must be a slice; have x (variable of type T constrained by any)" */ , y)
}
func _[T ~[]byte](x, y T) {
copy(x, y)
copy(x, "foo")
- copy("foo" /* ERROR "expects slice arguments" */ , y)
+ copy("foo" /* ERROR "argument must be a slice; have \"foo\" (untyped string constant)" */ , y)
var x2 []byte
copy(x2, y) // element types are identical
@@ -82,16 +82,17 @@ func _[T ~[]byte](x, y T) {
func _[T ~[]E, E any](x T, y []E) {
copy(x, y)
- copy(x /* ERROR "different element types" */ , "foo")
+ copy(x /* ERROR "arguments x (variable of type T constrained by ~[]E) and \"foo\" (untyped string constant) have different element types E and byte" */ , "foo")
}
func _[T ~string](x []byte, y T) {
copy(x, y)
- copy(y /* ERROR "expects slice arguments" */ , x)
+ copy([ /* ERROR "arguments []int{} (value of type []int) and y (variable of type T constrained by ~string) have different element types int and byte" */ ]int{}, y)
+ copy(y /* ERROR "argument must be a slice; have y (variable of type T constrained by ~string)" */ , x)
}
func _[T ~[]byte|~string](x T, y []byte) {
- copy(x /* ERROR "expects slice arguments" */ , y)
+ copy(x /* ERROR "argument must be a slice; have x (variable of type T constrained by ~[]byte | ~string)" */ , y)
copy(y, x)
}
diff --git a/src/internal/types/testdata/fixedbugs/issue49735.go b/src/internal/types/testdata/fixedbugs/issue49735.go
index 07603b712c..b719e1353f 100644
--- a/src/internal/types/testdata/fixedbugs/issue49735.go
+++ b/src/internal/types/testdata/fixedbugs/issue49735.go
@@ -5,8 +5,8 @@
package p
func _[P1 any, P2 ~byte, P3 []int | []byte](s1 P1, s2 P2, s3 P3) {
- _ = append(nil /* ERROR "invalid append: first argument must be a slice; have untyped nil" */, 0)
- _ = append(s1 /* ERROR "invalid append: first argument must be a slice; have s1 (variable of type P1 constrained by any)" */, 0)
- _ = append(s2 /* ERROR "invalid append: first argument must be a slice; have s2 (variable of type P2 constrained by ~byte)" */, 0)
+ _ = append(nil /* ERROR "invalid append: argument must be a slice; have untyped nil" */, 0)
+ _ = append(s1 /* ERROR "invalid append: argument must be a slice; have s1 (variable of type P1 constrained by any)" */, 0)
+ _ = append(s2 /* ERROR "invalid append: argument must be a slice; have s2 (variable of type P2 constrained by ~byte)" */, 0)
_ = append(s3 /* ERROR "invalid append: mismatched slice element types int and byte in s3 (variable of type P3 constrained by []int | []byte)" */, 0)
}