Commit ba6275b
committed
Auto merge of rust-lang#82608 - Aaron1011:feature/final-preexp-tts, r=petrochenkov
Implement token-based handling of attributes during expansion
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.File tree
33 files changed
+2041
-1187
lines changed- compiler
- rustc_ast_lowering/src
- rustc_ast/src
- attr
- rustc_builtin_macros/src
- rustc_expand/src
- rustc_parse/src
- parser
- rustc_session/src
- src/test/ui/proc-macro
33 files changed
+2041
-1187
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | | - | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| 9 | + | |
| 10 | + | |
8 | 11 | | |
9 | 12 | | |
10 | 13 | | |
11 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
12 | 23 | | |
13 | 24 | | |
14 | 25 | | |
15 | 26 | | |
16 | 27 | | |
17 | 28 | | |
| 29 | + | |
18 | 30 | | |
19 | 31 | | |
20 | 32 | | |
| |||
26 | 38 | | |
27 | 39 | | |
28 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
29 | 90 | | |
30 | 91 | | |
31 | 92 | | |
| |||
35 | 96 | | |
36 | 97 | | |
37 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
38 | 103 | | |
39 | 104 | | |
40 | 105 | | |
| |||
66 | 131 | | |
67 | 132 | | |
68 | 133 | | |
| 134 | + | |
| 135 | + | |
69 | 136 | | |
70 | 137 | | |
71 | 138 | | |
| |||
79 | 146 | | |
80 | 147 | | |
81 | 148 | | |
| 149 | + | |
| 150 | + | |
82 | 151 | | |
83 | 152 | | |
84 | 153 | | |
| |||
94 | 163 | | |
95 | 164 | | |
96 | 165 | | |
| 166 | + | |
| 167 | + | |
97 | 168 | | |
98 | 169 | | |
99 | 170 | | |
| |||
127 | 198 | | |
128 | 199 | | |
129 | 200 | | |
130 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
131 | 205 | | |
| 206 | + | |
| 207 | + | |
132 | 208 | | |
133 | 209 | | |
134 | 210 | | |
| |||
140 | 216 | | |
141 | 217 | | |
142 | 218 | | |
| 219 | + | |
143 | 220 | | |
144 | 221 | | |
145 | 222 | | |
146 | 223 | | |
147 | 224 | | |
148 | 225 | | |
149 | 226 | | |
| 227 | + | |
| 228 | + | |
150 | 229 | | |
151 | 230 | | |
152 | 231 | | |
| |||
165 | 244 | | |
166 | 245 | | |
167 | 246 | | |
| 247 | + | |
| 248 | + | |
168 | 249 | | |
169 | 250 | | |
170 | 251 | | |
171 | 252 | | |
172 | 253 | | |
173 | | - | |
174 | 254 | | |
175 | 255 | | |
176 | 256 | | |
177 | 257 | | |
178 | 258 | | |
179 | 259 | | |
180 | 260 | | |
181 | | - | |
182 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
183 | 270 | | |
184 | | - | |
| 271 | + | |
| 272 | + | |
185 | 273 | | |
186 | 274 | | |
187 | 275 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
268 | 270 | | |
269 | 271 | | |
270 | 272 | | |
271 | | - | |
| 273 | + | |
272 | 274 | | |
273 | 275 | | |
274 | 276 | | |
275 | 277 | | |
276 | 278 | | |
277 | | - | |
278 | | - | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
279 | 285 | | |
280 | 286 | | |
281 | 287 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
630 | 630 | | |
631 | 631 | | |
632 | 632 | | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
633 | 660 | | |
634 | 661 | | |
635 | 662 | | |
| |||
652 | 679 | | |
653 | 680 | | |
654 | 681 | | |
655 | | - | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
656 | 693 | | |
657 | | - | |
| 694 | + | |
658 | 695 | | |
659 | | - | |
| 696 | + | |
660 | 697 | | |
661 | | - | |
| 698 | + | |
662 | 699 | | |
663 | 700 | | |
664 | 701 | | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
665 | 706 | | |
666 | 707 | | |
667 | 708 | | |
| |||
0 commit comments