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, 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::{Async, Const, Defaultness, IsAuto, PathSegment, StrLit, Unsafe};
15 use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
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.check_fn_front_matter() {
101 let (ident, sig, generics, body) = self.parse_fn(&mut false, &mut attrs, |_| true)?;
102 let kind = ItemKind::Fn(sig, generics, body);
103 return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
106 if self.eat_keyword(kw::Extern) {
107 if self.eat_keyword(kw::Crate) {
109 return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
112 let abi = self.parse_abi();
113 return Ok(Some(self.parse_item_foreign_mod(lo, abi, vis, attrs)?));
116 if self.is_static_global() {
119 let m = self.parse_mutability();
120 let info = self.parse_item_const(Some(m))?;
121 return self.mk_item_with_info(attrs, lo, vis, info);
124 if let Const::Yes(const_span) = self.parse_constness() {
126 if self.eat_keyword(kw::Mut) {
127 let prev_span = self.prev_span;
128 self.struct_span_err(prev_span, "const globals cannot be mutable")
129 .span_label(prev_span, "cannot be mutable")
132 "you might want to declare a static instead",
134 Applicability::MaybeIncorrect,
139 let info = self.parse_item_const(None)?;
140 return self.mk_item_with_info(attrs, lo, vis, info);
143 if self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) {
145 let unsafety = self.parse_unsafety();
146 let info = self.parse_item_trait(lo, unsafety)?;
147 return self.mk_item_with_info(attrs, lo, vis, info);
150 if self.check_keyword(kw::Impl)
151 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
152 || self.check_keyword(kw::Default) && self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
155 let defaultness = self.parse_defaultness();
156 let unsafety = self.parse_unsafety();
157 self.expect_keyword(kw::Impl)?;
158 let info = self.parse_item_impl(unsafety, defaultness)?;
159 return self.mk_item_with_info(attrs, lo, vis, info);
162 if self.eat_keyword(kw::Mod) {
164 let info = self.parse_item_mod(&attrs[..])?;
165 return self.mk_item_with_info(attrs, lo, vis, info);
168 if self.eat_keyword(kw::Type) {
170 let (ident, ty, generics) = self.parse_type_alias()?;
171 let kind = ItemKind::TyAlias(ty, generics);
172 return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
175 if self.eat_keyword(kw::Enum) {
177 let info = self.parse_item_enum()?;
178 return self.mk_item_with_info(attrs, lo, vis, info);
181 if self.check_keyword(kw::Trait)
182 || (self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait]))
185 let info = self.parse_item_trait(lo, Unsafe::No)?;
186 return self.mk_item_with_info(attrs, lo, vis, info);
189 if self.eat_keyword(kw::Struct) {
191 let info = self.parse_item_struct()?;
192 return self.mk_item_with_info(attrs, lo, vis, info);
195 if self.is_union_item() {
198 let info = self.parse_item_union()?;
199 return self.mk_item_with_info(attrs, lo, vis, info);
202 if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
203 return Ok(Some(macro_def));
206 if vis.node.is_pub() && self.check_ident() && self.look_ahead(1, |t| *t != token::Not) {
207 self.recover_missing_kw_before_item()?;
209 self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
212 /// Recover on encountering a struct or method definition where the user
213 /// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
214 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
215 // Space between `pub` keyword and the identifier
218 // ^^^ `sp` points here
219 let sp = self.prev_span.between(self.token.span);
220 let full_sp = self.prev_span.to(self.token.span);
221 let ident_sp = self.token.span;
222 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
223 // possible public struct definition where `struct` was forgotten
224 let ident = self.parse_ident().unwrap();
225 let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
226 let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
227 err.span_suggestion_short(
231 Applicability::MaybeIncorrect, // speculative
234 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
235 let ident = self.parse_ident().unwrap();
237 let kw_name = self.recover_first_param();
238 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
239 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
240 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
242 ("fn", kw_name, false)
243 } else if self.check(&token::OpenDelim(token::Brace)) {
245 ("fn", kw_name, false)
246 } else if self.check(&token::Colon) {
250 ("fn` or `struct", "function or struct", true)
253 let msg = format!("missing `{}` for {} definition", kw, kw_name);
254 let mut err = self.struct_span_err(sp, &msg);
256 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
258 format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
259 err.span_suggestion_short(
263 Applicability::MachineApplicable,
266 if let Ok(snippet) = self.span_to_snippet(ident_sp) {
269 "if you meant to call a macro, try",
270 format!("{}!", snippet),
271 // this is the `ambiguous` conditional branch
272 Applicability::MaybeIncorrect,
276 "if you meant to call a macro, remove the `pub` \
277 and add a trailing `!` after the identifier",
282 } else if self.look_ahead(1, |t| *t == token::Lt) {
283 let ident = self.parse_ident().unwrap();
284 self.eat_to_tokens(&[&token::Gt]);
286 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
287 ("fn", self.recover_first_param(), false)
288 } else if self.check(&token::OpenDelim(token::Brace)) {
289 ("struct", "struct", false)
291 ("fn` or `struct", "function or struct", true)
293 let msg = format!("missing `{}` for {} definition", kw, kw_name);
294 let mut err = self.struct_span_err(sp, &msg);
296 err.span_suggestion_short(
298 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
300 Applicability::MachineApplicable,
309 pub(super) fn mk_item_with_info(
311 attrs: Vec<Attribute>,
315 ) -> PResult<'a, Option<P<Item>>> {
316 let (ident, item, extra_attrs) = info;
317 let span = lo.to(self.prev_span);
318 let attrs = Self::maybe_append(attrs, extra_attrs);
319 Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
322 fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
323 if let Some(ref mut rhs) = rhs {
329 /// This is the fall-through for parsing items.
330 fn parse_macro_use_or_failure(
332 attrs: Vec<Attribute>,
333 macros_allowed: bool,
334 attributes_allowed: bool,
336 visibility: Visibility,
337 ) -> PResult<'a, Option<P<Item>>> {
339 && self.token.is_path_start()
340 && !(self.is_async_fn() && self.token.span.rust_2015())
342 // MACRO INVOCATION ITEM
344 let prev_span = self.prev_span;
345 self.complain_if_pub_macro(&visibility.node, prev_span);
348 let path = self.parse_path(PathStyle::Mod)?;
349 self.expect(&token::Not)?;
350 let args = self.parse_mac_args()?;
351 if args.need_semicolon() && !self.eat(&token::Semi) {
352 self.report_invalid_macro_expansion_item();
355 let hi = self.prev_span;
356 let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
358 self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
359 return Ok(Some(item));
362 // FAILURE TO PARSE ITEM
363 match visibility.node {
364 VisibilityKind::Inherited => {}
365 _ => return Err(self.struct_span_err(self.prev_span, "unmatched visibility `pub`")),
368 if !attributes_allowed && !attrs.is_empty() {
369 self.expected_item_err(&attrs)?;
374 /// Emits an expected-item-after-attributes error.
375 fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
376 let message = match attrs.last() {
377 Some(&Attribute { kind: AttrKind::DocComment(_), .. }) => {
378 "expected item after doc comment"
380 _ => "expected item after attributes",
383 let mut err = self.struct_span_err(self.prev_span, message);
384 if attrs.last().unwrap().is_doc_comment() {
385 err.span_label(self.prev_span, "this doc comment doesn't document anything");
390 pub(super) fn is_async_fn(&self) -> bool {
391 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
394 /// Parses a macro invocation inside a `trait`, `impl` or `extern` block.
395 fn parse_assoc_macro_invoc(
398 vis: Option<&Visibility>,
400 ) -> PResult<'a, Option<Mac>> {
401 if self.token.is_path_start() && !(self.is_async_fn() && self.token.span.rust_2015()) {
402 let prev_span = self.prev_span;
403 let path = self.parse_path(PathStyle::Mod)?;
405 if path.segments.len() == 1 {
406 if !self.eat(&token::Not) {
407 return Err(self.missing_assoc_item_kind_err(item_kind, prev_span));
410 self.expect(&token::Not)?;
413 if let Some(vis) = vis {
414 self.complain_if_pub_macro(&vis.node, prev_span);
419 // eat a matched-delimiter token tree:
420 let args = self.parse_mac_args()?;
421 if args.need_semicolon() {
425 Ok(Some(Mac { path, args, prior_type_ascription: self.last_type_ascription }))
431 fn missing_assoc_item_kind_err(
435 ) -> DiagnosticBuilder<'a> {
436 let expected_kinds = if item_type == "extern" {
437 "missing `fn`, `type`, or `static`"
439 "missing `fn`, `type`, or `const`"
442 // Given this code `path(`, it seems like this is not
443 // setting the visibility of a macro invocation, but rather
444 // a mistyped method declaration.
445 // Create a diagnostic pointing out that `fn` is missing.
447 // x | pub path(&self) {
448 // | ^ missing `fn`, `type`, or `const`
450 // ^^ `sp` below will point to this
451 let sp = prev_span.between(self.prev_span);
453 .struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
454 err.span_label(sp, expected_kinds);
458 /// Parses an implementation item, `impl` keyword is already parsed.
460 /// impl<'a, T> TYPE { /* impl items */ }
461 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
462 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
463 /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
465 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
466 /// `impl` GENERICS `const`? `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
467 /// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
471 defaultness: Defaultness,
472 ) -> PResult<'a, ItemInfo> {
473 // First, parse generic parameters if necessary.
474 let mut generics = if self.choose_generics_over_qpath() {
475 self.parse_generics()?
477 let mut generics = Generics::default();
479 // /\ this is where `generics.span` should point when there are no type params.
480 generics.span = self.prev_span.shrink_to_hi();
484 let constness = self.parse_constness();
485 if let Const::Yes(span) = constness {
486 self.sess.gated_spans.gate(sym::const_trait_impl, span);
489 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
490 let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
492 ast::ImplPolarity::Negative
494 ast::ImplPolarity::Positive
497 // Parse both types and traits as a type, then reinterpret if necessary.
498 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
499 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
501 let span = self.prev_span.between(self.token.span);
502 self.struct_span_err(span, "missing trait in a trait impl").emit();
503 P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
508 // If `for` is missing we try to recover.
509 let has_for = self.eat_keyword(kw::For);
510 let missing_for_span = self.prev_span.between(self.token.span);
512 let ty_second = if self.token == token::DotDot {
513 // We need to report this error after `cfg` expansion for compatibility reasons
514 self.bump(); // `..`, do not add it to expected tokens
515 Some(self.mk_ty(self.prev_span, TyKind::Err))
516 } else if has_for || self.token.can_begin_type() {
517 Some(self.parse_ty()?)
522 generics.where_clause = self.parse_where_clause()?;
524 let (impl_items, attrs) = self.parse_item_list(|p, at_end| p.parse_impl_item(at_end))?;
526 let item_kind = match ty_second {
528 // impl Trait for Type
530 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
531 .span_suggestion_short(
535 Applicability::MachineApplicable,
540 let ty_first = ty_first.into_inner();
541 let path = match ty_first.kind {
542 // This notably includes paths passed through `ty` macro fragments (#46438).
543 TyKind::Path(None, path) => path,
545 self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
546 err_path(ty_first.span)
549 let trait_ref = TraitRef { path, ref_id: ty_first.id };
557 of_trait: Some(trait_ref),
577 Ok((Ident::invalid(), item_kind, Some(attrs)))
580 fn parse_item_list<T>(
582 mut parse_item: impl FnMut(&mut Parser<'a>, &mut bool) -> PResult<'a, T>,
583 ) -> PResult<'a, (Vec<T>, Vec<Attribute>)> {
584 self.expect(&token::OpenDelim(token::Brace))?;
585 let attrs = self.parse_inner_attributes()?;
587 let mut items = Vec::new();
588 while !self.eat(&token::CloseDelim(token::Brace)) {
589 if self.recover_doc_comment_before_brace() {
592 let mut at_end = false;
593 match parse_item(self, &mut at_end) {
594 Ok(item) => items.push(item),
598 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
607 /// Recover on a doc comment before `}`.
608 fn recover_doc_comment_before_brace(&mut self) -> bool {
609 if let token::DocComment(_) = self.token.kind {
610 if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
615 "found a documentation comment that doesn't document anything",
617 .span_label(self.token.span, "this doc comment doesn't document anything")
619 "doc comments must come before what they document, maybe a \
620 comment was intended with `//`?",
630 /// Parses defaultness (i.e., `default` or nothing).
631 fn parse_defaultness(&mut self) -> Defaultness {
632 // `pub` is included for better error messages
633 if self.check_keyword(kw::Default)
634 && self.is_keyword_ahead(
648 self.bump(); // `default`
655 /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
656 fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafe) -> PResult<'a, ItemInfo> {
657 // Parse optional `auto` prefix.
658 let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
660 self.expect_keyword(kw::Trait)?;
661 let ident = self.parse_ident()?;
662 let mut tps = self.parse_generics()?;
664 // Parse optional colon and supertrait bounds.
665 let had_colon = self.eat(&token::Colon);
666 let span_at_colon = self.prev_span;
668 if had_colon { self.parse_generic_bounds(Some(self.prev_span))? } else { Vec::new() };
670 let span_before_eq = self.prev_span;
671 if self.eat(&token::Eq) {
672 // It's a trait alias.
674 let span = span_at_colon.to(span_before_eq);
675 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
678 let bounds = self.parse_generic_bounds(None)?;
679 tps.where_clause = self.parse_where_clause()?;
682 let whole_span = lo.to(self.prev_span);
683 if is_auto == IsAuto::Yes {
684 let msg = "trait aliases cannot be `auto`";
685 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
687 if let Unsafe::Yes(_) = unsafety {
688 let msg = "trait aliases cannot be `unsafe`";
689 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
692 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
694 Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
696 // It's a normal trait.
697 tps.where_clause = self.parse_where_clause()?;
698 let (items, attrs) = self.parse_item_list(|p, at_end| p.parse_trait_item(at_end))?;
699 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, items), Some(attrs)))
703 pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
704 maybe_whole!(self, NtImplItem, |x| x);
705 self.parse_assoc_item(at_end, |_| true)
708 pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
709 maybe_whole!(self, NtTraitItem, |x| x);
710 // This is somewhat dubious; We don't want to allow
711 // param names to be left off if there is a definition...
713 // We don't allow param names to be left off in edition 2018.
714 self.parse_assoc_item(at_end, |t| t.span.rust_2018())
717 /// Parses associated items.
721 req_name: fn(&token::Token) -> bool,
722 ) -> PResult<'a, P<AssocItem>> {
723 let attrs = self.parse_outer_attributes()?;
724 let mut unclosed_delims = vec![];
725 let (mut item, tokens) = self.collect_tokens(|this| {
726 let item = this.parse_assoc_item_(at_end, attrs, req_name);
727 unclosed_delims.append(&mut this.unclosed_delims);
730 self.unclosed_delims.append(&mut unclosed_delims);
731 // See `parse_item` for why this clause is here.
732 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
733 item.tokens = Some(tokens);
738 fn parse_assoc_item_(
741 mut attrs: Vec<Attribute>,
742 req_name: fn(&token::Token) -> bool,
743 ) -> PResult<'a, AssocItem> {
744 let lo = self.token.span;
745 let vis = self.parse_visibility(FollowedByType::No)?;
746 let defaultness = self.parse_defaultness();
748 let (ident, kind, generics) = if self.eat_keyword(kw::Type) {
749 self.parse_assoc_ty()?
750 } else if self.check_fn_front_matter() {
751 let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
752 (ident, AssocItemKind::Fn(sig, body), generics)
753 } else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
754 (Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
756 self.parse_assoc_const()?
759 let span = lo.to(self.prev_span);
760 let id = DUMMY_NODE_ID;
761 Ok(AssocItem { id, span, ident, attrs, vis, defaultness, generics, kind, tokens: None })
764 /// This parses the grammar:
766 /// AssocConst = "const" Ident ":" Ty "=" Expr ";"
767 fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
768 self.expect_keyword(kw::Const)?;
769 let ident = self.parse_ident()?;
770 self.expect(&token::Colon)?;
771 let ty = self.parse_ty()?;
772 let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
774 Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
777 /// Parses the following grammar:
779 /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
780 fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
781 let ident = self.parse_ident()?;
782 let mut generics = self.parse_generics()?;
784 // Parse optional colon and param bounds.
786 if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
787 generics.where_clause = self.parse_where_clause()?;
789 let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
792 Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
795 /// Parses a `UseTree`.
798 /// USE_TREE = [`::`] `*` |
799 /// [`::`] `{` USE_TREE_LIST `}` |
801 /// PATH `::` `{` USE_TREE_LIST `}` |
802 /// PATH [`as` IDENT]
804 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
805 let lo = self.token.span;
807 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
808 let kind = if self.check(&token::OpenDelim(token::Brace))
809 || self.check(&token::BinOp(token::Star))
810 || self.is_import_coupler()
812 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
813 let mod_sep_ctxt = self.token.span.ctxt();
814 if self.eat(&token::ModSep) {
817 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
820 self.parse_use_tree_glob_or_nested()?
822 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
823 prefix = self.parse_path(PathStyle::Mod)?;
825 if self.eat(&token::ModSep) {
826 self.parse_use_tree_glob_or_nested()?
828 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
832 Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
835 /// Parses `*` or `{...}`.
836 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
837 Ok(if self.eat(&token::BinOp(token::Star)) {
840 UseTreeKind::Nested(self.parse_use_tree_list()?)
844 /// Parses a `UseTreeKind::Nested(list)`.
847 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
849 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
850 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
854 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
855 if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
858 fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
859 match self.token.kind {
860 token::Ident(name, false) if name == kw::Underscore => {
861 let span = self.token.span;
863 Ok(Ident::new(name, span))
865 _ => self.parse_ident(),
869 /// Parses `extern crate` links.
874 /// extern crate foo;
875 /// extern crate bar as foo;
877 fn parse_item_extern_crate(
880 visibility: Visibility,
881 attrs: Vec<Attribute>,
882 ) -> PResult<'a, P<Item>> {
883 // Accept `extern crate name-like-this` for better diagnostics
884 let orig_name = self.parse_crate_name_with_dashes()?;
885 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
886 (rename, Some(orig_name.name))
892 let span = lo.to(self.prev_span);
893 Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
896 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
897 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
898 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
900 let mut ident = if self.token.is_keyword(kw::SelfLower) {
901 self.parse_path_segment_ident()
905 let mut idents = vec![];
906 let mut replacement = vec![];
907 let mut fixed_crate_name = false;
908 // Accept `extern crate name-like-this` for better diagnostics.
909 let dash = token::BinOp(token::BinOpToken::Minus);
910 if self.token == dash {
911 // Do not include `-` as part of the expected tokens list.
912 while self.eat(&dash) {
913 fixed_crate_name = true;
914 replacement.push((self.prev_span, "_".to_string()));
915 idents.push(self.parse_ident()?);
918 if fixed_crate_name {
919 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
920 let mut fixed_name = format!("{}", ident.name);
922 fixed_name.push_str(&format!("_{}", part.name));
924 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
926 self.struct_span_err(fixed_name_sp, error_msg)
927 .span_label(fixed_name_sp, "dash-separated idents are not valid")
928 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
934 /// Parses `extern` for foreign ABIs modules.
936 /// `extern` is expected to have been
937 /// consumed before calling this method.
941 /// ```ignore (only-for-syntax-highlight)
945 fn parse_item_foreign_mod(
950 mut attrs: Vec<Attribute>,
951 ) -> PResult<'a, P<Item>> {
952 let (items, iattrs) = self.parse_item_list(|p, at_end| p.parse_foreign_item(at_end))?;
953 attrs.extend(iattrs);
954 let span = lo.to(self.prev_span);
955 let m = ast::ForeignMod { abi, items };
956 Ok(self.mk_item(span, Ident::invalid(), ItemKind::ForeignMod(m), vis, attrs))
959 /// Parses a foreign item (one in an `extern { ... }` block).
960 pub fn parse_foreign_item(&mut self, at_end: &mut bool) -> PResult<'a, P<ForeignItem>> {
961 maybe_whole!(self, NtForeignItem, |ni| ni);
963 let mut attrs = self.parse_outer_attributes()?;
964 let lo = self.token.span;
965 let vis = self.parse_visibility(FollowedByType::No)?;
967 let (ident, kind) = if self.check_keyword(kw::Type) {
969 self.parse_item_foreign_type()?
970 } else if self.check_fn_front_matter() {
971 // FOREIGN FUNCTION ITEM
972 let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, |_| true)?;
973 (ident, ForeignItemKind::Fn(sig, generics, body))
974 } else if self.is_static_global() {
975 // FOREIGN STATIC ITEM
976 self.bump(); // `static`
977 self.parse_item_foreign_static()?
978 } else if self.token.is_keyword(kw::Const) {
979 // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
980 self.bump(); // `const`
981 self.struct_span_err(self.prev_span, "extern items cannot be `const`")
984 "try using a static value",
986 Applicability::MachineApplicable,
989 self.parse_item_foreign_static()?
990 } else if let Some(mac) = self.parse_assoc_macro_invoc("extern", Some(&vis), at_end)? {
991 (Ident::invalid(), ForeignItemKind::Macro(mac))
993 if !attrs.is_empty() {
994 self.expected_item_err(&attrs)?;
999 let span = lo.to(self.prev_span);
1000 Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
1003 /// Parses a static item from a foreign module.
1004 /// Assumes that the `static` keyword is already parsed.
1005 fn parse_item_foreign_static(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
1006 let mutbl = self.parse_mutability();
1007 let ident = self.parse_ident()?;
1008 self.expect(&token::Colon)?;
1009 let ty = self.parse_ty()?;
1010 self.expect_semi()?;
1011 Ok((ident, ForeignItemKind::Static(ty, mutbl)))
1014 /// Parses a type from a foreign module.
1015 fn parse_item_foreign_type(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
1016 self.expect_keyword(kw::Type)?;
1017 let ident = self.parse_ident()?;
1018 self.expect_semi()?;
1019 Ok((ident, ForeignItemKind::Ty))
1022 fn is_static_global(&mut self) -> bool {
1023 if self.check_keyword(kw::Static) {
1024 // Check if this could be a closure.
1025 !self.look_ahead(1, |token| {
1026 if token.is_keyword(kw::Move) {
1030 token::BinOp(token::Or) | token::OrOr => true,
1039 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
1040 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1042 /// When `m` is `"const"`, `$ident` may also be `"_"`.
1043 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
1044 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1046 // Parse the type of a `const` or `static mut?` item.
1047 // That is, the `":" $ty` fragment.
1048 let ty = if self.token == token::Eq {
1049 self.recover_missing_const_type(id, m)
1051 // Not `=` so expect `":"" $ty` as usual.
1052 self.expect(&token::Colon)?;
1056 self.expect(&token::Eq)?;
1057 let e = self.parse_expr()?;
1058 self.expect_semi()?;
1059 let item = match m {
1060 Some(m) => ItemKind::Static(ty, m, e),
1061 None => ItemKind::Const(ty, e),
1063 Ok((id, item, None))
1066 /// We were supposed to parse `:` but instead, we're already at `=`.
1067 /// This means that the type is missing.
1068 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1069 // Construct the error and stash it away with the hope
1070 // that typeck will later enrich the error with a type.
1071 let kind = match m {
1072 Some(Mutability::Mut) => "static mut",
1073 Some(Mutability::Not) => "static",
1076 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1077 err.span_suggestion(
1079 "provide a type for the item",
1080 format!("{}: <type>", id),
1081 Applicability::HasPlaceholders,
1083 err.stash(id.span, StashKey::ItemNoType);
1085 // The user intended that the type be inferred,
1086 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1087 P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
1090 /// Parses the grammar:
1091 /// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
1092 fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
1093 let ident = self.parse_ident()?;
1094 let mut tps = self.parse_generics()?;
1095 tps.where_clause = self.parse_where_clause()?;
1096 self.expect(&token::Eq)?;
1097 let ty = self.parse_ty()?;
1098 self.expect_semi()?;
1099 Ok((ident, ty, tps))
1102 /// Parses an enum declaration.
1103 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1104 let id = self.parse_ident()?;
1105 let mut generics = self.parse_generics()?;
1106 generics.where_clause = self.parse_where_clause()?;
1109 self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1110 self.recover_stmt();
1114 let enum_definition =
1115 EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
1116 Ok((id, ItemKind::Enum(enum_definition, generics), None))
1119 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1120 let variant_attrs = self.parse_outer_attributes()?;
1121 let vlo = self.token.span;
1123 let vis = self.parse_visibility(FollowedByType::No)?;
1124 if !self.recover_nested_adt_item(kw::Enum)? {
1127 let ident = self.parse_ident()?;
1129 let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
1130 // Parse a struct variant.
1131 let (fields, recovered) = self.parse_record_struct_body()?;
1132 VariantData::Struct(fields, recovered)
1133 } else if self.check(&token::OpenDelim(token::Paren)) {
1134 VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1136 VariantData::Unit(DUMMY_NODE_ID)
1140 if self.eat(&token::Eq) { Some(self.parse_anon_const_expr()?) } else { None };
1142 let vr = ast::Variant {
1146 attrs: variant_attrs,
1149 span: vlo.to(self.prev_span),
1150 is_placeholder: false,
1156 /// Parses `struct Foo { ... }`.
1157 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1158 let class_name = self.parse_ident()?;
1160 let mut generics = self.parse_generics()?;
1162 // There is a special case worth noting here, as reported in issue #17904.
1163 // If we are parsing a tuple struct it is the case that the where clause
1164 // should follow the field list. Like so:
1166 // struct Foo<T>(T) where T: Copy;
1168 // If we are parsing a normal record-style struct it is the case
1169 // that the where clause comes before the body, and after the generics.
1170 // So if we look ahead and see a brace or a where-clause we begin
1171 // parsing a record style struct.
1173 // Otherwise if we look ahead and see a paren we parse a tuple-style
1176 let vdata = if self.token.is_keyword(kw::Where) {
1177 generics.where_clause = self.parse_where_clause()?;
1178 if self.eat(&token::Semi) {
1179 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1180 VariantData::Unit(DUMMY_NODE_ID)
1182 // If we see: `struct Foo<T> where T: Copy { ... }`
1183 let (fields, recovered) = self.parse_record_struct_body()?;
1184 VariantData::Struct(fields, recovered)
1186 // No `where` so: `struct Foo<T>;`
1187 } else if self.eat(&token::Semi) {
1188 VariantData::Unit(DUMMY_NODE_ID)
1189 // Record-style struct definition
1190 } else if self.token == token::OpenDelim(token::Brace) {
1191 let (fields, recovered) = self.parse_record_struct_body()?;
1192 VariantData::Struct(fields, recovered)
1193 // Tuple-style struct definition with optional where-clause.
1194 } else if self.token == token::OpenDelim(token::Paren) {
1195 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1196 generics.where_clause = self.parse_where_clause()?;
1197 self.expect_semi()?;
1200 let token_str = super::token_descr(&self.token);
1202 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1205 let mut err = self.struct_span_err(self.token.span, msg);
1206 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1210 Ok((class_name, ItemKind::Struct(vdata, generics), None))
1213 /// Parses `union Foo { ... }`.
1214 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1215 let class_name = self.parse_ident()?;
1217 let mut generics = self.parse_generics()?;
1219 let vdata = if self.token.is_keyword(kw::Where) {
1220 generics.where_clause = self.parse_where_clause()?;
1221 let (fields, recovered) = self.parse_record_struct_body()?;
1222 VariantData::Struct(fields, recovered)
1223 } else if self.token == token::OpenDelim(token::Brace) {
1224 let (fields, recovered) = self.parse_record_struct_body()?;
1225 VariantData::Struct(fields, recovered)
1227 let token_str = super::token_descr(&self.token);
1228 let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1229 let mut err = self.struct_span_err(self.token.span, msg);
1230 err.span_label(self.token.span, "expected `where` or `{` after union name");
1234 Ok((class_name, ItemKind::Union(vdata, generics), None))
1237 pub(super) fn is_union_item(&self) -> bool {
1238 self.token.is_keyword(kw::Union)
1239 && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
1242 fn parse_record_struct_body(
1244 ) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
1245 let mut fields = Vec::new();
1246 let mut recovered = false;
1247 if self.eat(&token::OpenDelim(token::Brace)) {
1248 while self.token != token::CloseDelim(token::Brace) {
1249 let field = self.parse_struct_decl_field().map_err(|e| {
1250 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1255 Ok(field) => fields.push(field),
1262 self.eat(&token::CloseDelim(token::Brace));
1264 let token_str = super::token_descr(&self.token);
1265 let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
1266 let mut err = self.struct_span_err(self.token.span, msg);
1267 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1271 Ok((fields, recovered))
1274 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
1275 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1276 // Unit like structs are handled in parse_item_struct function
1277 self.parse_paren_comma_seq(|p| {
1278 let attrs = p.parse_outer_attributes()?;
1279 let lo = p.token.span;
1280 let vis = p.parse_visibility(FollowedByType::Yes)?;
1281 let ty = p.parse_ty()?;
1283 span: lo.to(ty.span),
1289 is_placeholder: false,
1295 /// Parses an element of a struct declaration.
1296 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
1297 let attrs = self.parse_outer_attributes()?;
1298 let lo = self.token.span;
1299 let vis = self.parse_visibility(FollowedByType::No)?;
1300 self.parse_single_struct_field(lo, vis, attrs)
1303 /// Parses a structure field declaration.
1304 fn parse_single_struct_field(
1308 attrs: Vec<Attribute>,
1309 ) -> PResult<'a, StructField> {
1310 let mut seen_comma: bool = false;
1311 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1312 if self.token == token::Comma {
1315 match self.token.kind {
1319 token::CloseDelim(token::Brace) => {}
1320 token::DocComment(_) => {
1321 let previous_span = self.prev_span;
1322 let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1323 self.bump(); // consume the doc comment
1324 let comma_after_doc_seen = self.eat(&token::Comma);
1325 // `seen_comma` is always false, because we are inside doc block
1326 // condition is here to make code more readable
1327 if seen_comma == false && comma_after_doc_seen == true {
1330 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1333 if seen_comma == false {
1334 let sp = self.sess.source_map().next_point(previous_span);
1335 err.span_suggestion(
1337 "missing comma here",
1339 Applicability::MachineApplicable,
1346 let sp = self.prev_span.shrink_to_hi();
1347 let mut err = self.struct_span_err(
1349 &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1351 if self.token.is_ident() {
1352 // This is likely another field; emit the diagnostic and keep going
1353 err.span_suggestion(
1355 "try adding a comma",
1357 Applicability::MachineApplicable,
1368 /// Parses a structure field.
1369 fn parse_name_and_ty(
1373 attrs: Vec<Attribute>,
1374 ) -> PResult<'a, StructField> {
1375 let name = self.parse_ident()?;
1376 self.expect(&token::Colon)?;
1377 let ty = self.parse_ty()?;
1379 span: lo.to(self.prev_span),
1385 is_placeholder: false,
1389 pub(super) fn eat_macro_def(
1391 attrs: &[Attribute],
1394 ) -> PResult<'a, Option<P<Item>>> {
1395 let (ident, def) = if self.eat_keyword(kw::Macro) {
1396 let ident = self.parse_ident()?;
1397 let body = if self.check(&token::OpenDelim(token::Brace)) {
1398 self.parse_mac_args()?
1399 } else if self.check(&token::OpenDelim(token::Paren)) {
1400 let params = self.parse_token_tree();
1401 let pspan = params.span();
1402 let body = if self.check(&token::OpenDelim(token::Brace)) {
1403 self.parse_token_tree()
1405 return self.unexpected();
1407 let bspan = body.span();
1408 let tokens = TokenStream::new(vec![
1410 TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
1413 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1414 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1416 return self.unexpected();
1419 (ident, ast::MacroDef { body, legacy: false })
1420 } else if self.check_keyword(sym::macro_rules)
1421 && self.look_ahead(1, |t| *t == token::Not)
1422 && self.look_ahead(2, |t| t.is_ident())
1424 let prev_span = self.prev_span;
1425 self.complain_if_pub_macro(&vis.node, prev_span);
1429 let ident = self.parse_ident()?;
1430 let body = self.parse_mac_args()?;
1431 if body.need_semicolon() && !self.eat(&token::Semi) {
1432 self.report_invalid_macro_expansion_item();
1435 (ident, ast::MacroDef { body, legacy: true })
1440 let span = lo.to(self.prev_span);
1443 self.sess.gated_spans.gate(sym::decl_macro, span);
1446 Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
1449 fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
1451 VisibilityKind::Inherited => {}
1453 let mut err = if self.token.is_keyword(sym::macro_rules) {
1455 self.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
1456 err.span_suggestion(
1458 "try exporting the macro",
1459 "#[macro_export]".to_owned(),
1460 Applicability::MaybeIncorrect, // speculative
1465 self.struct_span_err(sp, "can't qualify macro invocation with `pub`");
1466 err.help("try adjusting the macro to put `pub` inside the invocation");
1474 fn report_invalid_macro_expansion_item(&self) {
1475 let has_close_delim = self
1478 .span_to_snippet(self.prev_span)
1479 .map(|s| s.ends_with(")") || s.ends_with("]"))
1482 let mut err = self.struct_span_err(
1484 "macros that expand to items must be delimited with braces or followed by a semicolon",
1487 // To avoid ICE, we shouldn't emit actual suggestions when it hasn't closing delims
1488 if has_close_delim {
1489 err.multipart_suggestion(
1490 "change the delimiters to curly braces",
1492 (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), '{'.to_string()),
1493 (self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
1495 Applicability::MaybeIncorrect,
1498 err.span_suggestion(
1500 "change the delimiters to curly braces",
1501 " { /* items */ }".to_string(),
1502 Applicability::HasPlaceholders,
1506 err.span_suggestion(
1507 self.prev_span.shrink_to_hi(),
1510 Applicability::MaybeIncorrect,
1515 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1516 /// it is, we try to parse the item and report error about nested types.
1517 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1518 if (self.token.is_keyword(kw::Enum)
1519 || self.token.is_keyword(kw::Struct)
1520 || self.token.is_keyword(kw::Union))
1521 && self.look_ahead(1, |t| t.is_ident())
1523 let kw_token = self.token.clone();
1524 let kw_str = pprust::token_to_string(&kw_token);
1525 let item = self.parse_item()?;
1527 self.struct_span_err(
1529 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1533 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1535 Applicability::MaybeIncorrect,
1538 // We successfully parsed the item but we must inform the caller about nested problem.
1550 attrs: Vec<Attribute>,
1552 P(Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None })
1556 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1558 /// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1559 type ReqName = fn(&token::Token) -> bool;
1561 /// Parsing of functions and methods.
1562 impl<'a> Parser<'a> {
1563 /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
1567 attrs: &mut Vec<Attribute>,
1569 ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
1570 let header = self.parse_fn_front_matter()?; // `const ... fn`
1571 let ident = self.parse_ident()?; // `foo`
1572 let mut generics = self.parse_generics()?; // `<'a, T, ...>`
1573 let decl = self.parse_fn_decl(req_name, AllowPlus::Yes)?; // `(p: u8, ...)`
1574 generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
1575 let body = self.parse_fn_body(at_end, attrs)?; // `;` or `{ ... }`.
1576 Ok((ident, FnSig { header, decl }, generics, body))
1579 /// Parse the "body" of a function.
1580 /// This can either be `;` when there's no body,
1581 /// or e.g. a block when the function is a provided one.
1585 attrs: &mut Vec<Attribute>,
1586 ) -> PResult<'a, Option<P<Block>>> {
1587 let (inner_attrs, body) = match self.token.kind {
1592 token::OpenDelim(token::Brace) => {
1593 let (attrs, body) = self.parse_inner_attrs_and_block()?;
1596 token::Interpolated(ref nt) => match **nt {
1597 token::NtBlock(..) => {
1598 let (attrs, body) = self.parse_inner_attrs_and_block()?;
1601 _ => return self.expected_semi_or_open_brace(),
1603 _ => return self.expected_semi_or_open_brace(),
1605 attrs.extend(inner_attrs);
1610 /// Is the current token the start of an `FnHeader` / not a valid parse?
1611 fn check_fn_front_matter(&mut self) -> bool {
1612 // We use an over-approximation here.
1613 // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
1614 const QUALS: [Symbol; 4] = [kw::Const, kw::Async, kw::Unsafe, kw::Extern];
1615 self.check_keyword(kw::Fn) // Definitely an `fn`.
1616 // `$qual fn` or `$qual $qual`:
1617 || QUALS.iter().any(|&kw| self.check_keyword(kw))
1618 && self.look_ahead(1, |t| {
1619 // ...qualified and then `fn`, e.g. `const fn`.
1620 t.is_keyword(kw::Fn)
1621 // Two qualifiers. This is enough. Due `async` we need to check that it's reserved.
1622 || t.is_non_raw_ident_where(|i| QUALS.contains(&i.name) && i.is_reserved())
1625 || self.check_keyword(kw::Extern)
1626 && self.look_ahead(1, |t| t.can_begin_literal_or_bool())
1627 && self.look_ahead(2, |t| t.is_keyword(kw::Fn))
1630 /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
1631 /// up to and including the `fn` keyword. The formal grammar is:
1634 /// Extern = "extern" StringLit ;
1635 /// FnQual = "const"? "async"? "unsafe"? Extern? ;
1636 /// FnFrontMatter = FnQual? "fn" ;
1638 fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1639 let constness = self.parse_constness();
1640 let asyncness = self.parse_asyncness();
1641 let unsafety = self.parse_unsafety();
1642 let ext = self.parse_extern()?;
1644 if let Async::Yes { span, .. } = asyncness {
1645 self.ban_async_in_2015(span);
1648 if !self.eat_keyword(kw::Fn) {
1649 // It is possible for `expect_one_of` to recover given the contents of
1650 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1651 // account for this.
1652 if !self.expect_one_of(&[], &[])? {
1657 Ok(FnHeader { constness, unsafety, asyncness, ext })
1660 /// We are parsing `async fn`. If we are on Rust 2015, emit an error.
1661 fn ban_async_in_2015(&self, span: Span) {
1662 if span.rust_2015() {
1663 let diag = self.diagnostic();
1664 struct_span_err!(diag, span, E0670, "`async fn` is not permitted in the 2015 edition")
1665 .note("to use `async fn`, switch to Rust 2018")
1666 .help("set `edition = \"2018\"` in `Cargo.toml`")
1667 .note("for more on editions, read https://doc.rust-lang.org/edition-guide")
1672 /// Parses the parameter list and result type of a function declaration.
1673 pub(super) fn parse_fn_decl(
1676 ret_allow_plus: AllowPlus,
1677 ) -> PResult<'a, P<FnDecl>> {
1679 inputs: self.parse_fn_params(req_name)?,
1680 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
1684 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1685 fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
1686 let mut first_param = true;
1687 // Parse the arguments, starting out with `self` being allowed...
1688 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1689 let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
1691 let lo = p.prev_span;
1692 // Skip every token until next possible arg or end.
1693 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1694 // Create a placeholder argument for proper arg count (issue #34264).
1695 Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
1697 // ...now that we've parsed the first argument, `self` is no longer allowed.
1698 first_param = false;
1701 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1702 self.deduplicate_recovered_params_names(&mut params);
1706 /// Parses a single function parameter.
1708 /// - `self` is syntactically allowed when `first_param` holds.
1709 fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
1710 let lo = self.token.span;
1711 let attrs = self.parse_outer_attributes()?;
1713 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1714 if let Some(mut param) = self.parse_self_param()? {
1715 param.attrs = attrs.into();
1716 return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
1719 let is_name_required = match self.token.kind {
1720 token::DotDotDot => false,
1721 _ => req_name(&self.token),
1723 let (pat, ty) = if is_name_required || self.is_named_param() {
1724 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1726 let pat = self.parse_fn_param_pat()?;
1727 if let Err(mut err) = self.expect(&token::Colon) {
1728 return if let Some(ident) =
1729 self.parameter_without_type(&mut err, pat, is_name_required, first_param)
1732 Ok(dummy_arg(ident))
1738 self.eat_incorrect_doc_comment_for_param_type();
1739 (pat, self.parse_ty_for_param()?)
1741 debug!("parse_param_general ident_to_pat");
1742 let parser_snapshot_before_ty = self.clone();
1743 self.eat_incorrect_doc_comment_for_param_type();
1744 let mut ty = self.parse_ty_for_param();
1746 && self.token != token::Comma
1747 && self.token != token::CloseDelim(token::Paren)
1749 // This wasn't actually a type, but a pattern looking like a type,
1750 // so we are going to rollback and re-parse for recovery.
1751 ty = self.unexpected();
1755 let ident = Ident::new(kw::Invalid, self.prev_span);
1756 let bm = BindingMode::ByValue(Mutability::Not);
1757 let pat = self.mk_pat_ident(ty.span, bm, ident);
1760 // If this is a C-variadic argument and we hit an error, return the error.
1761 Err(err) if self.token == token::DotDotDot => return Err(err),
1762 // Recover from attempting to parse the argument as a type without pattern.
1765 mem::replace(self, parser_snapshot_before_ty);
1766 self.recover_arg_parse()?
1771 let span = lo.to(self.token.span);
1774 attrs: attrs.into(),
1775 id: ast::DUMMY_NODE_ID,
1776 is_placeholder: false,
1783 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1784 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1785 // Extract an identifier *after* having confirmed that the token is one.
1786 let expect_self_ident = |this: &mut Self| {
1787 match this.token.kind {
1788 // Preserve hygienic context.
1789 token::Ident(name, _) => {
1790 let span = this.token.span;
1792 Ident::new(name, span)
1794 _ => unreachable!(),
1797 // Is `self` `n` tokens ahead?
1798 let is_isolated_self = |this: &Self, n| {
1799 this.is_keyword_ahead(n, &[kw::SelfLower])
1800 && this.look_ahead(n + 1, |t| t != &token::ModSep)
1802 // Is `mut self` `n` tokens ahead?
1803 let is_isolated_mut_self =
1804 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
1805 // Parse `self` or `self: TYPE`. We already know the current token is `self`.
1806 let parse_self_possibly_typed = |this: &mut Self, m| {
1807 let eself_ident = expect_self_ident(this);
1808 let eself_hi = this.prev_span;
1809 let eself = if this.eat(&token::Colon) {
1810 SelfKind::Explicit(this.parse_ty()?, m)
1814 Ok((eself, eself_ident, eself_hi))
1816 // Recover for the grammar `*self`, `*const self`, and `*mut self`.
1817 let recover_self_ptr = |this: &mut Self| {
1818 let msg = "cannot pass `self` by raw pointer";
1819 let span = this.token.span;
1820 this.struct_span_err(span, msg).span_label(span, msg).emit();
1822 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_span))
1825 // Parse optional `self` parameter of a method.
1826 // Only a limited set of initial token sequences is considered `self` parameters; anything
1827 // else is parsed as a normal function parameter list, so some lookahead is required.
1828 let eself_lo = self.token.span;
1829 let (eself, eself_ident, eself_hi) = match self.token.kind {
1830 token::BinOp(token::And) => {
1831 let eself = if is_isolated_self(self, 1) {
1834 SelfKind::Region(None, Mutability::Not)
1835 } else if is_isolated_mut_self(self, 1) {
1839 SelfKind::Region(None, Mutability::Mut)
1840 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
1843 let lt = self.expect_lifetime();
1844 SelfKind::Region(Some(lt), Mutability::Not)
1845 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
1848 let lt = self.expect_lifetime();
1850 SelfKind::Region(Some(lt), Mutability::Mut)
1855 (eself, expect_self_ident(self), self.prev_span)
1858 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
1860 recover_self_ptr(self)?
1862 // `*mut self` and `*const self`
1863 token::BinOp(token::Star)
1864 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
1868 recover_self_ptr(self)?
1870 // `self` and `self: TYPE`
1871 token::Ident(..) if is_isolated_self(self, 0) => {
1872 parse_self_possibly_typed(self, Mutability::Not)?
1874 // `mut self` and `mut self: TYPE`
1875 token::Ident(..) if is_isolated_mut_self(self, 0) => {
1877 parse_self_possibly_typed(self, Mutability::Mut)?
1879 _ => return Ok(None),
1882 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
1883 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
1886 fn is_named_param(&self) -> bool {
1887 let offset = match self.token.kind {
1888 token::Interpolated(ref nt) => match **nt {
1889 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
1892 token::BinOp(token::And) | token::AndAnd => 1,
1893 _ if self.token.is_keyword(kw::Mut) => 1,
1897 self.look_ahead(offset, |t| t.is_ident())
1898 && self.look_ahead(offset + 1, |t| t == &token::Colon)
1901 fn recover_first_param(&mut self) -> &'static str {
1903 .parse_outer_attributes()
1904 .and_then(|_| self.parse_self_param())
1905 .map_err(|mut e| e.cancel())
1907 Ok(Some(_)) => "method",