Implement token-based handling of attributes during expansion#82608
Implement token-based handling of attributes during expansion#82608bors merged 1 commit intorust-lang:masterfrom
Conversation
|
@bors try @rust-timer queue |
|
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
|
⌛ Trying commit ac71b40b84add10df1cc6b4394ca53f5c3bfe16d with merge b86108850e24b4dd26ad05cb34d04ef489f2385a... |
|
☀️ Try build successful - checks-actions |
|
Queued b86108850e24b4dd26ad05cb34d04ef489f2385a with parent ec7f8d9, future comparison URL. |
|
Finished benchmarking try commit (b86108850e24b4dd26ad05cb34d04ef489f2385a): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
|
@Aaron1011 |
|
@petrochenkov Sure |
|
Opened #82643 |
|
(Still need to review changes in |
ac71b40 to
87977c6
Compare
|
@bors try @rust-timer queue |
|
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
|
⌛ Trying commit 87977c6a776ca0b4075f0998bde21267b3addcd6 with merge f1c431c58af7789983cbdfd61c470034c37eb631... |
|
☀️ Try build successful - checks-actions |
|
Queued f1c431c58af7789983cbdfd61c470034c37eb631 with parent 573a697, future comparison URL. |
|
Finished benchmarking try commit (f1c431c58af7789983cbdfd61c470034c37eb631): comparison url. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up. @bors rollup=never |
|
There appears to be a significant hit from being unable to bail out early from
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I've rebased against master - this should now be ready to merge. |
|
r=me with commits squashed. |
This PR modifies the macro expansion infrastructure to handle attributes in a fully token-based manner. As a result: * Derives macros no longer lose spans when their input is modified by eager cfg-expansion. This is accomplished by performing eager cfg-expansion on the token stream that we pass to the derive proc-macro * Inner attributes now preserve spans in all cases, including when we have multiple inner attributes in a row. This is accomplished through the following changes: * New structs `AttrAnnotatedTokenStream` and `AttrAnnotatedTokenTree` are introduced. These are very similar to a normal `TokenTree`, but they also track the position of attributes and attribute targets within the stream. They are built when we collect tokens during parsing. An `AttrAnnotatedTokenStream` is converted to a regular `TokenStream` when we invoke a macro. * Token capturing and `LazyTokenStream` are modified to work with `AttrAnnotatedTokenStream`. A new `ReplaceRange` type is introduced, which is created during the parsing of a nested AST node to make the 'outer' AST node aware of the attributes and attribute target stored deeper in the token stream. * When we need to perform eager cfg-expansion (either due to `#[derive]` or `#[cfg_eval]`), we tokenize and reparse our target, capturing additional information about the locations of `#[cfg]` and `#[cfg_attr]` attributes at any depth within the target. This is a performance optimization, allowing us to perform less work in the typical case where captured tokens never have eager cfg-expansion run.
|
@bors r=petrochenkov |
|
📌 Commit a93c4f0 has been approved by |
|
☀️ Test successful - checks-actions |
|
I believe this PR introduced a regression, breaking the |
|
@EliaGeretto: Thanks for finding that! I've opened #84130 to fix it |
This PR modifies the macro expansion infrastructure to handle attributes
in a fully token-based manner. As a result:
by eager cfg-expansion. This is accomplished by performing eager
cfg-expansion on the token stream that we pass to the derive
proc-macro
have multiple inner attributes in a row.
This is accomplished through the following changes:
AttrAnnotatedTokenStreamandAttrAnnotatedTokenTreeare introduced.These are very similar to a normal
TokenTree, but they also trackthe position of attributes and attribute targets within the stream.
They are built when we collect tokens during parsing.
An
AttrAnnotatedTokenStreamis converted to a regularTokenStreamwhenwe invoke a macro.
LazyTokenStreamare modified to work withAttrAnnotatedTokenStream. A newReplaceRangetype is introduced, whichis created during the parsing of a nested AST node to make the 'outer'
AST node aware of the attributes and attribute target stored deeper in the token stream.
#[derive]or#[cfg_eval]), we tokenize and reparse our target, capturing additional information about the locations of#[cfg]and#[cfg_attr]attributes at any depth within the target. This is a performance optimization, allowing us to perform less work in the typical case where captured tokens never have eager cfg-expansion run.