Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/annealer_corpus/rustc_parse.rs

69.1 KiB, 1 run

created by r1870400018:11814, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1pub mod attr;
2mod attr_wrapper;
3mod diagnostics;
4mod expr;
5mod generics;
6mod item;
7mod nonterminal;
8mod pat;
9mod path;
10mod stmt;
11pub mod token_type;
12mod ty;
13
14// Parsers for non-functionlike builtin macros are defined in rustc_parse so they can be used by
15// both rustc_builtin_macros and rustfmt.
16pub mod asm;
17pub mod cfg_select;
18
19use std::{debug_assert_matches, fmt, mem, slice};
20
21use attr_wrapper::{AttrWrapper, UsePreAttrPos};
22pub use diagnostics::AttemptLocalParseRecovery;
23// Public to use it for custom `if` expressions in rustfmt forks like https://github.com/tucant/rustfmt
24pub use expr::LetChainsPolicy;
25pub(crate) use item::{FnContext, FnParseMode};
26pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
27pub use path::PathStyle;
28use rustc_ast::token::{
29 self, IdentIsRaw, InvisibleOrigin, MetaVarKind, NtExprKind, NtPatKind, Token, TokenKind,
30};
31use rustc_ast::tokenstream::{
32 ParserRange, ParserReplacement, Spacing, TokenCursor, TokenStream, TokenTree, TokenTreeCursor,
33};
34use rustc_ast::util::case::Case;
35use rustc_ast::util::classify;
36use rustc_ast::{
37 self as ast, AnonConst, AttrArgs, AttrId, BinOpKind, ByRef, Const, CoroutineKind,
38 DUMMY_NODE_ID, DelimArgs, Expr, ExprKind, Extern, HasAttrs, HasTokens, ImplRestriction,
39 MgcaDisambiguation, Mutability, Recovered, RestrictionKind, Safety, StrLit, Visibility,
40 VisibilityKind,
41};
42use rustc_ast_pretty::pprust;
43use rustc_data_structures::fx::FxHashMap;
44use rustc_errors::{Applicability, Diag, FatalError, MultiSpan, PResult};
45use rustc_index::interval::IntervalSet;
46use rustc_session::parse::ParseSess;
47use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
48use thin_vec::ThinVec;
49use token_type::TokenTypeSet;
50pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
51use tracing::debug;
52
53use crate::errors::{
54 self, IncorrectImplRestriction, IncorrectVisibilityRestriction, NonStringAbiLiteral,
55 TokenDescription,
56};
57use crate::exp;
58
59#[cfg(test)]
60mod tests;
61
62// Ideally, these tests would be in `rustc_ast`. But they depend on having a
63// parser, so they are here.
64#[cfg(test)]
65mod tokenstream {
66 mod tests;
67}
68
69bitflags::bitflags! {
70 /// Restrictions applied while parsing.
71 ///
72 /// The parser maintains a bitset of restrictions it will honor while
73 /// parsing. This is essentially used as a way of tracking state of what
74 /// is being parsed and to change behavior based on that.
75 #[derive(Clone, Copy, Debug)]
76 struct Restrictions: u8 {
77 /// Restricts expressions for use in statement position.
78 ///
79 /// When expressions are used in various places, like statements or
80 /// match arms, this is used to stop parsing once certain tokens are
81 /// reached.
82 ///
83 /// For example, `if true {} & 1` with `STMT_EXPR` in effect is parsed
84 /// as two separate expression statements (`if` and a reference to 1).
85 /// Otherwise it is parsed as a bitwise AND where `if` is on the left
86 /// and 1 is on the right.
87 const STMT_EXPR = 1 << 0;
88 /// Do not allow struct literals.
89 ///
90 /// There are several places in the grammar where we don't want to
91 /// allow struct literals because they can require lookahead, or
92 /// otherwise could be ambiguous or cause confusion. For example,
93 /// `if Foo {} {}` isn't clear if it is `Foo{}` struct literal, or
94 /// just `Foo` is the condition, followed by a consequent block,
95 /// followed by an empty block.
96 ///
97 /// See [RFC 92](https://rust-lang.github.io/rfcs/0092-struct-grammar.html).
98 const NO_STRUCT_LITERAL = 1 << 1;
99 /// Used to provide better error messages for const generic arguments.
100 ///
101 /// An un-braced const generic argument is limited to a very small
102 /// subset of expressions. This is used to detect the situation where
103 /// an expression outside of that subset is used, and to suggest to
104 /// wrap the expression in braces.
105 const CONST_EXPR = 1 << 2;
106 /// Allows `let` expressions.
107 ///
108 /// `let pattern = scrutinee` is parsed as an expression, but it is
109 /// only allowed in let chains (`if` and `while` conditions).
110 /// Otherwise it is not an expression (note that `let` in statement
111 /// positions is treated as a `StmtKind::Let` statement, which has a
112 /// slightly different grammar).
113 const ALLOW_LET = 1 << 3;
114 /// Used to detect a missing `=>` in a match guard.
115 ///
116 /// This is used for error handling in a match guard to give a better
117 /// error message if the `=>` is missing. It is set when parsing the
118 /// guard expression.
119 const IN_IF_GUARD = 1 << 4;
120 /// Used to detect the incorrect use of expressions in patterns.
121 ///
122 /// This is used for error handling while parsing a pattern. During
123 /// error recovery, this will be set to try to parse the pattern as an
124 /// expression, but halts parsing the expression when reaching certain
125 /// tokens like `=`.
126 const IS_PAT = 1 << 5;
127 }
128}
129
130#[derive(Clone, Copy, PartialEq, Debug)]
131enum SemiColonMode {
132 Break,
133 Ignore,
134 Comma,
135}
136
137#[derive(Clone, Copy, PartialEq, Debug)]
138enum BlockMode {
139 Break,
140 Ignore,
141}
142
143/// Whether or not we should force collection of tokens for an AST node,
144/// regardless of whether or not it has attributes
145#[derive(Clone, Copy, Debug, PartialEq)]
146pub enum ForceCollect {
147 Yes,
148 No,
149}
150
151/// Whether to accept `const { ... }` as a shorthand for `const _: () = const { ... }`.
152#[derive(Clone, Copy, Debug, PartialEq, Eq)]
153pub enum AllowConstBlockItems {
154 Yes,
155 No,
156 DoesNotMatter,
157}
158
159/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
160#[macro_export]
161macro_rules! maybe_recover_from_interpolated_ty_qpath {
162 ($self: expr, $allow_qpath_recovery: expr) => {
163 if $allow_qpath_recovery
164 && $self.may_recover()
165 && let Some(mv_kind) = $self.token.is_metavar_seq()
166 && let token::MetaVarKind::Ty { .. } = mv_kind
167 && $self.check_noexpect_past_close_delim(&token::PathSep)
168 {
169 // Reparse the type, then move to recovery.
170 let ty = $self
171 .eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
172 .expect("metavar seq ty");
173
174 return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
175 }
176 };
177}
178
179#[derive(Clone, Copy, Debug)]
180pub enum Recovery {
181 Allowed,
182 Forbidden,
183}
184
185#[derive(Clone)]
186pub struct Parser<'a> {
187 pub psess: &'a ParseSess,
188 /// The current token.
189 pub token: Token = Token::dummy(),
190 /// The spacing for the current token.
191 token_spacing: Spacing = Spacing::Alone,
192 /// The previous token.
193 pub prev_token: Token = Token::dummy(),
194 pub capture_cfg: bool = false,
195 restrictions: Restrictions = Restrictions::empty(),
196 expected_token_types: TokenTypeSet = TokenTypeSet::new(),
197 token_cursor: TokenCursor,
198 // The number of calls to `bump`, i.e. the position in the token stream.
199 num_bump_calls: u32 = 0,
200 // During parsing we may sometimes need to "unglue" a glued token into two
201 // or three component tokens (e.g. `>>` into `>` and `>`, or `>>=` into `>`
202 // and `>` and `=`), so the parser can consume them one at a time. This
203 // process bypasses the normal capturing mechanism (e.g. `num_bump_calls`
204 // will not be incremented), since the "unglued" tokens due not exist in
205 // the original `TokenStream`.
206 //
207 // If we end up consuming all the component tokens, this is not an issue,
208 // because we'll end up capturing the single "glued" token.
209 //
210 // However, sometimes we may want to capture not all of the original
211 // token. For example, capturing the `Vec<u8>` in `Option<Vec<u8>>`
212 // requires us to unglue the trailing `>>` token. The `break_last_token`
213 // field is used to track these tokens. They get appended to the captured
214 // stream when we evaluate a `LazyAttrTokenStream`.
215 //
216 // This value is always 0, 1, or 2. It can only reach 2 when splitting
217 // `>>=` or `<<=`.
218 break_last_token: u32 = 0,
219 /// This field is used to keep track of how many left angle brackets we have seen. This is
220 /// required in order to detect extra leading left angle brackets (`<` characters) and error
221 /// appropriately.
222 ///
223 /// See the comments in the `parse_path_segment` function for more details.
224 unmatched_angle_bracket_count: u16 = 0,
225 angle_bracket_nesting: u16 = 0,
226 /// Keep track of when we're within `<...>` for proper error recovery.
227 parsing_generics: bool = false,
228
229 last_unexpected_token_span: Option<Span> = None,
230 /// If present, this `Parser` is not parsing Rust code but rather a macro call.
231 subparser_name: Option<&'static str>,
232 capture_state: CaptureState,
233 /// This allows us to recover when the user forget to add braces around
234 /// multiple statements in the closure body.
235 current_closure: Option<ClosureSpans> = None,
236 /// Whether the parser is allowed to do recovery.
237 /// This is disabled when parsing macro arguments, see #103534
238 recovery: Recovery = Recovery::Allowed,
239 /// Whether we're parsing a function body.
240 in_fn_body: bool = false,
241 /// Whether we have detected a missing semicolon in function body.
242 pub fn_body_missing_semi_guar: Option<ErrorGuaranteed> = None,
243}
244
245// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with
246// nonterminals. Make sure it doesn't unintentionally get bigger. We only check a few arches
247// though, because `TokenTypeSet(u128)` alignment varies on others, changing the total size.
248#[cfg(all(target_pointer_width = "64", any(target_arch = "aarch64", target_arch = "x86_64")))]
249rustc_data_structures::static_assert_size!(Parser<'_>, 288);
250
251/// Stores span information about a closure.
252#[derive(Clone, Debug)]
253struct ClosureSpans {
254 whole_closure: Span,
255 closing_pipe: Span,
256 body: Span,
257}
258
259/// Controls how we capture tokens. Capturing can be expensive,
260/// so we try to avoid performing capturing in cases where
261/// we will never need an `AttrTokenStream`.
262#[derive(Copy, Clone, Debug)]
263enum Capturing {
264 /// We aren't performing any capturing - this is the default mode.
265 No,
266 /// We are capturing tokens
267 Yes,
268}
269
270// This state is used by `Parser::collect_tokens`.
271#[derive(Clone, Debug)]
272struct CaptureState {
273 capturing: Capturing,
274 parser_replacements: Vec<ParserReplacement>,
275 inner_attr_parser_ranges: FxHashMap<AttrId, ParserRange>,
276 // `IntervalSet` is good for perf because attrs are mostly added to this
277 // set in contiguous ranges.
278 seen_attrs: IntervalSet<AttrId>,
279}
280
281/// A sequence separator.
282#[derive(Debug)]
283struct SeqSep {
284 /// The separator token.
285 sep: Option<ExpTokenPair>,
286 /// `true` if a trailing separator is allowed.
287 trailing_sep_allowed: bool,
288}
289
290impl SeqSep {
291 fn trailing_allowed(sep: ExpTokenPair) -> SeqSep {
292 SeqSep { sep: Some(sep), trailing_sep_allowed: true }
293 }
294
295 fn none() -> SeqSep {
296 SeqSep { sep: None, trailing_sep_allowed: false }
297 }
298}
299
300#[derive(Debug)]
301pub enum FollowedByType {
302 Yes,
303 No,
304}
305
306#[derive(Copy, Clone, Debug)]
307pub enum Trailing {
308 No,
309 Yes,
310}
311
312impl From<bool> for Trailing {
313 fn from(b: bool) -> Trailing {
314 if b { Trailing::Yes } else { Trailing::No }
315 }
316}
317
318pub fn token_descr(token: &Token) -> String {
319 let s = pprust::token_to_string(token).to_string();
320
321 match (TokenDescription::from_token(token), &token.kind) {
322 (Some(TokenDescription::ReservedIdentifier), _) => format!("reserved identifier `{s}`"),
323 (Some(TokenDescription::Keyword), _) => format!("keyword `{s}`"),
324 (Some(TokenDescription::ReservedKeyword), _) => format!("reserved keyword `{s}`"),
325 (Some(TokenDescription::DocComment), _) => format!("doc comment `{s}`"),
326 // Deliberately doesn't print `s`, which is empty.
327 (Some(TokenDescription::MetaVar(kind)), _) => format!("`{kind}` metavariable"),
328 (None, TokenKind::NtIdent(..)) => format!("identifier `{s}`"),
329 (None, TokenKind::NtLifetime(..)) => format!("lifetime `{s}`"),
330 (None, _) => format!("`{s}`"),
331 }
332}
333
334impl<'a> Parser<'a> {
335 pub fn new(
336 psess: &'a ParseSess,
337 stream: TokenStream,
338 subparser_name: Option<&'static str>,
339 ) -> Self {
340 let mut parser = Parser {
341 psess,
342 token_cursor: TokenCursor { curr: TokenTreeCursor::new(stream), stack: Vec::new() },
343 subparser_name,
344 capture_state: CaptureState {
345 capturing: Capturing::No,
346 parser_replacements: Vec::new(),
347 inner_attr_parser_ranges: Default::default(),
348 seen_attrs: IntervalSet::new(u32::MAX as usize),
349 },
350 ..
351 };
352
353 // Make parser point to the first token.
354 parser.bump();
355
356 // Change this from 1 back to 0 after the bump. This eases debugging of
357 // `Parser::collect_tokens` because 0-indexed token positions are nicer
358 // than 1-indexed token positions.
359 parser.num_bump_calls = 0;
360
361 parser
362 }
363
364 #[inline]
365 pub fn recovery(mut self, recovery: Recovery) -> Self {
366 self.recovery = recovery;
367 self
368 }
369
370 #[inline]
371 fn with_recovery<T>(&mut self, recovery: Recovery, f: impl FnOnce(&mut Self) -> T) -> T {
372 let old = mem::replace(&mut self.recovery, recovery);
373 let res = f(self);
374 self.recovery = old;
375 res
376 }
377
378 /// Whether the parser is allowed to recover from broken code.
379 ///
380 /// If this returns false, recovering broken code into valid code (especially if this recovery does lookahead)
381 /// is not allowed. All recovery done by the parser must be gated behind this check.
382 ///
383 /// Technically, this only needs to restrict eager recovery by doing lookahead at more tokens.
384 /// But making the distinction is very subtle, and simply forbidding all recovery is a lot simpler to uphold.
385 #[inline]
386 fn may_recover(&self) -> bool {
387 matches!(self.recovery, Recovery::Allowed)
388 }
389
390 /// Version of [`unexpected`](Parser::unexpected) that "returns" any type in the `Ok`
391 /// (both those functions never return "Ok", and so can lie like that in the type).
392 pub fn unexpected_any<T>(&mut self) -> PResult<'a, T> {
393 match self.expect_one_of(&[], &[]) {
394 Err(e) => Err(e),
395 // We can get `Ok(true)` from `recover_closing_delimiter`
396 // which is called in `expected_one_of_not_found`.
397 Ok(_) => FatalError.raise(),
398 }
399 }
400
401 pub fn unexpected(&mut self) -> PResult<'a, ()> {
402 self.unexpected_any()
403 }
404
405 /// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
406 pub fn expect(&mut self, exp: ExpTokenPair) -> PResult<'a, Recovered> {
407 if self.expected_token_types.is_empty() {
408 if self.token == exp.tok {
409 self.bump();
410 Ok(Recovered::No)
411 } else {
412 self.unexpected_try_recover(&exp.tok)
413 }
414 } else {
415 self.expect_one_of(slice::from_ref(&exp), &[])
416 }
417 }
418
419 /// Expect next token to be edible or inedible token. If edible,
420 /// then consume it; if inedible, then return without consuming
421 /// anything. Signal a fatal error if next token is unexpected.
422 fn expect_one_of(
423 &mut self,
424 edible: &[ExpTokenPair],
425 inedible: &[ExpTokenPair],
426 ) -> PResult<'a, Recovered> {
427 if edible.iter().any(|exp| exp.tok == self.token.kind) {
428 self.bump();
429 Ok(Recovered::No)
430 } else if inedible.iter().any(|exp| exp.tok == self.token.kind) {
431 // leave it in the input
432 Ok(Recovered::No)
433 } else if self.token != token::Eof
434 && self.last_unexpected_token_span == Some(self.token.span)
435 {
436 FatalError.raise();
437 } else {
438 self.expected_one_of_not_found(edible, inedible)
439 .map(|error_guaranteed| Recovered::Yes(error_guaranteed))
440 }
441 }
442
443 // Public for rustfmt usage.
444 pub fn parse_ident(&mut self) -> PResult<'a, Ident> {
445 self.parse_ident_common(self.may_recover())
446 }
447
448 pub(crate) fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
449 let (ident, is_raw) = self.ident_or_err(recover)?;
450
451 if is_raw == IdentIsRaw::No && ident.is_reserved() {
452 let err = self.expected_ident_found_err();
453 if recover {
454 err.emit();
455 } else {
456 return Err(err);
457 }
458 }
459 self.bump();
460 Ok(ident)
461 }
462
463 fn ident_or_err(&mut self, recover: bool) -> PResult<'a, (Ident, IdentIsRaw)> {
464 match self.token.ident() {
465 Some(ident) => Ok(ident),
466 None => self.expected_ident_found(recover),
467 }
468 }
469
470 /// Checks if the next token is `tok`, and returns `true` if so.
471 ///
472 /// This method will automatically add `tok` to `expected_token_types` if `tok` is not
473 /// encountered.
474 #[inline]
475 pub fn check(&mut self, exp: ExpTokenPair) -> bool {
476 let is_present = self.token == exp.tok;
477 if !is_present {
478 self.expected_token_types.insert(exp.token_type);
479 }
480 is_present
481 }
482
483 #[inline]
484 #[must_use]
485 fn check_noexpect(&self, tok: &TokenKind) -> bool {
486 self.token == *tok
487 }
488
489 // Check the first token after the delimiter that closes the current
490 // delimited sequence. (Panics if used in the outermost token stream, which
491 // has no delimiters.) It uses a clone of the relevant tree cursor to skip
492 // past the entire `TokenTree::Delimited` in a single step, avoiding the
493 // need for unbounded token lookahead.
494 //
495 // Primarily used when `self.token` matches `OpenInvisible(_))`, to look
496 // ahead through the current metavar expansion.
497 fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
498 let mut tree_cursor = self.token_cursor.stack.last().unwrap().clone();
499 tree_cursor.bump();
500 matches!(
501 tree_cursor.curr(),
502 Some(TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
503 )
504 }
505
506 /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
507 ///
508 /// the main purpose of this function is to reduce the cluttering of the suggestions list
509 /// which using the normal eat method could introduce in some cases.
510 #[inline]
511 #[must_use]
512 fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
513 let is_present = self.check_noexpect(tok);
514 if is_present {
515 self.bump()
516 }
517 is_present
518 }
519
520 /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
521 #[inline]
522 #[must_use]
523 pub fn eat(&mut self, exp: ExpTokenPair) -> bool {
524 let is_present = self.check(exp);
525 if is_present {
526 self.bump()
527 }
528 is_present
529 }
530
531 /// If the next token is the given keyword, returns `true` without eating it.
532 /// An expectation is also added for diagnostics purposes.
533 #[inline]
534 #[must_use]
535 fn check_keyword(&mut self, exp: ExpKeywordPair) -> bool {
536 let is_keyword = self.token.is_keyword(exp.kw);
537 if !is_keyword {
538 self.expected_token_types.insert(exp.token_type);
539 }
540 is_keyword
541 }
542
543 #[inline]
544 #[must_use]
545 fn check_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
546 if self.check_keyword(exp) {
547 true
548 } else if case == Case::Insensitive
549 && let Some((ident, IdentIsRaw::No)) = self.token.ident()
550 // Do an ASCII case-insensitive match, because all keywords are ASCII.
551 && ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
552 {
553 true
554 } else {
555 false
556 }
557 }
558
559 /// If the next token is the given keyword, eats it and returns `true`.
560 /// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
561 // Public for rustc_builtin_macros and rustfmt usage.
562 #[inline]
563 #[must_use]
564 pub fn eat_keyword(&mut self, exp: ExpKeywordPair) -> bool {
565 let is_keyword = self.check_keyword(exp);
566 if is_keyword {
567 self.bump();
568 }
569 is_keyword
570 }
571
572 /// Eats a keyword, optionally ignoring the case.
573 /// If the case differs (and is ignored) an error is issued.
574 /// This is useful for recovery.
575 #[inline]
576 #[must_use]
577 fn eat_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
578 if self.eat_keyword(exp) {
579 true
580 } else if case == Case::Insensitive
581 && let Some((ident, IdentIsRaw::No)) = self.token.ident()
582 // Do an ASCII case-insensitive match, because all keywords are ASCII.
583 && ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
584 {
585 let kw = exp.kw.as_str();
586 let is_upper = kw.chars().all(char::is_uppercase);
587 let is_lower = kw.chars().all(char::is_lowercase);
588
589 let case = match (is_upper, is_lower) {
590 (true, true) => {
591 unreachable!("keyword that is both fully upper- and fully lowercase")
592 }
593 (true, false) => errors::Case::Upper,
594 (false, true) => errors::Case::Lower,
595 (false, false) => errors::Case::Mixed,
596 };
597
598 self.dcx().emit_err(errors::KwBadCase { span: ident.span, kw, case });
599 self.bump();
600 true
601 } else {
602 false
603 }
604 }
605
606 /// If the next token is the given keyword, eats it and returns `true`.
607 /// Otherwise, returns `false`. No expectation is added.
608 // Public for rustc_builtin_macros usage.
609 #[inline]
610 #[must_use]
611 pub fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
612 let is_keyword = self.token.is_keyword(kw);
613 if is_keyword {
614 self.bump();
615 }
616 is_keyword
617 }
618
619 /// If the given word is not a keyword, signals an error.
620 /// If the next token is not the given word, signals an error.
621 /// Otherwise, eats it.
622 pub fn expect_keyword(&mut self, exp: ExpKeywordPair) -> PResult<'a, ()> {
623 if !self.eat_keyword(exp) { self.unexpected() } else { Ok(()) }
624 }
625
626 /// Consume a sequence produced by a metavar expansion, if present.
627 pub fn eat_metavar_seq<T>(
628 &mut self,
629 mv_kind: MetaVarKind,
630 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
631 ) -> Option<T> {
632 self.eat_metavar_seq_with_matcher(|mvk| mvk == mv_kind, f)
633 }
634
635 /// A slightly more general form of `eat_metavar_seq`, for use with the
636 /// `MetaVarKind` variants that have parameters, where an exact match isn't
637 /// desired.
638 fn eat_metavar_seq_with_matcher<T>(
639 &mut self,
640 match_mv_kind: impl Fn(MetaVarKind) -> bool,
641 mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
642 ) -> Option<T> {
643 if let token::OpenInvisible(InvisibleOrigin::MetaVar(mv_kind)) = self.token.kind
644 && match_mv_kind(mv_kind)
645 {
646 self.bump();
647
648 // Recovery is disabled when parsing macro arguments, so it must
649 // also be disabled when reparsing pasted macro arguments,
650 // otherwise we get inconsistent results (e.g. #137874).
651 let res = self.with_recovery(Recovery::Forbidden, |this| f(this));
652
653 let res = match res {
654 Ok(res) => res,
655 Err(err) => {
656 // This can occur in unusual error cases, e.g. #139445.
657 err.delay_as_bug();
658 return None;
659 }
660 };
661
662 if let token::CloseInvisible(InvisibleOrigin::MetaVar(mv_kind)) = self.token.kind
663 && match_mv_kind(mv_kind)
664 {
665 self.bump();
666 Some(res)
667 } else {
668 // This can occur when invalid syntax is passed to a decl macro. E.g. see #139248,
669 // where the reparse attempt of an invalid expr consumed the trailing invisible
670 // delimiter.
671 self.dcx()
672 .span_delayed_bug(self.token.span, "no close delim with reparsing {mv_kind:?}");
673 None
674 }
675 } else {
676 None
677 }
678 }
679
680 /// Is the given keyword `kw` followed by a non-reserved identifier?
681 fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
682 self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_non_reserved_ident())
683 }
684
685 #[inline]
686 fn check_or_expected(&mut self, ok: bool, token_type: TokenType) -> bool {
687 if !ok {
688 self.expected_token_types.insert(token_type);
689 }
690 ok
691 }
692
693 fn check_ident(&mut self) -> bool {
694 self.check_or_expected(self.token.is_ident(), TokenType::Ident)
695 }
696
697 fn check_path(&mut self) -> bool {
698 self.check_or_expected(self.token.is_path_start(), TokenType::Path)
699 }
700
701 fn check_type(&mut self) -> bool {
702 self.check_or_expected(self.token.can_begin_type(), TokenType::Type)
703 }
704
705 fn check_const_arg(&mut self) -> bool {
706 let is_mcg_arg = self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const);
707 let is_mgca_arg = self.is_keyword_ahead(0, &[kw::Const])
708 && self.look_ahead(1, |t| *t == token::OpenBrace);
709 is_mcg_arg || is_mgca_arg
710 }
711
712 fn check_const_closure(&self) -> bool {
713 self.is_keyword_ahead(0, &[kw::Const])
714 && self.look_ahead(1, |t| match &t.kind {
715 // async closures do not work with const closures, so we do not parse that here.
716 token::Ident(kw::Move | kw::Use | kw::Static, IdentIsRaw::No)
717 | token::OrOr
718 | token::Or => true,
719 _ => false,
720 })
721 }
722
723 fn check_inline_const(&self, dist: usize) -> bool {
724 self.is_keyword_ahead(dist, &[kw::Const])
725 && self.look_ahead(dist + 1, |t| match &t.kind {
726 token::OpenBrace => true,
727 token::OpenInvisible(InvisibleOrigin::MetaVar(MetaVarKind::Block)) => true,
728 _ => false,
729 })
730 }
731
732 /// Checks to see if the next token is either `+` or `+=`.
733 /// Otherwise returns `false`.
734 #[inline]
735 fn check_plus(&mut self) -> bool {
736 self.check_or_expected(self.token.is_like_plus(), TokenType::Plus)
737 }
738
739 /// Eats the expected token if it's present possibly breaking
740 /// compound tokens like multi-character operators in process.
741 /// Returns `true` if the token was eaten.
742 fn break_and_eat(&mut self, exp: ExpTokenPair) -> bool {
743 if self.token == exp.tok {
744 self.bump();
745 return true;
746 }
747 match self.token.kind.break_two_token_op(1) {
748 Some((first, second)) if first == exp.tok => {
749 let first_span = self.psess.source_map().start_point(self.token.span);
750 let second_span = self.token.span.with_lo(first_span.hi());
751 self.token = Token::new(first, first_span);
752 // Keep track of this token - if we end token capturing now,
753 // we'll want to append this token to the captured stream.
754 //
755 // If we consume any additional tokens, then this token
756 // is not needed (we'll capture the entire 'glued' token),
757 // and `bump` will set this field to 0.
758 self.break_last_token += 1;
759 // Use the spacing of the glued token as the spacing of the
760 // unglued second token.
761 self.bump_with((Token::new(second, second_span), self.token_spacing));
762 true
763 }
764 _ => {
765 self.expected_token_types.insert(exp.token_type);
766 false
767 }
768 }
769 }
770
771 /// Eats `+` possibly breaking tokens like `+=` in process.
772 fn eat_plus(&mut self) -> bool {
773 self.break_and_eat(exp!(Plus))
774 }
775
776 /// Eats `&` possibly breaking tokens like `&&` in process.
777 /// Signals an error if `&` is not eaten.
778 fn expect_and(&mut self) -> PResult<'a, ()> {
779 if self.break_and_eat(exp!(And)) { Ok(()) } else { self.unexpected() }
780 }
781
782 /// Eats `|` possibly breaking tokens like `||` in process.
783 /// Signals an error if `|` was not eaten.
784 fn expect_or(&mut self) -> PResult<'a, ()> {
785 if self.break_and_eat(exp!(Or)) { Ok(()) } else { self.unexpected() }
786 }
787
788 /// Eats `<` possibly breaking tokens like `<<` in process.
789 fn eat_lt(&mut self) -> bool {
790 let ate = self.break_and_eat(exp!(Lt));
791 if ate {
792 // See doc comment for `unmatched_angle_bracket_count`.
793 self.unmatched_angle_bracket_count += 1;
794 debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
795 }
796 ate
797 }
798
799 /// Eats `<` possibly breaking tokens like `<<` in process.
800 /// Signals an error if `<` was not eaten.
801 fn expect_lt(&mut self) -> PResult<'a, ()> {
802 if self.eat_lt() { Ok(()) } else { self.unexpected() }
803 }
804
805 /// Eats `>` possibly breaking tokens like `>>` in process.
806 /// Signals an error if `>` was not eaten.
807 fn expect_gt(&mut self) -> PResult<'a, ()> {
808 if self.break_and_eat(exp!(Gt)) {
809 // See doc comment for `unmatched_angle_bracket_count`.
810 if self.unmatched_angle_bracket_count > 0 {
811 self.unmatched_angle_bracket_count -= 1;
812 debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
813 }
814 Ok(())
815 } else {
816 self.unexpected()
817 }
818 }
819
820 /// Checks if the next token is contained within `closes`, and returns `true` if so.
821 fn expect_any_with_type(
822 &mut self,
823 closes_expected: &[ExpTokenPair],
824 closes_not_expected: &[&TokenKind],
825 ) -> bool {
826 closes_expected.iter().any(|&close| self.check(close))
827 || closes_not_expected.iter().any(|k| self.check_noexpect(k))
828 }
829
830 /// Parses a sequence until the specified delimiters. The function
831 /// `f` must consume tokens until reaching the next separator or
832 /// closing bracket.
833 fn parse_seq_to_before_tokens<T>(
834 &mut self,
835 closes_expected: &[ExpTokenPair],
836 closes_not_expected: &[&TokenKind],
837 sep: SeqSep,
838 mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
839 ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
840 let mut first = true;
841 let mut recovered = Recovered::No;
842 let mut trailing = Trailing::No;
843 let mut v = ThinVec::new();
844
845 while !self.expect_any_with_type(closes_expected, closes_not_expected) {
846 if self.token.kind.is_close_delim_or_eof() {
847 break;
848 }
849 if let Some(exp) = sep.sep {
850 if first {
851 // no separator for the first element
852 first = false;
853 } else {
854 // check for separator
855 match self.expect(exp) {
856 Ok(Recovered::No) => {
857 self.current_closure.take();
858 }
859 Ok(Recovered::Yes(guar)) => {
860 self.current_closure.take();
861 recovered = Recovered::Yes(guar);
862 break;
863 }
864 Err(mut expect_err) => {
865 let sp = self.prev_token.span.shrink_to_hi();
866 let token_str = pprust::token_kind_to_string(&exp.tok);
867
868 match self.current_closure.take() {
869 Some(closure_spans) if self.token == TokenKind::Semi => {
870 // Finding a semicolon instead of a comma
871 // after a closure body indicates that the
872 // closure body may be a block but the user
873 // forgot to put braces around its
874 // statements.
875
876 self.recover_missing_braces_around_closure_body(
877 closure_spans,
878 expect_err,
879 )?;
880
881 continue;
882 }
883
884 _ => {
885 // Attempt to keep parsing if it was a similar separator.
886 if exp.tok.similar_tokens().contains(&self.token.kind) {
887 self.bump();
888 }
889 }
890 }
891
892 // If this was a missing `@` in a binding pattern
893 // bail with a suggestion
894 // https://github.com/rust-lang/rust/issues/72373
895 if self.prev_token.is_ident() && self.token == token::DotDot {
896 let msg = format!(
897 "if you meant to bind the contents of the rest of the array \
898 pattern into `{}`, use `@`",
899 pprust::token_to_string(&self.prev_token)
900 );
901 expect_err
902 .with_span_suggestion_verbose(
903 self.prev_token.span.shrink_to_hi().until(self.token.span),
904 msg,
905 " @ ",
906 Applicability::MaybeIncorrect,
907 )
908 .emit();
909 break;
910 }
911
912 // Attempt to keep parsing if it was an omitted separator.
913 self.last_unexpected_token_span = None;
914 match f(self) {
915 Ok(t) => {
916 // Parsed successfully, therefore most probably the code only
917 // misses a separator.
918 expect_err
919 .with_span_suggestion_short(
920 sp,
921 format!("missing `{token_str}`"),
922 token_str,
923 Applicability::MaybeIncorrect,
924 )
925 .emit();
926
927 v.push(t);
928 continue;
929 }
930 Err(e) => {
931 // Parsing failed, therefore it must be something more serious
932 // than just a missing separator.
933 for xx in &e.children {
934 // Propagate the help message from sub error `e` to main
935 // error `expect_err`.
936 expect_err.children.push(xx.clone());
937 }
938 e.cancel();
939 if self.token == token::Colon {
940 // We will try to recover in
941 // `maybe_recover_struct_lit_bad_delims`.
942 return Err(expect_err);
943 } else if let [exp] = closes_expected
944 && exp.token_type == TokenType::CloseParen
945 {
946 return Err(expect_err);
947 } else {
948 expect_err.emit();
949 break;
950 }
951 }
952 }
953 }
954 }
955 }
956 }
957 if sep.trailing_sep_allowed
958 && self.expect_any_with_type(closes_expected, closes_not_expected)
959 {
960 trailing = Trailing::Yes;
961 break;
962 }
963
964 let t = f(self)?;
965 v.push(t);
966 }
967
968 Ok((v, trailing, recovered))
969 }
970
971 fn recover_missing_braces_around_closure_body(
972 &mut self,
973 closure_spans: ClosureSpans,
974 mut expect_err: Diag<'_>,
975 ) -> PResult<'a, ()> {
976 let initial_semicolon = self.token.span;
977
978 while self.eat(exp!(Semi)) {
979 let _ = self
980 .parse_stmt_without_recovery(false, ForceCollect::No, false)
981 .unwrap_or_else(|e| {
982 e.cancel();
983 None
984 });
985 }
986
987 expect_err
988 .primary_message("closure bodies that contain statements must be surrounded by braces");
989
990 let preceding_pipe_span = closure_spans.closing_pipe;
991 let following_token_span = self.token.span;
992
993 let mut first_note = MultiSpan::from(vec![initial_semicolon]);
994 first_note.push_span_label(
995 initial_semicolon,
996 "this `;` turns the preceding closure into a statement",
997 );
998 first_note.push_span_label(
999 closure_spans.body,
1000 "this expression is a statement because of the trailing semicolon",
1001 );
1002 expect_err.span_note(first_note, "statement found outside of a block");
1003
1004 let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
1005 second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
1006 second_note.push_span_label(
1007 following_token_span,
1008 "...but likely you meant the closure to end here",
1009 );
1010 expect_err.span_note(second_note, "the closure body may be incorrectly delimited");
1011
1012 expect_err.span(vec![preceding_pipe_span, following_token_span]);
1013
1014 let opening_suggestion_str = " {".to_string();
1015 let closing_suggestion_str = "}".to_string();
1016
1017 expect_err.multipart_suggestion(
1018 "try adding braces",
1019 vec![
1020 (preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
1021 (following_token_span.shrink_to_lo(), closing_suggestion_str),
1022 ],
1023 Applicability::MaybeIncorrect,
1024 );
1025
1026 expect_err.emit();
1027
1028 Ok(())
1029 }
1030
1031 /// Parses a sequence, not including the delimiters. The function
1032 /// `f` must consume tokens until reaching the next separator or
1033 /// closing bracket.
1034 fn parse_seq_to_before_end<T>(
1035 &mut self,
1036 close: ExpTokenPair,
1037 sep: SeqSep,
1038 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1039 ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
1040 self.parse_seq_to_before_tokens(&[close], &[], sep, f)
1041 }
1042
1043 /// Parses a sequence, including only the closing delimiter. The function
1044 /// `f` must consume tokens until reaching the next separator or
1045 /// closing bracket.
1046 fn parse_seq_to_end<T>(
1047 &mut self,
1048 close: ExpTokenPair,
1049 sep: SeqSep,
1050 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1051 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1052 let (val, trailing, recovered) = self.parse_seq_to_before_end(close, sep, f)?;
1053 if matches!(recovered, Recovered::No) && !self.eat(close) {
1054 self.dcx().span_delayed_bug(
1055 self.token.span,
1056 "recovered but `parse_seq_to_before_end` did not give us the close token",
1057 );
1058 }
1059 Ok((val, trailing))
1060 }
1061
1062 /// Parses a sequence, including both delimiters. The function
1063 /// `f` must consume tokens until reaching the next separator or
1064 /// closing bracket.
1065 fn parse_unspanned_seq<T>(
1066 &mut self,
1067 open: ExpTokenPair,
1068 close: ExpTokenPair,
1069 sep: SeqSep,
1070 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1071 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1072 self.expect(open)?;
1073 self.parse_seq_to_end(close, sep, f)
1074 }
1075
1076 /// Parses a comma-separated sequence, including both delimiters.
1077 /// The function `f` must consume tokens until reaching the next separator or
1078 /// closing bracket.
1079 fn parse_delim_comma_seq<T>(
1080 &mut self,
1081 open: ExpTokenPair,
1082 close: ExpTokenPair,
1083 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1084 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1085 self.parse_unspanned_seq(open, close, SeqSep::trailing_allowed(exp!(Comma)), f)
1086 }
1087
1088 /// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
1089 /// The function `f` must consume tokens until reaching the next separator or
1090 /// closing bracket.
1091 pub fn parse_paren_comma_seq<T>(
1092 &mut self,
1093 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1094 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1095 self.parse_delim_comma_seq(exp!(OpenParen), exp!(CloseParen), f)
1096 }
1097
1098 /// Advance the parser by one token using provided token as the next one.
1099 fn bump_with(&mut self, next: (Token, Spacing)) {
1100 self.inlined_bump_with(next)
1101 }
1102
1103 /// This always-inlined version should only be used on hot code paths.
1104 #[inline(always)]
1105 fn inlined_bump_with(&mut self, (next_token, next_spacing): (Token, Spacing)) {
1106 // Update the current and previous tokens.
1107 self.prev_token = mem::replace(&mut self.token, next_token);
1108 self.token_spacing = next_spacing;
1109
1110 // Diagnostics.
1111 self.expected_token_types.clear();
1112 }
1113
1114 /// Advance the parser by one token.
1115 pub fn bump(&mut self) {
1116 // Note: destructuring here would give nicer code, but it was found in #96210 to be slower
1117 // than `.0`/`.1` access.
1118 let mut next = self.token_cursor.inlined_next();
1119 self.num_bump_calls += 1;
1120 // We got a token from the underlying cursor and no longer need to
1121 // worry about an unglued token. See `break_and_eat` for more details.
1122 self.break_last_token = 0;
1123 if next.0.span.is_dummy() {
1124 // Tweak the location for better diagnostics, but keep syntactic context intact.
1125 let fallback_span = self.token.span;
1126 next.0.span = fallback_span.with_ctxt(next.0.span.ctxt());
1127 }
1128 debug_assert!(!matches!(
1129 next.0.kind,
1130 token::OpenInvisible(origin) | token::CloseInvisible(origin) if origin.skip()
1131 ));
1132 self.inlined_bump_with(next)
1133 }
1134
1135 /// Look-ahead `dist` tokens of `self.token` and get access to that token there.
1136 /// When `dist == 0` then the current token is looked at. `Eof` will be
1137 /// returned if the look-ahead is any distance past the end of the tokens.
1138 pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
1139 if dist == 0 {
1140 return looker(&self.token);
1141 }
1142
1143 // Typically around 98% of the `dist > 0` cases have `dist == 1`, so we
1144 // have a fast special case for that.
1145 if dist == 1 {
1146 // The index is zero because the tree cursor's index always points
1147 // to the next token to be gotten.
1148 match self.token_cursor.curr.curr() {
1149 Some(tree) => {
1150 // Indexing stayed within the current token tree.
1151 match tree {
1152 TokenTree::Token(token, _) => return looker(token),
1153 &TokenTree::Delimited(dspan, _, delim, _) => {
1154 if !delim.skip() {
1155 return looker(&Token::new(delim.as_open_token_kind(), dspan.open));
1156 }
1157 }
1158 }
1159 }
1160 None => {
1161 // The tree cursor lookahead went (one) past the end of the
1162 // current token tree. Try to return a close delimiter.
1163 if let Some(last) = self.token_cursor.stack.last()
1164 && let Some(&TokenTree::Delimited(span, _, delim, _)) = last.curr()
1165 && !delim.skip()
1166 {
1167 // We are not in the outermost token stream, so we have
1168 // delimiters. Also, those delimiters are not skipped.
1169 return looker(&Token::new(delim.as_close_token_kind(), span.close));
1170 }
1171 }
1172 }
1173 }
1174
1175 // Just clone the token cursor and use `next`, skipping delimiters as
1176 // necessary. Slow but simple.
1177 let mut cursor = self.token_cursor.clone();
1178 let mut i = 0;
1179 let mut token = Token::dummy();
1180 while i < dist {
1181 token = cursor.next().0;
1182 if let token::OpenInvisible(origin) | token::CloseInvisible(origin) = token.kind
1183 && origin.skip()
1184 {
1185 continue;
1186 }
1187 i += 1;
1188 }
1189 looker(&token)
1190 }
1191
1192 /// Like `lookahead`, but skips over token trees rather than tokens. Useful
1193 /// when looking past possible metavariable pasting sites.
1194 pub fn tree_look_ahead<R>(
1195 &self,
1196 dist: usize,
1197 looker: impl FnOnce(&TokenTree) -> R,
1198 ) -> Option<R> {
1199 assert_ne!(dist, 0);
1200 self.token_cursor.curr.look_ahead(dist - 1).map(looker)
1201 }
1202
1203 /// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
1204 pub(crate) fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
1205 self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
1206 }
1207
1208 /// Parses asyncness: `async` or nothing.
1209 fn parse_coroutine_kind(&mut self, case: Case) -> Option<CoroutineKind> {
1210 let span = self.token_uninterpolated_span();
1211 if self.eat_keyword_case(exp!(Async), case) {
1212 // FIXME(gen_blocks): Do we want to unconditionally parse `gen` and then
1213 // error if edition <= 2024, like we do with async and edition <= 2018?
1214 if self.token_uninterpolated_span().at_least_rust_2024()
1215 && self.eat_keyword_case(exp!(Gen), case)
1216 {
1217 let gen_span = self.prev_token_uninterpolated_span();
1218 Some(CoroutineKind::AsyncGen {
1219 span: span.to(gen_span),
1220 closure_id: DUMMY_NODE_ID,
1221 return_impl_trait_id: DUMMY_NODE_ID,
1222 })
1223 } else {
1224 Some(CoroutineKind::Async {
1225 span,
1226 closure_id: DUMMY_NODE_ID,
1227 return_impl_trait_id: DUMMY_NODE_ID,
1228 })
1229 }
1230 } else if self.token_uninterpolated_span().at_least_rust_2024()
1231 && self.eat_keyword_case(exp!(Gen), case)
1232 {
1233 Some(CoroutineKind::Gen {
1234 span,
1235 closure_id: DUMMY_NODE_ID,
1236 return_impl_trait_id: DUMMY_NODE_ID,
1237 })
1238 } else {
1239 None
1240 }
1241 }
1242
1243 /// Parses fn unsafety: `unsafe`, `safe` or nothing.
1244 fn parse_safety(&mut self, case: Case) -> Safety {
1245 if self.eat_keyword_case(exp!(Unsafe), case) {
1246 Safety::Unsafe(self.prev_token_uninterpolated_span())
1247 } else if self.eat_keyword_case(exp!(Safe), case) {
1248 Safety::Safe(self.prev_token_uninterpolated_span())
1249 } else {
1250 Safety::Default
1251 }
1252 }
1253
1254 /// Parses constness: `const` or nothing.
1255 fn parse_constness(&mut self, case: Case) -> Const {
1256 self.parse_constness_(case, false)
1257 }
1258
1259 /// Parses constness for closures (case sensitive, feature-gated)
1260 fn parse_closure_constness(&mut self) -> Const {
1261 let constness = self.parse_constness_(Case::Sensitive, true);
1262 if let Const::Yes(span) = constness {
1263 self.psess.gated_spans.gate(sym::const_closures, span);
1264 }
1265 constness
1266 }
1267
1268 fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
1269 // Avoid const blocks and const closures to be parsed as const items
1270 if (self.check_const_closure() == is_closure)
1271 && !self.look_ahead(1, |t| *t == token::OpenBrace || t.is_metavar_block())
1272 && self.eat_keyword_case(exp!(Const), case)
1273 {
1274 Const::Yes(self.prev_token_uninterpolated_span())
1275 } else {
1276 Const::No
1277 }
1278 }
1279
1280 /// Parses inline const expressions.
1281 fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, Box<Expr>> {
1282 self.expect_keyword(exp!(Const))?;
1283 let (attrs, blk) = self.parse_inner_attrs_and_block(None)?;
1284 let anon_const = AnonConst {
1285 id: DUMMY_NODE_ID,
1286 value: self.mk_expr(blk.span, ExprKind::Block(blk, None)),
1287 mgca_disambiguation: MgcaDisambiguation::AnonConst,
1288 };
1289 let blk_span = anon_const.value.span;
1290 let kind = if pat {
1291 let guar = self
1292 .dcx()
1293 .struct_span_err(blk_span, "const blocks cannot be used as patterns")
1294 .with_help(
1295 "use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead",
1296 )
1297 .emit();
1298 ExprKind::Err(guar)
1299 } else {
1300 ExprKind::ConstBlock(anon_const)
1301 };
1302 Ok(self.mk_expr_with_attrs(span.to(blk_span), kind, attrs))
1303 }
1304
1305 /// Parse nothing or `mut`.
1306 fn parse_mutability(&mut self) -> Mutability {
1307 if self.eat_keyword(exp!(Mut)) { Mutability::Mut } else { Mutability::Not }
1308 }
1309
1310 /// Parse nothing or a by-reference mode.
1311 ///
1312 /// ```ebnf
1313 /// ByRef = "ref" PinAndMut?
1314 /// ```
1315 fn parse_byref(&mut self) -> ByRef {
1316 if self.eat_keyword(exp!(Ref)) {
1317 let (pinnedness, mutability) = self.parse_pin_and_mut();
1318 ByRef::Yes(pinnedness, mutability)
1319 } else {
1320 ByRef::No
1321 }
1322 }
1323
1324 /// Parse nothing or "explicit" mutability.
1325 ///
1326 /// ```ebnf
1327 /// MutOrConst = "mut" | "const"
1328 /// ```
1329 fn parse_mut_or_const(&mut self) -> Option<Mutability> {
1330 if self.eat_keyword(exp!(Mut)) {
1331 Some(Mutability::Mut)
1332 } else if self.eat_keyword(exp!(Const)) {
1333 Some(Mutability::Not)
1334 } else {
1335 None
1336 }
1337 }
1338
1339 /// Parse a field name.
1340 ///
1341 /// ```enbf
1342 /// FieldName = IntLit | Ident
1343 /// ```
1344 fn parse_field_name(&mut self) -> PResult<'a, Ident> {
1345 if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
1346 {
1347 if let Some(suffix) = suffix {
1348 self.dcx().emit_err(errors::InvalidLiteralSuffixOnTupleIndex {
1349 span: self.token.span,
1350 suffix,
1351 });
1352 }
1353 self.bump();
1354 Ok(Ident::new(symbol, self.prev_token.span))
1355 } else {
1356 self.parse_ident_common(true)
1357 }
1358 }
1359
1360 fn parse_delim_args(&mut self) -> PResult<'a, Box<DelimArgs>> {
1361 if let Some(args) = self.parse_delim_args_inner() {
1362 Ok(Box::new(args))
1363 } else {
1364 self.unexpected_any()
1365 }
1366 }
1367
1368 fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> {
1369 Ok(if let Some(args) = self.parse_delim_args_inner() {
1370 AttrArgs::Delimited(args)
1371 } else if self.eat(exp!(Eq)) {
1372 let eq_span = self.prev_token.span;
1373 let expr = self.parse_expr_force_collect()?;
1374 AttrArgs::Eq { eq_span, expr }
1375 } else {
1376 AttrArgs::Empty
1377 })
1378 }
1379
1380 fn parse_delim_args_inner(&mut self) -> Option<DelimArgs> {
1381 let delimited = self.check(exp!(OpenParen))
1382 || self.check(exp!(OpenBracket))
1383 || self.check(exp!(OpenBrace));
1384
1385 delimited.then(|| {
1386 let TokenTree::Delimited(dspan, _, delim, tokens) = self.parse_token_tree() else {
1387 unreachable!()
1388 };
1389 DelimArgs { dspan, delim, tokens }
1390 })
1391 }
1392
1393 /// Parses a single token tree from the input.
1394 pub fn parse_token_tree(&mut self) -> TokenTree {
1395 if self.token.kind.open_delim().is_some() {
1396 // Clone the `TokenTree::Delimited` that we are currently
1397 // within. That's what we are going to return.
1398 let tree = self.token_cursor.stack.last().unwrap().curr().unwrap().clone();
1399 debug_assert_matches!(tree, TokenTree::Delimited(..));
1400
1401 // Advance the token cursor through the entire delimited
1402 // sequence. After getting the `OpenDelim` we are *within* the
1403 // delimited sequence, i.e. at depth `d`. After getting the
1404 // matching `CloseDelim` we are *after* the delimited sequence,
1405 // i.e. at depth `d - 1`.
1406 let target_depth = self.token_cursor.stack.len() - 1;
1407
1408 if let Capturing::No = self.capture_state.capturing {
1409 // We are not capturing tokens, so skip to the end of the
1410 // delimited sequence. This is a perf win when dealing with
1411 // declarative macros that pass large `tt` fragments through
1412 // multiple rules, as seen in the uom-0.37.0 crate.
1413 self.token_cursor.curr.bump_to_end();
1414 self.bump();
1415 debug_assert_eq!(self.token_cursor.stack.len(), target_depth);
1416 } else {
1417 loop {
1418 // Advance one token at a time, so `TokenCursor::next()`
1419 // can capture these tokens if necessary.
1420 self.bump();
1421 if self.token_cursor.stack.len() == target_depth {
1422 break;
1423 }
1424 }
1425 }
1426 debug_assert!(self.token.kind.close_delim().is_some());
1427
1428 // Consume close delimiter
1429 self.bump();
1430 tree
1431 } else {
1432 assert!(!self.token.kind.is_close_delim_or_eof());
1433 let prev_spacing = self.token_spacing;
1434 self.bump();
1435 TokenTree::Token(self.prev_token, prev_spacing)
1436 }
1437 }
1438
1439 pub fn parse_tokens(&mut self) -> TokenStream {
1440 let mut result = Vec::new();
1441 loop {
1442 if self.token.kind.is_close_delim_or_eof() {
1443 break;
1444 } else {
1445 result.push(self.parse_token_tree());
1446 }
1447 }
1448 TokenStream::new(result)
1449 }
1450
1451 /// Evaluates the closure with restrictions in place.
1452 ///
1453 /// Afters the closure is evaluated, restrictions are reset.
1454 fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
1455 let old = self.restrictions;
1456 self.restrictions = res;
1457 let res = f(self);
1458 self.restrictions = old;
1459 res
1460 }
1461
1462 /// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
1463 /// for `pub(in self)` and `pub(super)` for `pub(in super)`.
1464 /// If the following element can't be a tuple (i.e., it's a function definition), then
1465 /// it's not a tuple struct field), and the contents within the parentheses aren't valid,
1466 /// so emit a proper diagnostic.
1467 // Public for rustfmt usage.
1468 pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1469 if let Some(vis) = self
1470 .eat_metavar_seq(MetaVarKind::Vis, |this| this.parse_visibility(FollowedByType::Yes))
1471 {
1472 return Ok(vis);
1473 }
1474
1475 if !self.eat_keyword(exp!(Pub)) {
1476 // We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
1477 // keyword to grab a span from for inherited visibility; an empty span at the
1478 // beginning of the current token would seem to be the "Schelling span".
1479 return Ok(Visibility {
1480 span: self.token.span.shrink_to_lo(),
1481 kind: VisibilityKind::Inherited,
1482 tokens: None,
1483 });
1484 }
1485 let lo = self.prev_token.span;
1486
1487 if self.check(exp!(OpenParen)) {
1488 // We don't `self.bump()` the `(` yet because this might be a struct definition where
1489 // `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
1490 // Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
1491 // by the following tokens.
1492 if self.is_keyword_ahead(1, &[kw::In]) {
1493 // Parse `pub(in path)`.
1494 self.bump(); // `(`
1495 self.bump(); // `in`
1496 let path = self.parse_path(PathStyle::Mod)?; // `path`
1497 self.expect(exp!(CloseParen))?; // `)`
1498 let vis = VisibilityKind::Restricted {
1499 path: Box::new(path),
1500 id: ast::DUMMY_NODE_ID,
1501 shorthand: false,
1502 };
1503 return Ok(Visibility {
1504 span: lo.to(self.prev_token.span),
1505 kind: vis,
1506 tokens: None,
1507 });
1508 } else if self.look_ahead(2, |t| t == &token::CloseParen)
1509 && self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
1510 {
1511 // Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
1512 self.bump(); // `(`
1513 let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
1514 self.expect(exp!(CloseParen))?; // `)`
1515 let vis = VisibilityKind::Restricted {
1516 path: Box::new(path),
1517 id: ast::DUMMY_NODE_ID,
1518 shorthand: true,
1519 };
1520 return Ok(Visibility {
1521 span: lo.to(self.prev_token.span),
1522 kind: vis,
1523 tokens: None,
1524 });
1525 } else if let FollowedByType::No = fbt {
1526 // Provide this diagnostic if a type cannot follow;
1527 // in particular, if this is not a tuple struct.
1528 self.recover_incorrect_vis_restriction()?;
1529 // Emit diagnostic, but continue with public visibility.
1530 }
1531 }
1532
1533 Ok(Visibility { span: lo, kind: VisibilityKind::Public, tokens: None })
1534 }
1535
1536 /// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
1537 fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
1538 self.bump(); // `(`
1539 let path = self.parse_path(PathStyle::Mod)?;
1540 self.expect(exp!(CloseParen))?; // `)`
1541
1542 let path_str = pprust::path_to_string(&path);
1543 self.dcx()
1544 .emit_err(IncorrectVisibilityRestriction { span: path.span, inner_str: path_str });
1545
1546 Ok(())
1547 }
1548
1549 /// Parses an optional `impl` restriction.
1550 /// Enforces the `impl_restriction` feature gate whenever an explicit restriction is encountered.
1551 fn parse_impl_restriction(&mut self) -> PResult<'a, ImplRestriction> {
1552 if self.eat_keyword(exp!(Impl)) {
1553 let lo = self.prev_token.span;
1554 // No units or tuples are allowed to follow `impl` here, so we can safely bump `(`.
1555 self.expect(exp!(OpenParen))?;
1556 if self.eat_keyword(exp!(In)) {
1557 let path = self.parse_path(PathStyle::Mod)?; // `in path`
1558 self.expect(exp!(CloseParen))?; // `)`
1559 let restriction = RestrictionKind::Restricted {
1560 path: Box::new(path),
1561 id: ast::DUMMY_NODE_ID,
1562 shorthand: false,
1563 };
1564 let span = lo.to(self.prev_token.span);
1565 self.psess.gated_spans.gate(sym::impl_restriction, span);
1566 return Ok(ImplRestriction { kind: restriction, span, tokens: None });
1567 } else if self.look_ahead(1, |t| t == &token::CloseParen)
1568 && self.is_keyword_ahead(0, &[kw::Crate, kw::Super, kw::SelfLower])
1569 {
1570 let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
1571 self.expect(exp!(CloseParen))?; // `)`
1572 let restriction = RestrictionKind::Restricted {
1573 path: Box::new(path),
1574 id: ast::DUMMY_NODE_ID,
1575 shorthand: true,
1576 };
1577 let span = lo.to(self.prev_token.span);
1578 self.psess.gated_spans.gate(sym::impl_restriction, span);
1579 return Ok(ImplRestriction { kind: restriction, span, tokens: None });
1580 } else {
1581 self.recover_incorrect_impl_restriction(lo)?;
1582 // Emit diagnostic, but continue with no impl restriction.
1583 }
1584 }
1585 Ok(ImplRestriction {
1586 kind: RestrictionKind::Unrestricted,
1587 span: self.token.span.shrink_to_lo(),
1588 tokens: None,
1589 })
1590 }
1591
1592 /// Recovery for e.g. `impl(something) trait`
1593 fn recover_incorrect_impl_restriction(&mut self, lo: Span) -> PResult<'a, ()> {
1594 let path = self.parse_path(PathStyle::Mod)?;
1595 self.expect(exp!(CloseParen))?; // `)`
1596 let path_str = pprust::path_to_string(&path);
1597 self.dcx().emit_err(IncorrectImplRestriction { span: path.span, inner_str: path_str });
1598 let end = self.prev_token.span;
1599 self.psess.gated_spans.gate(sym::impl_restriction, lo.to(end));
1600 Ok(())
1601 }
1602
1603 /// Parses `extern string_literal?`.
1604 fn parse_extern(&mut self, case: Case) -> Extern {
1605 if self.eat_keyword_case(exp!(Extern), case) {
1606 let mut extern_span = self.prev_token.span;
1607 let abi = self.parse_abi();
1608 if let Some(abi) = abi {
1609 extern_span = extern_span.to(abi.span);
1610 }
1611 Extern::from_abi(abi, extern_span)
1612 } else {
1613 Extern::None
1614 }
1615 }
1616
1617 /// Parses a string literal as an ABI spec.
1618 fn parse_abi(&mut self) -> Option<StrLit> {
1619 match self.parse_str_lit() {
1620 Ok(str_lit) => Some(str_lit),
1621 Err(Some(lit)) => match lit.kind {
1622 ast::LitKind::Err(_) => None,
1623 _ => {
1624 self.dcx().emit_err(NonStringAbiLiteral { span: lit.span });
1625 None
1626 }
1627 },
1628 Err(None) => None,
1629 }
1630 }
1631
1632 fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
1633 &mut self,
1634 f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1635 ) -> PResult<'a, R> {
1636 // The only reason to call `collect_tokens_no_attrs` is if you want tokens, so use
1637 // `ForceCollect::Yes`
1638 self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |this, _attrs| {
1639 Ok((f(this)?, Trailing::No, UsePreAttrPos::No))
1640 })
1641 }
1642
1643 /// Checks for `::` or, potentially, `:::` and then look ahead after it.
1644 fn check_path_sep_and_look_ahead(&mut self, looker: impl Fn(&Token) -> bool) -> bool {
1645 if self.check(exp!(PathSep)) {
1646 if self.may_recover() && self.look_ahead(1, |t| t.kind == token::Colon) {
1647 debug_assert!(!self.look_ahead(1, &looker), "Looker must not match on colon");
1648 self.look_ahead(2, looker)
1649 } else {
1650 self.look_ahead(1, looker)
1651 }
1652 } else {
1653 false
1654 }
1655 }
1656
1657 /// `::{` or `::*`
1658 fn is_import_coupler(&mut self) -> bool {
1659 self.check_path_sep_and_look_ahead(|t| matches!(t.kind, token::OpenBrace | token::Star))
1660 }
1661
1662 // Debug view of the parser's token stream, up to `{lookahead}` tokens.
1663 // Only used when debugging.
1664 #[allow(unused)]
1665 pub(crate) fn debug_lookahead(&self, lookahead: usize) -> impl fmt::Debug {
1666 fmt::from_fn(move |f| {
1667 let mut dbg_fmt = f.debug_struct("Parser"); // or at least, one view of
1668
1669 // we don't need N spans, but we want at least one, so print all of prev_token
1670 dbg_fmt.field("prev_token", &self.prev_token);
1671 let mut tokens = vec![];
1672 for i in 0..lookahead {
1673 let tok = self.look_ahead(i, |tok| tok.kind);
1674 let is_eof = tok == TokenKind::Eof;
1675 tokens.push(tok);
1676 if is_eof {
1677 // Don't look ahead past EOF.
1678 break;
1679 }
1680 }
1681 dbg_fmt.field_with("tokens", |field| field.debug_list().entries(tokens).finish());
1682 dbg_fmt.field("approx_token_stream_pos", &self.num_bump_calls);
1683
1684 // some fields are interesting for certain values, as they relate to macro parsing
1685 if let Some(subparser) = self.subparser_name {
1686 dbg_fmt.field("subparser_name", &subparser);
1687 }
1688 if let Recovery::Forbidden = self.recovery {
1689 dbg_fmt.field("recovery", &self.recovery);
1690 }
1691
1692 // imply there's "more to know" than this view
1693 dbg_fmt.finish_non_exhaustive()
1694 })
1695 }
1696
1697 pub fn clear_expected_token_types(&mut self) {
1698 self.expected_token_types.clear();
1699 }
1700
1701 pub fn approx_token_stream_pos(&self) -> u32 {
1702 self.num_bump_calls
1703 }
1704
1705 /// For interpolated `self.token`, returns a span of the fragment to which
1706 /// the interpolated token refers. For all other tokens this is just a
1707 /// regular span. It is particularly important to use this for identifiers
1708 /// and lifetimes for which spans affect name resolution and edition
1709 /// checks. Note that keywords are also identifiers, so they should use
1710 /// this if they keep spans or perform edition checks.
1711 pub fn token_uninterpolated_span(&self) -> Span {
1712 match &self.token.kind {
1713 token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1714 token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => self.look_ahead(1, |t| t.span),
1715 _ => self.token.span,
1716 }
1717 }
1718
1719 /// Like `token_uninterpolated_span`, but works on `self.prev_token`.
1720 pub fn prev_token_uninterpolated_span(&self) -> Span {
1721 match &self.prev_token.kind {
1722 token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1723 token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => self.look_ahead(0, |t| t.span),
1724 _ => self.prev_token.span,
1725 }
1726 }
1727
1728 fn missing_semi_from_binop(
1729 &self,
1730 kind_desc: &str,
1731 expr: &Expr,
1732 decl_lo: Option<Span>,
1733 ) -> Option<(Span, ErrorGuaranteed)> {
1734 if self.token == TokenKind::Semi {
1735 return None;
1736 }
1737 if !self.may_recover() || expr.span.from_expansion() {
1738 return None;
1739 }
1740 let sm = self.psess.source_map();
1741 if let ExprKind::Binary(op, lhs, rhs) = &expr.kind
1742 && sm.is_multiline(lhs.span.shrink_to_hi().until(rhs.span.shrink_to_lo()))
1743 && matches!(op.node, BinOpKind::Mul | BinOpKind::BitAnd)
1744 && classify::expr_requires_semi_to_be_stmt(rhs)
1745 {
1746 let lhs_end_span = lhs.span.shrink_to_hi();
1747 let token_str = token_descr(&self.token);
1748 let mut err = self
1749 .dcx()
1750 .struct_span_err(lhs_end_span, format!("expected `;`, found {token_str}"));
1751 err.span_label(self.token.span, "unexpected token");
1752
1753 // Use the declaration start if provided, otherwise fall back to lhs_end_span.
1754 let continuation_start = decl_lo.unwrap_or(lhs_end_span);
1755 let continuation_span = continuation_start.until(rhs.span.shrink_to_hi());
1756 err.span_label(
1757 continuation_span,
1758 format!(
1759 "to finish parsing this {kind_desc}, expected this to be followed by a `;`",
1760 ),
1761 );
1762 let op_desc = match op.node {
1763 BinOpKind::BitAnd => "a bit-and",
1764 BinOpKind::Mul => "a multiplication",
1765 _ => "a binary",
1766 };
1767 let mut note_spans = MultiSpan::new();
1768 note_spans.push_span_label(lhs.span, "parsed as the left-hand expression");
1769 note_spans.push_span_label(rhs.span, "parsed as the right-hand expression");
1770 note_spans.push_span_label(op.span, format!("this was parsed as {op_desc}"));
1771 err.span_note(
1772 note_spans,
1773 format!("the {kind_desc} was parsed as having {op_desc} binary expression"),
1774 );
1775
1776 err.span_suggestion(
1777 lhs_end_span,
1778 format!("you may have meant to write a `;` to terminate the {kind_desc} earlier"),
1779 ";",
1780 Applicability::MaybeIncorrect,
1781 );
1782 return Some((lhs.span, err.emit()));
1783 }
1784 None
1785 }
1786}
1787
1788// Metavar captures of various kinds.
1789#[derive(Clone, Debug)]
1790pub enum ParseNtResult {
1791 Tt(TokenTree),
1792 Ident(Ident, IdentIsRaw),
1793 Lifetime(Ident, IdentIsRaw),
1794 Item(Box<ast::Item>),
1795 Block(Box<ast::Block>),
1796 Stmt(Box<ast::Stmt>),
1797 Pat(Box<ast::Pat>, NtPatKind),
1798 Expr(Box<ast::Expr>, NtExprKind),
1799 Literal(Box<ast::Expr>),
1800 Ty(Box<ast::Ty>),
1801 Meta(Box<ast::AttrItem>),
1802 Path(Box<ast::Path>),
1803 Vis(Box<ast::Visibility>),
1804 Guard(Box<ast::Guard>),
1805}