Skip to content

Commit 97f49e1

Browse files
authored
Rollup merge of rust-lang#128224 - nnethercote:fewer-replace_ranges, r=petrochenkov
Remove unnecessary range replacements This PR removes an unnecessary range replacement in `collect_tokens_trailing_token`, and does a couple of other small cleanups. r? ```@petrochenkov```
2 parents d0de531 + 55d37ae commit 97f49e1

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,15 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
110110
replace_ranges.sort_by_key(|(range, _)| range.start);
111111

112112
#[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+
);
124122
}
125123

126124
// Process the replace ranges, starting from the highest start
@@ -133,9 +131,9 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
133131
// `#[cfg(FALSE)] struct Foo { #[cfg(FALSE)] field: bool }`
134132
//
135133
// 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.
139137
for (range, target) in replace_ranges.into_iter().rev() {
140138
assert!(!range.is_empty(), "Cannot replace an empty range: {range:?}");
141139

@@ -293,11 +291,13 @@ impl<'a> Parser<'a> {
293291
// with `None`, which means the relevant tokens will be removed. (More
294292
// details below.)
295293
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+
}
301301
}
302302
}
303303

@@ -333,8 +333,7 @@ impl<'a> Parser<'a> {
333333
// When parsing `m`:
334334
// - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
335335
// - `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.
338337
// - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
339338
// including its outer attribute), with:
340339
// - `attrs`: includes the outer and the inner attr.
@@ -365,12 +364,10 @@ impl<'a> Parser<'a> {
365364

366365
// What is the status here when parsing the example code at the top of this method?
367366
//
368-
// When parsing `g`, we add two entries:
367+
// When parsing `g`, we add one entry:
369368
// - The `start_pos..end_pos` (`3..33`) entry has a new `AttrsTarget` with:
370369
// - `attrs`: includes the outer and the inner attr.
371370
// - `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`).
374371
//
375372
// When parsing `m`, we do nothing here.
376373

@@ -380,7 +377,6 @@ impl<'a> Parser<'a> {
380377
let start_pos = if has_outer_attrs { attrs.start_pos } else { start_pos };
381378
let target = AttrsTarget { attrs: ret.attrs().iter().cloned().collect(), tokens };
382379
self.capture_state.replace_ranges.push((start_pos..end_pos, Some(target)));
383-
self.capture_state.replace_ranges.extend(inner_attr_replace_ranges);
384380
} else if matches!(self.capture_state.capturing, Capturing::No) {
385381
// Only clear the ranges once we've finished capturing entirely, i.e. we've finished
386382
// the outermost call to this method.

0 commit comments

Comments
 (0)