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

Closed
dominikh opened this issue May 11, 2022 · 2 comments
Closed
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dominikh
Copy link
Member

https://tip.golang.org/ref/spec#Slice_expressions states that

The core type of a must be a string, array, pointer to array, or slice.

https://tip.golang.org/ref/spec#Appending_and_copying_slices 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

@findleyr
Copy link
Contributor

CC @griesemer

@griesemer griesemer self-assigned this May 11, 2022
@griesemer griesemer added the NeedsFix The path to resolution is known, but the work has not been done. label May 11, 2022
@griesemer griesemer added this to the Go1.19 milestone May 11, 2022
@gopherbot
Copy link

Change https://go.dev/cl/412234 mentions this issue: spec: document operations which accept []byte|string constrained types

jproberts pushed a commit to jproberts/go that referenced this issue Aug 10, 2022
Pre-1.18, as special cases, the built-in operations append and copy
accepted strings as second arguments if the first argument was a byte
slice. With Go 1.18, these two built-ins as well as slice expressions
rely on the notion of core types in their specification.

Because we want to permit slice expressions, append, and copy to
operate on (1st or 2nd operands) that are type parameters restricted
by []byte | string (and variations thereof), the simple notion of
core type is not sufficient for these three operations. (The compiler
already permits such more relaxed operations).

In the section on core types, add a paragraph and examples introducing
the (artificial) core type "bypestring", which describes the core type
of type sets whose underlying types are []byte or string. Adjust the
rules for slice expressions, append, and copy accordingly.

Also (unrelated): Adjust prose in the only paragraph where we used
personal speech ("we") to impersonal speech, to match the rest of
the spec.

Fixes golang#52859.

Change-Id: I1cbda3095a1136fb99334cc3a62a9a349a27ce1e
Reviewed-on: https://go-review.googlesource.com/c/go/+/412234
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
@golang golang locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

4 participants