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;
8 use rustc_ast::token::{self, TokenKind};
9 use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
10 use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID};
11 use rustc_ast::{AssocItem, AssocItemKind, ForeignItemKind, Item, ItemKind, Mod};
12 use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind};
13 use rustc_ast::{BindingMode, Block, FnDecl, FnSig, Param, SelfKind};
14 use rustc_ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
15 use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, VisibilityKind};
16 use rustc_ast::{MacArgs, MacCall, MacDelimiter};
17 use rustc_ast_pretty::pprust;
18 use rustc_errors::{struct_span_err, Applicability, PResult, StashKey};
19 use rustc_span::edition::Edition;
20 use rustc_span::source_map::{self, Span};
21 use rustc_span::symbol::{kw, sym, Ident, Symbol};
23 use std::convert::TryFrom;
28 /// Parses a source module as a crate. This is the main entry point for the parser.
29 pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
30 let lo = self.token.span;
31 let (module, attrs) = self.parse_mod(&token::Eof, Unsafe::No)?;
32 let span = lo.to(self.token.span);
33 let proc_macros = Vec::new(); // Filled in by `proc_macro_harness::inject()`.
34 Ok(ast::Crate { attrs, module, span, proc_macros })
37 /// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
38 fn parse_item_mod(&mut self, attrs: &mut Vec<Attribute>) -> PResult<'a, ItemInfo> {
39 let unsafety = self.parse_unsafety();
40 self.expect_keyword(kw::Mod)?;
41 let id = self.parse_ident()?;
42 let (module, mut inner_attrs) = if self.eat(&token::Semi) {
43 (Mod { inner: Span::default(), unsafety, items: Vec::new(), inline: false }, Vec::new())
45 self.expect(&token::OpenDelim(token::Brace))?;
46 self.parse_mod(&token::CloseDelim(token::Brace), unsafety)?
48 attrs.append(&mut inner_attrs);
49 Ok((id, ItemKind::Mod(module)))
52 /// Parses the contents of a module (inner attributes followed by module items).
57 ) -> PResult<'a, (Mod, Vec<Attribute>)> {
58 let lo = self.token.span;
59 let attrs = self.parse_inner_attributes()?;
60 let module = self.parse_mod_items(term, lo, unsafety)?;
64 /// Given a termination token, parses all of the items in a module.
70 ) -> PResult<'a, Mod> {
71 let mut items = vec![];
72 while let Some(item) = self.parse_item()? {
74 self.maybe_consume_incorrect_semicolon(&items);
78 let token_str = super::token_descr(&self.token);
79 if !self.maybe_consume_incorrect_semicolon(&items) {
80 let msg = &format!("expected item, found {}", token_str);
81 let mut err = self.struct_span_err(self.token.span, msg);
82 err.span_label(self.token.span, "expected item");
87 let hi = if self.token.span.is_dummy() { inner_lo } else { self.prev_token.span };
89 Ok(Mod { inner: inner_lo.to(hi), unsafety, items, inline: true })
93 pub(super) type ItemInfo = (Ident, ItemKind);
96 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
97 self.parse_item_(|_| true).map(|i| i.map(P))
100 fn parse_item_(&mut self, req_name: ReqName) -> PResult<'a, Option<Item>> {
101 let attrs = self.parse_outer_attributes()?;
102 self.parse_item_common(attrs, true, false, req_name)
105 pub(super) fn parse_item_common(
107 mut attrs: Vec<Attribute>,
111 ) -> PResult<'a, Option<Item>> {
112 maybe_whole!(self, NtItem, |item| {
114 mem::swap(&mut item.attrs, &mut attrs);
115 item.attrs.extend(attrs);
116 Some(item.into_inner())
119 let needs_tokens = super::attr::maybe_needs_tokens(&attrs);
121 let mut unclosed_delims = vec![];
122 let parse_item = |this: &mut Self| {
123 let item = this.parse_item_common_(attrs, mac_allowed, attrs_allowed, req_name);
124 unclosed_delims.append(&mut this.unclosed_delims);
128 let (mut item, tokens) = if needs_tokens {
129 let (item, tokens) = self.collect_tokens(parse_item)?;
132 (parse_item(self)?, None)
134 if let Some(item) = &mut item {
135 // If we captured tokens during parsing (due to encountering an `NtItem`),
137 if item.tokens.is_none() {
138 item.tokens = tokens;
142 self.unclosed_delims.append(&mut unclosed_delims);
146 fn parse_item_common_(
148 mut attrs: Vec<Attribute>,
152 ) -> PResult<'a, Option<Item>> {
153 let lo = self.token.span;
154 let vis = self.parse_visibility(FollowedByType::No)?;
155 let mut def = self.parse_defaultness();
156 let kind = self.parse_item_kind(&mut attrs, mac_allowed, lo, &vis, &mut def, req_name)?;
157 if let Some((ident, kind)) = kind {
158 self.error_on_unconsumed_default(def, &kind);
159 let span = lo.to(self.prev_token.span);
160 let id = DUMMY_NODE_ID;
161 let item = Item { ident, attrs, id, kind, vis, span, tokens: None };
162 return Ok(Some(item));
165 // At this point, we have failed to parse an item.
166 self.error_on_unmatched_vis(&vis);
167 self.error_on_unmatched_defaultness(def);
169 self.recover_attrs_no_item(&attrs)?;
174 /// Error in-case a non-inherited visibility was parsed but no item followed.
175 fn error_on_unmatched_vis(&self, vis: &Visibility) {
176 if let VisibilityKind::Inherited = vis.kind {
179 let vs = pprust::vis_to_string(&vis);
180 let vs = vs.trim_end();
181 self.struct_span_err(vis.span, &format!("visibility `{}` is not followed by an item", vs))
182 .span_label(vis.span, "the visibility")
183 .help(&format!("you likely meant to define an item, e.g., `{} fn foo() {{}}`", vs))
187 /// Error in-case a `default` was parsed but no item followed.
188 fn error_on_unmatched_defaultness(&self, def: Defaultness) {
189 if let Defaultness::Default(sp) = def {
190 self.struct_span_err(sp, "`default` is not followed by an item")
191 .span_label(sp, "the `default` qualifier")
192 .note("only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`")
197 /// Error in-case `default` was parsed in an in-appropriate context.
198 fn error_on_unconsumed_default(&self, def: Defaultness, kind: &ItemKind) {
199 if let Defaultness::Default(span) = def {
200 let msg = format!("{} {} cannot be `default`", kind.article(), kind.descr());
201 self.struct_span_err(span, &msg)
202 .span_label(span, "`default` because of this")
203 .note("only associated `fn`, `const`, and `type` items can be `default`")
208 /// Parses one of the items allowed by the flags.
211 attrs: &mut Vec<Attribute>,
212 macros_allowed: bool,
215 def: &mut Defaultness,
217 ) -> PResult<'a, Option<ItemInfo>> {
218 let mut def = || mem::replace(def, Defaultness::Final);
220 let info = if self.eat_keyword(kw::Use) {
222 let tree = self.parse_use_tree()?;
224 (Ident::invalid(), ItemKind::Use(P(tree)))
225 } else if self.check_fn_front_matter() {
227 let (ident, sig, generics, body) = self.parse_fn(attrs, req_name, lo)?;
228 (ident, ItemKind::Fn(def(), sig, generics, body))
229 } else if self.eat_keyword(kw::Extern) {
230 if self.eat_keyword(kw::Crate) {
232 self.parse_item_extern_crate()?
235 self.parse_item_foreign_mod(attrs, Unsafe::No)?
237 } else if self.is_unsafe_foreign_mod() {
239 let unsafety = self.parse_unsafety();
240 self.expect_keyword(kw::Extern)?;
241 self.parse_item_foreign_mod(attrs, unsafety)?
242 } else if self.is_static_global() {
244 self.bump(); // `static`
245 let m = self.parse_mutability();
246 let (ident, ty, expr) = self.parse_item_global(Some(m))?;
247 (ident, ItemKind::Static(ty, m, expr))
248 } else if let Const::Yes(const_span) = self.parse_constness() {
250 if self.token.is_keyword(kw::Impl) {
251 // recover from `const impl`, suggest `impl const`
252 self.recover_const_impl(const_span, attrs, def())?
254 self.recover_const_mut(const_span);
255 let (ident, ty, expr) = self.parse_item_global(None)?;
256 (ident, ItemKind::Const(def(), ty, expr))
258 } else if self.check_keyword(kw::Trait) || self.check_auto_or_unsafe_trait_item() {
260 self.parse_item_trait(attrs, lo)?
261 } else if self.check_keyword(kw::Impl)
262 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
265 self.parse_item_impl(attrs, def())?
266 } else if self.check_keyword(kw::Mod)
267 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Mod])
270 self.parse_item_mod(attrs)?
271 } else if self.eat_keyword(kw::Type) {
273 self.parse_type_alias(def())?
274 } else if self.eat_keyword(kw::Enum) {
276 self.parse_item_enum()?
277 } else if self.eat_keyword(kw::Struct) {
279 self.parse_item_struct()?
280 } else if self.is_kw_followed_by_ident(kw::Union) {
282 self.bump(); // `union`
283 self.parse_item_union()?
284 } else if self.eat_keyword(kw::Macro) {
286 self.parse_item_decl_macro(lo)?
287 } else if self.is_macro_rules_item() {
289 self.parse_item_macro_rules(vis)?
290 } else if vis.kind.is_pub() && self.isnt_macro_invocation() {
291 self.recover_missing_kw_before_item()?;
293 } else if macros_allowed && self.check_path() {
294 // MACRO INVOCATION ITEM
295 (Ident::invalid(), ItemKind::MacCall(self.parse_item_macro(vis)?))
302 /// When parsing a statement, would the start of a path be an item?
303 pub(super) fn is_path_start_item(&mut self) -> bool {
304 self.is_crate_vis() // no: `crate::b`, yes: `crate $item`
305 || self.is_kw_followed_by_ident(kw::Union) // no: `union::b`, yes: `union U { .. }`
306 || self.check_auto_or_unsafe_trait_item() // no: `auto::b`, yes: `auto trait X { .. }`
307 || self.is_async_fn() // no(2015): `async::b`, yes: `async fn`
308 || self.is_macro_rules_item() // no: `macro_rules::b`, yes: `macro_rules! mac`
311 /// Are we sure this could not possibly be a macro invocation?
312 fn isnt_macro_invocation(&mut self) -> bool {
313 self.check_ident() && self.look_ahead(1, |t| *t != token::Not && *t != token::ModSep)
316 /// Recover on encountering a struct or method definition where the user
317 /// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
318 fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
319 // Space between `pub` keyword and the identifier
322 // ^^^ `sp` points here
323 let sp = self.prev_token.span.between(self.token.span);
324 let full_sp = self.prev_token.span.to(self.token.span);
325 let ident_sp = self.token.span;
326 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
327 // possible public struct definition where `struct` was forgotten
328 let ident = self.parse_ident().unwrap();
329 let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
330 let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
331 err.span_suggestion_short(
335 Applicability::MaybeIncorrect, // speculative
338 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
339 let ident = self.parse_ident().unwrap();
341 let kw_name = self.recover_first_param();
342 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
343 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
344 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
346 ("fn", kw_name, false)
347 } else if self.check(&token::OpenDelim(token::Brace)) {
349 ("fn", kw_name, false)
350 } else if self.check(&token::Colon) {
354 ("fn` or `struct", "function or struct", true)
357 let msg = format!("missing `{}` for {} definition", kw, kw_name);
358 let mut err = self.struct_span_err(sp, &msg);
360 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
362 format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
363 err.span_suggestion_short(
367 Applicability::MachineApplicable,
369 } else if let Ok(snippet) = self.span_to_snippet(ident_sp) {
372 "if you meant to call a macro, try",
373 format!("{}!", snippet),
374 // this is the `ambiguous` conditional branch
375 Applicability::MaybeIncorrect,
379 "if you meant to call a macro, remove the `pub` \
380 and add a trailing `!` after the identifier",
384 } else if self.look_ahead(1, |t| *t == token::Lt) {
385 let ident = self.parse_ident().unwrap();
386 self.eat_to_tokens(&[&token::Gt]);
388 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
389 ("fn", self.recover_first_param(), false)
390 } else if self.check(&token::OpenDelim(token::Brace)) {
391 ("struct", "struct", false)
393 ("fn` or `struct", "function or struct", true)
395 let msg = format!("missing `{}` for {} definition", kw, kw_name);
396 let mut err = self.struct_span_err(sp, &msg);
398 err.span_suggestion_short(
400 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
402 Applicability::MachineApplicable,
411 /// Parses an item macro, e.g., `item!();`.
412 fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
413 let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
414 self.expect(&token::Not)?; // `!`
415 let args = self.parse_mac_args()?; // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
416 self.eat_semi_for_macro_if_needed(&args);
417 self.complain_if_pub_macro(vis, false);
418 Ok(MacCall { path, args, prior_type_ascription: self.last_type_ascription })
421 /// Recover if we parsed attributes and expected an item but there was none.
422 fn recover_attrs_no_item(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
423 let (start, end) = match attrs {
425 [x0 @ xn] | [x0, .., xn] => (x0, xn),
427 let msg = if end.is_doc_comment() {
428 "expected item after doc comment"
430 "expected item after attributes"
432 let mut err = self.struct_span_err(end.span, msg);
433 if end.is_doc_comment() {
434 err.span_label(end.span, "this doc comment doesn't document anything");
436 if let [.., penultimate, _] = attrs {
437 err.span_label(start.span.to(penultimate.span), "other attributes here");
442 fn is_async_fn(&self) -> bool {
443 self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
446 fn parse_polarity(&mut self) -> ast::ImplPolarity {
447 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
448 if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
450 ast::ImplPolarity::Negative(self.prev_token.span)
452 ast::ImplPolarity::Positive
456 /// Parses an implementation item.
459 /// impl<'a, T> TYPE { /* impl items */ }
460 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
461 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
462 /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
465 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
467 /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
468 /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
472 attrs: &mut Vec<Attribute>,
473 defaultness: Defaultness,
474 ) -> PResult<'a, ItemInfo> {
475 let unsafety = self.parse_unsafety();
476 self.expect_keyword(kw::Impl)?;
478 // First, parse generic parameters if necessary.
479 let mut generics = if self.choose_generics_over_qpath(0) {
480 self.parse_generics()?
482 let mut generics = Generics::default();
484 // /\ this is where `generics.span` should point when there are no type params.
485 generics.span = self.prev_token.span.shrink_to_hi();
489 let constness = self.parse_constness();
490 if let Const::Yes(span) = constness {
491 self.sess.gated_spans.gate(sym::const_trait_impl, span);
494 let polarity = self.parse_polarity();
496 // Parse both types and traits as a type, then reinterpret if necessary.
497 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
498 let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
500 let span = self.prev_token.span.between(self.token.span);
501 self.struct_span_err(span, "missing trait in a trait impl").emit();
503 kind: TyKind::Path(None, err_path(span)),
512 // If `for` is missing we try to recover.
513 let has_for = self.eat_keyword(kw::For);
514 let missing_for_span = self.prev_token.span.between(self.token.span);
516 let ty_second = if self.token == token::DotDot {
517 // We need to report this error after `cfg` expansion for compatibility reasons
518 self.bump(); // `..`, do not add it to expected tokens
519 Some(self.mk_ty(self.prev_token.span, TyKind::Err))
520 } else if has_for || self.token.can_begin_type() {
521 Some(self.parse_ty()?)
526 generics.where_clause = self.parse_where_clause()?;
528 let impl_items = self.parse_item_list(attrs, |p| p.parse_impl_item())?;
530 let item_kind = match ty_second {
532 // impl Trait for Type
534 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
535 .span_suggestion_short(
539 Applicability::MachineApplicable,
544 let ty_first = ty_first.into_inner();
545 let path = match ty_first.kind {
546 // This notably includes paths passed through `ty` macro fragments (#46438).
547 TyKind::Path(None, path) => path,
549 self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
550 err_path(ty_first.span)
553 let trait_ref = TraitRef { path, ref_id: ty_first.id };
561 of_trait: Some(trait_ref),
581 Ok((Ident::invalid(), item_kind))
584 fn parse_item_list<T>(
586 attrs: &mut Vec<Attribute>,
587 mut parse_item: impl FnMut(&mut Parser<'a>) -> PResult<'a, Option<Option<T>>>,
588 ) -> PResult<'a, Vec<T>> {
589 let open_brace_span = self.token.span;
590 self.expect(&token::OpenDelim(token::Brace))?;
591 attrs.append(&mut self.parse_inner_attributes()?);
593 let mut items = Vec::new();
594 while !self.eat(&token::CloseDelim(token::Brace)) {
595 if self.recover_doc_comment_before_brace() {
598 match parse_item(self) {
600 // We have to bail or we'll potentially never make progress.
601 let non_item_span = self.token.span;
602 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
603 self.struct_span_err(non_item_span, "non-item in item list")
604 .span_label(open_brace_span, "item list starts here")
605 .span_label(non_item_span, "non-item starts here")
606 .span_label(self.prev_token.span, "item list ends here")
610 Ok(Some(item)) => items.extend(item),
612 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
613 err.span_label(open_brace_span, "while parsing this item list starting here")
614 .span_label(self.prev_token.span, "the item list ends here")
623 /// Recover on a doc comment before `}`.
624 fn recover_doc_comment_before_brace(&mut self) -> bool {
625 if let token::DocComment(..) = self.token.kind {
626 if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
631 "found a documentation comment that doesn't document anything",
633 .span_label(self.token.span, "this doc comment doesn't document anything")
635 "doc comments must come before what they document, maybe a \
636 comment was intended with `//`?",
646 /// Parses defaultness (i.e., `default` or nothing).
647 fn parse_defaultness(&mut self) -> Defaultness {
648 // We are interested in `default` followed by another identifier.
649 // However, we must avoid keywords that occur as binary operators.
650 // Currently, the only applicable keyword is `as` (`default as Ty`).
651 if self.check_keyword(kw::Default)
652 && self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
654 self.bump(); // `default`
655 Defaultness::Default(self.prev_token.uninterpolated_span())
661 /// Is this an `(unsafe auto? | auto) trait` item?
662 fn check_auto_or_unsafe_trait_item(&mut self) -> bool {
664 self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait])
666 || self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
669 /// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
670 fn parse_item_trait(&mut self, attrs: &mut Vec<Attribute>, lo: Span) -> PResult<'a, ItemInfo> {
671 let unsafety = self.parse_unsafety();
672 // Parse optional `auto` prefix.
673 let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
675 self.expect_keyword(kw::Trait)?;
676 let ident = self.parse_ident()?;
677 let mut tps = self.parse_generics()?;
679 // Parse optional colon and supertrait bounds.
680 let had_colon = self.eat(&token::Colon);
681 let span_at_colon = self.prev_token.span;
682 let bounds = if had_colon {
683 self.parse_generic_bounds(Some(self.prev_token.span))?
688 let span_before_eq = self.prev_token.span;
689 if self.eat(&token::Eq) {
690 // It's a trait alias.
692 let span = span_at_colon.to(span_before_eq);
693 self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
696 let bounds = self.parse_generic_bounds(None)?;
697 tps.where_clause = self.parse_where_clause()?;
700 let whole_span = lo.to(self.prev_token.span);
701 if is_auto == IsAuto::Yes {
702 let msg = "trait aliases cannot be `auto`";
703 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
705 if let Unsafe::Yes(_) = unsafety {
706 let msg = "trait aliases cannot be `unsafe`";
707 self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
710 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
712 Ok((ident, ItemKind::TraitAlias(tps, bounds)))
714 // It's a normal trait.
715 tps.where_clause = self.parse_where_clause()?;
716 let items = self.parse_item_list(attrs, |p| p.parse_trait_item())?;
717 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, items)))
721 pub fn parse_impl_item(&mut self) -> PResult<'a, Option<Option<P<AssocItem>>>> {
722 self.parse_assoc_item(|_| true)
725 pub fn parse_trait_item(&mut self) -> PResult<'a, Option<Option<P<AssocItem>>>> {
726 self.parse_assoc_item(|edition| edition >= Edition::Edition2018)
729 /// Parses associated items.
730 fn parse_assoc_item(&mut self, req_name: ReqName) -> PResult<'a, Option<Option<P<AssocItem>>>> {
731 Ok(self.parse_item_(req_name)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
732 let kind = match AssocItemKind::try_from(kind) {
734 Err(kind) => match kind {
735 ItemKind::Static(a, _, b) => {
736 self.struct_span_err(span, "associated `static` items are not allowed")
738 AssocItemKind::Const(Defaultness::Final, a, b)
740 _ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
743 Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
747 /// Parses a `type` alias with the following grammar:
749 /// TypeAlias = "type" Ident Generics {":" GenericBounds}? {"=" Ty}? ";" ;
751 /// The `"type"` has already been eaten.
752 fn parse_type_alias(&mut self, def: Defaultness) -> PResult<'a, ItemInfo> {
753 let ident = self.parse_ident()?;
754 let mut generics = self.parse_generics()?;
756 // Parse optional colon and param bounds.
758 if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
759 generics.where_clause = self.parse_where_clause()?;
761 let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
764 Ok((ident, ItemKind::TyAlias(def, generics, bounds, default)))
767 /// Parses a `UseTree`.
770 /// USE_TREE = [`::`] `*` |
771 /// [`::`] `{` USE_TREE_LIST `}` |
773 /// PATH `::` `{` USE_TREE_LIST `}` |
774 /// PATH [`as` IDENT]
776 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
777 let lo = self.token.span;
779 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo(), tokens: None };
780 let kind = if self.check(&token::OpenDelim(token::Brace))
781 || self.check(&token::BinOp(token::Star))
782 || self.is_import_coupler()
784 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
785 let mod_sep_ctxt = self.token.span.ctxt();
786 if self.eat(&token::ModSep) {
789 .push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
792 self.parse_use_tree_glob_or_nested()?
794 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
795 prefix = self.parse_path(PathStyle::Mod)?;
797 if self.eat(&token::ModSep) {
798 self.parse_use_tree_glob_or_nested()?
800 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
804 Ok(UseTree { prefix, kind, span: lo.to(self.prev_token.span) })
807 /// Parses `*` or `{...}`.
808 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
809 Ok(if self.eat(&token::BinOp(token::Star)) {
812 UseTreeKind::Nested(self.parse_use_tree_list()?)
816 /// Parses a `UseTreeKind::Nested(list)`.
819 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
821 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
822 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
826 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
827 if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
830 fn parse_ident_or_underscore(&mut self) -> PResult<'a, Ident> {
831 match self.token.ident() {
832 Some((ident @ Ident { name: kw::Underscore, .. }, false)) => {
836 _ => self.parse_ident(),
840 /// Parses `extern crate` links.
845 /// extern crate foo;
846 /// extern crate bar as foo;
848 fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemInfo> {
849 // Accept `extern crate name-like-this` for better diagnostics
850 let orig_name = self.parse_crate_name_with_dashes()?;
851 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
852 (rename, Some(orig_name.name))
857 Ok((item_name, ItemKind::ExternCrate(orig_name)))
860 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, Ident> {
861 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
862 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
864 let mut ident = if self.token.is_keyword(kw::SelfLower) {
865 self.parse_path_segment_ident()
869 let mut idents = vec![];
870 let mut replacement = vec![];
871 let mut fixed_crate_name = false;
872 // Accept `extern crate name-like-this` for better diagnostics.
873 let dash = token::BinOp(token::BinOpToken::Minus);
874 if self.token == dash {
875 // Do not include `-` as part of the expected tokens list.
876 while self.eat(&dash) {
877 fixed_crate_name = true;
878 replacement.push((self.prev_token.span, "_".to_string()));
879 idents.push(self.parse_ident()?);
882 if fixed_crate_name {
883 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
884 let mut fixed_name = format!("{}", ident.name);
886 fixed_name.push_str(&format!("_{}", part.name));
888 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
890 self.struct_span_err(fixed_name_sp, error_msg)
891 .span_label(fixed_name_sp, "dash-separated idents are not valid")
892 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
898 /// Parses `extern` for foreign ABIs modules.
900 /// `extern` is expected to have been consumed before calling this method.
904 /// ```ignore (only-for-syntax-highlight)
908 fn parse_item_foreign_mod(
910 attrs: &mut Vec<Attribute>,
912 ) -> PResult<'a, ItemInfo> {
913 let abi = self.parse_abi(); // ABI?
914 let items = self.parse_item_list(attrs, |p| p.parse_foreign_item())?;
915 let module = ast::ForeignMod { unsafety, abi, items };
916 Ok((Ident::invalid(), ItemKind::ForeignMod(module)))
919 /// Parses a foreign item (one in an `extern { ... }` block).
920 pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
921 Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
922 let kind = match ForeignItemKind::try_from(kind) {
924 Err(kind) => match kind {
925 ItemKind::Const(_, a, b) => {
926 self.error_on_foreign_const(span, ident);
927 ForeignItemKind::Static(a, Mutability::Not, b)
929 _ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
932 Some(P(Item { attrs, id, span, vis, ident, kind, tokens }))
936 fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
937 let span = self.sess.source_map().guess_head_span(span);
938 let descr = kind.descr();
939 self.struct_span_err(span, &format!("{} is not supported in {}", descr, ctx))
940 .help(&format!("consider moving the {} out to a nearby module scope", descr))
945 fn error_on_foreign_const(&self, span: Span, ident: Ident) {
946 self.struct_span_err(ident.span, "extern items cannot be `const`")
948 span.with_hi(ident.span.lo()),
949 "try using a static value",
950 "static ".to_string(),
951 Applicability::MachineApplicable,
953 .note("for more information, visit https://doc.rust-lang.org/std/keyword.extern.html")
957 fn is_unsafe_foreign_mod(&self) -> bool {
958 self.token.is_keyword(kw::Unsafe)
959 && self.is_keyword_ahead(1, &[kw::Extern])
961 2 + self.look_ahead(2, |t| t.can_begin_literal_maybe_minus() as usize),
962 |t| t.kind == token::OpenDelim(token::Brace),
966 fn is_static_global(&mut self) -> bool {
967 if self.check_keyword(kw::Static) {
968 // Check if this could be a closure.
969 !self.look_ahead(1, |token| {
970 if token.is_keyword(kw::Move) {
973 matches!(token.kind, token::BinOp(token::Or) | token::OrOr)
980 /// Recover on `const mut` with `const` already eaten.
981 fn recover_const_mut(&mut self, const_span: Span) {
982 if self.eat_keyword(kw::Mut) {
983 let span = self.prev_token.span;
984 self.struct_span_err(span, "const globals cannot be mutable")
985 .span_label(span, "cannot be mutable")
988 "you might want to declare a static instead",
990 Applicability::MaybeIncorrect,
996 /// Recover on `const impl` with `const` already eaten.
997 fn recover_const_impl(
1000 attrs: &mut Vec<Attribute>,
1001 defaultness: Defaultness,
1002 ) -> PResult<'a, ItemInfo> {
1003 let impl_span = self.token.span;
1004 let mut err = self.expected_ident_found();
1005 let mut impl_info = self.parse_item_impl(attrs, defaultness)?;
1007 // only try to recover if this is implementing a trait for a type
1008 ItemKind::Impl { of_trait: Some(ref trai), ref mut constness, .. } => {
1009 *constness = Const::Yes(const_span);
1011 let before_trait = trai.path.span.shrink_to_lo();
1012 let const_up_to_impl = const_span.with_hi(impl_span.lo());
1013 err.multipart_suggestion(
1014 "you might have meant to write a const trait impl",
1015 vec![(const_up_to_impl, "".to_owned()), (before_trait, "const ".to_owned())],
1016 Applicability::MaybeIncorrect,
1020 ItemKind::Impl { .. } => return Err(err),
1021 _ => unreachable!(),
1026 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty (= $expr)?` with
1027 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1029 /// When `m` is `"const"`, `$ident` may also be `"_"`.
1030 fn parse_item_global(
1032 m: Option<Mutability>,
1033 ) -> PResult<'a, (Ident, P<Ty>, Option<P<ast::Expr>>)> {
1034 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1036 // Parse the type of a `const` or `static mut?` item.
1037 // That is, the `":" $ty` fragment.
1038 let ty = if self.eat(&token::Colon) {
1041 self.recover_missing_const_type(id, m)
1044 let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
1045 self.expect_semi()?;
1049 /// We were supposed to parse `:` but the `:` was missing.
1050 /// This means that the type is missing.
1051 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1052 // Construct the error and stash it away with the hope
1053 // that typeck will later enrich the error with a type.
1054 let kind = match m {
1055 Some(Mutability::Mut) => "static mut",
1056 Some(Mutability::Not) => "static",
1059 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1060 err.span_suggestion(
1062 "provide a type for the item",
1063 format!("{}: <type>", id),
1064 Applicability::HasPlaceholders,
1066 err.stash(id.span, StashKey::ItemNoType);
1068 // The user intended that the type be inferred,
1069 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1070 P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID, tokens: None })
1073 /// Parses an enum declaration.
1074 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1075 let id = self.parse_ident()?;
1076 let mut generics = self.parse_generics()?;
1077 generics.where_clause = self.parse_where_clause()?;
1080 self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
1081 self.recover_stmt();
1085 let enum_definition =
1086 EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
1087 Ok((id, ItemKind::Enum(enum_definition, generics)))
1090 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1091 let variant_attrs = self.parse_outer_attributes()?;
1092 let vlo = self.token.span;
1094 let vis = self.parse_visibility(FollowedByType::No)?;
1095 if !self.recover_nested_adt_item(kw::Enum)? {
1098 let ident = self.parse_ident()?;
1100 let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
1101 // Parse a struct variant.
1102 let (fields, recovered) = self.parse_record_struct_body()?;
1103 VariantData::Struct(fields, recovered)
1104 } else if self.check(&token::OpenDelim(token::Paren)) {
1105 VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID)
1107 VariantData::Unit(DUMMY_NODE_ID)
1111 if self.eat(&token::Eq) { Some(self.parse_anon_const_expr()?) } else { None };
1113 let vr = ast::Variant {
1117 attrs: variant_attrs,
1120 span: vlo.to(self.prev_token.span),
1121 is_placeholder: false,
1127 /// Parses `struct Foo { ... }`.
1128 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1129 let class_name = self.parse_ident()?;
1131 let mut generics = self.parse_generics()?;
1133 // There is a special case worth noting here, as reported in issue #17904.
1134 // If we are parsing a tuple struct it is the case that the where clause
1135 // should follow the field list. Like so:
1137 // struct Foo<T>(T) where T: Copy;
1139 // If we are parsing a normal record-style struct it is the case
1140 // that the where clause comes before the body, and after the generics.
1141 // So if we look ahead and see a brace or a where-clause we begin
1142 // parsing a record style struct.
1144 // Otherwise if we look ahead and see a paren we parse a tuple-style
1147 let vdata = if self.token.is_keyword(kw::Where) {
1148 generics.where_clause = self.parse_where_clause()?;
1149 if self.eat(&token::Semi) {
1150 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1151 VariantData::Unit(DUMMY_NODE_ID)
1153 // If we see: `struct Foo<T> where T: Copy { ... }`
1154 let (fields, recovered) = self.parse_record_struct_body()?;
1155 VariantData::Struct(fields, recovered)
1157 // No `where` so: `struct Foo<T>;`
1158 } else if self.eat(&token::Semi) {
1159 VariantData::Unit(DUMMY_NODE_ID)
1160 // Record-style struct definition
1161 } else if self.token == token::OpenDelim(token::Brace) {
1162 let (fields, recovered) = self.parse_record_struct_body()?;
1163 VariantData::Struct(fields, recovered)
1164 // Tuple-style struct definition with optional where-clause.
1165 } else if self.token == token::OpenDelim(token::Paren) {
1166 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1167 generics.where_clause = self.parse_where_clause()?;
1168 self.expect_semi()?;
1171 let token_str = super::token_descr(&self.token);
1173 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1176 let mut err = self.struct_span_err(self.token.span, msg);
1177 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1181 Ok((class_name, ItemKind::Struct(vdata, generics)))
1184 /// Parses `union Foo { ... }`.
1185 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1186 let class_name = self.parse_ident()?;
1188 let mut generics = self.parse_generics()?;
1190 let vdata = if self.token.is_keyword(kw::Where) {
1191 generics.where_clause = self.parse_where_clause()?;
1192 let (fields, recovered) = self.parse_record_struct_body()?;
1193 VariantData::Struct(fields, recovered)
1194 } else if self.token == token::OpenDelim(token::Brace) {
1195 let (fields, recovered) = self.parse_record_struct_body()?;
1196 VariantData::Struct(fields, recovered)
1198 let token_str = super::token_descr(&self.token);
1199 let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
1200 let mut err = self.struct_span_err(self.token.span, msg);
1201 err.span_label(self.token.span, "expected `where` or `{` after union name");
1205 Ok((class_name, ItemKind::Union(vdata, generics)))
1208 fn parse_record_struct_body(
1210 ) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
1211 let mut fields = Vec::new();
1212 let mut recovered = false;
1213 if self.eat(&token::OpenDelim(token::Brace)) {
1214 while self.token != token::CloseDelim(token::Brace) {
1215 let field = self.parse_struct_decl_field().map_err(|e| {
1216 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1221 Ok(field) => fields.push(field),
1228 self.eat(&token::CloseDelim(token::Brace));
1230 let token_str = super::token_descr(&self.token);
1231 let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
1232 let mut err = self.struct_span_err(self.token.span, msg);
1233 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1237 Ok((fields, recovered))
1240 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
1241 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1242 // Unit like structs are handled in parse_item_struct function
1243 self.parse_paren_comma_seq(|p| {
1244 let attrs = p.parse_outer_attributes()?;
1245 let lo = p.token.span;
1246 let vis = p.parse_visibility(FollowedByType::Yes)?;
1247 let ty = p.parse_ty()?;
1249 span: lo.to(ty.span),
1255 is_placeholder: false,
1261 /// Parses an element of a struct declaration.
1262 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
1263 let attrs = self.parse_outer_attributes()?;
1264 let lo = self.token.span;
1265 let vis = self.parse_visibility(FollowedByType::No)?;
1266 self.parse_single_struct_field(lo, vis, attrs)
1269 /// Parses a structure field declaration.
1270 fn parse_single_struct_field(
1274 attrs: Vec<Attribute>,
1275 ) -> PResult<'a, StructField> {
1276 let mut seen_comma: bool = false;
1277 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1278 if self.token == token::Comma {
1281 match self.token.kind {
1285 token::CloseDelim(token::Brace) => {}
1286 token::DocComment(..) => {
1287 let previous_span = self.prev_token.span;
1288 let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1289 self.bump(); // consume the doc comment
1290 let comma_after_doc_seen = self.eat(&token::Comma);
1291 // `seen_comma` is always false, because we are inside doc block
1292 // condition is here to make code more readable
1293 if !seen_comma && comma_after_doc_seen {
1296 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1300 let sp = self.sess.source_map().next_point(previous_span);
1301 err.span_suggestion(
1303 "missing comma here",
1305 Applicability::MachineApplicable,
1312 let sp = self.prev_token.span.shrink_to_hi();
1313 let mut err = self.struct_span_err(
1315 &format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
1318 // Try to recover extra trailing angle brackets
1319 let mut recovered = false;
1320 if let TyKind::Path(_, Path { segments, .. }) = &a_var.ty.kind {
1321 if let Some(last_segment) = segments.last() {
1322 recovered = self.check_trailing_angle_brackets(
1324 &[&token::Comma, &token::CloseDelim(token::Brace)],
1327 // Handle a case like `Vec<u8>>,` where we can continue parsing fields
1329 self.eat(&token::Comma);
1330 // `check_trailing_angle_brackets` already emitted a nicer error
1336 if self.token.is_ident() {
1337 // This is likely another field; emit the diagnostic and keep going
1338 err.span_suggestion(
1340 "try adding a comma",
1342 Applicability::MachineApplicable,
1349 // Make sure an error was emitted (either by recovering an angle bracket,
1350 // or by finding an identifier as the next token), since we're
1351 // going to continue parsing
1352 assert!(self.sess.span_diagnostic.has_errors());
1361 /// Parses a structure field.
1362 fn parse_name_and_ty(
1366 attrs: Vec<Attribute>,
1367 ) -> PResult<'a, StructField> {
1368 let name = self.parse_ident_common(false)?;
1369 self.expect(&token::Colon)?;
1370 let ty = self.parse_ty()?;
1372 span: lo.to(self.prev_token.span),
1378 is_placeholder: false,
1382 /// Parses a declarative macro 2.0 definition.
1383 /// The `macro` keyword has already been parsed.
1385 /// MacBody = "{" TOKEN_STREAM "}" ;
1386 /// MacParams = "(" TOKEN_STREAM ")" ;
1387 /// DeclMac = "macro" Ident MacParams? MacBody ;
1389 fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemInfo> {
1390 let ident = self.parse_ident()?;
1391 let body = if self.check(&token::OpenDelim(token::Brace)) {
1392 self.parse_mac_args()? // `MacBody`
1393 } else if self.check(&token::OpenDelim(token::Paren)) {
1394 let params = self.parse_token_tree(); // `MacParams`
1395 let pspan = params.span();
1396 if !self.check(&token::OpenDelim(token::Brace)) {
1397 return self.unexpected();
1399 let body = self.parse_token_tree(); // `MacBody`
1400 // Convert `MacParams MacBody` into `{ MacParams => MacBody }`.
1401 let bspan = body.span();
1402 let arrow = TokenTree::token(token::FatArrow, pspan.between(bspan)); // `=>`
1403 let tokens = TokenStream::new(vec![params.into(), arrow.into(), body.into()]);
1404 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1405 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1407 return self.unexpected();
1410 self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
1411 Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: false })))
1414 /// Is this unambiguously the start of a `macro_rules! foo` item defnition?
1415 fn is_macro_rules_item(&mut self) -> bool {
1416 self.check_keyword(kw::MacroRules)
1417 && self.look_ahead(1, |t| *t == token::Not)
1418 && self.look_ahead(2, |t| t.is_ident())
1421 /// Parses a `macro_rules! foo { ... }` declarative macro.
1422 fn parse_item_macro_rules(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
1423 self.expect_keyword(kw::MacroRules)?; // `macro_rules`
1424 self.expect(&token::Not)?; // `!`
1426 let ident = self.parse_ident()?;
1427 let body = self.parse_mac_args()?;
1428 self.eat_semi_for_macro_if_needed(&body);
1429 self.complain_if_pub_macro(vis, true);
1431 Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: true })))
1434 /// Item macro invocations or `macro_rules!` definitions need inherited visibility.
1435 /// If that's not the case, emit an error.
1436 fn complain_if_pub_macro(&self, vis: &Visibility, macro_rules: bool) {
1437 if let VisibilityKind::Inherited = vis.kind {
1441 let vstr = pprust::vis_to_string(vis);
1442 let vstr = vstr.trim_end();
1444 let msg = format!("can't qualify macro_rules invocation with `{}`", vstr);
1445 self.struct_span_err(vis.span, &msg)
1448 "try exporting the macro",
1449 "#[macro_export]".to_owned(),
1450 Applicability::MaybeIncorrect, // speculative
1454 self.struct_span_err(vis.span, "can't qualify macro invocation with `pub`")
1457 "remove the visibility",
1459 Applicability::MachineApplicable,
1461 .help(&format!("try adjusting the macro to put `{}` inside the invocation", vstr))
1466 fn eat_semi_for_macro_if_needed(&mut self, args: &MacArgs) {
1467 if args.need_semicolon() && !self.eat(&token::Semi) {
1468 self.report_invalid_macro_expansion_item(args);
1472 fn report_invalid_macro_expansion_item(&self, args: &MacArgs) {
1473 let span = args.span().expect("undelimited macro call");
1474 let mut err = self.struct_span_err(
1476 "macros that expand to items must be delimited with braces or followed by a semicolon",
1478 if self.unclosed_delims.is_empty() {
1479 let DelimSpan { open, close } = match args {
1480 MacArgs::Empty | MacArgs::Eq(..) => unreachable!(),
1481 MacArgs::Delimited(dspan, ..) => *dspan,
1483 err.multipart_suggestion(
1484 "change the delimiters to curly braces",
1485 vec![(open, "{".to_string()), (close, '}'.to_string())],
1486 Applicability::MaybeIncorrect,
1489 err.span_suggestion(
1491 "change the delimiters to curly braces",
1492 " { /* items */ }".to_string(),
1493 Applicability::HasPlaceholders,
1496 err.span_suggestion(
1497 span.shrink_to_hi(),
1500 Applicability::MaybeIncorrect,
1505 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1506 /// it is, we try to parse the item and report error about nested types.
1507 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1508 if (self.token.is_keyword(kw::Enum)
1509 || self.token.is_keyword(kw::Struct)
1510 || self.token.is_keyword(kw::Union))
1511 && self.look_ahead(1, |t| t.is_ident())
1513 let kw_token = self.token.clone();
1514 let kw_str = pprust::token_to_string(&kw_token);
1515 let item = self.parse_item()?;
1517 self.struct_span_err(
1519 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1523 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1525 Applicability::MaybeIncorrect,
1528 // We successfully parsed the item but we must inform the caller about nested problem.
1535 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1537 /// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
1538 type ReqName = fn(Edition) -> bool;
1540 /// Parsing of functions and methods.
1541 impl<'a> Parser<'a> {
1542 /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
1545 attrs: &mut Vec<Attribute>,
1548 ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
1549 let header = self.parse_fn_front_matter()?; // `const ... fn`
1550 let ident = self.parse_ident()?; // `foo`
1551 let mut generics = self.parse_generics()?; // `<'a, T, ...>`
1552 let decl = self.parse_fn_decl(req_name, AllowPlus::Yes)?; // `(p: u8, ...)`
1553 generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
1555 let mut sig_hi = self.prev_token.span;
1556 let body = self.parse_fn_body(attrs, &ident, &mut sig_hi)?; // `;` or `{ ... }`.
1557 let fn_sig_span = sig_lo.to(sig_hi);
1558 Ok((ident, FnSig { header, decl, span: fn_sig_span }, generics, body))
1561 /// Parse the "body" of a function.
1562 /// This can either be `;` when there's no body,
1563 /// or e.g. a block when the function is a provided one.
1566 attrs: &mut Vec<Attribute>,
1569 ) -> PResult<'a, Option<P<Block>>> {
1570 let (inner_attrs, body) = if self.eat(&token::Semi) {
1571 // Include the trailing semicolon in the span of the signature
1572 *sig_hi = self.prev_token.span;
1574 } else if self.check(&token::OpenDelim(token::Brace)) || self.token.is_whole_block() {
1575 self.parse_inner_attrs_and_block().map(|(attrs, body)| (attrs, Some(body)))?
1576 } else if self.token.kind == token::Eq {
1577 // Recover `fn foo() = $expr;`.
1579 let eq_sp = self.prev_token.span;
1580 let _ = self.parse_expr()?;
1581 self.expect_semi()?; // `;`
1582 let span = eq_sp.to(self.prev_token.span);
1583 self.struct_span_err(span, "function body cannot be `= expression;`")
1584 .multipart_suggestion(
1585 "surround the expression with `{` and `}` instead of `=` and `;`",
1586 vec![(eq_sp, "{".to_string()), (self.prev_token.span, " }".to_string())],
1587 Applicability::MachineApplicable,
1590 (Vec::new(), Some(self.mk_block_err(span)))
1592 if let Err(mut err) =
1593 self.expected_one_of_not_found(&[], &[token::Semi, token::OpenDelim(token::Brace)])
1595 if self.token.kind == token::CloseDelim(token::Brace) {
1596 // The enclosing `mod`, `trait` or `impl` is being closed, so keep the `fn` in
1597 // the AST for typechecking.
1598 err.span_label(ident.span, "while parsing this `fn`");
1608 attrs.extend(inner_attrs);
1612 /// Is the current token the start of an `FnHeader` / not a valid parse?
1613 pub(super) fn check_fn_front_matter(&mut self) -> bool {
1614 // We use an over-approximation here.
1615 // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
1616 const QUALS: [Symbol; 4] = [kw::Const, kw::Async, kw::Unsafe, kw::Extern];
1617 self.check_keyword(kw::Fn) // Definitely an `fn`.
1618 // `$qual fn` or `$qual $qual`:
1619 || QUALS.iter().any(|&kw| self.check_keyword(kw))
1620 && self.look_ahead(1, |t| {
1621 // `$qual fn`, e.g. `const fn` or `async fn`.
1622 t.is_keyword(kw::Fn)
1623 // Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
1624 || t.is_non_raw_ident_where(|i| QUALS.contains(&i.name)
1625 // Rule out 2015 `const async: T = val`.
1627 // Rule out unsafe extern block.
1628 && !self.is_unsafe_foreign_mod())
1631 || self.check_keyword(kw::Extern)
1632 && self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
1633 && self.look_ahead(2, |t| t.is_keyword(kw::Fn))
1636 /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
1637 /// up to and including the `fn` keyword. The formal grammar is:
1640 /// Extern = "extern" StringLit? ;
1641 /// FnQual = "const"? "async"? "unsafe"? Extern? ;
1642 /// FnFrontMatter = FnQual "fn" ;
1644 pub(super) fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1645 let constness = self.parse_constness();
1646 let asyncness = self.parse_asyncness();
1647 let unsafety = self.parse_unsafety();
1648 let ext = self.parse_extern()?;
1650 if let Async::Yes { span, .. } = asyncness {
1651 self.ban_async_in_2015(span);
1654 if !self.eat_keyword(kw::Fn) {
1655 // It is possible for `expect_one_of` to recover given the contents of
1656 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1657 // account for this.
1658 if !self.expect_one_of(&[], &[])? {
1663 Ok(FnHeader { constness, unsafety, asyncness, ext })
1666 /// We are parsing `async fn`. If we are on Rust 2015, emit an error.
1667 fn ban_async_in_2015(&self, span: Span) {
1668 if span.rust_2015() {
1669 let diag = self.diagnostic();
1670 struct_span_err!(diag, span, E0670, "`async fn` is not permitted in the 2015 edition")
1671 .span_label(span, "to use `async fn`, switch to Rust 2018")
1672 .help("set `edition = \"2018\"` in `Cargo.toml`")
1673 .note("for more on editions, read https://doc.rust-lang.org/edition-guide")
1678 /// Parses the parameter list and result type of a function declaration.
1679 pub(super) fn parse_fn_decl(
1682 ret_allow_plus: AllowPlus,
1683 ) -> PResult<'a, P<FnDecl>> {
1685 inputs: self.parse_fn_params(req_name)?,
1686 output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
1690 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1691 fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
1692 let mut first_param = true;
1693 // Parse the arguments, starting out with `self` being allowed...
1694 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1695 let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
1697 let lo = p.prev_token.span;
1698 // Skip every token until next possible arg or end.
1699 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1700 // Create a placeholder argument for proper arg count (issue #34264).
1701 Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_token.span))))
1703 // ...now that we've parsed the first argument, `self` is no longer allowed.
1704 first_param = false;
1707 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1708 self.deduplicate_recovered_params_names(&mut params);
1712 /// Parses a single function parameter.
1714 /// - `self` is syntactically allowed when `first_param` holds.
1715 fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
1716 let lo = self.token.span;
1717 let attrs = self.parse_outer_attributes()?;
1719 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1720 if let Some(mut param) = self.parse_self_param()? {
1721 param.attrs = attrs.into();
1722 return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
1725 let is_name_required = match self.token.kind {
1726 token::DotDotDot => false,
1727 _ => req_name(self.token.span.edition()),
1729 let (pat, ty) = if is_name_required || self.is_named_param() {
1730 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1732 let pat = self.parse_fn_param_pat()?;
1733 if let Err(mut err) = self.expect(&token::Colon) {
1734 return if let Some(ident) =
1735 self.parameter_without_type(&mut err, pat, is_name_required, first_param)
1738 Ok(dummy_arg(ident))
1744 self.eat_incorrect_doc_comment_for_param_type();
1745 (pat, self.parse_ty_for_param()?)
1747 debug!("parse_param_general ident_to_pat");
1748 let parser_snapshot_before_ty = self.clone();
1749 self.eat_incorrect_doc_comment_for_param_type();
1750 let mut ty = self.parse_ty_for_param();
1752 && self.token != token::Comma
1753 && self.token != token::CloseDelim(token::Paren)
1755 // This wasn't actually a type, but a pattern looking like a type,
1756 // so we are going to rollback and re-parse for recovery.
1757 ty = self.unexpected();
1761 let ident = Ident::new(kw::Invalid, self.prev_token.span);
1762 let bm = BindingMode::ByValue(Mutability::Not);
1763 let pat = self.mk_pat_ident(ty.span, bm, ident);
1766 // If this is a C-variadic argument and we hit an error, return the error.
1767 Err(err) if self.token == token::DotDotDot => return Err(err),
1768 // Recover from attempting to parse the argument as a type without pattern.
1771 *self = parser_snapshot_before_ty;
1772 self.recover_arg_parse()?
1777 let span = lo.until(self.token.span);
1780 attrs: attrs.into(),
1781 id: ast::DUMMY_NODE_ID,
1782 is_placeholder: false,
1789 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1790 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1791 // Extract an identifier *after* having confirmed that the token is one.
1792 let expect_self_ident = |this: &mut Self| match this.token.ident() {
1793 Some((ident, false)) => {
1797 _ => unreachable!(),
1799 // Is `self` `n` tokens ahead?
1800 let is_isolated_self = |this: &Self, n| {
1801 this.is_keyword_ahead(n, &[kw::SelfLower])
1802 && this.look_ahead(n + 1, |t| t != &token::ModSep)
1804 // Is `mut self` `n` tokens ahead?
1805 let is_isolated_mut_self =
1806 |this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
1807 // Parse `self` or `self: TYPE`. We already know the current token is `self`.
1808 let parse_self_possibly_typed = |this: &mut Self, m| {
1809 let eself_ident = expect_self_ident(this);
1810 let eself_hi = this.prev_token.span;
1811 let eself = if this.eat(&token::Colon) {
1812 SelfKind::Explicit(this.parse_ty()?, m)
1816 Ok((eself, eself_ident, eself_hi))
1818 // Recover for the grammar `*self`, `*const self`, and `*mut self`.
1819 let recover_self_ptr = |this: &mut Self| {
1820 let msg = "cannot pass `self` by raw pointer";
1821 let span = this.token.span;
1822 this.struct_span_err(span, msg).span_label(span, msg).emit();
1824 Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_token.span))
1827 // Parse optional `self` parameter of a method.
1828 // Only a limited set of initial token sequences is considered `self` parameters; anything
1829 // else is parsed as a normal function parameter list, so some lookahead is required.
1830 let eself_lo = self.token.span;
1831 let (eself, eself_ident, eself_hi) = match self.token.uninterpolate().kind {
1832 token::BinOp(token::And) => {
1833 let eself = if is_isolated_self(self, 1) {
1836 SelfKind::Region(None, Mutability::Not)
1837 } else if is_isolated_mut_self(self, 1) {
1841 SelfKind::Region(None, Mutability::Mut)
1842 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
1845 let lt = self.expect_lifetime();
1846 SelfKind::Region(Some(lt), Mutability::Not)
1847 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
1850 let lt = self.expect_lifetime();
1852 SelfKind::Region(Some(lt), Mutability::Mut)
1857 (eself, expect_self_ident(self), self.prev_token.span)
1860 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
1862 recover_self_ptr(self)?
1864 // `*mut self` and `*const self`
1865 token::BinOp(token::Star)
1866 if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
1870 recover_self_ptr(self)?
1872 // `self` and `self: TYPE`
1873 token::Ident(..) if is_isolated_self(self, 0) => {
1874 parse_self_possibly_typed(self, Mutability::Not)?
1876 // `mut self` and `mut self: TYPE`
1877 token::Ident(..) if is_isolated_mut_self(self, 0) => {
1879 parse_self_possibly_typed(self, Mutability::Mut)?
1881 _ => return Ok(None),
1884 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
1885 Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
1888 fn is_named_param(&self) -> bool {
1889 let offset = match self.token.kind {
1890 token::Interpolated(ref nt) => match **nt {
1891 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
1894 token::BinOp(token::And) | token::AndAnd => 1,
1895 _ if self.token.is_keyword(kw::Mut) => 1,
1899 self.look_ahead(offset, |t| t.is_ident())
1900 && self.look_ahead(offset + 1, |t| t == &token::Colon)
1903 fn recover_first_param(&mut self) -> &'static str {
1905 .parse_outer_attributes()
1906 .and_then(|_| self.parse_self_param())
1907 .map_err(|mut e| e.cancel())
1909 Ok(Some(_)) => "method",