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

josharian opened this issue Feb 1, 2020 · 4 comments


Copy link

@josharian josharian commented Feb 1, 2020

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
Copy link

@networkimprov networkimprov commented Feb 1, 2020

Also needs fix for rune. Playground said:

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

@josharian josharian commented Feb 2, 2020

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

Copy link

@dotaheor dotaheor commented Feb 2, 2020

dup of #23536?

Copy link
Contributor Author

@josharian josharian commented Feb 2, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.