Skip to content

Commit 54eb6bc

Browse files
committed
Move TokenCursor::num_next_calls into Parser and rename it.
It's more of a `Parser`-level concern than a `TokenCursor`-level concern. Also, `num_bump_calls` is a more accurate name, because it's incremented in `Parser::bump`.
1 parent 203cba7 commit 54eb6bc

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a> Parser<'a> {
3636
pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, AttrWrapper> {
3737
let mut outer_attrs = ast::AttrVec::new();
3838
let mut just_parsed_doc_comment = false;
39-
let start_pos = self.token_cursor.num_next_calls;
39+
let start_pos = self.num_bump_calls;
4040
loop {
4141
let attr = if self.check(&token::Pound) {
4242
let prev_outer_attr_sp = outer_attrs.last().map(|attr| attr.span);
@@ -277,7 +277,7 @@ impl<'a> Parser<'a> {
277277
pub(crate) fn parse_inner_attributes(&mut self) -> PResult<'a, ast::AttrVec> {
278278
let mut attrs = ast::AttrVec::new();
279279
loop {
280-
let start_pos: u32 = self.token_cursor.num_next_calls.try_into().unwrap();
280+
let start_pos: u32 = self.num_bump_calls.try_into().unwrap();
281281
// Only try to parse if it is an inner attribute (has `!`).
282282
let attr = if self.check(&token::Pound) && self.look_ahead(1, |t| t == &token::Not) {
283283
Some(self.parse_attribute(InnerAttrPolicy::Permitted)?)
@@ -298,7 +298,7 @@ impl<'a> Parser<'a> {
298298
None
299299
};
300300
if let Some(attr) = attr {
301-
let end_pos: u32 = self.token_cursor.num_next_calls.try_into().unwrap();
301+
let end_pos: u32 = self.num_bump_calls.try_into().unwrap();
302302
// If we are currently capturing tokens, mark the location of this inner attribute.
303303
// If capturing ends up creating a `LazyAttrTokenStream`, we will include
304304
// this replace range with it, removing the inner attribute from the final

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ impl<'a> Parser<'a> {
213213

214214
let start_token = (self.token.clone(), self.token_spacing);
215215
let cursor_snapshot = self.token_cursor.clone();
216+
let start_pos = self.num_bump_calls;
216217

217218
let has_outer_attrs = !attrs.attrs.is_empty();
218219
let prev_capturing = std::mem::replace(&mut self.capture_state.capturing, Capturing::Yes);
@@ -273,8 +274,7 @@ impl<'a> Parser<'a> {
273274

274275
let replace_ranges_end = self.capture_state.replace_ranges.len();
275276

276-
let cursor_snapshot_next_calls = cursor_snapshot.num_next_calls;
277-
let mut end_pos = self.token_cursor.num_next_calls;
277+
let mut end_pos = self.num_bump_calls;
278278

279279
let mut captured_trailing = false;
280280

@@ -306,7 +306,7 @@ impl<'a> Parser<'a> {
306306
end_pos += 1;
307307
}
308308

309-
let num_calls = end_pos - cursor_snapshot_next_calls;
309+
let num_calls = end_pos - start_pos;
310310

311311
// If we have no attributes, then we will never need to
312312
// use any replace ranges.
@@ -316,7 +316,7 @@ impl<'a> Parser<'a> {
316316
// Grab any replace ranges that occur *inside* the current AST node.
317317
// We will perform the actual replacement when we convert the `LazyAttrTokenStream`
318318
// to an `AttrTokenStream`.
319-
let start_calls: u32 = cursor_snapshot_next_calls.try_into().unwrap();
319+
let start_calls: u32 = start_pos.try_into().unwrap();
320320
self.capture_state.replace_ranges[replace_ranges_start..replace_ranges_end]
321321
.iter()
322322
.cloned()
@@ -359,8 +359,7 @@ impl<'a> Parser<'a> {
359359
// with a `FlatToken::AttrTarget`. If this AST node is inside an item
360360
// that has `#[derive]`, then this will allow us to cfg-expand this
361361
// AST node.
362-
let start_pos =
363-
if has_outer_attrs { attrs.start_pos } else { cursor_snapshot_next_calls };
362+
let start_pos = if has_outer_attrs { attrs.start_pos } else { start_pos };
364363
let new_tokens = vec![(FlatToken::AttrTarget(attr_data), Spacing::Alone)];
365364

366365
assert!(
@@ -464,6 +463,6 @@ mod size_asserts {
464463
use rustc_data_structures::static_assert_size;
465464
// tidy-alphabetical-start
466465
static_assert_size!(AttrWrapper, 16);
467-
static_assert_size!(LazyAttrTokenStreamImpl, 120);
466+
static_assert_size!(LazyAttrTokenStreamImpl, 112);
468467
// tidy-alphabetical-end
469468
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ pub struct Parser<'a> {
135135
pub capture_cfg: bool,
136136
restrictions: Restrictions,
137137
expected_tokens: Vec<TokenType>,
138-
// Important: This must only be advanced from `bump` to ensure that
139-
// `token_cursor.num_next_calls` is updated properly.
140138
token_cursor: TokenCursor,
139+
// The number of calls to `bump`, i.e. the position in the token stream.
140+
num_bump_calls: usize,
141141
/// This field is used to keep track of how many left angle brackets we have seen. This is
142142
/// required in order to detect extra leading left angle brackets (`<` characters) and error
143143
/// appropriately.
@@ -224,9 +224,6 @@ struct TokenCursor {
224224
// because it's the outermost token stream which never has delimiters.
225225
stack: Vec<(TokenTreeCursor, Delimiter, DelimSpan)>,
226226

227-
// Counts the number of calls to `{,inlined_}next`.
228-
num_next_calls: usize,
229-
230227
// During parsing, we may sometimes need to 'unglue' a
231228
// glued token into two component tokens
232229
// (e.g. '>>' into '>' and '>), so that the parser
@@ -402,9 +399,9 @@ impl<'a> Parser<'a> {
402399
token_cursor: TokenCursor {
403400
tree_cursor: stream.into_trees(),
404401
stack: Vec::new(),
405-
num_next_calls: 0,
406402
break_last_token: false,
407403
},
404+
num_bump_calls: 0,
408405
unmatched_angle_bracket_count: 0,
409406
max_angle_bracket_count: 0,
410407
last_unexpected_token_span: None,
@@ -1049,7 +1046,7 @@ impl<'a> Parser<'a> {
10491046
// Note: destructuring here would give nicer code, but it was found in #96210 to be slower
10501047
// than `.0`/`.1` access.
10511048
let mut next = self.token_cursor.inlined_next();
1052-
self.token_cursor.num_next_calls += 1;
1049+
self.num_bump_calls += 1;
10531050
// We've retrieved an token from the underlying
10541051
// cursor, so we no longer need to worry about
10551052
// an unglued token. See `break_and_eat` for more details
@@ -1446,7 +1443,7 @@ impl<'a> Parser<'a> {
14461443
}
14471444

14481445
pub fn approx_token_stream_pos(&self) -> usize {
1449-
self.token_cursor.num_next_calls
1446+
self.num_bump_calls
14501447
}
14511448
}
14521449

0 commit comments

Comments
 (0)