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