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
14use std::assert_matches::debug_assert_matches;
15use std::{fmt, mem, slice};
16
17use attr_wrapper::{AttrWrapper, UsePreAttrPos};
18pub use diagnostics::AttemptLocalParseRecovery;
19pub(crate) use expr::ForbiddenLetReason;
20pub(crate) use item::FnParseMode;
21pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
22use path::PathStyle;
23use rustc_ast::ptr::P;
24use rustc_ast::token::{
25 self, IdentIsRaw, InvisibleOrigin, MetaVarKind, NtExprKind, NtPatKind, Token, TokenKind,
26};
27use rustc_ast::tokenstream::{
28 ParserRange, ParserReplacement, Spacing, TokenCursor, TokenStream, TokenTree, TokenTreeCursor,
29};
30use rustc_ast::util::case::Case;
31use rustc_ast::{
32 self as ast, AnonConst, AttrArgs, AttrId, ByRef, Const, CoroutineKind, DUMMY_NODE_ID,
33 DelimArgs, Expr, ExprKind, Extern, HasAttrs, HasTokens, Mutability, Recovered, Safety, StrLit,
34 Visibility, VisibilityKind,
35};
36use rustc_ast_pretty::pprust;
37use rustc_data_structures::fx::FxHashMap;
38use rustc_errors::{Applicability, Diag, FatalError, MultiSpan, PResult};
39use rustc_index::interval::IntervalSet;
40use rustc_session::parse::ParseSess;
41use rustc_span::{Ident, Span, Symbol, kw, sym};
42use thin_vec::ThinVec;
43use token_type::TokenTypeSet;
44pub use token_type::{ExpKeywordPair, ExpTokenPair, TokenType};
45use tracing::debug;
46
47use crate::errors::{self, IncorrectVisibilityRestriction, NonStringAbiLiteral};
48use crate::exp;
49
50#[cfg(test)]
51mod tests;
52
53#[cfg(test)]
56mod tokenstream {
57 mod tests;
58}
59
60bitflags::bitflags! {
61 #[derive(Clone, Copy, Debug)]
67 struct Restrictions: u8 {
68 const STMT_EXPR = 1 << 0;
79 const NO_STRUCT_LITERAL = 1 << 1;
90 const CONST_EXPR = 1 << 2;
97 const ALLOW_LET = 1 << 3;
105 const IN_IF_GUARD = 1 << 4;
111 const IS_PAT = 1 << 5;
118 }
119}
120
121#[derive(Clone, Copy, PartialEq, Debug)]
122enum SemiColonMode {
123 Break,
124 Ignore,
125 Comma,
126}
127
128#[derive(Clone, Copy, PartialEq, Debug)]
129enum BlockMode {
130 Break,
131 Ignore,
132}
133
134#[derive(Clone, Copy, Debug, PartialEq)]
137pub enum ForceCollect {
138 Yes,
139 No,
140}
141
142#[macro_export]
144macro_rules! maybe_recover_from_interpolated_ty_qpath {
145 ($self: expr, $allow_qpath_recovery: expr) => {
146 if $allow_qpath_recovery
147 && $self.may_recover()
148 && let Some(mv_kind) = $self.token.is_metavar_seq()
149 && let token::MetaVarKind::Ty { .. } = mv_kind
150 && $self.check_noexpect_past_close_delim(&token::PathSep)
151 {
152 let ty = $self
154 .eat_metavar_seq(mv_kind, |this| this.parse_ty_no_question_mark_recover())
155 .expect("metavar seq ty");
156
157 return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
158 }
159 };
160}
161
162#[derive(Clone, Copy, Debug)]
163pub enum Recovery {
164 Allowed,
165 Forbidden,
166}
167
168#[derive(Clone)]
169pub struct Parser<'a> {
170 pub psess: &'a ParseSess,
171 pub token: Token,
173 token_spacing: Spacing,
175 pub prev_token: Token,
177 pub capture_cfg: bool,
178 restrictions: Restrictions,
179 expected_token_types: TokenTypeSet,
180 token_cursor: TokenCursor,
181 num_bump_calls: u32,
183 break_last_token: u32,
202 unmatched_angle_bracket_count: u16,
208 angle_bracket_nesting: u16,
209
210 last_unexpected_token_span: Option<Span>,
211 subparser_name: Option<&'static str>,
213 capture_state: CaptureState,
214 current_closure: Option<ClosureSpans>,
217 recovery: Recovery,
220}
221
222#[cfg(all(target_pointer_width = "64", any(target_arch = "aarch64", target_arch = "x86_64")))]
226rustc_data_structures::static_assert_size!(Parser<'_>, 288);
227
228#[derive(Clone, Debug)]
230struct ClosureSpans {
231 whole_closure: Span,
232 closing_pipe: Span,
233 body: Span,
234}
235
236#[derive(Copy, Clone, Debug)]
240enum Capturing {
241 No,
243 Yes,
245}
246
247#[derive(Clone, Debug)]
249struct CaptureState {
250 capturing: Capturing,
251 parser_replacements: Vec<ParserReplacement>,
252 inner_attr_parser_ranges: FxHashMap<AttrId, ParserRange>,
253 seen_attrs: IntervalSet<AttrId>,
256}
257
258#[derive(Debug)]
260struct SeqSep<'a> {
261 sep: Option<ExpTokenPair<'a>>,
263 trailing_sep_allowed: bool,
265}
266
267impl<'a> SeqSep<'a> {
268 fn trailing_allowed(sep: ExpTokenPair<'a>) -> SeqSep<'a> {
269 SeqSep { sep: Some(sep), trailing_sep_allowed: true }
270 }
271
272 fn none() -> SeqSep<'a> {
273 SeqSep { sep: None, trailing_sep_allowed: false }
274 }
275}
276
277#[derive(Debug)]
278pub enum FollowedByType {
279 Yes,
280 No,
281}
282
283#[derive(Copy, Clone, Debug)]
284enum Trailing {
285 No,
286 Yes,
287}
288
289impl From<bool> for Trailing {
290 fn from(b: bool) -> Trailing {
291 if b { Trailing::Yes } else { Trailing::No }
292 }
293}
294
295#[derive(Clone, Copy, Debug, PartialEq, Eq)]
296pub(super) enum TokenDescription {
297 ReservedIdentifier,
298 Keyword,
299 ReservedKeyword,
300 DocComment,
301
302 MetaVar(MetaVarKind),
307}
308
309impl TokenDescription {
310 pub(super) fn from_token(token: &Token) -> Option<Self> {
311 match token.kind {
312 _ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
313 _ if token.is_used_keyword() => Some(TokenDescription::Keyword),
314 _ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
315 token::DocComment(..) => Some(TokenDescription::DocComment),
316 token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => {
317 Some(TokenDescription::MetaVar(kind))
318 }
319 _ => None,
320 }
321 }
322}
323
324pub fn token_descr(token: &Token) -> String {
325 let s = pprust::token_to_string(token).to_string();
326
327 match (TokenDescription::from_token(token), &token.kind) {
328 (Some(TokenDescription::ReservedIdentifier), _) => format!("reserved identifier `{s}`"),
329 (Some(TokenDescription::Keyword), _) => format!("keyword `{s}`"),
330 (Some(TokenDescription::ReservedKeyword), _) => format!("reserved keyword `{s}`"),
331 (Some(TokenDescription::DocComment), _) => format!("doc comment `{s}`"),
332 (Some(TokenDescription::MetaVar(kind)), _) => format!("`{kind}` metavariable"),
334 (None, TokenKind::NtIdent(..)) => format!("identifier `{s}`"),
335 (None, TokenKind::NtLifetime(..)) => format!("lifetime `{s}`"),
336 (None, _) => format!("`{s}`"),
337 }
338}
339
340impl<'a> Parser<'a> {
341 pub fn new(
342 psess: &'a ParseSess,
343 stream: TokenStream,
344 subparser_name: Option<&'static str>,
345 ) -> Self {
346 let mut parser = Parser {
347 psess,
348 token: Token::dummy(),
349 token_spacing: Spacing::Alone,
350 prev_token: Token::dummy(),
351 capture_cfg: false,
352 restrictions: Restrictions::empty(),
353 expected_token_types: TokenTypeSet::new(),
354 token_cursor: TokenCursor { curr: TokenTreeCursor::new(stream), stack: Vec::new() },
355 num_bump_calls: 0,
356 break_last_token: 0,
357 unmatched_angle_bracket_count: 0,
358 angle_bracket_nesting: 0,
359 last_unexpected_token_span: None,
360 subparser_name,
361 capture_state: CaptureState {
362 capturing: Capturing::No,
363 parser_replacements: Vec::new(),
364 inner_attr_parser_ranges: Default::default(),
365 seen_attrs: IntervalSet::new(u32::MAX as usize),
366 },
367 current_closure: None,
368 recovery: Recovery::Allowed,
369 };
370
371 parser.bump();
373
374 parser.num_bump_calls = 0;
378
379 parser
380 }
381
382 #[inline]
383 pub fn recovery(mut self, recovery: Recovery) -> Self {
384 self.recovery = recovery;
385 self
386 }
387
388 #[inline]
389 fn with_recovery<T>(&mut self, recovery: Recovery, f: impl FnOnce(&mut Self) -> T) -> T {
390 let old = mem::replace(&mut self.recovery, recovery);
391 let res = f(self);
392 self.recovery = old;
393 res
394 }
395
396 #[inline]
404 fn may_recover(&self) -> bool {
405 matches!(self.recovery, Recovery::Allowed)
406 }
407
408 pub fn unexpected_any<T>(&mut self) -> PResult<'a, T> {
411 match self.expect_one_of(&[], &[]) {
412 Err(e) => Err(e),
413 Ok(_) => FatalError.raise(),
416 }
417 }
418
419 pub fn unexpected(&mut self) -> PResult<'a, ()> {
420 self.unexpected_any()
421 }
422
423 pub fn expect(&mut self, exp: ExpTokenPair<'_>) -> PResult<'a, Recovered> {
425 if self.expected_token_types.is_empty() {
426 if self.token == *exp.tok {
427 self.bump();
428 Ok(Recovered::No)
429 } else {
430 self.unexpected_try_recover(exp.tok)
431 }
432 } else {
433 self.expect_one_of(slice::from_ref(&exp), &[])
434 }
435 }
436
437 fn expect_one_of(
441 &mut self,
442 edible: &[ExpTokenPair<'_>],
443 inedible: &[ExpTokenPair<'_>],
444 ) -> PResult<'a, Recovered> {
445 if edible.iter().any(|exp| exp.tok == &self.token.kind) {
446 self.bump();
447 Ok(Recovered::No)
448 } else if inedible.iter().any(|exp| exp.tok == &self.token.kind) {
449 Ok(Recovered::No)
451 } else if self.token != token::Eof
452 && self.last_unexpected_token_span == Some(self.token.span)
453 {
454 FatalError.raise();
455 } else {
456 self.expected_one_of_not_found(edible, inedible)
457 .map(|error_guaranteed| Recovered::Yes(error_guaranteed))
458 }
459 }
460
461 pub fn parse_ident(&mut self) -> PResult<'a, Ident> {
463 self.parse_ident_common(true)
464 }
465
466 fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
467 let (ident, is_raw) = self.ident_or_err(recover)?;
468
469 if matches!(is_raw, IdentIsRaw::No) && ident.is_reserved() {
470 let err = self.expected_ident_found_err();
471 if recover {
472 err.emit();
473 } else {
474 return Err(err);
475 }
476 }
477 self.bump();
478 Ok(ident)
479 }
480
481 fn ident_or_err(&mut self, recover: bool) -> PResult<'a, (Ident, IdentIsRaw)> {
482 match self.token.ident() {
483 Some(ident) => Ok(ident),
484 None => self.expected_ident_found(recover),
485 }
486 }
487
488 #[inline]
493 fn check(&mut self, exp: ExpTokenPair<'_>) -> bool {
494 let is_present = self.token == *exp.tok;
495 if !is_present {
496 self.expected_token_types.insert(exp.token_type);
497 }
498 is_present
499 }
500
501 #[inline]
502 #[must_use]
503 fn check_noexpect(&self, tok: &TokenKind) -> bool {
504 self.token == *tok
505 }
506
507 fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
516 let mut tree_cursor = self.token_cursor.stack.last().unwrap().clone();
517 tree_cursor.bump();
518 matches!(
519 tree_cursor.curr(),
520 Some(TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
521 )
522 }
523
524 #[inline]
529 #[must_use]
530 fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
531 let is_present = self.check_noexpect(tok);
532 if is_present {
533 self.bump()
534 }
535 is_present
536 }
537
538 #[inline]
540 #[must_use]
541 pub fn eat(&mut self, exp: ExpTokenPair<'_>) -> bool {
542 let is_present = self.check(exp);
543 if is_present {
544 self.bump()
545 }
546 is_present
547 }
548
549 #[inline]
552 #[must_use]
553 fn check_keyword(&mut self, exp: ExpKeywordPair) -> bool {
554 let is_keyword = self.token.is_keyword(exp.kw);
555 if !is_keyword {
556 self.expected_token_types.insert(exp.token_type);
557 }
558 is_keyword
559 }
560
561 #[inline]
562 #[must_use]
563 fn check_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
564 if self.check_keyword(exp) {
565 true
566 } else if case == Case::Insensitive
567 && let Some((ident, IdentIsRaw::No)) = self.token.ident()
568 && ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
570 {
571 true
572 } else {
573 false
574 }
575 }
576
577 #[inline]
581 #[must_use]
582 pub fn eat_keyword(&mut self, exp: ExpKeywordPair) -> bool {
583 let is_keyword = self.check_keyword(exp);
584 if is_keyword {
585 self.bump();
586 }
587 is_keyword
588 }
589
590 #[inline]
594 #[must_use]
595 fn eat_keyword_case(&mut self, exp: ExpKeywordPair, case: Case) -> bool {
596 if self.eat_keyword(exp) {
597 true
598 } else if case == Case::Insensitive
599 && let Some((ident, IdentIsRaw::No)) = self.token.ident()
600 && ident.as_str().eq_ignore_ascii_case(exp.kw.as_str())
602 {
603 self.dcx().emit_err(errors::KwBadCase { span: ident.span, kw: exp.kw.as_str() });
604 self.bump();
605 true
606 } else {
607 false
608 }
609 }
610
611 #[inline]
615 #[must_use]
616 pub fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
617 let is_keyword = self.token.is_keyword(kw);
618 if is_keyword {
619 self.bump();
620 }
621 is_keyword
622 }
623
624 pub fn expect_keyword(&mut self, exp: ExpKeywordPair) -> PResult<'a, ()> {
628 if !self.eat_keyword(exp) { self.unexpected() } else { Ok(()) }
629 }
630
631 fn eat_metavar_seq<T>(
633 &mut self,
634 mv_kind: MetaVarKind,
635 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
636 ) -> Option<T> {
637 self.eat_metavar_seq_with_matcher(|mvk| mvk == mv_kind, f)
638 }
639
640 fn eat_metavar_seq_with_matcher<T>(
644 &mut self,
645 match_mv_kind: impl Fn(MetaVarKind) -> bool,
646 mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
647 ) -> Option<T> {
648 if let token::OpenInvisible(InvisibleOrigin::MetaVar(mv_kind)) = self.token.kind
649 && match_mv_kind(mv_kind)
650 {
651 self.bump();
652
653 let res = self.with_recovery(Recovery::Forbidden, |this| f(this));
657
658 let res = match res {
659 Ok(res) => res,
660 Err(err) => {
661 err.delay_as_bug();
663 return None;
664 }
665 };
666
667 if let token::CloseInvisible(InvisibleOrigin::MetaVar(mv_kind)) = self.token.kind
668 && match_mv_kind(mv_kind)
669 {
670 self.bump();
671 Some(res)
672 } else {
673 self.dcx()
677 .span_delayed_bug(self.token.span, "no close delim with reparsing {mv_kind:?}");
678 None
679 }
680 } else {
681 None
682 }
683 }
684
685 fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
687 self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
688 }
689
690 #[inline]
691 fn check_or_expected(&mut self, ok: bool, token_type: TokenType) -> bool {
692 if !ok {
693 self.expected_token_types.insert(token_type);
694 }
695 ok
696 }
697
698 fn check_ident(&mut self) -> bool {
699 self.check_or_expected(self.token.is_ident(), TokenType::Ident)
700 }
701
702 fn check_path(&mut self) -> bool {
703 self.check_or_expected(self.token.is_path_start(), TokenType::Path)
704 }
705
706 fn check_type(&mut self) -> bool {
707 self.check_or_expected(self.token.can_begin_type(), TokenType::Type)
708 }
709
710 fn check_const_arg(&mut self) -> bool {
711 self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
712 }
713
714 fn check_const_closure(&self) -> bool {
715 self.is_keyword_ahead(0, &[kw::Const])
716 && self.look_ahead(1, |t| match &t.kind {
717 token::Ident(kw::Move | kw::Use | kw::Static, IdentIsRaw::No)
719 | token::OrOr
720 | token::Or => true,
721 _ => false,
722 })
723 }
724
725 fn check_inline_const(&self, dist: usize) -> bool {
726 self.is_keyword_ahead(dist, &[kw::Const])
727 && self.look_ahead(dist + 1, |t| match &t.kind {
728 token::OpenBrace => true,
729 token::OpenInvisible(InvisibleOrigin::MetaVar(MetaVarKind::Block)) => true,
730 _ => false,
731 })
732 }
733
734 #[inline]
737 fn check_plus(&mut self) -> bool {
738 self.check_or_expected(self.token.is_like_plus(), TokenType::Plus)
739 }
740
741 fn break_and_eat(&mut self, exp: ExpTokenPair<'_>) -> bool {
745 if self.token == *exp.tok {
746 self.bump();
747 return true;
748 }
749 match self.token.kind.break_two_token_op(1) {
750 Some((first, second)) if first == *exp.tok => {
751 let first_span = self.psess.source_map().start_point(self.token.span);
752 let second_span = self.token.span.with_lo(first_span.hi());
753 self.token = Token::new(first, first_span);
754 self.break_last_token += 1;
761 self.bump_with((Token::new(second, second_span), self.token_spacing));
764 true
765 }
766 _ => {
767 self.expected_token_types.insert(exp.token_type);
768 false
769 }
770 }
771 }
772
773 fn eat_plus(&mut self) -> bool {
775 self.break_and_eat(exp!(Plus))
776 }
777
778 fn expect_and(&mut self) -> PResult<'a, ()> {
781 if self.break_and_eat(exp!(And)) { Ok(()) } else { self.unexpected() }
782 }
783
784 fn expect_or(&mut self) -> PResult<'a, ()> {
787 if self.break_and_eat(exp!(Or)) { Ok(()) } else { self.unexpected() }
788 }
789
790 fn eat_lt(&mut self) -> bool {
792 let ate = self.break_and_eat(exp!(Lt));
793 if ate {
794 self.unmatched_angle_bracket_count += 1;
796 debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
797 }
798 ate
799 }
800
801 fn expect_lt(&mut self) -> PResult<'a, ()> {
804 if self.eat_lt() { Ok(()) } else { self.unexpected() }
805 }
806
807 fn expect_gt(&mut self) -> PResult<'a, ()> {
810 if self.break_and_eat(exp!(Gt)) {
811 if self.unmatched_angle_bracket_count > 0 {
813 self.unmatched_angle_bracket_count -= 1;
814 debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
815 }
816 Ok(())
817 } else {
818 self.unexpected()
819 }
820 }
821
822 fn expect_any_with_type(
824 &mut self,
825 closes_expected: &[ExpTokenPair<'_>],
826 closes_not_expected: &[&TokenKind],
827 ) -> bool {
828 closes_expected.iter().any(|&close| self.check(close))
829 || closes_not_expected.iter().any(|k| self.check_noexpect(k))
830 }
831
832 fn parse_seq_to_before_tokens<T>(
836 &mut self,
837 closes_expected: &[ExpTokenPair<'_>],
838 closes_not_expected: &[&TokenKind],
839 sep: SeqSep<'_>,
840 mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
841 ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
842 let mut first = true;
843 let mut recovered = Recovered::No;
844 let mut trailing = Trailing::No;
845 let mut v = ThinVec::new();
846
847 while !self.expect_any_with_type(closes_expected, closes_not_expected) {
848 if self.token.kind.is_close_delim_or_eof() {
849 break;
850 }
851 if let Some(exp) = sep.sep {
852 if first {
853 first = false;
855 } else {
856 match self.expect(exp) {
858 Ok(Recovered::No) => {
859 self.current_closure.take();
860 }
861 Ok(Recovered::Yes(guar)) => {
862 self.current_closure.take();
863 recovered = Recovered::Yes(guar);
864 break;
865 }
866 Err(mut expect_err) => {
867 let sp = self.prev_token.span.shrink_to_hi();
868 let token_str = pprust::token_kind_to_string(exp.tok);
869
870 match self.current_closure.take() {
871 Some(closure_spans) if self.token == TokenKind::Semi => {
872 self.recover_missing_braces_around_closure_body(
879 closure_spans,
880 expect_err,
881 )?;
882
883 continue;
884 }
885
886 _ => {
887 if exp.tok.similar_tokens().contains(&self.token.kind) {
889 self.bump();
890 }
891 }
892 }
893
894 if self.prev_token.is_ident() && self.token == token::DotDot {
898 let msg = format!(
899 "if you meant to bind the contents of the rest of the array \
900 pattern into `{}`, use `@`",
901 pprust::token_to_string(&self.prev_token)
902 );
903 expect_err
904 .with_span_suggestion_verbose(
905 self.prev_token.span.shrink_to_hi().until(self.token.span),
906 msg,
907 " @ ",
908 Applicability::MaybeIncorrect,
909 )
910 .emit();
911 break;
912 }
913
914 self.last_unexpected_token_span = None;
916 match f(self) {
917 Ok(t) => {
918 expect_err
921 .with_span_suggestion_short(
922 sp,
923 format!("missing `{token_str}`"),
924 token_str,
925 Applicability::MaybeIncorrect,
926 )
927 .emit();
928
929 v.push(t);
930 continue;
931 }
932 Err(e) => {
933 for xx in &e.children {
936 expect_err.children.push(xx.clone());
939 }
940 e.cancel();
941 if self.token == token::Colon {
942 return Err(expect_err);
945 } else if let [exp] = closes_expected
946 && exp.token_type == TokenType::CloseParen
947 {
948 return Err(expect_err);
949 } else {
950 expect_err.emit();
951 break;
952 }
953 }
954 }
955 }
956 }
957 }
958 }
959 if sep.trailing_sep_allowed
960 && self.expect_any_with_type(closes_expected, closes_not_expected)
961 {
962 trailing = Trailing::Yes;
963 break;
964 }
965
966 let t = f(self)?;
967 v.push(t);
968 }
969
970 Ok((v, trailing, recovered))
971 }
972
973 fn recover_missing_braces_around_closure_body(
974 &mut self,
975 closure_spans: ClosureSpans,
976 mut expect_err: Diag<'_>,
977 ) -> PResult<'a, ()> {
978 let initial_semicolon = self.token.span;
979
980 while self.eat(exp!(Semi)) {
981 let _ = self
982 .parse_stmt_without_recovery(false, ForceCollect::No, false)
983 .unwrap_or_else(|e| {
984 e.cancel();
985 None
986 });
987 }
988
989 expect_err
990 .primary_message("closure bodies that contain statements must be surrounded by braces");
991
992 let preceding_pipe_span = closure_spans.closing_pipe;
993 let following_token_span = self.token.span;
994
995 let mut first_note = MultiSpan::from(vec![initial_semicolon]);
996 first_note.push_span_label(
997 initial_semicolon,
998 "this `;` turns the preceding closure into a statement",
999 );
1000 first_note.push_span_label(
1001 closure_spans.body,
1002 "this expression is a statement because of the trailing semicolon",
1003 );
1004 expect_err.span_note(first_note, "statement found outside of a block");
1005
1006 let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
1007 second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
1008 second_note.push_span_label(
1009 following_token_span,
1010 "...but likely you meant the closure to end here",
1011 );
1012 expect_err.span_note(second_note, "the closure body may be incorrectly delimited");
1013
1014 expect_err.span(vec![preceding_pipe_span, following_token_span]);
1015
1016 let opening_suggestion_str = " {".to_string();
1017 let closing_suggestion_str = "}".to_string();
1018
1019 expect_err.multipart_suggestion(
1020 "try adding braces",
1021 vec![
1022 (preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
1023 (following_token_span.shrink_to_lo(), closing_suggestion_str),
1024 ],
1025 Applicability::MaybeIncorrect,
1026 );
1027
1028 expect_err.emit();
1029
1030 Ok(())
1031 }
1032
1033 fn parse_seq_to_before_end<T>(
1037 &mut self,
1038 close: ExpTokenPair<'_>,
1039 sep: SeqSep<'_>,
1040 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1041 ) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
1042 self.parse_seq_to_before_tokens(&[close], &[], sep, f)
1043 }
1044
1045 fn parse_seq_to_end<T>(
1049 &mut self,
1050 close: ExpTokenPair<'_>,
1051 sep: SeqSep<'_>,
1052 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1053 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1054 let (val, trailing, recovered) = self.parse_seq_to_before_end(close, sep, f)?;
1055 if matches!(recovered, Recovered::No) && !self.eat(close) {
1056 self.dcx().span_delayed_bug(
1057 self.token.span,
1058 "recovered but `parse_seq_to_before_end` did not give us the close token",
1059 );
1060 }
1061 Ok((val, trailing))
1062 }
1063
1064 fn parse_unspanned_seq<T>(
1068 &mut self,
1069 open: ExpTokenPair<'_>,
1070 close: ExpTokenPair<'_>,
1071 sep: SeqSep<'_>,
1072 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1073 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1074 self.expect(open)?;
1075 self.parse_seq_to_end(close, sep, f)
1076 }
1077
1078 fn parse_delim_comma_seq<T>(
1082 &mut self,
1083 open: ExpTokenPair<'_>,
1084 close: ExpTokenPair<'_>,
1085 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1086 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1087 self.parse_unspanned_seq(open, close, SeqSep::trailing_allowed(exp!(Comma)), f)
1088 }
1089
1090 fn parse_paren_comma_seq<T>(
1094 &mut self,
1095 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1096 ) -> PResult<'a, (ThinVec<T>, Trailing)> {
1097 self.parse_delim_comma_seq(exp!(OpenParen), exp!(CloseParen), f)
1098 }
1099
1100 fn bump_with(&mut self, next: (Token, Spacing)) {
1102 self.inlined_bump_with(next)
1103 }
1104
1105 #[inline(always)]
1107 fn inlined_bump_with(&mut self, (next_token, next_spacing): (Token, Spacing)) {
1108 self.prev_token = mem::replace(&mut self.token, next_token);
1110 self.token_spacing = next_spacing;
1111
1112 self.expected_token_types.clear();
1114 }
1115
1116 pub fn bump(&mut self) {
1118 let mut next = self.token_cursor.inlined_next();
1121 self.num_bump_calls += 1;
1122 self.break_last_token = 0;
1125 if next.0.span.is_dummy() {
1126 let fallback_span = self.token.span;
1128 next.0.span = fallback_span.with_ctxt(next.0.span.ctxt());
1129 }
1130 debug_assert!(!matches!(
1131 next.0.kind,
1132 token::OpenInvisible(origin) | token::CloseInvisible(origin) if origin.skip()
1133 ));
1134 self.inlined_bump_with(next)
1135 }
1136
1137 pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
1141 if dist == 0 {
1142 return looker(&self.token);
1143 }
1144
1145 if dist == 1 {
1148 match self.token_cursor.curr.curr() {
1151 Some(tree) => {
1152 match tree {
1154 TokenTree::Token(token, _) => return looker(token),
1155 &TokenTree::Delimited(dspan, _, delim, _) => {
1156 if !delim.skip() {
1157 return looker(&Token::new(delim.as_open_token_kind(), dspan.open));
1158 }
1159 }
1160 }
1161 }
1162 None => {
1163 if let Some(last) = self.token_cursor.stack.last()
1166 && let Some(&TokenTree::Delimited(span, _, delim, _)) = last.curr()
1167 && !delim.skip()
1168 {
1169 return looker(&Token::new(delim.as_close_token_kind(), span.close));
1172 }
1173 }
1174 }
1175 }
1176
1177 let mut cursor = self.token_cursor.clone();
1180 let mut i = 0;
1181 let mut token = Token::dummy();
1182 while i < dist {
1183 token = cursor.next().0;
1184 if matches!(
1185 token.kind,
1186 token::OpenInvisible(origin) | token::CloseInvisible(origin) if origin.skip()
1187 ) {
1188 continue;
1189 }
1190 i += 1;
1191 }
1192 looker(&token)
1193 }
1194
1195 pub fn tree_look_ahead<R>(
1198 &self,
1199 dist: usize,
1200 looker: impl FnOnce(&TokenTree) -> R,
1201 ) -> Option<R> {
1202 assert_ne!(dist, 0);
1203 self.token_cursor.curr.look_ahead(dist - 1).map(looker)
1204 }
1205
1206 pub(crate) fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
1208 self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
1209 }
1210
1211 fn parse_coroutine_kind(&mut self, case: Case) -> Option<CoroutineKind> {
1213 let span = self.token_uninterpolated_span();
1214 if self.eat_keyword_case(exp!(Async), case) {
1215 if self.token_uninterpolated_span().at_least_rust_2024()
1218 && self.eat_keyword_case(exp!(Gen), case)
1219 {
1220 let gen_span = self.prev_token_uninterpolated_span();
1221 Some(CoroutineKind::AsyncGen {
1222 span: span.to(gen_span),
1223 closure_id: DUMMY_NODE_ID,
1224 return_impl_trait_id: DUMMY_NODE_ID,
1225 })
1226 } else {
1227 Some(CoroutineKind::Async {
1228 span,
1229 closure_id: DUMMY_NODE_ID,
1230 return_impl_trait_id: DUMMY_NODE_ID,
1231 })
1232 }
1233 } else if self.token_uninterpolated_span().at_least_rust_2024()
1234 && self.eat_keyword_case(exp!(Gen), case)
1235 {
1236 Some(CoroutineKind::Gen {
1237 span,
1238 closure_id: DUMMY_NODE_ID,
1239 return_impl_trait_id: DUMMY_NODE_ID,
1240 })
1241 } else {
1242 None
1243 }
1244 }
1245
1246 fn parse_safety(&mut self, case: Case) -> Safety {
1248 if self.eat_keyword_case(exp!(Unsafe), case) {
1249 Safety::Unsafe(self.prev_token_uninterpolated_span())
1250 } else if self.eat_keyword_case(exp!(Safe), case) {
1251 Safety::Safe(self.prev_token_uninterpolated_span())
1252 } else {
1253 Safety::Default
1254 }
1255 }
1256
1257 fn parse_constness(&mut self, case: Case) -> Const {
1259 self.parse_constness_(case, false)
1260 }
1261
1262 fn parse_closure_constness(&mut self) -> Const {
1264 let constness = self.parse_constness_(Case::Sensitive, true);
1265 if let Const::Yes(span) = constness {
1266 self.psess.gated_spans.gate(sym::const_closures, span);
1267 }
1268 constness
1269 }
1270
1271 fn parse_constness_(&mut self, case: Case, is_closure: bool) -> Const {
1272 if (self.check_const_closure() == is_closure)
1274 && !self.look_ahead(1, |t| *t == token::OpenBrace || t.is_metavar_block())
1275 && self.eat_keyword_case(exp!(Const), case)
1276 {
1277 Const::Yes(self.prev_token_uninterpolated_span())
1278 } else {
1279 Const::No
1280 }
1281 }
1282
1283 fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, P<Expr>> {
1285 self.expect_keyword(exp!(Const))?;
1286 let (attrs, blk) = self.parse_inner_attrs_and_block(None)?;
1287 let anon_const = AnonConst {
1288 id: DUMMY_NODE_ID,
1289 value: self.mk_expr(blk.span, ExprKind::Block(blk, None)),
1290 };
1291 let blk_span = anon_const.value.span;
1292 let kind = if pat {
1293 let guar = self
1294 .dcx()
1295 .struct_span_err(blk_span, "`inline_const_pat` has been removed")
1296 .with_help("use a named `const`-item or an `if`-guard instead")
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 fn parse_mutability(&mut self) -> Mutability {
1307 if self.eat_keyword(exp!(Mut)) { Mutability::Mut } else { Mutability::Not }
1308 }
1309
1310 fn parse_byref(&mut self) -> ByRef {
1312 if self.eat_keyword(exp!(Ref)) { ByRef::Yes(self.parse_mutability()) } else { ByRef::No }
1313 }
1314
1315 fn parse_const_or_mut(&mut self) -> Option<Mutability> {
1317 if self.eat_keyword(exp!(Mut)) {
1318 Some(Mutability::Mut)
1319 } else if self.eat_keyword(exp!(Const)) {
1320 Some(Mutability::Not)
1321 } else {
1322 None
1323 }
1324 }
1325
1326 fn parse_field_name(&mut self) -> PResult<'a, Ident> {
1327 if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
1328 {
1329 if let Some(suffix) = suffix {
1330 self.expect_no_tuple_index_suffix(self.token.span, suffix);
1331 }
1332 self.bump();
1333 Ok(Ident::new(symbol, self.prev_token.span))
1334 } else {
1335 self.parse_ident_common(true)
1336 }
1337 }
1338
1339 fn parse_delim_args(&mut self) -> PResult<'a, P<DelimArgs>> {
1340 if let Some(args) = self.parse_delim_args_inner() {
1341 Ok(P(args))
1342 } else {
1343 self.unexpected_any()
1344 }
1345 }
1346
1347 fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> {
1348 Ok(if let Some(args) = self.parse_delim_args_inner() {
1349 AttrArgs::Delimited(args)
1350 } else if self.eat(exp!(Eq)) {
1351 let eq_span = self.prev_token.span;
1352 AttrArgs::Eq { eq_span, expr: self.parse_expr_force_collect()? }
1353 } else {
1354 AttrArgs::Empty
1355 })
1356 }
1357
1358 fn parse_delim_args_inner(&mut self) -> Option<DelimArgs> {
1359 let delimited = self.check(exp!(OpenParen))
1360 || self.check(exp!(OpenBracket))
1361 || self.check(exp!(OpenBrace));
1362
1363 delimited.then(|| {
1364 let TokenTree::Delimited(dspan, _, delim, tokens) = self.parse_token_tree() else {
1365 unreachable!()
1366 };
1367 DelimArgs { dspan, delim, tokens }
1368 })
1369 }
1370
1371 pub fn parse_token_tree(&mut self) -> TokenTree {
1373 if self.token.kind.open_delim().is_some() {
1374 let tree = self.token_cursor.stack.last().unwrap().curr().unwrap().clone();
1377 debug_assert_matches!(tree, TokenTree::Delimited(..));
1378
1379 let target_depth = self.token_cursor.stack.len() - 1;
1385 loop {
1386 self.bump();
1389 if self.token_cursor.stack.len() == target_depth {
1390 debug_assert!(self.token.kind.close_delim().is_some());
1391 break;
1392 }
1393 }
1394
1395 self.bump();
1397 tree
1398 } else {
1399 assert!(!self.token.kind.is_close_delim_or_eof());
1400 let prev_spacing = self.token_spacing;
1401 self.bump();
1402 TokenTree::Token(self.prev_token, prev_spacing)
1403 }
1404 }
1405
1406 pub fn parse_tokens(&mut self) -> TokenStream {
1407 let mut result = Vec::new();
1408 loop {
1409 if self.token.kind.is_close_delim_or_eof() {
1410 break;
1411 } else {
1412 result.push(self.parse_token_tree());
1413 }
1414 }
1415 TokenStream::new(result)
1416 }
1417
1418 fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
1422 let old = self.restrictions;
1423 self.restrictions = res;
1424 let res = f(self);
1425 self.restrictions = old;
1426 res
1427 }
1428
1429 pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
1436 if let Some(vis) = self
1437 .eat_metavar_seq(MetaVarKind::Vis, |this| this.parse_visibility(FollowedByType::Yes))
1438 {
1439 return Ok(vis);
1440 }
1441
1442 if !self.eat_keyword(exp!(Pub)) {
1443 return Ok(Visibility {
1447 span: self.token.span.shrink_to_lo(),
1448 kind: VisibilityKind::Inherited,
1449 tokens: None,
1450 });
1451 }
1452 let lo = self.prev_token.span;
1453
1454 if self.check(exp!(OpenParen)) {
1455 if self.is_keyword_ahead(1, &[kw::In]) {
1460 self.bump(); self.bump(); let path = self.parse_path(PathStyle::Mod)?; self.expect(exp!(CloseParen))?; let vis = VisibilityKind::Restricted {
1466 path: P(path),
1467 id: ast::DUMMY_NODE_ID,
1468 shorthand: false,
1469 };
1470 return Ok(Visibility {
1471 span: lo.to(self.prev_token.span),
1472 kind: vis,
1473 tokens: None,
1474 });
1475 } else if self.look_ahead(2, |t| t == &token::CloseParen)
1476 && self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
1477 {
1478 self.bump(); let path = self.parse_path(PathStyle::Mod)?; self.expect(exp!(CloseParen))?; let vis = VisibilityKind::Restricted {
1483 path: P(path),
1484 id: ast::DUMMY_NODE_ID,
1485 shorthand: true,
1486 };
1487 return Ok(Visibility {
1488 span: lo.to(self.prev_token.span),
1489 kind: vis,
1490 tokens: None,
1491 });
1492 } else if let FollowedByType::No = fbt {
1493 self.recover_incorrect_vis_restriction()?;
1496 }
1498 }
1499
1500 Ok(Visibility { span: lo, kind: VisibilityKind::Public, tokens: None })
1501 }
1502
1503 fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
1505 self.bump(); let path = self.parse_path(PathStyle::Mod)?;
1507 self.expect(exp!(CloseParen))?; let path_str = pprust::path_to_string(&path);
1510 self.dcx()
1511 .emit_err(IncorrectVisibilityRestriction { span: path.span, inner_str: path_str });
1512
1513 Ok(())
1514 }
1515
1516 fn parse_extern(&mut self, case: Case) -> Extern {
1518 if self.eat_keyword_case(exp!(Extern), case) {
1519 let mut extern_span = self.prev_token.span;
1520 let abi = self.parse_abi();
1521 if let Some(abi) = abi {
1522 extern_span = extern_span.to(abi.span);
1523 }
1524 Extern::from_abi(abi, extern_span)
1525 } else {
1526 Extern::None
1527 }
1528 }
1529
1530 fn parse_abi(&mut self) -> Option<StrLit> {
1532 match self.parse_str_lit() {
1533 Ok(str_lit) => Some(str_lit),
1534 Err(Some(lit)) => match lit.kind {
1535 ast::LitKind::Err(_) => None,
1536 _ => {
1537 self.dcx().emit_err(NonStringAbiLiteral { span: lit.span });
1538 None
1539 }
1540 },
1541 Err(None) => None,
1542 }
1543 }
1544
1545 fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
1546 &mut self,
1547 f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1548 ) -> PResult<'a, R> {
1549 self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |this, _attrs| {
1552 Ok((f(this)?, Trailing::No, UsePreAttrPos::No))
1553 })
1554 }
1555
1556 fn check_path_sep_and_look_ahead(&mut self, looker: impl Fn(&Token) -> bool) -> bool {
1558 if self.check(exp!(PathSep)) {
1559 if self.may_recover() && self.look_ahead(1, |t| t.kind == token::Colon) {
1560 debug_assert!(!self.look_ahead(1, &looker), "Looker must not match on colon");
1561 self.look_ahead(2, looker)
1562 } else {
1563 self.look_ahead(1, looker)
1564 }
1565 } else {
1566 false
1567 }
1568 }
1569
1570 fn is_import_coupler(&mut self) -> bool {
1572 self.check_path_sep_and_look_ahead(|t| matches!(t.kind, token::OpenBrace | token::Star))
1573 }
1574
1575 #[allow(unused)]
1578 pub(crate) fn debug_lookahead(&self, lookahead: usize) -> impl fmt::Debug {
1579 fmt::from_fn(move |f| {
1580 let mut dbg_fmt = f.debug_struct("Parser"); dbg_fmt.field("prev_token", &self.prev_token);
1584 let mut tokens = vec![];
1585 for i in 0..lookahead {
1586 let tok = self.look_ahead(i, |tok| tok.kind);
1587 let is_eof = tok == TokenKind::Eof;
1588 tokens.push(tok);
1589 if is_eof {
1590 break;
1592 }
1593 }
1594 dbg_fmt.field_with("tokens", |field| field.debug_list().entries(tokens).finish());
1595 dbg_fmt.field("approx_token_stream_pos", &self.num_bump_calls);
1596
1597 if let Some(subparser) = self.subparser_name {
1599 dbg_fmt.field("subparser_name", &subparser);
1600 }
1601 if let Recovery::Forbidden = self.recovery {
1602 dbg_fmt.field("recovery", &self.recovery);
1603 }
1604
1605 dbg_fmt.finish_non_exhaustive()
1607 })
1608 }
1609
1610 pub fn clear_expected_token_types(&mut self) {
1611 self.expected_token_types.clear();
1612 }
1613
1614 pub fn approx_token_stream_pos(&self) -> u32 {
1615 self.num_bump_calls
1616 }
1617
1618 pub fn token_uninterpolated_span(&self) -> Span {
1625 match &self.token.kind {
1626 token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1627 token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => self.look_ahead(1, |t| t.span),
1628 _ => self.token.span,
1629 }
1630 }
1631
1632 pub fn prev_token_uninterpolated_span(&self) -> Span {
1634 match &self.prev_token.kind {
1635 token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1636 token::OpenInvisible(InvisibleOrigin::MetaVar(_)) => self.look_ahead(0, |t| t.span),
1637 _ => self.prev_token.span,
1638 }
1639 }
1640}
1641
1642#[derive(Clone, Debug)]
1644pub enum ParseNtResult {
1645 Tt(TokenTree),
1646 Ident(Ident, IdentIsRaw),
1647 Lifetime(Ident, IdentIsRaw),
1648 Item(P<ast::Item>),
1649 Block(P<ast::Block>),
1650 Stmt(P<ast::Stmt>),
1651 Pat(P<ast::Pat>, NtPatKind),
1652 Expr(P<ast::Expr>, NtExprKind),
1653 Literal(P<ast::Expr>),
1654 Ty(P<ast::Ty>),
1655 Meta(P<ast::AttrItem>),
1656 Path(P<ast::Path>),
1657 Vis(P<ast::Visibility>),
1658}