Overhaul CfgTrace/CfgAttrTrace handling#159266
Conversation
Once `sym::ignore` has been matched, the attribute can't be a `DocComment` and trying to match one is pointless.
To explain some things that weren't obvious to me.
`AttrItem` currently contains an `AttrItemKind` which is either `Parsed` or `Unparsed`. In the `Parsed` case the surrounding `AttrItem` is basically fake, with a meaningless `unsafety` field, a synthetic (non-ident) symbol, and an empty `tokens`. This commit moves `Parsed` up one level to become a new variant of `AttrKind`. It no longer needs to pretend to be an `AttrKind::Normal`, and makes a number of things simpler: - No `unparsed_ref` calls needed. - Replaces the `attr_into_trace` and `Attribute::replace_args` mess with the much simpler `convert_normal_to_parsed`. The commit converts a numberof non-exhaustive `AttrKind` matches (those that don't involve matching a single attribute) to exhaustive, for future-proofing. It uses `unreachable!` on all the `AttrKind::Parsed` cases that aren't hit by the full test suite.
This commit pushes further than the previous one, relying more heavily on looking directly at `AttrKind::Parsed` to handle various cases. Note that the removed clippy code was actually dead.
AST attributes use "early parsed"/"parsed" terminology to refer to the
`CfgTrace` and `CfgAttrTrace` attributes. I think this terminology is
meant to echo the terminology used for HIR attributes, i.e.
`hir::Attribute::{Parsed,Unparsed}`, probably because
`hir::Attribute::Parsed` is used for attributes that aren't stored in a
token-based form.
But this naming is misleading. "Early parsed" attributes aren't parsed
at all because they are inserted by the compiler and cannot be written
in source code. There are also two comments that claim that these
attributes are kept in parsed form "so they don't have to be reparsed
every time they're used, for performance", which is simply incorrect.
This commit renames these as "synthetic" attributes, which better
reflects their nature. The commit also fixes the incorrect comments.
Note that `is_parsed_attribute` is unchanged, because it refers to the
HIR attribute meaning. (And the removal of the synthetic attributes from
it in the previous commit is now more obviously correct.)
|
Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs cc @ZuseZ4 Some changes occurred in compiler/rustc_attr_parsing cc @jdonszelmann, @JonathanBrouwer
cc @rust-lang/clippy The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
The final two commits are opinionated, especially the final one. I do think the new terminology is much better, as explained in the commit logs, but I would appreciate careful checking to make sure I haven't overlooked or misinterpreted anything. In particular, if there are any future plans for handling AST attributes that might be relevant. |
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Overhaul `CfgTrace`/`CfgAttrTrace` handling
|
(Would like to take a look at this as well, might take me a few days to get to it) |
|
There's one more PR in flight changing the data in |
There will be conflicts but I think they will be superficial. That PR is about adding an argument to |
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (61408d1): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. @bors rollup=never rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 4.4%, secondary -0.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -5.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 489.277s -> 489.827s (0.11%) |
|
Some tiny perf wins? Wasn't the goal here, but I'll take it :) |
| /// When adding new early parsed attributes, consider whether they should be pretty printed. | ||
| /// Synthetic attributes are inserted by the compiler and cannot be written in source code. They | ||
| /// receive special treatment in various ways because they must not affect observable behaviour: | ||
| /// they are invisible to proc macros, cannot be pretty-printed, and are unable to re-enter the |
There was a problem hiding this comment.
These attributes are not printed in AST pretty-printing, because people parse the pretty-printed expanded code in practice and we shouldn't break this scenario if we can.
They are printed in HIR pretty-printing because people mostly stopped trying to parse the pretty-printed HIR nowadays :D
"Observable" here means language behavior (including input for proc macros).
The effects from keeping the traces can still be observable in rustdoc output, lints, diagnostics, etc.
| .zip(name) | ||
| .all(|(s, n)| s.args.is_none() && s.ident.name == *n) | ||
| } | ||
| AttrKind::Synthetic(..) => false, |
There was a problem hiding this comment.
| AttrKind::Synthetic(..) => false, | |
| AttrKind::Synthetic(..) | AttrKind::DocComment(..) => false, |
And a couple more similar cases below.
| .unwrap_or_else(|| panic!("attribute is missing tokens: {self:?}")) | ||
| .to_attr_token_stream() | ||
| .to_token_trees(), | ||
| AttrKind::Synthetic(..) => vec![], |
There was a problem hiding this comment.
I think this place is now load-bearing and replaces what attr_into_trace previously did (emptying the attribute's token stream), making the traces unobservable to proc macros.
It deserves a comment.
| } | ||
|
|
||
| pub fn unwrap_normal_item(self) -> AttrItem { | ||
| pub fn convert_normal_to_synthetic(&mut self, synthetic_attr: SyntheticAttr) { |
There was a problem hiding this comment.
| pub fn convert_normal_to_synthetic(&mut self, synthetic_attr: SyntheticAttr) { | |
| pub fn convert_normal_to_synthetic(self, synthetic_attr: SyntheticAttr) -> Attribute { |
Both of the existing uses would fit into this signature better.
|
I like all the changes here, including the last commits. (Most of the code refactored here was added during the rustc_attr_parsing migration, previously the cfg trace attributes were just regular normal attributes with a weird non-identifier name and emptied tokens). |
I noticed that
AttrItemKindwas a bit clunky and by the time I was finished I had a PR that:AttrItemKind;sym::cfg_traceandsym::cfg_attr_trace;It's a sizeable change but I think it makes things a lot clearer. Details in individual commits.
r? @petrochenkov