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