1 use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
2 use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
3 use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, TrailingToken};
7 use rustc_ast::token::{self, TokenKind};
8 use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
9 use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID};
10 use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind};
11 use rustc_ast::{BindingMode, Block, FnDecl, FnSig, Param, SelfKind};
12 use rustc_ast::{EnumDef, FieldDef, Generics, TraitRef, Ty, TyKind, Variant, VariantData};
13 use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
14 use rustc_ast::{MacArgs, MacCall, MacDelimiter};
15 use rustc_ast_pretty::pprust;
16 use rustc_errors::{struct_span_err, Applicability, PResult, StashKey};
17 use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
18 use rustc_span::source_map::{self, Span};
19 use rustc_span::symbol::{kw, sym, Ident, Symbol};
21 use std::convert::TryFrom;
26 /// Parses a source module as a crate. This is the main entry point for the parser.
27 pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
28 let (attrs, items, span) = self.parse_mod(&token::Eof)?;
29 Ok(ast::Crate { attrs, items, span })
32 /// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
33 fn parse_item_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
34 let unsafety = self.parse_unsafety();
35 self.expect_keyword(kw::Mod)?;
36 let id = self.parse_ident()?;
37 let mod_kind = if self.eat(&token::Semi) {
40 self.expect(&token::OpenDelim(token::Brace))?;
41 let (mut inner_attrs, items, inner_span) =
42 self.parse_mod(&token::CloseDelim(token::Brace))?;
43 attrs.append(&mut inner_attrs);
44 ModKind::Loaded(items, Inline::Yes, inner_span)
46 Ok((id, ItemKind::Mod(unsafety, mod_kind)))
49 /// Parses the contents of a module (inner attributes followed by module items).
53 ) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> {
54 let lo = self.token.span;
55 let attrs = self.parse_inner_attributes()?;
57 let mut items = vec![];
58 while let Some(item) = self.parse_item(ForceCollect::No)? {
60 self.maybe_consume_incorrect_semicolon(&items);
64 let token_str = super::token_descr(&self.token);
65 if !self.maybe_consume_incorrect_semicolon(&items) {
66 let msg = &format!("expected item, found {}", token_str);
67 let mut err = self.struct_span_err(self.token.span, msg);
68 err.span_label(self.token.span, "expected item");
73 Ok((attrs, items, lo.to(self.prev_token.span)))
77 pub(super) type ItemInfo = (Ident, ItemKind);
80 pub fn parse_item(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<P<Item>>> {
81 self.parse_item_(|_| true, force_collect).map(|i| i.map(P))
87 force_collect: ForceCollect,
88 ) -> PResult<'a, Option<Item>> {
89 let attrs = self.parse_outer_attributes()?;
90 self.parse_item_common(attrs, true, false, req_name, force_collect)
93 pub(super) fn parse_item_common(
99 force_collect: ForceCollect,
100 ) -> PResult<'a, Option<Item>> {
101 // Don't use `maybe_whole` so that we have precise control
102 // over when we bump the parser
103 if let token::Interpolated(nt) = &self.token.kind {
104 if let token::NtItem(item) = &**nt {
105 let mut item = item.clone();
108 attrs.prepend_to_nt_inner(&mut item.attrs);
109 return Ok(Some(item.into_inner()));
113 let mut unclosed_delims = vec![];
115 self.collect_tokens_trailing_token(attrs, force_collect, |this: &mut Self, attrs| {
116 let item = this.parse_item_common_(attrs, mac_allowed, attrs_allowed, req_name);
117 unclosed_delims.append(&mut this.unclosed_delims);
118 Ok((item?, TrailingToken::None))
121 self.unclosed_delims.append(&mut unclosed_delims);
125 fn parse_item_common_(
127 mut attrs: Vec<Attribute>,
131 ) -> PResult<'a, Option<Item>> {
132 let lo = self.token.span;
133 let vis = self.parse_visibility(FollowedByType::No)?;
134 let mut def = self.parse_defaultness();
135 let kind = self.parse_item_kind(&mut attrs, mac_allowed, lo, &vis, &mut def, req_name)?;
136 if let Some((ident, kind)) = kind {
137 self.error_on_unconsumed_default(def, &kind);
138 let span = lo.to(self.prev_token.span);
139 let id = DUMMY_NODE_ID;
140 let item = Item { ident, attrs, id, kind, vis, span, tokens: None };
141 return Ok(Some(item));
144 // At this point, we have failed to parse an item.
145 self.error_on_unmatched_vis(&vis);
146 self.error_on_unmatched_defaultness(def);
148 self.recover_attrs_no_item(&attrs)?;
153 /// Error in-case a non-inherited visibility was parsed but no item followed.
154 fn error_on_unmatched_vis(&self, vis: &Visibility) {
155 if let VisibilityKind::Inherited = vis.kind {
158 let vs = pprust::vis_to_string(&vis);
159 let vs = vs.trim_end();
160 self.struct_span_err(vis.span, &format!("visibility `{}` is not followed by an item", vs))
161 .span_label(vis.span, "the visibility")
162 .help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
166 /// Error in-case a `default` was parsed but no item followed.
167 fn error_on_unmatched_defaultness(&self, def: Defaultness) {
168 if let Defaultness::Default(sp) = def {
169 self.struct_span_err(sp, "`default` is not followed by an item")
170 .span_label(sp, "the `default` qualifier")
171 .note("only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`")
176 /// Error in-case `default` was parsed in an in-appropriate context.
177 fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
178 if let Defaultness::Default(span) = def {
179 let msg = format!("{} {} cannot be `default`", kind.article(), kind.descr());
180 self.struct_span_err(span, &msg)
181 .span_label(span, "`default` because of this")
182 .note("only associated `fn`, `const`, and `type` items can be `default`")
187 /// Parses one of the items allowed by the flags.
190 attrs: &mut Vec<Attribute>,
191 macros_allowed: bool,
194 def: &mut Defaultness,
196 ) -> PResult<'a, Option<ItemInfo>> {
197 let def_final = def == &Defaultness::Final;
198 let mut def = || mem::replace(def, Defaultness::Final);
200 let info = if self.eat_keyword(kw::Use) {
202 let tree = self.parse_use_tree()?;
204 // If wildcard or glob-like brace syntax doesn't have `;`,
205 // the user may not know `*` or `{}` should be the last.
206 if let Err(mut e) = self.expect_semi() {
208 UseTreeKind::Glob => {
209 e.note("the wildcard token must be last on the path").emit();
211 UseTreeKind::Nested(..) => {
212 e.note("glob-like brace syntax must be last on the path").emit();
219 (Ident::empty(), ItemKind::Use(tree))
220 } else if self.check_fn_front_matter(def_final) {
222 let (ident, sig, generics, body) = self.parse_fn(attrs, req_name, lo)?;
223 (ident, ItemKind::Fn(Box::new(FnKind(def(), sig, generics, body))))
224 } else if self.eat_keyword(kw::Extern) {
225 if self.eat_keyword(kw::Crate) {
227 self.parse_item_extern_crate()?
230 self.parse_item_foreign_mod(attrs, Unsafe::No)?
232 } else if self.is_unsafe_foreign_mod() {
234 let unsafety = self.parse_unsafety();
235 self.expect_keyword(kw::Extern)?;
236 self.parse_item_foreign_mod(attrs, unsafety)?
237 } else if self.is_static_global() {
239 self.bump(); // `static`
240 let m = self.parse_mutability();
241 let (ident, ty, expr) = self.parse_item_global(Some(m))?;
242 (ident, ItemKind::Static(ty, m, expr))
243 } else if let Const::Yes(const_span) = self.parse_constness() {
245 if self.token.is_keyword(kw::Impl) {
246 // recover from `const impl`, suggest `impl const`
247 self.recover_const_impl(const_span, attrs, def())?
249 self.recover_const_mut(const_span);
250 let (ident, ty, expr) = self.parse_item_global(None)?;
251 (ident, ItemKind::Const(def(), ty, expr))
253 } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() {
255 self.parse_item_trait(attrs, lo)?
256 } else if self.check_keyword(kw::Impl)
257 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
260 self.parse_item_impl(attrs, def())?
261 } else if self.check_keyword(kw::Mod)
262 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Mod])
265 self.parse_item_mod(attrs)?
266 } else if self.eat_keyword(kw::Type) {
268 self.parse_type_alias(def())?
269 } else if self.eat_keyword(kw::Enum) {
271 self.parse_item_enum()?
272 } else if self.eat_keyword(kw::Struct) {
274 self.parse_item_struct()?
275 } else if self.is_kw_followed_by_ident(kw::Union) {
277 self.bump(); // `union`
278 self.parse_item_union()?
279 } else if self.eat_keyword(kw::Macro) {
281 self.parse_item_decl_macro(lo)?
282 } else if self.is_macro_rules_item() {
284 self.parse_item_macro_rules(vis)?
285 } else if vis.kind.is_pub() && self.isnt_macro_invocation() {
286 self.recover_missing_kw_before_item()?;
288 } else if macros_allowed && self.check_path() {
289 // MACRO INVOCATION ITEM
290 (Ident::empty(), ItemKind::MacCall(self.parse_item_macro(vis)?))
297 /// When parsing a statement, would the start of a path be an item?
298 pub(super) fn is_path_start_item(&mut self) -> bool {
299 self.is_crate_vis() // no: `crate::b`, yes: `crate $item`
300 || self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
301 || self.check_auto_or_unsafe_trait_item() // no: `auto::b`, yes: `auto trait X { .. }`
302 || self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
303 || self.is_macro_rules_item() // no: `macro_rules::b`, yes: `macro_rules! mac`
306 /// Are we sure this could not possibly be a macro invocation?
307 fn isnt_macro_invocation(&mut self) -> bool {
308 self.check_ident() && self.look_ahead(1, |t| *t != token::Not && *t != token::ModSep)
311 /// Recover on encountering a struct or method definition where the user
312 /// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
313 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
314 // Space between `pub` keyword and the identifier
317 // ^^^ `sp` points here
318 let sp = self.prev_token.span.between(self.token.span);
319 let full_sp = self.prev_token.span.to(self.token.span);
320 let ident_sp = self.token.span;
321 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
322 // possible public struct definition where `struct` was forgotten
323 let ident = self.parse_ident().unwrap();
324 let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
325 let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
326 err.span_suggestion_short(
330 Applicability::MaybeIncorrect, // speculative
333 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
334 let ident = self.parse_ident().unwrap();
336 let kw_name = self.recover_first_param();
337 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
338 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
339 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
341 ("fn", kw_name, false)
342 } else if self.check(&token::OpenDelim(token::Brace)) {
344 ("fn", kw_name, false)
345 } else if self.check(&token::Colon) {
349 ("fn` or `struct", "function or struct", true)
352 let msg = format!("missing `{}` for {} definition", kw, kw_name);
353 let mut err = self.struct_span_err(sp, &msg);
355 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
357 format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
358 err.span_suggestion_short(
362 Applicability::MachineApplicable,
364 } else if let Ok(snippet) = self.span_to_snippet(ident_sp) {
367 "if you meant to call a macro, try",
368 format!("{}!", snippet),
369 // this is the `ambiguous` conditional branch
370 Applicability::MaybeIncorrect,
374 "if you meant to call a macro, remove the `pub` \
375 and add a trailing `!` after the identifier",
379 } else if self.look_ahead(1, |t| *t == token::Lt) {
380 let ident = self.parse_ident().unwrap();
381 self.eat_to_tokens(&[&token::Gt]);
383 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
384 ("fn", self.recover_first_param(), false)
385 } else if self.check(&token::OpenDelim(token::Brace)) {
386 ("struct", "struct", false)
388 ("fn` or `struct", "function or struct", true)
390 let msg = format!("missing `{}` for {} definition", kw, kw_name);
391 let mut err = self.struct_span_err(sp, &msg);
393 err.span_suggestion_short(
395 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
397 Applicability::MachineApplicable,
406 /// Parses an item macro, e.g., `item!();`.
407 fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
408 let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
409 self.expect(&token::Not)?; // `!`
410 let args = self.parse_mac_args()?; // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
411 self.eat_semi_for_macro_if_needed(&args);
412 self.complain_if_pub_macro(vis, false);
413 Ok(MacCall { path, args, prior_type_ascription: self.last_type_ascription })
416 /// Recover if we parsed attributes and expected an item but there was none.
417 fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
418 let (start, end) = match attrs {
420 [x0 @ xn] | [x0, .., xn] => (x0, xn),
422 let msg = if end.is_doc_comment() {
423 "expected item after doc comment"
425 "expected item after attributes"
427 let mut err = self.struct_span_err(end.span, msg);
428 if end.is_doc_comment() {
429 err.span_label(end.span, "this doc comment doesn't document anything");
431 if let [.., penultimate, _] = attrs {
432 err.span_label(start.span.to(penultimate.span), "other attributes here");
437 fn is_async_fn(&self) -> bool {
438 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
441 fn parse_polarity(&mut self) -> ast::ImplPolarity {
442 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
443 if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
445 ast::ImplPolarity::Negative(self.prev_token.span)
447 ast::ImplPolarity::Positive
451 /// Parses an implementation item.
454 /// impl<'a, T> TYPE { /* impl items */ }
455 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
456 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
457 /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
460 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
462 /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
463 /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
467 attrs: &mut Vec<Attribute>,
468 defaultness: Defaultness,
469 ) -> PResult<'a, ItemInfo> {
470 let unsafety = self.parse_unsafety();
471 self.expect_keyword(kw::Impl)?;
473 // First, parse generic parameters if necessary.
474 let mut generics = if self.choose_generics_over_qpath(0) {
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_token.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 let polarity = self.parse_polarity();
491 // Parse both types and traits as a type, then reinterpret if necessary.
492 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Empty, span));
493 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
495 let span = self.prev_token.span.between(self.token.span);
496 self.struct_span_err(span, "missing trait in a trait impl")
501 Applicability::HasPlaceholders,
504 span.to(self.token.span),
505 "for an inherent impl, drop this `for`",
507 Applicability::MaybeIncorrect,
511 kind: TyKind::Path(None, err_path(span)),
520 // If `for` is missing we try to recover.
521 let has_for = self.eat_keyword(kw::For);
522 let missing_for_span = self.prev_token.span.between(self.token.span);
524 let ty_second = if self.token == token::DotDot {
525 // We need to report this error after `cfg` expansion for compatibility reasons
526 self.bump(); // `..`, do not add it to expected tokens
527 Some(self.mk_ty(self.prev_token.span, TyKind::Err))
528 } else if has_for || self.token.can_begin_type() {
529 Some(self.parse_ty()?)
534 generics.where_clause = self.parse_where_clause()?;
536 let impl_items = self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?;
538 let item_kind = match ty_second {
540 // impl Trait for Type
542 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
543 .span_suggestion_short(
547 Applicability::MachineApplicable,
552 let ty_first = ty_first.into_inner();
553 let path = match ty_first.kind {
554 // This notably includes paths passed through `ty` macro fragments (#46438).
555 TyKind::Path(None, path) => path,
557 self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
558 err_path(ty_first.span)
561 let trait_ref = TraitRef { path, ref_id: ty_first.id };
563 ItemKind::Impl(Box::new(ImplKind {
569 of_trait: Some(trait_ref),
576 ItemKind::Impl(Box::new(ImplKind {
589 Ok((Ident::empty(), item_kind))
592 fn parse_item_list<T>(
594 attrs: &mut Vec<Attribute>,
595 mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
596 ) -> PResult<'a, Vec<T>> {
597 let open_brace_span = self.token.span;
598 self.expect(&token::OpenDelim(token::Brace))?;
599 attrs.append(&mut self.parse_inner_attributes()?);
601 let mut items = Vec::new();
602 while !self.eat(&token::CloseDelim(token::Brace)) {
603 if self.recover_doc_comment_before_brace() {
606 match parse_item(self) {
608 // We have to bail or we'll potentially never make progress.
609 let non_item_span = self.token.span;
610 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
611 self.struct_span_err(non_item_span, "non-item in item list")
612 .span_label(open_brace_span, "item list starts here")
613 .span_label(non_item_span, "non-item starts here")
614 .span_label(self.prev_token.span, "item list ends here")
618 Ok(Some(item)) => items.extend(item),
620 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
621 err.span_label(open_brace_span, "while parsing this item list starting here")
622 .span_label(self.prev_token.span, "the item list ends here")
631 /// Recover on a doc comment before `}`.
632 fn recover_doc_comment_before_brace(&mut self) -> bool {
633 if let token::DocComment(..) = self.token.kind {
634 if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
639 "found a documentation comment that doesn't document anything",
641 .span_label(self.token.span, "this doc comment doesn't document anything")
643 "doc comments must come before what they document, maybe a \
644 comment was intended with `//`?",
654 /// Parses defaultness (i.e., `default` or nothing).
655 fn parse_defaultness(&mut self) -> Defaultness {
656 // We are interested in `default` followed by another identifier.
657 // However, we must avoid keywords that occur as binary operators.
658 // Currently, the only applicable keyword is `as` (`default as Ty`).
659 if self.check_keyword(kw::Default)
660 && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
662 self.bump(); // `default`
663 Defaultness::Default(self.prev_token.uninterpolated_span())
669 /// Is this an `(unsafe auto? | auto) trait` item?
670 fn check_auto_or_unsafe_trait_item(&mut self) -> bool {
672 self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait])
674 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
677 /// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
678 fn parse_item_trait(&mut self, attrs: &mut Vec<Attribute>, lo: Span) -> PResult<'a, ItemInfo> {
679 let unsafety = self.parse_unsafety();
680 // Parse optional `auto` prefix.
681 let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
683 self.expect_keyword(kw::Trait)?;
684 let ident = self.parse_ident()?;
685 let mut tps = self.parse_generics()?;
687 // Parse optional colon and supertrait bounds.
688 let had_colon = self.eat(&token::Colon);
689 let span_at_colon = self.prev_token.span;
690 let bounds = if had_colon {
691 self.parse_generic_bounds(Some(self.prev_token.span))?
696 let span_before_eq = self.prev_token.span;
697 if self.eat(&token::Eq) {
698 // It's a trait alias.
700 let span = span_at_colon.to(span_before_eq);
701 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
704 let bounds = self.parse_generic_bounds(None)?;
705 tps.where_clause = self.parse_where_clause()?;
708 let whole_span = lo.to(self.prev_token.span);
709 if is_auto == IsAuto::Yes {
710 let msg = "trait aliases cannot be `auto`";
711 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
713 if let Unsafe::Yes(_) = unsafety {
714 let msg = "trait aliases cannot be `unsafe`";
715 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
718 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
720 Ok((ident, ItemKind::TraitAlias(tps, bounds)))
722 // It's a normal trait.
723 tps.where_clause = self.parse_where_clause()?;
724 let items = self.parse_item_list(attrs, |p| p.parse_trait_item(ForceCollect::No))?;
725 Ok((ident, ItemKind::Trait(Box::new(TraitKind(is_auto, unsafety, tps, bounds, items)))))
729 pub fn parse_impl_item(
731 force_collect: ForceCollect,
732 ) -> PResult<'a, Option<Option<P<AssocItem>>>> {
733 self.parse_assoc_item(|_| true, force_collect)
736 pub fn parse_trait_item(
738 force_collect: ForceCollect,
739 ) -> PResult<'a, Option<Option<P<AssocItem>>>> {
740 self.parse_assoc_item(|edition| edition >= Edition::Edition2018, force_collect)
743 /// Parses associated items.
747 force_collect: ForceCollect,
748 ) -> PResult<'a, Option<Option<P<AssocItem>>>> {
749 Ok(self.parse_item_(req_name, force_collect)?.map(
750 |Item { attrs, id, span, vis, ident, kind, tokens }| {
751 let kind = match AssocItemKind::try_from(kind) {
753 Err(kind) => match kind {
754 ItemKind::Static(a, _, b) => {
755 self.struct_span_err(span, "associated `static` items are not allowed")
757 AssocItemKind::Const(Defaultness::Final, a, b)
759 _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
762 Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
767 /// Parses a `type` alias with the following grammar:
769 /// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
771 /// The `"type"` has already been eaten.
772 fn parse_type_alias(&mut self, def: Defaultness) -> PResult<'a, ItemInfo> {
773 let ident = self.parse_ident()?;
774 let mut generics = self.parse_generics()?;
776 // Parse optional colon and param bounds.
778 if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
779 generics.where_clause = self.parse_where_clause()?;
781 let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
784 Ok((ident, ItemKind::TyAlias(Box::new(TyAliasKind(def, generics, bounds, default)))))
787 /// Parses a `UseTree`.
790 /// USE_TREE = [`::`] `*` |
791 /// [`::`] `{` USE_TREE_LIST `}` |
793 /// PATH `::` `{` USE_TREE_LIST `}` |
794 /// PATH [`as` IDENT]
796 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
797 let lo = self.token.span;
799 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo(), tokens: None };
800 let kind = if self.check(&token::OpenDelim(token::Brace))
801 || self.check(&token::BinOp(token::Star))
802 || self.is_import_coupler()
804 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
805 let mod_sep_ctxt = self.token.span.ctxt();
806 if self.eat(&token::ModSep) {
809 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
812 self.parse_use_tree_glob_or_nested()?
814 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
815 prefix = self.parse_path(PathStyle::Mod)?;
817 if self.eat(&token::ModSep) {
818 self.parse_use_tree_glob_or_nested()?
820 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
824 Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
827 /// Parses `*` or `{...}`.
828 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
829 Ok(if self.eat(&token::BinOp(token::Star)) {
832 UseTreeKind::Nested(self.parse_use_tree_list()?)
836 /// Parses a `UseTreeKind::Nested(list)`.
839 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
841 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
842 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
846 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
847 if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
850 fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
851 match self.token.ident() {
852 Some((ident @ Ident { name: kw::Underscore, .. }, false)) => {
856 _ => self.parse_ident(),
860 /// Parses `extern crate` links.
865 /// extern crate foo;
866 /// extern crate bar as foo;
868 fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemInfo> {
869 // Accept `extern crate name-like-this` for better diagnostics
870 let orig_name = self.parse_crate_name_with_dashes()?;
871 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
872 (rename, Some(orig_name.name))
877 Ok((item_name, ItemKind::ExternCrate(orig_name)))
880 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
881 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
882 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
884 let mut ident = if self.token.is_keyword(kw::SelfLower) {
885 self.parse_path_segment_ident()
889 let mut idents = vec![];
890 let mut replacement = vec![];
891 let mut fixed_crate_name = false;
892 // Accept `extern crate name-like-this` for better diagnostics.
893 let dash = token::BinOp(token::BinOpToken::Minus);
894 if self.token == dash {
895 // Do not include `-` as part of the expected tokens list.
896 while self.eat(&dash) {
897 fixed_crate_name = true;
898 replacement.push((self.prev_token.span, "_".to_string()));
899 idents.push(self.parse_ident()?);
902 if fixed_crate_name {
903 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
904 let mut fixed_name = format!("{}", ident.name);
906 fixed_name.push_str(&format!("_{}", part.name));
908 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
910 self.struct_span_err(fixed_name_sp, error_msg)
911 .span_label(fixed_name_sp, "dash-separated idents are not valid")
912 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
918 /// Parses `extern` for foreign ABIs modules.
920 /// `extern` is expected to have been consumed before calling this method.
924 /// ```ignore (only-for-syntax-highlight)
928 fn parse_item_foreign_mod(
930 attrs: &mut Vec<Attribute>,
932 ) -> PResult<'a, ItemInfo> {
933 let abi = self.parse_abi(); // ABI?
934 let items = self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?;
935 let module = ast::ForeignMod { unsafety, abi, items };
936 Ok((Ident::empty(), ItemKind::ForeignMod(module)))
939 /// Parses a foreign item (one in an `extern { ... }` block).
940 pub fn parse_foreign_item(
942 force_collect: ForceCollect,
943 ) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
944 Ok(self.parse_item_(|_| true, force_collect)?.map(
945 |Item { attrs, id, span, vis, ident, kind, tokens }| {
946 let kind = match ForeignItemKind::try_from(kind) {
948 Err(kind) => match kind {
949 ItemKind::Const(_, a, b) => {
950 self.error_on_foreign_const(span, ident);
951 ForeignItemKind::Static(a, Mutability::Not, b)
953 _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
956 Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
961 fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
962 let span = self.sess.source_map().guess_head_span(span);
963 let descr = kind.descr();
964 self.struct_span_err(span, &format!("{} is not supported in {}", descr, ctx))
965 .help(&format!("consider moving the {} out to a nearby module scope", descr))
970 fn error_on_foreign_const(&self, span: Span, ident: Ident) {
971 self.struct_span_err(ident.span, "extern items cannot be `const`")
973 span.with_hi(ident.span.lo()),
974 "try using a static value",
975 "static ".to_string(),
976 Applicability::MachineApplicable,
978 .note("for more information, visit https://doc.rust-lang.org/std/keyword.extern.html")
982 fn is_unsafe_foreign_mod(&self) -> bool {
983 self.token.is_keyword(kw::Unsafe)
984 && self.is_keyword_ahead(1, &[kw::Extern])
986 2 + self.look_ahead(2, |t| t.can_begin_literal_maybe_minus() as usize),
987 |t| t.kind == token::OpenDelim(token::Brace),
991 fn is_static_global(&mut self) -> bool {
992 if self.check_keyword(kw::Static) {
993 // Check if this could be a closure.
994 !self.look_ahead(1, |token| {
995 if token.is_keyword(kw::Move) {
998 matches!(token.kind, token::BinOp(token::Or) | token::OrOr)
1005 /// Recover on `const mut` with `const` already eaten.
1006 fn recover_const_mut(&mut self, const_span: Span) {
1007 if self.eat_keyword(kw::Mut) {
1008 let span = self.prev_token.span;
1009 self.struct_span_err(span, "const globals cannot be mutable")
1010 .span_label(span, "cannot be mutable")
1013 "you might want to declare a static instead",
1014 "static".to_owned(),
1015 Applicability::MaybeIncorrect,
1021 /// Recover on `const impl` with `const` already eaten.
1022 fn recover_const_impl(
1025 attrs: &mut Vec<Attribute>,
1026 defaultness: Defaultness,
1027 ) -> PResult<'a, ItemInfo> {
1028 let impl_span = self.token.span;
1029 let mut err = self.expected_ident_found();
1031 // Only try to recover if this is implementing a trait for a type
1032 let mut impl_info = match self.parse_item_impl(attrs, defaultness) {
1033 Ok(impl_info) => impl_info,
1034 Err(mut recovery_error) => {
1035 // Recovery failed, raise the "expected identifier" error
1036 recovery_error.cancel();
1042 ItemKind::Impl(box ImplKind {
1043 of_trait: Some(ref trai), ref mut constness, ..
1045 *constness = Const::Yes(const_span);
1047 let before_trait = trai.path.span.shrink_to_lo();
1048 let const_up_to_impl = const_span.with_hi(impl_span.lo());
1049 err.multipart_suggestion(
1050 "you might have meant to write a const trait impl",
1051 vec![(const_up_to_impl, "".to_owned()), (before_trait, "const ".to_owned())],
1052 Applicability::MaybeIncorrect,
1056 ItemKind::Impl { .. } => return Err(err),
1057 _ => unreachable!(),
1063 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty (= $expr)?` with
1064 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1066 /// When `m` is `"const"`, `$ident` may also be `"_"`.
1067 fn parse_item_global(
1069 m: Option<Mutability>,
1070 ) -> PResult<'a, (Ident, P<Ty>, Option<P<ast::Expr>>)> {
1071 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1073 // Parse the type of a `const` or `static mut?` item.
1074 // That is, the `":" $ty` fragment.
1075 let ty = if self.eat(&token::Colon) {
1078 self.recover_missing_const_type(id, m)
1081 let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
1082 self.expect_semi()?;
1086 /// We were supposed to parse `:` but the `:` was missing.
1087 /// This means that the type is missing.
1088 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1089 // Construct the error and stash it away with the hope
1090 // that typeck will later enrich the error with a type.
1091 let kind = match m {
1092 Some(Mutability::Mut) => "static mut",
1093 Some(Mutability::Not) => "static",
1096 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1097 err.span_suggestion(
1099 "provide a type for the item",
1100 format!("{}: <type>", id),
1101 Applicability::HasPlaceholders,
1103 err.stash(id.span, StashKey::ItemNoType);
1105 // The user intended that the type be inferred,
1106 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1107 P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID, tokens: None })
1110 /// Parses an enum declaration.
1111 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1112 let id = self.parse_ident()?;
1113 let mut generics = self.parse_generics()?;
1114 generics.where_clause = self.parse_where_clause()?;
1117 self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1118 self.recover_stmt();
1122 let enum_definition = EnumDef { variants: variants.into_iter().flatten().collect() };
1123 Ok((id, ItemKind::Enum(enum_definition, generics)))
1126 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1127 let variant_attrs = self.parse_outer_attributes()?;
1128 self.collect_tokens_trailing_token(
1131 |this, variant_attrs| {
1132 let vlo = this.token.span;
1134 let vis = this.parse_visibility(FollowedByType::No)?;
1135 if !this.recover_nested_adt_item(kw::Enum)? {
1136 return Ok((None, TrailingToken::None));
1138 let ident = this.parse_field_ident("enum", vlo)?;
1140 let struct_def = if this.check(&token::OpenDelim(token::Brace)) {
1141 // Parse a struct variant.
1142 let (fields, recovered) = this.parse_record_struct_body("struct")?;
1143 VariantData::Struct(fields, recovered)
1144 } else if this.check(&token::OpenDelim(token::Paren)) {
1145 VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1147 VariantData::Unit(DUMMY_NODE_ID)
1151 if this.eat(&token::Eq) { Some(this.parse_anon_const_expr()?) } else { None };
1153 let vr = ast::Variant {
1157 attrs: variant_attrs.into(),
1160 span: vlo.to(this.prev_token.span),
1161 is_placeholder: false,
1164 Ok((Some(vr), TrailingToken::MaybeComma))
1169 /// Parses `struct Foo { ... }`.
1170 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1171 let class_name = self.parse_ident()?;
1173 let mut generics = self.parse_generics()?;
1175 // There is a special case worth noting here, as reported in issue #17904.
1176 // If we are parsing a tuple struct it is the case that the where clause
1177 // should follow the field list. Like so:
1179 // struct Foo<T>(T) where T: Copy;
1181 // If we are parsing a normal record-style struct it is the case
1182 // that the where clause comes before the body, and after the generics.
1183 // So if we look ahead and see a brace or a where-clause we begin
1184 // parsing a record style struct.
1186 // Otherwise if we look ahead and see a paren we parse a tuple-style
1189 let vdata = if self.token.is_keyword(kw::Where) {
1190 generics.where_clause = self.parse_where_clause()?;
1191 if self.eat(&token::Semi) {
1192 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1193 VariantData::Unit(DUMMY_NODE_ID)
1195 // If we see: `struct Foo<T> where T: Copy { ... }`
1196 let (fields, recovered) = self.parse_record_struct_body("struct")?;
1197 VariantData::Struct(fields, recovered)
1199 // No `where` so: `struct Foo<T>;`
1200 } else if self.eat(&token::Semi) {
1201 VariantData::Unit(DUMMY_NODE_ID)
1202 // Record-style struct definition
1203 } else if self.token == token::OpenDelim(token::Brace) {
1204 let (fields, recovered) = self.parse_record_struct_body("struct")?;
1205 VariantData::Struct(fields, recovered)
1206 // Tuple-style struct definition with optional where-clause.
1207 } else if self.token == token::OpenDelim(token::Paren) {
1208 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1209 generics.where_clause = self.parse_where_clause()?;
1210 self.expect_semi()?;
1213 let token_str = super::token_descr(&self.token);
1215 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1218 let mut err = self.struct_span_err(self.token.span, msg);
1219 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1223 Ok((class_name, ItemKind::Struct(vdata, generics)))
1226 /// Parses `union Foo { ... }`.
1227 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1228 let class_name = self.parse_ident()?;
1230 let mut generics = self.parse_generics()?;
1232 let vdata = if self.token.is_keyword(kw::Where) {
1233 generics.where_clause = self.parse_where_clause()?;
1234 let (fields, recovered) = self.parse_record_struct_body("union")?;
1235 VariantData::Struct(fields, recovered)
1236 } else if self.token == token::OpenDelim(token::Brace) {
1237 let (fields, recovered) = self.parse_record_struct_body("union")?;
1238 VariantData::Struct(fields, recovered)
1240 let token_str = super::token_descr(&self.token);
1241 let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1242 let mut err = self.struct_span_err(self.token.span, msg);
1243 err.span_label(self.token.span, "expected `where` or `{` after union name");
1247 Ok((class_name, ItemKind::Union(vdata, generics)))
1250 fn parse_record_struct_body(
1253 ) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
1254 let mut fields = Vec::new();
1255 let mut recovered = false;
1256 if self.eat(&token::OpenDelim(token::Brace)) {
1257 while self.token != token::CloseDelim(token::Brace) {
1258 let field = self.parse_field_def(adt_ty).map_err(|e| {
1259 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1264 Ok(field) => fields.push(field),
1271 self.eat(&token::CloseDelim(token::Brace));
1273 let token_str = super::token_descr(&self.token);
1274 let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
1275 let mut err = self.struct_span_err(self.token.span, msg);
1276 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1280 Ok((fields, recovered))
1283 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<FieldDef>> {
1284 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1285 // Unit like structs are handled in parse_item_struct function
1286 self.parse_paren_comma_seq(|p| {
1287 let attrs = p.parse_outer_attributes()?;
1288 p.collect_tokens_trailing_token(attrs, ForceCollect::No, |p, attrs| {
1289 let lo = p.token.span;
1290 let vis = p.parse_visibility(FollowedByType::Yes)?;
1291 let ty = p.parse_ty()?;
1295 span: lo.to(ty.span),
1300 attrs: attrs.into(),
1301 is_placeholder: false,
1303 TrailingToken::MaybeComma,
1310 /// Parses an element of a struct declaration.
1311 fn parse_field_def(&mut self, adt_ty: &str) -> PResult<'a, FieldDef> {
1312 let attrs = self.parse_outer_attributes()?;
1313 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
1314 let lo = this.token.span;
1315 let vis = this.parse_visibility(FollowedByType::No)?;
1316 Ok((this.parse_single_struct_field(adt_ty, lo, vis, attrs)?, TrailingToken::None))
1320 /// Parses a structure field declaration.
1321 fn parse_single_struct_field(
1326 attrs: Vec<Attribute>,
1327 ) -> PResult<'a, FieldDef> {
1328 let mut seen_comma: bool = false;
1329 let a_var = self.parse_name_and_ty(adt_ty, lo, vis, attrs)?;
1330 if self.token == token::Comma {
1333 match self.token.kind {
1337 token::CloseDelim(token::Brace) => {}
1338 token::DocComment(..) => {
1339 let previous_span = self.prev_token.span;
1340 let mut err = self.span_err(self.token.span, Error::UselessDocComment);
1341 self.bump(); // consume the doc comment
1342 let comma_after_doc_seen = self.eat(&token::Comma);
1343 // `seen_comma` is always false, because we are inside doc block
1344 // condition is here to make code more readable
1345 if !seen_comma && comma_after_doc_seen {
1348 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1352 let sp = self.sess.source_map().next_point(previous_span);
1353 err.span_suggestion(
1355 "missing comma here",
1357 Applicability::MachineApplicable,
1364 let sp = self.prev_token.span.shrink_to_hi();
1365 let mut err = self.struct_span_err(
1367 &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1370 // Try to recover extra trailing angle brackets
1371 let mut recovered = false;
1372 if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind {
1373 if let Some(last_segment) = segments.last() {
1374 recovered = self.check_trailing_angle_brackets(
1376 &[&token::Comma, &token::CloseDelim(token::Brace)],
1379 // Handle a case like `Vec<u8>>,` where we can continue parsing fields
1381 self.eat(&token::Comma);
1382 // `check_trailing_angle_brackets` already emitted a nicer error
1388 if self.token.is_ident() {
1389 // This is likely another field; emit the diagnostic and keep going
1390 err.span_suggestion(
1392 "try adding a comma",
1394 Applicability::MachineApplicable,
1401 // Make sure an error was emitted (either by recovering an angle bracket,
1402 // or by finding an identifier as the next token), since we're
1403 // going to continue parsing
1404 assert!(self.sess.span_diagnostic.has_errors());
1413 fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
1414 if let Err(mut err) = self.expect(&token::Colon) {
1415 let sm = self.sess.source_map();
1416 let eq_typo = self.token.kind == token::Eq && self.look_ahead(1, |t| t.is_path_start());
1417 let semi_typo = self.token.kind == token::Semi
1418 && self.look_ahead(1, |t| {
1420 // We check that we are in a situation like `foo; bar` to avoid bad suggestions
1421 // when there's no type and `;` was used instead of a comma.
1422 && match (sm.lookup_line(self.token.span.hi()), sm.lookup_line(t.span.lo())) {
1423 (Ok(l), Ok(r)) => l.line == r.line,
1427 if eq_typo || semi_typo {
1429 // Gracefully handle small typos.
1430 err.span_suggestion_short(
1431 self.prev_token.span,
1432 "field names and their types are separated with `:`",
1434 Applicability::MachineApplicable,
1444 /// Parses a structure field.
1445 fn parse_name_and_ty(
1450 attrs: Vec<Attribute>,
1451 ) -> PResult<'a, FieldDef> {
1452 let name = self.parse_field_ident(adt_ty, lo)?;
1453 self.expect_field_ty_separator()?;
1454 let ty = self.parse_ty()?;
1455 if self.token.kind == token::Eq {
1457 let const_expr = self.parse_anon_const_expr()?;
1458 let sp = ty.span.shrink_to_hi().to(const_expr.value.span);
1459 self.struct_span_err(sp, "default values on `struct` fields aren't supported")
1462 "remove this unsupported default value",
1464 Applicability::MachineApplicable,
1469 span: lo.to(self.prev_token.span),
1474 attrs: attrs.into(),
1475 is_placeholder: false,
1479 /// Parses a field identifier. Specialized version of `parse_ident_common`
1480 /// for better diagnostics and suggestions.
1481 fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
1482 let (ident, is_raw) = self.ident_or_err()?;
1483 if !is_raw && ident.is_reserved() {
1484 let err = if self.check_fn_front_matter(false) {
1485 // We use `parse_fn` to get a span for the function
1486 if let Err(mut db) = self.parse_fn(&mut Vec::new(), |_| true, lo) {
1489 let mut err = self.struct_span_err(
1490 lo.to(self.prev_token.span),
1491 &format!("functions are not allowed in {} definitions", adt_ty),
1493 err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
1494 err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
1497 self.expected_ident_found()
1505 /// Parses a declarative macro 2.0 definition.
1506 /// The `macro` keyword has already been parsed.
1508 /// MacBody = "{" TOKEN_STREAM "}" ;
1509 /// MacParams = "(" TOKEN_STREAM ")" ;
1510 /// DeclMac = "macro" Ident MacParams? MacBody ;
1512 fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemInfo> {
1513 let ident = self.parse_ident()?;
1514 let body = if self.check(&token::OpenDelim(token::Brace)) {
1515 self.parse_mac_args()? // `MacBody`
1516 } else if self.check(&token::OpenDelim(token::Paren)) {
1517 let params = self.parse_token_tree(); // `MacParams`
1518 let pspan = params.span();
1519 if !self.check(&token::OpenDelim(token::Brace)) {
1520 return self.unexpected();
1522 let body = self.parse_token_tree(); // `MacBody`
1523 // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
1524 let bspan = body.span();
1525 let arrow = TokenTree::token(token::FatArrow, pspan.between(bspan)); // `=>`
1526 let tokens = TokenStream::new(vec![params.into(), arrow.into(), body.into()]);
1527 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1528 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1530 return self.unexpected();
1533 self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
1534 Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: false })))
1537 /// Is this unambiguously the start of a `macro_rules! foo` item definition?
1538 fn is_macro_rules_item(&mut self) -> bool {
1539 self.check_keyword(kw::MacroRules)
1540 && self.look_ahead(1, |t| *t == token::Not)
1541 && self.look_ahead(2, |t| t.is_ident())
1544 /// Parses a `macro_rules! foo { ... }` declarative macro.
1545 fn parse_item_macro_rules(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
1546 self.expect_keyword(kw::MacroRules)?; // `macro_rules`
1547 self.expect(&token::Not)?; // `!`
1549 let ident = self.parse_ident()?;
1551 if self.eat(&token::Not) {
1552 // Handle macro_rules! foo!
1553 let span = self.prev_token.span;
1554 self.struct_span_err(span, "macro names aren't followed by a `!`")
1559 Applicability::MachineApplicable,
1564 let body = self.parse_mac_args()?;
1565 self.eat_semi_for_macro_if_needed(&body);
1566 self.complain_if_pub_macro(vis, true);
1568 Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: true })))
1571 /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
1572 /// If that's not the case, emit an error.
1573 fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
1574 if let VisibilityKind::Inherited = vis.kind {
1578 let vstr = pprust::vis_to_string(vis);
1579 let vstr = vstr.trim_end();
1581 let msg = format!("can't qualify macro_rules invocation with `{}`", vstr);
1582 self.struct_span_err(vis.span, &msg)
1585 "try exporting the macro",
1586 "#[macro_export]".to_owned(),
1587 Applicability::MaybeIncorrect, // speculative
1591 self.struct_span_err(vis.span, "can't qualify macro invocation with `pub`")
1594 "remove the visibility",
1596 Applicability::MachineApplicable,
1598 .help(&format!("try adjusting the macro to put `{}` inside the invocation", vstr))
1603 fn eat_semi_for_macro_if_needed(&mut self, args: &MacArgs) {
1604 if args.need_semicolon() && !self.eat(&token::Semi) {
1605 self.report_invalid_macro_expansion_item(args);
1609 fn report_invalid_macro_expansion_item(&self, args: &MacArgs) {
1610 let span = args.span().expect("undelimited macro call");
1611 let mut err = self.struct_span_err(
1613 "macros that expand to items must be delimited with braces or followed by a semicolon",
1615 if self.unclosed_delims.is_empty() {
1616 let DelimSpan { open, close } = match args {
1617 MacArgs::Empty | MacArgs::Eq(..) => unreachable!(),
1618 MacArgs::Delimited(dspan, ..) => *dspan,
1620 err.multipart_suggestion(
1621 "change the delimiters to curly braces",
1622 vec![(open, "{".to_string()), (close, '}'.to_string())],
1623 Applicability::MaybeIncorrect,
1626 err.span_suggestion(
1628 "change the delimiters to curly braces",
1629 " { /* items */ }".to_string(),
1630 Applicability::HasPlaceholders,
1633 err.span_suggestion(
1634 span.shrink_to_hi(),
1637 Applicability::MaybeIncorrect,
1642 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1643 /// it is, we try to parse the item and report error about nested types.
1644 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1645 if (self.token.is_keyword(kw::Enum)
1646 || self.token.is_keyword(kw::Struct)
1647 || self.token.is_keyword(kw::Union))
1648 && self.look_ahead(1, |t| t.is_ident())
1650 let kw_token = self.token.clone();
1651 let kw_str = pprust::token_to_string(&kw_token);
1652 let item = self.parse_item(ForceCollect::No)?;
1654 self.struct_span_err(
1656 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1660 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1662 Applicability::MaybeIncorrect,
1665 // We successfully parsed the item but we must inform the caller about nested problem.
1672 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1674 /// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1675 type ReqName = fn(Edition) -> bool;
1677 /// Parsing of functions and methods.
1678 impl<'a> Parser<'a> {
1679 /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
1682 attrs: &mut Vec<Attribute>,
1685 ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
1686 let header = self.parse_fn_front_matter()?; // `const ... fn`
1687 let ident = self.parse_ident()?; // `foo`
1688 let mut generics = self.parse_generics()?; // `<'a, T, ...>`
1689 let decl = self.parse_fn_decl(req_name, AllowPlus::Yes, RecoverReturnSign::Yes)?; // `(p: u8, ...)`
1690 generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
1692 let mut sig_hi = self.prev_token.span;
1693 let body = self.parse_fn_body(attrs, &ident, &mut sig_hi)?; // `;` or `{ ... }`.
1694 let fn_sig_span = sig_lo.to(sig_hi);
1695 Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, body))
1698 /// Parse the "body" of a function.
1699 /// This can either be `;` when there's no body,
1700 /// or e.g. a block when the function is a provided one.
1703 attrs: &mut Vec<Attribute>,
1706 ) -> PResult<'a, Option<P<Block>>> {
1707 let (inner_attrs, body) = if self.eat(&token::Semi) {
1708 // Include the trailing semicolon in the span of the signature
1709 *sig_hi = self.prev_token.span;
1711 } else if self.check(&token::OpenDelim(token::Brace)) || self.token.is_whole_block() {
1712 self.parse_inner_attrs_and_block().map(|(attrs, body)| (attrs, Some(body)))?
1713 } else if self.token.kind == token::Eq {
1714 // Recover `fn foo() = $expr;`.
1716 let eq_sp = self.prev_token.span;
1717 let _ = self.parse_expr()?;
1718 self.expect_semi()?; // `;`
1719 let span = eq_sp.to(self.prev_token.span);
1720 self.struct_span_err(span, "function body cannot be `= expression;`")
1721 .multipart_suggestion(
1722 "surround the expression with `{` and `}` instead of `=` and `;`",
1723 vec![(eq_sp, "{".to_string()), (self.prev_token.span, " }".to_string())],
1724 Applicability::MachineApplicable,
1727 (Vec::new(), Some(self.mk_block_err(span)))
1729 if let Err(mut err) =
1730 self.expected_one_of_not_found(&[], &[token::Semi, token::OpenDelim(token::Brace)])
1732 if self.token.kind == token::CloseDelim(token::Brace) {
1733 // The enclosing `mod`, `trait` or `impl` is being closed, so keep the `fn` in
1734 // the AST for typechecking.
1735 err.span_label(ident.span, "while parsing this `fn`");
1743 attrs.extend(inner_attrs);
1747 /// Is the current token the start of an `FnHeader` / not a valid parse?
1749 /// `check_pub` adds additional `pub` to the checks in case users place it
1750 /// wrongly, can be used to ensure `pub` never comes after `default`.
1751 pub(super) fn check_fn_front_matter(&mut self, check_pub: bool) -> bool {
1752 // We use an over-approximation here.
1753 // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
1754 // `pub` is added in case users got confused with the ordering like `async pub fn`,
1755 // only if it wasn't preceeded by `default` as `default pub` is invalid.
1756 let quals: &[Symbol] = if check_pub {
1757 &[kw::Pub, kw::Const, kw::Async, kw::Unsafe, kw::Extern]
1759 &[kw::Const, kw::Async, kw::Unsafe, kw::Extern]
1761 self.check_keyword(kw::Fn) // Definitely an `fn`.
1762 // `$qual fn` or `$qual $qual`:
1763 || quals.iter().any(|&kw| self.check_keyword(kw))
1764 && self.look_ahead(1, |t| {
1765 // `$qual fn`, e.g. `const fn` or `async fn`.
1766 t.is_keyword(kw::Fn)
1767 // Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
1768 || t.is_non_raw_ident_where(|i| quals.contains(&i.name)
1769 // Rule out 2015 `const async: T = val`.
1771 // Rule out unsafe extern block.
1772 && !self.is_unsafe_foreign_mod())
1775 || self.check_keyword(kw::Extern)
1776 && self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
1777 && self.look_ahead(2, |t| t.is_keyword(kw::Fn))
1780 /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
1781 /// up to and including the `fn` keyword. The formal grammar is:
1784 /// Extern = "extern" StringLit? ;
1785 /// FnQual = "const"? "async"? "unsafe"? Extern? ;
1786 /// FnFrontMatter = FnQual "fn" ;
1788 pub(super) fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1789 let sp_start = self.token.span;
1790 let constness = self.parse_constness();
1792 let async_start_sp = self.token.span;
1793 let asyncness = self.parse_asyncness();
1795 let unsafe_start_sp = self.token.span;
1796 let unsafety = self.parse_unsafety();
1798 let ext_start_sp = self.token.span;
1799 let ext = self.parse_extern();
1801 if let Async::Yes { span, .. } = asyncness {
1802 self.ban_async_in_2015(span);
1805 if !self.eat_keyword(kw::Fn) {
1806 // It is possible for `expect_one_of` to recover given the contents of
1807 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1808 // account for this.
1809 match self.expect_one_of(&[], &[]) {
1811 Ok(false) => unreachable!(),
1813 // Qualifier keywords ordering check
1815 // This will allow the machine fix to directly place the keyword in the correct place
1816 let current_qual_sp = if self.check_keyword(kw::Const) {
1817 Some(async_start_sp)
1818 } else if self.check_keyword(kw::Async) {
1819 Some(unsafe_start_sp)
1820 } else if self.check_keyword(kw::Unsafe) {
1826 if let Some(current_qual_sp) = current_qual_sp {
1827 let current_qual_sp = current_qual_sp.to(self.prev_token.span);
1828 if let Ok(current_qual) = self.span_to_snippet(current_qual_sp) {
1829 let invalid_qual_sp = self.token.uninterpolated_span();
1830 let invalid_qual = self.span_to_snippet(invalid_qual_sp).unwrap();
1832 err.span_suggestion(
1833 current_qual_sp.to(invalid_qual_sp),
1834 &format!("`{}` must come before `{}`", invalid_qual, current_qual),
1835 format!("{} {}", invalid_qual, current_qual),
1836 Applicability::MachineApplicable,
1837 ).note("keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern`");
1840 // Recover incorrect visibility order such as `async pub`.
1841 else if self.check_keyword(kw::Pub) {
1842 let sp = sp_start.to(self.prev_token.span);
1843 if let Ok(snippet) = self.span_to_snippet(sp) {
1844 let vis = match self.parse_visibility(FollowedByType::No) {
1851 let vs = pprust::vis_to_string(&vis);
1852 let vs = vs.trim_end();
1853 err.span_suggestion(
1854 sp_start.to(self.prev_token.span),
1855 &format!("visibility `{}` must come before `{}`", vs, snippet),
1856 format!("{} {}", vs, snippet),
1857 Applicability::MachineApplicable,
1866 Ok(FnHeader { constness, unsafety, asyncness, ext })
1869 /// We are parsing `async fn`. If we are on Rust 2015, emit an error.
1870 fn ban_async_in_2015(&self, span: Span) {
1871 if span.rust_2015() {
1872 let diag = self.diagnostic();
1873 struct_span_err!(diag, span, E0670, "`async fn` is not permitted in Rust 2015")
1874 .span_label(span, "to use `async fn`, switch to Rust 2018 or later")
1875 .help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION))
1876 .note("for more on editions, read https://doc.rust-lang.org/edition-guide")
1881 /// Parses the parameter list and result type of a function declaration.
1882 pub(super) fn parse_fn_decl(
1885 ret_allow_plus: AllowPlus,
1886 recover_return_sign: RecoverReturnSign,
1887 ) -> PResult<'a, P<FnDecl>> {
1889 inputs: self.parse_fn_params(req_name)?,
1890 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes, recover_return_sign)?,
1894 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1895 fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
1896 let mut first_param = true;
1897 // Parse the arguments, starting out with `self` being allowed...
1898 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1899 let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
1901 let lo = p.prev_token.span;
1902 // Skip every token until next possible arg or end.
1903 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1904 // Create a placeholder argument for proper arg count (issue #34264).
1905 Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span))))
1907 // ...now that we've parsed the first argument, `self` is no longer allowed.
1908 first_param = false;
1911 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1912 self.deduplicate_recovered_params_names(&mut params);
1916 /// Parses a single function parameter.
1918 /// - `self` is syntactically allowed when `first_param` holds.
1919 fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
1920 let lo = self.token.span;
1921 let attrs = self.parse_outer_attributes()?;
1922 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
1923 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1924 if let Some(mut param) = this.parse_self_param()? {
1925 param.attrs = attrs.into();
1926 let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
1927 return Ok((res?, TrailingToken::None));
1930 let is_name_required = match this.token.kind {
1931 token::DotDotDot => false,
1932 _ => req_name(this.token.span.edition()),
1934 let (pat, ty) = if is_name_required || this.is_named_param() {
1935 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1937 let (pat, colon) = this.parse_fn_param_pat_colon()?;
1939 let mut err = this.unexpected::<()>().unwrap_err();
1940 return if let Some(ident) =
1941 this.parameter_without_type(&mut err, pat, is_name_required, first_param)
1944 Ok((dummy_arg(ident), TrailingToken::None))
1950 this.eat_incorrect_doc_comment_for_param_type();
1951 (pat, this.parse_ty_for_param()?)
1953 debug!("parse_param_general ident_to_pat");
1954 let parser_snapshot_before_ty = this.clone();
1955 this.eat_incorrect_doc_comment_for_param_type();
1956 let mut ty = this.parse_ty_for_param();
1958 && this.token != token::Comma
1959 && this.token != token::CloseDelim(token::Paren)
1961 // This wasn't actually a type, but a pattern looking like a type,
1962 // so we are going to rollback and re-parse for recovery.
1963 ty = this.unexpected();
1967 let ident = Ident::new(kw::Empty, this.prev_token.span);
1968 let bm = BindingMode::ByValue(Mutability::Not);
1969 let pat = this.mk_pat_ident(ty.span, bm, ident);
1972 // If this is a C-variadic argument and we hit an error, return the error.
1973 Err(err) if this.token == token::DotDotDot => return Err(err),
1974 // Recover from attempting to parse the argument as a type without pattern.
1977 *this = parser_snapshot_before_ty;
1978 this.recover_arg_parse()?
1983 let span = lo.until(this.token.span);
1987 attrs: attrs.into(),
1988 id: ast::DUMMY_NODE_ID,
1989 is_placeholder: false,
1994 TrailingToken::None,
1999 /// Returns the parsed optional self parameter and whether a self shortcut was used.
2000 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
2001 // Extract an identifier *after* having confirmed that the token is one.
2002 let expect_self_ident = |this: &mut Self| match this.token.ident() {
2003 Some((ident, false)) => {
2007 _ => unreachable!(),
2009 // Is `self` `n` tokens ahead?
2010 let is_isolated_self = |this: &Self, n| {
2011 this.is_keyword_ahead(n, &[kw::SelfLower])
2012 && this.look_ahead(n + 1, |t| t != &token::ModSep)
2014 // Is `mut self` `n` tokens ahead?
2015 let is_isolated_mut_self =
2016 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
2017 // Parse `self` or `self: TYPE`. We already know the current token is `self`.
2018 let parse_self_possibly_typed = |this: &mut Self, m| {
2019 let eself_ident = expect_self_ident(this);
2020 let eself_hi = this.prev_token.span;
2021 let eself = if this.eat(&token::Colon) {
2022 SelfKind::Explicit(this.parse_ty()?, m)
2026 Ok((eself, eself_ident, eself_hi))
2028 // Recover for the grammar `*self`, `*const self`, and `*mut self`.
2029 let recover_self_ptr = |this: &mut Self| {
2030 let msg = "cannot pass `self` by raw pointer";
2031 let span = this.token.span;
2032 this.struct_span_err(span, msg).span_label(span, msg).emit();
2034 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
2037 // Parse optional `self` parameter of a method.
2038 // Only a limited set of initial token sequences is considered `self` parameters; anything
2039 // else is parsed as a normal function parameter list, so some lookahead is required.
2040 let eself_lo = self.token.span;
2041 let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
2042 token::BinOp(token::And) => {
2043 let eself = if is_isolated_self(self, 1) {
2046 SelfKind::Region(None, Mutability::Not)
2047 } else if is_isolated_mut_self(self, 1) {
2051 SelfKind::Region(None, Mutability::Mut)
2052 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
2055 let lt = self.expect_lifetime();
2056 SelfKind::Region(Some(lt), Mutability::Not)
2057 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
2060 let lt = self.expect_lifetime();
2062 SelfKind::Region(Some(lt), Mutability::Mut)
2067 (eself, expect_self_ident(self), self.prev_token.span)
2070 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
2072 recover_self_ptr(self)?
2074 // `*mut self` and `*const self`
2075 token::BinOp(token::Star)
2076 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
2080 recover_self_ptr(self)?
2082 // `self` and `self: TYPE`
2083 token::Ident(..) if is_isolated_self(self, 0) => {
2084 parse_self_possibly_typed(self, Mutability::Not)?
2086 // `mut self` and `mut self: TYPE`
2087 token::Ident(..) if is_isolated_mut_self(self, 0) => {
2089 parse_self_possibly_typed(self, Mutability::Mut)?
2091 _ => return Ok(None),
2094 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
2095 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
2098 fn is_named_param(&self) -> bool {
2099 let offset = match self.token.kind {
2100 token::Interpolated(ref nt) => match **nt {
2101 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2104 token::BinOp(token::And) | token::AndAnd => 1,
2105 _ if self.token.is_keyword(kw::Mut) => 1,
2109 self.look_ahead(offset, |t| t.is_ident())
2110 && self.look_ahead(offset + 1, |t| t == &token::Colon)
2113 fn recover_first_param(&mut self) -> &'static str {
2115 .parse_outer_attributes()
2116 .and_then(|_| self.parse_self_param())
2117 .map_err(|mut e| e.cancel())
2119 Ok(Some(_)) => "method",