-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggest using an appropriate keyword for struct
and enum
#94996
Suggest using an appropriate keyword for struct
and enum
#94996
Conversation
r? @nagisa (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
struct
and enum
struct
and enum
help: consider using `enum` instead of `struct` | ||
| | ||
LL | #[derive(enum)] | ||
| ~~~~ |
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.
Seems like this shouldn't happen, the suggested replacement has 0 chance to work at all.
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 fixed it.
help: consider using `enum` instead of `struct` | ||
| | ||
LL | pub enum NotStruct { | ||
| ~~~~ |
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'm not entirely sure if we should be as trigger happy as proposed here about suggesting a different ADT type. I feel like it is much more likely that an ADT body will contain mistakes compared to user having specified a wrong kind of ADT.
Here it feels like the user was much more likely to just have forgetten to specify a type : _
for whatever reason. Though I could still see us making a suggestion as proposed here if there was at least one variant with data. That is in cases like these:
pub struct NotStruct {
Variant,
Variant2(u8),
}
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 agree with nagisa's comment. I think that for a case as ambiguous as these we might be better served by pointing at all the relevant places and let the user figure what they wanted. That could be done for cases like this one by adding a label to the struct
keyword saying "while parsing this struct
".
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.
The comment above is still valid. I would prefer to simplify the logic and "just" add a span_label pointing at the item type being parsed. The likelihood of false positives is too high as it is.
@@ -1176,13 +1178,14 @@ impl<'a> Parser<'a> { | |||
} | |||
|
|||
/// Parses an enum declaration. | |||
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> { | |||
fn parse_item_enum(&mut self, start_span: Span) -> PResult<'a, ItemInfo> { |
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.
start_span
seems a little misnomer here, given that the span begins at the enum
or struct
keyword, rather than covering the entire item (i.e. including the qualifiers such as pub
). Maybe something like keyword_span
would work better?
Ok(_) => { | ||
let mut err = this.struct_span_err( | ||
enum_field_start_span.to(this.prev_token.span), | ||
"the enum cannot have a struct field declaration", |
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.
Diagnostics in parser largely follow a “expected ...” style of messages. The suggested wording would likely be more consistent with the rest of the diagnostics coming from the parser.
"the enum cannot have a struct field declaration", | |
"expected an `enum` variant, found a field", |
let (fields, recovered) = self.parse_record_struct_body( | ||
"union", | ||
generics.where_clause.has_where_token, | ||
None, |
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.
If we're handling struct
s, we should also handle union
s.
); | ||
err.span_suggestion_verbose( | ||
start_span, | ||
"consider using `struct` instead of `enum`", |
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.
Hm, an union
could also make sense here, wouldn't it? While it is comparatively much rarer, looking to define an enum
like this may very well be thinking of an union
in the first place.
"consider using `struct` instead of `enum`", | |
"consider defining a `struct` or an `union` instead of an `enum`", |
Not entirely sure how or if span_suggestion
can provide multiple alternative replacements, though.
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.
It might very well be that this suggestion is largely superfluous with the improved error message.
I'm going to pass this on to @estebank for a further review, since they have a better grasp over the overall direction of diagnostics in Rust. |
r? @estebank |
Let's make sure we have tests to avoid regressing on knock down errors (let's have a struct and enum with multiple variants and fields to see that we only emit a single error for them). |
@estebank |
Switching review labels until PR is ready again for review, thanks! @rustbot author |
c0d2c11
to
f49601d
Compare
☔ The latest upstream changes (presumably #96528) made this pull request unmergeable. Please resolve the merge conflicts. |
…nion-ident, r=estebank Add a label to struct/enum/union ident name Based on rust-lang#94996 (comment) cc: ``@estebank``
…on-ident, r=estebank Add a label to struct/enum/union ident name Based on rust-lang#94996 (comment) cc: `@estebank`
closes #75770