-
Notifications
You must be signed in to change notification settings - Fork 12.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
Fix Clippy handling of ExpnKind::Desugaring
#73468
Fix Clippy handling of ExpnKind::Desugaring
#73468
Conversation
Following PR rust-lang#72389, we create many more spans with `ExpnKind::Desugaring`. This exposed a latent bug in Clippy - only the top-most `ExpnData` is considered when checking if code is the result of a macro expansion. If code emitted by a macro expansion gets annotated with an `ExpnKind::Desugaring` (e.g. an operator or a for loop), Clippy will incorrectly act as though this code is not the result of a macro expansion. This PR introduces the `ClosestAstOrMacro` enum, which allows linting code to quickly determine if a given `Span` is the result of a macro expansion. For any `ExpnId`, we keep track of closest `ExpnKind::Macro` or `ExpnKind::AstPass` in the `call_site` chain. This is determined when the `ExpnData` is set for an `ExpnId`, which allows us to avoid walking the entire chain in Clippy.
Using `Span::from_expansion` results in false positives - it will return `true` if have an `ExpnKind::Desugaring`, even if is not the result of a macro expansion. Using `ClosestAstOrMacro` from rustc, we can now cheaply determine if a macro or ast expansion occurs anywhere in the call chain.
(rust_highfive has picked a reviewer for you, use r? to override) |
ExpnKind::Desugard
ExpnKind::Desugaring
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.
clippy side looks great, thanks!
Hm, there is still the Also it seems, that Clippy is not tested on |
r=me with nit, but @petrochenkov self-assigned, so probably good to wait for them to weigh in |
@flip1995: The |
I haven't seen #72389 but it appears to be a serious misuse of expansions. I need to look at #72389 in more detail and see what it does and whether it can be done without hacks. (That said, |
I would personally prefer to revert #72389 in the meantime and fix clippy. |
Yeah like I said before diagnostics should not take priority over existing functionality |
@petrochenkov: Can you elaborate on this? While operators are not 'desugared' in the same way that 'if' statements and 'for' loops are desugared (operators are desugared during MIR building, rather than AST lowering), I believe it fits the way that way that |
MIR desugaring is not the same as the source-to-source desugaring that occurs in When it comes to diagnostics source-to-source transforms are the ones that cause the most pain and that's why they're handled this way. I hadn't looked closely at this before but now that I look through it I find myself agreeing with @petrochenkov, it's a mistake to lump MIR transforms into DesugaringKind. |
@Manishearth @petrochenkov: PR #73571 is up, which reverts just the operator desugaring changes. |
See rust-lang#73468 (comment) This removes the immediate need for PR rust-lang#73468, since Clippy will no longer be confused by the precense of a new `ExpnId` 'on top of' a macro expansion `ExpnId`. This makes some of the 'self move diagnostics' added by PR rust-lang#72389 slightly worse. I've left the operator-related diagnostics code in place, so it should be easy for a followup PR to plug in a new way of detecting desugared operators.
Closing in favor of the revert. |
Now that the revert has landed in #73594, I think we should consider landing this. Clippy's current method detecting macro expansions is very fragile - any additional |
So I still don't understand how this would lead to a false positive in clippy: could you give an example? |
Like, overall AST desugarings are pretty rare, and it's basically only the initial few that have always existed. Changes to desugarings necessitate changes in Clippy anyway since sometimes we need to un-desugar them. |
I'm going to close this for now, with #72389 reverted it's not super urgent, and I'd like to consider eliminating lowering-time expansions entirely. |
PR #72389 exposed a latent issue in Clippy: only the top-most
ExpnKind
was checked forExpnKind::Macro
orExpnKind::AstPass
. Under certain circumstances, this could result in Clippy emitting warnings for code produced by a macro expansion, due to the topmostExpnId
havingExpnKind::Desugaring
.This PR tracks the presence of
ExpnKind::Macro
andExpnKind::AstPass
in thecall_site
chain through a newClosestAstOrMacro
. This enum is used in Clippy, allowing all of Clippy's UI tests to pass. As a result, we can re-enable Clippy tests insrc/bootstrap/test.rs