Skip to content

Commit 6fc2c48

Browse files
committed
Move TokenCursor::break_last_token into Parser.
Similar to the last commit, it's more of a `Parser`-level concern than a `TokenCursor`-level concern. And the struct size reductions are nice. After this change, `TokenCursor` is as minimal as possible (two fields and two methods) which is nice.
1 parent 54eb6bc commit 6fc2c48

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl<'a> Parser<'a> {
301301
// then extend the range of captured tokens to include it, since the parser
302302
// was not actually bumped past it. When the `LazyAttrTokenStream` gets converted
303303
// into an `AttrTokenStream`, we will create the proper token.
304-
if self.token_cursor.break_last_token {
304+
if self.break_last_token {
305305
assert!(!captured_trailing, "Cannot set break_last_token and have trailing token");
306306
end_pos += 1;
307307
}
@@ -331,7 +331,7 @@ impl<'a> Parser<'a> {
331331
start_token,
332332
num_calls,
333333
cursor_snapshot,
334-
break_last_token: self.token_cursor.break_last_token,
334+
break_last_token: self.break_last_token,
335335
replace_ranges,
336336
});
337337

@@ -362,10 +362,7 @@ impl<'a> Parser<'a> {
362362
let start_pos = if has_outer_attrs { attrs.start_pos } else { start_pos };
363363
let new_tokens = vec![(FlatToken::AttrTarget(attr_data), Spacing::Alone)];
364364

365-
assert!(
366-
!self.token_cursor.break_last_token,
367-
"Should not have unglued last token with cfg attr"
368-
);
365+
assert!(!self.break_last_token, "Should not have unglued last token with cfg attr");
369366
let range: Range<u32> = (start_pos.try_into().unwrap())..(end_pos.try_into().unwrap());
370367
self.capture_state.replace_ranges.push((range, new_tokens));
371368
self.capture_state.replace_ranges.extend(inner_attr_replace_ranges);
@@ -463,6 +460,6 @@ mod size_asserts {
463460
use rustc_data_structures::static_assert_size;
464461
// tidy-alphabetical-start
465462
static_assert_size!(AttrWrapper, 16);
466-
static_assert_size!(LazyAttrTokenStreamImpl, 112);
463+
static_assert_size!(LazyAttrTokenStreamImpl, 104);
467464
// tidy-alphabetical-end
468465
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ impl<'a> Parser<'a> {
11671167
DestructuredFloat::TrailingDot(sym, sym_span, dot_span) => {
11681168
assert!(suffix.is_none());
11691169
// Analogous to `Self::break_and_eat`
1170-
self.token_cursor.break_last_token = true;
1170+
self.break_last_token = true;
11711171
// This might work, in cases like `1. 2`, and might not,
11721172
// in cases like `offset_of!(Ty, 1.)`. It depends on what comes
11731173
// after the float-like token, and therefore we have to make

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ pub struct Parser<'a> {
138138
token_cursor: TokenCursor,
139139
// The number of calls to `bump`, i.e. the position in the token stream.
140140
num_bump_calls: usize,
141+
// During parsing we may sometimes need to 'unglue' a glued token into two
142+
// component tokens (e.g. '>>' into '>' and '>), so the parser can consume
143+
// them one at a time. This process bypasses the normal capturing mechanism
144+
// (e.g. `num_bump_calls` will not be incremented), since the 'unglued'
145+
// tokens due not exist in the original `TokenStream`.
146+
//
147+
// If we end up consuming both unglued tokens, this is not an issue. We'll
148+
// end up capturing the single 'glued' token.
149+
//
150+
// However, sometimes we may want to capture just the first 'unglued'
151+
// token. For example, capturing the `Vec<u8>` in `Option<Vec<u8>>`
152+
// requires us to unglue the trailing `>>` token. The `break_last_token`
153+
// field is used to track this token. It gets appended to the captured
154+
// stream when we evaluate a `LazyAttrTokenStream`.
155+
break_last_token: bool,
141156
/// This field is used to keep track of how many left angle brackets we have seen. This is
142157
/// required in order to detect extra leading left angle brackets (`<` characters) and error
143158
/// appropriately.
@@ -161,7 +176,7 @@ pub struct Parser<'a> {
161176
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
162177
// it doesn't unintentionally get bigger.
163178
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
164-
rustc_data_structures::static_assert_size!(Parser<'_>, 272);
179+
rustc_data_structures::static_assert_size!(Parser<'_>, 264);
165180

166181
/// Stores span information about a closure.
167182
#[derive(Clone)]
@@ -223,29 +238,6 @@ struct TokenCursor {
223238
// tokens are in `stack[n-1]`. `stack[0]` (when present) has no delimiters
224239
// because it's the outermost token stream which never has delimiters.
225240
stack: Vec<(TokenTreeCursor, Delimiter, DelimSpan)>,
226-
227-
// During parsing, we may sometimes need to 'unglue' a
228-
// glued token into two component tokens
229-
// (e.g. '>>' into '>' and '>), so that the parser
230-
// can consume them one at a time. This process
231-
// bypasses the normal capturing mechanism
232-
// (e.g. `num_next_calls` will not be incremented),
233-
// since the 'unglued' tokens due not exist in
234-
// the original `TokenStream`.
235-
//
236-
// If we end up consuming both unglued tokens,
237-
// then this is not an issue - we'll end up
238-
// capturing the single 'glued' token.
239-
//
240-
// However, in certain circumstances, we may
241-
// want to capture just the first 'unglued' token.
242-
// For example, capturing the `Vec<u8>`
243-
// in `Option<Vec<u8>>` requires us to unglue
244-
// the trailing `>>` token. The `break_last_token`
245-
// field is used to track this token - it gets
246-
// appended to the captured stream when
247-
// we evaluate a `LazyAttrTokenStream`.
248-
break_last_token: bool,
249241
}
250242

251243
impl TokenCursor {
@@ -396,12 +388,9 @@ impl<'a> Parser<'a> {
396388
capture_cfg: false,
397389
restrictions: Restrictions::empty(),
398390
expected_tokens: Vec::new(),
399-
token_cursor: TokenCursor {
400-
tree_cursor: stream.into_trees(),
401-
stack: Vec::new(),
402-
break_last_token: false,
403-
},
391+
token_cursor: TokenCursor { tree_cursor: stream.into_trees(), stack: Vec::new() },
404392
num_bump_calls: 0,
393+
break_last_token: false,
405394
unmatched_angle_bracket_count: 0,
406395
max_angle_bracket_count: 0,
407396
last_unexpected_token_span: None,
@@ -704,7 +693,7 @@ impl<'a> Parser<'a> {
704693
// If we consume any additional tokens, then this token
705694
// is not needed (we'll capture the entire 'glued' token),
706695
// and `bump` will set this field to `None`
707-
self.token_cursor.break_last_token = true;
696+
self.break_last_token = true;
708697
// Use the spacing of the glued token as the spacing
709698
// of the unglued second token.
710699
self.bump_with((Token::new(second, second_span), self.token_spacing));
@@ -1050,7 +1039,7 @@ impl<'a> Parser<'a> {
10501039
// We've retrieved an token from the underlying
10511040
// cursor, so we no longer need to worry about
10521041
// an unglued token. See `break_and_eat` for more details
1053-
self.token_cursor.break_last_token = false;
1042+
self.break_last_token = false;
10541043
if next.0.span.is_dummy() {
10551044
// Tweak the location for better diagnostics, but keep syntactic context intact.
10561045
let fallback_span = self.token.span;

0 commit comments

Comments
 (0)