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 syntax::ast::{self, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
12 use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
13 use syntax::ast::{Async, Const, Defaultness, IsAuto, PathSegment, Unsafe};
14 use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
15 use syntax::ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
16 use syntax::ast::{FnHeader, ForeignItem, ForeignItemKind, Mutability, Visibility, VisibilityKind};
19 use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
24 pub(super) type ItemInfo = (Ident, ItemKind);
27 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
28 let attrs = self.parse_outer_attributes()?;
29 self.parse_item_(attrs, true, false)
32 pub(super) fn parse_item_(
34 attrs: Vec<Attribute>,
36 attributes_allowed: bool,
37 ) -> PResult<'a, Option<P<Item>>> {
38 let mut unclosed_delims = vec![];
39 let (ret, tokens) = self.collect_tokens(|this| {
40 let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
41 unclosed_delims.append(&mut this.unclosed_delims);
44 self.unclosed_delims.append(&mut unclosed_delims);
46 // Once we've parsed an item and recorded the tokens we got while
47 // parsing we may want to store `tokens` into the item we're about to
48 // return. Note, though, that we specifically didn't capture tokens
49 // related to outer attributes. The `tokens` field here may later be
50 // used with procedural macros to convert this item back into a token
51 // stream, but during expansion we may be removing attributes as we go
54 // If we've got inner attributes then the `tokens` we've got above holds
55 // these inner attributes. If an inner attribute is expanded we won't
56 // actually remove it from the token stream, so we'll just keep yielding
57 // it (bad!). To work around this case for now we just avoid recording
58 // `tokens` if we detect any inner attributes. This should help keep
59 // expansion correct, but we should fix this bug one day!
62 if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
63 i.tokens = Some(tokens);
70 /// Parses one of the items allowed by the flags.
71 fn parse_item_implementation(
73 mut attrs: Vec<Attribute>,
75 attributes_allowed: bool,
76 ) -> PResult<'a, Option<P<Item>>> {
77 maybe_whole!(self, NtItem, |item| {
79 mem::swap(&mut item.attrs, &mut attrs);
80 item.attrs.extend(attrs);
84 let lo = self.token.span;
85 let vis = self.parse_visibility(FollowedByType::No)?;
87 if let Some((ident, kind)) = self.parse_item_kind(&mut attrs, macros_allowed, lo, &vis)? {
88 return Ok(Some(P(self.mk_item(lo, ident, kind, vis, attrs))));
91 // FAILURE TO PARSE ITEM
92 if let VisibilityKind::Inherited = vis.node {
94 let vs = pprust::vis_to_string(&vis);
95 let vs = vs.trim_end();
96 self.struct_span_err(vis.span, &format!("unmatched visibility `{}`", vs))
97 .span_label(vis.span, "the unmatched visibility")
98 .help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
102 if !attributes_allowed {
103 self.recover_attrs_no_item(&attrs)?;
108 /// Parses one of the items allowed by the flags.
111 attrs: &mut Vec<Attribute>,
112 macros_allowed: bool,
115 ) -> PResult<'a, Option<ItemInfo>> {
116 let info = if self.eat_keyword(kw::Use) {
118 let tree = self.parse_use_tree()?;
120 (Ident::invalid(), ItemKind::Use(P(tree)))
121 } else if self.check_fn_front_matter() {
123 let (ident, sig, generics, body) = self.parse_fn(&mut false, attrs, |_| true)?;
124 (ident, ItemKind::Fn(sig, generics, body))
125 } else if self.eat_keyword(kw::Extern) {
126 if self.eat_keyword(kw::Crate) {
128 self.parse_item_extern_crate()?
131 self.parse_item_foreign_mod(attrs)?
133 } else if self.is_static_global() {
135 self.bump(); // `static`
136 let m = self.parse_mutability();
137 self.parse_item_const(Some(m))?
138 } else if let Const::Yes(const_span) = self.parse_constness() {
140 self.recover_const_mut(const_span);
141 self.parse_item_const(None)?
142 } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() {
144 self.parse_item_trait(attrs, lo)?
145 } else if self.check_keyword(kw::Impl)
146 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
147 || self.check_keyword(kw::Default) && self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
150 let defaultness = self.parse_defaultness();
151 let unsafety = self.parse_unsafety();
152 self.expect_keyword(kw::Impl)?;
153 self.parse_item_impl(attrs, unsafety, defaultness)?
154 } else if self.eat_keyword(kw::Mod) {
156 self.parse_item_mod(attrs)?
157 } else if self.eat_keyword(kw::Type) {
159 let (ident, ty, generics) = self.parse_type_alias()?;
160 (ident, ItemKind::TyAlias(ty, generics))
161 } else if self.eat_keyword(kw::Enum) {
163 self.parse_item_enum()?
164 } else if self.eat_keyword(kw::Struct) {
166 self.parse_item_struct()?
167 } else if self.is_kw_followed_by_ident(kw::Union) {
169 self.bump(); // `union`
170 self.parse_item_union()?
171 } else if self.eat_keyword(kw::Macro) {
173 self.parse_item_decl_macro(lo)?
174 } else if self.is_macro_rules_item() {
176 self.parse_item_macro_rules(vis)?
177 } else if vis.node.is_pub() && self.isnt_macro_invocation() {
178 self.recover_missing_kw_before_item()?;
180 } else if macros_allowed && self.token.is_path_start() {
181 // MACRO INVOCATION ITEM
182 (Ident::invalid(), ItemKind::Mac(self.parse_item_macro(vis)?))
189 /// When parsing a statement, would the start of a path be an item?
190 pub(super) fn is_path_start_item(&mut self) -> bool {
191 self.is_crate_vis() // no: `crate::b`, yes: `crate $item`
192 || self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
193 || self.check_auto_or_unsafe_trait_item() // no: `auto::b`, yes: `auto trait X { .. }`
194 || self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
195 || self.is_macro_rules_item() // no: `macro_rules::b`, yes: `macro_rules! mac`
198 /// Are we sure this could not possibly be a macro invocation?
199 fn isnt_macro_invocation(&mut self) -> bool {
200 self.check_ident() && self.look_ahead(1, |t| *t != token::Not && *t != token::ModSep)
203 /// Recover on encountering a struct or method definition where the user
204 /// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
205 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
206 // Space between `pub` keyword and the identifier
209 // ^^^ `sp` points here
210 let sp = self.prev_span.between(self.token.span);
211 let full_sp = self.prev_span.to(self.token.span);
212 let ident_sp = self.token.span;
213 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
214 // possible public struct definition where `struct` was forgotten
215 let ident = self.parse_ident().unwrap();
216 let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
217 let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
218 err.span_suggestion_short(
222 Applicability::MaybeIncorrect, // speculative
225 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
226 let ident = self.parse_ident().unwrap();
228 let kw_name = self.recover_first_param();
229 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
230 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
231 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
233 ("fn", kw_name, false)
234 } else if self.check(&token::OpenDelim(token::Brace)) {
236 ("fn", kw_name, false)
237 } else if self.check(&token::Colon) {
241 ("fn` or `struct", "function or struct", true)
244 let msg = format!("missing `{}` for {} definition", kw, kw_name);
245 let mut err = self.struct_span_err(sp, &msg);
247 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
249 format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
250 err.span_suggestion_short(
254 Applicability::MachineApplicable,
257 if let Ok(snippet) = self.span_to_snippet(ident_sp) {
260 "if you meant to call a macro, try",
261 format!("{}!", snippet),
262 // this is the `ambiguous` conditional branch
263 Applicability::MaybeIncorrect,
267 "if you meant to call a macro, remove the `pub` \
268 and add a trailing `!` after the identifier",
273 } else if self.look_ahead(1, |t| *t == token::Lt) {
274 let ident = self.parse_ident().unwrap();
275 self.eat_to_tokens(&[&token::Gt]);
277 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
278 ("fn", self.recover_first_param(), false)
279 } else if self.check(&token::OpenDelim(token::Brace)) {
280 ("struct", "struct", false)
282 ("fn` or `struct", "function or struct", true)
284 let msg = format!("missing `{}` for {} definition", kw, kw_name);
285 let mut err = self.struct_span_err(sp, &msg);
287 err.span_suggestion_short(
289 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
291 Applicability::MachineApplicable,
300 /// Parses an item macro, e.g., `item!();`.
301 fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, Mac> {
302 let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
303 self.expect(&token::Not)?; // `!`
304 let args = self.parse_mac_args()?; // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
305 self.eat_semi_for_macro_if_needed(&args);
306 self.complain_if_pub_macro(vis, false);
307 Ok(Mac { path, args, prior_type_ascription: self.last_type_ascription })
310 /// Recover if we parsed attributes and expected an item but there was none.
311 fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
312 let (start, end) = match attrs {
315 [x0, .., xn] => (x0, xn),
317 let msg = if end.is_doc_comment() {
318 "expected item after doc comment"
320 "expected item after attributes"
322 let mut err = self.struct_span_err(end.span, msg);
323 if end.is_doc_comment() {
324 err.span_label(end.span, "this doc comment doesn't document anything");
326 if let [.., penultimate, _] = attrs {
327 err.span_label(start.span.to(penultimate.span), "other attributes here");
332 fn is_async_fn(&self) -> bool {
333 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
336 fn missing_assoc_item_kind_err(
340 ) -> DiagnosticBuilder<'a> {
341 let expected_kinds = if item_type == "extern" {
342 "missing `fn`, `type`, or `static`"
344 "missing `fn`, `type`, or `const`"
347 // Given this code `path(`, it seems like this is not
348 // setting the visibility of a macro invocation, but rather
349 // a mistyped method declaration.
350 // Create a diagnostic pointing out that `fn` is missing.
352 // x | pub path(&self) {
353 // | ^ missing `fn`, `type`, or `const`
355 // ^^ `sp` below will point to this
356 let sp = prev_span.between(self.token.span);
358 .struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
359 err.span_label(sp, expected_kinds);
363 /// Parses an implementation item, `impl` keyword is already parsed.
366 /// impl<'a, T> TYPE { /* impl items */ }
367 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
368 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
369 /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
372 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
374 /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
375 /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
379 attrs: &mut Vec<Attribute>,
381 defaultness: Defaultness,
382 ) -> PResult<'a, ItemInfo> {
383 // First, parse generic parameters if necessary.
384 let mut generics = if self.choose_generics_over_qpath() {
385 self.parse_generics()?
387 let mut generics = Generics::default();
389 // /\ this is where `generics.span` should point when there are no type params.
390 generics.span = self.prev_span.shrink_to_hi();
394 let constness = self.parse_constness();
395 if let Const::Yes(span) = constness {
396 self.sess.gated_spans.gate(sym::const_trait_impl, span);
399 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
400 let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
402 ast::ImplPolarity::Negative
404 ast::ImplPolarity::Positive
407 // Parse both types and traits as a type, then reinterpret if necessary.
408 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
409 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
411 let span = self.prev_span.between(self.token.span);
412 self.struct_span_err(span, "missing trait in a trait impl").emit();
413 P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
418 // If `for` is missing we try to recover.
419 let has_for = self.eat_keyword(kw::For);
420 let missing_for_span = self.prev_span.between(self.token.span);
422 let ty_second = if self.token == token::DotDot {
423 // We need to report this error after `cfg` expansion for compatibility reasons
424 self.bump(); // `..`, do not add it to expected tokens
425 Some(self.mk_ty(self.prev_span, TyKind::Err))
426 } else if has_for || self.token.can_begin_type() {
427 Some(self.parse_ty()?)
432 generics.where_clause = self.parse_where_clause()?;
434 let impl_items = self.parse_item_list(attrs, |p, at_end| p.parse_impl_item(at_end))?;
436 let item_kind = match ty_second {
438 // impl Trait for Type
440 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
441 .span_suggestion_short(
445 Applicability::MachineApplicable,
450 let ty_first = ty_first.into_inner();
451 let path = match ty_first.kind {
452 // This notably includes paths passed through `ty` macro fragments (#46438).
453 TyKind::Path(None, path) => path,
455 self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
456 err_path(ty_first.span)
459 let trait_ref = TraitRef { path, ref_id: ty_first.id };
467 of_trait: Some(trait_ref),
487 Ok((Ident::invalid(), item_kind))
490 fn parse_item_list<T>(
492 attrs: &mut Vec<Attribute>,
493 mut parse_item: impl FnMut(&mut Parser<'a>, &mut bool) -> PResult<'a, T>,
494 ) -> PResult<'a, Vec<T>> {
495 self.expect(&token::OpenDelim(token::Brace))?;
496 attrs.append(&mut self.parse_inner_attributes()?);
498 let mut items = Vec::new();
499 while !self.eat(&token::CloseDelim(token::Brace)) {
500 if self.recover_doc_comment_before_brace() {
503 let mut at_end = false;
504 match parse_item(self, &mut at_end) {
505 Ok(item) => items.push(item),
509 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
518 /// Recover on a doc comment before `}`.
519 fn recover_doc_comment_before_brace(&mut self) -> bool {
520 if let token::DocComment(_) = self.token.kind {
521 if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
526 "found a documentation comment that doesn't document anything",
528 .span_label(self.token.span, "this doc comment doesn't document anything")
530 "doc comments must come before what they document, maybe a \
531 comment was intended with `//`?",
541 /// Parses defaultness (i.e., `default` or nothing).
542 fn parse_defaultness(&mut self) -> Defaultness {
543 // `pub` is included for better error messages
544 if self.check_keyword(kw::Default)
545 && self.is_keyword_ahead(
559 self.bump(); // `default`
566 /// Is this an `(unsafe auto? | auto) trait` item?
567 fn check_auto_or_unsafe_trait_item(&mut self) -> bool {
569 self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait])
571 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
574 /// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
575 fn parse_item_trait(&mut self, attrs: &mut Vec<Attribute>, lo: Span) -> PResult<'a, ItemInfo> {
576 let unsafety = self.parse_unsafety();
577 // Parse optional `auto` prefix.
578 let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
580 self.expect_keyword(kw::Trait)?;
581 let ident = self.parse_ident()?;
582 let mut tps = self.parse_generics()?;
584 // Parse optional colon and supertrait bounds.
585 let had_colon = self.eat(&token::Colon);
586 let span_at_colon = self.prev_span;
588 if had_colon { self.parse_generic_bounds(Some(self.prev_span))? } else { Vec::new() };
590 let span_before_eq = self.prev_span;
591 if self.eat(&token::Eq) {
592 // It's a trait alias.
594 let span = span_at_colon.to(span_before_eq);
595 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
598 let bounds = self.parse_generic_bounds(None)?;
599 tps.where_clause = self.parse_where_clause()?;
602 let whole_span = lo.to(self.prev_span);
603 if is_auto == IsAuto::Yes {
604 let msg = "trait aliases cannot be `auto`";
605 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
607 if let Unsafe::Yes(_) = unsafety {
608 let msg = "trait aliases cannot be `unsafe`";
609 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
612 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
614 Ok((ident, ItemKind::TraitAlias(tps, bounds)))
616 // It's a normal trait.
617 tps.where_clause = self.parse_where_clause()?;
618 let items = self.parse_item_list(attrs, |p, at_end| p.parse_trait_item(at_end))?;
619 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, items)))
623 pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
624 maybe_whole!(self, NtImplItem, |x| x);
625 self.parse_assoc_item(at_end, |_| true)
628 pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
629 maybe_whole!(self, NtTraitItem, |x| x);
630 // This is somewhat dubious; We don't want to allow
631 // param names to be left off if there is a definition...
633 // We don't allow param names to be left off in edition 2018.
634 self.parse_assoc_item(at_end, |t| t.span.rust_2018())
637 /// Parses associated items.
641 req_name: fn(&token::Token) -> bool,
642 ) -> PResult<'a, P<AssocItem>> {
643 let attrs = self.parse_outer_attributes()?;
644 let mut unclosed_delims = vec![];
645 let (mut item, tokens) = self.collect_tokens(|this| {
646 let item = this.parse_assoc_item_(at_end, attrs, req_name);
647 unclosed_delims.append(&mut this.unclosed_delims);
650 self.unclosed_delims.append(&mut unclosed_delims);
651 // See `parse_item` for why this clause is here.
652 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
653 item.tokens = Some(tokens);
658 fn parse_assoc_item_(
661 mut attrs: Vec<Attribute>,
662 req_name: fn(&token::Token) -> bool,
663 ) -> PResult<'a, AssocItem> {
664 let lo = self.token.span;
665 let vis = self.parse_visibility(FollowedByType::No)?;
666 let defaultness = self.parse_defaultness();
668 let (ident, kind, generics) = if self.eat_keyword(kw::Type) {
669 self.parse_assoc_ty()?
670 } else if self.check_fn_front_matter() {
671 let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
672 (ident, AssocItemKind::Fn(sig, body), generics)
673 } else if self.check_keyword(kw::Const) {
674 self.parse_assoc_const()?
675 } else if self.isnt_macro_invocation() {
676 return Err(self.missing_assoc_item_kind_err("associated", self.prev_span));
677 } else if self.token.is_path_start() {
678 let mac = self.parse_item_macro(&vis)?;
680 (Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
682 self.recover_attrs_no_item(&attrs)?;
686 let span = lo.to(self.prev_span);
687 let id = DUMMY_NODE_ID;
688 Ok(AssocItem { id, span, ident, attrs, vis, defaultness, generics, kind, tokens: None })
691 /// This parses the grammar:
693 /// AssocConst = "const" Ident ":" Ty "=" Expr ";"
694 fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
695 self.expect_keyword(kw::Const)?;
696 let ident = self.parse_ident()?;
697 self.expect(&token::Colon)?;
698 let ty = self.parse_ty()?;
699 let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
701 Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
704 /// Parses the following grammar:
706 /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
707 fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
708 let ident = self.parse_ident()?;
709 let mut generics = self.parse_generics()?;
711 // Parse optional colon and param bounds.
713 if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
714 generics.where_clause = self.parse_where_clause()?;
716 let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
719 Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
722 /// Parses a `UseTree`.
725 /// USE_TREE = [`::`] `*` |
726 /// [`::`] `{` USE_TREE_LIST `}` |
728 /// PATH `::` `{` USE_TREE_LIST `}` |
729 /// PATH [`as` IDENT]
731 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
732 let lo = self.token.span;
734 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
735 let kind = if self.check(&token::OpenDelim(token::Brace))
736 || self.check(&token::BinOp(token::Star))
737 || self.is_import_coupler()
739 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
740 let mod_sep_ctxt = self.token.span.ctxt();
741 if self.eat(&token::ModSep) {
744 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
747 self.parse_use_tree_glob_or_nested()?
749 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
750 prefix = self.parse_path(PathStyle::Mod)?;
752 if self.eat(&token::ModSep) {
753 self.parse_use_tree_glob_or_nested()?
755 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
759 Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
762 /// Parses `*` or `{...}`.
763 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
764 Ok(if self.eat(&token::BinOp(token::Star)) {
767 UseTreeKind::Nested(self.parse_use_tree_list()?)
771 /// Parses a `UseTreeKind::Nested(list)`.
774 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
776 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
777 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
781 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
782 if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
785 fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
786 match self.token.kind {
787 token::Ident(name @ kw::Underscore, false) => {
788 let span = self.token.span;
790 Ok(Ident::new(name, span))
792 _ => self.parse_ident(),
796 /// Parses `extern crate` links.
801 /// extern crate foo;
802 /// extern crate bar as foo;
804 fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemInfo> {
805 // Accept `extern crate name-like-this` for better diagnostics
806 let orig_name = self.parse_crate_name_with_dashes()?;
807 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
808 (rename, Some(orig_name.name))
813 Ok((item_name, ItemKind::ExternCrate(orig_name)))
816 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
817 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
818 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
820 let mut ident = if self.token.is_keyword(kw::SelfLower) {
821 self.parse_path_segment_ident()
825 let mut idents = vec![];
826 let mut replacement = vec![];
827 let mut fixed_crate_name = false;
828 // Accept `extern crate name-like-this` for better diagnostics.
829 let dash = token::BinOp(token::BinOpToken::Minus);
830 if self.token == dash {
831 // Do not include `-` as part of the expected tokens list.
832 while self.eat(&dash) {
833 fixed_crate_name = true;
834 replacement.push((self.prev_span, "_".to_string()));
835 idents.push(self.parse_ident()?);
838 if fixed_crate_name {
839 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
840 let mut fixed_name = format!("{}", ident.name);
842 fixed_name.push_str(&format!("_{}", part.name));
844 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
846 self.struct_span_err(fixed_name_sp, error_msg)
847 .span_label(fixed_name_sp, "dash-separated idents are not valid")
848 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
854 /// Parses `extern` for foreign ABIs modules.
856 /// `extern` is expected to have been consumed before calling this method.
860 /// ```ignore (only-for-syntax-highlight)
864 fn parse_item_foreign_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
865 let abi = self.parse_abi(); // ABI?
866 let items = self.parse_item_list(attrs, |p, at_end| p.parse_foreign_item(at_end))?;
867 let module = ast::ForeignMod { abi, items };
868 Ok((Ident::invalid(), ItemKind::ForeignMod(module)))
871 /// Parses a foreign item (one in an `extern { ... }` block).
872 pub fn parse_foreign_item(&mut self, at_end: &mut bool) -> PResult<'a, P<ForeignItem>> {
873 maybe_whole!(self, NtForeignItem, |ni| ni);
875 let mut attrs = self.parse_outer_attributes()?;
876 let lo = self.token.span;
877 let vis = self.parse_visibility(FollowedByType::No)?;
879 let (ident, kind) = if self.check_keyword(kw::Type) {
881 self.parse_item_foreign_type()?
882 } else if self.check_fn_front_matter() {
883 // FOREIGN FUNCTION ITEM
884 let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, |_| true)?;
885 (ident, ForeignItemKind::Fn(sig, generics, body))
886 } else if self.is_static_global() {
887 // FOREIGN STATIC ITEM
888 self.bump(); // `static`
889 self.parse_item_foreign_static()?
890 } else if self.token.is_keyword(kw::Const) {
891 // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
892 self.bump(); // `const`
893 self.struct_span_err(self.prev_span, "extern items cannot be `const`")
896 "try using a static value",
898 Applicability::MachineApplicable,
901 self.parse_item_foreign_static()?
902 } else if self.isnt_macro_invocation() {
903 return Err(self.missing_assoc_item_kind_err("extern", self.prev_span));
904 } else if self.token.is_path_start() {
905 let mac = self.parse_item_macro(&vis)?;
907 (Ident::invalid(), ForeignItemKind::Macro(mac))
909 self.recover_attrs_no_item(&attrs)?;
912 Ok(P(self.mk_item(lo, ident, kind, vis, attrs)))
915 /// Parses a static item from a foreign module.
916 /// Assumes that the `static` keyword is already parsed.
917 fn parse_item_foreign_static(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
918 let mutbl = self.parse_mutability();
919 let ident = self.parse_ident()?;
920 self.expect(&token::Colon)?;
921 let ty = self.parse_ty()?;
923 Ok((ident, ForeignItemKind::Static(ty, mutbl)))
926 /// Parses a type from a foreign module.
927 fn parse_item_foreign_type(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
928 self.expect_keyword(kw::Type)?;
929 let ident = self.parse_ident()?;
931 Ok((ident, ForeignItemKind::Ty))
934 fn is_static_global(&mut self) -> bool {
935 if self.check_keyword(kw::Static) {
936 // Check if this could be a closure.
937 !self.look_ahead(1, |token| {
938 if token.is_keyword(kw::Move) {
942 token::BinOp(token::Or) | token::OrOr => true,
951 /// Recover on `const mut` with `const` already eaten.
952 fn recover_const_mut(&mut self, const_span: Span) {
953 if self.eat_keyword(kw::Mut) {
954 let span = self.prev_span;
955 self.struct_span_err(span, "const globals cannot be mutable")
956 .span_label(span, "cannot be mutable")
959 "you might want to declare a static instead",
961 Applicability::MaybeIncorrect,
967 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
968 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
970 /// When `m` is `"const"`, `$ident` may also be `"_"`.
971 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
972 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
974 // Parse the type of a `const` or `static mut?` item.
975 // That is, the `":" $ty` fragment.
976 let ty = if self.token == token::Eq {
977 self.recover_missing_const_type(id, m)
979 // Not `=` so expect `":"" $ty` as usual.
980 self.expect(&token::Colon)?;
984 self.expect(&token::Eq)?;
985 let e = self.parse_expr()?;
988 Some(m) => ItemKind::Static(ty, m, e),
989 None => ItemKind::Const(ty, e),
994 /// We were supposed to parse `:` but instead, we're already at `=`.
995 /// This means that the type is missing.
996 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
997 // Construct the error and stash it away with the hope
998 // that typeck will later enrich the error with a type.
1000 Some(Mutability::Mut) => "static mut",
1001 Some(Mutability::Not) => "static",
1004 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1005 err.span_suggestion(
1007 "provide a type for the item",
1008 format!("{}: <type>", id),
1009 Applicability::HasPlaceholders,
1011 err.stash(id.span, StashKey::ItemNoType);
1013 // The user intended that the type be inferred,
1014 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1015 P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
1018 /// Parses the grammar:
1019 /// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
1020 fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
1021 let ident = self.parse_ident()?;
1022 let mut tps = self.parse_generics()?;
1023 tps.where_clause = self.parse_where_clause()?;
1024 self.expect(&token::Eq)?;
1025 let ty = self.parse_ty()?;
1026 self.expect_semi()?;
1027 Ok((ident, ty, tps))
1030 /// Parses an enum declaration.
1031 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1032 let id = self.parse_ident()?;
1033 let mut generics = self.parse_generics()?;
1034 generics.where_clause = self.parse_where_clause()?;
1037 self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1038 self.recover_stmt();
1042 let enum_definition =
1043 EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
1044 Ok((id, ItemKind::Enum(enum_definition, generics)))
1047 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1048 let variant_attrs = self.parse_outer_attributes()?;
1049 let vlo = self.token.span;
1051 let vis = self.parse_visibility(FollowedByType::No)?;
1052 if !self.recover_nested_adt_item(kw::Enum)? {
1055 let ident = self.parse_ident()?;
1057 let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
1058 // Parse a struct variant.
1059 let (fields, recovered) = self.parse_record_struct_body()?;
1060 VariantData::Struct(fields, recovered)
1061 } else if self.check(&token::OpenDelim(token::Paren)) {
1062 VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1064 VariantData::Unit(DUMMY_NODE_ID)
1068 if self.eat(&token::Eq) { Some(self.parse_anon_const_expr()?) } else { None };
1070 let vr = ast::Variant {
1074 attrs: variant_attrs,
1077 span: vlo.to(self.prev_span),
1078 is_placeholder: false,
1084 /// Parses `struct Foo { ... }`.
1085 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1086 let class_name = self.parse_ident()?;
1088 let mut generics = self.parse_generics()?;
1090 // There is a special case worth noting here, as reported in issue #17904.
1091 // If we are parsing a tuple struct it is the case that the where clause
1092 // should follow the field list. Like so:
1094 // struct Foo<T>(T) where T: Copy;
1096 // If we are parsing a normal record-style struct it is the case
1097 // that the where clause comes before the body, and after the generics.
1098 // So if we look ahead and see a brace or a where-clause we begin
1099 // parsing a record style struct.
1101 // Otherwise if we look ahead and see a paren we parse a tuple-style
1104 let vdata = if self.token.is_keyword(kw::Where) {
1105 generics.where_clause = self.parse_where_clause()?;
1106 if self.eat(&token::Semi) {
1107 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1108 VariantData::Unit(DUMMY_NODE_ID)
1110 // If we see: `struct Foo<T> where T: Copy { ... }`
1111 let (fields, recovered) = self.parse_record_struct_body()?;
1112 VariantData::Struct(fields, recovered)
1114 // No `where` so: `struct Foo<T>;`
1115 } else if self.eat(&token::Semi) {
1116 VariantData::Unit(DUMMY_NODE_ID)
1117 // Record-style struct definition
1118 } else if self.token == token::OpenDelim(token::Brace) {
1119 let (fields, recovered) = self.parse_record_struct_body()?;
1120 VariantData::Struct(fields, recovered)
1121 // Tuple-style struct definition with optional where-clause.
1122 } else if self.token == token::OpenDelim(token::Paren) {
1123 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1124 generics.where_clause = self.parse_where_clause()?;
1125 self.expect_semi()?;
1128 let token_str = super::token_descr(&self.token);
1130 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1133 let mut err = self.struct_span_err(self.token.span, msg);
1134 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1138 Ok((class_name, ItemKind::Struct(vdata, generics)))
1141 /// Parses `union Foo { ... }`.
1142 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1143 let class_name = self.parse_ident()?;
1145 let mut generics = self.parse_generics()?;
1147 let vdata = if self.token.is_keyword(kw::Where) {
1148 generics.where_clause = self.parse_where_clause()?;
1149 let (fields, recovered) = self.parse_record_struct_body()?;
1150 VariantData::Struct(fields, recovered)
1151 } else if self.token == token::OpenDelim(token::Brace) {
1152 let (fields, recovered) = self.parse_record_struct_body()?;
1153 VariantData::Struct(fields, recovered)
1155 let token_str = super::token_descr(&self.token);
1156 let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1157 let mut err = self.struct_span_err(self.token.span, msg);
1158 err.span_label(self.token.span, "expected `where` or `{` after union name");
1162 Ok((class_name, ItemKind::Union(vdata, generics)))
1165 fn parse_record_struct_body(
1167 ) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
1168 let mut fields = Vec::new();
1169 let mut recovered = false;
1170 if self.eat(&token::OpenDelim(token::Brace)) {
1171 while self.token != token::CloseDelim(token::Brace) {
1172 let field = self.parse_struct_decl_field().map_err(|e| {
1173 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1178 Ok(field) => fields.push(field),
1185 self.eat(&token::CloseDelim(token::Brace));
1187 let token_str = super::token_descr(&self.token);
1188 let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
1189 let mut err = self.struct_span_err(self.token.span, msg);
1190 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1194 Ok((fields, recovered))
1197 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
1198 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1199 // Unit like structs are handled in parse_item_struct function
1200 self.parse_paren_comma_seq(|p| {
1201 let attrs = p.parse_outer_attributes()?;
1202 let lo = p.token.span;
1203 let vis = p.parse_visibility(FollowedByType::Yes)?;
1204 let ty = p.parse_ty()?;
1206 span: lo.to(ty.span),
1212 is_placeholder: false,
1218 /// Parses an element of a struct declaration.
1219 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
1220 let attrs = self.parse_outer_attributes()?;
1221 let lo = self.token.span;
1222 let vis = self.parse_visibility(FollowedByType::No)?;
1223 self.parse_single_struct_field(lo, vis, attrs)
1226 /// Parses a structure field declaration.
1227 fn parse_single_struct_field(
1231 attrs: Vec<Attribute>,
1232 ) -> PResult<'a, StructField> {
1233 let mut seen_comma: bool = false;
1234 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1235 if self.token == token::Comma {
1238 match self.token.kind {
1242 token::CloseDelim(token::Brace) => {}
1243 token::DocComment(_) => {
1244 let previous_span = self.prev_span;
1245 let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1246 self.bump(); // consume the doc comment
1247 let comma_after_doc_seen = self.eat(&token::Comma);
1248 // `seen_comma` is always false, because we are inside doc block
1249 // condition is here to make code more readable
1250 if seen_comma == false && comma_after_doc_seen == true {
1253 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1256 if seen_comma == false {
1257 let sp = self.sess.source_map().next_point(previous_span);
1258 err.span_suggestion(
1260 "missing comma here",
1262 Applicability::MachineApplicable,
1269 let sp = self.prev_span.shrink_to_hi();
1270 let mut err = self.struct_span_err(
1272 &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1274 if self.token.is_ident() {
1275 // This is likely another field; emit the diagnostic and keep going
1276 err.span_suggestion(
1278 "try adding a comma",
1280 Applicability::MachineApplicable,
1291 /// Parses a structure field.
1292 fn parse_name_and_ty(
1296 attrs: Vec<Attribute>,
1297 ) -> PResult<'a, StructField> {
1298 let name = self.parse_ident()?;
1299 self.expect(&token::Colon)?;
1300 let ty = self.parse_ty()?;
1302 span: lo.to(self.prev_span),
1308 is_placeholder: false,
1312 /// Parses a declarative macro 2.0 definition.
1313 /// The `macro` keyword has already been parsed.
1315 /// MacBody = "{" TOKEN_STREAM "}" ;
1316 /// MacParams = "(" TOKEN_STREAM ")" ;
1317 /// DeclMac = "macro" Ident MacParams? MacBody ;
1319 fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemInfo> {
1320 let ident = self.parse_ident()?;
1321 let body = if self.check(&token::OpenDelim(token::Brace)) {
1322 self.parse_mac_args()? // `MacBody`
1323 } else if self.check(&token::OpenDelim(token::Paren)) {
1324 let params = self.parse_token_tree(); // `MacParams`
1325 let pspan = params.span();
1326 if !self.check(&token::OpenDelim(token::Brace)) {
1327 return self.unexpected();
1329 let body = self.parse_token_tree(); // `MacBody`
1330 // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
1331 let bspan = body.span();
1332 let arrow = TokenTree::token(token::FatArrow, pspan.between(bspan)); // `=>`
1333 let tokens = TokenStream::new(vec![params.into(), arrow.into(), body.into()]);
1334 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1335 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1337 return self.unexpected();
1340 self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_span));
1341 Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: false })))
1344 /// Is this unambiguously the start of a `macro_rules! foo` item defnition?
1345 fn is_macro_rules_item(&mut self) -> bool {
1346 self.check_keyword(sym::macro_rules)
1347 && self.look_ahead(1, |t| *t == token::Not)
1348 && self.look_ahead(2, |t| t.is_ident())
1351 /// Parses a legacy `macro_rules! foo { ... }` declarative macro.
1352 fn parse_item_macro_rules(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
1353 self.expect_keyword(sym::macro_rules)?; // `macro_rules`
1354 self.expect(&token::Not)?; // `!`
1356 let ident = self.parse_ident()?;
1357 let body = self.parse_mac_args()?;
1358 self.eat_semi_for_macro_if_needed(&body);
1359 self.complain_if_pub_macro(vis, true);
1361 Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true })))
1364 /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
1365 /// If that's not the case, emit an error.
1366 fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
1367 if let VisibilityKind::Inherited = vis.node {
1371 let vstr = pprust::vis_to_string(vis);
1372 let vstr = vstr.trim_end();
1374 let msg = format!("can't qualify macro_rules invocation with `{}`", vstr);
1375 self.struct_span_err(vis.span, &msg)
1378 "try exporting the macro",
1379 "#[macro_export]".to_owned(),
1380 Applicability::MaybeIncorrect, // speculative
1384 self.struct_span_err(vis.span, "can't qualify macro invocation with `pub`")
1387 "remove the visibility",
1389 Applicability::MachineApplicable,
1391 .help(&format!("try adjusting the macro to put `{}` inside the invocation", vstr))
1396 fn eat_semi_for_macro_if_needed(&mut self, args: &MacArgs) {
1397 if args.need_semicolon() && !self.eat(&token::Semi) {
1398 self.report_invalid_macro_expansion_item(args);
1402 fn report_invalid_macro_expansion_item(&self, args: &MacArgs) {
1403 let mut err = self.struct_span_err(
1405 "macros that expand to items must be delimited with braces or followed by a semicolon",
1407 if self.unclosed_delims.is_empty() {
1408 let DelimSpan { open, close } = match args {
1409 MacArgs::Empty | MacArgs::Eq(..) => unreachable!(),
1410 MacArgs::Delimited(dspan, ..) => *dspan,
1412 err.multipart_suggestion(
1413 "change the delimiters to curly braces",
1414 vec![(open, "{".to_string()), (close, '}'.to_string())],
1415 Applicability::MaybeIncorrect,
1418 err.span_suggestion(
1420 "change the delimiters to curly braces",
1421 " { /* items */ }".to_string(),
1422 Applicability::HasPlaceholders,
1425 err.span_suggestion(
1426 self.prev_span.shrink_to_hi(),
1429 Applicability::MaybeIncorrect,
1434 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1435 /// it is, we try to parse the item and report error about nested types.
1436 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1437 if (self.token.is_keyword(kw::Enum)
1438 || self.token.is_keyword(kw::Struct)
1439 || self.token.is_keyword(kw::Union))
1440 && self.look_ahead(1, |t| t.is_ident())
1442 let kw_token = self.token.clone();
1443 let kw_str = pprust::token_to_string(&kw_token);
1444 let item = self.parse_item()?;
1446 self.struct_span_err(
1448 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1452 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1454 Applicability::MaybeIncorrect,
1457 // We successfully parsed the item but we must inform the caller about nested problem.
1469 attrs: Vec<Attribute>,
1471 let span = lo.to(self.prev_span);
1472 Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None }
1476 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1478 /// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1479 type ReqName = fn(&token::Token) -> bool;
1481 /// Parsing of functions and methods.
1482 impl<'a> Parser<'a> {
1483 /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
1487 attrs: &mut Vec<Attribute>,
1489 ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
1490 let header = self.parse_fn_front_matter()?; // `const ... fn`
1491 let ident = self.parse_ident()?; // `foo`
1492 let mut generics = self.parse_generics()?; // `<'a, T, ...>`
1493 let decl = self.parse_fn_decl(req_name, AllowPlus::Yes)?; // `(p: u8, ...)`
1494 generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
1495 let body = self.parse_fn_body(at_end, attrs)?; // `;` or `{ ... }`.
1496 Ok((ident, FnSig { header, decl }, generics, body))
1499 /// Parse the "body" of a function.
1500 /// This can either be `;` when there's no body,
1501 /// or e.g. a block when the function is a provided one.
1505 attrs: &mut Vec<Attribute>,
1506 ) -> PResult<'a, Option<P<Block>>> {
1507 let (inner_attrs, body) = match self.token.kind {
1512 token::OpenDelim(token::Brace) => {
1513 let (attrs, body) = self.parse_inner_attrs_and_block()?;
1516 token::Interpolated(ref nt) => match **nt {
1517 token::NtBlock(..) => {
1518 let (attrs, body) = self.parse_inner_attrs_and_block()?;
1521 _ => return self.expected_semi_or_open_brace(),
1523 _ => return self.expected_semi_or_open_brace(),
1525 attrs.extend(inner_attrs);
1530 /// Is the current token the start of an `FnHeader` / not a valid parse?
1531 fn check_fn_front_matter(&mut self) -> bool {
1532 // We use an over-approximation here.
1533 // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
1534 const QUALS: [Symbol; 4] = [kw::Const, kw::Async, kw::Unsafe, kw::Extern];
1535 self.check_keyword(kw::Fn) // Definitely an `fn`.
1536 // `$qual fn` or `$qual $qual`:
1537 || QUALS.iter().any(|&kw| self.check_keyword(kw))
1538 && self.look_ahead(1, |t| {
1539 // ...qualified and then `fn`, e.g. `const fn`.
1540 t.is_keyword(kw::Fn)
1541 // Two qualifiers. This is enough. Due `async` we need to check that it's reserved.
1542 || t.is_non_raw_ident_where(|i| QUALS.contains(&i.name) && i.is_reserved())
1545 || self.check_keyword(kw::Extern)
1546 && self.look_ahead(1, |t| t.can_begin_literal_or_bool())
1547 && self.look_ahead(2, |t| t.is_keyword(kw::Fn))
1550 /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
1551 /// up to and including the `fn` keyword. The formal grammar is:
1554 /// Extern = "extern" StringLit ;
1555 /// FnQual = "const"? "async"? "unsafe"? Extern? ;
1556 /// FnFrontMatter = FnQual? "fn" ;
1558 fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1559 let constness = self.parse_constness();
1560 let asyncness = self.parse_asyncness();
1561 let unsafety = self.parse_unsafety();
1562 let ext = self.parse_extern()?;
1564 if let Async::Yes { span, .. } = asyncness {
1565 self.ban_async_in_2015(span);
1568 if !self.eat_keyword(kw::Fn) {
1569 // It is possible for `expect_one_of` to recover given the contents of
1570 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1571 // account for this.
1572 if !self.expect_one_of(&[], &[])? {
1577 Ok(FnHeader { constness, unsafety, asyncness, ext })
1580 /// We are parsing `async fn`. If we are on Rust 2015, emit an error.
1581 fn ban_async_in_2015(&self, span: Span) {
1582 if span.rust_2015() {
1583 let diag = self.diagnostic();
1584 struct_span_err!(diag, span, E0670, "`async fn` is not permitted in the 2015 edition")
1585 .note("to use `async fn`, switch to Rust 2018")
1586 .help("set `edition = \"2018\"` in `Cargo.toml`")
1587 .note("for more on editions, read https://doc.rust-lang.org/edition-guide")
1592 /// Parses the parameter list and result type of a function declaration.
1593 pub(super) fn parse_fn_decl(
1596 ret_allow_plus: AllowPlus,
1597 ) -> PResult<'a, P<FnDecl>> {
1599 inputs: self.parse_fn_params(req_name)?,
1600 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
1604 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1605 fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
1606 let mut first_param = true;
1607 // Parse the arguments, starting out with `self` being allowed...
1608 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1609 let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
1611 let lo = p.prev_span;
1612 // Skip every token until next possible arg or end.
1613 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1614 // Create a placeholder argument for proper arg count (issue #34264).
1615 Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
1617 // ...now that we've parsed the first argument, `self` is no longer allowed.
1618 first_param = false;
1621 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1622 self.deduplicate_recovered_params_names(&mut params);
1626 /// Parses a single function parameter.
1628 /// - `self` is syntactically allowed when `first_param` holds.
1629 fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
1630 let lo = self.token.span;
1631 let attrs = self.parse_outer_attributes()?;
1633 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1634 if let Some(mut param) = self.parse_self_param()? {
1635 param.attrs = attrs.into();
1636 return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
1639 let is_name_required = match self.token.kind {
1640 token::DotDotDot => false,
1641 _ => req_name(&self.token),
1643 let (pat, ty) = if is_name_required || self.is_named_param() {
1644 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1646 let pat = self.parse_fn_param_pat()?;
1647 if let Err(mut err) = self.expect(&token::Colon) {
1648 return if let Some(ident) =
1649 self.parameter_without_type(&mut err, pat, is_name_required, first_param)
1652 Ok(dummy_arg(ident))
1658 self.eat_incorrect_doc_comment_for_param_type();
1659 (pat, self.parse_ty_for_param()?)
1661 debug!("parse_param_general ident_to_pat");
1662 let parser_snapshot_before_ty = self.clone();
1663 self.eat_incorrect_doc_comment_for_param_type();
1664 let mut ty = self.parse_ty_for_param();
1666 && self.token != token::Comma
1667 && self.token != token::CloseDelim(token::Paren)
1669 // This wasn't actually a type, but a pattern looking like a type,
1670 // so we are going to rollback and re-parse for recovery.
1671 ty = self.unexpected();
1675 let ident = Ident::new(kw::Invalid, self.prev_span);
1676 let bm = BindingMode::ByValue(Mutability::Not);
1677 let pat = self.mk_pat_ident(ty.span, bm, ident);
1680 // If this is a C-variadic argument and we hit an error, return the error.
1681 Err(err) if self.token == token::DotDotDot => return Err(err),
1682 // Recover from attempting to parse the argument as a type without pattern.
1685 mem::replace(self, parser_snapshot_before_ty);
1686 self.recover_arg_parse()?
1691 let span = lo.to(self.token.span);
1694 attrs: attrs.into(),
1695 id: ast::DUMMY_NODE_ID,
1696 is_placeholder: false,
1703 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1704 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1705 // Extract an identifier *after* having confirmed that the token is one.
1706 let expect_self_ident = |this: &mut Self| {
1707 match this.token.kind {
1708 // Preserve hygienic context.
1709 token::Ident(name, _) => {
1710 let span = this.token.span;
1712 Ident::new(name, span)
1714 _ => unreachable!(),
1717 // Is `self` `n` tokens ahead?
1718 let is_isolated_self = |this: &Self, n| {
1719 this.is_keyword_ahead(n, &[kw::SelfLower])
1720 && this.look_ahead(n + 1, |t| t != &token::ModSep)
1722 // Is `mut self` `n` tokens ahead?
1723 let is_isolated_mut_self =
1724 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
1725 // Parse `self` or `self: TYPE`. We already know the current token is `self`.
1726 let parse_self_possibly_typed = |this: &mut Self, m| {
1727 let eself_ident = expect_self_ident(this);
1728 let eself_hi = this.prev_span;
1729 let eself = if this.eat(&token::Colon) {
1730 SelfKind::Explicit(this.parse_ty()?, m)
1734 Ok((eself, eself_ident, eself_hi))
1736 // Recover for the grammar `*self`, `*const self`, and `*mut self`.
1737 let recover_self_ptr = |this: &mut Self| {
1738 let msg = "cannot pass `self` by raw pointer";
1739 let span = this.token.span;
1740 this.struct_span_err(span, msg).span_label(span, msg).emit();
1742 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_span))
1745 // Parse optional `self` parameter of a method.
1746 // Only a limited set of initial token sequences is considered `self` parameters; anything
1747 // else is parsed as a normal function parameter list, so some lookahead is required.
1748 let eself_lo = self.token.span;
1749 let (eself, eself_ident, eself_hi) = match self.token.kind {
1750 token::BinOp(token::And) => {
1751 let eself = if is_isolated_self(self, 1) {
1754 SelfKind::Region(None, Mutability::Not)
1755 } else if is_isolated_mut_self(self, 1) {
1759 SelfKind::Region(None, Mutability::Mut)
1760 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
1763 let lt = self.expect_lifetime();
1764 SelfKind::Region(Some(lt), Mutability::Not)
1765 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
1768 let lt = self.expect_lifetime();
1770 SelfKind::Region(Some(lt), Mutability::Mut)
1775 (eself, expect_self_ident(self), self.prev_span)
1778 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
1780 recover_self_ptr(self)?
1782 // `*mut self` and `*const self`
1783 token::BinOp(token::Star)
1784 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
1788 recover_self_ptr(self)?
1790 // `self` and `self: TYPE`
1791 token::Ident(..) if is_isolated_self(self, 0) => {
1792 parse_self_possibly_typed(self, Mutability::Not)?
1794 // `mut self` and `mut self: TYPE`
1795 token::Ident(..) if is_isolated_mut_self(self, 0) => {
1797 parse_self_possibly_typed(self, Mutability::Mut)?
1799 _ => return Ok(None),
1802 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
1803 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
1806 fn is_named_param(&self) -> bool {
1807 let offset = match self.token.kind {
1808 token::Interpolated(ref nt) => match **nt {
1809 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
1812 token::BinOp(token::And) | token::AndAnd => 1,
1813 _ if self.token.is_keyword(kw::Mut) => 1,
1817 self.look_ahead(offset, |t| t.is_ident())
1818 && self.look_ahead(offset + 1, |t| t == &token::Colon)
1821 fn recover_first_param(&mut self) -> &'static str {
1823 .parse_outer_attributes()
1824 .and_then(|_| self.parse_self_param())
1825 .map_err(|mut e| e.cancel())
1827 Ok(Some(_)) => "method",