go/test/copy1.go
Robert Griesemer 2d097e363a 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>
2025-03-10 21:30:51 -07:00

28 lines
878 B
Go

// errorcheck
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Verify that copy arguments requirements are enforced by the
// compiler.
package main
func main() {
si := make([]int, 8)
sf := make([]float64, 8)
_ = copy() // ERROR "not enough arguments"
_ = copy(1, 2, 3) // ERROR "too many arguments"
_ = copy(si, "hi") // ERROR "have different element types(.*int.*string| int and byte)"
_ = copy(si, sf) // ERROR "have different element types.*int.*float64"
_ = copy(1, 2) // ERROR "must be slices; have int, int|argument must be a slice; have 1"
_ = copy(1, si) // ERROR "first argument to copy should be|argument must be a slice; have 1"
_ = copy(si, 2) // ERROR "second argument to copy should be|argument must be a slice; have 2"
}