Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8a390ba

Browse files
committed
Change empty replace range condition.
The new condition is equivalent in practice, but it's much more obvious that it would result in an empty range, because the condition lines up with the contents of the iterator.
1 parent f552794 commit 8a390ba

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,22 @@ impl<'a> Parser<'a> {
295295

296296
let num_calls = end_pos - start_pos;
297297

298-
// If we have no attributes, then we will never need to
299-
// use any replace ranges.
300-
let replace_ranges: Box<[ReplaceRange]> = if ret.attrs().is_empty() && !self.capture_cfg {
301-
Box::new([])
302-
} else {
303-
// Grab any replace ranges that occur *inside* the current AST node.
304-
// We will perform the actual replacement when we convert the `LazyAttrTokenStream`
305-
// to an `AttrTokenStream`.
306-
self.capture_state.replace_ranges[replace_ranges_start..replace_ranges_end]
307-
.iter()
308-
.cloned()
309-
.chain(inner_attr_replace_ranges.iter().cloned())
310-
.map(|(range, data)| ((range.start - start_pos)..(range.end - start_pos), data))
311-
.collect()
312-
};
298+
// This is hot enough for `deep-vector` that checking the conditions for an empty iterator
299+
// is measurably faster than actually executing the iterator.
300+
let replace_ranges: Box<[ReplaceRange]> =
301+
if replace_ranges_start == replace_ranges_end && inner_attr_replace_ranges.is_empty() {
302+
Box::new([])
303+
} else {
304+
// Grab any replace ranges that occur *inside* the current AST node.
305+
// We will perform the actual replacement when we convert the `LazyAttrTokenStream`
306+
// to an `AttrTokenStream`.
307+
self.capture_state.replace_ranges[replace_ranges_start..replace_ranges_end]
308+
.iter()
309+
.cloned()
310+
.chain(inner_attr_replace_ranges.iter().cloned())
311+
.map(|(range, data)| ((range.start - start_pos)..(range.end - start_pos), data))
312+
.collect()
313+
};
313314

314315
let tokens = LazyAttrTokenStream::new(LazyAttrTokenStreamImpl {
315316
start_token,

0 commit comments

Comments
 (0)