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