Use default field values in Parser

This commit is contained in:
Esteban Küber 2026-01-15 19:46:54 +00:00
parent d194795f14
commit c6c4372f82
3 changed files with 16 additions and 29 deletions

View file

@ -625,12 +625,12 @@ impl TokenKind {
}
impl Token {
pub fn new(kind: TokenKind, span: Span) -> Self {
pub const fn new(kind: TokenKind, span: Span) -> Self {
Token { kind, span }
}
/// Some token that will be thrown away later.
pub fn dummy() -> Self {
pub const fn dummy() -> Self {
Token::new(TokenKind::Question, DUMMY_SP)
}

View file

@ -175,17 +175,17 @@ pub enum Recovery {
pub struct Parser<'a> {
pub psess: &'a ParseSess,
/// The current token.
pub token: Token,
pub token: Token = Token::dummy(),
/// The spacing for the current token.
token_spacing: Spacing,
token_spacing: Spacing = Spacing::Alone,
/// The previous token.
pub prev_token: Token,
pub capture_cfg: bool,
restrictions: Restrictions,
expected_token_types: TokenTypeSet,
pub prev_token: Token = Token::dummy(),
pub capture_cfg: bool = false,
restrictions: Restrictions = Restrictions::empty(),
expected_token_types: TokenTypeSet = TokenTypeSet::new(),
token_cursor: TokenCursor,
// The number of calls to `bump`, i.e. the position in the token stream.
num_bump_calls: u32,
num_bump_calls: u32 = 0,
// During parsing we may sometimes need to "unglue" a glued token into two
// or three component tokens (e.g. `>>` into `>` and `>`, or `>>=` into `>`
// and `>` and `=`), so the parser can consume them one at a time. This
@ -204,27 +204,27 @@ pub struct Parser<'a> {
//
// This value is always 0, 1, or 2. It can only reach 2 when splitting
// `>>=` or `<<=`.
break_last_token: u32,
break_last_token: u32 = 0,
/// This field is used to keep track of how many left angle brackets we have seen. This is
/// required in order to detect extra leading left angle brackets (`<` characters) and error
/// appropriately.
///
/// See the comments in the `parse_path_segment` function for more details.
unmatched_angle_bracket_count: u16,
angle_bracket_nesting: u16,
unmatched_angle_bracket_count: u16 = 0,
angle_bracket_nesting: u16 = 0,
/// Keep track of when we're within `<...>` for proper error recovery.
parsing_generics: bool = false,
last_unexpected_token_span: Option<Span>,
last_unexpected_token_span: Option<Span> = None,
/// If present, this `Parser` is not parsing Rust code but rather a macro call.
subparser_name: Option<&'static str>,
capture_state: CaptureState,
/// This allows us to recover when the user forget to add braces around
/// multiple statements in the closure body.
current_closure: Option<ClosureSpans>,
current_closure: Option<ClosureSpans> = None,
/// Whether the parser is allowed to do recovery.
/// This is disabled when parsing macro arguments, see #103534
recovery: Recovery,
recovery: Recovery = Recovery::Allowed,
}
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with
@ -353,18 +353,7 @@ impl<'a> Parser<'a> {
) -> Self {
let mut parser = Parser {
psess,
token: Token::dummy(),
token_spacing: Spacing::Alone,
prev_token: Token::dummy(),
capture_cfg: false,
restrictions: Restrictions::empty(),
expected_token_types: TokenTypeSet::new(),
token_cursor: TokenCursor { curr: TokenTreeCursor::new(stream), stack: Vec::new() },
num_bump_calls: 0,
break_last_token: 0,
unmatched_angle_bracket_count: 0,
angle_bracket_nesting: 0,
last_unexpected_token_span: None,
subparser_name,
capture_state: CaptureState {
capturing: Capturing::No,
@ -372,8 +361,6 @@ impl<'a> Parser<'a> {
inner_attr_parser_ranges: Default::default(),
seen_attrs: IntervalSet::new(u32::MAX as usize),
},
current_closure: None,
recovery: Recovery::Allowed,
..
};

View file

@ -585,7 +585,7 @@ macro_rules! exp {
pub(super) struct TokenTypeSet(u128);
impl TokenTypeSet {
pub(super) fn new() -> TokenTypeSet {
pub(super) const fn new() -> TokenTypeSet {
TokenTypeSet(0)
}