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