-
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
Require #[must_use]
on Diagnostic
structs to prevent future bugs
#119180
Conversation
To prevent bugs like 118972.
To prevent bugs like 118972.
Some changes occurred in compiler/rustc_codegen_gcc Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt
|
Thanks, @Enselic! I'm happy to do the review -- but I'd like confirmation from @rust-lang/wg-diagnostics that they are generally OK with the approach. A quick summary:
Does that sound OK? |
@michaelwoerister lets go for it. |
Requiring a mandatory line of boilerplate on 1,300+ structs seems suboptimal to me :( |
I agree with @nnethercote. I don't think that this is maintainable, tbh, |
☔ The latest upstream changes (presumably #119190) made this pull request unmergeable. Please resolve the merge conflicts. |
One alternative would be to turn the derive macro into an attribute macro and make that add |
I don't quite agree on the maintainability aspect, by the way. As far as I can tell, one would only come into contact with this when adding a new diagnostic struct and the error message gives a simple instruction on what to do. I think the approach is acceptable. But yes, requiring the user to add boilerplate code is indeed not exactly optimal 🙂 |
Let's explore that idea a bit more. It is not clear to me exactly how we would achieve this. Let's take this struct as an example: #[derive(Diagnostic, Clone, Copy)]
#[diag(ast_lowering_underscore_expr_lhs_assign)]
pub struct UnderscoreExprLhsAssign {
#[primary_span]
#[label]
pub span: Span,
} what would this struct definition look like with the attribute macro approach? |
@Enselic I believe that derive macros can't add annotations to the annotated item, only expand to new items, while proc macro attributes can fully rewrite the item they are applied to, including adding new attributes. |
I might explore the proc macro attribute approach later. Since this PR is not actionable before someone has explored the proc macro attribute approach, I will close this PR for now. I would like to thank everyone who provided feedback so far 👍 |
The attribute macro approach should be rather similar. The main difference is that attribute macros will expect that the item given in the input token stream will be re-emitted (possibly modified) along with any additional items (e.g. "derived" impls). Then the example above would look like:
The macro (now |
In the example above, I moved the |
This will prevent bugs like #118972 at compile time.
Also see #118973 where this was originally discussed.
This PR handles
Diagnostic
structsWe should also begin to require
#[must_use]
onDiagnostic
enumsSubdiagnostic
structsSubdiagnostic
enumsbut let's wait with that until we know we want this at all.
r? @michaelwoerister (who maybe want to delegate this to someone else)
cc @rust-lang/wg-diagnostics
Note: Review this PR commit-by-commit since one of the commits is repetitive but big...
Alternatives
We could maybe turn the
Diagnostic
derive macro into an attribute macro so that it can add#[must_use]
automatically behind the scenes. But I am personally not against the clarity of an explicit#[must_use]
attribute.