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