]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/item.rs
Auto merge of #94279 - tmiasko:write-print, r=Mark-Simulacrum
[rust.git] / compiler / rustc_parse / src / parser / item.rs
1 use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
2 use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
3 use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken};
4
5 use rustc_ast::ast::*;
6 use rustc_ast::ptr::P;
7 use rustc_ast::token::{self, TokenKind};
8 use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
9 use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID};
10 use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind};
11 use rustc_ast::{BindingMode, Block, FnDecl, FnSig, Param, SelfKind};
12 use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, VariantData};
13 use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
14 use rustc_ast::{MacArgs, MacCall, MacDelimiter};
15 use rustc_ast_pretty::pprust;
16 use rustc_errors::{struct_span_err, Applicability, ErrorReported, PResult, StashKey};
17 use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
18 use rustc_span::lev_distance::lev_distance;
19 use rustc_span::source_map::{self, Span};
20 use rustc_span::symbol::{kw, sym, Ident, Symbol};
21
22 use std::convert::TryFrom;
23 use std::mem;
24 use tracing::debug;
25
26 impl<'a> Parser<'a> {
27     /// Parses a source module as a crate. This is the main entry point for the parser.
28     pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
29         let (attrs, items, span) = self.parse_mod(&token::Eof)?;
30         Ok(ast::Crate { attrs, items, span, id: DUMMY_NODE_ID, is_placeholder: false })
31     }
32
33     /// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
34     fn parse_item_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
35         let unsafety = self.parse_unsafety();
36         self.expect_keyword(kw::Mod)?;
37         let id = self.parse_ident()?;
38         let mod_kind = if self.eat(&token::Semi) {
39             ModKind::Unloaded
40         } else {
41             self.expect(&token::OpenDelim(token::Brace))?;
42             let (mut inner_attrs, items, inner_span) =
43                 self.parse_mod(&token::CloseDelim(token::Brace))?;
44             attrs.append(&mut inner_attrs);
45             ModKind::Loaded(items, Inline::Yes, inner_span)
46         };
47         Ok((id, ItemKind::Mod(unsafety, mod_kind)))
48     }
49
50     /// Parses the contents of a module (inner attributes followed by module items).
51     pub fn parse_mod(
52         &mut self,
53         term: &TokenKind,
54     ) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> {
55         let lo = self.token.span;
56         let attrs = self.parse_inner_attributes()?;
57
58         let mut items = vec![];
59         while let Some(item) = self.parse_item(ForceCollect::No)? {
60             items.push(item);
61             self.maybe_consume_incorrect_semicolon(&items);
62         }
63
64         if !self.eat(term) {
65             let token_str = super::token_descr(&self.token);
66             if !self.maybe_consume_incorrect_semicolon(&items) {
67                 let msg = &format!("expected item, found {}", token_str);
68                 let mut err = self.struct_span_err(self.token.span, msg);
69                 err.span_label(self.token.span, "expected item");
70                 return Err(err);
71             }
72         }
73
74         Ok((attrs, items, lo.to(self.prev_token.span)))
75     }
76 }
77
78 pub(super) type ItemInfo = (Ident, ItemKind);
79
80 impl<'a> Parser<'a> {
81     pub fn parse_item(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<P<Item>>> {
82         let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: true };
83         self.parse_item_(fn_parse_mode, force_collect).map(|i| i.map(P))
84     }
85
86     fn parse_item_(
87         &mut self,
88         fn_parse_mode: FnParseMode,
89         force_collect: ForceCollect,
90     ) -> PResult<'a, Option<Item>> {
91         let attrs = self.parse_outer_attributes()?;
92         self.parse_item_common(attrs, true, false, fn_parse_mode, force_collect)
93     }
94
95     pub(super) fn parse_item_common(
96         &mut self,
97         attrs: AttrWrapper,
98         mac_allowed: bool,
99         attrs_allowed: bool,
100         fn_parse_mode: FnParseMode,
101         force_collect: ForceCollect,
102     ) -> PResult<'a, Option<Item>> {
103         // Don't use `maybe_whole` so that we have precise control
104         // over when we bump the parser
105         if let token::Interpolated(nt) = &self.token.kind {
106             if let token::NtItem(item) = &**nt {
107                 let mut item = item.clone();
108                 self.bump();
109
110                 attrs.prepend_to_nt_inner(&mut item.attrs);
111                 return Ok(Some(item.into_inner()));
112             }
113         };
114
115         let mut unclosed_delims = vec![];
116         let item =
117             self.collect_tokens_trailing_token(attrs, force_collect, |this: &mut Self, attrs| {
118                 let item =
119                     this.parse_item_common_(attrs, mac_allowed, attrs_allowed, fn_parse_mode);
120                 unclosed_delims.append(&mut this.unclosed_delims);
121                 Ok((item?, TrailingToken::None))
122             })?;
123
124         self.unclosed_delims.append(&mut unclosed_delims);
125         Ok(item)
126     }
127
128     fn parse_item_common_(
129         &mut self,
130         mut attrs: Vec<Attribute>,
131         mac_allowed: bool,
132         attrs_allowed: bool,
133         fn_parse_mode: FnParseMode,
134     ) -> PResult<'a, Option<Item>> {
135         let lo = self.token.span;
136         let vis = self.parse_visibility(FollowedByType::No)?;
137         let mut def = self.parse_defaultness();
138         let kind =
139             self.parse_item_kind(&mut attrs, mac_allowed, lo, &vis, &mut def, fn_parse_mode)?;
140         if let Some((ident, kind)) = kind {
141             self.error_on_unconsumed_default(def, &kind);
142             let span = lo.to(self.prev_token.span);
143             let id = DUMMY_NODE_ID;
144             let item = Item { ident, attrs, id, kind, vis, span, tokens: None };
145             return Ok(Some(item));
146         }
147
148         // At this point, we have failed to parse an item.
149         self.error_on_unmatched_vis(&vis);
150         self.error_on_unmatched_defaultness(def);
151         if !attrs_allowed {
152             self.recover_attrs_no_item(&attrs)?;
153         }
154         Ok(None)
155     }
156
157     /// Error in-case a non-inherited visibility was parsed but no item followed.
158     fn error_on_unmatched_vis(&self, vis: &Visibility) {
159         if let VisibilityKind::Inherited = vis.kind {
160             return;
161         }
162         let vs = pprust::vis_to_string(&vis);
163         let vs = vs.trim_end();
164         self.struct_span_err(vis.span, &format!("visibility `{}` is not followed by an item", vs))
165             .span_label(vis.span, "the visibility")
166             .help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
167             .emit();
168     }
169
170     /// Error in-case a `default` was parsed but no item followed.
171     fn error_on_unmatched_defaultness(&self, def: Defaultness) {
172         if let Defaultness::Default(sp) = def {
173             self.struct_span_err(sp, "`default` is not followed by an item")
174                 .span_label(sp, "the `default` qualifier")
175                 .note("only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`")
176                 .emit();
177         }
178     }
179
180     /// Error in-case `default` was parsed in an in-appropriate context.
181     fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
182         if let Defaultness::Default(span) = def {
183             let msg = format!("{} {} cannot be `default`", kind.article(), kind.descr());
184             self.struct_span_err(span, &msg)
185                 .span_label(span, "`default` because of this")
186                 .note("only associated `fn`, `const`, and `type` items can be `default`")
187                 .emit();
188         }
189     }
190
191     /// Parses one of the items allowed by the flags.
192     fn parse_item_kind(
193         &mut self,
194         attrs: &mut Vec<Attribute>,
195         macros_allowed: bool,
196         lo: Span,
197         vis: &Visibility,
198         def: &mut Defaultness,
199         fn_parse_mode: FnParseMode,
200     ) -> PResult<'a, Option<ItemInfo>> {
201         let def_final = def == &Defaultness::Final;
202         let mut def = || mem::replace(def, Defaultness::Final);
203
204         let info = if self.eat_keyword(kw::Use) {
205             // USE ITEM
206             let tree = self.parse_use_tree()?;
207
208             // If wildcard or glob-like brace syntax doesn't have `;`,
209             // the user may not know `*` or `{}` should be the last.
210             if let Err(mut e) = self.expect_semi() {
211                 match tree.kind {
212                     UseTreeKind::Glob => {
213                         e.note("the wildcard token must be last on the path").emit();
214                     }
215                     UseTreeKind::Nested(..) => {
216                         e.note("glob-like brace syntax must be last on the path").emit();
217                     }
218                     _ => (),
219                 }
220                 return Err(e);
221             }
222
223             (Ident::empty(), ItemKind::Use(tree))
224         } else if self.check_fn_front_matter(def_final) {
225             // FUNCTION ITEM
226             let (ident, sig, generics, body) = self.parse_fn(attrs, fn_parse_mode, lo, vis)?;
227             (ident, ItemKind::Fn(Box::new(Fn { defaultness: def(), sig, generics, body })))
228         } else if self.eat_keyword(kw::Extern) {
229             if self.eat_keyword(kw::Crate) {
230                 // EXTERN CRATE
231                 self.parse_item_extern_crate()?
232             } else {
233                 // EXTERN BLOCK
234                 self.parse_item_foreign_mod(attrs, Unsafe::No)?
235             }
236         } else if self.is_unsafe_foreign_mod() {
237             // EXTERN BLOCK
238             let unsafety = self.parse_unsafety();
239             self.expect_keyword(kw::Extern)?;
240             self.parse_item_foreign_mod(attrs, unsafety)?
241         } else if self.is_static_global() {
242             // STATIC ITEM
243             self.bump(); // `static`
244             let m = self.parse_mutability();
245             let (ident, ty, expr) = self.parse_item_global(Some(m))?;
246             (ident, ItemKind::Static(ty, m, expr))
247         } else if let Const::Yes(const_span) = self.parse_constness() {
248             // CONST ITEM
249             if self.token.is_keyword(kw::Impl) {
250                 // recover from `const impl`, suggest `impl const`
251                 self.recover_const_impl(const_span, attrs, def())?
252             } else {
253                 self.recover_const_mut(const_span);
254                 let (ident, ty, expr) = self.parse_item_global(None)?;
255                 (ident, ItemKind::Const(def(), ty, expr))
256             }
257         } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() {
258             // TRAIT ITEM
259             self.parse_item_trait(attrs, lo)?
260         } else if self.check_keyword(kw::Impl)
261             || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
262         {
263             // IMPL ITEM
264             self.parse_item_impl(attrs, def())?
265         } else if self.check_keyword(kw::Mod)
266             || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Mod])
267         {
268             // MODULE ITEM
269             self.parse_item_mod(attrs)?
270         } else if self.eat_keyword(kw::Type) {
271             // TYPE ITEM
272             self.parse_type_alias(def())?
273         } else if self.eat_keyword(kw::Enum) {
274             // ENUM ITEM
275             self.parse_item_enum()?
276         } else if self.eat_keyword(kw::Struct) {
277             // STRUCT ITEM
278             self.parse_item_struct()?
279         } else if self.is_kw_followed_by_ident(kw::Union) {
280             // UNION ITEM
281             self.bump(); // `union`
282             self.parse_item_union()?
283         } else if self.eat_keyword(kw::Macro) {
284             // MACROS 2.0 ITEM
285             self.parse_item_decl_macro(lo)?
286         } else if let IsMacroRulesItem::Yes { has_bang } = self.is_macro_rules_item() {
287             // MACRO_RULES ITEM
288             self.parse_item_macro_rules(vis, has_bang)?
289         } else if vis.kind.is_pub() && self.isnt_macro_invocation() {
290             self.recover_missing_kw_before_item()?;
291             return Ok(None);
292         } else if macros_allowed && self.check_path() {
293             // MACRO INVOCATION ITEM
294             (Ident::empty(), ItemKind::MacCall(self.parse_item_macro(vis)?))
295         } else {
296             return Ok(None);
297         };
298         Ok(Some(info))
299     }
300
301     /// When parsing a statement, would the start of a path be an item?
302     pub(super) fn is_path_start_item(&mut self) -> bool {
303         self.is_crate_vis() // no: `crate::b`, yes: `crate $item`
304         || self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
305         || self.check_auto_or_unsafe_trait_item() // no: `auto::b`, yes: `auto trait X { .. }`
306         || self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
307         || matches!(self.is_macro_rules_item(), IsMacroRulesItem::Yes{..}) // no: `macro_rules::b`, yes: `macro_rules! mac`
308     }
309
310     /// Are we sure this could not possibly be a macro invocation?
311     fn isnt_macro_invocation(&mut self) -> bool {
312         self.check_ident() && self.look_ahead(1, |t| *t != token::Not && *t != token::ModSep)
313     }
314
315     /// Recover on encountering a struct or method definition where the user
316     /// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
317     fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
318         // Space between `pub` keyword and the identifier
319         //
320         //     pub   S {}
321         //        ^^^ `sp` points here
322         let sp = self.prev_token.span.between(self.token.span);
323         let full_sp = self.prev_token.span.to(self.token.span);
324         let ident_sp = self.token.span;
325         if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
326             // possible public struct definition where `struct` was forgotten
327             let ident = self.parse_ident().unwrap();
328             let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
329             let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
330             err.span_suggestion_short(
331                 sp,
332                 &msg,
333                 " struct ".into(),
334                 Applicability::MaybeIncorrect, // speculative
335             );
336             Err(err)
337         } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
338             let ident = self.parse_ident().unwrap();
339             self.bump(); // `(`
340             let kw_name = self.recover_first_param();
341             self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
342             let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
343                 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
344                 self.bump(); // `{`
345                 ("fn", kw_name, false)
346             } else if self.check(&token::OpenDelim(token::Brace)) {
347                 self.bump(); // `{`
348                 ("fn", kw_name, false)
349             } else if self.check(&token::Colon) {
350                 let kw = "struct";
351                 (kw, kw, false)
352             } else {
353                 ("fn` or `struct", "function or struct", true)
354             };
355
356             let msg = format!("missing `{}` for {} definition", kw, kw_name);
357             let mut err = self.struct_span_err(sp, &msg);
358             if !ambiguous {
359                 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
360                 let suggestion =
361                     format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
362                 err.span_suggestion_short(
363                     sp,
364                     &suggestion,
365                     format!(" {} ", kw),
366                     Applicability::MachineApplicable,
367                 );
368             } else if let Ok(snippet) = self.span_to_snippet(ident_sp) {
369                 err.span_suggestion(
370                     full_sp,
371                     "if you meant to call a macro, try",
372                     format!("{}!", snippet),
373                     // this is the `ambiguous` conditional branch
374                     Applicability::MaybeIncorrect,
375                 );
376             } else {
377                 err.help(
378                     "if you meant to call a macro, remove the `pub` \
379                               and add a trailing `!` after the identifier",
380                 );
381             }
382             Err(err)
383         } else if self.look_ahead(1, |t| *t == token::Lt) {
384             let ident = self.parse_ident().unwrap();
385             self.eat_to_tokens(&[&token::Gt]);
386             self.bump(); // `>`
387             let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
388                 ("fn", self.recover_first_param(), false)
389             } else if self.check(&token::OpenDelim(token::Brace)) {
390                 ("struct", "struct", false)
391             } else {
392                 ("fn` or `struct", "function or struct", true)
393             };
394             let msg = format!("missing `{}` for {} definition", kw, kw_name);
395             let mut err = self.struct_span_err(sp, &msg);
396             if !ambiguous {
397                 err.span_suggestion_short(
398                     sp,
399                     &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
400                     format!(" {} ", kw),
401                     Applicability::MachineApplicable,
402                 );
403             }
404             Err(err)
405         } else {
406             Ok(())
407         }
408     }
409
410     /// Parses an item macro, e.g., `item!();`.
411     fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
412         let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
413         self.expect(&token::Not)?; // `!`
414         match self.parse_mac_args() {
415             // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
416             Ok(args) => {
417                 self.eat_semi_for_macro_if_needed(&args);
418                 self.complain_if_pub_macro(vis, false);
419                 Ok(MacCall { path, args, prior_type_ascription: self.last_type_ascription })
420             }
421
422             Err(mut err) => {
423                 // Maybe the user misspelled `macro_rules` (issue #91227)
424                 if self.token.is_ident()
425                     && path.segments.len() == 1
426                     && lev_distance("macro_rules", &path.segments[0].ident.to_string(), 3).is_some()
427                 {
428                     err.span_suggestion(
429                         path.span,
430                         "perhaps you meant to define a macro",
431                         "macro_rules".to_string(),
432                         Applicability::MachineApplicable,
433                     );
434                 }
435                 Err(err)
436             }
437         }
438     }
439
440     /// Recover if we parsed attributes and expected an item but there was none.
441     fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
442         let ([start @ end] | [start, .., end]) = attrs else {
443             return Ok(());
444         };
445         let msg = if end.is_doc_comment() {
446             "expected item after doc comment"
447         } else {
448             "expected item after attributes"
449         };
450         let mut err = self.struct_span_err(end.span, msg);
451         if end.is_doc_comment() {
452             err.span_label(end.span, "this doc comment doesn't document anything");
453         }
454         if let [.., penultimate, _] = attrs {
455             err.span_label(start.span.to(penultimate.span), "other attributes here");
456         }
457         Err(err)
458     }
459
460     fn is_async_fn(&self) -> bool {
461         self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
462     }
463
464     fn parse_polarity(&mut self) -> ast::ImplPolarity {
465         // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
466         if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
467             self.bump(); // `!`
468             ast::ImplPolarity::Negative(self.prev_token.span)
469         } else {
470             ast::ImplPolarity::Positive
471         }
472     }
473
474     /// Parses an implementation item.
475     ///
476     /// ```
477     /// impl<'a, T> TYPE { /* impl items */ }
478     /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
479     /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
480     /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
481     /// ```
482     ///
483     /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
484     /// ```
485     /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
486     /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
487     /// ```
488     fn parse_item_impl(
489         &mut self,
490         attrs: &mut Vec<Attribute>,
491         defaultness: Defaultness,
492     ) -> PResult<'a, ItemInfo> {
493         let unsafety = self.parse_unsafety();
494         self.expect_keyword(kw::Impl)?;
495
496         // First, parse generic parameters if necessary.
497         let mut generics = if self.choose_generics_over_qpath(0) {
498             self.parse_generics()?
499         } else {
500             let mut generics = Generics::default();
501             // impl A for B {}
502             //    /\ this is where `generics.span` should point when there are no type params.
503             generics.span = self.prev_token.span.shrink_to_hi();
504             generics
505         };
506
507         let constness = self.parse_constness();
508         if let Const::Yes(span) = constness {
509             self.sess.gated_spans.gate(sym::const_trait_impl, span);
510         }
511
512         let polarity = self.parse_polarity();
513
514         // Parse both types and traits as a type, then reinterpret if necessary.
515         let err_path = |span| ast::Path::from_ident(Ident::new(kw::Empty, span));
516         let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
517         {
518             let span = self.prev_token.span.between(self.token.span);
519             self.struct_span_err(span, "missing trait in a trait impl")
520                 .span_suggestion(
521                     span,
522                     "add a trait here",
523                     " Trait ".into(),
524                     Applicability::HasPlaceholders,
525                 )
526                 .span_suggestion(
527                     span.to(self.token.span),
528                     "for an inherent impl, drop this `for`",
529                     "".into(),
530                     Applicability::MaybeIncorrect,
531                 )
532                 .emit();
533             P(Ty {
534                 kind: TyKind::Path(None, err_path(span)),
535                 span,
536                 id: DUMMY_NODE_ID,
537                 tokens: None,
538             })
539         } else {
540             self.parse_ty_with_generics_recovery(&generics)?
541         };
542
543         // If `for` is missing we try to recover.
544         let has_for = self.eat_keyword(kw::For);
545         let missing_for_span = self.prev_token.span.between(self.token.span);
546
547         let ty_second = if self.token == token::DotDot {
548             // We need to report this error after `cfg` expansion for compatibility reasons
549             self.bump(); // `..`, do not add it to expected tokens
550             Some(self.mk_ty(self.prev_token.span, TyKind::Err))
551         } else if has_for || self.token.can_begin_type() {
552             Some(self.parse_ty()?)
553         } else {
554             None
555         };
556
557         generics.where_clause = self.parse_where_clause()?;
558
559         let impl_items = self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?;
560
561         let item_kind = match ty_second {
562             Some(ty_second) => {
563                 // impl Trait for Type
564                 if !has_for {
565                     self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
566                         .span_suggestion_short(
567                             missing_for_span,
568                             "add `for` here",
569                             " for ".to_string(),
570                             Applicability::MachineApplicable,
571                         )
572                         .emit();
573                 }
574
575                 let ty_first = ty_first.into_inner();
576                 let path = match ty_first.kind {
577                     // This notably includes paths passed through `ty` macro fragments (#46438).
578                     TyKind::Path(None, path) => path,
579                     _ => {
580                         self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
581                         err_path(ty_first.span)
582                     }
583                 };
584                 let trait_ref = TraitRef { path, ref_id: ty_first.id };
585
586                 ItemKind::Impl(Box::new(Impl {
587                     unsafety,
588                     polarity,
589                     defaultness,
590                     constness,
591                     generics,
592                     of_trait: Some(trait_ref),
593                     self_ty: ty_second,
594                     items: impl_items,
595                 }))
596             }
597             None => {
598                 // impl Type
599                 ItemKind::Impl(Box::new(Impl {
600                     unsafety,
601                     polarity,
602                     defaultness,
603                     constness,
604                     generics,
605                     of_trait: None,
606                     self_ty: ty_first,
607                     items: impl_items,
608                 }))
609             }
610         };
611
612         Ok((Ident::empty(), item_kind))
613     }
614
615     fn parse_item_list<T>(
616         &mut self,
617         attrs: &mut Vec<Attribute>,
618         mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
619     ) -> PResult<'a, Vec<T>> {
620         let open_brace_span = self.token.span;
621         self.expect(&token::OpenDelim(token::Brace))?;
622         attrs.append(&mut self.parse_inner_attributes()?);
623
624         let mut items = Vec::new();
625         while !self.eat(&token::CloseDelim(token::Brace)) {
626             if self.recover_doc_comment_before_brace() {
627                 continue;
628             }
629             match parse_item(self) {
630                 Ok(None) => {
631                     // We have to bail or we'll potentially never make progress.
632                     let non_item_span = self.token.span;
633                     self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
634                     self.struct_span_err(non_item_span, "non-item in item list")
635                         .span_label(open_brace_span, "item list starts here")
636                         .span_label(non_item_span, "non-item starts here")
637                         .span_label(self.prev_token.span, "item list ends here")
638                         .emit();
639                     break;
640                 }
641                 Ok(Some(item)) => items.extend(item),
642                 Err(mut err) => {
643                     self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
644                     err.span_label(open_brace_span, "while parsing this item list starting here")
645                         .span_label(self.prev_token.span, "the item list ends here")
646                         .emit();
647                     break;
648                 }
649             }
650         }
651         Ok(items)
652     }
653
654     /// Recover on a doc comment before `}`.
655     fn recover_doc_comment_before_brace(&mut self) -> bool {
656         if let token::DocComment(..) = self.token.kind {
657             if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
658                 struct_span_err!(
659                     self.diagnostic(),
660                     self.token.span,
661                     E0584,
662                     "found a documentation comment that doesn't document anything",
663                 )
664                 .span_label(self.token.span, "this doc comment doesn't document anything")
665                 .help(
666                     "doc comments must come before what they document, maybe a \
667                     comment was intended with `//`?",
668                 )
669                 .emit();
670                 self.bump();
671                 return true;
672             }
673         }
674         false
675     }
676
677     /// Parses defaultness (i.e., `default` or nothing).
678     fn parse_defaultness(&mut self) -> Defaultness {
679         // We are interested in `default` followed by another identifier.
680         // However, we must avoid keywords that occur as binary operators.
681         // Currently, the only applicable keyword is `as` (`default as Ty`).
682         if self.check_keyword(kw::Default)
683             && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
684         {
685             self.bump(); // `default`
686             Defaultness::Default(self.prev_token.uninterpolated_span())
687         } else {
688             Defaultness::Final
689         }
690     }
691
692     /// Is this an `(unsafe auto? | auto) trait` item?
693     fn check_auto_or_unsafe_trait_item(&mut self) -> bool {
694         // auto trait
695         self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait])
696             // unsafe auto trait
697             || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
698     }
699
700     /// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
701     fn parse_item_trait(&mut self, attrs: &mut Vec<Attribute>, lo: Span) -> PResult<'a, ItemInfo> {
702         let unsafety = self.parse_unsafety();
703         // Parse optional `auto` prefix.
704         let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
705
706         self.expect_keyword(kw::Trait)?;
707         let ident = self.parse_ident()?;
708         let mut generics = self.parse_generics()?;
709
710         // Parse optional colon and supertrait bounds.
711         let had_colon = self.eat(&token::Colon);
712         let span_at_colon = self.prev_token.span;
713         let bounds = if had_colon {
714             self.parse_generic_bounds(Some(self.prev_token.span))?
715         } else {
716             Vec::new()
717         };
718
719         let span_before_eq = self.prev_token.span;
720         if self.eat(&token::Eq) {
721             // It's a trait alias.
722             if had_colon {
723                 let span = span_at_colon.to(span_before_eq);
724                 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
725             }
726
727             let bounds = self.parse_generic_bounds(None)?;
728             generics.where_clause = self.parse_where_clause()?;
729             self.expect_semi()?;
730
731             let whole_span = lo.to(self.prev_token.span);
732             if is_auto == IsAuto::Yes {
733                 let msg = "trait aliases cannot be `auto`";
734                 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
735             }
736             if let Unsafe::Yes(_) = unsafety {
737                 let msg = "trait aliases cannot be `unsafe`";
738                 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
739             }
740
741             self.sess.gated_spans.gate(sym::trait_alias, whole_span);
742
743             Ok((ident, ItemKind::TraitAlias(generics, bounds)))
744         } else {
745             // It's a normal trait.
746             generics.where_clause = self.parse_where_clause()?;
747             let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
748             Ok((
749                 ident,
750                 ItemKind::Trait(Box::new(Trait { is_auto, unsafety, generics, bounds, items })),
751             ))
752         }
753     }
754
755     pub fn parse_impl_item(
756         &mut self,
757         force_collect: ForceCollect,
758     ) -> PResult<'a, Option<Option<P<AssocItem>>>> {
759         let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: true };
760         self.parse_assoc_item(fn_parse_mode, force_collect)
761     }
762
763     pub fn parse_trait_item(
764         &mut self,
765         force_collect: ForceCollect,
766     ) -> PResult<'a, Option<Option<P<AssocItem>>>> {
767         let fn_parse_mode =
768             FnParseMode { req_name: |edition| edition >= Edition::Edition2018, req_body: false };
769         self.parse_assoc_item(fn_parse_mode, force_collect)
770     }
771
772     /// Parses associated items.
773     fn parse_assoc_item(
774         &mut self,
775         fn_parse_mode: FnParseMode,
776         force_collect: ForceCollect,
777     ) -> PResult<'a, Option<Option<P<AssocItem>>>> {
778         Ok(self.parse_item_(fn_parse_mode, force_collect)?.map(
779             |Item { attrs, id, span, vis, ident, kind, tokens }| {
780                 let kind = match AssocItemKind::try_from(kind) {
781                     Ok(kind) => kind,
782                     Err(kind) => match kind {
783                         ItemKind::Static(a, _, b) => {
784                             self.struct_span_err(span, "associated `static` items are not allowed")
785                                 .emit();
786                             AssocItemKind::Const(Defaultness::Final, a, b)
787                         }
788                         _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
789                     },
790                 };
791                 Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
792             },
793         ))
794     }
795
796     /// Emits an error that the where clause at the end of a type alias is not
797     /// allowed and suggests moving it.
798     fn error_ty_alias_where(
799         &self,
800         before_where_clause_present: bool,
801         before_where_clause_span: Span,
802         after_predicates: &[WherePredicate],
803         after_where_clause_span: Span,
804     ) -> ErrorReported {
805         let mut err =
806             self.struct_span_err(after_where_clause_span, "where clause not allowed here");
807         if !after_predicates.is_empty() {
808             let mut state = crate::pprust::State::new();
809             if !before_where_clause_present {
810                 state.space();
811                 state.word_space("where");
812             } else {
813                 state.word_space(",");
814             }
815             let mut first = true;
816             for p in after_predicates.iter() {
817                 if !first {
818                     state.word_space(",");
819                 }
820                 first = false;
821                 state.print_where_predicate(p);
822             }
823             let suggestion = state.s.eof();
824             err.span_suggestion(
825                 before_where_clause_span.shrink_to_hi(),
826                 "move it here",
827                 suggestion,
828                 Applicability::MachineApplicable,
829             );
830         }
831         err.emit()
832     }
833
834     /// Parses a `type` alias with the following grammar:
835     /// ```
836     /// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
837     /// ```
838     /// The `"type"` has already been eaten.
839     fn parse_type_alias(&mut self, defaultness: Defaultness) -> PResult<'a, ItemInfo> {
840         let ident = self.parse_ident()?;
841         let mut generics = self.parse_generics()?;
842
843         // Parse optional colon and param bounds.
844         let bounds =
845             if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
846
847         generics.where_clause = self.parse_where_clause()?;
848
849         let ty = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
850
851         if self.token.is_keyword(kw::Where) {
852             let after_where_clause = self.parse_where_clause()?;
853
854             self.error_ty_alias_where(
855                 generics.where_clause.has_where_token,
856                 generics.where_clause.span,
857                 &after_where_clause.predicates,
858                 after_where_clause.span,
859             );
860
861             generics.where_clause.predicates.extend(after_where_clause.predicates.into_iter());
862         }
863
864         self.expect_semi()?;
865
866         Ok((ident, ItemKind::TyAlias(Box::new(TyAlias { defaultness, generics, bounds, ty }))))
867     }
868
869     /// Parses a `UseTree`.
870     ///
871     /// ```text
872     /// USE_TREE = [`::`] `*` |
873     ///            [`::`] `{` USE_TREE_LIST `}` |
874     ///            PATH `::` `*` |
875     ///            PATH `::` `{` USE_TREE_LIST `}` |
876     ///            PATH [`as` IDENT]
877     /// ```
878     fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
879         let lo = self.token.span;
880
881         let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo(), tokens: None };
882         let kind = if self.check(&token::OpenDelim(token::Brace))
883             || self.check(&token::BinOp(token::Star))
884             || self.is_import_coupler()
885         {
886             // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
887             let mod_sep_ctxt = self.token.span.ctxt();
888             if self.eat(&token::ModSep) {
889                 prefix
890                     .segments
891                     .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
892             }
893
894             self.parse_use_tree_glob_or_nested()?
895         } else {
896             // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
897             prefix = self.parse_path(PathStyle::Mod)?;
898
899             if self.eat(&token::ModSep) {
900                 self.parse_use_tree_glob_or_nested()?
901             } else {
902                 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
903             }
904         };
905
906         Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
907     }
908
909     /// Parses `*` or `{...}`.
910     fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
911         Ok(if self.eat(&token::BinOp(token::Star)) {
912             UseTreeKind::Glob
913         } else {
914             UseTreeKind::Nested(self.parse_use_tree_list()?)
915         })
916     }
917
918     /// Parses a `UseTreeKind::Nested(list)`.
919     ///
920     /// ```text
921     /// USE_TREE_LIST = Ã˜ | (USE_TREE `,`)* USE_TREE [`,`]
922     /// ```
923     fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
924         self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
925             .map(|(r, _)| r)
926     }
927
928     fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
929         if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
930     }
931
932     fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
933         match self.token.ident() {
934             Some((ident @ Ident { name: kw::Underscore, .. }, false)) => {
935                 self.bump();
936                 Ok(ident)
937             }
938             _ => self.parse_ident(),
939         }
940     }
941
942     /// Parses `extern crate` links.
943     ///
944     /// # Examples
945     ///
946     /// ```
947     /// extern crate foo;
948     /// extern crate bar as foo;
949     /// ```
950     fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemInfo> {
951         // Accept `extern crate name-like-this` for better diagnostics
952         let orig_name = self.parse_crate_name_with_dashes()?;
953         let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
954             (rename, Some(orig_name.name))
955         } else {
956             (orig_name, None)
957         };
958         self.expect_semi()?;
959         Ok((item_name, ItemKind::ExternCrate(orig_name)))
960     }
961
962     fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
963         let error_msg = "crate name using dashes are not valid in `extern crate` statements";
964         let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
965                               in the code";
966         let mut ident = if self.token.is_keyword(kw::SelfLower) {
967             self.parse_path_segment_ident()
968         } else {
969             self.parse_ident()
970         }?;
971         let mut idents = vec![];
972         let mut replacement = vec![];
973         let mut fixed_crate_name = false;
974         // Accept `extern crate name-like-this` for better diagnostics.
975         let dash = token::BinOp(token::BinOpToken::Minus);
976         if self.token == dash {
977             // Do not include `-` as part of the expected tokens list.
978             while self.eat(&dash) {
979                 fixed_crate_name = true;
980                 replacement.push((self.prev_token.span, "_".to_string()));
981                 idents.push(self.parse_ident()?);
982             }
983         }
984         if fixed_crate_name {
985             let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
986             let mut fixed_name = format!("{}", ident.name);
987             for part in idents {
988                 fixed_name.push_str(&format!("_{}", part.name));
989             }
990             ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
991
992             self.struct_span_err(fixed_name_sp, error_msg)
993                 .span_label(fixed_name_sp, "dash-separated idents are not valid")
994                 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
995                 .emit();
996         }
997         Ok(ident)
998     }
999
1000     /// Parses `extern` for foreign ABIs modules.
1001     ///
1002     /// `extern` is expected to have been consumed before calling this method.
1003     ///
1004     /// # Examples
1005     ///
1006     /// ```ignore (only-for-syntax-highlight)
1007     /// extern "C" {}
1008     /// extern {}
1009     /// ```
1010     fn parse_item_foreign_mod(
1011         &mut self,
1012         attrs: &mut Vec<Attribute>,
1013         unsafety: Unsafe,
1014     ) -> PResult<'a, ItemInfo> {
1015         let abi = self.parse_abi(); // ABI?
1016         let items = self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?;
1017         let module = ast::ForeignMod { unsafety, abi, items };
1018         Ok((Ident::empty(), ItemKind::ForeignMod(module)))
1019     }
1020
1021     /// Parses a foreign item (one in an `extern { ... }` block).
1022     pub fn parse_foreign_item(
1023         &mut self,
1024         force_collect: ForceCollect,
1025     ) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
1026         let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: false };
1027         Ok(self.parse_item_(fn_parse_mode, force_collect)?.map(
1028             |Item { attrs, id, span, vis, ident, kind, tokens }| {
1029                 let kind = match ForeignItemKind::try_from(kind) {
1030                     Ok(kind) => kind,
1031                     Err(kind) => match kind {
1032                         ItemKind::Const(_, a, b) => {
1033                             self.error_on_foreign_const(span, ident);
1034                             ForeignItemKind::Static(a, Mutability::Not, b)
1035                         }
1036                         _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
1037                     },
1038                 };
1039                 Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
1040             },
1041         ))
1042     }
1043
1044     fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
1045         let span = self.sess.source_map().guess_head_span(span);
1046         let descr = kind.descr();
1047         self.struct_span_err(span, &format!("{} is not supported in {}", descr, ctx))
1048             .help(&format!("consider moving the {} out to a nearby module scope", descr))
1049             .emit();
1050         None
1051     }
1052
1053     fn error_on_foreign_const(&self, span: Span, ident: Ident) {
1054         self.struct_span_err(ident.span, "extern items cannot be `const`")
1055             .span_suggestion(
1056                 span.with_hi(ident.span.lo()),
1057                 "try using a static value",
1058                 "static ".to_string(),
1059                 Applicability::MachineApplicable,
1060             )
1061             .note("for more information, visit https://doc.rust-lang.org/std/keyword.extern.html")
1062             .emit();
1063     }
1064
1065     fn is_unsafe_foreign_mod(&self) -> bool {
1066         self.token.is_keyword(kw::Unsafe)
1067             && self.is_keyword_ahead(1, &[kw::Extern])
1068             && self.look_ahead(
1069                 2 + self.look_ahead(2, |t| t.can_begin_literal_maybe_minus() as usize),
1070                 |t| t.kind == token::OpenDelim(token::Brace),
1071             )
1072     }
1073
1074     fn is_static_global(&mut self) -> bool {
1075         if self.check_keyword(kw::Static) {
1076             // Check if this could be a closure.
1077             !self.look_ahead(1, |token| {
1078                 if token.is_keyword(kw::Move) {
1079                     return true;
1080                 }
1081                 matches!(token.kind, token::BinOp(token::Or) | token::OrOr)
1082             })
1083         } else {
1084             false
1085         }
1086     }
1087
1088     /// Recover on `const mut` with `const` already eaten.
1089     fn recover_const_mut(&mut self, const_span: Span) {
1090         if self.eat_keyword(kw::Mut) {
1091             let span = self.prev_token.span;
1092             self.struct_span_err(span, "const globals cannot be mutable")
1093                 .span_label(span, "cannot be mutable")
1094                 .span_suggestion(
1095                     const_span,
1096                     "you might want to declare a static instead",
1097                     "static".to_owned(),
1098                     Applicability::MaybeIncorrect,
1099                 )
1100                 .emit();
1101         }
1102     }
1103
1104     /// Recover on `const impl` with `const` already eaten.
1105     fn recover_const_impl(
1106         &mut self,
1107         const_span: Span,
1108         attrs: &mut Vec<Attribute>,
1109         defaultness: Defaultness,
1110     ) -> PResult<'a, ItemInfo> {
1111         let impl_span = self.token.span;
1112         let mut err = self.expected_ident_found();
1113
1114         // Only try to recover if this is implementing a trait for a type
1115         let mut impl_info = match self.parse_item_impl(attrs, defaultness) {
1116             Ok(impl_info) => impl_info,
1117             Err(recovery_error) => {
1118                 // Recovery failed, raise the "expected identifier" error
1119                 recovery_error.cancel();
1120                 return Err(err);
1121             }
1122         };
1123
1124         match impl_info.1 {
1125             ItemKind::Impl(box Impl { of_trait: Some(ref trai), ref mut constness, .. }) => {
1126                 *constness = Const::Yes(const_span);
1127
1128                 let before_trait = trai.path.span.shrink_to_lo();
1129                 let const_up_to_impl = const_span.with_hi(impl_span.lo());
1130                 err.multipart_suggestion(
1131                     "you might have meant to write a const trait impl",
1132                     vec![(const_up_to_impl, "".to_owned()), (before_trait, "const ".to_owned())],
1133                     Applicability::MaybeIncorrect,
1134                 )
1135                 .emit();
1136             }
1137             ItemKind::Impl { .. } => return Err(err),
1138             _ => unreachable!(),
1139         }
1140
1141         Ok(impl_info)
1142     }
1143
1144     /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty (= $expr)?` with
1145     /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1146     ///
1147     /// When `m` is `"const"`, `$ident` may also be `"_"`.
1148     fn parse_item_global(
1149         &mut self,
1150         m: Option<Mutability>,
1151     ) -> PResult<'a, (Ident, P<Ty>, Option<P<ast::Expr>>)> {
1152         let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1153
1154         // Parse the type of a `const` or `static mut?` item.
1155         // That is, the `":" $ty` fragment.
1156         let ty = if self.eat(&token::Colon) {
1157             self.parse_ty()?
1158         } else {
1159             self.recover_missing_const_type(id, m)
1160         };
1161
1162         let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
1163         self.expect_semi()?;
1164         Ok((id, ty, expr))
1165     }
1166
1167     /// We were supposed to parse `:` but the `:` was missing.
1168     /// This means that the type is missing.
1169     fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1170         // Construct the error and stash it away with the hope
1171         // that typeck will later enrich the error with a type.
1172         let kind = match m {
1173             Some(Mutability::Mut) => "static mut",
1174             Some(Mutability::Not) => "static",
1175             None => "const",
1176         };
1177         let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1178         err.span_suggestion(
1179             id.span,
1180             "provide a type for the item",
1181             format!("{}: <type>", id),
1182             Applicability::HasPlaceholders,
1183         );
1184         err.stash(id.span, StashKey::ItemNoType);
1185
1186         // The user intended that the type be inferred,
1187         // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1188         P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID, tokens: None })
1189     }
1190
1191     /// Parses an enum declaration.
1192     fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1193         let id = self.parse_ident()?;
1194         let mut generics = self.parse_generics()?;
1195         generics.where_clause = self.parse_where_clause()?;
1196
1197         let (variants, _) =
1198             self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1199                 self.recover_stmt();
1200                 e
1201             })?;
1202
1203         let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1204         Ok((id, ItemKind::Enum(enum_definition, generics)))
1205     }
1206
1207     fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1208         let variant_attrs = self.parse_outer_attributes()?;
1209         self.collect_tokens_trailing_token(
1210             variant_attrs,
1211             ForceCollect::No,
1212             |this, variant_attrs| {
1213                 let vlo = this.token.span;
1214
1215                 let vis = this.parse_visibility(FollowedByType::No)?;
1216                 if !this.recover_nested_adt_item(kw::Enum)? {
1217                     return Ok((None, TrailingToken::None));
1218                 }
1219                 let ident = this.parse_field_ident("enum", vlo)?;
1220
1221                 let struct_def = if this.check(&token::OpenDelim(token::Brace)) {
1222                     // Parse a struct variant.
1223                     let (fields, recovered) = this.parse_record_struct_body("struct", false)?;
1224                     VariantData::Struct(fields, recovered)
1225                 } else if this.check(&token::OpenDelim(token::Paren)) {
1226                     VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1227                 } else {
1228                     VariantData::Unit(DUMMY_NODE_ID)
1229                 };
1230
1231                 let disr_expr =
1232                     if this.eat(&token::Eq) { Some(this.parse_anon_const_expr()?) } else { None };
1233
1234                 let vr = ast::Variant {
1235                     ident,
1236                     vis,
1237                     id: DUMMY_NODE_ID,
1238                     attrs: variant_attrs.into(),
1239                     data: struct_def,
1240                     disr_expr,
1241                     span: vlo.to(this.prev_token.span),
1242                     is_placeholder: false,
1243                 };
1244
1245                 Ok((Some(vr), TrailingToken::MaybeComma))
1246             },
1247         )
1248     }
1249
1250     /// Parses `struct Foo { ... }`.
1251     fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1252         let class_name = self.parse_ident()?;
1253
1254         let mut generics = self.parse_generics()?;
1255
1256         // There is a special case worth noting here, as reported in issue #17904.
1257         // If we are parsing a tuple struct it is the case that the where clause
1258         // should follow the field list. Like so:
1259         //
1260         // struct Foo<T>(T) where T: Copy;
1261         //
1262         // If we are parsing a normal record-style struct it is the case
1263         // that the where clause comes before the body, and after the generics.
1264         // So if we look ahead and see a brace or a where-clause we begin
1265         // parsing a record style struct.
1266         //
1267         // Otherwise if we look ahead and see a paren we parse a tuple-style
1268         // struct.
1269
1270         let vdata = if self.token.is_keyword(kw::Where) {
1271             generics.where_clause = self.parse_where_clause()?;
1272             if self.eat(&token::Semi) {
1273                 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1274                 VariantData::Unit(DUMMY_NODE_ID)
1275             } else {
1276                 // If we see: `struct Foo<T> where T: Copy { ... }`
1277                 let (fields, recovered) =
1278                     self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?;
1279                 VariantData::Struct(fields, recovered)
1280             }
1281         // No `where` so: `struct Foo<T>;`
1282         } else if self.eat(&token::Semi) {
1283             VariantData::Unit(DUMMY_NODE_ID)
1284         // Record-style struct definition
1285         } else if self.token == token::OpenDelim(token::Brace) {
1286             let (fields, recovered) =
1287                 self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?;
1288             VariantData::Struct(fields, recovered)
1289         // Tuple-style struct definition with optional where-clause.
1290         } else if self.token == token::OpenDelim(token::Paren) {
1291             let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1292             generics.where_clause = self.parse_where_clause()?;
1293             self.expect_semi()?;
1294             body
1295         } else {
1296             let token_str = super::token_descr(&self.token);
1297             let msg = &format!(
1298                 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1299                 token_str
1300             );
1301             let mut err = self.struct_span_err(self.token.span, msg);
1302             err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1303             return Err(err);
1304         };
1305
1306         Ok((class_name, ItemKind::Struct(vdata, generics)))
1307     }
1308
1309     /// Parses `union Foo { ... }`.
1310     fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1311         let class_name = self.parse_ident()?;
1312
1313         let mut generics = self.parse_generics()?;
1314
1315         let vdata = if self.token.is_keyword(kw::Where) {
1316             generics.where_clause = self.parse_where_clause()?;
1317             let (fields, recovered) =
1318                 self.parse_record_struct_body("union", generics.where_clause.has_where_token)?;
1319             VariantData::Struct(fields, recovered)
1320         } else if self.token == token::OpenDelim(token::Brace) {
1321             let (fields, recovered) =
1322                 self.parse_record_struct_body("union", generics.where_clause.has_where_token)?;
1323             VariantData::Struct(fields, recovered)
1324         } else {
1325             let token_str = super::token_descr(&self.token);
1326             let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1327             let mut err = self.struct_span_err(self.token.span, msg);
1328             err.span_label(self.token.span, "expected `where` or `{` after union name");
1329             return Err(err);
1330         };
1331
1332         Ok((class_name, ItemKind::Union(vdata, generics)))
1333     }
1334
1335     fn parse_record_struct_body(
1336         &mut self,
1337         adt_ty: &str,
1338         parsed_where: bool,
1339     ) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
1340         let mut fields = Vec::new();
1341         let mut recovered = false;
1342         if self.eat(&token::OpenDelim(token::Brace)) {
1343             while self.token != token::CloseDelim(token::Brace) {
1344                 let field = self.parse_field_def(adt_ty).map_err(|e| {
1345                     self.consume_block(token::Brace, ConsumeClosingDelim::No);
1346                     recovered = true;
1347                     e
1348                 });
1349                 match field {
1350                     Ok(field) => fields.push(field),
1351                     Err(mut err) => {
1352                         err.emit();
1353                         break;
1354                     }
1355                 }
1356             }
1357             self.eat(&token::CloseDelim(token::Brace));
1358         } else {
1359             let token_str = super::token_descr(&self.token);
1360             let msg = &format!(
1361                 "expected {}`{{` after struct name, found {}",
1362                 if parsed_where { "" } else { "`where`, or " },
1363                 token_str
1364             );
1365             let mut err = self.struct_span_err(self.token.span, msg);
1366             err.span_label(
1367                 self.token.span,
1368                 format!(
1369                     "expected {}`{{` after struct name",
1370                     if parsed_where { "" } else { "`where`, or " }
1371                 ),
1372             );
1373             return Err(err);
1374         }
1375
1376         Ok((fields, recovered))
1377     }
1378
1379     fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<FieldDef>> {
1380         // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1381         // Unit like structs are handled in parse_item_struct function
1382         self.parse_paren_comma_seq(|p| {
1383             let attrs = p.parse_outer_attributes()?;
1384             p.collect_tokens_trailing_token(attrs, ForceCollect::No, |p, attrs| {
1385                 let lo = p.token.span;
1386                 let vis = p.parse_visibility(FollowedByType::Yes)?;
1387                 let ty = p.parse_ty()?;
1388
1389                 Ok((
1390                     FieldDef {
1391                         span: lo.to(ty.span),
1392                         vis,
1393                         ident: None,
1394                         id: DUMMY_NODE_ID,
1395                         ty,
1396                         attrs: attrs.into(),
1397                         is_placeholder: false,
1398                     },
1399                     TrailingToken::MaybeComma,
1400                 ))
1401             })
1402         })
1403         .map(|(r, _)| r)
1404     }
1405
1406     /// Parses an element of a struct declaration.
1407     fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> {
1408         let attrs = self.parse_outer_attributes()?;
1409         self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
1410             let lo = this.token.span;
1411             let vis = this.parse_visibility(FollowedByType::No)?;
1412             Ok((this.parse_single_struct_field(adt_ty, lo, vis, attrs)?, TrailingToken::None))
1413         })
1414     }
1415
1416     /// Parses a structure field declaration.
1417     fn parse_single_struct_field(
1418         &mut self,
1419         adt_ty: &str,
1420         lo: Span,
1421         vis: Visibility,
1422         attrs: Vec<Attribute>,
1423     ) -> PResult<'a, FieldDef> {
1424         let mut seen_comma: bool = false;
1425         let a_var = self.parse_name_and_ty(adt_ty, lo, vis, attrs)?;
1426         if self.token == token::Comma {
1427             seen_comma = true;
1428         }
1429         match self.token.kind {
1430             token::Comma => {
1431                 self.bump();
1432             }
1433             token::CloseDelim(token::Brace) => {}
1434             token::DocComment(..) => {
1435                 let previous_span = self.prev_token.span;
1436                 let mut err = self.span_err(self.token.span, Error::UselessDocComment);
1437                 self.bump(); // consume the doc comment
1438                 let comma_after_doc_seen = self.eat(&token::Comma);
1439                 // `seen_comma` is always false, because we are inside doc block
1440                 // condition is here to make code more readable
1441                 if !seen_comma && comma_after_doc_seen {
1442                     seen_comma = true;
1443                 }
1444                 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1445                     err.emit();
1446                 } else {
1447                     if !seen_comma {
1448                         let sp = self.sess.source_map().next_point(previous_span);
1449                         err.span_suggestion(
1450                             sp,
1451                             "missing comma here",
1452                             ",".into(),
1453                             Applicability::MachineApplicable,
1454                         );
1455                     }
1456                     return Err(err);
1457                 }
1458             }
1459             _ => {
1460                 let sp = self.prev_token.span.shrink_to_hi();
1461                 let mut err = self.struct_span_err(
1462                     sp,
1463                     &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1464                 );
1465
1466                 // Try to recover extra trailing angle brackets
1467                 let mut recovered = false;
1468                 if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind {
1469                     if let Some(last_segment) = segments.last() {
1470                         recovered = self.check_trailing_angle_brackets(
1471                             last_segment,
1472                             &[&token::Comma, &token::CloseDelim(token::Brace)],
1473                         );
1474                         if recovered {
1475                             // Handle a case like `Vec<u8>>,` where we can continue parsing fields
1476                             // after the comma
1477                             self.eat(&token::Comma);
1478                             // `check_trailing_angle_brackets` already emitted a nicer error
1479                             // NOTE(eddyb) this was `.cancel()`, but `err`
1480                             // gets returned, so we can't fully defuse it.
1481                             err.downgrade_to_delayed_bug();
1482                         }
1483                     }
1484                 }
1485
1486                 if self.token.is_ident() {
1487                     // This is likely another field; emit the diagnostic and keep going
1488                     err.span_suggestion(
1489                         sp,
1490                         "try adding a comma",
1491                         ",".into(),
1492                         Applicability::MachineApplicable,
1493                     );
1494                     err.emit();
1495                     recovered = true;
1496                 }
1497
1498                 if recovered {
1499                     // Make sure an error was emitted (either by recovering an angle bracket,
1500                     // or by finding an identifier as the next token), since we're
1501                     // going to continue parsing
1502                     assert!(self.sess.span_diagnostic.has_errors());
1503                 } else {
1504                     return Err(err);
1505                 }
1506             }
1507         }
1508         Ok(a_var)
1509     }
1510
1511     fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
1512         if let Err(mut err) = self.expect(&token::Colon) {
1513             let sm = self.sess.source_map();
1514             let eq_typo = self.token.kind == token::Eq && self.look_ahead(1, |t| t.is_path_start());
1515             let semi_typo = self.token.kind == token::Semi
1516                 && self.look_ahead(1, |t| {
1517                     t.is_path_start()
1518                     // We check that we are in a situation like `foo; bar` to avoid bad suggestions
1519                     // when there's no type and `;` was used instead of a comma.
1520                     && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
1521                         (Ok(l), Ok(r)) => l.line == r.line,
1522                         _ => true,
1523                     }
1524                 });
1525             if eq_typo || semi_typo {
1526                 self.bump();
1527                 // Gracefully handle small typos.
1528                 err.span_suggestion_short(
1529                     self.prev_token.span,
1530                     "field names and their types are separated with `:`",
1531                     ":".to_string(),
1532                     Applicability::MachineApplicable,
1533                 );
1534                 err.emit();
1535             } else {
1536                 return Err(err);
1537             }
1538         }
1539         Ok(())
1540     }
1541
1542     /// Parses a structure field.
1543     fn parse_name_and_ty(
1544         &mut self,
1545         adt_ty: &str,
1546         lo: Span,
1547         vis: Visibility,
1548         attrs: Vec<Attribute>,
1549     ) -> PResult<'a, FieldDef> {
1550         let name = self.parse_field_ident(adt_ty, lo)?;
1551         self.expect_field_ty_separator()?;
1552         let ty = self.parse_ty()?;
1553         if self.token.kind == token::Eq {
1554             self.bump();
1555             let const_expr = self.parse_anon_const_expr()?;
1556             let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
1557             self.struct_span_err(sp, "default values on `struct` fields aren't supported")
1558                 .span_suggestion(
1559                     sp,
1560                     "remove this unsupported default value",
1561                     String::new(),
1562                     Applicability::MachineApplicable,
1563                 )
1564                 .emit();
1565         }
1566         Ok(FieldDef {
1567             span: lo.to(self.prev_token.span),
1568             ident: Some(name),
1569             vis,
1570             id: DUMMY_NODE_ID,
1571             ty,
1572             attrs: attrs.into(),
1573             is_placeholder: false,
1574         })
1575     }
1576
1577     /// Parses a field identifier. Specialized version of `parse_ident_common`
1578     /// for better diagnostics and suggestions.
1579     fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
1580         let (ident, is_raw) = self.ident_or_err()?;
1581         if !is_raw && ident.is_reserved() {
1582             let err = if self.check_fn_front_matter(false) {
1583                 let inherited_vis = Visibility {
1584                     span: rustc_span::DUMMY_SP,
1585                     kind: VisibilityKind::Inherited,
1586                     tokens: None,
1587                 };
1588                 // We use `parse_fn` to get a span for the function
1589                 let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: true };
1590                 if let Err(mut db) =
1591                     self.parse_fn(&mut Vec::new(), fn_parse_mode, lo, &inherited_vis)
1592                 {
1593                     db.delay_as_bug();
1594                 }
1595                 let mut err = self.struct_span_err(
1596                     lo.to(self.prev_token.span),
1597                     &format!("functions are not allowed in {} definitions", adt_ty),
1598                 );
1599                 err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
1600                 err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
1601                 err
1602             } else {
1603                 self.expected_ident_found()
1604             };
1605             return Err(err);
1606         }
1607         self.bump();
1608         Ok(ident)
1609     }
1610
1611     /// Parses a declarative macro 2.0 definition.
1612     /// The `macro` keyword has already been parsed.
1613     /// ```
1614     /// MacBody = "{" TOKEN_STREAM "}" ;
1615     /// MacParams = "(" TOKEN_STREAM ")" ;
1616     /// DeclMac = "macro" Ident MacParams? MacBody ;
1617     /// ```
1618     fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemInfo> {
1619         let ident = self.parse_ident()?;
1620         let body = if self.check(&token::OpenDelim(token::Brace)) {
1621             self.parse_mac_args()? // `MacBody`
1622         } else if self.check(&token::OpenDelim(token::Paren)) {
1623             let params = self.parse_token_tree(); // `MacParams`
1624             let pspan = params.span();
1625             if !self.check(&token::OpenDelim(token::Brace)) {
1626                 return self.unexpected();
1627             }
1628             let body = self.parse_token_tree(); // `MacBody`
1629             // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
1630             let bspan = body.span();
1631             let arrow = TokenTree::token(token::FatArrow, pspan.between(bspan)); // `=>`
1632             let tokens = TokenStream::new(vec![params.into(), arrow.into(), body.into()]);
1633             let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1634             P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1635         } else {
1636             return self.unexpected();
1637         };
1638
1639         self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
1640         Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: false })))
1641     }
1642
1643     /// Is this a possibly malformed start of a `macro_rules! foo` item definition?
1644
1645     fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
1646         if self.check_keyword(kw::MacroRules) {
1647             let macro_rules_span = self.token.span;
1648
1649             if self.look_ahead(1, |t| *t == token::Not) && self.look_ahead(2, |t| t.is_ident()) {
1650                 return IsMacroRulesItem::Yes { has_bang: true };
1651             } else if self.look_ahead(1, |t| (t.is_ident())) {
1652                 // macro_rules foo
1653                 self.struct_span_err(macro_rules_span, "expected `!` after `macro_rules`")
1654                     .span_suggestion(
1655                         macro_rules_span,
1656                         "add a `!`",
1657                         "macro_rules!".to_owned(),
1658                         Applicability::MachineApplicable,
1659                     )
1660                     .emit();
1661
1662                 return IsMacroRulesItem::Yes { has_bang: false };
1663             }
1664         }
1665
1666         IsMacroRulesItem::No
1667     }
1668
1669     /// Parses a `macro_rules! foo { ... }` declarative macro.
1670     fn parse_item_macro_rules(
1671         &mut self,
1672         vis: &Visibility,
1673         has_bang: bool,
1674     ) -> PResult<'a, ItemInfo> {
1675         self.expect_keyword(kw::MacroRules)?; // `macro_rules`
1676
1677         if has_bang {
1678             self.expect(&token::Not)?; // `!`
1679         }
1680         let ident = self.parse_ident()?;
1681
1682         if self.eat(&token::Not) {
1683             // Handle macro_rules! foo!
1684             let span = self.prev_token.span;
1685             self.struct_span_err(span, "macro names aren't followed by a `!`")
1686                 .span_suggestion(
1687                     span,
1688                     "remove the `!`",
1689                     "".to_owned(),
1690                     Applicability::MachineApplicable,
1691                 )
1692                 .emit();
1693         }
1694
1695         let body = self.parse_mac_args()?;
1696         self.eat_semi_for_macro_if_needed(&body);
1697         self.complain_if_pub_macro(vis, true);
1698
1699         Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: true })))
1700     }
1701
1702     /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
1703     /// If that's not the case, emit an error.
1704     fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
1705         if let VisibilityKind::Inherited = vis.kind {
1706             return;
1707         }
1708
1709         let vstr = pprust::vis_to_string(vis);
1710         let vstr = vstr.trim_end();
1711         if macro_rules {
1712             let msg = format!("can't qualify macro_rules invocation with `{}`", vstr);
1713             self.struct_span_err(vis.span, &msg)
1714                 .span_suggestion(
1715                     vis.span,
1716                     "try exporting the macro",
1717                     "#[macro_export]".to_owned(),
1718                     Applicability::MaybeIncorrect, // speculative
1719                 )
1720                 .emit();
1721         } else {
1722             self.struct_span_err(vis.span, "can't qualify macro invocation with `pub`")
1723                 .span_suggestion(
1724                     vis.span,
1725                     "remove the visibility",
1726                     String::new(),
1727                     Applicability::MachineApplicable,
1728                 )
1729                 .help(&format!("try adjusting the macro to put `{}` inside the invocation", vstr))
1730                 .emit();
1731         }
1732     }
1733
1734     fn eat_semi_for_macro_if_needed(&mut self, args: &MacArgs) {
1735         if args.need_semicolon() && !self.eat(&token::Semi) {
1736             self.report_invalid_macro_expansion_item(args);
1737         }
1738     }
1739
1740     fn report_invalid_macro_expansion_item(&self, args: &MacArgs) {
1741         let span = args.span().expect("undelimited macro call");
1742         let mut err = self.struct_span_err(
1743             span,
1744             "macros that expand to items must be delimited with braces or followed by a semicolon",
1745         );
1746         if self.unclosed_delims.is_empty() {
1747             let DelimSpan { open, close } = match args {
1748                 MacArgs::Empty | MacArgs::Eq(..) => unreachable!(),
1749                 MacArgs::Delimited(dspan, ..) => *dspan,
1750             };
1751             err.multipart_suggestion(
1752                 "change the delimiters to curly braces",
1753                 vec![(open, "{".to_string()), (close, '}'.to_string())],
1754                 Applicability::MaybeIncorrect,
1755             );
1756         } else {
1757             err.span_suggestion(
1758                 span,
1759                 "change the delimiters to curly braces",
1760                 " { /* items */ }".to_string(),
1761                 Applicability::HasPlaceholders,
1762             );
1763         }
1764         err.span_suggestion(
1765             span.shrink_to_hi(),
1766             "add a semicolon",
1767             ';'.to_string(),
1768             Applicability::MaybeIncorrect,
1769         );
1770         err.emit();
1771     }
1772
1773     /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1774     /// it is, we try to parse the item and report error about nested types.
1775     fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1776         if (self.token.is_keyword(kw::Enum)
1777             || self.token.is_keyword(kw::Struct)
1778             || self.token.is_keyword(kw::Union))
1779             && self.look_ahead(1, |t| t.is_ident())
1780         {
1781             let kw_token = self.token.clone();
1782             let kw_str = pprust::token_to_string(&kw_token);
1783             let item = self.parse_item(ForceCollect::No)?;
1784
1785             self.struct_span_err(
1786                 kw_token.span,
1787                 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1788             )
1789             .span_suggestion(
1790                 item.unwrap().span,
1791                 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1792                 String::new(),
1793                 Applicability::MaybeIncorrect,
1794             )
1795             .emit();
1796             // We successfully parsed the item but we must inform the caller about nested problem.
1797             return Ok(false);
1798         }
1799         Ok(true)
1800     }
1801 }
1802
1803 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1804 ///
1805 /// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1806 ///
1807 /// This function pointer accepts an edition, because in edition 2015, trait declarations
1808 /// were allowed to omit parameter names. In 2018, they became required.
1809 type ReqName = fn(Edition) -> bool;
1810
1811 /// Parsing configuration for functions.
1812 ///
1813 /// The syntax of function items is slightly different within trait definitions,
1814 /// impl blocks, and modules. It is still parsed using the same code, just with
1815 /// different flags set, so that even when the input is wrong and produces a parse
1816 /// error, it still gets into the AST and the rest of the parser and
1817 /// type checker can run.
1818 #[derive(Clone, Copy)]
1819 pub(crate) struct FnParseMode {
1820     /// A function pointer that decides if, per-parameter `p`, `p` must have a
1821     /// pattern or just a type. This field affects parsing of the parameters list.
1822     ///
1823     /// ```text
1824     /// fn foo(alef: A) -> X { X::new() }
1825     ///        -----^^ affects parsing this part of the function signature
1826     ///        |
1827     ///        if req_name returns false, then this name is optional
1828     ///
1829     /// fn bar(A) -> X;
1830     ///        ^
1831     ///        |
1832     ///        if req_name returns true, this is an error
1833     /// ```
1834     ///
1835     /// Calling this function pointer should only return false if:
1836     ///
1837     ///   * The item is being parsed inside of a trait definition.
1838     ///     Within an impl block or a module, it should always evaluate
1839     ///     to true.
1840     ///   * The span is from Edition 2015. In particular, you can get a
1841     ///     2015 span inside a 2021 crate using macros.
1842     pub req_name: ReqName,
1843     /// If this flag is set to `true`, then plain, semicolon-terminated function
1844     /// prototypes are not allowed here.
1845     ///
1846     /// ```text
1847     /// fn foo(alef: A) -> X { X::new() }
1848     ///                      ^^^^^^^^^^^^
1849     ///                      |
1850     ///                      this is always allowed
1851     ///
1852     /// fn bar(alef: A, bet: B) -> X;
1853     ///                             ^
1854     ///                             |
1855     ///                             if req_body is set to true, this is an error
1856     /// ```
1857     ///
1858     /// This field should only be set to false if the item is inside of a trait
1859     /// definition or extern block. Within an impl block or a module, it should
1860     /// always be set to true.
1861     pub req_body: bool,
1862 }
1863
1864 /// Parsing of functions and methods.
1865 impl<'a> Parser<'a> {
1866     /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
1867     fn parse_fn(
1868         &mut self,
1869         attrs: &mut Vec<Attribute>,
1870         fn_parse_mode: FnParseMode,
1871         sig_lo: Span,
1872         vis: &Visibility,
1873     ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
1874         let header = self.parse_fn_front_matter(vis)?; // `const ... fn`
1875         let ident = self.parse_ident()?; // `foo`
1876         let mut generics = self.parse_generics()?; // `<'a, T, ...>`
1877         let decl =
1878             self.parse_fn_decl(fn_parse_mode.req_name, AllowPlus::Yes, RecoverReturnSign::Yes)?; // `(p: u8, ...)`
1879         generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
1880
1881         let mut sig_hi = self.prev_token.span;
1882         let body = self.parse_fn_body(attrs, &ident, &mut sig_hi, fn_parse_mode.req_body)?; // `;` or `{ ... }`.
1883         let fn_sig_span = sig_lo.to(sig_hi);
1884         Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, body))
1885     }
1886
1887     /// Parse the "body" of a function.
1888     /// This can either be `;` when there's no body,
1889     /// or e.g. a block when the function is a provided one.
1890     fn parse_fn_body(
1891         &mut self,
1892         attrs: &mut Vec<Attribute>,
1893         ident: &Ident,
1894         sig_hi: &mut Span,
1895         req_body: bool,
1896     ) -> PResult<'a, Option<P<Block>>> {
1897         let has_semi = if req_body {
1898             self.token.kind == TokenKind::Semi
1899         } else {
1900             // Only include `;` in list of expected tokens if body is not required
1901             self.check(&TokenKind::Semi)
1902         };
1903         let (inner_attrs, body) = if has_semi {
1904             // Include the trailing semicolon in the span of the signature
1905             self.expect_semi()?;
1906             *sig_hi = self.prev_token.span;
1907             (Vec::new(), None)
1908         } else if self.check(&token::OpenDelim(token::Brace)) || self.token.is_whole_block() {
1909             self.parse_inner_attrs_and_block().map(|(attrs, body)| (attrs, Some(body)))?
1910         } else if self.token.kind == token::Eq {
1911             // Recover `fn foo() = $expr;`.
1912             self.bump(); // `=`
1913             let eq_sp = self.prev_token.span;
1914             let _ = self.parse_expr()?;
1915             self.expect_semi()?; // `;`
1916             let span = eq_sp.to(self.prev_token.span);
1917             self.struct_span_err(span, "function body cannot be `= expression;`")
1918                 .multipart_suggestion(
1919                     "surround the expression with `{` and `}` instead of `=` and `;`",
1920                     vec![(eq_sp, "{".to_string()), (self.prev_token.span, " }".to_string())],
1921                     Applicability::MachineApplicable,
1922                 )
1923                 .emit();
1924             (Vec::new(), Some(self.mk_block_err(span)))
1925         } else {
1926             let expected = if req_body {
1927                 &[token::OpenDelim(token::Brace)][..]
1928             } else {
1929                 &[token::Semi, token::OpenDelim(token::Brace)]
1930             };
1931             if let Err(mut err) = self.expected_one_of_not_found(&[], &expected) {
1932                 if self.token.kind == token::CloseDelim(token::Brace) {
1933                     // The enclosing `mod`, `trait` or `impl` is being closed, so keep the `fn` in
1934                     // the AST for typechecking.
1935                     err.span_label(ident.span, "while parsing this `fn`");
1936                     err.emit();
1937                 } else {
1938                     return Err(err);
1939                 }
1940             }
1941             (Vec::new(), None)
1942         };
1943         attrs.extend(inner_attrs);
1944         Ok(body)
1945     }
1946
1947     /// Is the current token the start of an `FnHeader` / not a valid parse?
1948     ///
1949     /// `check_pub` adds additional `pub` to the checks in case users place it
1950     /// wrongly, can be used to ensure `pub` never comes after `default`.
1951     pub(super) fn check_fn_front_matter(&mut self, check_pub: bool) -> bool {
1952         // We use an over-approximation here.
1953         // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
1954         // `pub` is added in case users got confused with the ordering like `async pub fn`,
1955         // only if it wasn't preceeded by `default` as `default pub` is invalid.
1956         let quals: &[Symbol] = if check_pub {
1957             &[kw::Pub, kw::Const, kw::Async, kw::Unsafe, kw::Extern]
1958         } else {
1959             &[kw::Const, kw::Async, kw::Unsafe, kw::Extern]
1960         };
1961         self.check_keyword(kw::Fn) // Definitely an `fn`.
1962             // `$qual fn` or `$qual $qual`:
1963             || quals.iter().any(|&kw| self.check_keyword(kw))
1964                 && self.look_ahead(1, |t| {
1965                     // `$qual fn`, e.g. `const fn` or `async fn`.
1966                     t.is_keyword(kw::Fn)
1967                     // Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
1968                     || t.is_non_raw_ident_where(|i| quals.contains(&i.name)
1969                         // Rule out 2015 `const async: T = val`.
1970                         && i.is_reserved()
1971                         // Rule out unsafe extern block.
1972                         && !self.is_unsafe_foreign_mod())
1973                 })
1974             // `extern ABI fn`
1975             || self.check_keyword(kw::Extern)
1976                 && self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
1977                 && self.look_ahead(2, |t| t.is_keyword(kw::Fn))
1978     }
1979
1980     /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
1981     /// up to and including the `fn` keyword. The formal grammar is:
1982     ///
1983     /// ```text
1984     /// Extern = "extern" StringLit? ;
1985     /// FnQual = "const"? "async"? "unsafe"? Extern? ;
1986     /// FnFrontMatter = FnQual "fn" ;
1987     /// ```
1988     ///
1989     /// `vis` represents the visibility that was already parsed, if any. Use
1990     /// `Visibility::Inherited` when no visibility is known.
1991     pub(super) fn parse_fn_front_matter(&mut self, orig_vis: &Visibility) -> PResult<'a, FnHeader> {
1992         let sp_start = self.token.span;
1993         let constness = self.parse_constness();
1994
1995         let async_start_sp = self.token.span;
1996         let asyncness = self.parse_asyncness();
1997
1998         let unsafe_start_sp = self.token.span;
1999         let unsafety = self.parse_unsafety();
2000
2001         let ext_start_sp = self.token.span;
2002         let ext = self.parse_extern();
2003
2004         if let Async::Yes { span, .. } = asyncness {
2005             self.ban_async_in_2015(span);
2006         }
2007
2008         if !self.eat_keyword(kw::Fn) {
2009             // It is possible for `expect_one_of` to recover given the contents of
2010             // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
2011             // account for this.
2012             match self.expect_one_of(&[], &[]) {
2013                 Ok(true) => {}
2014                 Ok(false) => unreachable!(),
2015                 Err(mut err) => {
2016                     // Qualifier keywords ordering check
2017                     enum WrongKw {
2018                         Duplicated(Span),
2019                         Misplaced(Span),
2020                     }
2021
2022                     // This will allow the machine fix to directly place the keyword in the correct place or to indicate
2023                     // that the keyword is already present and the second instance should be removed.
2024                     let wrong_kw = if self.check_keyword(kw::Const) {
2025                         match constness {
2026                             Const::Yes(sp) => Some(WrongKw::Duplicated(sp)),
2027                             Const::No => Some(WrongKw::Misplaced(async_start_sp)),
2028                         }
2029                     } else if self.check_keyword(kw::Async) {
2030                         match asyncness {
2031                             Async::Yes { span, .. } => Some(WrongKw::Duplicated(span)),
2032                             Async::No => Some(WrongKw::Misplaced(unsafe_start_sp)),
2033                         }
2034                     } else if self.check_keyword(kw::Unsafe) {
2035                         match unsafety {
2036                             Unsafe::Yes(sp) => Some(WrongKw::Duplicated(sp)),
2037                             Unsafe::No => Some(WrongKw::Misplaced(ext_start_sp)),
2038                         }
2039                     } else {
2040                         None
2041                     };
2042
2043                     // The keyword is already present, suggest removal of the second instance
2044                     if let Some(WrongKw::Duplicated(original_sp)) = wrong_kw {
2045                         let original_kw = self
2046                             .span_to_snippet(original_sp)
2047                             .expect("Span extracted directly from keyword should always work");
2048
2049                         err.span_suggestion(
2050                             self.token.uninterpolated_span(),
2051                             &format!("`{}` already used earlier, remove this one", original_kw),
2052                             "".to_string(),
2053                             Applicability::MachineApplicable,
2054                         )
2055                         .span_note(original_sp, &format!("`{}` first seen here", original_kw));
2056                     }
2057                     // The keyword has not been seen yet, suggest correct placement in the function front matter
2058                     else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
2059                         let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
2060                         if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
2061                             let misplaced_qual_sp = self.token.uninterpolated_span();
2062                             let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
2063
2064                             err.span_suggestion(
2065                                     correct_pos_sp.to(misplaced_qual_sp),
2066                                     &format!("`{}` must come before `{}`", misplaced_qual, current_qual),
2067                                     format!("{} {}", misplaced_qual, current_qual),
2068                                     Applicability::MachineApplicable,
2069                                 ).note("keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`");
2070                         }
2071                     }
2072                     // Recover incorrect visibility order such as `async pub`
2073                     else if self.check_keyword(kw::Pub) {
2074                         let sp = sp_start.to(self.prev_token.span);
2075                         if let Ok(snippet) = self.span_to_snippet(sp) {
2076                             let current_vis = match self.parse_visibility(FollowedByType::No) {
2077                                 Ok(v) => v,
2078                                 Err(d) => {
2079                                     d.cancel();
2080                                     return Err(err);
2081                                 }
2082                             };
2083                             let vs = pprust::vis_to_string(&current_vis);
2084                             let vs = vs.trim_end();
2085
2086                             // There was no explicit visibility
2087                             if matches!(orig_vis.kind, VisibilityKind::Inherited) {
2088                                 err.span_suggestion(
2089                                     sp_start.to(self.prev_token.span),
2090                                     &format!("visibility `{}` must come before `{}`", vs, snippet),
2091                                     format!("{} {}", vs, snippet),
2092                                     Applicability::MachineApplicable,
2093                                 );
2094                             }
2095                             // There was an explicit visibility
2096                             else {
2097                                 err.span_suggestion(
2098                                     current_vis.span,
2099                                     "there is already a visibility modifier, remove one",
2100                                     "".to_string(),
2101                                     Applicability::MachineApplicable,
2102                                 )
2103                                 .span_note(orig_vis.span, "explicit visibility first seen here");
2104                             }
2105                         }
2106                     }
2107                     return Err(err);
2108                 }
2109             }
2110         }
2111
2112         Ok(FnHeader { constness, unsafety, asyncness, ext })
2113     }
2114
2115     /// We are parsing `async fn`. If we are on Rust 2015, emit an error.
2116     fn ban_async_in_2015(&self, span: Span) {
2117         if span.rust_2015() {
2118             let diag = self.diagnostic();
2119             struct_span_err!(diag, span, E0670, "`async fn` is not permitted in Rust 2015")
2120                 .span_label(span, "to use `async fn`, switch to Rust 2018 or later")
2121                 .help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION))
2122                 .note("for more on editions, read https://doc.rust-lang.org/edition-guide")
2123                 .emit();
2124         }
2125     }
2126
2127     /// Parses the parameter list and result type of a function declaration.
2128     pub(super) fn parse_fn_decl(
2129         &mut self,
2130         req_name: ReqName,
2131         ret_allow_plus: AllowPlus,
2132         recover_return_sign: RecoverReturnSign,
2133     ) -> PResult<'a, P<FnDecl>> {
2134         Ok(P(FnDecl {
2135             inputs: self.parse_fn_params(req_name)?,
2136             output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
2137         }))
2138     }
2139
2140     /// Parses the parameter list of a function, including the `(` and `)` delimiters.
2141     fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
2142         let mut first_param = true;
2143         // Parse the arguments, starting out with `self` being allowed...
2144         let (mut params, _) = self.parse_paren_comma_seq(|p| {
2145             let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
2146                 e.emit();
2147                 let lo = p.prev_token.span;
2148                 // Skip every token until next possible arg or end.
2149                 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
2150                 // Create a placeholder argument for proper arg count (issue #34264).
2151                 Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span))))
2152             });
2153             // ...now that we've parsed the first argument, `self` is no longer allowed.
2154             first_param = false;
2155             param
2156         })?;
2157         // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
2158         self.deduplicate_recovered_params_names(&mut params);
2159         Ok(params)
2160     }
2161
2162     /// Parses a single function parameter.
2163     ///
2164     /// - `self` is syntactically allowed when `first_param` holds.
2165     fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
2166         let lo = self.token.span;
2167         let attrs = self.parse_outer_attributes()?;
2168         self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
2169             // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
2170             if let Some(mut param) = this.parse_self_param()? {
2171                 param.attrs = attrs.into();
2172                 let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
2173                 return Ok((res?, TrailingToken::None));
2174             }
2175
2176             let is_name_required = match this.token.kind {
2177                 token::DotDotDot => false,
2178                 _ => req_name(this.token.span.edition()),
2179             };
2180             let (pat, ty) = if is_name_required || this.is_named_param() {
2181                 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
2182
2183                 let (pat, colon) = this.parse_fn_param_pat_colon()?;
2184                 if !colon {
2185                     let mut err = this.unexpected::<()>().unwrap_err();
2186                     return if let Some(ident) =
2187                         this.parameter_without_type(&mut err, pat, is_name_required, first_param)
2188                     {
2189                         err.emit();
2190                         Ok((dummy_arg(ident), TrailingToken::None))
2191                     } else {
2192                         Err(err)
2193                     };
2194                 }
2195
2196                 this.eat_incorrect_doc_comment_for_param_type();
2197                 (pat, this.parse_ty_for_param()?)
2198             } else {
2199                 debug!("parse_param_general ident_to_pat");
2200                 let parser_snapshot_before_ty = this.clone();
2201                 this.eat_incorrect_doc_comment_for_param_type();
2202                 let mut ty = this.parse_ty_for_param();
2203                 if ty.is_ok()
2204                     && this.token != token::Comma
2205                     && this.token != token::CloseDelim(token::Paren)
2206                 {
2207                     // This wasn't actually a type, but a pattern looking like a type,
2208                     // so we are going to rollback and re-parse for recovery.
2209                     ty = this.unexpected();
2210                 }
2211                 match ty {
2212                     Ok(ty) => {
2213                         let ident = Ident::new(kw::Empty, this.prev_token.span);
2214                         let bm = BindingMode::ByValue(Mutability::Not);
2215                         let pat = this.mk_pat_ident(ty.span, bm, ident);
2216                         (pat, ty)
2217                     }
2218                     // If this is a C-variadic argument and we hit an error, return the error.
2219                     Err(err) if this.token == token::DotDotDot => return Err(err),
2220                     // Recover from attempting to parse the argument as a type without pattern.
2221                     Err(err) => {
2222                         err.cancel();
2223                         *this = parser_snapshot_before_ty;
2224                         this.recover_arg_parse()?
2225                     }
2226                 }
2227             };
2228
2229             let span = lo.until(this.token.span);
2230
2231             Ok((
2232                 Param {
2233                     attrs: attrs.into(),
2234                     id: ast::DUMMY_NODE_ID,
2235                     is_placeholder: false,
2236                     pat,
2237                     span,
2238                     ty,
2239                 },
2240                 TrailingToken::None,
2241             ))
2242         })
2243     }
2244
2245     /// Returns the parsed optional self parameter and whether a self shortcut was used.
2246     fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
2247         // Extract an identifier *after* having confirmed that the token is one.
2248         let expect_self_ident = |this: &mut Self| match this.token.ident() {
2249             Some((ident, false)) => {
2250                 this.bump();
2251                 ident
2252             }
2253             _ => unreachable!(),
2254         };
2255         // Is `self` `n` tokens ahead?
2256         let is_isolated_self = |this: &Self, n| {
2257             this.is_keyword_ahead(n, &[kw::SelfLower])
2258                 && this.look_ahead(n + 1, |t| t != &token::ModSep)
2259         };
2260         // Is `mut self` `n` tokens ahead?
2261         let is_isolated_mut_self =
2262             |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
2263         // Parse `self` or `self: TYPE`. We already know the current token is `self`.
2264         let parse_self_possibly_typed = |this: &mut Self, m| {
2265             let eself_ident = expect_self_ident(this);
2266             let eself_hi = this.prev_token.span;
2267             let eself = if this.eat(&token::Colon) {
2268                 SelfKind::Explicit(this.parse_ty()?, m)
2269             } else {
2270                 SelfKind::Value(m)
2271             };
2272             Ok((eself, eself_ident, eself_hi))
2273         };
2274         // Recover for the grammar `*self`, `*const self`, and `*mut self`.
2275         let recover_self_ptr = |this: &mut Self| {
2276             let msg = "cannot pass `self` by raw pointer";
2277             let span = this.token.span;
2278             this.struct_span_err(span, msg).span_label(span, msg).emit();
2279
2280             Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
2281         };
2282
2283         // Parse optional `self` parameter of a method.
2284         // Only a limited set of initial token sequences is considered `self` parameters; anything
2285         // else is parsed as a normal function parameter list, so some lookahead is required.
2286         let eself_lo = self.token.span;
2287         let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
2288             token::BinOp(token::And) => {
2289                 let eself = if is_isolated_self(self, 1) {
2290                     // `&self`
2291                     self.bump();
2292                     SelfKind::Region(None, Mutability::Not)
2293                 } else if is_isolated_mut_self(self, 1) {
2294                     // `&mut self`
2295                     self.bump();
2296                     self.bump();
2297                     SelfKind::Region(None, Mutability::Mut)
2298                 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
2299                     // `&'lt self`
2300                     self.bump();
2301                     let lt = self.expect_lifetime();
2302                     SelfKind::Region(Some(lt), Mutability::Not)
2303                 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
2304                     // `&'lt mut self`
2305                     self.bump();
2306                     let lt = self.expect_lifetime();
2307                     self.bump();
2308                     SelfKind::Region(Some(lt), Mutability::Mut)
2309                 } else {
2310                     // `&not_self`
2311                     return Ok(None);
2312                 };
2313                 (eself, expect_self_ident(self), self.prev_token.span)
2314             }
2315             // `*self`
2316             token::BinOp(token::Star) if is_isolated_self(self, 1) => {
2317                 self.bump();
2318                 recover_self_ptr(self)?
2319             }
2320             // `*mut self` and `*const self`
2321             token::BinOp(token::Star)
2322                 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
2323             {
2324                 self.bump();
2325                 self.bump();
2326                 recover_self_ptr(self)?
2327             }
2328             // `self` and `self: TYPE`
2329             token::Ident(..) if is_isolated_self(self, 0) => {
2330                 parse_self_possibly_typed(self, Mutability::Not)?
2331             }
2332             // `mut self` and `mut self: TYPE`
2333             token::Ident(..) if is_isolated_mut_self(self, 0) => {
2334                 self.bump();
2335                 parse_self_possibly_typed(self, Mutability::Mut)?
2336             }
2337             _ => return Ok(None),
2338         };
2339
2340         let eself = source_map::respan(eself_lo.to(eself_hi), eself);
2341         Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
2342     }
2343
2344     fn is_named_param(&self) -> bool {
2345         let offset = match self.token.kind {
2346             token::Interpolated(ref nt) => match **nt {
2347                 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2348                 _ => 0,
2349             },
2350             token::BinOp(token::And) | token::AndAnd => 1,
2351             _ if self.token.is_keyword(kw::Mut) => 1,
2352             _ => 0,
2353         };
2354
2355         self.look_ahead(offset, |t| t.is_ident())
2356             && self.look_ahead(offset + 1, |t| t == &token::Colon)
2357     }
2358
2359     fn recover_first_param(&mut self) -> &'static str {
2360         match self
2361             .parse_outer_attributes()
2362             .and_then(|_| self.parse_self_param())
2363             .map_err(|e| e.cancel())
2364         {
2365             Ok(Some(_)) => "method",
2366             _ => "function",
2367         }
2368     }
2369 }
2370
2371 enum IsMacroRulesItem {
2372     Yes { has_bang: bool },
2373     No,
2374 }