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