Rollup merge of #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````
This commit is contained in:
commit
af52be2cea
1 changed files with 21 additions and 25 deletions
|
@ -110,7 +110,6 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
|
||||||
replace_ranges.sort_by_key(|(range, _)| range.start);
|
replace_ranges.sort_by_key(|(range, _)| range.start);
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
|
||||||
for [(range, tokens), (next_range, next_tokens)] in replace_ranges.array_windows() {
|
for [(range, tokens), (next_range, next_tokens)] in replace_ranges.array_windows() {
|
||||||
assert!(
|
assert!(
|
||||||
range.end <= next_range.start || range.end >= next_range.end,
|
range.end <= next_range.start || range.end >= next_range.end,
|
||||||
|
@ -121,7 +120,6 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
|
||||||
next_tokens,
|
next_tokens,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Process the replace ranges, starting from the highest start
|
// Process the replace ranges, starting from the highest start
|
||||||
// position and working our way back. If have tokens like:
|
// position and working our way back. If have tokens like:
|
||||||
|
@ -133,9 +131,9 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
|
||||||
// `#[cfg(FALSE)] struct Foo { #[cfg(FALSE)] field: bool }`
|
// `#[cfg(FALSE)] struct Foo { #[cfg(FALSE)] field: bool }`
|
||||||
//
|
//
|
||||||
// By starting processing from the replace range with the greatest
|
// By starting processing from the replace range with the greatest
|
||||||
// start position, we ensure that any replace range which encloses
|
// start position, we ensure that any (outer) replace range which
|
||||||
// another replace range will capture the *replaced* tokens for the inner
|
// encloses another (inner) replace range will fully overwrite the
|
||||||
// range, not the original tokens.
|
// inner range's replacement.
|
||||||
for (range, target) in replace_ranges.into_iter().rev() {
|
for (range, target) in replace_ranges.into_iter().rev() {
|
||||||
assert!(!range.is_empty(), "Cannot replace an empty range: {range:?}");
|
assert!(!range.is_empty(), "Cannot replace an empty range: {range:?}");
|
||||||
|
|
||||||
|
@ -293,11 +291,13 @@ impl<'a> Parser<'a> {
|
||||||
// with `None`, which means the relevant tokens will be removed. (More
|
// with `None`, which means the relevant tokens will be removed. (More
|
||||||
// details below.)
|
// details below.)
|
||||||
let mut inner_attr_replace_ranges = Vec::new();
|
let mut inner_attr_replace_ranges = Vec::new();
|
||||||
for inner_attr in ret.attrs().iter().filter(|a| a.style == ast::AttrStyle::Inner) {
|
for attr in ret.attrs() {
|
||||||
if let Some(attr_range) = self.capture_state.inner_attr_ranges.remove(&inner_attr.id) {
|
if attr.style == ast::AttrStyle::Inner {
|
||||||
|
if let Some(attr_range) = self.capture_state.inner_attr_ranges.remove(&attr.id) {
|
||||||
inner_attr_replace_ranges.push((attr_range, None));
|
inner_attr_replace_ranges.push((attr_range, None));
|
||||||
} else {
|
} else {
|
||||||
self.dcx().span_delayed_bug(inner_attr.span, "Missing token range for attribute");
|
self.dcx().span_delayed_bug(attr.span, "Missing token range for attribute");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,8 +333,7 @@ impl<'a> Parser<'a> {
|
||||||
// When parsing `m`:
|
// When parsing `m`:
|
||||||
// - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
|
// - `start_pos..end_pos` is `0..34` (`mod m`, excluding the `#[cfg_eval]` attribute).
|
||||||
// - `inner_attr_replace_ranges` is empty.
|
// - `inner_attr_replace_ranges` is empty.
|
||||||
// - `replace_range_start..replace_ranges_end` has two entries.
|
// - `replace_range_start..replace_ranges_end` has one entry.
|
||||||
// - One to delete the inner attribute (`17..27`), obtained when parsing `g` (see above).
|
|
||||||
// - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
|
// - One `AttrsTarget` (added below when parsing `g`) to replace all of `g` (`3..33`,
|
||||||
// including its outer attribute), with:
|
// including its outer attribute), with:
|
||||||
// - `attrs`: includes the outer and the inner attr.
|
// - `attrs`: includes the outer and the inner attr.
|
||||||
|
@ -365,12 +364,10 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
// What is the status here when parsing the example code at the top of this method?
|
// What is the status here when parsing the example code at the top of this method?
|
||||||
//
|
//
|
||||||
// When parsing `g`, we add two entries:
|
// When parsing `g`, we add one entry:
|
||||||
// - The `start_pos..end_pos` (`3..33`) entry has a new `AttrsTarget` with:
|
// - The `start_pos..end_pos` (`3..33`) entry has a new `AttrsTarget` with:
|
||||||
// - `attrs`: includes the outer and the inner attr.
|
// - `attrs`: includes the outer and the inner attr.
|
||||||
// - `tokens`: lazy tokens for `g` (with its inner attr deleted).
|
// - `tokens`: lazy tokens for `g` (with its inner attr deleted).
|
||||||
// - `inner_attr_replace_ranges` contains the one entry to delete the inner attr's
|
|
||||||
// tokens (`17..27`).
|
|
||||||
//
|
//
|
||||||
// When parsing `m`, we do nothing here.
|
// When parsing `m`, we do nothing here.
|
||||||
|
|
||||||
|
@ -380,7 +377,6 @@ impl<'a> Parser<'a> {
|
||||||
let start_pos = if has_outer_attrs { attrs.start_pos } else { start_pos };
|
let start_pos = if has_outer_attrs { attrs.start_pos } else { start_pos };
|
||||||
let target = AttrsTarget { attrs: ret.attrs().iter().cloned().collect(), tokens };
|
let target = AttrsTarget { attrs: ret.attrs().iter().cloned().collect(), tokens };
|
||||||
self.capture_state.replace_ranges.push((start_pos..end_pos, Some(target)));
|
self.capture_state.replace_ranges.push((start_pos..end_pos, Some(target)));
|
||||||
self.capture_state.replace_ranges.extend(inner_attr_replace_ranges);
|
|
||||||
} else if matches!(self.capture_state.capturing, Capturing::No) {
|
} else if matches!(self.capture_state.capturing, Capturing::No) {
|
||||||
// Only clear the ranges once we've finished capturing entirely, i.e. we've finished
|
// Only clear the ranges once we've finished capturing entirely, i.e. we've finished
|
||||||
// the outermost call to this method.
|
// the outermost call to this method.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue