Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help people debug this error: "arguments to copy must be slices or array pointers" #539

Closed
jackpal opened this issue Jan 17, 2010 · 4 comments

Comments

@jackpal
Copy link
Contributor

jackpal commented Jan 17, 2010

When the built-in copy() operation is misused, users get this error:

bitset.go:27: arguments to copy must be slices or array pointers

However, this can be hard to debug because the user may not understand the 
exact types of the two arguments to copy.

Please expand this error message to say something like:

argument 1 is invalid because it is a []byte which is not a slice or an array 
pointer.
@peterGo
Copy link
Contributor

peterGo commented Jan 17, 2010

Comment 1:

The copy built-in function is explained in detail, with examples, in §Copying slices,
The Go Programming Language Specification.
http://golang.org/doc/go_spec.html#Copying_slices
One of the rules given in the specification is: "The built-in function copy copies
array or slice elements from a source src to a destination dst ..." The error message
"arguments to copy must be slices or array pointers" indicates that your code broke
this rule. You did not provide an example of your failing code.
[]byte is a slice type. Therefore, your proposed error messsage, "argument 1 is
invalid because it is a []byte which is not a slice or an array pointer," after
substitution, would read, "argument 1 is invalid because it is a slice[sic] which is
not a slice or an array pointer."

@rsc
Copy link
Contributor

rsc commented Jan 17, 2010

Comment 2:

Labels changed: added compilerbug.

Owner changed to r...@golang.org.

Status changed to Accepted.

@jackpal
Copy link
Contributor Author

jackpal commented Jan 18, 2010

Comment 3:

Sorry for not providing examples. Here is one:
package main
func main() {
    var a [4]byte
    var b [4]byte
    copy(a,b) // fails
    copy(&a,&b) // works
}
It may be obvious to a go expert that an array is not a slice or an array pointer, but
it 
is not necessarily obvious to an inexperienced go developer. The language does so 
much to hide the difference between pointer and reference and array that the 
inexperienced developer may not realize what to do to solve the problem.
That's why a hint from the compiler as to which arguments are causing the problem 
and what the incorrect type is would help the developer correct their code.

@rsc
Copy link
Contributor

rsc commented Jan 19, 2010

Comment 4:

This issue was closed by revision 07fc145.

Status changed to Fixed.

Merged into issue #-.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants