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

cmd/compile: should accept converting slices of named byte types to strings #36965

Closed
josharian opened this issue Feb 1, 2020 · 4 comments
Closed
Milestone

Comments

@josharian
Copy link
Contributor

package p

type B byte

func f() {
	var x []B
	_ = string(x)
}

go/types accepts this code. Go 1.12, 1.13, and 1.14beta1 all reject it with:

x.go:7:12: cannot use x (type []B) as type []byte in argument to runtime.slicebytetostring

Noticed while looking at #36890.

Easy fix, will plan to do for 1.15.

@josharian josharian added this to the Go1.15 milestone Feb 1, 2020
@networkimprov
Copy link

Also needs fix for rune. Playground said:

cannot use r (type []R) as type []rune in argument to runtime.slicerunetostring

@josharian
Copy link
Contributor Author

Actually, go/types may be wrong here. From the spec, on conversions:

A non-constant value x can be converted to type T in any of these cases:
...
x is an integer or a slice of bytes or runes and T is a string type.
x is a string and T is a slice of bytes or runes.

It doesn’t say that T is a slice whose elements have underlying type byte or rune.

cc @griesemer for spec clarification

cc @ianlancetaylor for gccgo

@dotaheor
Copy link

dotaheor commented Feb 2, 2020

dup of #23536?

@josharian
Copy link
Contributor Author

Yes, it appears so. Unfortunately it doesn’t appear that that issue was brought to a conclusion.

@golang golang locked and limited conversation to collaborators Feb 1, 2021
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