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

spec: several sections require core types and don't mention []byte | string #52859

dominikh opened this issue May 11, 2022 · 1 comment


Copy link

@dominikh dominikh commented May 11, 2022 states that

The core type of a must be a string, array, pointer to array, or slice. states for append that

The core type of s must be a slice of type []E. The values x are passed to a parameter of type ...E and the respective parameter passing rules apply. As a special case, if the core type of s is []byte, append also accepts a second argument with core type string followed by ....

and for copy that

The core types of both arguments must be slices with identical element type. [...] As a special case, if the destination's core type is []byte, copy also accepts a source argument with core type string.

All three passages require that the types have core types. append and copy do allow for two different core types (string and []byte) for the two arguments, but they don't allow []byte | string for the second argument. The compiler, however, special-cases []byte | string in all these places, as can be seen in the following function, which compiles:

func foo[T []byte | string](x T) {
	var dst []byte
	dst = append(dst, x...)
	_ = x[0:1]
	copy(dst, x)

/cc @findleyr

Copy link

@findleyr findleyr commented May 11, 2022

@griesemer griesemer self-assigned this May 11, 2022
@griesemer griesemer added the NeedsFix label May 11, 2022
@griesemer griesemer added this to the Go1.19 milestone May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants