1 use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
2 use super::{FollowedByType, Parser, PathStyle};
4 use crate::maybe_whole;
6 use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey};
7 use rustc_span::source_map::{self, respan, Span, Spanned};
8 use rustc_span::symbol::{kw, sym, Symbol};
9 use rustc_span::BytePos;
10 use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
11 use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
12 use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
13 use syntax::ast::{Constness, Defaultness, Extern, IsAsync, IsAuto, PathSegment, StrLit, Unsafety};
14 use syntax::ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
15 use syntax::ast::{FnHeader, ForeignItem, ForeignItemKind, Mutability, Visibility, VisibilityKind};
16 use syntax::print::pprust;
19 use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
24 pub(super) type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute>>);
27 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
28 let attrs = self.parse_outer_attributes()?;
29 self.parse_item_(attrs, true, false)
32 pub(super) fn parse_item_(
34 attrs: Vec<Attribute>,
36 attributes_allowed: bool,
37 ) -> PResult<'a, Option<P<Item>>> {
38 let mut unclosed_delims = vec![];
39 let (ret, tokens) = self.collect_tokens(|this| {
40 let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
41 unclosed_delims.append(&mut this.unclosed_delims);
44 self.unclosed_delims.append(&mut unclosed_delims);
46 // Once we've parsed an item and recorded the tokens we got while
47 // parsing we may want to store `tokens` into the item we're about to
48 // return. Note, though, that we specifically didn't capture tokens
49 // related to outer attributes. The `tokens` field here may later be
50 // used with procedural macros to convert this item back into a token
51 // stream, but during expansion we may be removing attributes as we go
54 // If we've got inner attributes then the `tokens` we've got above holds
55 // these inner attributes. If an inner attribute is expanded we won't
56 // actually remove it from the token stream, so we'll just keep yielding
57 // it (bad!). To work around this case for now we just avoid recording
58 // `tokens` if we detect any inner attributes. This should help keep
59 // expansion correct, but we should fix this bug one day!
62 if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
63 i.tokens = Some(tokens);
70 /// Parses one of the items allowed by the flags.
71 fn parse_item_implementation(
73 attrs: Vec<Attribute>,
75 attributes_allowed: bool,
76 ) -> PResult<'a, Option<P<Item>>> {
77 maybe_whole!(self, NtItem, |item| {
78 let mut item = item.into_inner();
79 let mut attrs = attrs;
80 mem::swap(&mut item.attrs, &mut attrs);
81 item.attrs.extend(attrs);
85 let lo = self.token.span;
87 let vis = self.parse_visibility(FollowedByType::No)?;
89 if self.eat_keyword(kw::Use) {
91 let item_ = ItemKind::Use(P(self.parse_use_tree()?));
94 let span = lo.to(self.prev_span);
95 let item = self.mk_item(span, Ident::invalid(), item_, vis, attrs);
96 return Ok(Some(item));
99 if self.eat_keyword(kw::Extern) {
100 let extern_sp = self.prev_span;
101 if self.eat_keyword(kw::Crate) {
102 return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
105 let abi = self.parse_abi();
107 if self.eat_keyword(kw::Fn) {
108 // EXTERN FUNCTION ITEM
109 let fn_span = self.prev_span;
110 let header = FnHeader {
111 unsafety: Unsafety::Normal,
112 asyncness: respan(fn_span, IsAsync::NotAsync),
113 constness: respan(fn_span, Constness::NotConst),
114 ext: Extern::from_abi(abi),
116 return self.parse_item_fn(lo, vis, attrs, header);
117 } else if self.check(&token::OpenDelim(token::Brace)) {
118 return Ok(Some(self.parse_item_foreign_mod(lo, abi, vis, attrs, extern_sp)?));
124 if self.is_static_global() {
127 let m = self.parse_mutability();
128 let info = self.parse_item_const(Some(m))?;
129 return self.mk_item_with_info(attrs, lo, vis, info);
132 if self.eat_keyword(kw::Const) {
133 let const_span = self.prev_span;
134 if [kw::Fn, kw::Unsafe, kw::Extern].iter().any(|k| self.check_keyword(*k)) {
135 // CONST FUNCTION ITEM
136 let unsafety = self.parse_unsafety();
138 if self.check_keyword(kw::Extern) {
139 self.sess.gated_spans.gate(sym::const_extern_fn, lo.to(self.token.span));
141 let ext = self.parse_extern()?;
142 self.expect_keyword(kw::Fn)?;
144 let header = FnHeader {
146 asyncness: respan(const_span, IsAsync::NotAsync),
147 constness: respan(const_span, Constness::Const),
150 return self.parse_item_fn(lo, vis, attrs, header);
154 if self.eat_keyword(kw::Mut) {
155 let prev_span = self.prev_span;
156 self.struct_span_err(prev_span, "const globals cannot be mutable")
157 .span_label(prev_span, "cannot be mutable")
160 "you might want to declare a static instead",
162 Applicability::MaybeIncorrect,
167 let info = self.parse_item_const(None)?;
168 return self.mk_item_with_info(attrs, lo, vis, info);
171 // Parses `async unsafe? fn`.
172 if self.check_keyword(kw::Async) {
173 let async_span = self.token.span;
174 if self.is_keyword_ahead(1, &[kw::Fn]) || self.is_keyword_ahead(2, &[kw::Fn]) {
175 // ASYNC FUNCTION ITEM
176 self.bump(); // `async`
177 let unsafety = self.parse_unsafety(); // `unsafe`?
178 self.expect_keyword(kw::Fn)?; // `fn`
179 let fn_span = self.prev_span;
180 let asyncness = respan(
183 closure_id: DUMMY_NODE_ID,
184 return_impl_trait_id: DUMMY_NODE_ID,
187 self.ban_async_in_2015(async_span);
188 let header = FnHeader {
191 constness: respan(fn_span, Constness::NotConst),
194 return self.parse_item_fn(lo, vis, attrs, header);
198 if self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) {
200 self.bump(); // `unsafe`
201 let info = self.parse_item_trait(lo, Unsafety::Unsafe)?;
202 return self.mk_item_with_info(attrs, lo, vis, info);
205 if self.check_keyword(kw::Impl)
206 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
207 || self.check_keyword(kw::Default) && self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
210 let defaultness = self.parse_defaultness();
211 let unsafety = self.parse_unsafety();
212 self.expect_keyword(kw::Impl)?;
213 let info = self.parse_item_impl(unsafety, defaultness)?;
214 return self.mk_item_with_info(attrs, lo, vis, info);
217 if self.check_keyword(kw::Fn) {
220 let fn_span = self.prev_span;
221 let header = FnHeader {
222 unsafety: Unsafety::Normal,
223 asyncness: respan(fn_span, IsAsync::NotAsync),
224 constness: respan(fn_span, Constness::NotConst),
227 return self.parse_item_fn(lo, vis, attrs, header);
230 if self.check_keyword(kw::Unsafe)
231 && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace))
233 // UNSAFE FUNCTION ITEM
234 self.bump(); // `unsafe`
235 // `{` is also expected after `unsafe`; in case of error, include it in the diagnostic.
236 self.check(&token::OpenDelim(token::Brace));
237 let ext = self.parse_extern()?;
238 self.expect_keyword(kw::Fn)?;
239 let fn_span = self.prev_span;
240 let header = FnHeader {
241 unsafety: Unsafety::Unsafe,
242 asyncness: respan(fn_span, IsAsync::NotAsync),
243 constness: respan(fn_span, Constness::NotConst),
246 return self.parse_item_fn(lo, vis, attrs, header);
249 if self.eat_keyword(kw::Mod) {
251 let info = self.parse_item_mod(&attrs[..])?;
252 return self.mk_item_with_info(attrs, lo, vis, info);
255 if self.eat_keyword(kw::Type) {
257 let (ident, ty, generics) = self.parse_type_alias()?;
258 let kind = ItemKind::TyAlias(ty, generics);
259 return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
262 if self.eat_keyword(kw::Enum) {
264 let info = self.parse_item_enum()?;
265 return self.mk_item_with_info(attrs, lo, vis, info);
268 if self.check_keyword(kw::Trait)
269 || (self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait]))
272 let info = self.parse_item_trait(lo, Unsafety::Normal)?;
273 return self.mk_item_with_info(attrs, lo, vis, info);
276 if self.eat_keyword(kw::Struct) {
278 let info = self.parse_item_struct()?;
279 return self.mk_item_with_info(attrs, lo, vis, info);
282 if self.is_union_item() {
285 let info = self.parse_item_union()?;
286 return self.mk_item_with_info(attrs, lo, vis, info);
289 if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
290 return Ok(Some(macro_def));
293 // Verify whether we have encountered a struct or method definition where the user forgot to
294 // add the `struct` or `fn` keyword after writing `pub`: `pub S {}`
295 if vis.node.is_pub() && self.check_ident() && self.look_ahead(1, |t| *t != token::Not) {
296 // Space between `pub` keyword and the identifier
299 // ^^^ `sp` points here
300 let sp = self.prev_span.between(self.token.span);
301 let full_sp = self.prev_span.to(self.token.span);
302 let ident_sp = self.token.span;
303 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
304 // possible public struct definition where `struct` was forgotten
305 let ident = self.parse_ident().unwrap();
306 let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
307 let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
308 err.span_suggestion_short(
312 Applicability::MaybeIncorrect, // speculative
315 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
316 let ident = self.parse_ident().unwrap();
318 let kw_name = self.recover_first_param();
319 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
320 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
321 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
323 ("fn", kw_name, false)
324 } else if self.check(&token::OpenDelim(token::Brace)) {
326 ("fn", kw_name, false)
327 } else if self.check(&token::Colon) {
331 ("fn` or `struct", "function or struct", true)
334 let msg = format!("missing `{}` for {} definition", kw, kw_name);
335 let mut err = self.struct_span_err(sp, &msg);
337 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
339 format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
340 err.span_suggestion_short(
344 Applicability::MachineApplicable,
347 if let Ok(snippet) = self.span_to_snippet(ident_sp) {
350 "if you meant to call a macro, try",
351 format!("{}!", snippet),
352 // this is the `ambiguous` conditional branch
353 Applicability::MaybeIncorrect,
357 "if you meant to call a macro, remove the `pub` \
358 and add a trailing `!` after the identifier",
363 } else if self.look_ahead(1, |t| *t == token::Lt) {
364 let ident = self.parse_ident().unwrap();
365 self.eat_to_tokens(&[&token::Gt]);
367 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
368 ("fn", self.recover_first_param(), false)
369 } else if self.check(&token::OpenDelim(token::Brace)) {
370 ("struct", "struct", false)
372 ("fn` or `struct", "function or struct", true)
374 let msg = format!("missing `{}` for {} definition", kw, kw_name);
375 let mut err = self.struct_span_err(sp, &msg);
377 err.span_suggestion_short(
379 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
381 Applicability::MachineApplicable,
387 self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
390 pub(super) fn mk_item_with_info(
392 attrs: Vec<Attribute>,
396 ) -> PResult<'a, Option<P<Item>>> {
397 let (ident, item, extra_attrs) = info;
398 let span = lo.to(self.prev_span);
399 let attrs = Self::maybe_append(attrs, extra_attrs);
400 Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
403 fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
404 if let Some(ref mut rhs) = rhs {
410 /// This is the fall-through for parsing items.
411 fn parse_macro_use_or_failure(
413 attrs: Vec<Attribute>,
414 macros_allowed: bool,
415 attributes_allowed: bool,
417 visibility: Visibility,
418 ) -> PResult<'a, Option<P<Item>>> {
420 && self.token.is_path_start()
421 && !(self.is_async_fn() && self.token.span.rust_2015())
423 // MACRO INVOCATION ITEM
425 let prev_span = self.prev_span;
426 self.complain_if_pub_macro(&visibility.node, prev_span);
429 let path = self.parse_path(PathStyle::Mod)?;
430 self.expect(&token::Not)?;
431 let args = self.parse_mac_args()?;
432 if args.need_semicolon() && !self.eat(&token::Semi) {
433 self.report_invalid_macro_expansion_item();
436 let hi = self.prev_span;
437 let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
439 self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
440 return Ok(Some(item));
443 // FAILURE TO PARSE ITEM
444 match visibility.node {
445 VisibilityKind::Inherited => {}
446 _ => return Err(self.struct_span_err(self.prev_span, "unmatched visibility `pub`")),
449 if !attributes_allowed && !attrs.is_empty() {
450 self.expected_item_err(&attrs)?;
455 /// Emits an expected-item-after-attributes error.
456 fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
457 let message = match attrs.last() {
458 Some(&Attribute { kind: AttrKind::DocComment(_), .. }) => {
459 "expected item after doc comment"
461 _ => "expected item after attributes",
464 let mut err = self.struct_span_err(self.prev_span, message);
465 if attrs.last().unwrap().is_doc_comment() {
466 err.span_label(self.prev_span, "this doc comment doesn't document anything");
471 pub(super) fn is_async_fn(&self) -> bool {
472 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
475 /// Parses a macro invocation inside a `trait`, `impl` or `extern` block.
476 fn parse_assoc_macro_invoc(
479 vis: Option<&Visibility>,
481 ) -> PResult<'a, Option<Mac>> {
482 if self.token.is_path_start() && !(self.is_async_fn() && self.token.span.rust_2015()) {
483 let prev_span = self.prev_span;
484 let path = self.parse_path(PathStyle::Mod)?;
486 if path.segments.len() == 1 {
487 if !self.eat(&token::Not) {
488 return Err(self.missing_assoc_item_kind_err(item_kind, prev_span));
491 self.expect(&token::Not)?;
494 if let Some(vis) = vis {
495 self.complain_if_pub_macro(&vis.node, prev_span);
500 // eat a matched-delimiter token tree:
501 let args = self.parse_mac_args()?;
502 if args.need_semicolon() {
506 Ok(Some(Mac { path, args, prior_type_ascription: self.last_type_ascription }))
512 fn missing_assoc_item_kind_err(
516 ) -> DiagnosticBuilder<'a> {
517 let expected_kinds = if item_type == "extern" {
518 "missing `fn`, `type`, or `static`"
520 "missing `fn`, `type`, or `const`"
523 // Given this code `path(`, it seems like this is not
524 // setting the visibility of a macro invocation, but rather
525 // a mistyped method declaration.
526 // Create a diagnostic pointing out that `fn` is missing.
528 // x | pub path(&self) {
529 // | ^ missing `fn`, `type`, or `const`
531 // ^^ `sp` below will point to this
532 let sp = prev_span.between(self.prev_span);
534 .struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
535 err.span_label(sp, expected_kinds);
539 /// Parses an implementation item, `impl` keyword is already parsed.
541 /// impl<'a, T> TYPE { /* impl items */ }
542 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
543 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
544 /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
546 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
547 /// `impl` GENERICS `const`? `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
548 /// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
552 defaultness: Defaultness,
553 ) -> PResult<'a, ItemInfo> {
554 // First, parse generic parameters if necessary.
555 let mut generics = if self.choose_generics_over_qpath() {
556 self.parse_generics()?
558 let mut generics = Generics::default();
560 // /\ this is where `generics.span` should point when there are no type params.
561 generics.span = self.prev_span.shrink_to_hi();
565 let constness = if self.eat_keyword(kw::Const) {
566 let span = self.prev_span;
567 self.sess.gated_spans.gate(sym::const_trait_impl, span);
568 Some(respan(span, Constness::Const))
573 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
574 let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
576 ast::ImplPolarity::Negative
578 ast::ImplPolarity::Positive
581 // Parse both types and traits as a type, then reinterpret if necessary.
582 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
583 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
585 let span = self.prev_span.between(self.token.span);
586 self.struct_span_err(span, "missing trait in a trait impl").emit();
587 P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
592 // If `for` is missing we try to recover.
593 let has_for = self.eat_keyword(kw::For);
594 let missing_for_span = self.prev_span.between(self.token.span);
596 let ty_second = if self.token == token::DotDot {
597 // We need to report this error after `cfg` expansion for compatibility reasons
598 self.bump(); // `..`, do not add it to expected tokens
599 Some(self.mk_ty(self.prev_span, TyKind::Err))
600 } else if has_for || self.token.can_begin_type() {
601 Some(self.parse_ty()?)
606 generics.where_clause = self.parse_where_clause()?;
608 let (impl_items, attrs) = self.parse_impl_body()?;
610 let item_kind = match ty_second {
612 // impl Trait for Type
614 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
615 .span_suggestion_short(
619 Applicability::MachineApplicable,
624 let ty_first = ty_first.into_inner();
625 let path = match ty_first.kind {
626 // This notably includes paths passed through `ty` macro fragments (#46438).
627 TyKind::Path(None, path) => path,
629 self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
630 err_path(ty_first.span)
633 let constness = constness.map(|c| c.node);
634 let trait_ref = TraitRef { path, constness, ref_id: ty_first.id };
641 of_trait: Some(trait_ref),
647 // Reject `impl const Type {}` here
648 if let Some(Spanned { node: Constness::Const, span }) = constness {
649 self.struct_span_err(span, "`const` cannot modify an inherent impl")
650 .help("only a trait impl can be `const`")
667 Ok((Ident::invalid(), item_kind, Some(attrs)))
670 fn parse_impl_body(&mut self) -> PResult<'a, (Vec<AssocItem>, Vec<Attribute>)> {
671 self.expect(&token::OpenDelim(token::Brace))?;
672 let attrs = self.parse_inner_attributes()?;
674 let mut impl_items = Vec::new();
675 while !self.eat(&token::CloseDelim(token::Brace)) {
676 let mut at_end = false;
677 match self.parse_impl_item(&mut at_end) {
678 Ok(impl_item) => impl_items.push(impl_item),
682 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
688 Ok((impl_items, attrs))
691 /// Parses defaultness (i.e., `default` or nothing).
692 fn parse_defaultness(&mut self) -> Defaultness {
693 // `pub` is included for better error messages
694 if self.check_keyword(kw::Default)
695 && self.is_keyword_ahead(
709 self.bump(); // `default`
716 /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
717 fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
718 // Parse optional `auto` prefix.
719 let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
721 self.expect_keyword(kw::Trait)?;
722 let ident = self.parse_ident()?;
723 let mut tps = self.parse_generics()?;
725 // Parse optional colon and supertrait bounds.
726 let had_colon = self.eat(&token::Colon);
727 let span_at_colon = self.prev_span;
729 if had_colon { self.parse_generic_bounds(Some(self.prev_span))? } else { Vec::new() };
731 let span_before_eq = self.prev_span;
732 if self.eat(&token::Eq) {
733 // It's a trait alias.
735 let span = span_at_colon.to(span_before_eq);
736 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
739 let bounds = self.parse_generic_bounds(None)?;
740 tps.where_clause = self.parse_where_clause()?;
743 let whole_span = lo.to(self.prev_span);
744 if is_auto == IsAuto::Yes {
745 let msg = "trait aliases cannot be `auto`";
746 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
748 if unsafety != Unsafety::Normal {
749 let msg = "trait aliases cannot be `unsafe`";
750 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
753 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
755 Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
757 // It's a normal trait.
758 tps.where_clause = self.parse_where_clause()?;
759 self.expect(&token::OpenDelim(token::Brace))?;
760 let mut trait_items = vec![];
761 while !self.eat(&token::CloseDelim(token::Brace)) {
762 if let token::DocComment(_) = self.token.kind {
763 if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
768 "found a documentation comment that doesn't document anything",
771 "doc comments must come before what they document, maybe a \
772 comment was intended with `//`?",
779 let mut at_end = false;
780 match self.parse_trait_item(&mut at_end) {
781 Ok(item) => trait_items.push(item),
785 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
791 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None))
795 pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
796 maybe_whole!(self, NtImplItem, |x| x);
797 self.parse_assoc_item(at_end, |_| true)
800 pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
801 maybe_whole!(self, NtTraitItem, |x| x);
802 // This is somewhat dubious; We don't want to allow
803 // param names to be left off if there is a definition...
805 // We don't allow param names to be left off in edition 2018.
806 self.parse_assoc_item(at_end, |t| t.span.rust_2018())
809 /// Parses associated items.
813 is_name_required: fn(&token::Token) -> bool,
814 ) -> PResult<'a, AssocItem> {
815 let attrs = self.parse_outer_attributes()?;
816 let mut unclosed_delims = vec![];
817 let (mut item, tokens) = self.collect_tokens(|this| {
818 let item = this.parse_assoc_item_(at_end, attrs, is_name_required);
819 unclosed_delims.append(&mut this.unclosed_delims);
822 self.unclosed_delims.append(&mut unclosed_delims);
823 // See `parse_item` for why this clause is here.
824 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
825 item.tokens = Some(tokens);
830 fn parse_assoc_item_(
833 mut attrs: Vec<Attribute>,
834 is_name_required: fn(&token::Token) -> bool,
835 ) -> PResult<'a, AssocItem> {
836 let lo = self.token.span;
837 let vis = self.parse_visibility(FollowedByType::No)?;
838 let defaultness = self.parse_defaultness();
839 let (name, kind, generics) = if self.eat_keyword(kw::Type) {
840 self.parse_assoc_ty()?
841 } else if self.is_const_item() {
842 self.parse_assoc_const()?
843 } else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
844 (Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
846 self.parse_assoc_fn(at_end, &mut attrs, is_name_required)?
851 span: lo.to(self.prev_span),
862 /// Returns `true` if we are looking at `const ID`
863 /// (returns `false` for things like `const fn`, etc.).
864 fn is_const_item(&self) -> bool {
865 self.token.is_keyword(kw::Const) && !self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe])
868 /// This parses the grammar:
870 /// AssocConst = "const" Ident ":" Ty "=" Expr ";"
871 fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
872 self.expect_keyword(kw::Const)?;
873 let ident = self.parse_ident()?;
874 self.expect(&token::Colon)?;
875 let ty = self.parse_ty()?;
876 let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
878 Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
881 /// Parses the following grammar:
883 /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
884 fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
885 let ident = self.parse_ident()?;
886 let mut generics = self.parse_generics()?;
888 // Parse optional colon and param bounds.
890 if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
891 generics.where_clause = self.parse_where_clause()?;
893 let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
896 Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
899 /// Parses a `UseTree`.
902 /// USE_TREE = [`::`] `*` |
903 /// [`::`] `{` USE_TREE_LIST `}` |
905 /// PATH `::` `{` USE_TREE_LIST `}` |
906 /// PATH [`as` IDENT]
908 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
909 let lo = self.token.span;
911 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
912 let kind = if self.check(&token::OpenDelim(token::Brace))
913 || self.check(&token::BinOp(token::Star))
914 || self.is_import_coupler()
916 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
917 let mod_sep_ctxt = self.token.span.ctxt();
918 if self.eat(&token::ModSep) {
921 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
924 self.parse_use_tree_glob_or_nested()?
926 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
927 prefix = self.parse_path(PathStyle::Mod)?;
929 if self.eat(&token::ModSep) {
930 self.parse_use_tree_glob_or_nested()?
932 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
936 Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
939 /// Parses `*` or `{...}`.
940 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
941 Ok(if self.eat(&token::BinOp(token::Star)) {
944 UseTreeKind::Nested(self.parse_use_tree_list()?)
948 /// Parses a `UseTreeKind::Nested(list)`.
951 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
953 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
954 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
958 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
959 if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
962 fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
963 match self.token.kind {
964 token::Ident(name, false) if name == kw::Underscore => {
965 let span = self.token.span;
967 Ok(Ident::new(name, span))
969 _ => self.parse_ident(),
973 /// Parses `extern crate` links.
978 /// extern crate foo;
979 /// extern crate bar as foo;
981 fn parse_item_extern_crate(
984 visibility: Visibility,
985 attrs: Vec<Attribute>,
986 ) -> PResult<'a, P<Item>> {
987 // Accept `extern crate name-like-this` for better diagnostics
988 let orig_name = self.parse_crate_name_with_dashes()?;
989 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
990 (rename, Some(orig_name.name))
996 let span = lo.to(self.prev_span);
997 Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
1000 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
1001 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
1002 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
1004 let mut ident = if self.token.is_keyword(kw::SelfLower) {
1005 self.parse_path_segment_ident()
1009 let mut idents = vec![];
1010 let mut replacement = vec![];
1011 let mut fixed_crate_name = false;
1012 // Accept `extern crate name-like-this` for better diagnostics.
1013 let dash = token::BinOp(token::BinOpToken::Minus);
1014 if self.token == dash {
1015 // Do not include `-` as part of the expected tokens list.
1016 while self.eat(&dash) {
1017 fixed_crate_name = true;
1018 replacement.push((self.prev_span, "_".to_string()));
1019 idents.push(self.parse_ident()?);
1022 if fixed_crate_name {
1023 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1024 let mut fixed_name = format!("{}", ident.name);
1025 for part in idents {
1026 fixed_name.push_str(&format!("_{}", part.name));
1028 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
1030 self.struct_span_err(fixed_name_sp, error_msg)
1031 .span_label(fixed_name_sp, "dash-separated idents are not valid")
1032 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
1038 /// Parses `extern` for foreign ABIs modules.
1040 /// `extern` is expected to have been
1041 /// consumed before calling this method.
1045 /// ```ignore (only-for-syntax-highlight)
1049 fn parse_item_foreign_mod(
1052 abi: Option<StrLit>,
1053 visibility: Visibility,
1054 mut attrs: Vec<Attribute>,
1056 ) -> PResult<'a, P<Item>> {
1057 self.expect(&token::OpenDelim(token::Brace))?;
1059 attrs.extend(self.parse_inner_attributes()?);
1061 let mut foreign_items = vec![];
1062 while !self.eat(&token::CloseDelim(token::Brace)) {
1063 foreign_items.push(self.parse_foreign_item(extern_sp)?);
1066 let prev_span = self.prev_span;
1067 let m = ast::ForeignMod { abi, items: foreign_items };
1068 let invalid = Ident::invalid();
1069 Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs))
1072 /// Parses a foreign item.
1073 pub fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, ForeignItem> {
1074 maybe_whole!(self, NtForeignItem, |ni| ni);
1076 let attrs = self.parse_outer_attributes()?;
1077 let lo = self.token.span;
1078 let visibility = self.parse_visibility(FollowedByType::No)?;
1080 // FOREIGN STATIC ITEM
1081 // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
1082 if self.check_keyword(kw::Static) || self.token.is_keyword(kw::Const) {
1083 if self.token.is_keyword(kw::Const) {
1085 self.struct_span_err(self.token.span, "extern items cannot be `const`");
1087 // The user wrote 'const fn'
1088 if self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe]) {
1092 // Consume `unsafe` if present, since `extern` blocks
1093 // don't allow it. This will leave behind a plain 'fn'
1094 self.eat_keyword(kw::Unsafe);
1095 // Treat 'const fn` as a plain `fn` for error recovery purposes.
1096 // We've already emitted an error, so compilation is guaranteed
1098 return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?);
1100 err.span_suggestion(
1102 "try using a static value",
1103 "static".to_owned(),
1104 Applicability::MachineApplicable,
1108 self.bump(); // `static` or `const`
1109 return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?);
1111 // FOREIGN FUNCTION ITEM
1112 if self.check_keyword(kw::Fn) {
1113 return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?);
1115 // FOREIGN TYPE ITEM
1116 if self.check_keyword(kw::Type) {
1117 return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?);
1120 match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
1121 Some(mac) => Ok(ForeignItem {
1122 ident: Ident::invalid(),
1123 span: lo.to(self.prev_span),
1127 kind: ForeignItemKind::Macro(mac),
1131 if !attrs.is_empty() {
1132 self.expected_item_err(&attrs)?;
1140 /// Parses a static item from a foreign module.
1141 /// Assumes that the `static` keyword is already parsed.
1142 fn parse_item_foreign_static(
1144 vis: ast::Visibility,
1146 attrs: Vec<Attribute>,
1147 ) -> PResult<'a, ForeignItem> {
1148 let mutbl = self.parse_mutability();
1149 let ident = self.parse_ident()?;
1150 self.expect(&token::Colon)?;
1151 let ty = self.parse_ty()?;
1152 let hi = self.token.span;
1153 self.expect_semi()?;
1157 kind: ForeignItemKind::Static(ty, mutbl),
1165 /// Parses a type from a foreign module.
1166 fn parse_item_foreign_type(
1168 vis: ast::Visibility,
1170 attrs: Vec<Attribute>,
1171 ) -> PResult<'a, ForeignItem> {
1172 self.expect_keyword(kw::Type)?;
1174 let ident = self.parse_ident()?;
1175 let hi = self.token.span;
1176 self.expect_semi()?;
1177 Ok(ast::ForeignItem {
1180 kind: ForeignItemKind::Ty,
1188 fn is_static_global(&mut self) -> bool {
1189 if self.check_keyword(kw::Static) {
1190 // Check if this could be a closure.
1191 !self.look_ahead(1, |token| {
1192 if token.is_keyword(kw::Move) {
1196 token::BinOp(token::Or) | token::OrOr => true,
1205 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
1206 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1208 /// When `m` is `"const"`, `$ident` may also be `"_"`.
1209 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
1210 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1212 // Parse the type of a `const` or `static mut?` item.
1213 // That is, the `":" $ty` fragment.
1214 let ty = if self.token == token::Eq {
1215 self.recover_missing_const_type(id, m)
1217 // Not `=` so expect `":"" $ty` as usual.
1218 self.expect(&token::Colon)?;
1222 self.expect(&token::Eq)?;
1223 let e = self.parse_expr()?;
1224 self.expect_semi()?;
1225 let item = match m {
1226 Some(m) => ItemKind::Static(ty, m, e),
1227 None => ItemKind::Const(ty, e),
1229 Ok((id, item, None))
1232 /// We were supposed to parse `:` but instead, we're already at `=`.
1233 /// This means that the type is missing.
1234 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1235 // Construct the error and stash it away with the hope
1236 // that typeck will later enrich the error with a type.
1237 let kind = match m {
1238 Some(Mutability::Mut) => "static mut",
1239 Some(Mutability::Not) => "static",
1242 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1243 err.span_suggestion(
1245 "provide a type for the item",
1246 format!("{}: <type>", id),
1247 Applicability::HasPlaceholders,
1249 err.stash(id.span, StashKey::ItemNoType);
1251 // The user intended that the type be inferred,
1252 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1253 P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
1256 /// Parses the grammar:
1257 /// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
1258 fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
1259 let ident = self.parse_ident()?;
1260 let mut tps = self.parse_generics()?;
1261 tps.where_clause = self.parse_where_clause()?;
1262 self.expect(&token::Eq)?;
1263 let ty = self.parse_ty()?;
1264 self.expect_semi()?;
1265 Ok((ident, ty, tps))
1268 /// Parses an enum declaration.
1269 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1270 let id = self.parse_ident()?;
1271 let mut generics = self.parse_generics()?;
1272 generics.where_clause = self.parse_where_clause()?;
1275 self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1276 self.recover_stmt();
1280 let enum_definition =
1281 EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
1282 Ok((id, ItemKind::Enum(enum_definition, generics), None))
1285 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1286 let variant_attrs = self.parse_outer_attributes()?;
1287 let vlo = self.token.span;
1289 let vis = self.parse_visibility(FollowedByType::No)?;
1290 if !self.recover_nested_adt_item(kw::Enum)? {
1293 let ident = self.parse_ident()?;
1295 let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
1296 // Parse a struct variant.
1297 let (fields, recovered) = self.parse_record_struct_body()?;
1298 VariantData::Struct(fields, recovered)
1299 } else if self.check(&token::OpenDelim(token::Paren)) {
1300 VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1302 VariantData::Unit(DUMMY_NODE_ID)
1306 if self.eat(&token::Eq) { Some(self.parse_anon_const_expr()?) } else { None };
1308 let vr = ast::Variant {
1312 attrs: variant_attrs,
1315 span: vlo.to(self.prev_span),
1316 is_placeholder: false,
1322 /// Parses `struct Foo { ... }`.
1323 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1324 let class_name = self.parse_ident()?;
1326 let mut generics = self.parse_generics()?;
1328 // There is a special case worth noting here, as reported in issue #17904.
1329 // If we are parsing a tuple struct it is the case that the where clause
1330 // should follow the field list. Like so:
1332 // struct Foo<T>(T) where T: Copy;
1334 // If we are parsing a normal record-style struct it is the case
1335 // that the where clause comes before the body, and after the generics.
1336 // So if we look ahead and see a brace or a where-clause we begin
1337 // parsing a record style struct.
1339 // Otherwise if we look ahead and see a paren we parse a tuple-style
1342 let vdata = if self.token.is_keyword(kw::Where) {
1343 generics.where_clause = self.parse_where_clause()?;
1344 if self.eat(&token::Semi) {
1345 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1346 VariantData::Unit(DUMMY_NODE_ID)
1348 // If we see: `struct Foo<T> where T: Copy { ... }`
1349 let (fields, recovered) = self.parse_record_struct_body()?;
1350 VariantData::Struct(fields, recovered)
1352 // No `where` so: `struct Foo<T>;`
1353 } else if self.eat(&token::Semi) {
1354 VariantData::Unit(DUMMY_NODE_ID)
1355 // Record-style struct definition
1356 } else if self.token == token::OpenDelim(token::Brace) {
1357 let (fields, recovered) = self.parse_record_struct_body()?;
1358 VariantData::Struct(fields, recovered)
1359 // Tuple-style struct definition with optional where-clause.
1360 } else if self.token == token::OpenDelim(token::Paren) {
1361 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1362 generics.where_clause = self.parse_where_clause()?;
1363 self.expect_semi()?;
1366 let token_str = super::token_descr(&self.token);
1368 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1371 let mut err = self.struct_span_err(self.token.span, msg);
1372 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1376 Ok((class_name, ItemKind::Struct(vdata, generics), None))
1379 /// Parses `union Foo { ... }`.
1380 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1381 let class_name = self.parse_ident()?;
1383 let mut generics = self.parse_generics()?;
1385 let vdata = if self.token.is_keyword(kw::Where) {
1386 generics.where_clause = self.parse_where_clause()?;
1387 let (fields, recovered) = self.parse_record_struct_body()?;
1388 VariantData::Struct(fields, recovered)
1389 } else if self.token == token::OpenDelim(token::Brace) {
1390 let (fields, recovered) = self.parse_record_struct_body()?;
1391 VariantData::Struct(fields, recovered)
1393 let token_str = super::token_descr(&self.token);
1394 let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1395 let mut err = self.struct_span_err(self.token.span, msg);
1396 err.span_label(self.token.span, "expected `where` or `{` after union name");
1400 Ok((class_name, ItemKind::Union(vdata, generics), None))
1403 pub(super) fn is_union_item(&self) -> bool {
1404 self.token.is_keyword(kw::Union)
1405 && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
1408 fn parse_record_struct_body(
1410 ) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
1411 let mut fields = Vec::new();
1412 let mut recovered = false;
1413 if self.eat(&token::OpenDelim(token::Brace)) {
1414 while self.token != token::CloseDelim(token::Brace) {
1415 let field = self.parse_struct_decl_field().map_err(|e| {
1416 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1421 Ok(field) => fields.push(field),
1428 self.eat(&token::CloseDelim(token::Brace));
1430 let token_str = super::token_descr(&self.token);
1431 let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
1432 let mut err = self.struct_span_err(self.token.span, msg);
1433 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1437 Ok((fields, recovered))
1440 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
1441 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1442 // Unit like structs are handled in parse_item_struct function
1443 self.parse_paren_comma_seq(|p| {
1444 let attrs = p.parse_outer_attributes()?;
1445 let lo = p.token.span;
1446 let vis = p.parse_visibility(FollowedByType::Yes)?;
1447 let ty = p.parse_ty()?;
1449 span: lo.to(ty.span),
1455 is_placeholder: false,
1461 /// Parses an element of a struct declaration.
1462 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
1463 let attrs = self.parse_outer_attributes()?;
1464 let lo = self.token.span;
1465 let vis = self.parse_visibility(FollowedByType::No)?;
1466 self.parse_single_struct_field(lo, vis, attrs)
1469 /// Parses a structure field declaration.
1470 fn parse_single_struct_field(
1474 attrs: Vec<Attribute>,
1475 ) -> PResult<'a, StructField> {
1476 let mut seen_comma: bool = false;
1477 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1478 if self.token == token::Comma {
1481 match self.token.kind {
1485 token::CloseDelim(token::Brace) => {}
1486 token::DocComment(_) => {
1487 let previous_span = self.prev_span;
1488 let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1489 self.bump(); // consume the doc comment
1490 let comma_after_doc_seen = self.eat(&token::Comma);
1491 // `seen_comma` is always false, because we are inside doc block
1492 // condition is here to make code more readable
1493 if seen_comma == false && comma_after_doc_seen == true {
1496 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1499 if seen_comma == false {
1500 let sp = self.sess.source_map().next_point(previous_span);
1501 err.span_suggestion(
1503 "missing comma here",
1505 Applicability::MachineApplicable,
1512 let sp = self.prev_span.shrink_to_hi();
1513 let mut err = self.struct_span_err(
1515 &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1517 if self.token.is_ident() {
1518 // This is likely another field; emit the diagnostic and keep going
1519 err.span_suggestion(
1521 "try adding a comma",
1523 Applicability::MachineApplicable,
1534 /// Parses a structure field.
1535 fn parse_name_and_ty(
1539 attrs: Vec<Attribute>,
1540 ) -> PResult<'a, StructField> {
1541 let name = self.parse_ident()?;
1542 self.expect(&token::Colon)?;
1543 let ty = self.parse_ty()?;
1545 span: lo.to(self.prev_span),
1551 is_placeholder: false,
1555 pub(super) fn eat_macro_def(
1557 attrs: &[Attribute],
1560 ) -> PResult<'a, Option<P<Item>>> {
1561 let (ident, def) = if self.eat_keyword(kw::Macro) {
1562 let ident = self.parse_ident()?;
1563 let body = if self.check(&token::OpenDelim(token::Brace)) {
1564 self.parse_mac_args()?
1565 } else if self.check(&token::OpenDelim(token::Paren)) {
1566 let params = self.parse_token_tree();
1567 let pspan = params.span();
1568 let body = if self.check(&token::OpenDelim(token::Brace)) {
1569 self.parse_token_tree()
1571 return self.unexpected();
1573 let bspan = body.span();
1574 let tokens = TokenStream::new(vec![
1576 TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
1579 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1580 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1582 return self.unexpected();
1585 (ident, ast::MacroDef { body, legacy: false })
1586 } else if self.check_keyword(sym::macro_rules)
1587 && self.look_ahead(1, |t| *t == token::Not)
1588 && self.look_ahead(2, |t| t.is_ident())
1590 let prev_span = self.prev_span;
1591 self.complain_if_pub_macro(&vis.node, prev_span);
1595 let ident = self.parse_ident()?;
1596 let body = self.parse_mac_args()?;
1597 if body.need_semicolon() && !self.eat(&token::Semi) {
1598 self.report_invalid_macro_expansion_item();
1601 (ident, ast::MacroDef { body, legacy: true })
1606 let span = lo.to(self.prev_span);
1609 self.sess.gated_spans.gate(sym::decl_macro, span);
1612 Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
1615 fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
1617 VisibilityKind::Inherited => {}
1619 let mut err = if self.token.is_keyword(sym::macro_rules) {
1621 self.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
1622 err.span_suggestion(
1624 "try exporting the macro",
1625 "#[macro_export]".to_owned(),
1626 Applicability::MaybeIncorrect, // speculative
1631 self.struct_span_err(sp, "can't qualify macro invocation with `pub`");
1632 err.help("try adjusting the macro to put `pub` inside the invocation");
1640 fn report_invalid_macro_expansion_item(&self) {
1641 let has_close_delim = self
1644 .span_to_snippet(self.prev_span)
1645 .map(|s| s.ends_with(")") || s.ends_with("]"))
1647 let right_brace_span = if has_close_delim {
1648 // it's safe to peel off one character only when it has the close delim
1649 self.prev_span.with_lo(self.prev_span.hi() - BytePos(1))
1651 self.prev_span.shrink_to_hi()
1654 self.struct_span_err(
1656 "macros that expand to items must be delimited with braces or followed by a semicolon",
1658 .multipart_suggestion(
1659 "change the delimiters to curly braces",
1661 (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()),
1662 (right_brace_span, '}'.to_string()),
1664 Applicability::MaybeIncorrect,
1667 self.prev_span.shrink_to_hi(),
1670 Applicability::MaybeIncorrect,
1675 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1676 /// it is, we try to parse the item and report error about nested types.
1677 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1678 if (self.token.is_keyword(kw::Enum)
1679 || self.token.is_keyword(kw::Struct)
1680 || self.token.is_keyword(kw::Union))
1681 && self.look_ahead(1, |t| t.is_ident())
1683 let kw_token = self.token.clone();
1684 let kw_str = pprust::token_to_string(&kw_token);
1685 let item = self.parse_item()?;
1687 self.struct_span_err(
1689 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1693 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1695 Applicability::MaybeIncorrect,
1698 // We successfully parsed the item but we must inform the caller about nested problem.
1710 attrs: Vec<Attribute>,
1712 P(Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None })
1716 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1717 pub(super) struct ParamCfg {
1718 /// Is `self` is allowed as the first parameter?
1719 pub is_self_allowed: bool,
1720 /// `is_name_required` decides if, per-parameter,
1721 /// the parameter must have a pattern or just a type.
1722 pub is_name_required: fn(&token::Token) -> bool,
1725 /// Parsing of functions and methods.
1726 impl<'a> Parser<'a> {
1727 /// Parses an item-position function declaration.
1732 attrs: Vec<Attribute>,
1734 ) -> PResult<'a, Option<P<Item>>> {
1735 let (ident, decl, generics) =
1736 self.parse_fn_sig(ParamCfg { is_self_allowed: false, is_name_required: |_| true })?;
1737 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1738 let kind = ItemKind::Fn(FnSig { decl, header }, generics, body);
1739 self.mk_item_with_info(attrs, lo, vis, (ident, kind, Some(inner_attrs)))
1742 /// Parses a function declaration from a foreign module.
1743 fn parse_item_foreign_fn(
1745 vis: ast::Visibility,
1747 attrs: Vec<Attribute>,
1749 ) -> PResult<'a, ForeignItem> {
1750 self.expect_keyword(kw::Fn)?;
1751 let (ident, decl, generics) =
1752 self.parse_fn_sig(ParamCfg { is_self_allowed: false, is_name_required: |_| true })?;
1753 let span = lo.to(self.token.span);
1754 self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?;
1755 Ok(ast::ForeignItem {
1758 kind: ForeignItemKind::Fn(decl, generics),
1769 attrs: &mut Vec<Attribute>,
1770 is_name_required: fn(&token::Token) -> bool,
1771 ) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
1772 let header = self.parse_fn_front_matter()?;
1773 let (ident, decl, generics) =
1774 self.parse_fn_sig(ParamCfg { is_self_allowed: true, is_name_required })?;
1775 let sig = FnSig { header, decl };
1776 let body = self.parse_assoc_fn_body(at_end, attrs)?;
1777 Ok((ident, AssocItemKind::Fn(sig, body), generics))
1780 /// Parse the "body" of a method in an associated item definition.
1781 /// This can either be `;` when there's no body,
1782 /// or e.g. a block when the method is a provided one.
1783 fn parse_assoc_fn_body(
1786 attrs: &mut Vec<Attribute>,
1787 ) -> PResult<'a, Option<P<Block>>> {
1788 Ok(match self.token.kind {
1790 debug!("parse_assoc_fn_body(): parsing required method");
1795 token::OpenDelim(token::Brace) => {
1796 debug!("parse_assoc_fn_body(): parsing provided method");
1798 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1799 attrs.extend(inner_attrs.iter().cloned());
1802 token::Interpolated(ref nt) => match **nt {
1803 token::NtBlock(..) => {
1805 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1806 attrs.extend(inner_attrs.iter().cloned());
1809 _ => return self.expected_semi_or_open_brace(),
1811 _ => return self.expected_semi_or_open_brace(),
1815 /// Parses all the "front matter" for a `fn` declaration, up to
1816 /// and including the `fn` keyword:
1820 /// - `const unsafe fn`
1823 fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1824 let is_const_fn = self.eat_keyword(kw::Const);
1825 let const_span = self.prev_span;
1826 let asyncness = self.parse_asyncness();
1827 if let IsAsync::Async { .. } = asyncness {
1828 self.ban_async_in_2015(self.prev_span);
1830 let asyncness = respan(self.prev_span, asyncness);
1831 let unsafety = self.parse_unsafety();
1832 let (constness, unsafety, ext) = if is_const_fn {
1833 (respan(const_span, Constness::Const), unsafety, Extern::None)
1835 let ext = self.parse_extern()?;
1836 (respan(self.prev_span, Constness::NotConst), unsafety, ext)
1838 if !self.eat_keyword(kw::Fn) {
1839 // It is possible for `expect_one_of` to recover given the contents of
1840 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1841 // account for this.
1842 if !self.expect_one_of(&[], &[])? {
1846 Ok(FnHeader { constness, unsafety, asyncness, ext })
1849 /// Parse the "signature", including the identifier, parameters, and generics of a function.
1850 fn parse_fn_sig(&mut self, cfg: ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
1851 let ident = self.parse_ident()?;
1852 let mut generics = self.parse_generics()?;
1853 let decl = self.parse_fn_decl(cfg, true)?;
1854 generics.where_clause = self.parse_where_clause()?;
1855 Ok((ident, decl, generics))
1858 /// Parses the parameter list and result type of a function declaration.
1859 pub(super) fn parse_fn_decl(
1862 ret_allow_plus: bool,
1863 ) -> PResult<'a, P<FnDecl>> {
1865 inputs: self.parse_fn_params(cfg)?,
1866 output: self.parse_ret_ty(ret_allow_plus, true)?,
1870 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1871 fn parse_fn_params(&mut self, mut cfg: ParamCfg) -> PResult<'a, Vec<Param>> {
1872 let is_trait_item = cfg.is_self_allowed;
1873 // Parse the arguments, starting out with `self` being possibly allowed...
1874 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1875 let param = p.parse_param_general(&cfg, is_trait_item).or_else(|mut e| {
1877 let lo = p.prev_span;
1878 // Skip every token until next possible arg or end.
1879 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1880 // Create a placeholder argument for proper arg count (issue #34264).
1881 Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
1883 // ...now that we've parsed the first argument, `self` is no longer allowed.
1884 cfg.is_self_allowed = false;
1887 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1888 self.deduplicate_recovered_params_names(&mut params);
1892 /// Skips unexpected attributes and doc comments in this position and emits an appropriate
1894 /// This version of parse param doesn't necessarily require identifier names.
1895 fn parse_param_general(&mut self, cfg: &ParamCfg, is_trait_item: bool) -> PResult<'a, Param> {
1896 let lo = self.token.span;
1897 let attrs = self.parse_outer_attributes()?;
1899 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1900 if let Some(mut param) = self.parse_self_param()? {
1901 param.attrs = attrs.into();
1902 return if cfg.is_self_allowed {
1905 self.recover_bad_self_param(param, is_trait_item)
1909 let is_name_required = match self.token.kind {
1910 token::DotDotDot => false,
1911 _ => (cfg.is_name_required)(&self.token),
1913 let (pat, ty) = if is_name_required || self.is_named_param() {
1914 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1916 let pat = self.parse_fn_param_pat()?;
1917 if let Err(mut err) = self.expect(&token::Colon) {
1918 return if let Some(ident) = self.parameter_without_type(
1922 cfg.is_self_allowed,
1926 Ok(dummy_arg(ident))
1932 self.eat_incorrect_doc_comment_for_param_type();
1933 (pat, self.parse_ty_for_param()?)
1935 debug!("parse_param_general ident_to_pat");
1936 let parser_snapshot_before_ty = self.clone();
1937 self.eat_incorrect_doc_comment_for_param_type();
1938 let mut ty = self.parse_ty_for_param();
1940 && self.token != token::Comma
1941 && self.token != token::CloseDelim(token::Paren)
1943 // This wasn't actually a type, but a pattern looking like a type,
1944 // so we are going to rollback and re-parse for recovery.
1945 ty = self.unexpected();
1949 let ident = Ident::new(kw::Invalid, self.prev_span);
1950 let bm = BindingMode::ByValue(Mutability::Not);
1951 let pat = self.mk_pat_ident(ty.span, bm, ident);
1954 // If this is a C-variadic argument and we hit an error, return the error.
1955 Err(err) if self.token == token::DotDotDot => return Err(err),
1956 // Recover from attempting to parse the argument as a type without pattern.
1959 mem::replace(self, parser_snapshot_before_ty);
1960 self.recover_arg_parse()?
1965 let span = lo.to(self.token.span);
1968 attrs: attrs.into(),
1969 id: ast::DUMMY_NODE_ID,
1970 is_placeholder: false,
1977 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1979 /// See `parse_self_param_with_attrs` to collect attributes.
1980 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1981 // Extract an identifier *after* having confirmed that the token is one.
1982 let expect_self_ident = |this: &mut Self| {
1983 match this.token.kind {
1984 // Preserve hygienic context.
1985 token::Ident(name, _) => {
1986 let span = this.token.span;
1988 Ident::new(name, span)
1990 _ => unreachable!(),
1993 // Is `self` `n` tokens ahead?
1994 let is_isolated_self = |this: &Self, n| {
1995 this.is_keyword_ahead(n, &[kw::SelfLower])
1996 && this.look_ahead(n + 1, |t| t != &token::ModSep)
1998 // Is `mut self` `n` tokens ahead?
1999 let is_isolated_mut_self =
2000 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
2001 // Parse `self` or `self: TYPE`. We already know the current token is `self`.
2002 let parse_self_possibly_typed = |this: &mut Self, m| {
2003 let eself_ident = expect_self_ident(this);
2004 let eself_hi = this.prev_span;
2005 let eself = if this.eat(&token::Colon) {
2006 SelfKind::Explicit(this.parse_ty()?, m)
2010 Ok((eself, eself_ident, eself_hi))
2012 // Recover for the grammar `*self`, `*const self`, and `*mut self`.
2013 let recover_self_ptr = |this: &mut Self| {
2014 let msg = "cannot pass `self` by raw pointer";
2015 let span = this.token.span;
2016 this.struct_span_err(span, msg).span_label(span, msg).emit();
2018 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_span))
2021 // Parse optional `self` parameter of a method.
2022 // Only a limited set of initial token sequences is considered `self` parameters; anything
2023 // else is parsed as a normal function parameter list, so some lookahead is required.
2024 let eself_lo = self.token.span;
2025 let (eself, eself_ident, eself_hi) = match self.token.kind {
2026 token::BinOp(token::And) => {
2027 let eself = if is_isolated_self(self, 1) {
2030 SelfKind::Region(None, Mutability::Not)
2031 } else if is_isolated_mut_self(self, 1) {
2035 SelfKind::Region(None, Mutability::Mut)
2036 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
2039 let lt = self.expect_lifetime();
2040 SelfKind::Region(Some(lt), Mutability::Not)
2041 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
2044 let lt = self.expect_lifetime();
2046 SelfKind::Region(Some(lt), Mutability::Mut)
2051 (eself, expect_self_ident(self), self.prev_span)
2054 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
2056 recover_self_ptr(self)?
2058 // `*mut self` and `*const self`
2059 token::BinOp(token::Star)
2060 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
2064 recover_self_ptr(self)?
2066 // `self` and `self: TYPE`
2067 token::Ident(..) if is_isolated_self(self, 0) => {
2068 parse_self_possibly_typed(self, Mutability::Not)?
2070 // `mut self` and `mut self: TYPE`
2071 token::Ident(..) if is_isolated_mut_self(self, 0) => {
2073 parse_self_possibly_typed(self, Mutability::Mut)?
2075 _ => return Ok(None),
2078 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
2079 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
2082 fn is_named_param(&self) -> bool {
2083 let offset = match self.token.kind {
2084 token::Interpolated(ref nt) => match **nt {
2085 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2088 token::BinOp(token::And) | token::AndAnd => 1,
2089 _ if self.token.is_keyword(kw::Mut) => 1,
2093 self.look_ahead(offset, |t| t.is_ident())
2094 && self.look_ahead(offset + 1, |t| t == &token::Colon)
2097 fn recover_first_param(&mut self) -> &'static str {
2099 .parse_outer_attributes()
2100 .and_then(|_| self.parse_self_param())
2101 .map_err(|mut e| e.cancel())
2103 Ok(Some(_)) => "method",