@@ -114,17 +114,15 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
114
114
replace_ranges. sort_by_key ( |( range, _) | range. start ) ;
115
115
116
116
#[ cfg( debug_assertions) ]
117
- {
118
- for [ ( range, tokens) , ( next_range, next_tokens) ] in replace_ranges. array_windows ( ) {
119
- assert ! (
120
- range. end <= next_range. start || range. end >= next_range. end,
121
- "Replace ranges should either be disjoint or nested: ({:?}, {:?}) ({:?}, {:?})" ,
122
- range,
123
- tokens,
124
- next_range,
125
- next_tokens,
126
- ) ;
127
- }
117
+ for [ ( range, tokens) , ( next_range, next_tokens) ] in replace_ranges. array_windows ( ) {
118
+ assert ! (
119
+ range. end <= next_range. start || range. end >= next_range. end,
120
+ "Replace ranges should either be disjoint or nested: ({:?}, {:?}) ({:?}, {:?})" ,
121
+ range,
122
+ tokens,
123
+ next_range,
124
+ next_tokens,
125
+ ) ;
128
126
}
129
127
130
128
// Process the replace ranges, starting from the highest start
@@ -137,9 +135,9 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
137
135
// `#[cfg(FALSE)] struct Foo { #[cfg(FALSE)] field: bool }`
138
136
//
139
137
// By starting processing from the replace range with the greatest
140
- // start position, we ensure that any replace range which encloses
141
- // another replace range will capture the *replaced* tokens for the inner
142
- // range, not the original tokens .
138
+ // start position, we ensure that any (outer) replace range which
139
+ // encloses another (inner) replace range will fully overwrite the
140
+ // inner range's replacement .
143
141
for ( range, target) in replace_ranges. into_iter ( ) . rev ( ) {
144
142
assert ! ( !range. is_empty( ) , "Cannot replace an empty range: {range:?}" ) ;
145
143
@@ -297,11 +295,13 @@ impl<'a> Parser<'a> {
297
295
// with `None`, which means the relevant tokens will be removed. (More
298
296
// details below.)
299
297
let mut inner_attr_replace_ranges = Vec :: new ( ) ;
300
- for inner_attr in ret. attrs ( ) . iter ( ) . filter ( |a| a. style == ast:: AttrStyle :: Inner ) {
301
- if let Some ( attr_range) = self . capture_state . inner_attr_ranges . remove ( & inner_attr. id ) {
302
- inner_attr_replace_ranges. push ( ( attr_range, None ) ) ;
303
- } else {
304
- self . dcx ( ) . span_delayed_bug ( inner_attr. span , "Missing token range for attribute" ) ;
298
+ for attr in ret. attrs ( ) {
299
+ if attr. style == ast:: AttrStyle :: Inner {
300
+ if let Some ( attr_range) = self . capture_state . inner_attr_ranges . remove ( & attr. id ) {
301
+ inner_attr_replace_ranges. push ( ( attr_range, None ) ) ;
302
+ } else {
303
+ self . dcx ( ) . span_delayed_bug ( attr. span , "Missing token range for attribute" ) ;
304
+ }
305
305
}
306
306
}
307
307
@@ -337,8 +337,7 @@ impl<'a> Parser<'a> {
337
337
// When parsing `m`:
338
338
// - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
339
339
// - `inner_attr_replace_ranges` is empty.
340
- // - `replace_range_start..replace_ranges_end` has two entries.
341
- // - One to delete the inner attribute (`17..27`), obtained when parsing `g` (see above).
340
+ // - `replace_range_start..replace_ranges_end` has one entry.
342
341
// - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
343
342
// including its outer attribute), with:
344
343
// - `attrs`: includes the outer and the inner attr.
@@ -369,12 +368,10 @@ impl<'a> Parser<'a> {
369
368
370
369
// What is the status here when parsing the example code at the top of this method?
371
370
//
372
- // When parsing `g`, we add two entries :
371
+ // When parsing `g`, we add one entry :
373
372
// - The `start_pos..end_pos` (`3..33`) entry has a new `AttrsTarget` with:
374
373
// - `attrs`: includes the outer and the inner attr.
375
374
// - `tokens`: lazy tokens for `g` (with its inner attr deleted).
376
- // - `inner_attr_replace_ranges` contains the one entry to delete the inner attr's
377
- // tokens (`17..27`).
378
375
//
379
376
// When parsing `m`, we do nothing here.
380
377
@@ -384,7 +381,6 @@ impl<'a> Parser<'a> {
384
381
let start_pos = if has_outer_attrs { attrs. start_pos } else { start_pos } ;
385
382
let target = AttrsTarget { attrs : ret. attrs ( ) . iter ( ) . cloned ( ) . collect ( ) , tokens } ;
386
383
self . capture_state . replace_ranges . push ( ( start_pos..end_pos, Some ( target) ) ) ;
387
- self . capture_state . replace_ranges . extend ( inner_attr_replace_ranges) ;
388
384
} else if matches ! ( self . capture_state. capturing, Capturing :: No ) {
389
385
// Only clear the ranges once we've finished capturing entirely, i.e. we've finished
390
386
// the outermost call to this method.
0 commit comments