-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
resolve: further improvements to "try using the enum's variant" diagnostic #77855
resolve: further improvements to "try using the enum's variant" diagnostic #77855
Conversation
d5f9e40
to
16e16aa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the change, but have a couple of nitpicks. What do you think?
help: try using one of the enum's variants | ||
help: try using the enum's variant which have fields | ||
| | ||
LL | let _: E = E::Unit; | ||
| ^^^^^^^ | ||
help: try using one of the enum's variants which have fields | ||
| | ||
LL | let _: E = (E::Fn(/* fields */)); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
LL | let _: E = (E::Struct { /* fields */ }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
help: you might have meant to use the following enum variant(s)
|
help: alternatively, the following enum variants are also available
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the PR to use this wording.
let needs_placeholder = |def_id: DefId, kind: CtorKind| { | ||
let has_no_fields = | ||
self.r.field_names.get(&def_id).map(|f| f.is_empty()).unwrap_or(false); | ||
match kind { | ||
CtorKind::Const => false, | ||
CtorKind::Fn | CtorKind::Fictive if has_no_fields => false, | ||
_ => true, | ||
} | ||
} | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be useful to either include the field names in the suggestion (instead of /*fields*/
) with some placeholder per field, or to have a span pointing at the def, otherwise you are forcing people to consult the docs/code to fix the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a note that points at the definition of the enum, this seemed more straightforward than adjusting the suggestions while still having the desired effect.
This commit improves the diagnostic modified in rust-lang#77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). Signed-off-by: David Wood <[email protected]>
This commit improves the tuple struct case added in rust-lang#77341 so that the context is mentioned in more of the message. Signed-off-by: David Wood <[email protected]>
16e16aa
to
f897162
Compare
@bors r+ |
📌 Commit f897162 has been approved by |
…nstructable-variants, r=estebank resolve: further improvements to "try using the enum's variant" diagnostic Follow-up on rust-lang#77341 (comment). This PR improves the diagnostic modified in rust-lang#77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). In addition, the wording of the tuple-variant-only case is improved slightly. I've not made further changes to the tuple-variant-only case (e.g. to only suggest variants with the correct number of fields) because I don't think I have enough information to do so reliably (e.g. in the case where there is an attempt to construct a tuple variant, I have no information on how many fields were provided; and in the case of pattern matching, I only have a slice of spans and would need to check for things like `..` in those spans, which doesn't seem worth it). r? @estebank
…nstructable-variants, r=estebank resolve: further improvements to "try using the enum's variant" diagnostic Follow-up on rust-lang#77341 (comment). This PR improves the diagnostic modified in rust-lang#77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). In addition, the wording of the tuple-variant-only case is improved slightly. I've not made further changes to the tuple-variant-only case (e.g. to only suggest variants with the correct number of fields) because I don't think I have enough information to do so reliably (e.g. in the case where there is an attempt to construct a tuple variant, I have no information on how many fields were provided; and in the case of pattern matching, I only have a slice of spans and would need to check for things like `..` in those spans, which doesn't seem worth it). r? @estebank
Rollup of 10 pull requests Successful merges: - rust-lang#75209 (Suggest imports of unresolved macros) - rust-lang#77547 (stabilize union with 'ManuallyDrop' fields and 'impl Drop for Union') - rust-lang#77827 (Don't link to nightly primitives on stable channel) - rust-lang#77855 (resolve: further improvements to "try using the enum's variant" diagnostic) - rust-lang#77900 (Use fdatasync for File::sync_data on more OSes) - rust-lang#77925 (Suggest minimal subset features in `incomplete_features` lint) - rust-lang#77971 (Deny broken intra-doc links in linkchecker) - rust-lang#77991 (Bump backtrace-rs) - rust-lang#77992 (instrument-coverage: try our best to not ICE) - rust-lang#78013 (Fix sidebar scroll on mobile devices) Failed merges: r? `@ghost`
Follow-up on #77341 (comment).
This PR improves the diagnostic modified in #77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). In addition, the wording of the tuple-variant-only case is improved slightly.
I've not made further changes to the tuple-variant-only case (e.g. to only suggest variants with the correct number of fields) because I don't think I have enough information to do so reliably (e.g. in the case where there is an attempt to construct a tuple variant, I have no information on how many fields were provided; and in the case of pattern matching, I only have a slice of spans and would need to check for things like
..
in those spans, which doesn't seem worth it).r? @estebank