1 use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
2 use super::ty::{AllowPlus, RecoverQPath};
3 use super::{FollowedByType, Parser, PathStyle};
5 use crate::maybe_whole;
7 use rustc_ast_pretty::pprust;
8 use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey};
9 use rustc_span::source_map::{self, respan, Span};
10 use rustc_span::symbol::{kw, sym, Symbol};
11 use rustc_span::BytePos;
12 use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
13 use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
14 use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
15 use syntax::ast::{Const, Defaultness, Extern, IsAsync, IsAuto, PathSegment, StrLit, Unsafe};
16 use syntax::ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
17 use syntax::ast::{FnHeader, ForeignItem, ForeignItemKind, Mutability, Visibility, VisibilityKind};
20 use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
25 pub(super) type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute>>);
28 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
29 let attrs = self.parse_outer_attributes()?;
30 self.parse_item_(attrs, true, false)
33 pub(super) fn parse_item_(
35 attrs: Vec<Attribute>,
37 attributes_allowed: bool,
38 ) -> PResult<'a, Option<P<Item>>> {
39 let mut unclosed_delims = vec![];
40 let (ret, tokens) = self.collect_tokens(|this| {
41 let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
42 unclosed_delims.append(&mut this.unclosed_delims);
45 self.unclosed_delims.append(&mut unclosed_delims);
47 // Once we've parsed an item and recorded the tokens we got while
48 // parsing we may want to store `tokens` into the item we're about to
49 // return. Note, though, that we specifically didn't capture tokens
50 // related to outer attributes. The `tokens` field here may later be
51 // used with procedural macros to convert this item back into a token
52 // stream, but during expansion we may be removing attributes as we go
55 // If we've got inner attributes then the `tokens` we've got above holds
56 // these inner attributes. If an inner attribute is expanded we won't
57 // actually remove it from the token stream, so we'll just keep yielding
58 // it (bad!). To work around this case for now we just avoid recording
59 // `tokens` if we detect any inner attributes. This should help keep
60 // expansion correct, but we should fix this bug one day!
63 if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
64 i.tokens = Some(tokens);
71 /// Parses one of the items allowed by the flags.
72 fn parse_item_implementation(
74 mut attrs: Vec<Attribute>,
76 attributes_allowed: bool,
77 ) -> PResult<'a, Option<P<Item>>> {
78 maybe_whole!(self, NtItem, |item| {
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 if self.eat_keyword(kw::Crate) {
101 return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
104 let abi = self.parse_abi();
106 if self.eat_keyword(kw::Fn) {
107 // EXTERN FUNCTION ITEM
108 let fn_span = self.prev_span;
109 let header = FnHeader {
110 unsafety: Unsafe::No,
111 asyncness: respan(fn_span, IsAsync::NotAsync),
112 constness: Const::No,
113 ext: Extern::from_abi(abi),
115 return self.parse_item_fn(lo, vis, attrs, header);
116 } else if self.check(&token::OpenDelim(token::Brace)) {
117 return Ok(Some(self.parse_item_foreign_mod(lo, abi, vis, attrs)?));
123 if self.is_static_global() {
126 let m = self.parse_mutability();
127 let info = self.parse_item_const(Some(m))?;
128 return self.mk_item_with_info(attrs, lo, vis, info);
131 let constness = self.parse_constness();
132 if let Const::Yes(const_span) = constness {
133 if [kw::Fn, kw::Unsafe, kw::Extern].iter().any(|k| self.check_keyword(*k)) {
134 // CONST FUNCTION ITEM
135 let unsafety = self.parse_unsafety();
137 if self.check_keyword(kw::Extern) {
138 self.sess.gated_spans.gate(sym::const_extern_fn, lo.to(self.token.span));
140 let ext = self.parse_extern()?;
141 self.expect_keyword(kw::Fn)?;
143 let header = FnHeader {
145 asyncness: respan(const_span, IsAsync::NotAsync),
149 return self.parse_item_fn(lo, vis, attrs, header);
153 if self.eat_keyword(kw::Mut) {
154 let prev_span = self.prev_span;
155 self.struct_span_err(prev_span, "const globals cannot be mutable")
156 .span_label(prev_span, "cannot be mutable")
159 "you might want to declare a static instead",
161 Applicability::MaybeIncorrect,
166 let info = self.parse_item_const(None)?;
167 return self.mk_item_with_info(attrs, lo, vis, info);
170 // Parses `async unsafe? fn`.
171 if self.check_keyword(kw::Async) {
172 let async_span = self.token.span;
173 if self.is_keyword_ahead(1, &[kw::Fn]) || self.is_keyword_ahead(2, &[kw::Fn]) {
174 // ASYNC FUNCTION ITEM
175 self.bump(); // `async`
176 let unsafety = self.parse_unsafety(); // `unsafe`?
177 self.expect_keyword(kw::Fn)?; // `fn`
178 let asyncness = respan(
181 closure_id: DUMMY_NODE_ID,
182 return_impl_trait_id: DUMMY_NODE_ID,
185 self.ban_async_in_2015(async_span);
187 FnHeader { unsafety, asyncness, constness: Const::No, ext: Extern::None };
188 return self.parse_item_fn(lo, vis, attrs, header);
192 if self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) {
194 let unsafety = self.parse_unsafety();
195 let info = self.parse_item_trait(lo, unsafety)?;
196 return self.mk_item_with_info(attrs, lo, vis, info);
199 if self.check_keyword(kw::Impl)
200 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
201 || self.check_keyword(kw::Default) && self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
204 let defaultness = self.parse_defaultness();
205 let unsafety = self.parse_unsafety();
206 self.expect_keyword(kw::Impl)?;
207 let info = self.parse_item_impl(unsafety, defaultness)?;
208 return self.mk_item_with_info(attrs, lo, vis, info);
211 if self.check_keyword(kw::Fn) {
214 let fn_span = self.prev_span;
215 let header = FnHeader {
216 unsafety: Unsafe::No,
217 asyncness: respan(fn_span, IsAsync::NotAsync),
218 constness: Const::No,
221 return self.parse_item_fn(lo, vis, attrs, header);
224 if self.check_keyword(kw::Unsafe)
225 && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace))
227 // UNSAFE FUNCTION ITEM
228 let unsafety = self.parse_unsafety();
229 // `{` is also expected after `unsafe`; in case of error, include it in the diagnostic.
230 self.check(&token::OpenDelim(token::Brace));
231 let ext = self.parse_extern()?;
232 self.expect_keyword(kw::Fn)?;
233 let fn_span = self.prev_span;
234 let header = FnHeader {
236 asyncness: respan(fn_span, IsAsync::NotAsync),
237 constness: Const::No,
240 return self.parse_item_fn(lo, vis, attrs, header);
243 if self.eat_keyword(kw::Mod) {
245 let info = self.parse_item_mod(&attrs[..])?;
246 return self.mk_item_with_info(attrs, lo, vis, info);
249 if self.eat_keyword(kw::Type) {
251 let (ident, ty, generics) = self.parse_type_alias()?;
252 let kind = ItemKind::TyAlias(ty, generics);
253 return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
256 if self.eat_keyword(kw::Enum) {
258 let info = self.parse_item_enum()?;
259 return self.mk_item_with_info(attrs, lo, vis, info);
262 if self.check_keyword(kw::Trait)
263 || (self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait]))
266 let info = self.parse_item_trait(lo, Unsafe::No)?;
267 return self.mk_item_with_info(attrs, lo, vis, info);
270 if self.eat_keyword(kw::Struct) {
272 let info = self.parse_item_struct()?;
273 return self.mk_item_with_info(attrs, lo, vis, info);
276 if self.is_union_item() {
279 let info = self.parse_item_union()?;
280 return self.mk_item_with_info(attrs, lo, vis, info);
283 if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
284 return Ok(Some(macro_def));
287 // Verify whether we have encountered a struct or method definition where the user forgot to
288 // add the `struct` or `fn` keyword after writing `pub`: `pub S {}`
289 if vis.node.is_pub() && self.check_ident() && self.look_ahead(1, |t| *t != token::Not) {
290 // Space between `pub` keyword and the identifier
293 // ^^^ `sp` points here
294 let sp = self.prev_span.between(self.token.span);
295 let full_sp = self.prev_span.to(self.token.span);
296 let ident_sp = self.token.span;
297 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
298 // possible public struct definition where `struct` was forgotten
299 let ident = self.parse_ident().unwrap();
300 let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
301 let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
302 err.span_suggestion_short(
306 Applicability::MaybeIncorrect, // speculative
309 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
310 let ident = self.parse_ident().unwrap();
312 let kw_name = self.recover_first_param();
313 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
314 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
315 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
317 ("fn", kw_name, false)
318 } else if self.check(&token::OpenDelim(token::Brace)) {
320 ("fn", kw_name, false)
321 } else if self.check(&token::Colon) {
325 ("fn` or `struct", "function or struct", true)
328 let msg = format!("missing `{}` for {} definition", kw, kw_name);
329 let mut err = self.struct_span_err(sp, &msg);
331 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
333 format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
334 err.span_suggestion_short(
338 Applicability::MachineApplicable,
341 if let Ok(snippet) = self.span_to_snippet(ident_sp) {
344 "if you meant to call a macro, try",
345 format!("{}!", snippet),
346 // this is the `ambiguous` conditional branch
347 Applicability::MaybeIncorrect,
351 "if you meant to call a macro, remove the `pub` \
352 and add a trailing `!` after the identifier",
357 } else if self.look_ahead(1, |t| *t == token::Lt) {
358 let ident = self.parse_ident().unwrap();
359 self.eat_to_tokens(&[&token::Gt]);
361 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
362 ("fn", self.recover_first_param(), false)
363 } else if self.check(&token::OpenDelim(token::Brace)) {
364 ("struct", "struct", false)
366 ("fn` or `struct", "function or struct", true)
368 let msg = format!("missing `{}` for {} definition", kw, kw_name);
369 let mut err = self.struct_span_err(sp, &msg);
371 err.span_suggestion_short(
373 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
375 Applicability::MachineApplicable,
381 self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
384 pub(super) fn mk_item_with_info(
386 attrs: Vec<Attribute>,
390 ) -> PResult<'a, Option<P<Item>>> {
391 let (ident, item, extra_attrs) = info;
392 let span = lo.to(self.prev_span);
393 let attrs = Self::maybe_append(attrs, extra_attrs);
394 Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
397 fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
398 if let Some(ref mut rhs) = rhs {
404 /// This is the fall-through for parsing items.
405 fn parse_macro_use_or_failure(
407 attrs: Vec<Attribute>,
408 macros_allowed: bool,
409 attributes_allowed: bool,
411 visibility: Visibility,
412 ) -> PResult<'a, Option<P<Item>>> {
414 && self.token.is_path_start()
415 && !(self.is_async_fn() && self.token.span.rust_2015())
417 // MACRO INVOCATION ITEM
419 let prev_span = self.prev_span;
420 self.complain_if_pub_macro(&visibility.node, prev_span);
423 let path = self.parse_path(PathStyle::Mod)?;
424 self.expect(&token::Not)?;
425 let args = self.parse_mac_args()?;
426 if args.need_semicolon() && !self.eat(&token::Semi) {
427 self.report_invalid_macro_expansion_item();
430 let hi = self.prev_span;
431 let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
433 self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
434 return Ok(Some(item));
437 // FAILURE TO PARSE ITEM
438 match visibility.node {
439 VisibilityKind::Inherited => {}
440 _ => return Err(self.struct_span_err(self.prev_span, "unmatched visibility `pub`")),
443 if !attributes_allowed && !attrs.is_empty() {
444 self.expected_item_err(&attrs)?;
449 /// Emits an expected-item-after-attributes error.
450 fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
451 let message = match attrs.last() {
452 Some(&Attribute { kind: AttrKind::DocComment(_), .. }) => {
453 "expected item after doc comment"
455 _ => "expected item after attributes",
458 let mut err = self.struct_span_err(self.prev_span, message);
459 if attrs.last().unwrap().is_doc_comment() {
460 err.span_label(self.prev_span, "this doc comment doesn't document anything");
465 pub(super) fn is_async_fn(&self) -> bool {
466 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
469 /// Parses a macro invocation inside a `trait`, `impl` or `extern` block.
470 fn parse_assoc_macro_invoc(
473 vis: Option<&Visibility>,
475 ) -> PResult<'a, Option<Mac>> {
476 if self.token.is_path_start() && !(self.is_async_fn() && self.token.span.rust_2015()) {
477 let prev_span = self.prev_span;
478 let path = self.parse_path(PathStyle::Mod)?;
480 if path.segments.len() == 1 {
481 if !self.eat(&token::Not) {
482 return Err(self.missing_assoc_item_kind_err(item_kind, prev_span));
485 self.expect(&token::Not)?;
488 if let Some(vis) = vis {
489 self.complain_if_pub_macro(&vis.node, prev_span);
494 // eat a matched-delimiter token tree:
495 let args = self.parse_mac_args()?;
496 if args.need_semicolon() {
500 Ok(Some(Mac { path, args, prior_type_ascription: self.last_type_ascription }))
506 fn missing_assoc_item_kind_err(
510 ) -> DiagnosticBuilder<'a> {
511 let expected_kinds = if item_type == "extern" {
512 "missing `fn`, `type`, or `static`"
514 "missing `fn`, `type`, or `const`"
517 // Given this code `path(`, it seems like this is not
518 // setting the visibility of a macro invocation, but rather
519 // a mistyped method declaration.
520 // Create a diagnostic pointing out that `fn` is missing.
522 // x | pub path(&self) {
523 // | ^ missing `fn`, `type`, or `const`
525 // ^^ `sp` below will point to this
526 let sp = prev_span.between(self.prev_span);
528 .struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
529 err.span_label(sp, expected_kinds);
533 /// Parses an implementation item, `impl` keyword is already parsed.
535 /// impl<'a, T> TYPE { /* impl items */ }
536 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
537 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
538 /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
540 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
541 /// `impl` GENERICS `const`? `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
542 /// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
546 defaultness: Defaultness,
547 ) -> PResult<'a, ItemInfo> {
548 // First, parse generic parameters if necessary.
549 let mut generics = if self.choose_generics_over_qpath() {
550 self.parse_generics()?
552 let mut generics = Generics::default();
554 // /\ this is where `generics.span` should point when there are no type params.
555 generics.span = self.prev_span.shrink_to_hi();
559 let constness = self.parse_constness();
560 if let Const::Yes(span) = constness {
561 self.sess.gated_spans.gate(sym::const_trait_impl, span);
564 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
565 let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
567 ast::ImplPolarity::Negative
569 ast::ImplPolarity::Positive
572 // Parse both types and traits as a type, then reinterpret if necessary.
573 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
574 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
576 let span = self.prev_span.between(self.token.span);
577 self.struct_span_err(span, "missing trait in a trait impl").emit();
578 P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
583 // If `for` is missing we try to recover.
584 let has_for = self.eat_keyword(kw::For);
585 let missing_for_span = self.prev_span.between(self.token.span);
587 let ty_second = if self.token == token::DotDot {
588 // We need to report this error after `cfg` expansion for compatibility reasons
589 self.bump(); // `..`, do not add it to expected tokens
590 Some(self.mk_ty(self.prev_span, TyKind::Err))
591 } else if has_for || self.token.can_begin_type() {
592 Some(self.parse_ty()?)
597 generics.where_clause = self.parse_where_clause()?;
599 let (impl_items, attrs) = self.parse_impl_body()?;
601 let item_kind = match ty_second {
603 // impl Trait for Type
605 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
606 .span_suggestion_short(
610 Applicability::MachineApplicable,
615 let ty_first = ty_first.into_inner();
616 let path = match ty_first.kind {
617 // This notably includes paths passed through `ty` macro fragments (#46438).
618 TyKind::Path(None, path) => path,
620 self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
621 err_path(ty_first.span)
624 let trait_ref = TraitRef { path, ref_id: ty_first.id };
632 of_trait: Some(trait_ref),
652 Ok((Ident::invalid(), item_kind, Some(attrs)))
655 fn parse_impl_body(&mut self) -> PResult<'a, (Vec<P<AssocItem>>, Vec<Attribute>)> {
656 self.expect(&token::OpenDelim(token::Brace))?;
657 let attrs = self.parse_inner_attributes()?;
659 let mut impl_items = Vec::new();
660 while !self.eat(&token::CloseDelim(token::Brace)) {
661 let mut at_end = false;
662 match self.parse_impl_item(&mut at_end) {
663 Ok(impl_item) => impl_items.push(impl_item),
667 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
673 Ok((impl_items, attrs))
676 /// Parses defaultness (i.e., `default` or nothing).
677 fn parse_defaultness(&mut self) -> Defaultness {
678 // `pub` is included for better error messages
679 if self.check_keyword(kw::Default)
680 && self.is_keyword_ahead(
694 self.bump(); // `default`
701 /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
702 fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafe) -> PResult<'a, ItemInfo> {
703 // Parse optional `auto` prefix.
704 let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
706 self.expect_keyword(kw::Trait)?;
707 let ident = self.parse_ident()?;
708 let mut tps = self.parse_generics()?;
710 // Parse optional colon and supertrait bounds.
711 let had_colon = self.eat(&token::Colon);
712 let span_at_colon = self.prev_span;
714 if had_colon { self.parse_generic_bounds(Some(self.prev_span))? } else { Vec::new() };
716 let span_before_eq = self.prev_span;
717 if self.eat(&token::Eq) {
718 // It's a trait alias.
720 let span = span_at_colon.to(span_before_eq);
721 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
724 let bounds = self.parse_generic_bounds(None)?;
725 tps.where_clause = self.parse_where_clause()?;
728 let whole_span = lo.to(self.prev_span);
729 if let IsAuto::Yes = is_auto {
730 let msg = "trait aliases cannot be `auto`";
731 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
733 if let Unsafe::Yes(_) = unsafety {
734 let msg = "trait aliases cannot be `unsafe`";
735 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
738 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
740 Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
742 // It's a normal trait.
743 tps.where_clause = self.parse_where_clause()?;
744 self.expect(&token::OpenDelim(token::Brace))?;
745 let mut trait_items = vec![];
746 while !self.eat(&token::CloseDelim(token::Brace)) {
747 if let token::DocComment(_) = self.token.kind {
748 if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
753 "found a documentation comment that doesn't document anything",
756 "doc comments must come before what they document, maybe a \
757 comment was intended with `//`?",
764 let mut at_end = false;
765 match self.parse_trait_item(&mut at_end) {
766 Ok(item) => trait_items.push(item),
770 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
776 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None))
780 pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
781 maybe_whole!(self, NtImplItem, |x| x);
782 self.parse_assoc_item(at_end, |_| true)
785 pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
786 maybe_whole!(self, NtTraitItem, |x| x);
787 // This is somewhat dubious; We don't want to allow
788 // param names to be left off if there is a definition...
790 // We don't allow param names to be left off in edition 2018.
791 self.parse_assoc_item(at_end, |t| t.span.rust_2018())
794 /// Parses associated items.
798 is_name_required: fn(&token::Token) -> bool,
799 ) -> PResult<'a, P<AssocItem>> {
800 let attrs = self.parse_outer_attributes()?;
801 let mut unclosed_delims = vec![];
802 let (mut item, tokens) = self.collect_tokens(|this| {
803 let item = this.parse_assoc_item_(at_end, attrs, is_name_required);
804 unclosed_delims.append(&mut this.unclosed_delims);
807 self.unclosed_delims.append(&mut unclosed_delims);
808 // See `parse_item` for why this clause is here.
809 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
810 item.tokens = Some(tokens);
815 fn parse_assoc_item_(
818 mut attrs: Vec<Attribute>,
819 is_name_required: fn(&token::Token) -> bool,
820 ) -> PResult<'a, AssocItem> {
821 let lo = self.token.span;
822 let vis = self.parse_visibility(FollowedByType::No)?;
823 let defaultness = self.parse_defaultness();
824 let (name, kind, generics) = if self.eat_keyword(kw::Type) {
825 self.parse_assoc_ty()?
826 } else if self.is_const_item() {
827 self.parse_assoc_const()?
828 } else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
829 (Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
831 self.parse_assoc_fn(at_end, &mut attrs, is_name_required)?
836 span: lo.to(self.prev_span),
847 /// Returns `true` if we are looking at `const ID`
848 /// (returns `false` for things like `const fn`, etc.).
849 fn is_const_item(&self) -> bool {
850 self.token.is_keyword(kw::Const) && !self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe])
853 /// This parses the grammar:
855 /// AssocConst = "const" Ident ":" Ty "=" Expr ";"
856 fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
857 self.expect_keyword(kw::Const)?;
858 let ident = self.parse_ident()?;
859 self.expect(&token::Colon)?;
860 let ty = self.parse_ty()?;
861 let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
863 Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
866 /// Parses the following grammar:
868 /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
869 fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
870 let ident = self.parse_ident()?;
871 let mut generics = self.parse_generics()?;
873 // Parse optional colon and param bounds.
875 if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
876 generics.where_clause = self.parse_where_clause()?;
878 let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
881 Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
884 /// Parses a `UseTree`.
887 /// USE_TREE = [`::`] `*` |
888 /// [`::`] `{` USE_TREE_LIST `}` |
890 /// PATH `::` `{` USE_TREE_LIST `}` |
891 /// PATH [`as` IDENT]
893 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
894 let lo = self.token.span;
896 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
897 let kind = if self.check(&token::OpenDelim(token::Brace))
898 || self.check(&token::BinOp(token::Star))
899 || self.is_import_coupler()
901 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
902 let mod_sep_ctxt = self.token.span.ctxt();
903 if self.eat(&token::ModSep) {
906 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
909 self.parse_use_tree_glob_or_nested()?
911 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
912 prefix = self.parse_path(PathStyle::Mod)?;
914 if self.eat(&token::ModSep) {
915 self.parse_use_tree_glob_or_nested()?
917 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
921 Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
924 /// Parses `*` or `{...}`.
925 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
926 Ok(if self.eat(&token::BinOp(token::Star)) {
929 UseTreeKind::Nested(self.parse_use_tree_list()?)
933 /// Parses a `UseTreeKind::Nested(list)`.
936 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
938 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
939 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
943 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
944 if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
947 fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
948 match self.token.kind {
949 token::Ident(name, false) if name == kw::Underscore => {
950 let span = self.token.span;
952 Ok(Ident::new(name, span))
954 _ => self.parse_ident(),
958 /// Parses `extern crate` links.
963 /// extern crate foo;
964 /// extern crate bar as foo;
966 fn parse_item_extern_crate(
969 visibility: Visibility,
970 attrs: Vec<Attribute>,
971 ) -> PResult<'a, P<Item>> {
972 // Accept `extern crate name-like-this` for better diagnostics
973 let orig_name = self.parse_crate_name_with_dashes()?;
974 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
975 (rename, Some(orig_name.name))
981 let span = lo.to(self.prev_span);
982 Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
985 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
986 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
987 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
989 let mut ident = if self.token.is_keyword(kw::SelfLower) {
990 self.parse_path_segment_ident()
994 let mut idents = vec![];
995 let mut replacement = vec![];
996 let mut fixed_crate_name = false;
997 // Accept `extern crate name-like-this` for better diagnostics.
998 let dash = token::BinOp(token::BinOpToken::Minus);
999 if self.token == dash {
1000 // Do not include `-` as part of the expected tokens list.
1001 while self.eat(&dash) {
1002 fixed_crate_name = true;
1003 replacement.push((self.prev_span, "_".to_string()));
1004 idents.push(self.parse_ident()?);
1007 if fixed_crate_name {
1008 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1009 let mut fixed_name = format!("{}", ident.name);
1010 for part in idents {
1011 fixed_name.push_str(&format!("_{}", part.name));
1013 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
1015 self.struct_span_err(fixed_name_sp, error_msg)
1016 .span_label(fixed_name_sp, "dash-separated idents are not valid")
1017 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
1023 /// Parses `extern` for foreign ABIs modules.
1025 /// `extern` is expected to have been
1026 /// consumed before calling this method.
1030 /// ```ignore (only-for-syntax-highlight)
1034 fn parse_item_foreign_mod(
1037 abi: Option<StrLit>,
1038 visibility: Visibility,
1039 mut attrs: Vec<Attribute>,
1040 ) -> PResult<'a, P<Item>> {
1041 self.expect(&token::OpenDelim(token::Brace))?;
1043 attrs.extend(self.parse_inner_attributes()?);
1045 let mut foreign_items = vec![];
1046 while !self.eat(&token::CloseDelim(token::Brace)) {
1047 foreign_items.push(self.parse_foreign_item()?);
1050 let prev_span = self.prev_span;
1051 let m = ast::ForeignMod { abi, items: foreign_items };
1052 let invalid = Ident::invalid();
1053 Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs))
1056 /// Parses a foreign item.
1057 pub fn parse_foreign_item(&mut self) -> PResult<'a, P<ForeignItem>> {
1058 maybe_whole!(self, NtForeignItem, |ni| ni);
1060 let attrs = self.parse_outer_attributes()?;
1061 let lo = self.token.span;
1062 let visibility = self.parse_visibility(FollowedByType::No)?;
1064 // FOREIGN TYPE ITEM
1065 if self.check_keyword(kw::Type) {
1066 return self.parse_item_foreign_type(visibility, lo, attrs);
1069 // FOREIGN STATIC ITEM
1070 if self.is_static_global() {
1071 self.bump(); // `static`
1072 return self.parse_item_foreign_static(visibility, lo, attrs);
1075 // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
1076 if self.is_kw_followed_by_ident(kw::Const) {
1077 self.bump(); // `const`
1078 self.struct_span_err(self.prev_span, "extern items cannot be `const`")
1081 "try using a static value",
1082 "static".to_owned(),
1083 Applicability::MachineApplicable,
1086 return self.parse_item_foreign_static(visibility, lo, attrs);
1089 // FOREIGN FUNCTION ITEM
1090 const MAY_INTRODUCE_FN: &[Symbol] = &[kw::Const, kw::Async, kw::Unsafe, kw::Extern, kw::Fn];
1091 if MAY_INTRODUCE_FN.iter().any(|&kw| self.check_keyword(kw)) {
1092 return self.parse_item_foreign_fn(visibility, lo, attrs);
1095 match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
1096 Some(mac) => Ok(P(ForeignItem {
1097 ident: Ident::invalid(),
1098 span: lo.to(self.prev_span),
1102 kind: ForeignItemKind::Macro(mac),
1106 if !attrs.is_empty() {
1107 self.expected_item_err(&attrs)?;
1115 /// Parses a static item from a foreign module.
1116 /// Assumes that the `static` keyword is already parsed.
1117 fn parse_item_foreign_static(
1119 vis: ast::Visibility,
1121 attrs: Vec<Attribute>,
1122 ) -> PResult<'a, P<ForeignItem>> {
1123 let mutbl = self.parse_mutability();
1124 let ident = self.parse_ident()?;
1125 self.expect(&token::Colon)?;
1126 let ty = self.parse_ty()?;
1127 let hi = self.token.span;
1128 self.expect_semi()?;
1132 kind: ForeignItemKind::Static(ty, mutbl),
1140 /// Parses a type from a foreign module.
1141 fn parse_item_foreign_type(
1143 vis: ast::Visibility,
1145 attrs: Vec<Attribute>,
1146 ) -> PResult<'a, P<ForeignItem>> {
1147 self.expect_keyword(kw::Type)?;
1149 let ident = self.parse_ident()?;
1150 let hi = self.token.span;
1151 self.expect_semi()?;
1152 Ok(P(ast::ForeignItem {
1155 kind: ForeignItemKind::Ty,
1163 fn is_static_global(&mut self) -> bool {
1164 if self.check_keyword(kw::Static) {
1165 // Check if this could be a closure.
1166 !self.look_ahead(1, |token| {
1167 if token.is_keyword(kw::Move) {
1171 token::BinOp(token::Or) | token::OrOr => true,
1180 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
1181 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1183 /// When `m` is `"const"`, `$ident` may also be `"_"`.
1184 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
1185 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1187 // Parse the type of a `const` or `static mut?` item.
1188 // That is, the `":" $ty` fragment.
1189 let ty = if self.token == token::Eq {
1190 self.recover_missing_const_type(id, m)
1192 // Not `=` so expect `":"" $ty` as usual.
1193 self.expect(&token::Colon)?;
1197 self.expect(&token::Eq)?;
1198 let e = self.parse_expr()?;
1199 self.expect_semi()?;
1200 let item = match m {
1201 Some(m) => ItemKind::Static(ty, m, e),
1202 None => ItemKind::Const(ty, e),
1204 Ok((id, item, None))
1207 /// We were supposed to parse `:` but instead, we're already at `=`.
1208 /// This means that the type is missing.
1209 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1210 // Construct the error and stash it away with the hope
1211 // that typeck will later enrich the error with a type.
1212 let kind = match m {
1213 Some(Mutability::Mut) => "static mut",
1214 Some(Mutability::Not) => "static",
1217 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1218 err.span_suggestion(
1220 "provide a type for the item",
1221 format!("{}: <type>", id),
1222 Applicability::HasPlaceholders,
1224 err.stash(id.span, StashKey::ItemNoType);
1226 // The user intended that the type be inferred,
1227 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1228 P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
1231 /// Parses the grammar:
1232 /// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
1233 fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
1234 let ident = self.parse_ident()?;
1235 let mut tps = self.parse_generics()?;
1236 tps.where_clause = self.parse_where_clause()?;
1237 self.expect(&token::Eq)?;
1238 let ty = self.parse_ty()?;
1239 self.expect_semi()?;
1240 Ok((ident, ty, tps))
1243 /// Parses an enum declaration.
1244 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1245 let id = self.parse_ident()?;
1246 let mut generics = self.parse_generics()?;
1247 generics.where_clause = self.parse_where_clause()?;
1250 self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1251 self.recover_stmt();
1255 let enum_definition =
1256 EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
1257 Ok((id, ItemKind::Enum(enum_definition, generics), None))
1260 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1261 let variant_attrs = self.parse_outer_attributes()?;
1262 let vlo = self.token.span;
1264 let vis = self.parse_visibility(FollowedByType::No)?;
1265 if !self.recover_nested_adt_item(kw::Enum)? {
1268 let ident = self.parse_ident()?;
1270 let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
1271 // Parse a struct variant.
1272 let (fields, recovered) = self.parse_record_struct_body()?;
1273 VariantData::Struct(fields, recovered)
1274 } else if self.check(&token::OpenDelim(token::Paren)) {
1275 VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1277 VariantData::Unit(DUMMY_NODE_ID)
1281 if self.eat(&token::Eq) { Some(self.parse_anon_const_expr()?) } else { None };
1283 let vr = ast::Variant {
1287 attrs: variant_attrs,
1290 span: vlo.to(self.prev_span),
1291 is_placeholder: false,
1297 /// Parses `struct Foo { ... }`.
1298 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1299 let class_name = self.parse_ident()?;
1301 let mut generics = self.parse_generics()?;
1303 // There is a special case worth noting here, as reported in issue #17904.
1304 // If we are parsing a tuple struct it is the case that the where clause
1305 // should follow the field list. Like so:
1307 // struct Foo<T>(T) where T: Copy;
1309 // If we are parsing a normal record-style struct it is the case
1310 // that the where clause comes before the body, and after the generics.
1311 // So if we look ahead and see a brace or a where-clause we begin
1312 // parsing a record style struct.
1314 // Otherwise if we look ahead and see a paren we parse a tuple-style
1317 let vdata = if self.token.is_keyword(kw::Where) {
1318 generics.where_clause = self.parse_where_clause()?;
1319 if self.eat(&token::Semi) {
1320 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1321 VariantData::Unit(DUMMY_NODE_ID)
1323 // If we see: `struct Foo<T> where T: Copy { ... }`
1324 let (fields, recovered) = self.parse_record_struct_body()?;
1325 VariantData::Struct(fields, recovered)
1327 // No `where` so: `struct Foo<T>;`
1328 } else if self.eat(&token::Semi) {
1329 VariantData::Unit(DUMMY_NODE_ID)
1330 // Record-style struct definition
1331 } else if self.token == token::OpenDelim(token::Brace) {
1332 let (fields, recovered) = self.parse_record_struct_body()?;
1333 VariantData::Struct(fields, recovered)
1334 // Tuple-style struct definition with optional where-clause.
1335 } else if self.token == token::OpenDelim(token::Paren) {
1336 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1337 generics.where_clause = self.parse_where_clause()?;
1338 self.expect_semi()?;
1341 let token_str = super::token_descr(&self.token);
1343 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1346 let mut err = self.struct_span_err(self.token.span, msg);
1347 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1351 Ok((class_name, ItemKind::Struct(vdata, generics), None))
1354 /// Parses `union Foo { ... }`.
1355 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1356 let class_name = self.parse_ident()?;
1358 let mut generics = self.parse_generics()?;
1360 let vdata = if self.token.is_keyword(kw::Where) {
1361 generics.where_clause = self.parse_where_clause()?;
1362 let (fields, recovered) = self.parse_record_struct_body()?;
1363 VariantData::Struct(fields, recovered)
1364 } else if self.token == token::OpenDelim(token::Brace) {
1365 let (fields, recovered) = self.parse_record_struct_body()?;
1366 VariantData::Struct(fields, recovered)
1368 let token_str = super::token_descr(&self.token);
1369 let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1370 let mut err = self.struct_span_err(self.token.span, msg);
1371 err.span_label(self.token.span, "expected `where` or `{` after union name");
1375 Ok((class_name, ItemKind::Union(vdata, generics), None))
1378 pub(super) fn is_union_item(&self) -> bool {
1379 self.token.is_keyword(kw::Union)
1380 && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
1383 fn parse_record_struct_body(
1385 ) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
1386 let mut fields = Vec::new();
1387 let mut recovered = false;
1388 if self.eat(&token::OpenDelim(token::Brace)) {
1389 while self.token != token::CloseDelim(token::Brace) {
1390 let field = self.parse_struct_decl_field().map_err(|e| {
1391 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1396 Ok(field) => fields.push(field),
1403 self.eat(&token::CloseDelim(token::Brace));
1405 let token_str = super::token_descr(&self.token);
1406 let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
1407 let mut err = self.struct_span_err(self.token.span, msg);
1408 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1412 Ok((fields, recovered))
1415 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
1416 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1417 // Unit like structs are handled in parse_item_struct function
1418 self.parse_paren_comma_seq(|p| {
1419 let attrs = p.parse_outer_attributes()?;
1420 let lo = p.token.span;
1421 let vis = p.parse_visibility(FollowedByType::Yes)?;
1422 let ty = p.parse_ty()?;
1424 span: lo.to(ty.span),
1430 is_placeholder: false,
1436 /// Parses an element of a struct declaration.
1437 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
1438 let attrs = self.parse_outer_attributes()?;
1439 let lo = self.token.span;
1440 let vis = self.parse_visibility(FollowedByType::No)?;
1441 self.parse_single_struct_field(lo, vis, attrs)
1444 /// Parses a structure field declaration.
1445 fn parse_single_struct_field(
1449 attrs: Vec<Attribute>,
1450 ) -> PResult<'a, StructField> {
1451 let mut seen_comma: bool = false;
1452 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1453 if self.token == token::Comma {
1456 match self.token.kind {
1460 token::CloseDelim(token::Brace) => {}
1461 token::DocComment(_) => {
1462 let previous_span = self.prev_span;
1463 let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1464 self.bump(); // consume the doc comment
1465 let comma_after_doc_seen = self.eat(&token::Comma);
1466 // `seen_comma` is always false, because we are inside doc block
1467 // condition is here to make code more readable
1468 if seen_comma == false && comma_after_doc_seen == true {
1471 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1474 if seen_comma == false {
1475 let sp = self.sess.source_map().next_point(previous_span);
1476 err.span_suggestion(
1478 "missing comma here",
1480 Applicability::MachineApplicable,
1487 let sp = self.prev_span.shrink_to_hi();
1488 let mut err = self.struct_span_err(
1490 &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1492 if self.token.is_ident() {
1493 // This is likely another field; emit the diagnostic and keep going
1494 err.span_suggestion(
1496 "try adding a comma",
1498 Applicability::MachineApplicable,
1509 /// Parses a structure field.
1510 fn parse_name_and_ty(
1514 attrs: Vec<Attribute>,
1515 ) -> PResult<'a, StructField> {
1516 let name = self.parse_ident()?;
1517 self.expect(&token::Colon)?;
1518 let ty = self.parse_ty()?;
1520 span: lo.to(self.prev_span),
1526 is_placeholder: false,
1530 pub(super) fn eat_macro_def(
1532 attrs: &[Attribute],
1535 ) -> PResult<'a, Option<P<Item>>> {
1536 let (ident, def) = if self.eat_keyword(kw::Macro) {
1537 let ident = self.parse_ident()?;
1538 let body = if self.check(&token::OpenDelim(token::Brace)) {
1539 self.parse_mac_args()?
1540 } else if self.check(&token::OpenDelim(token::Paren)) {
1541 let params = self.parse_token_tree();
1542 let pspan = params.span();
1543 let body = if self.check(&token::OpenDelim(token::Brace)) {
1544 self.parse_token_tree()
1546 return self.unexpected();
1548 let bspan = body.span();
1549 let tokens = TokenStream::new(vec![
1551 TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
1554 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1555 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1557 return self.unexpected();
1560 (ident, ast::MacroDef { body, legacy: false })
1561 } else if self.check_keyword(sym::macro_rules)
1562 && self.look_ahead(1, |t| *t == token::Not)
1563 && self.look_ahead(2, |t| t.is_ident())
1565 let prev_span = self.prev_span;
1566 self.complain_if_pub_macro(&vis.node, prev_span);
1570 let ident = self.parse_ident()?;
1571 let body = self.parse_mac_args()?;
1572 if body.need_semicolon() && !self.eat(&token::Semi) {
1573 self.report_invalid_macro_expansion_item();
1576 (ident, ast::MacroDef { body, legacy: true })
1581 let span = lo.to(self.prev_span);
1584 self.sess.gated_spans.gate(sym::decl_macro, span);
1587 Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
1590 fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
1592 VisibilityKind::Inherited => {}
1594 let mut err = if self.token.is_keyword(sym::macro_rules) {
1596 self.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
1597 err.span_suggestion(
1599 "try exporting the macro",
1600 "#[macro_export]".to_owned(),
1601 Applicability::MaybeIncorrect, // speculative
1606 self.struct_span_err(sp, "can't qualify macro invocation with `pub`");
1607 err.help("try adjusting the macro to put `pub` inside the invocation");
1615 fn report_invalid_macro_expansion_item(&self) {
1616 let has_close_delim = self
1619 .span_to_snippet(self.prev_span)
1620 .map(|s| s.ends_with(")") || s.ends_with("]"))
1623 let mut err = self.struct_span_err(
1625 "macros that expand to items must be delimited with braces or followed by a semicolon",
1628 // To avoid ICE, we shouldn't emit actual suggestions when it hasn't closing delims
1629 if has_close_delim {
1630 err.multipart_suggestion(
1631 "change the delimiters to curly braces",
1633 (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), '{'.to_string()),
1634 (self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
1636 Applicability::MaybeIncorrect,
1639 err.span_suggestion(
1641 "change the delimiters to curly braces",
1642 " { /* items */ }".to_string(),
1643 Applicability::HasPlaceholders,
1647 err.span_suggestion(
1648 self.prev_span.shrink_to_hi(),
1651 Applicability::MaybeIncorrect,
1656 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1657 /// it is, we try to parse the item and report error about nested types.
1658 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1659 if (self.token.is_keyword(kw::Enum)
1660 || self.token.is_keyword(kw::Struct)
1661 || self.token.is_keyword(kw::Union))
1662 && self.look_ahead(1, |t| t.is_ident())
1664 let kw_token = self.token.clone();
1665 let kw_str = pprust::token_to_string(&kw_token);
1666 let item = self.parse_item()?;
1668 self.struct_span_err(
1670 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1674 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1676 Applicability::MaybeIncorrect,
1679 // We successfully parsed the item but we must inform the caller about nested problem.
1691 attrs: Vec<Attribute>,
1693 P(Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None })
1697 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1698 pub(super) struct ParamCfg {
1699 /// `is_name_required` decides if, per-parameter,
1700 /// the parameter must have a pattern or just a type.
1701 pub is_name_required: fn(&token::Token) -> bool,
1704 /// Parsing of functions and methods.
1705 impl<'a> Parser<'a> {
1706 /// Parses an item-position function declaration.
1711 mut attrs: Vec<Attribute>,
1713 ) -> PResult<'a, Option<P<Item>>> {
1714 let cfg = ParamCfg { is_name_required: |_| true };
1715 let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
1716 let body = self.parse_fn_body(&mut false, &mut attrs)?;
1717 let kind = ItemKind::Fn(FnSig { decl, header }, generics, body);
1718 self.mk_item_with_info(attrs, lo, vis, (ident, kind, None))
1721 /// Parses a function declaration from a foreign module.
1722 fn parse_item_foreign_fn(
1724 vis: ast::Visibility,
1726 mut attrs: Vec<Attribute>,
1727 ) -> PResult<'a, P<ForeignItem>> {
1728 let cfg = ParamCfg { is_name_required: |_| true };
1729 let header = self.parse_fn_front_matter()?;
1730 let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
1731 let body = self.parse_fn_body(&mut false, &mut attrs)?;
1732 let kind = ForeignItemKind::Fn(FnSig { header, decl }, generics, body);
1733 let span = lo.to(self.prev_span);
1734 Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
1740 attrs: &mut Vec<Attribute>,
1741 is_name_required: fn(&token::Token) -> bool,
1742 ) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
1743 let header = self.parse_fn_front_matter()?;
1744 let (ident, decl, generics) = self.parse_fn_sig(&&ParamCfg { is_name_required })?;
1745 let body = self.parse_fn_body(at_end, attrs)?;
1746 Ok((ident, AssocItemKind::Fn(FnSig { header, decl }, body), generics))
1749 /// Parse the "body" of a function.
1750 /// This can either be `;` when there's no body,
1751 /// or e.g. a block when the function is a provided one.
1755 attrs: &mut Vec<Attribute>,
1756 ) -> PResult<'a, Option<P<Block>>> {
1757 let (inner_attrs, body) = match self.token.kind {
1762 token::OpenDelim(token::Brace) => {
1763 let (attrs, body) = self.parse_inner_attrs_and_block()?;
1766 token::Interpolated(ref nt) => match **nt {
1767 token::NtBlock(..) => {
1768 let (attrs, body) = self.parse_inner_attrs_and_block()?;
1771 _ => return self.expected_semi_or_open_brace(),
1773 _ => return self.expected_semi_or_open_brace(),
1775 attrs.extend(inner_attrs);
1780 /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
1781 /// up to and including the `fn` keyword. The formal grammar is:
1784 /// Extern = "extern" StringLit ;
1785 /// FnQual = "const"? "async"? "unsafe"? Extern? ;
1786 /// FnFrontMatter = FnQual? "fn" ;
1788 fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1789 let constness = self.parse_constness();
1790 let asyncness = self.parse_asyncness();
1791 if let IsAsync::Async { .. } = asyncness {
1792 self.ban_async_in_2015(self.prev_span);
1794 let asyncness = respan(self.prev_span, asyncness);
1795 let unsafety = self.parse_unsafety();
1796 let (constness, unsafety, ext) = if let Const::Yes(_) = constness {
1797 (constness, unsafety, Extern::None)
1799 let ext = self.parse_extern()?;
1800 (Const::No, unsafety, ext)
1802 if !self.eat_keyword(kw::Fn) {
1803 // It is possible for `expect_one_of` to recover given the contents of
1804 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1805 // account for this.
1806 if !self.expect_one_of(&[], &[])? {
1810 Ok(FnHeader { constness, unsafety, asyncness, ext })
1813 /// Parse the "signature", including the identifier, parameters, and generics of a function.
1814 fn parse_fn_sig(&mut self, cfg: &ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
1815 let ident = self.parse_ident()?;
1816 let mut generics = self.parse_generics()?;
1817 let decl = self.parse_fn_decl(cfg, AllowPlus::Yes)?;
1818 generics.where_clause = self.parse_where_clause()?;
1819 Ok((ident, decl, generics))
1822 /// Parses the parameter list and result type of a function declaration.
1823 pub(super) fn parse_fn_decl(
1826 ret_allow_plus: AllowPlus,
1827 ) -> PResult<'a, P<FnDecl>> {
1829 inputs: self.parse_fn_params(cfg)?,
1830 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
1834 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1835 fn parse_fn_params(&mut self, cfg: &ParamCfg) -> PResult<'a, Vec<Param>> {
1836 let mut first_param = true;
1837 // Parse the arguments, starting out with `self` being allowed...
1838 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1839 let param = p.parse_param_general(&cfg, first_param).or_else(|mut e| {
1841 let lo = p.prev_span;
1842 // Skip every token until next possible arg or end.
1843 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1844 // Create a placeholder argument for proper arg count (issue #34264).
1845 Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
1847 // ...now that we've parsed the first argument, `self` is no longer allowed.
1848 first_param = false;
1851 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1852 self.deduplicate_recovered_params_names(&mut params);
1856 /// Parses a single function parameter.
1858 /// - `self` is syntactically allowed when `first_param` holds.
1859 fn parse_param_general(&mut self, cfg: &ParamCfg, first_param: bool) -> PResult<'a, Param> {
1860 let lo = self.token.span;
1861 let attrs = self.parse_outer_attributes()?;
1863 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1864 if let Some(mut param) = self.parse_self_param()? {
1865 param.attrs = attrs.into();
1866 return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
1869 let is_name_required = match self.token.kind {
1870 token::DotDotDot => false,
1871 _ => (cfg.is_name_required)(&self.token),
1873 let (pat, ty) = if is_name_required || self.is_named_param() {
1874 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1876 let pat = self.parse_fn_param_pat()?;
1877 if let Err(mut err) = self.expect(&token::Colon) {
1878 return if let Some(ident) =
1879 self.parameter_without_type(&mut err, pat, is_name_required, first_param)
1882 Ok(dummy_arg(ident))
1888 self.eat_incorrect_doc_comment_for_param_type();
1889 (pat, self.parse_ty_for_param()?)
1891 debug!("parse_param_general ident_to_pat");
1892 let parser_snapshot_before_ty = self.clone();
1893 self.eat_incorrect_doc_comment_for_param_type();
1894 let mut ty = self.parse_ty_for_param();
1896 && self.token != token::Comma
1897 && self.token != token::CloseDelim(token::Paren)
1899 // This wasn't actually a type, but a pattern looking like a type,
1900 // so we are going to rollback and re-parse for recovery.
1901 ty = self.unexpected();
1905 let ident = Ident::new(kw::Invalid, self.prev_span);
1906 let bm = BindingMode::ByValue(Mutability::Not);
1907 let pat = self.mk_pat_ident(ty.span, bm, ident);
1910 // If this is a C-variadic argument and we hit an error, return the error.
1911 Err(err) if self.token == token::DotDotDot => return Err(err),
1912 // Recover from attempting to parse the argument as a type without pattern.
1915 mem::replace(self, parser_snapshot_before_ty);
1916 self.recover_arg_parse()?
1921 let span = lo.to(self.token.span);
1924 attrs: attrs.into(),
1925 id: ast::DUMMY_NODE_ID,
1926 is_placeholder: false,
1933 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1934 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1935 // Extract an identifier *after* having confirmed that the token is one.
1936 let expect_self_ident = |this: &mut Self| {
1937 match this.token.kind {
1938 // Preserve hygienic context.
1939 token::Ident(name, _) => {
1940 let span = this.token.span;
1942 Ident::new(name, span)
1944 _ => unreachable!(),
1947 // Is `self` `n` tokens ahead?
1948 let is_isolated_self = |this: &Self, n| {
1949 this.is_keyword_ahead(n, &[kw::SelfLower])
1950 && this.look_ahead(n + 1, |t| t != &token::ModSep)
1952 // Is `mut self` `n` tokens ahead?
1953 let is_isolated_mut_self =
1954 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
1955 // Parse `self` or `self: TYPE`. We already know the current token is `self`.
1956 let parse_self_possibly_typed = |this: &mut Self, m| {
1957 let eself_ident = expect_self_ident(this);
1958 let eself_hi = this.prev_span;
1959 let eself = if this.eat(&token::Colon) {
1960 SelfKind::Explicit(this.parse_ty()?, m)
1964 Ok((eself, eself_ident, eself_hi))
1966 // Recover for the grammar `*self`, `*const self`, and `*mut self`.
1967 let recover_self_ptr = |this: &mut Self| {
1968 let msg = "cannot pass `self` by raw pointer";
1969 let span = this.token.span;
1970 this.struct_span_err(span, msg).span_label(span, msg).emit();
1972 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_span))
1975 // Parse optional `self` parameter of a method.
1976 // Only a limited set of initial token sequences is considered `self` parameters; anything
1977 // else is parsed as a normal function parameter list, so some lookahead is required.
1978 let eself_lo = self.token.span;
1979 let (eself, eself_ident, eself_hi) = match self.token.kind {
1980 token::BinOp(token::And) => {
1981 let eself = if is_isolated_self(self, 1) {
1984 SelfKind::Region(None, Mutability::Not)
1985 } else if is_isolated_mut_self(self, 1) {
1989 SelfKind::Region(None, Mutability::Mut)
1990 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
1993 let lt = self.expect_lifetime();
1994 SelfKind::Region(Some(lt), Mutability::Not)
1995 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
1998 let lt = self.expect_lifetime();
2000 SelfKind::Region(Some(lt), Mutability::Mut)
2005 (eself, expect_self_ident(self), self.prev_span)
2008 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
2010 recover_self_ptr(self)?
2012 // `*mut self` and `*const self`
2013 token::BinOp(token::Star)
2014 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
2018 recover_self_ptr(self)?
2020 // `self` and `self: TYPE`
2021 token::Ident(..) if is_isolated_self(self, 0) => {
2022 parse_self_possibly_typed(self, Mutability::Not)?
2024 // `mut self` and `mut self: TYPE`
2025 token::Ident(..) if is_isolated_mut_self(self, 0) => {
2027 parse_self_possibly_typed(self, Mutability::Mut)?
2029 _ => return Ok(None),
2032 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
2033 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
2036 fn is_named_param(&self) -> bool {
2037 let offset = match self.token.kind {
2038 token::Interpolated(ref nt) => match **nt {
2039 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2042 token::BinOp(token::And) | token::AndAnd => 1,
2043 _ if self.token.is_keyword(kw::Mut) => 1,
2047 self.look_ahead(offset, |t| t.is_ident())
2048 && self.look_ahead(offset + 1, |t| t == &token::Colon)
2051 fn recover_first_param(&mut self) -> &'static str {
2053 .parse_outer_attributes()
2054 .and_then(|_| self.parse_self_param())
2055 .map_err(|mut e| e.cancel())
2057 Ok(Some(_)) => "method",