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: in array and slice literals, the index/key can be non-integer type #23529
Comments
At the beginning of September 2016, the spec simply said
Which was vague but -as I read it- allowed things like The spec was updated in CL 30474, fix to issue #16679, and now says:
This seems more "strict", in the sense that it explicitly says that typed constants need to be of integer type, and looking at the spec, the part about constant declarations:
it seems to me that a constant declared with type specifier (as So it seems that the new spec phrasing introduced in September 2016 made the rules more strict, but the compiler was not updated accordingly. cc @griesemer |
What does gccgo do, @ianlancetaylor? |
This seems to me like an oversight (== bug) in the compiler. The index key is supposed to follow the exact same rules as an index for a slice/array. It's not valid to use a float64 index go/types complains in all these cases. |
Btw I just tried with gccgo (7.2) and, like gc, it accepts the program above without complaints. |
I'm worried that we can't fix this in the compilers at this point, as it might break existing programs (unless they were checked also against go vet, which complains). This will need a decision. cc: @ianlancetaylor @rsc |
@griesemer couldn't go fix mitigate the problem by redeclaring the constant as untyped in blocks where the constant is used for indexing? |
@j7b Yes, but that would still require that source to be changed and thus possibly brake existing programs. Or perhaps I misunderstood what you meant. |
What I was thinking is mechanically complicated, it'd be easier to have go fix explicitly convert non-int constants to int, that shouldn't break anything that would compile otherwise. |
Vet is going to start yelling about this (because it uses go/types) at some point (maybe Go 1.11; Go 1.10 ignores go/types failures unfortunately). After that point maybe we can fix the compilers. |
@griesemer @rsc go/types misses the case where the index to an array/slice is a typed constant with an underlying type int - same with composite array/slice literals. This should not work according to the spec: https://play.golang.org/p/dn62_WXGxem type foo float64 |
@ak2196 I'm sure I'm missing something but why do you say that your example should not work? A type defined to |
@ianlancetaylor maybe I am confused or reading the spec wrong bar's type is foo, foo's underlying type is int but the spec says for array literals: "An element with a key uses the key as its index. The key must be a non-negative constant representable by a value of type int; and if it is typed it must be of integer type." In this case the key is typed - the type is not 'int' it's foo but foo's underlying type is int. I guess it depends on what "integer type" means. Does it mean just int or anything that has int as the underlying type? |
@ak2196 "integer type" does not mean just |
@ianlancetaylor That definitely makes sense. Thanks for clearing it up. |
Revisit to see if there's anything to do here. |
It looks the behavior has been changed since 1.17. |
Both the 1.17 and 1.18 compiler correctly report an error here and follow the spec. This is now working as expected. Closing. |
What version of Go are you using (
go version
)?Go playground
What operating system and processor architecture are you using (
go env
)?See https://play.golang.org/p/x3keRiD0fVT. This compiles without an error.
What did you expect to see?
From the spec:
It mentions that if the index is typed, it must be of integer type. However, the above code compiles even if the index has type
float64
. Probably the spec should be saying "can be converted to int" or something, or the compiler should report an error?The text was updated successfully, but these errors were encountered: