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