]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_parse/src/parser/mod.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / compiler / rustc_parse / src / parser / mod.rs
CommitLineData
e74abb32 1pub mod attr;
6a06907d 2mod attr_wrapper;
3dfed10e 3mod diagnostics;
416331ca 4mod expr;
3dfed10e 5mod generics;
416331ca 6mod item;
3dfed10e 7mod nonterminal;
dfeec247 8mod pat;
416331ca 9mod path;
dfeec247 10mod stmt;
3dfed10e 11mod ty;
416331ca 12
60c5eb7d 13use crate::lexer::UnmatchedBrace;
6a06907d 14pub use attr_wrapper::AttrWrapper;
29967ef6 15pub use diagnostics::AttemptLocalParseRecovery;
a2a8927a 16pub(crate) use item::FnParseMode;
5e7ed085 17pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
3dfed10e 18pub use path::PathStyle;
60c5eb7d 19
74b04a01 20use rustc_ast::ptr::P;
04454e1e 21use rustc_ast::token::{self, Delimiter, Nonterminal, Token, TokenKind};
cdc7bbd5 22use rustc_ast::tokenstream::AttributesData;
6a06907d 23use rustc_ast::tokenstream::{self, DelimSpan, Spacing};
cdc7bbd5 24use rustc_ast::tokenstream::{TokenStream, TokenTree};
487cf647 25use rustc_ast::util::case::Case;
cdc7bbd5 26use rustc_ast::AttrId;
3dfed10e 27use rustc_ast::DUMMY_NODE_ID;
487cf647
FG
28use rustc_ast::{self as ast, AnonConst, AttrStyle, AttrVec, Const, DelimArgs, Extern};
29use rustc_ast::{Async, AttrArgs, AttrArgsEq, Expr, ExprKind, MacDelimiter, Mutability, StrLit};
04454e1e 30use rustc_ast::{HasAttrs, HasTokens, Unsafe, Visibility, VisibilityKind};
74b04a01 31use rustc_ast_pretty::pprust;
cdc7bbd5 32use rustc_data_structures::fx::FxHashMap;
29967ef6 33use rustc_errors::PResult;
5e7ed085 34use rustc_errors::{
2b03887a 35 Applicability, DiagnosticBuilder, ErrorGuaranteed, FatalError, IntoDiagnostic, MultiSpan,
5e7ed085 36};
74b04a01 37use rustc_session::parse::ParseSess;
04454e1e 38use rustc_span::source_map::{Span, DUMMY_SP};
f9f354fc 39use rustc_span::symbol::{kw, sym, Ident, Symbol};
74b04a01 40
cdc7bbd5 41use std::ops::Range;
dfeec247 42use std::{cmp, mem, slice};
60c5eb7d 43
2b03887a
FG
44use crate::errors::{
45 DocCommentDoesNotDocumentAnything, IncorrectVisibilityRestriction, MismatchedClosingDelimiter,
46 NonStringAbiLiteral,
47};
48
9fa01778 49bitflags::bitflags! {
94b46f34 50 struct Restrictions: u8 {
ea8adc8c
XL
51 const STMT_EXPR = 1 << 0;
52 const NO_STRUCT_LITERAL = 1 << 1;
29967ef6 53 const CONST_EXPR = 1 << 2;
064997fb 54 const ALLOW_LET = 1 << 3;
1a4d82fc 55 }
223e47cc
LB
56}
57
8faf50e0 58#[derive(Clone, Copy, PartialEq, Debug)]
e74abb32 59enum SemiColonMode {
7453a54e
SL
60 Break,
61 Ignore,
9fa01778 62 Comma,
7453a54e
SL
63}
64
8faf50e0 65#[derive(Clone, Copy, PartialEq, Debug)]
e74abb32 66enum BlockMode {
cc61c64b
XL
67 Break,
68 Ignore,
69}
70
5869c6ff
XL
71/// Whether or not we should force collection of tokens for an AST node,
72/// regardless of whether or not it has attributes
17df50a5 73#[derive(Clone, Copy, PartialEq)]
5869c6ff
XL
74pub enum ForceCollect {
75 Yes,
76 No,
77}
78
cdc7bbd5 79#[derive(Debug, Eq, PartialEq)]
5869c6ff
XL
80pub enum TrailingToken {
81 None,
82 Semi,
2b03887a 83 Gt,
6a06907d
XL
84 /// If the trailing token is a comma, then capture it
85 /// Otherwise, ignore the trailing token
86 MaybeComma,
5869c6ff
XL
87}
88
e1599b0c 89/// Like `maybe_whole_expr`, but for things other than expressions.
416331ca 90#[macro_export]
1a4d82fc 91macro_rules! maybe_whole {
c30ab7b3 92 ($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
dc9dc135 93 if let token::Interpolated(nt) = &$p.token.kind {
532ac7d7
XL
94 if let token::$constructor(x) = &**nt {
95 let $x = x.clone();
c30ab7b3
SL
96 $p.bump();
97 return Ok($e);
223e47cc 98 }
1a4d82fc 99 }
c30ab7b3 100 };
1a4d82fc 101}
223e47cc 102
532ac7d7 103/// If the next tokens are ill-formed `$ty::` recover them as `<$ty>::`.
416331ca 104#[macro_export]
532ac7d7
XL
105macro_rules! maybe_recover_from_interpolated_ty_qpath {
106 ($self: expr, $allow_qpath_recovery: expr) => {
5e7ed085 107 if $allow_qpath_recovery
487cf647 108 && $self.may_recover()
5e7ed085
FG
109 && $self.look_ahead(1, |t| t == &token::ModSep)
110 && let token::Interpolated(nt) = &$self.token.kind
111 && let token::NtTy(ty) = &**nt
112 {
532ac7d7
XL
113 let ty = ty.clone();
114 $self.bump();
74b04a01 115 return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
532ac7d7 116 }
dfeec247 117 };
532ac7d7
XL
118}
119
2b03887a
FG
120#[derive(Clone, Copy)]
121pub enum Recovery {
122 Allowed,
123 Forbidden,
124}
125
041b39d2 126#[derive(Clone)]
1a4d82fc
JJ
127pub struct Parser<'a> {
128 pub sess: &'a ParseSess,
74b04a01 129 /// The current token.
dc9dc135 130 pub token: Token,
29967ef6
XL
131 /// The spacing for the current token
132 pub token_spacing: Spacing,
74b04a01
XL
133 /// The previous token.
134 pub prev_token: Token,
cdc7bbd5 135 pub capture_cfg: bool,
94b46f34 136 restrictions: Restrictions,
e74abb32 137 expected_tokens: Vec<TokenType>,
04454e1e
FG
138 // Important: This must only be advanced from `bump` to ensure that
139 // `token_cursor.num_next_calls` is updated properly.
e1599b0c 140 token_cursor: TokenCursor,
94b46f34 141 desugar_doc_comments: bool,
9fa01778
XL
142 /// This field is used to keep track of how many left angle brackets we have seen. This is
143 /// required in order to detect extra leading left angle brackets (`<` characters) and error
144 /// appropriately.
145 ///
146 /// See the comments in the `parse_path_segment` function for more details.
e74abb32
XL
147 unmatched_angle_bracket_count: u32,
148 max_angle_bracket_count: u32,
e1599b0c 149 /// A list of all unclosed delimiters found by the lexer. If an entry is used for error recovery
9fa01778
XL
150 /// it gets removed from here. Every entry left at the end gets emitted as an independent
151 /// error.
e74abb32
XL
152 pub(super) unclosed_delims: Vec<UnmatchedBrace>,
153 last_unexpected_token_span: Option<Span>,
3dfed10e
XL
154 /// Span pointing at the `:` for the last type ascription the parser has seen, and whether it
155 /// looked like it could have been a mistyped path or literal `Option:Some(42)`).
e74abb32 156 pub last_type_ascription: Option<(Span, bool /* likely path typo */)>,
dc9dc135 157 /// If present, this `Parser` is not parsing Rust code but rather a macro call.
e74abb32 158 subparser_name: Option<&'static str>,
cdc7bbd5 159 capture_state: CaptureState,
c295e0f8
XL
160 /// This allows us to recover when the user forget to add braces around
161 /// multiple statements in the closure body.
162 pub current_closure: Option<ClosureSpans>,
2b03887a
FG
163 /// Whether the parser is allowed to do recovery.
164 /// This is disabled when parsing macro arguments, see #103534
165 pub recovery: Recovery,
c295e0f8
XL
166}
167
2b03887a 168// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
04454e1e
FG
169// it doesn't unintentionally get bigger.
170#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
2b03887a 171rustc_data_structures::static_assert_size!(Parser<'_>, 336);
04454e1e 172
5e7ed085 173/// Stores span information about a closure.
c295e0f8
XL
174#[derive(Clone)]
175pub struct ClosureSpans {
176 pub whole_closure: Span,
177 pub closing_pipe: Span,
178 pub body: Span,
cdc7bbd5
XL
179}
180
181/// Indicates a range of tokens that should be replaced by
182/// the tokens in the provided vector. This is used in two
183/// places during token collection:
184///
185/// 1. During the parsing of an AST node that may have a `#[derive]`
186/// attribute, we parse a nested AST node that has `#[cfg]` or `#[cfg_attr]`
187/// In this case, we use a `ReplaceRange` to replace the entire inner AST node
188/// with `FlatToken::AttrTarget`, allowing us to perform eager cfg-expansion
f2b60f7d 189/// on an `AttrTokenStream`.
cdc7bbd5
XL
190///
191/// 2. When we parse an inner attribute while collecting tokens. We
192/// remove inner attributes from the token stream entirely, and
193/// instead track them through the `attrs` field on the AST node.
194/// This allows us to easily manipulate them (for example, removing
195/// the first macro inner attribute to invoke a proc-macro).
196/// When create a `TokenStream`, the inner attributes get inserted
197/// into the proper place in the token stream.
198pub type ReplaceRange = (Range<u32>, Vec<(FlatToken, Spacing)>);
199
200/// Controls how we capture tokens. Capturing can be expensive,
201/// so we try to avoid performing capturing in cases where
f2b60f7d 202/// we will never need an `AttrTokenStream`.
cdc7bbd5
XL
203#[derive(Copy, Clone)]
204pub enum Capturing {
205 /// We aren't performing any capturing - this is the default mode.
206 No,
207 /// We are capturing tokens
208 Yes,
209}
210
211#[derive(Clone)]
212struct CaptureState {
213 capturing: Capturing,
214 replace_ranges: Vec<ReplaceRange>,
215 inner_attr_ranges: FxHashMap<AttrId, ReplaceRange>,
1a4d82fc
JJ
216}
217
9fa01778
XL
218impl<'a> Drop for Parser<'a> {
219 fn drop(&mut self) {
e74abb32 220 emit_unclosed_delims(&mut self.unclosed_delims, &self.sess);
9fa01778
XL
221 }
222}
7cac9316 223
041b39d2 224#[derive(Clone)]
e1599b0c 225struct TokenCursor {
04454e1e
FG
226 // The current (innermost) frame. `frame` and `stack` could be combined,
227 // but it's faster to have them separately to access `frame` directly
228 // rather than via something like `stack.last().unwrap()` or
229 // `stack[stack.len() - 1]`.
e1599b0c 230 frame: TokenCursorFrame,
04454e1e 231 // Additional frames that enclose `frame`.
e1599b0c 232 stack: Vec<TokenCursorFrame>,
29967ef6 233 desugar_doc_comments: bool,
04454e1e 234 // Counts the number of calls to `{,inlined_}next`.
29967ef6 235 num_next_calls: usize,
fc512014
XL
236 // During parsing, we may sometimes need to 'unglue' a
237 // glued token into two component tokens
238 // (e.g. '>>' into '>' and '>), so that the parser
239 // can consume them one at a time. This process
240 // bypasses the normal capturing mechanism
241 // (e.g. `num_next_calls` will not be incremented),
242 // since the 'unglued' tokens due not exist in
243 // the original `TokenStream`.
244 //
245 // If we end up consuming both unglued tokens,
246 // then this is not an issue - we'll end up
247 // capturing the single 'glued' token.
248 //
249 // However, in certain circumstances, we may
250 // want to capture just the first 'unglued' token.
251 // For example, capturing the `Vec<u8>`
252 // in `Option<Vec<u8>>` requires us to unglue
cdc7bbd5 253 // the trailing `>>` token. The `break_last_token`
fc512014
XL
254 // field is used to track this token - it gets
255 // appended to the captured stream when
f2b60f7d 256 // we evaluate a `LazyAttrTokenStream`.
cdc7bbd5 257 break_last_token: bool,
8bb4bdeb
XL
258}
259
041b39d2 260#[derive(Clone)]
e1599b0c 261struct TokenCursorFrame {
04454e1e 262 delim_sp: Option<(Delimiter, DelimSpan)>,
e1599b0c 263 tree_cursor: tokenstream::Cursor,
3b2f2976
XL
264}
265
8bb4bdeb 266impl TokenCursorFrame {
04454e1e
FG
267 fn new(delim_sp: Option<(Delimiter, DelimSpan)>, tts: TokenStream) -> Self {
268 TokenCursorFrame { delim_sp, tree_cursor: tts.into_trees() }
8bb4bdeb
XL
269 }
270}
271
272impl TokenCursor {
04454e1e
FG
273 fn next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
274 self.inlined_next(desugar_doc_comments)
5e7ed085
FG
275 }
276
277 /// This always-inlined version should only be used on hot code paths.
278 #[inline(always)]
04454e1e 279 fn inlined_next(&mut self, desugar_doc_comments: bool) -> (Token, Spacing) {
8bb4bdeb 280 loop {
04454e1e
FG
281 // FIXME: we currently don't return `Delimiter` open/close delims. To fix #67062 we will
282 // need to, whereupon the `delim != Delimiter::Invisible` conditions below can be
283 // removed.
064997fb 284 if let Some(tree) = self.frame.tree_cursor.next_ref() {
04454e1e 285 match tree {
064997fb 286 &TokenTree::Token(ref token, spacing) => match (desugar_doc_comments, token) {
04454e1e
FG
287 (true, &Token { kind: token::DocComment(_, attr_style, data), span }) => {
288 return self.desugar(attr_style, data, span);
289 }
064997fb 290 _ => return (token.clone(), spacing),
04454e1e
FG
291 },
292 &TokenTree::Delimited(sp, delim, ref tts) => {
293 // Set `open_delim` to true here because we deal with it immediately.
294 let frame = TokenCursorFrame::new(Some((delim, sp)), tts.clone());
295 self.stack.push(mem::replace(&mut self.frame, frame));
296 if delim != Delimiter::Invisible {
297 return (Token::new(token::OpenDelim(delim), sp.open), Spacing::Alone);
298 }
f2b60f7d 299 // No open delimiter to return; continue on to the next iteration.
04454e1e
FG
300 }
301 };
8bb4bdeb 302 } else if let Some(frame) = self.stack.pop() {
04454e1e
FG
303 if let Some((delim, span)) = self.frame.delim_sp && delim != Delimiter::Invisible {
304 self.frame = frame;
305 return (Token::new(token::CloseDelim(delim), span.close), Spacing::Alone);
306 }
8bb4bdeb 307 self.frame = frame;
04454e1e 308 // No close delimiter to return; continue on to the next iteration.
8bb4bdeb 309 } else {
04454e1e 310 return (Token::new(token::Eof, DUMMY_SP), Spacing::Alone);
8bb4bdeb
XL
311 }
312 }
313 }
314
04454e1e 315 fn desugar(&mut self, attr_style: AttrStyle, data: Symbol, span: Span) -> (Token, Spacing) {
8bb4bdeb 316 // Searches for the occurrences of `"#*` and returns the minimum number of `#`s
2b03887a
FG
317 // required to wrap the text. E.g.
318 // - `abc d` is wrapped as `r"abc d"` (num_of_hashes = 0)
319 // - `abc "d"` is wrapped as `r#"abc "d""#` (num_of_hashes = 1)
320 // - `abc "##d##"` is wrapped as `r###"abc "d""###` (num_of_hashes = 3)
8bb4bdeb
XL
321 let mut num_of_hashes = 0;
322 let mut count = 0;
3dfed10e 323 for ch in data.as_str().chars() {
8bb4bdeb
XL
324 count = match ch {
325 '"' => 1,
326 '#' if count > 0 => count + 1,
327 _ => 0,
328 };
329 num_of_hashes = cmp::max(num_of_hashes, count);
330 }
331
2b03887a 332 // `/// foo` becomes `doc = r"foo".
04454e1e 333 let delim_span = DelimSpan::from_single(span);
0731742a
XL
334 let body = TokenTree::Delimited(
335 delim_span,
04454e1e 336 Delimiter::Bracket,
dc9dc135 337 [
064997fb
FG
338 TokenTree::token_alone(token::Ident(sym::doc, false), span),
339 TokenTree::token_alone(token::Eq, span),
340 TokenTree::token_alone(
341 TokenKind::lit(token::StrRaw(num_of_hashes), data, None),
342 span,
343 ),
0731742a 344 ]
064997fb 345 .into_iter()
74b04a01 346 .collect::<TokenStream>(),
0731742a 347 );
8bb4bdeb 348
dfeec247
XL
349 self.stack.push(mem::replace(
350 &mut self.frame,
351 TokenCursorFrame::new(
04454e1e 352 None,
29967ef6 353 if attr_style == AttrStyle::Inner {
064997fb
FG
354 [
355 TokenTree::token_alone(token::Pound, span),
356 TokenTree::token_alone(token::Not, span),
357 body,
358 ]
359 .into_iter()
360 .collect::<TokenStream>()
dfeec247 361 } else {
064997fb
FG
362 [TokenTree::token_alone(token::Pound, span), body]
363 .into_iter()
dfeec247
XL
364 .collect::<TokenStream>()
365 },
366 ),
367 ));
8bb4bdeb 368
04454e1e 369 self.next(/* desugar_doc_comments */ false)
8bb4bdeb
XL
370 }
371}
372
5869c6ff 373#[derive(Debug, Clone, PartialEq)]
e74abb32 374enum TokenType {
dc9dc135
XL
375 Token(TokenKind),
376 Keyword(Symbol),
1a4d82fc 377 Operator,
32a655c1
SL
378 Lifetime,
379 Ident,
380 Path,
381 Type,
9fa01778 382 Const,
1a4d82fc 383}
223e47cc 384
1a4d82fc 385impl TokenType {
e74abb32 386 fn to_string(&self) -> String {
487cf647
FG
387 match self {
388 TokenType::Token(t) => format!("`{}`", pprust::token_kind_to_string(t)),
dc9dc135 389 TokenType::Keyword(kw) => format!("`{}`", kw),
32a655c1
SL
390 TokenType::Operator => "an operator".to_string(),
391 TokenType::Lifetime => "lifetime".to_string(),
392 TokenType::Ident => "identifier".to_string(),
393 TokenType::Path => "path".to_string(),
394 TokenType::Type => "type".to_string(),
fc512014 395 TokenType::Const => "a const expression".to_string(),
1a4d82fc
JJ
396 }
397 }
223e47cc
LB
398}
399
8faf50e0 400#[derive(Copy, Clone, Debug)]
e74abb32 401enum TokenExpectType {
ea8adc8c
XL
402 Expect,
403 NoExpect,
404}
405
e74abb32
XL
406/// A sequence separator.
407struct SeqSep {
408 /// The separator token.
409 sep: Option<TokenKind>,
410 /// `true` if a trailing separator is allowed.
411 trailing_sep_allowed: bool,
412}
413
414impl SeqSep {
415 fn trailing_allowed(t: TokenKind) -> SeqSep {
dfeec247 416 SeqSep { sep: Some(t), trailing_sep_allowed: true }
e74abb32
XL
417 }
418
419 fn none() -> SeqSep {
dfeec247 420 SeqSep { sep: None, trailing_sep_allowed: false }
e74abb32
XL
421 }
422}
423
dfeec247
XL
424pub enum FollowedByType {
425 Yes,
426 No,
427}
428
2b03887a
FG
429#[derive(Clone, Copy, PartialEq, Eq)]
430pub enum TokenDescription {
431 ReservedIdentifier,
432 Keyword,
433 ReservedKeyword,
434 DocComment,
dfeec247
XL
435}
436
2b03887a
FG
437impl TokenDescription {
438 pub fn from_token(token: &Token) -> Option<Self> {
439 match token.kind {
440 _ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
441 _ if token.is_used_keyword() => Some(TokenDescription::Keyword),
442 _ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
443 token::DocComment(..) => Some(TokenDescription::DocComment),
444 _ => None,
445 }
dfeec247
XL
446 }
447}
60c5eb7d 448
2b03887a
FG
449pub(super) fn token_descr(token: &Token) -> String {
450 let name = pprust::token_to_string(token).to_string();
451
452 let kind = TokenDescription::from_token(token).map(|kind| match kind {
453 TokenDescription::ReservedIdentifier => "reserved identifier",
454 TokenDescription::Keyword => "keyword",
455 TokenDescription::ReservedKeyword => "reserved keyword",
456 TokenDescription::DocComment => "doc comment",
457 });
458
459 if let Some(kind) = kind { format!("{} `{}`", kind, name) } else { format!("`{}`", name) }
460}
461
1a4d82fc 462impl<'a> Parser<'a> {
dc9dc135
XL
463 pub fn new(
464 sess: &'a ParseSess,
465 tokens: TokenStream,
dc9dc135
XL
466 desugar_doc_comments: bool,
467 subparser_name: Option<&'static str>,
468 ) -> Self {
c30ab7b3 469 let mut parser = Parser {
3b2f2976 470 sess,
dc9dc135 471 token: Token::dummy(),
29967ef6 472 token_spacing: Spacing::Alone,
74b04a01 473 prev_token: Token::dummy(),
cdc7bbd5 474 capture_cfg: false,
d9579d0f 475 restrictions: Restrictions::empty(),
1a4d82fc 476 expected_tokens: Vec::new(),
8bb4bdeb 477 token_cursor: TokenCursor {
04454e1e 478 frame: TokenCursorFrame::new(None, tokens),
8bb4bdeb 479 stack: Vec::new(),
29967ef6
XL
480 num_next_calls: 0,
481 desugar_doc_comments,
cdc7bbd5 482 break_last_token: false,
8bb4bdeb 483 },
3b2f2976 484 desugar_doc_comments,
9fa01778
XL
485 unmatched_angle_bracket_count: 0,
486 max_angle_bracket_count: 0,
487 unclosed_delims: Vec::new(),
488 last_unexpected_token_span: None,
416331ca 489 last_type_ascription: None,
dc9dc135 490 subparser_name,
cdc7bbd5
XL
491 capture_state: CaptureState {
492 capturing: Capturing::No,
493 replace_ranges: Vec::new(),
494 inner_attr_ranges: Default::default(),
495 },
c295e0f8 496 current_closure: None,
2b03887a 497 recovery: Recovery::Allowed,
c30ab7b3
SL
498 };
499
74b04a01
XL
500 // Make parser point to the first token.
501 parser.bump();
7cac9316 502
c30ab7b3
SL
503 parser
504 }
505
487cf647
FG
506 pub fn recovery(mut self, recovery: Recovery) -> Self {
507 self.recovery = recovery;
2b03887a
FG
508 self
509 }
510
511 /// Whether the parser is allowed to recover from broken code.
512 ///
513 /// If this returns false, recovering broken code into valid code (especially if this recovery does lookahead)
514 /// is not allowed. All recovery done by the parser must be gated behind this check.
515 ///
516 /// Technically, this only needs to restrict eager recovery by doing lookahead at more tokens.
517 /// But making the distinction is very subtle, and simply forbidding all recovery is a lot simpler to uphold.
518 fn may_recover(&self) -> bool {
519 matches!(self.recovery, Recovery::Allowed)
520 }
521
29967ef6 522 pub fn unexpected<T>(&mut self) -> PResult<'a, T> {
9346a6ac 523 match self.expect_one_of(&[], &[]) {
9cc50fc6 524 Err(e) => Err(e),
e74abb32
XL
525 // We can get `Ok(true)` from `recover_closing_delimiter`
526 // which is called in `expected_one_of_not_found`.
527 Ok(_) => FatalError.raise(),
9346a6ac 528 }
1a4d82fc
JJ
529 }
530
9fa01778 531 /// Expects and consumes the token `t`. Signals an error if the next token is not `t`.
dc9dc135 532 pub fn expect(&mut self, t: &TokenKind) -> PResult<'a, bool /* recovered */> {
1a4d82fc
JJ
533 if self.expected_tokens.is_empty() {
534 if self.token == *t {
9cc50fc6 535 self.bump();
9fa01778 536 Ok(false)
1a4d82fc 537 } else {
dc9dc135 538 self.unexpected_try_recover(t)
1a4d82fc
JJ
539 }
540 } else {
94b46f34 541 self.expect_one_of(slice::from_ref(t), &[])
1a4d82fc
JJ
542 }
543 }
544
545 /// Expect next token to be edible or inedible token. If edible,
546 /// then consume it; if inedible, then return without consuming
547 /// anything. Signal a fatal error if next token is unexpected.
9fa01778
XL
548 pub fn expect_one_of(
549 &mut self,
dc9dc135
XL
550 edible: &[TokenKind],
551 inedible: &[TokenKind],
9fa01778 552 ) -> PResult<'a, bool /* recovered */> {
dc9dc135 553 if edible.contains(&self.token.kind) {
9cc50fc6 554 self.bump();
9fa01778 555 Ok(false)
dc9dc135 556 } else if inedible.contains(&self.token.kind) {
1a4d82fc 557 // leave it in the input
9fa01778 558 Ok(false)
dc9dc135 559 } else if self.last_unexpected_token_span == Some(self.token.span) {
9fa01778 560 FatalError.raise();
1a4d82fc 561 } else {
dc9dc135 562 self.expected_one_of_not_found(edible, inedible)
1a4d82fc 563 }
970d7e83
LB
564 }
565
dfeec247 566 // Public for rustfmt usage.
f9f354fc 567 pub fn parse_ident(&mut self) -> PResult<'a, Ident> {
2c00a5a8
XL
568 self.parse_ident_common(true)
569 }
570
17df50a5
XL
571 fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
572 self.token.ident().ok_or_else(|| match self.prev_token.kind {
2b03887a
FG
573 TokenKind::DocComment(..) => DocCommentDoesNotDocumentAnything {
574 span: self.prev_token.span,
575 missing_comma: None,
17df50a5 576 }
2b03887a 577 .into_diagnostic(&self.sess.span_diagnostic),
17df50a5
XL
578 _ => self.expected_ident_found(),
579 })
580 }
581
f9f354fc 582 fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
17df50a5
XL
583 let (ident, is_raw) = self.ident_or_err()?;
584 if !is_raw && ident.is_reserved() {
585 let mut err = self.expected_ident_found();
586 if recover {
587 err.emit();
588 } else {
589 return Err(err);
970d7e83 590 }
970d7e83 591 }
17df50a5
XL
592 self.bump();
593 Ok(ident)
970d7e83
LB
594 }
595
9fa01778 596 /// Checks if the next token is `tok`, and returns `true` if so.
1a4d82fc 597 ///
7453a54e 598 /// This method will automatically add `tok` to `expected_tokens` if `tok` is not
1a4d82fc 599 /// encountered.
e74abb32 600 fn check(&mut self, tok: &TokenKind) -> bool {
1a4d82fc 601 let is_present = self.token == *tok;
dfeec247
XL
602 if !is_present {
603 self.expected_tokens.push(TokenType::Token(tok.clone()));
604 }
1a4d82fc 605 is_present
970d7e83
LB
606 }
607
923072b8
FG
608 fn check_noexpect(&self, tok: &TokenKind) -> bool {
609 self.token == *tok
610 }
611
612 /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
613 ///
614 /// the main purpose of this function is to reduce the cluttering of the suggestions list
615 /// which using the normal eat method could introduce in some cases.
616 pub fn eat_noexpect(&mut self, tok: &TokenKind) -> bool {
617 let is_present = self.check_noexpect(tok);
618 if is_present {
619 self.bump()
620 }
621 is_present
622 }
623
9fa01778 624 /// Consumes a token 'tok' if it exists. Returns whether the given token was present.
dc9dc135 625 pub fn eat(&mut self, tok: &TokenKind) -> bool {
1a4d82fc 626 let is_present = self.check(tok);
dfeec247
XL
627 if is_present {
628 self.bump()
629 }
9cc50fc6 630 is_present
970d7e83
LB
631 }
632
e74abb32
XL
633 /// If the next token is the given keyword, returns `true` without eating it.
634 /// An expectation is also added for diagnostics purposes.
dc9dc135 635 fn check_keyword(&mut self, kw: Symbol) -> bool {
85aaf69f
SL
636 self.expected_tokens.push(TokenType::Keyword(kw));
637 self.token.is_keyword(kw)
638 }
639
487cf647
FG
640 fn check_keyword_case(&mut self, kw: Symbol, case: Case) -> bool {
641 if self.check_keyword(kw) {
642 return true;
643 }
644
645 if case == Case::Insensitive
646 && let Some((ident, /* is_raw */ false)) = self.token.ident()
647 && ident.as_str().to_lowercase() == kw.as_str().to_lowercase() {
648 true
649 } else {
650 false
651 }
652 }
653
e74abb32
XL
654 /// If the next token is the given keyword, eats it and returns `true`.
655 /// Otherwise, returns `false`. An expectation is also added for diagnostics purposes.
dfeec247
XL
656 // Public for rustfmt usage.
657 pub fn eat_keyword(&mut self, kw: Symbol) -> bool {
85aaf69f 658 if self.check_keyword(kw) {
9cc50fc6
SL
659 self.bump();
660 true
85aaf69f 661 } else {
9cc50fc6 662 false
85aaf69f
SL
663 }
664 }
665
487cf647
FG
666 /// Eats a keyword, optionally ignoring the case.
667 /// If the case differs (and is ignored) an error is issued.
668 /// This is useful for recovery.
669 fn eat_keyword_case(&mut self, kw: Symbol, case: Case) -> bool {
670 if self.eat_keyword(kw) {
671 return true;
672 }
673
674 if case == Case::Insensitive
675 && let Some((ident, /* is_raw */ false)) = self.token.ident()
676 && ident.as_str().to_lowercase() == kw.as_str().to_lowercase() {
677 self
678 .struct_span_err(ident.span, format!("keyword `{kw}` is written in a wrong case"))
679 .span_suggestion(
680 ident.span,
681 "write it in the correct case",
682 kw,
683 Applicability::MachineApplicable
684 ).emit();
685
686 self.bump();
687 return true;
688 }
689
690 false
691 }
692
dc9dc135 693 fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool {
1a4d82fc 694 if self.token.is_keyword(kw) {
9cc50fc6
SL
695 self.bump();
696 true
1a4d82fc 697 } else {
9cc50fc6 698 false
1a4d82fc 699 }
970d7e83
LB
700 }
701
9fa01778
XL
702 /// If the given word is not a keyword, signals an error.
703 /// If the next token is not the given word, signals an error.
704 /// Otherwise, eats it.
dc9dc135 705 fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> {
dfeec247 706 if !self.eat_keyword(kw) { self.unexpected() } else { Ok(()) }
970d7e83
LB
707 }
708
74b04a01
XL
709 /// Is the given keyword `kw` followed by a non-reserved identifier?
710 fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
711 self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
712 }
713
e74abb32
XL
714 fn check_or_expected(&mut self, ok: bool, typ: TokenType) -> bool {
715 if ok {
32a655c1
SL
716 true
717 } else {
e74abb32 718 self.expected_tokens.push(typ);
32a655c1
SL
719 false
720 }
721 }
722
e74abb32
XL
723 fn check_ident(&mut self) -> bool {
724 self.check_or_expected(self.token.is_ident(), TokenType::Ident)
725 }
726
32a655c1 727 fn check_path(&mut self) -> bool {
e74abb32 728 self.check_or_expected(self.token.is_path_start(), TokenType::Path)
32a655c1
SL
729 }
730
731 fn check_type(&mut self) -> bool {
e74abb32 732 self.check_or_expected(self.token.can_begin_type(), TokenType::Type)
32a655c1
SL
733 }
734
9fa01778 735 fn check_const_arg(&mut self) -> bool {
e74abb32
XL
736 self.check_or_expected(self.token.can_begin_const_arg(), TokenType::Const)
737 }
738
29967ef6
XL
739 fn check_inline_const(&self, dist: usize) -> bool {
740 self.is_keyword_ahead(dist, &[kw::Const])
487cf647
FG
741 && self.look_ahead(dist + 1, |t| match &t.kind {
742 token::Interpolated(nt) => matches!(**nt, token::NtBlock(..)),
04454e1e 743 token::OpenDelim(Delimiter::Brace) => true,
29967ef6
XL
744 _ => false,
745 })
746 }
747
e74abb32
XL
748 /// Checks to see if the next token is either `+` or `+=`.
749 /// Otherwise returns `false`.
750 fn check_plus(&mut self) -> bool {
751 self.check_or_expected(
752 self.token.is_like_plus(),
753 TokenType::Token(token::BinOp(token::Plus)),
754 )
9fa01778
XL
755 }
756
74b04a01
XL
757 /// Eats the expected token if it's present possibly breaking
758 /// compound tokens like multi-character operators in process.
759 /// Returns `true` if the token was eaten.
760 fn break_and_eat(&mut self, expected: TokenKind) -> bool {
761 if self.token.kind == expected {
762 self.bump();
763 return true;
764 }
765 match self.token.kind.break_two_token_op() {
766 Some((first, second)) if first == expected => {
767 let first_span = self.sess.source_map().start_point(self.token.span);
768 let second_span = self.token.span.with_lo(first_span.hi());
769 self.token = Token::new(first, first_span);
fc512014
XL
770 // Keep track of this token - if we end token capturing now,
771 // we'll want to append this token to the captured stream.
772 //
773 // If we consume any additional tokens, then this token
774 // is not needed (we'll capture the entire 'glued' token),
04454e1e 775 // and `bump` will set this field to `None`
cdc7bbd5 776 self.token_cursor.break_last_token = true;
29967ef6
XL
777 // Use the spacing of the glued token as the spacing
778 // of the unglued second token.
779 self.bump_with((Token::new(second, second_span), self.token_spacing));
94b46f34
XL
780 true
781 }
74b04a01
XL
782 _ => {
783 self.expected_tokens.push(TokenType::Token(expected));
784 false
94b46f34 785 }
94b46f34
XL
786 }
787 }
788
74b04a01
XL
789 /// Eats `+` possibly breaking tokens like `+=` in process.
790 fn eat_plus(&mut self) -> bool {
791 self.break_and_eat(token::BinOp(token::Plus))
792 }
793
794 /// Eats `&` possibly breaking tokens like `&&` in process.
795 /// Signals an error if `&` is not eaten.
9cc50fc6 796 fn expect_and(&mut self) -> PResult<'a, ()> {
74b04a01 797 if self.break_and_eat(token::BinOp(token::And)) { Ok(()) } else { self.unexpected() }
ea8adc8c
XL
798 }
799
74b04a01
XL
800 /// Eats `|` possibly breaking tokens like `||` in process.
801 /// Signals an error if `|` was not eaten.
ea8adc8c 802 fn expect_or(&mut self) -> PResult<'a, ()> {
74b04a01 803 if self.break_and_eat(token::BinOp(token::Or)) { Ok(()) } else { self.unexpected() }
970d7e83
LB
804 }
805
74b04a01 806 /// Eats `<` possibly breaking tokens like `<<` in process.
9cc50fc6 807 fn eat_lt(&mut self) -> bool {
74b04a01 808 let ate = self.break_and_eat(token::Lt);
9fa01778
XL
809 if ate {
810 // See doc comment for `unmatched_angle_bracket_count`.
811 self.unmatched_angle_bracket_count += 1;
812 self.max_angle_bracket_count += 1;
813 debug!("eat_lt: (increment) count={:?}", self.unmatched_angle_bracket_count);
1a4d82fc 814 }
9fa01778 815 ate
1a4d82fc
JJ
816 }
817
74b04a01
XL
818 /// Eats `<` possibly breaking tokens like `<<` in process.
819 /// Signals an error if `<` was not eaten.
9cc50fc6 820 fn expect_lt(&mut self) -> PResult<'a, ()> {
74b04a01 821 if self.eat_lt() { Ok(()) } else { self.unexpected() }
1a4d82fc
JJ
822 }
823
74b04a01
XL
824 /// Eats `>` possibly breaking tokens like `>>` in process.
825 /// Signals an error if `>` was not eaten.
94b46f34 826 fn expect_gt(&mut self) -> PResult<'a, ()> {
74b04a01
XL
827 if self.break_and_eat(token::Gt) {
828 // See doc comment for `unmatched_angle_bracket_count`.
829 if self.unmatched_angle_bracket_count > 0 {
830 self.unmatched_angle_bracket_count -= 1;
831 debug!("expect_gt: (decrement) count={:?}", self.unmatched_angle_bracket_count);
dfeec247 832 }
74b04a01
XL
833 Ok(())
834 } else {
835 self.unexpected()
1a4d82fc
JJ
836 }
837 }
838
416331ca 839 fn expect_any_with_type(&mut self, kets: &[&TokenKind], expect: TokenExpectType) -> bool {
dfeec247
XL
840 kets.iter().any(|k| match expect {
841 TokenExpectType::Expect => self.check(k),
842 TokenExpectType::NoExpect => self.token == **k,
416331ca
XL
843 })
844 }
845
e74abb32 846 fn parse_seq_to_before_tokens<T>(
b7449926 847 &mut self,
dc9dc135 848 kets: &[&TokenKind],
b7449926
XL
849 sep: SeqSep,
850 expect: TokenExpectType,
416331ca
XL
851 mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
852 ) -> PResult<'a, (Vec<T>, bool /* trailing */, bool /* recovered */)> {
9fa01778
XL
853 let mut first = true;
854 let mut recovered = false;
416331ca 855 let mut trailing = false;
c30ab7b3 856 let mut v = vec![];
cdc7bbd5
XL
857 let unclosed_delims = !self.unclosed_delims.is_empty();
858
416331ca
XL
859 while !self.expect_any_with_type(kets, expect) {
860 if let token::CloseDelim(..) | token::Eof = self.token.kind {
dfeec247 861 break;
416331ca 862 }
487cf647 863 if let Some(t) = &sep.sep {
7cac9316
XL
864 if first {
865 first = false;
866 } else {
9fa01778 867 match self.expect(t) {
c295e0f8
XL
868 Ok(false) => {
869 self.current_closure.take();
870 }
9fa01778 871 Ok(true) => {
c295e0f8 872 self.current_closure.take();
9fa01778
XL
873 recovered = true;
874 break;
abe05a73 875 }
60c5eb7d 876 Err(mut expect_err) => {
74b04a01 877 let sp = self.prev_token.span.shrink_to_hi();
60c5eb7d
XL
878 let token_str = pprust::token_kind_to_string(t);
879
c295e0f8
XL
880 match self.current_closure.take() {
881 Some(closure_spans) if self.token.kind == TokenKind::Semi => {
882 // Finding a semicolon instead of a comma
883 // after a closure body indicates that the
884 // closure body may be a block but the user
885 // forgot to put braces around its
886 // statements.
887
888 self.recover_missing_braces_around_closure_body(
889 closure_spans,
890 expect_err,
891 )?;
892
893 continue;
894 }
895
896 _ => {
897 // Attempt to keep parsing if it was a similar separator.
487cf647 898 if let Some(tokens) = t.similar_tokens() {
c295e0f8
XL
899 if tokens.contains(&self.token.kind) && !unclosed_delims {
900 self.bump();
901 }
902 }
9fa01778
XL
903 }
904 }
60c5eb7d 905
f9f354fc
XL
906 // If this was a missing `@` in a binding pattern
907 // bail with a suggestion
908 // https://github.com/rust-lang/rust/issues/72373
f035d41b 909 if self.prev_token.is_ident() && self.token.kind == token::DotDot {
f9f354fc
XL
910 let msg = format!(
911 "if you meant to bind the contents of \
912 the rest of the array pattern into `{}`, use `@`",
913 pprust::token_to_string(&self.prev_token)
914 );
915 expect_err
916 .span_suggestion_verbose(
917 self.prev_token.span.shrink_to_hi().until(self.token.span),
918 &msg,
04454e1e 919 " @ ",
f9f354fc
XL
920 Applicability::MaybeIncorrect,
921 )
922 .emit();
923 break;
924 }
925
e1599b0c 926 // Attempt to keep parsing if it was an omitted separator.
9fa01778
XL
927 match f(self) {
928 Ok(t) => {
60c5eb7d
XL
929 // Parsed successfully, therefore most probably the code only
930 // misses a separator.
931 expect_err
932 .span_suggestion_short(
5869c6ff 933 sp,
60c5eb7d 934 &format!("missing `{}`", token_str),
04454e1e 935 token_str,
60c5eb7d
XL
936 Applicability::MaybeIncorrect,
937 )
938 .emit();
939
9fa01778
XL
940 v.push(t);
941 continue;
dfeec247 942 }
5e7ed085 943 Err(e) => {
60c5eb7d
XL
944 // Parsing failed, therefore it must be something more serious
945 // than just a missing separator.
487cf647
FG
946 for xx in &e.children {
947 // propagate the help message from sub error 'e' to main error 'expect_err;
948 expect_err.children.push(xx.clone());
949 }
60c5eb7d
XL
950 expect_err.emit();
951
9fa01778
XL
952 e.cancel();
953 break;
954 }
abe05a73
XL
955 }
956 }
7453a54e
SL
957 }
958 }
7453a54e 959 }
416331ca
XL
960 if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
961 trailing = true;
7453a54e
SL
962 break;
963 }
964
abe05a73
XL
965 let t = f(self)?;
966 v.push(t);
970d7e83 967 }
7453a54e 968
416331ca 969 Ok((v, trailing, recovered))
970d7e83
LB
970 }
971
c295e0f8
XL
972 fn recover_missing_braces_around_closure_body(
973 &mut self,
974 closure_spans: ClosureSpans,
5e7ed085 975 mut expect_err: DiagnosticBuilder<'_, ErrorGuaranteed>,
c295e0f8
XL
976 ) -> PResult<'a, ()> {
977 let initial_semicolon = self.token.span;
978
979 while self.eat(&TokenKind::Semi) {
980 let _ = self.parse_stmt(ForceCollect::Yes)?;
981 }
982
983 expect_err.set_primary_message(
984 "closure bodies that contain statements must be surrounded by braces",
985 );
986
987 let preceding_pipe_span = closure_spans.closing_pipe;
988 let following_token_span = self.token.span;
989
990 let mut first_note = MultiSpan::from(vec![initial_semicolon]);
991 first_note.push_span_label(
992 initial_semicolon,
064997fb 993 "this `;` turns the preceding closure into a statement",
c295e0f8
XL
994 );
995 first_note.push_span_label(
996 closure_spans.body,
064997fb 997 "this expression is a statement because of the trailing semicolon",
c295e0f8
XL
998 );
999 expect_err.span_note(first_note, "statement found outside of a block");
1000
1001 let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
064997fb 1002 second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
c295e0f8
XL
1003 second_note.push_span_label(
1004 following_token_span,
064997fb 1005 "...but likely you meant the closure to end here",
c295e0f8
XL
1006 );
1007 expect_err.span_note(second_note, "the closure body may be incorrectly delimited");
1008
1009 expect_err.set_span(vec![preceding_pipe_span, following_token_span]);
1010
1011 let opening_suggestion_str = " {".to_string();
1012 let closing_suggestion_str = "}".to_string();
1013
1014 expect_err.multipart_suggestion(
1015 "try adding braces",
1016 vec![
1017 (preceding_pipe_span.shrink_to_hi(), opening_suggestion_str),
1018 (following_token_span.shrink_to_lo(), closing_suggestion_str),
1019 ],
1020 Applicability::MaybeIncorrect,
1021 );
1022
1023 expect_err.emit();
1024
1025 Ok(())
1026 }
1027
dfeec247
XL
1028 /// Parses a sequence, not including the closing delimiter. The function
1029 /// `f` must consume tokens until reaching the next separator or
1030 /// closing bracket.
1031 fn parse_seq_to_before_end<T>(
1032 &mut self,
1033 ket: &TokenKind,
1034 sep: SeqSep,
1035 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1036 ) -> PResult<'a, (Vec<T>, bool, bool)> {
1037 self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
1038 }
1039
1040 /// Parses a sequence, including the closing delimiter. The function
1041 /// `f` must consume tokens until reaching the next separator or
1042 /// closing bracket.
1043 fn parse_seq_to_end<T>(
1044 &mut self,
1045 ket: &TokenKind,
1046 sep: SeqSep,
1047 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1048 ) -> PResult<'a, (Vec<T>, bool /* trailing */)> {
1049 let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
1050 if !recovered {
1051 self.eat(ket);
1052 }
1053 Ok((val, trailing))
1054 }
1055
9fa01778
XL
1056 /// Parses a sequence, including the closing delimiter. The function
1057 /// `f` must consume tokens until reaching the next separator or
1a4d82fc 1058 /// closing bracket.
416331ca 1059 fn parse_unspanned_seq<T>(
9fa01778 1060 &mut self,
dc9dc135
XL
1061 bra: &TokenKind,
1062 ket: &TokenKind,
9fa01778 1063 sep: SeqSep,
416331ca
XL
1064 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1065 ) -> PResult<'a, (Vec<T>, bool)> {
54a0048b 1066 self.expect(bra)?;
dfeec247 1067 self.parse_seq_to_end(ket, sep, f)
416331ca
XL
1068 }
1069
1070 fn parse_delim_comma_seq<T>(
1071 &mut self,
04454e1e 1072 delim: Delimiter,
416331ca
XL
1073 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1074 ) -> PResult<'a, (Vec<T>, bool)> {
1075 self.parse_unspanned_seq(
1076 &token::OpenDelim(delim),
1077 &token::CloseDelim(delim),
1078 SeqSep::trailing_allowed(token::Comma),
1079 f,
1080 )
1081 }
1082
1083 fn parse_paren_comma_seq<T>(
1084 &mut self,
1085 f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1086 ) -> PResult<'a, (Vec<T>, bool)> {
04454e1e 1087 self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
970d7e83
LB
1088 }
1089
74b04a01 1090 /// Advance the parser by one token using provided token as the next one.
5e7ed085
FG
1091 fn bump_with(&mut self, next: (Token, Spacing)) {
1092 self.inlined_bump_with(next)
1093 }
1094
1095 /// This always-inlined version should only be used on hot code paths.
1096 #[inline(always)]
1097 fn inlined_bump_with(&mut self, (next_token, next_spacing): (Token, Spacing)) {
74b04a01
XL
1098 // Update the current and previous tokens.
1099 self.prev_token = mem::replace(&mut self.token, next_token);
29967ef6 1100 self.token_spacing = next_spacing;
9e0c209e 1101
74b04a01 1102 // Diagnostics.
7453a54e 1103 self.expected_tokens.clear();
223e47cc 1104 }
7453a54e 1105
74b04a01
XL
1106 /// Advance the parser by one token.
1107 pub fn bump(&mut self) {
04454e1e
FG
1108 // Note: destructuring here would give nicer code, but it was found in #96210 to be slower
1109 // than `.0`/`.1` access.
1110 let mut next = self.token_cursor.inlined_next(self.desugar_doc_comments);
1111 self.token_cursor.num_next_calls += 1;
1112 // We've retrieved an token from the underlying
1113 // cursor, so we no longer need to worry about
1114 // an unglued token. See `break_and_eat` for more details
1115 self.token_cursor.break_last_token = false;
1116 if next.0.span.is_dummy() {
1117 // Tweak the location for better diagnostics, but keep syntactic context intact.
1118 let fallback_span = self.token.span;
1119 next.0.span = fallback_span.with_ctxt(next.0.span.ctxt());
1120 }
1121 debug_assert!(!matches!(
1122 next.0.kind,
1123 token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)
1124 ));
1125 self.inlined_bump_with(next)
74b04a01
XL
1126 }
1127
e74abb32
XL
1128 /// Look-ahead `dist` tokens of `self.token` and get access to that token there.
1129 /// When `dist == 0` then the current token is looked at.
1130 pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
c30ab7b3 1131 if dist == 0 {
e74abb32 1132 return looker(&self.token);
223e47cc 1133 }
8bb4bdeb 1134
dc9dc135 1135 let frame = &self.token_cursor.frame;
04454e1e 1136 if let Some((delim, span)) = frame.delim_sp && delim != Delimiter::Invisible {
cdc7bbd5
XL
1137 let all_normal = (0..dist).all(|i| {
1138 let token = frame.tree_cursor.look_ahead(i);
04454e1e 1139 !matches!(token, Some(TokenTree::Delimited(_, Delimiter::Invisible, _)))
cdc7bbd5
XL
1140 });
1141 if all_normal {
1142 return match frame.tree_cursor.look_ahead(dist - 1) {
1143 Some(tree) => match tree {
064997fb 1144 TokenTree::Token(token, _) => looker(token),
cdc7bbd5
XL
1145 TokenTree::Delimited(dspan, delim, _) => {
1146 looker(&Token::new(token::OpenDelim(*delim), dspan.open))
1147 }
1148 },
04454e1e 1149 None => looker(&Token::new(token::CloseDelim(delim), span.close)),
cdc7bbd5
XL
1150 };
1151 }
29967ef6 1152 }
cdc7bbd5
XL
1153
1154 let mut cursor = self.token_cursor.clone();
1155 let mut i = 0;
1156 let mut token = Token::dummy();
1157 while i < dist {
04454e1e 1158 token = cursor.next(/* desugar_doc_comments */ false).0;
cdc7bbd5
XL
1159 if matches!(
1160 token.kind,
04454e1e 1161 token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible)
cdc7bbd5
XL
1162 ) {
1163 continue;
1164 }
1165 i += 1;
1166 }
1167 return looker(&token);
223e47cc 1168 }
ff7c6d11 1169
dc9dc135
XL
1170 /// Returns whether any of the given keywords are `dist` tokens ahead of the current one.
1171 fn is_keyword_ahead(&self, dist: usize, kws: &[Symbol]) -> bool {
1172 self.look_ahead(dist, |t| kws.iter().any(|&kw| t.is_keyword(kw)))
223e47cc 1173 }
223e47cc 1174
9fa01778 1175 /// Parses asyncness: `async` or nothing.
487cf647
FG
1176 fn parse_asyncness(&mut self, case: Case) -> Async {
1177 if self.eat_keyword_case(kw::Async, case) {
74b04a01
XL
1178 let span = self.prev_token.uninterpolated_span();
1179 Async::Yes { span, closure_id: DUMMY_NODE_ID, return_impl_trait_id: DUMMY_NODE_ID }
8faf50e0 1180 } else {
74b04a01 1181 Async::No
8faf50e0
XL
1182 }
1183 }
1184
9fa01778 1185 /// Parses unsafety: `unsafe` or nothing.
487cf647
FG
1186 fn parse_unsafety(&mut self, case: Case) -> Unsafe {
1187 if self.eat_keyword_case(kw::Unsafe, case) {
74b04a01
XL
1188 Unsafe::Yes(self.prev_token.uninterpolated_span())
1189 } else {
1190 Unsafe::No
1191 }
1192 }
1193
1194 /// Parses constness: `const` or nothing.
487cf647 1195 fn parse_constness(&mut self, case: Case) -> Const {
29967ef6 1196 // Avoid const blocks to be parsed as const items
04454e1e 1197 if self.look_ahead(1, |t| t != &token::OpenDelim(Delimiter::Brace))
487cf647 1198 && self.eat_keyword_case(kw::Const, case)
29967ef6 1199 {
74b04a01
XL
1200 Const::Yes(self.prev_token.uninterpolated_span())
1201 } else {
1202 Const::No
1203 }
1a4d82fc
JJ
1204 }
1205
29967ef6 1206 /// Parses inline const expressions.
3c0e092e
XL
1207 fn parse_const_block(&mut self, span: Span, pat: bool) -> PResult<'a, P<Expr>> {
1208 if pat {
1209 self.sess.gated_spans.gate(sym::inline_const_pat, span);
1210 } else {
1211 self.sess.gated_spans.gate(sym::inline_const, span);
1212 }
29967ef6 1213 self.eat_keyword(kw::Const);
04454e1e 1214 let (attrs, blk) = self.parse_inner_attrs_and_block()?;
29967ef6
XL
1215 let anon_const = AnonConst {
1216 id: DUMMY_NODE_ID,
f2b60f7d 1217 value: self.mk_expr(blk.span, ExprKind::Block(blk, None)),
29967ef6 1218 };
fc512014 1219 let blk_span = anon_const.value.span;
f2b60f7d
FG
1220 Ok(self.mk_expr_with_attrs(
1221 span.to(blk_span),
1222 ExprKind::ConstBlock(anon_const),
1223 AttrVec::from(attrs),
1224 ))
29967ef6
XL
1225 }
1226
416331ca
XL
1227 /// Parses mutability (`mut` or nothing).
1228 fn parse_mutability(&mut self) -> Mutability {
dfeec247 1229 if self.eat_keyword(kw::Mut) { Mutability::Mut } else { Mutability::Not }
223e47cc
LB
1230 }
1231
e74abb32
XL
1232 /// Possibly parses mutability (`const` or `mut`).
1233 fn parse_const_or_mut(&mut self) -> Option<Mutability> {
1234 if self.eat_keyword(kw::Mut) {
dfeec247 1235 Some(Mutability::Mut)
e74abb32 1236 } else if self.eat_keyword(kw::Const) {
dfeec247 1237 Some(Mutability::Not)
e74abb32
XL
1238 } else {
1239 None
1240 }
1241 }
1242
416331ca 1243 fn parse_field_name(&mut self) -> PResult<'a, Ident> {
dfeec247
XL
1244 if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
1245 {
2b03887a
FG
1246 if let Some(suffix) = suffix {
1247 self.expect_no_tuple_index_suffix(self.token.span, suffix);
1248 }
416331ca 1249 self.bump();
74b04a01 1250 Ok(Ident::new(symbol, self.prev_token.span))
223e47cc 1251 } else {
6a06907d 1252 self.parse_ident_common(true)
223e47cc
LB
1253 }
1254 }
1255
487cf647
FG
1256 fn parse_delim_args(&mut self) -> PResult<'a, P<DelimArgs>> {
1257 if let Some(args) = self.parse_delim_args_inner() { Ok(P(args)) } else { self.unexpected() }
60c5eb7d
XL
1258 }
1259
487cf647
FG
1260 fn parse_attr_args(&mut self) -> PResult<'a, AttrArgs> {
1261 Ok(if let Some(args) = self.parse_delim_args_inner() {
1262 AttrArgs::Delimited(args)
1263 } else {
1264 if self.eat(&token::Eq) {
1265 let eq_span = self.prev_token.span;
1266 AttrArgs::Eq(eq_span, AttrArgsEq::Ast(self.parse_expr_force_collect()?))
1267 } else {
1268 AttrArgs::Empty
1269 }
1270 })
60c5eb7d
XL
1271 }
1272
487cf647
FG
1273 fn parse_delim_args_inner(&mut self) -> Option<DelimArgs> {
1274 if self.check(&token::OpenDelim(Delimiter::Parenthesis))
1275 || self.check(&token::OpenDelim(Delimiter::Bracket))
1276 || self.check(&token::OpenDelim(Delimiter::Brace))
1277 {
1278 match self.parse_token_tree() {
1279 // We've confirmed above that there is a delimiter so unwrapping is OK.
1280 TokenTree::Delimited(dspan, delim, tokens) => Some(DelimArgs {
1281 dspan,
1282 delim: MacDelimiter::from_token(delim).unwrap(),
1283 tokens,
1284 }),
1285 _ => unreachable!(),
1286 }
1287 } else {
1288 None
1289 }
416331ca 1290 }
041b39d2 1291
e74abb32
XL
1292 fn parse_or_use_outer_attributes(
1293 &mut self,
6a06907d
XL
1294 already_parsed_attrs: Option<AttrWrapper>,
1295 ) -> PResult<'a, AttrWrapper> {
416331ca
XL
1296 if let Some(attrs) = already_parsed_attrs {
1297 Ok(attrs)
970d7e83 1298 } else {
6a06907d 1299 self.parse_outer_attributes()
970d7e83
LB
1300 }
1301 }
1302
416331ca 1303 /// Parses a single token tree from the input.
3dfed10e 1304 pub(crate) fn parse_token_tree(&mut self) -> TokenTree {
dc9dc135 1305 match self.token.kind {
416331ca 1306 token::OpenDelim(..) => {
04454e1e
FG
1307 // Grab the tokens from this frame.
1308 let frame = &self.token_cursor.frame;
1309 let stream = frame.tree_cursor.stream.clone();
1310 let (delim, span) = frame.delim_sp.unwrap();
1311
1312 // Advance the token cursor through the entire delimited
1313 // sequence. After getting the `OpenDelim` we are *within* the
1314 // delimited sequence, i.e. at depth `d`. After getting the
1315 // matching `CloseDelim` we are *after* the delimited sequence,
1316 // i.e. at depth `d - 1`.
1317 let target_depth = self.token_cursor.stack.len() - 1;
1318 loop {
29967ef6
XL
1319 // Advance one token at a time, so `TokenCursor::next()`
1320 // can capture these tokens if necessary.
1321 self.bump();
04454e1e
FG
1322 if self.token_cursor.stack.len() == target_depth {
1323 debug_assert!(matches!(self.token.kind, token::CloseDelim(_)));
1324 break;
1325 }
29967ef6 1326 }
04454e1e 1327
29967ef6 1328 // Consume close delimiter
0731742a 1329 self.bump();
29967ef6 1330 TokenTree::Delimited(span, delim, stream)
dfeec247 1331 }
416331ca
XL
1332 token::CloseDelim(_) | token::Eof => unreachable!(),
1333 _ => {
416331ca 1334 self.bump();
064997fb 1335 TokenTree::Token(self.prev_token.clone(), Spacing::Alone)
0731742a 1336 }
0731742a
XL
1337 }
1338 }
1339
416331ca
XL
1340 /// Parses a stream of tokens into a list of `TokenTree`s, up to EOF.
1341 pub fn parse_all_token_trees(&mut self) -> PResult<'a, Vec<TokenTree>> {
1342 let mut tts = Vec::new();
1343 while self.token != token::Eof {
1344 tts.push(self.parse_token_tree());
54a0048b 1345 }
416331ca 1346 Ok(tts)
54a0048b
SL
1347 }
1348
416331ca
XL
1349 pub fn parse_tokens(&mut self) -> TokenStream {
1350 let mut result = Vec::new();
1351 loop {
1352 match self.token.kind {
1353 token::Eof | token::CloseDelim(..) => break,
064997fb 1354 _ => result.push(self.parse_token_tree()),
ff7c6d11 1355 }
223e47cc 1356 }
416331ca 1357 TokenStream::new(result)
223e47cc
LB
1358 }
1359
416331ca
XL
1360 /// Evaluates the closure with restrictions in place.
1361 ///
1362 /// Afters the closure is evaluated, restrictions are reset.
e74abb32 1363 fn with_res<T>(&mut self, res: Restrictions, f: impl FnOnce(&mut Self) -> T) -> T {
416331ca 1364 let old = self.restrictions;
e74abb32
XL
1365 self.restrictions = res;
1366 let res = f(self);
416331ca 1367 self.restrictions = old;
e74abb32 1368 res
8faf50e0
XL
1369 }
1370
923072b8
FG
1371 /// Parses `pub` and `pub(in path)` plus shortcuts `pub(crate)` for `pub(in crate)`, `pub(self)`
1372 /// for `pub(in self)` and `pub(super)` for `pub(in super)`.
416331ca 1373 /// If the following element can't be a tuple (i.e., it's a function definition), then
3c0e092e 1374 /// it's not a tuple struct field), and the contents within the parentheses aren't valid,
416331ca 1375 /// so emit a proper diagnostic.
1b1a35ee
XL
1376 // Public for rustfmt usage.
1377 pub fn parse_visibility(&mut self, fbt: FollowedByType) -> PResult<'a, Visibility> {
04454e1e 1378 maybe_whole!(self, NtVis, |x| x.into_inner());
dc9dc135 1379
416331ca
XL
1380 if !self.eat_keyword(kw::Pub) {
1381 // We need a span for our `Spanned<VisibilityKind>`, but there's inherently no
1382 // keyword to grab a span from for inherited visibility; an empty span at the
1383 // beginning of the current token would seem to be the "Schelling span".
1b1a35ee
XL
1384 return Ok(Visibility {
1385 span: self.token.span.shrink_to_lo(),
1386 kind: VisibilityKind::Inherited,
1387 tokens: None,
1388 });
416331ca 1389 }
74b04a01 1390 let lo = self.prev_token.span;
223e47cc 1391
04454e1e 1392 if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
416331ca
XL
1393 // We don't `self.bump()` the `(` yet because this might be a struct definition where
1394 // `()` or a tuple might be allowed. For example, `struct Struct(pub (), pub (usize));`.
1395 // Because of this, we only `bump` the `(` if we're assured it is appropriate to do so
1396 // by the following tokens.
923072b8 1397 if self.is_keyword_ahead(1, &[kw::In]) {
e74abb32 1398 // Parse `pub(in path)`.
416331ca
XL
1399 self.bump(); // `(`
1400 self.bump(); // `in`
1401 let path = self.parse_path(PathStyle::Mod)?; // `path`
04454e1e 1402 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
f2b60f7d
FG
1403 let vis = VisibilityKind::Restricted {
1404 path: P(path),
1405 id: ast::DUMMY_NODE_ID,
1406 shorthand: false,
1407 };
1b1a35ee
XL
1408 return Ok(Visibility {
1409 span: lo.to(self.prev_token.span),
1410 kind: vis,
1411 tokens: None,
1412 });
04454e1e 1413 } else if self.look_ahead(2, |t| t == &token::CloseDelim(Delimiter::Parenthesis))
923072b8 1414 && self.is_keyword_ahead(1, &[kw::Crate, kw::Super, kw::SelfLower])
416331ca 1415 {
923072b8 1416 // Parse `pub(crate)`, `pub(self)`, or `pub(super)`.
416331ca 1417 self.bump(); // `(`
923072b8 1418 let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
04454e1e 1419 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
f2b60f7d
FG
1420 let vis = VisibilityKind::Restricted {
1421 path: P(path),
1422 id: ast::DUMMY_NODE_ID,
1423 shorthand: true,
1424 };
1b1a35ee
XL
1425 return Ok(Visibility {
1426 span: lo.to(self.prev_token.span),
1427 kind: vis,
1428 tokens: None,
1429 });
60c5eb7d
XL
1430 } else if let FollowedByType::No = fbt {
1431 // Provide this diagnostic if a type cannot follow;
1432 // in particular, if this is not a tuple struct.
e74abb32
XL
1433 self.recover_incorrect_vis_restriction()?;
1434 // Emit diagnostic, but continue with public visibility.
532ac7d7 1435 }
223e47cc 1436 }
223e47cc 1437
1b1a35ee 1438 Ok(Visibility { span: lo, kind: VisibilityKind::Public, tokens: None })
223e47cc
LB
1439 }
1440
e74abb32
XL
1441 /// Recovery for e.g. `pub(something) fn ...` or `struct X { pub(something) y: Z }`
1442 fn recover_incorrect_vis_restriction(&mut self) -> PResult<'a, ()> {
1443 self.bump(); // `(`
1444 let path = self.parse_path(PathStyle::Mod)?;
04454e1e 1445 self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
e74abb32 1446
e74abb32 1447 let path_str = pprust::path_to_string(&path);
2b03887a 1448 self.sess.emit_err(IncorrectVisibilityRestriction { span: path.span, inner_str: path_str });
e74abb32
XL
1449
1450 Ok(())
1451 }
1452
60c5eb7d 1453 /// Parses `extern string_literal?`.
487cf647
FG
1454 fn parse_extern(&mut self, case: Case) -> Extern {
1455 if self.eat_keyword_case(kw::Extern, case) {
064997fb
FG
1456 let mut extern_span = self.prev_token.span;
1457 let abi = self.parse_abi();
1458 if let Some(abi) = abi {
1459 extern_span = extern_span.to(abi.span);
1460 }
1461 Extern::from_abi(abi, extern_span)
1462 } else {
1463 Extern::None
1464 }
e74abb32
XL
1465 }
1466
60c5eb7d
XL
1467 /// Parses a string literal as an ABI spec.
1468 fn parse_abi(&mut self) -> Option<StrLit> {
1469 match self.parse_str_lit() {
1470 Ok(str_lit) => Some(str_lit),
1471 Err(Some(lit)) => match lit.kind {
f2b60f7d 1472 ast::LitKind::Err => None,
60c5eb7d 1473 _ => {
2b03887a 1474 self.sess.emit_err(NonStringAbiLiteral { span: lit.span });
60c5eb7d 1475 None
223e47cc 1476 }
dfeec247 1477 },
60c5eb7d 1478 Err(None) => None,
223e47cc
LB
1479 }
1480 }
1481
04454e1e 1482 pub fn collect_tokens_no_attrs<R: HasAttrs + HasTokens>(
5869c6ff
XL
1483 &mut self,
1484 f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1485 ) -> PResult<'a, R> {
6a06907d
XL
1486 // The only reason to call `collect_tokens_no_attrs` is if you want tokens, so use
1487 // `ForceCollect::Yes`
1488 self.collect_tokens_trailing_token(
1489 AttrWrapper::empty(),
1490 ForceCollect::Yes,
1491 |this, _attrs| Ok((f(this)?, TrailingToken::None)),
1492 )
3b2f2976
XL
1493 }
1494
0531ce1d
XL
1495 /// `::{` or `::*`
1496 fn is_import_coupler(&mut self) -> bool {
dfeec247
XL
1497 self.check(&token::ModSep)
1498 && self.look_ahead(1, |t| {
04454e1e 1499 *t == token::OpenDelim(Delimiter::Brace) || *t == token::BinOp(token::Star)
dfeec247 1500 })
a7813a04 1501 }
1b1a35ee
XL
1502
1503 pub fn clear_expected_tokens(&mut self) {
1504 self.expected_tokens.clear();
1505 }
e74abb32 1506}
532ac7d7 1507
923072b8 1508pub(crate) fn make_unclosed_delims_error(
e74abb32
XL
1509 unmatched: UnmatchedBrace,
1510 sess: &ParseSess,
5e7ed085 1511) -> Option<DiagnosticBuilder<'_, ErrorGuaranteed>> {
e74abb32
XL
1512 // `None` here means an `Eof` was found. We already emit those errors elsewhere, we add them to
1513 // `unmatched_braces` only for error recovery in the `Parser`.
1514 let found_delim = unmatched.found_delim?;
2b03887a 1515 let mut spans = vec![unmatched.found_span];
e74abb32 1516 if let Some(sp) = unmatched.unclosed_span {
2b03887a
FG
1517 spans.push(sp);
1518 };
1519 let err = MismatchedClosingDelimiter {
1520 spans,
1521 delimiter: pprust::token_kind_to_string(&token::CloseDelim(found_delim)).to_string(),
1522 unmatched: unmatched.found_span,
1523 opening_candidate: unmatched.candidate_span,
1524 unclosed: unmatched.unclosed_span,
1525 }
1526 .into_diagnostic(&sess.span_diagnostic);
e74abb32 1527 Some(err)
223e47cc 1528}
9fa01778 1529
e74abb32 1530pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, sess: &ParseSess) {
dfeec247
XL
1531 *sess.reached_eof.borrow_mut() |=
1532 unclosed_delims.iter().any(|unmatched_delim| unmatched_delim.found_delim.is_none());
e74abb32 1533 for unmatched in unclosed_delims.drain(..) {
f9f354fc 1534 if let Some(mut e) = make_unclosed_delims_error(unmatched, sess) {
74b04a01 1535 e.emit();
f9f354fc 1536 }
9fa01778 1537 }
9fa01778 1538}
cdc7bbd5 1539
f2b60f7d
FG
1540/// A helper struct used when building an `AttrTokenStream` from
1541/// a `LazyAttrTokenStream`. Both delimiter and non-delimited tokens
cdc7bbd5 1542/// are stored as `FlatToken::Token`. A vector of `FlatToken`s
f2b60f7d
FG
1543/// is then 'parsed' to build up an `AttrTokenStream` with nested
1544/// `AttrTokenTree::Delimited` tokens.
cdc7bbd5
XL
1545#[derive(Debug, Clone)]
1546pub enum FlatToken {
1547 /// A token - this holds both delimiter (e.g. '{' and '}')
1548 /// and non-delimiter tokens
1549 Token(Token),
1550 /// Holds the `AttributesData` for an AST node. The
1551 /// `AttributesData` is inserted directly into the
f2b60f7d
FG
1552 /// constructed `AttrTokenStream` as
1553 /// an `AttrTokenTree::Attributes`.
cdc7bbd5
XL
1554 AttrTarget(AttributesData),
1555 /// A special 'empty' token that is ignored during the conversion
f2b60f7d 1556 /// to an `AttrTokenStream`. This is used to simplify the
cdc7bbd5
XL
1557 /// handling of replace ranges.
1558 Empty,
1559}
5e7ed085
FG
1560
1561#[derive(Debug)]
1562pub enum NtOrTt {
1563 Nt(Nonterminal),
1564 Tt(TokenTree),
1565}