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