-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: clearer error message for unkeyed composite literal with unexported field #28053
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
You cannot modify unexported fields from separate packages. This applies to composite literals too. The error seems clear enough to me. |
I'm not referring to the error description. I'm suggesting literal assignments should just be banned, or when an unexported field exists, initialised to zero value when attempting to do literal assignment (since you can't access the unexported field anyway). Please reopen issue. |
I don't understand what you mean, but I'm going to reopen this for now. |
Also, please note that language changes should be proposals, as they need careful consideration. You should try to make your proposal as detailed and clear as possible. |
In your example, the solution would be to use a keyed literal. Note that using an unkeyed composite literal of a struct type defined in another package is already explicitly recommended against, to preserve compatibility when such types add fields - the Go 1 compatibility promise specifically excludes such unkeyed literals, for example. There's even a proposal to disallow them altogether, and they're already a warning in vet. |
@pjebs |
@mvdan Minor point: please don't add a NeedsDecision label to Go 2 issues. We're using that as a marker to be applied only after review. Thanks. |
|
I showed the 2 different error messages to highlight that the status quo seemed inconsistent, since the first error message says Hence the proposal. Remove or rectify. |
The solution is to use a keyed literal. The vet We aren't going to require people to use keyed literals even for structs defined in the same package. And we don't want to have different rules for literals defined in the same package and literals defined in a different package. We could perhaps improve the compiler error messages for these cases. I'll repurpose this issue for that. |
I have a struct:
From an outside package:
TRY 1
Compiler error: too few values in df.SortKey literal
TRY 2
Compiler error: implicit assignment of unexported field 'seriesIndex' in df.SortKey literal
You can't win. Perhaps it should just be disallowed for Go 2 - It messes up backwards compatibility anyway amongst minor versions of packages (eg. net/http.Request and Context situation).
The text was updated successfully, but these errors were encountered: