-
Notifications
You must be signed in to change notification settings - Fork 18k
go/types: incorrect behavior for string conversions of byte slices #23536
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
Comments
And this is more weird (non-consistent in spec itself).
.
Or this is a compile bug? [edit]
If the first definition is adopted, then |
You say there is an inconsistency, but then you cite a section of the spec on explicit type conversions, and a section of the spec on the Closing because I see no bug here. I strongly encourage you to discuss these cases on golang-nuts before opening issues for them. Thanks. |
It is not an issue about conversions. It is about the definition of "slice of bytes". Different official Go docs use different definitions. |
I see, I think. |
It looks gccgo thinks a byte slice is a slice whose element underlying type is |
There should be no difference at all between gccgo and gc regarding byte slices. I don't see any mention of gccgo above; can you expand on what you mean? |
I think @dotaheor is on to something. Here's a slightly modified example based on the example above:
go/types accepts this code w/o complaints. But gc complains with:
So at the very least we have an inconsistency. Furthermore, if gc is correct (which I need to investigate), the error message is not very good: There's no mention of runtime in this code, so the error message shouldn't mention it either. |
More cases:
go/types accepts all of them. gc complains twice:
It looks to me that the spec is pretty clear with specific examples (https://tip.golang.org/ref/spec#Conversions). It does appear that gc is correct: A "slice of bytes" means any type whose underlying type is |
I filed separate #23813 for a better gc error message. |
gccgo seems to accept this code without error. |
@ianlancetaylor The spec's examples (see 2. in https://tip.golang.org/ref/spec#Conversions) only shows examples with types with underlying type |
Maybe not bad, the current gccgo implementation provides an inefficient way to convert values between
|
@dotaheor So does go/types. However, the spec does not explicitly permit this, it appears. |
It is interesting that both gc and gccgo reflections think
|
Interesting. The code is actually accepted (now?) by the type checker and the error comes later, from the compiler. @danscales would there be an issue to make this last example work in the compiler? |
I encountered this in #36965. I implemented it then, and it was very straightforward. |
Change https://golang.org/cl/376056 mentions this issue: |
@griesemer : yes, I think we can fix this. I have change uploaded that seems to work well, but will see what the feedback (from Keith/Matthew) is on my fix. |
There are two remaining problems of this issue:
The following program should prints two
And the two issues could be closed if all are done. |
OK, maybe we should leave this issue open until the reflect and append/copy issues are addressed. By the way #23813 is essentially fixed, since there is no error or error message any more. |
Yes, let's leave this open. It's one thing to accept different types in a conversion, but a different thing if we permit changing types in what amounts to assignments (via |
Changing milestone to 1.19. |
types2 allows the conversion of a slice of a user-defined byte type B (not builtin uint8 or byte) to string. But runtime.slicebytetostring requires a []byte argument, so add in a CONVNOP from []B to []byte if needed. Same for the conversion of a slice of user-defined rune types to string. I made the same change in the transformations of the old typechecker, so as to keep tcConv() and transformConv() in sync. That fixes the bug for -G=0 mode as well. Fixes golang#23536 Change-Id: Ic79364427f27489187f3f8015bdfbf0769a70d69 Reviewed-on: https://go-review.googlesource.com/c/go/+/376056 Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Trust: Dan Scales <[email protected]> Run-TryBot: Dan Scales <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
Conversions between string types and slice of bytes types are now maximally liberal and simply require an underlying string type or an underlying byte type for "bytes slices". This is now also expressed in the spec.
That is, With respect to adjusting the reflect package, I filed #53523. Closing. |
What version of Go are you using (
go version
)?go version go1.9.2 linux/amd64
Does this issue reproduce with the latest release?
yes
What did you do?
It looks the definitions for
slice of bytes
are not consistent between spec and thebuiltin
docs.In spec
In he
builtin
docsHowever:
[edit] It looks
[]T
is treated as a slice of bytes inx = []T(str)
, but not in the copy and append calls.What did you expect to see?
all fail to compile or all compile okay.
What did you see instead?
non-consistent behavior
The text was updated successfully, but these errors were encountered: