-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: clearer error for invalid const values #69955
Comments
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
It's not precisely correct to say that "only boolean, numeric and string literals" can appear in constant expressions. For example, We could at least "is not a constant expression" which is a better search term. |
Yeah, I got that simple expressions as being allowed from reading the spec earlier, I meant it more like "out of all literals only these literals are allowed" but it's hard to convey that within the limited space of a single error message. I'm not sure if there are other compiler error messages which are split across multiple lines. If that's acceptable, there's potentially more room to provide helpful information without it being overwhelming. |
Change https://go.dev/cl/621435 mentions this issue: |
cc @golang/compiler |
I believe the current error message "is not constant" is just as clear as what is proposed. I don't believe anything needs to be done here. This is just churn and quite possibly will break a bunch of tests. |
Marking as backlog. There may be a better way to guide users in the right direction, but changing from "not a constant" to "not a constant expression" doesn't seem to add much and produces needless churn. For one, a simple search for "golang constant" produces about equally helpful results as "golang constant expression". |
That's why I provided several alternatives in the issue description, for example, this one:
|
The existing error messages (reasonably) assume the reader knows that only basic types can be constant. One small tweak we could make would be to put the type kind name (pointer, chan, numeric, etc) in the error message if the type is named. For example "value of numeric type S" instead "of value of type S". The original error would then become:
which may help remind the reader that struct types have no constants. |
@varungandhi-src "use var instead of const" may work in this specific case, but the compiler doesn't know if that is in fact the error. Maybe you actually meant to use a constant, and then saying "using var" would be strange. That is why the compiler in general doesn't provide suggestions: it simply cannot know what the right suggestion is. I like @adonovan's suggestion. I will take care of it. |
Change https://go.dev/cl/621458 mentions this issue: |
Go version
go version go1.23.2 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/entU62NdkPA
What did you see happen?
The error message is:
What did you expect to see?
It's not unusual to want to specify struct constants in code, but using struct literals with constant fields is not supported.
The error message could be made clearer to indicate that struct literals are not permitted in constant expressions (because at face value, struct literals with constant fields and no pointers or casting are conceptually constant). It might be helpful to state that only boolean, numeric and string literals are allowed in constant expressions.
Potential ideas for improved error messages:
The text was updated successfully, but these errors were encountered: