Skip to content

Commit ace7ed6

Browse files
authored
Unrolled build for rust-lang#128224
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 7c2012d + 55d37ae commit ace7ed6

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
@@ -114,17 +114,15 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
114114
replace_ranges.sort_by_key(|(range, _)| range.start);
115115

116116
#[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+
);
128126
}
129127

130128
// Process the replace ranges, starting from the highest start
@@ -137,9 +135,9 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
137135
// `#[cfg(FALSE)] struct Foo { #[cfg(FALSE)] field: bool }`
138136
//
139137
// 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.
143141
for (range, target) in replace_ranges.into_iter().rev() {
144142
assert!(!range.is_empty(), "Cannot replace an empty range: {range:?}");
145143

@@ -297,11 +295,13 @@ impl<'a> Parser<'a> {
297295
// with `None`, which means the relevant tokens will be removed. (More
298296
// details below.)
299297
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+
}
305305
}
306306
}
307307

@@ -337,8 +337,7 @@ impl<'a> Parser<'a> {
337337
// When parsing `m`:
338338
// - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
339339
// - `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.
342341
// - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
343342
// including its outer attribute), with:
344343
// - `attrs`: includes the outer and the inner attr.
@@ -369,12 +368,10 @@ impl<'a> Parser<'a> {
369368

370369
// What is the status here when parsing the example code at the top of this method?
371370
//
372-
// When parsing `g`, we add two entries:
371+
// When parsing `g`, we add one entry:
373372
// - The `start_pos..end_pos` (`3..33`) entry has a new `AttrsTarget` with:
374373
// - `attrs`: includes the outer and the inner attr.
375374
// - `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`).
378375
//
379376
// When parsing `m`, we do nothing here.
380377

@@ -384,7 +381,6 @@ impl<'a> Parser<'a> {
384381
let start_pos = if has_outer_attrs { attrs.start_pos } else { start_pos };
385382
let target = AttrsTarget { attrs: ret.attrs().iter().cloned().collect(), tokens };
386383
self.capture_state.replace_ranges.push((start_pos..end_pos, Some(target)));
387-
self.capture_state.replace_ranges.extend(inner_attr_replace_ranges);
388384
} else if matches!(self.capture_state.capturing, Capturing::No) {
389385
// Only clear the ranges once we've finished capturing entirely, i.e. we've finished
390386
// the outermost call to this method.

0 commit comments

Comments
 (0)