1 use super::{Parser, PathStyle, FollowedByType};
2 use super::diagnostics::{Error, dummy_arg, ConsumeClosingDelim};
4 use crate::maybe_whole;
6 use rustc_errors::{PResult, Applicability, DiagnosticBuilder, StashKey};
7 use rustc_error_codes::*;
8 use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item};
9 use syntax::ast::{AssocItem, AssocItemKind, ItemKind, UseTree, UseTreeKind};
10 use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
11 use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
12 use syntax::ast::{Ty, TyKind, Generics, TraitRef, EnumDef, Variant, VariantData, StructField};
13 use syntax::ast::{Mac, MacArgs, MacDelimiter, Block, BindingMode, FnDecl, FnSig, SelfKind, Param};
14 use syntax::print::pprust;
18 use syntax::tokenstream::{DelimSpan, TokenTree, TokenStream};
19 use syntax::struct_span_err;
20 use syntax_pos::BytePos;
21 use syntax_pos::source_map::{self, respan, Span};
22 use syntax_pos::symbol::{kw, sym, Symbol};
27 pub(super) type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute>>);
30 pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
31 let attrs = self.parse_outer_attributes()?;
32 self.parse_item_(attrs, true, false)
35 pub(super) fn parse_item_(
37 attrs: Vec<Attribute>,
39 attributes_allowed: bool,
40 ) -> PResult<'a, Option<P<Item>>> {
41 let mut unclosed_delims = vec![];
42 let (ret, tokens) = self.collect_tokens(|this| {
43 let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
44 unclosed_delims.append(&mut this.unclosed_delims);
47 self.unclosed_delims.append(&mut unclosed_delims);
49 // Once we've parsed an item and recorded the tokens we got while
50 // parsing we may want to store `tokens` into the item we're about to
51 // return. Note, though, that we specifically didn't capture tokens
52 // related to outer attributes. The `tokens` field here may later be
53 // used with procedural macros to convert this item back into a token
54 // stream, but during expansion we may be removing attributes as we go
57 // If we've got inner attributes then the `tokens` we've got above holds
58 // these inner attributes. If an inner attribute is expanded we won't
59 // actually remove it from the token stream, so we'll just keep yielding
60 // it (bad!). To work around this case for now we just avoid recording
61 // `tokens` if we detect any inner attributes. This should help keep
62 // expansion correct, but we should fix this bug one day!
65 if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
66 i.tokens = Some(tokens);
73 /// Parses one of the items allowed by the flags.
74 fn parse_item_implementation(
76 attrs: Vec<Attribute>,
78 attributes_allowed: bool,
79 ) -> PResult<'a, Option<P<Item>>> {
80 maybe_whole!(self, NtItem, |item| {
81 let mut item = item.into_inner();
82 let mut attrs = attrs;
83 mem::swap(&mut item.attrs, &mut attrs);
84 item.attrs.extend(attrs);
88 let lo = self.token.span;
90 let vis = self.parse_visibility(FollowedByType::No)?;
92 if self.eat_keyword(kw::Use) {
94 let item_ = ItemKind::Use(P(self.parse_use_tree()?));
97 let span = lo.to(self.prev_span);
98 let item = self.mk_item(span, Ident::invalid(), item_, vis, attrs);
99 return Ok(Some(item));
102 if self.eat_keyword(kw::Extern) {
103 let extern_sp = self.prev_span;
104 if self.eat_keyword(kw::Crate) {
105 return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
108 let abi = self.parse_abi();
110 if self.eat_keyword(kw::Fn) {
111 // EXTERN FUNCTION ITEM
112 let fn_span = self.prev_span;
113 let header = FnHeader {
114 unsafety: Unsafety::Normal,
115 asyncness: respan(fn_span, IsAsync::NotAsync),
116 constness: respan(fn_span, Constness::NotConst),
117 ext: Extern::from_abi(abi),
119 return self.parse_item_fn(lo, vis, attrs, header);
120 } else if self.check(&token::OpenDelim(token::Brace)) {
122 self.parse_item_foreign_mod(lo, abi, vis, attrs, extern_sp)?,
129 if self.is_static_global() {
132 let m = self.parse_mutability();
133 let info = self.parse_item_const(Some(m))?;
134 return self.mk_item_with_info(attrs, lo, vis, info);
137 if self.eat_keyword(kw::Const) {
138 let const_span = self.prev_span;
139 if [kw::Fn, kw::Unsafe, kw::Extern].iter().any(|k| self.check_keyword(*k)) {
140 // CONST FUNCTION ITEM
141 let unsafety = self.parse_unsafety();
143 if self.check_keyword(kw::Extern) {
144 self.sess.gated_spans.gate(sym::const_extern_fn, lo.to(self.token.span));
146 let ext = self.parse_extern()?;
149 let header = FnHeader {
151 asyncness: respan(const_span, IsAsync::NotAsync),
152 constness: respan(const_span, Constness::Const),
155 return self.parse_item_fn(lo, vis, attrs, header);
159 if self.eat_keyword(kw::Mut) {
160 let prev_span = self.prev_span;
161 self.struct_span_err(prev_span, "const globals cannot be mutable")
162 .span_label(prev_span, "cannot be mutable")
165 "you might want to declare a static instead",
167 Applicability::MaybeIncorrect,
172 let info = self.parse_item_const(None)?;
173 return self.mk_item_with_info(attrs, lo, vis, info);
176 // Parses `async unsafe? fn`.
177 if self.check_keyword(kw::Async) {
178 let async_span = self.token.span;
179 if self.is_keyword_ahead(1, &[kw::Fn])
180 || self.is_keyword_ahead(2, &[kw::Fn])
182 // ASYNC FUNCTION ITEM
183 self.bump(); // `async`
184 let unsafety = self.parse_unsafety(); // `unsafe`?
185 self.expect_keyword(kw::Fn)?; // `fn`
186 let fn_span = self.prev_span;
187 let asyncness = respan(async_span, IsAsync::Async {
188 closure_id: DUMMY_NODE_ID,
189 return_impl_trait_id: DUMMY_NODE_ID,
191 self.ban_async_in_2015(async_span);
192 let header = FnHeader {
195 constness: respan(fn_span, Constness::NotConst),
198 return self.parse_item_fn(lo, vis, attrs, header);
202 if self.check_keyword(kw::Unsafe) &&
203 self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
206 self.bump(); // `unsafe`
207 let info = self.parse_item_trait(lo, Unsafety::Unsafe)?;
208 return self.mk_item_with_info(attrs, lo, vis, info);
211 if self.check_keyword(kw::Impl) ||
212 self.check_keyword(kw::Unsafe) &&
213 self.is_keyword_ahead(1, &[kw::Impl]) ||
214 self.check_keyword(kw::Default) &&
215 self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
218 let defaultness = self.parse_defaultness();
219 let unsafety = self.parse_unsafety();
220 self.expect_keyword(kw::Impl)?;
221 let info = self.parse_item_impl(unsafety, defaultness)?;
222 return self.mk_item_with_info(attrs, lo, vis, info);
225 if self.check_keyword(kw::Fn) {
228 let fn_span = self.prev_span;
229 let header = FnHeader {
230 unsafety: Unsafety::Normal,
231 asyncness: respan(fn_span, IsAsync::NotAsync),
232 constness: respan(fn_span, Constness::NotConst),
235 return self.parse_item_fn(lo, vis, attrs, header);
238 if self.check_keyword(kw::Unsafe)
239 && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace))
241 // UNSAFE FUNCTION ITEM
242 self.bump(); // `unsafe`
243 // `{` is also expected after `unsafe`; in case of error, include it in the diagnostic.
244 self.check(&token::OpenDelim(token::Brace));
245 let ext = self.parse_extern()?;
246 self.expect_keyword(kw::Fn)?;
247 let fn_span = self.prev_span;
248 let header = FnHeader {
249 unsafety: Unsafety::Unsafe,
250 asyncness: respan(fn_span, IsAsync::NotAsync),
251 constness: respan(fn_span, Constness::NotConst),
254 return self.parse_item_fn(lo, vis, attrs, header);
257 if self.eat_keyword(kw::Mod) {
259 let info = self.parse_item_mod(&attrs[..])?;
260 return self.mk_item_with_info(attrs, lo, vis, info);
263 if self.eat_keyword(kw::Type) {
265 let (ident, ty, generics) = self.parse_type_alias()?;
266 let kind = ItemKind::TyAlias(ty, generics);
267 return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
270 if self.eat_keyword(kw::Enum) {
272 let info = self.parse_item_enum()?;
273 return self.mk_item_with_info(attrs, lo, vis, info);
276 if self.check_keyword(kw::Trait)
277 || (self.check_keyword(kw::Auto)
278 && self.is_keyword_ahead(1, &[kw::Trait]))
281 let info = self.parse_item_trait(lo, Unsafety::Normal)?;
282 return self.mk_item_with_info(attrs, lo, vis, info);
285 if self.eat_keyword(kw::Struct) {
287 let info = self.parse_item_struct()?;
288 return self.mk_item_with_info(attrs, lo, vis, info);
291 if self.is_union_item() {
294 let info = self.parse_item_union()?;
295 return self.mk_item_with_info(attrs, lo, vis, info);
298 if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
299 return Ok(Some(macro_def));
302 // Verify whether we have encountered a struct or method definition where the user forgot to
303 // add the `struct` or `fn` keyword after writing `pub`: `pub S {}`
304 if vis.node.is_pub() &&
305 self.check_ident() &&
306 self.look_ahead(1, |t| *t != token::Not)
308 // Space between `pub` keyword and the identifier
311 // ^^^ `sp` points here
312 let sp = self.prev_span.between(self.token.span);
313 let full_sp = self.prev_span.to(self.token.span);
314 let ident_sp = self.token.span;
315 if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
316 // possible public struct definition where `struct` was forgotten
317 let ident = self.parse_ident().unwrap();
318 let msg = format!("add `struct` here to parse `{}` as a public struct",
320 let mut err = self.diagnostic()
321 .struct_span_err(sp, "missing `struct` for struct definition");
322 err.span_suggestion_short(
323 sp, &msg, " struct ".into(), Applicability::MaybeIncorrect // speculative
326 } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
327 let ident = self.parse_ident().unwrap();
329 let kw_name = self.recover_first_param();
330 self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
331 let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
332 self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
334 ("fn", kw_name, false)
335 } else if self.check(&token::OpenDelim(token::Brace)) {
337 ("fn", kw_name, false)
338 } else if self.check(&token::Colon) {
342 ("fn` or `struct", "function or struct", true)
345 let msg = format!("missing `{}` for {} definition", kw, kw_name);
346 let mut err = self.diagnostic().struct_span_err(sp, &msg);
348 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
349 let suggestion = format!("add `{}` here to parse `{}` as a public {}",
353 err.span_suggestion_short(
354 sp, &suggestion, format!(" {} ", kw), Applicability::MachineApplicable
357 if let Ok(snippet) = self.span_to_snippet(ident_sp) {
360 "if you meant to call a macro, try",
361 format!("{}!", snippet),
362 // this is the `ambiguous` conditional branch
363 Applicability::MaybeIncorrect
366 err.help("if you meant to call a macro, remove the `pub` \
367 and add a trailing `!` after the identifier");
371 } else if self.look_ahead(1, |t| *t == token::Lt) {
372 let ident = self.parse_ident().unwrap();
373 self.eat_to_tokens(&[&token::Gt]);
375 let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
376 ("fn", self.recover_first_param(), false)
377 } else if self.check(&token::OpenDelim(token::Brace)) {
378 ("struct", "struct", false)
380 ("fn` or `struct", "function or struct", true)
382 let msg = format!("missing `{}` for {} definition", kw, kw_name);
383 let mut err = self.diagnostic().struct_span_err(sp, &msg);
385 err.span_suggestion_short(
387 &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
389 Applicability::MachineApplicable,
395 self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
398 pub(super) fn mk_item_with_info(
400 attrs: Vec<Attribute>,
404 ) -> PResult<'a, Option<P<Item>>> {
405 let (ident, item, extra_attrs) = info;
406 let span = lo.to(self.prev_span);
407 let attrs = Self::maybe_append(attrs, extra_attrs);
408 Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
411 fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
412 if let Some(ref mut rhs) = rhs {
418 /// This is the fall-through for parsing items.
419 fn parse_macro_use_or_failure(
421 attrs: Vec<Attribute> ,
422 macros_allowed: bool,
423 attributes_allowed: bool,
425 visibility: Visibility
426 ) -> PResult<'a, Option<P<Item>>> {
427 if macros_allowed && self.token.is_path_start() &&
428 !(self.is_async_fn() && self.token.span.rust_2015()) {
429 // MACRO INVOCATION ITEM
431 let prev_span = self.prev_span;
432 self.complain_if_pub_macro(&visibility.node, prev_span);
435 let path = self.parse_path(PathStyle::Mod)?;
436 self.expect(&token::Not)?;
437 let args = self.parse_mac_args()?;
438 if args.need_semicolon() && !self.eat(&token::Semi) {
439 self.report_invalid_macro_expansion_item();
442 let hi = self.prev_span;
446 prior_type_ascription: self.last_type_ascription,
449 self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
450 return Ok(Some(item));
453 // FAILURE TO PARSE ITEM
454 match visibility.node {
455 VisibilityKind::Inherited => {}
457 return Err(self.span_fatal(self.prev_span, "unmatched visibility `pub`"));
461 if !attributes_allowed && !attrs.is_empty() {
462 self.expected_item_err(&attrs)?;
467 /// Emits an expected-item-after-attributes error.
468 fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
469 let message = match attrs.last() {
470 Some(&Attribute { kind: AttrKind::DocComment(_), .. }) =>
471 "expected item after doc comment",
473 "expected item after attributes",
476 let mut err = self.diagnostic().struct_span_err(self.prev_span, message);
477 if attrs.last().unwrap().is_doc_comment() {
478 err.span_label(self.prev_span, "this doc comment doesn't document anything");
483 pub(super) fn is_async_fn(&self) -> bool {
484 self.token.is_keyword(kw::Async) &&
485 self.is_keyword_ahead(1, &[kw::Fn])
488 /// Parses a macro invocation inside a `trait`, `impl` or `extern` block.
489 fn parse_assoc_macro_invoc(
492 vis: Option<&Visibility>,
494 ) -> PResult<'a, Option<Mac>> {
495 if self.token.is_path_start() &&
496 !(self.is_async_fn() && self.token.span.rust_2015()) {
497 let prev_span = self.prev_span;
498 let path = self.parse_path(PathStyle::Mod)?;
500 if path.segments.len() == 1 {
501 if !self.eat(&token::Not) {
502 return Err(self.missing_assoc_item_kind_err(item_kind, prev_span));
505 self.expect(&token::Not)?;
508 if let Some(vis) = vis {
509 self.complain_if_pub_macro(&vis.node, prev_span);
514 // eat a matched-delimiter token tree:
515 let args = self.parse_mac_args()?;
516 if args.need_semicolon() {
523 prior_type_ascription: self.last_type_ascription,
530 fn missing_assoc_item_kind_err(
534 ) -> DiagnosticBuilder<'a> {
535 let expected_kinds = if item_type == "extern" {
536 "missing `fn`, `type`, or `static`"
538 "missing `fn`, `type`, or `const`"
541 // Given this code `path(`, it seems like this is not
542 // setting the visibility of a macro invocation, but rather
543 // a mistyped method declaration.
544 // Create a diagnostic pointing out that `fn` is missing.
546 // x | pub path(&self) {
547 // | ^ missing `fn`, `type`, or `const`
549 // ^^ `sp` below will point to this
550 let sp = prev_span.between(self.prev_span);
551 let mut err = self.diagnostic().struct_span_err(
553 &format!("{} for {}-item declaration",
554 expected_kinds, item_type));
555 err.span_label(sp, expected_kinds);
559 /// Parses an implementation item, `impl` keyword is already parsed.
561 /// impl<'a, T> TYPE { /* impl items */ }
562 /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
563 /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
565 /// We actually parse slightly more relaxed grammar for better error reporting and recovery.
566 /// `impl` GENERICS `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
567 /// `impl` GENERICS `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
568 fn parse_item_impl(&mut self, unsafety: Unsafety, defaultness: Defaultness)
569 -> PResult<'a, ItemInfo> {
570 // First, parse generic parameters if necessary.
571 let mut generics = if self.choose_generics_over_qpath() {
572 self.parse_generics()?
577 // Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
578 let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
580 ast::ImplPolarity::Negative
582 ast::ImplPolarity::Positive
585 // Parse both types and traits as a type, then reinterpret if necessary.
586 let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
587 let ty_first = if self.token.is_keyword(kw::For) &&
588 self.look_ahead(1, |t| t != &token::Lt) {
589 let span = self.prev_span.between(self.token.span);
590 self.struct_span_err(span, "missing trait in a trait impl").emit();
591 P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
596 // If `for` is missing we try to recover.
597 let has_for = self.eat_keyword(kw::For);
598 let missing_for_span = self.prev_span.between(self.token.span);
600 let ty_second = if self.token == token::DotDot {
601 // We need to report this error after `cfg` expansion for compatibility reasons
602 self.bump(); // `..`, do not add it to expected tokens
603 Some(self.mk_ty(self.prev_span, TyKind::Err))
604 } else if has_for || self.token.can_begin_type() {
605 Some(self.parse_ty()?)
610 generics.where_clause = self.parse_where_clause()?;
612 let (impl_items, attrs) = self.parse_impl_body()?;
614 let item_kind = match ty_second {
616 // impl Trait for Type
618 self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
619 .span_suggestion_short(
623 Applicability::MachineApplicable,
627 let ty_first = ty_first.into_inner();
628 let path = match ty_first.kind {
629 // This notably includes paths passed through `ty` macro fragments (#46438).
630 TyKind::Path(None, path) => path,
632 self.span_err(ty_first.span, "expected a trait, found type");
633 err_path(ty_first.span)
636 let trait_ref = TraitRef { path, ref_id: ty_first.id };
638 ItemKind::Impl(unsafety, polarity, defaultness,
639 generics, Some(trait_ref), ty_second, impl_items)
643 ItemKind::Impl(unsafety, polarity, defaultness,
644 generics, None, ty_first, impl_items)
648 Ok((Ident::invalid(), item_kind, Some(attrs)))
651 fn parse_impl_body(&mut self) -> PResult<'a, (Vec<AssocItem>, Vec<Attribute>)> {
652 self.expect(&token::OpenDelim(token::Brace))?;
653 let attrs = self.parse_inner_attributes()?;
655 let mut impl_items = Vec::new();
656 while !self.eat(&token::CloseDelim(token::Brace)) {
657 let mut at_end = false;
658 match self.parse_impl_item(&mut at_end) {
659 Ok(impl_item) => impl_items.push(impl_item),
663 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
669 Ok((impl_items, attrs))
672 /// Parses defaultness (i.e., `default` or nothing).
673 fn parse_defaultness(&mut self) -> Defaultness {
674 // `pub` is included for better error messages
675 if self.check_keyword(kw::Default) &&
676 self.is_keyword_ahead(1, &[
687 self.bump(); // `default`
694 /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
695 fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
696 // Parse optional `auto` prefix.
697 let is_auto = if self.eat_keyword(kw::Auto) {
703 self.expect_keyword(kw::Trait)?;
704 let ident = self.parse_ident()?;
705 let mut tps = self.parse_generics()?;
707 // Parse optional colon and supertrait bounds.
708 let had_colon = self.eat(&token::Colon);
709 let span_at_colon = self.prev_span;
710 let bounds = if had_colon {
711 self.parse_generic_bounds(Some(self.prev_span))?
716 let span_before_eq = self.prev_span;
717 if self.eat(&token::Eq) {
718 // It's a trait alias.
720 let span = span_at_colon.to(span_before_eq);
721 self.struct_span_err(span, "bounds are not allowed on trait aliases")
725 let bounds = self.parse_generic_bounds(None)?;
726 tps.where_clause = self.parse_where_clause()?;
729 let whole_span = lo.to(self.prev_span);
730 if is_auto == IsAuto::Yes {
731 let msg = "trait aliases cannot be `auto`";
732 self.struct_span_err(whole_span, msg)
733 .span_label(whole_span, msg)
736 if unsafety != Unsafety::Normal {
737 let msg = "trait aliases cannot be `unsafe`";
738 self.struct_span_err(whole_span, msg)
739 .span_label(whole_span, msg)
743 self.sess.gated_spans.gate(sym::trait_alias, whole_span);
745 Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
747 // It's a normal trait.
748 tps.where_clause = self.parse_where_clause()?;
749 self.expect(&token::OpenDelim(token::Brace))?;
750 let mut trait_items = vec![];
751 while !self.eat(&token::CloseDelim(token::Brace)) {
752 if let token::DocComment(_) = self.token.kind {
753 if self.look_ahead(1,
754 |tok| tok == &token::CloseDelim(token::Brace)) {
759 "found a documentation comment that doesn't document anything",
762 "doc comments must come before what they document, maybe a \
763 comment was intended with `//`?",
770 let mut at_end = false;
771 match self.parse_trait_item(&mut at_end) {
772 Ok(item) => trait_items.push(item),
776 self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
782 Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None))
786 pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
787 maybe_whole!(self, NtImplItem, |x| x);
788 self.parse_assoc_item(at_end, |_| true)
791 pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
792 maybe_whole!(self, NtTraitItem, |x| x);
793 // This is somewhat dubious; We don't want to allow
794 // param names to be left off if there is a definition...
796 // We don't allow param names to be left off in edition 2018.
797 self.parse_assoc_item(at_end, |t| t.span.rust_2018())
800 /// Parses associated items.
804 is_name_required: fn(&token::Token) -> bool,
805 ) -> PResult<'a, AssocItem> {
806 let attrs = self.parse_outer_attributes()?;
807 let mut unclosed_delims = vec![];
808 let (mut item, tokens) = self.collect_tokens(|this| {
809 let item = this.parse_assoc_item_(at_end, attrs, is_name_required);
810 unclosed_delims.append(&mut this.unclosed_delims);
813 self.unclosed_delims.append(&mut unclosed_delims);
814 // See `parse_item` for why this clause is here.
815 if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
816 item.tokens = Some(tokens);
821 fn parse_assoc_item_(
824 mut attrs: Vec<Attribute>,
825 is_name_required: fn(&token::Token) -> bool,
826 ) -> PResult<'a, AssocItem> {
827 let lo = self.token.span;
828 let vis = self.parse_visibility(FollowedByType::No)?;
829 let defaultness = self.parse_defaultness();
830 let (name, kind, generics) = if self.eat_keyword(kw::Type) {
831 self.parse_assoc_ty()?
832 } else if self.is_const_item() {
833 self.parse_assoc_const()?
834 } else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
835 (Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
837 self.parse_assoc_fn(at_end, &mut attrs, is_name_required)?
842 span: lo.to(self.prev_span),
853 /// Returns `true` if we are looking at `const ID`
854 /// (returns `false` for things like `const fn`, etc.).
855 fn is_const_item(&self) -> bool {
856 self.token.is_keyword(kw::Const) &&
857 !self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe])
860 /// This parses the grammar:
862 /// AssocConst = "const" Ident ":" Ty "=" Expr ";"
863 fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
864 self.expect_keyword(kw::Const)?;
865 let ident = self.parse_ident()?;
866 self.expect(&token::Colon)?;
867 let ty = self.parse_ty()?;
868 let expr = if self.eat(&token::Eq) {
869 Some(self.parse_expr()?)
874 Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
877 /// Parses the following grammar:
879 /// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
880 fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
881 let ident = self.parse_ident()?;
882 let mut generics = self.parse_generics()?;
884 // Parse optional colon and param bounds.
885 let bounds = if self.eat(&token::Colon) {
886 self.parse_generic_bounds(None)?
890 generics.where_clause = self.parse_where_clause()?;
892 let default = if self.eat(&token::Eq) {
893 Some(self.parse_ty()?)
899 Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
902 /// Parses a `UseTree`.
905 /// USE_TREE = [`::`] `*` |
906 /// [`::`] `{` USE_TREE_LIST `}` |
908 /// PATH `::` `{` USE_TREE_LIST `}` |
909 /// PATH [`as` IDENT]
911 fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
912 let lo = self.token.span;
914 let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
915 let kind = if self.check(&token::OpenDelim(token::Brace)) ||
916 self.check(&token::BinOp(token::Star)) ||
917 self.is_import_coupler() {
918 // `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
919 let mod_sep_ctxt = self.token.span.ctxt();
920 if self.eat(&token::ModSep) {
921 prefix.segments.push(
922 PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt))
926 self.parse_use_tree_glob_or_nested()?
928 // `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
929 prefix = self.parse_path(PathStyle::Mod)?;
931 if self.eat(&token::ModSep) {
932 self.parse_use_tree_glob_or_nested()?
934 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
938 Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
941 /// Parses `*` or `{...}`.
942 fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
943 Ok(if self.eat(&token::BinOp(token::Star)) {
946 UseTreeKind::Nested(self.parse_use_tree_list()?)
950 /// Parses a `UseTreeKind::Nested(list)`.
953 /// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
955 fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
956 self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
960 fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
961 if self.eat_keyword(kw::As) {
962 self.parse_ident_or_underscore().map(Some)
968 fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
969 match self.token.kind {
970 token::Ident(name, false) if name == kw::Underscore => {
971 let span = self.token.span;
973 Ok(Ident::new(name, span))
975 _ => self.parse_ident(),
979 /// Parses `extern crate` links.
984 /// extern crate foo;
985 /// extern crate bar as foo;
987 fn parse_item_extern_crate(
990 visibility: Visibility,
991 attrs: Vec<Attribute>
992 ) -> PResult<'a, P<Item>> {
993 // Accept `extern crate name-like-this` for better diagnostics
994 let orig_name = self.parse_crate_name_with_dashes()?;
995 let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
996 (rename, Some(orig_name.name))
1000 self.expect_semi()?;
1002 let span = lo.to(self.prev_span);
1003 Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
1006 fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
1007 let error_msg = "crate name using dashes are not valid in `extern crate` statements";
1008 let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
1010 let mut ident = if self.token.is_keyword(kw::SelfLower) {
1011 self.parse_path_segment_ident()
1015 let mut idents = vec![];
1016 let mut replacement = vec![];
1017 let mut fixed_crate_name = false;
1018 // Accept `extern crate name-like-this` for better diagnostics.
1019 let dash = token::BinOp(token::BinOpToken::Minus);
1020 if self.token == dash { // Do not include `-` as part of the expected tokens list.
1021 while self.eat(&dash) {
1022 fixed_crate_name = true;
1023 replacement.push((self.prev_span, "_".to_string()));
1024 idents.push(self.parse_ident()?);
1027 if fixed_crate_name {
1028 let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
1029 let mut fixed_name = format!("{}", ident.name);
1030 for part in idents {
1031 fixed_name.push_str(&format!("_{}", part.name));
1033 ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
1035 self.struct_span_err(fixed_name_sp, error_msg)
1036 .span_label(fixed_name_sp, "dash-separated idents are not valid")
1037 .multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
1043 /// Parses `extern` for foreign ABIs modules.
1045 /// `extern` is expected to have been
1046 /// consumed before calling this method.
1050 /// ```ignore (only-for-syntax-highlight)
1054 fn parse_item_foreign_mod(
1057 abi: Option<StrLit>,
1058 visibility: Visibility,
1059 mut attrs: Vec<Attribute>,
1061 ) -> PResult<'a, P<Item>> {
1062 self.expect(&token::OpenDelim(token::Brace))?;
1064 attrs.extend(self.parse_inner_attributes()?);
1066 let mut foreign_items = vec![];
1067 while !self.eat(&token::CloseDelim(token::Brace)) {
1068 foreign_items.push(self.parse_foreign_item(extern_sp)?);
1071 let prev_span = self.prev_span;
1072 let m = ast::ForeignMod {
1074 items: foreign_items
1076 let invalid = Ident::invalid();
1077 Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs))
1080 /// Parses a foreign item.
1081 pub fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, ForeignItem> {
1082 maybe_whole!(self, NtForeignItem, |ni| ni);
1084 let attrs = self.parse_outer_attributes()?;
1085 let lo = self.token.span;
1086 let visibility = self.parse_visibility(FollowedByType::No)?;
1088 // FOREIGN STATIC ITEM
1089 // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
1090 if self.check_keyword(kw::Static) || self.token.is_keyword(kw::Const) {
1091 if self.token.is_keyword(kw::Const) {
1093 .struct_span_err(self.token.span, "extern items cannot be `const`");
1096 // The user wrote 'const fn'
1097 if self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe]) {
1101 // Consume `unsafe` if present, since `extern` blocks
1102 // don't allow it. This will leave behind a plain 'fn'
1103 self.eat_keyword(kw::Unsafe);
1104 // Treat 'const fn` as a plain `fn` for error recovery purposes.
1105 // We've already emitted an error, so compilation is guaranteed
1107 return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?);
1109 err.span_suggestion(
1111 "try using a static value",
1112 "static".to_owned(),
1113 Applicability::MachineApplicable
1117 self.bump(); // `static` or `const`
1118 return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?);
1120 // FOREIGN FUNCTION ITEM
1121 if self.check_keyword(kw::Fn) {
1122 return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?);
1124 // FOREIGN TYPE ITEM
1125 if self.check_keyword(kw::Type) {
1126 return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?);
1129 match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
1133 ident: Ident::invalid(),
1134 span: lo.to(self.prev_span),
1138 kind: ForeignItemKind::Macro(mac),
1144 if !attrs.is_empty() {
1145 self.expected_item_err(&attrs)?;
1153 /// Parses a static item from a foreign module.
1154 /// Assumes that the `static` keyword is already parsed.
1155 fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
1156 -> PResult<'a, ForeignItem> {
1157 let mutbl = self.parse_mutability();
1158 let ident = self.parse_ident()?;
1159 self.expect(&token::Colon)?;
1160 let ty = self.parse_ty()?;
1161 let hi = self.token.span;
1162 self.expect_semi()?;
1166 kind: ForeignItemKind::Static(ty, mutbl),
1174 /// Parses a type from a foreign module.
1175 fn parse_item_foreign_type(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
1176 -> PResult<'a, ForeignItem> {
1177 self.expect_keyword(kw::Type)?;
1179 let ident = self.parse_ident()?;
1180 let hi = self.token.span;
1181 self.expect_semi()?;
1182 Ok(ast::ForeignItem {
1185 kind: ForeignItemKind::Ty,
1193 fn is_static_global(&mut self) -> bool {
1194 if self.check_keyword(kw::Static) {
1195 // Check if this could be a closure.
1196 !self.look_ahead(1, |token| {
1197 if token.is_keyword(kw::Move) {
1201 token::BinOp(token::Or) | token::OrOr => true,
1210 /// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
1211 /// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
1213 /// When `m` is `"const"`, `$ident` may also be `"_"`.
1214 fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
1215 let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1217 // Parse the type of a `const` or `static mut?` item.
1218 // That is, the `":" $ty` fragment.
1219 let ty = if self.token == token::Eq {
1220 self.recover_missing_const_type(id, m)
1222 // Not `=` so expect `":"" $ty` as usual.
1223 self.expect(&token::Colon)?;
1227 self.expect(&token::Eq)?;
1228 let e = self.parse_expr()?;
1229 self.expect_semi()?;
1230 let item = match m {
1231 Some(m) => ItemKind::Static(ty, m, e),
1232 None => ItemKind::Const(ty, e),
1234 Ok((id, item, None))
1237 /// We were supposed to parse `:` but instead, we're already at `=`.
1238 /// This means that the type is missing.
1239 fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
1240 // Construct the error and stash it away with the hope
1241 // that typeck will later enrich the error with a type.
1242 let kind = match m {
1243 Some(Mutability::Mutable) => "static mut",
1244 Some(Mutability::Immutable) => "static",
1247 let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
1248 err.span_suggestion(
1250 "provide a type for the item",
1251 format!("{}: <type>", id),
1252 Applicability::HasPlaceholders,
1254 err.stash(id.span, StashKey::ItemNoType);
1256 // The user intended that the type be inferred,
1257 // so treat this as if the user wrote e.g. `const A: _ = expr;`.
1259 kind: TyKind::Infer,
1261 id: ast::DUMMY_NODE_ID,
1265 /// Parses the grammar:
1266 /// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
1267 fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
1268 let ident = self.parse_ident()?;
1269 let mut tps = self.parse_generics()?;
1270 tps.where_clause = self.parse_where_clause()?;
1271 self.expect(&token::Eq)?;
1272 let ty = self.parse_ty()?;
1273 self.expect_semi()?;
1274 Ok((ident, ty, tps))
1277 /// Parses an enum declaration.
1278 fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
1279 let id = self.parse_ident()?;
1280 let mut generics = self.parse_generics()?;
1281 generics.where_clause = self.parse_where_clause()?;
1283 let (variants, _) = self.parse_delim_comma_seq(
1285 |p| p.parse_enum_variant(),
1287 self.recover_stmt();
1291 let enum_definition = EnumDef {
1292 variants: variants.into_iter().filter_map(|v| v).collect(),
1294 Ok((id, ItemKind::Enum(enum_definition, generics), None))
1297 fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
1298 let variant_attrs = self.parse_outer_attributes()?;
1299 let vlo = self.token.span;
1301 let vis = self.parse_visibility(FollowedByType::No)?;
1302 if !self.recover_nested_adt_item(kw::Enum)? {
1305 let ident = self.parse_ident()?;
1307 let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
1308 // Parse a struct variant.
1309 let (fields, recovered) = self.parse_record_struct_body()?;
1310 VariantData::Struct(fields, recovered)
1311 } else if self.check(&token::OpenDelim(token::Paren)) {
1313 self.parse_tuple_struct_body()?,
1317 VariantData::Unit(DUMMY_NODE_ID)
1320 let disr_expr = if self.eat(&token::Eq) {
1323 value: self.parse_expr()?,
1329 let vr = ast::Variant {
1333 attrs: variant_attrs,
1336 span: vlo.to(self.prev_span),
1337 is_placeholder: false,
1343 /// Parses `struct Foo { ... }`.
1344 fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
1345 let class_name = self.parse_ident()?;
1347 let mut generics = self.parse_generics()?;
1349 // There is a special case worth noting here, as reported in issue #17904.
1350 // If we are parsing a tuple struct it is the case that the where clause
1351 // should follow the field list. Like so:
1353 // struct Foo<T>(T) where T: Copy;
1355 // If we are parsing a normal record-style struct it is the case
1356 // that the where clause comes before the body, and after the generics.
1357 // So if we look ahead and see a brace or a where-clause we begin
1358 // parsing a record style struct.
1360 // Otherwise if we look ahead and see a paren we parse a tuple-style
1363 let vdata = if self.token.is_keyword(kw::Where) {
1364 generics.where_clause = self.parse_where_clause()?;
1365 if self.eat(&token::Semi) {
1366 // If we see a: `struct Foo<T> where T: Copy;` style decl.
1367 VariantData::Unit(DUMMY_NODE_ID)
1369 // If we see: `struct Foo<T> where T: Copy { ... }`
1370 let (fields, recovered) = self.parse_record_struct_body()?;
1371 VariantData::Struct(fields, recovered)
1373 // No `where` so: `struct Foo<T>;`
1374 } else if self.eat(&token::Semi) {
1375 VariantData::Unit(DUMMY_NODE_ID)
1376 // Record-style struct definition
1377 } else if self.token == token::OpenDelim(token::Brace) {
1378 let (fields, recovered) = self.parse_record_struct_body()?;
1379 VariantData::Struct(fields, recovered)
1380 // Tuple-style struct definition with optional where-clause.
1381 } else if self.token == token::OpenDelim(token::Paren) {
1382 let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
1383 generics.where_clause = self.parse_where_clause()?;
1384 self.expect_semi()?;
1387 let token_str = self.this_token_descr();
1388 let mut err = self.fatal(&format!(
1389 "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
1392 err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
1396 Ok((class_name, ItemKind::Struct(vdata, generics), None))
1399 /// Parses `union Foo { ... }`.
1400 fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
1401 let class_name = self.parse_ident()?;
1403 let mut generics = self.parse_generics()?;
1405 let vdata = if self.token.is_keyword(kw::Where) {
1406 generics.where_clause = self.parse_where_clause()?;
1407 let (fields, recovered) = self.parse_record_struct_body()?;
1408 VariantData::Struct(fields, recovered)
1409 } else if self.token == token::OpenDelim(token::Brace) {
1410 let (fields, recovered) = self.parse_record_struct_body()?;
1411 VariantData::Struct(fields, recovered)
1413 let token_str = self.this_token_descr();
1414 let mut err = self.fatal(&format!(
1415 "expected `where` or `{{` after union name, found {}", token_str));
1416 err.span_label(self.token.span, "expected `where` or `{` after union name");
1420 Ok((class_name, ItemKind::Union(vdata, generics), None))
1423 pub(super) fn is_union_item(&self) -> bool {
1424 self.token.is_keyword(kw::Union) &&
1425 self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
1428 fn parse_record_struct_body(
1430 ) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
1431 let mut fields = Vec::new();
1432 let mut recovered = false;
1433 if self.eat(&token::OpenDelim(token::Brace)) {
1434 while self.token != token::CloseDelim(token::Brace) {
1435 let field = self.parse_struct_decl_field().map_err(|e| {
1436 self.consume_block(token::Brace, ConsumeClosingDelim::No);
1441 Ok(field) => fields.push(field),
1448 self.eat(&token::CloseDelim(token::Brace));
1450 let token_str = self.this_token_descr();
1451 let mut err = self.fatal(&format!(
1452 "expected `where`, or `{{` after struct name, found {}", token_str));
1453 err.span_label(self.token.span, "expected `where`, or `{` after struct name");
1457 Ok((fields, recovered))
1460 fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
1461 // This is the case where we find `struct Foo<T>(T) where T: Copy;`
1462 // Unit like structs are handled in parse_item_struct function
1463 self.parse_paren_comma_seq(|p| {
1464 let attrs = p.parse_outer_attributes()?;
1465 let lo = p.token.span;
1466 let vis = p.parse_visibility(FollowedByType::Yes)?;
1467 let ty = p.parse_ty()?;
1469 span: lo.to(ty.span),
1475 is_placeholder: false,
1480 /// Parses an element of a struct declaration.
1481 fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
1482 let attrs = self.parse_outer_attributes()?;
1483 let lo = self.token.span;
1484 let vis = self.parse_visibility(FollowedByType::No)?;
1485 self.parse_single_struct_field(lo, vis, attrs)
1488 /// Parses a structure field declaration.
1489 fn parse_single_struct_field(&mut self,
1492 attrs: Vec<Attribute> )
1493 -> PResult<'a, StructField> {
1494 let mut seen_comma: bool = false;
1495 let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
1496 if self.token == token::Comma {
1499 match self.token.kind {
1503 token::CloseDelim(token::Brace) => {}
1504 token::DocComment(_) => {
1505 let previous_span = self.prev_span;
1506 let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1507 self.bump(); // consume the doc comment
1508 let comma_after_doc_seen = self.eat(&token::Comma);
1509 // `seen_comma` is always false, because we are inside doc block
1510 // condition is here to make code more readable
1511 if seen_comma == false && comma_after_doc_seen == true {
1514 if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
1517 if seen_comma == false {
1518 let sp = self.sess.source_map().next_point(previous_span);
1519 err.span_suggestion(
1521 "missing comma here",
1523 Applicability::MachineApplicable
1530 let sp = self.sess.source_map().next_point(self.prev_span);
1531 let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found {}",
1532 self.this_token_descr()));
1533 if self.token.is_ident() {
1534 // This is likely another field; emit the diagnostic and keep going
1535 err.span_suggestion(
1537 "try adding a comma",
1539 Applicability::MachineApplicable,
1550 /// Parses a structure field.
1551 fn parse_name_and_ty(
1555 attrs: Vec<Attribute>
1556 ) -> PResult<'a, StructField> {
1557 let name = self.parse_ident()?;
1558 self.expect(&token::Colon)?;
1559 let ty = self.parse_ty()?;
1561 span: lo.to(self.prev_span),
1567 is_placeholder: false,
1571 pub(super) fn eat_macro_def(
1573 attrs: &[Attribute],
1576 ) -> PResult<'a, Option<P<Item>>> {
1577 let (ident, def) = if self.eat_keyword(kw::Macro) {
1578 let ident = self.parse_ident()?;
1579 let body = if self.check(&token::OpenDelim(token::Brace)) {
1580 self.parse_mac_args()?
1581 } else if self.check(&token::OpenDelim(token::Paren)) {
1582 let params = self.parse_token_tree();
1583 let pspan = params.span();
1584 let body = if self.check(&token::OpenDelim(token::Brace)) {
1585 self.parse_token_tree()
1587 return self.unexpected();
1589 let bspan = body.span();
1590 let tokens = TokenStream::new(vec![
1592 TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
1595 let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1596 P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
1598 return self.unexpected();
1601 (ident, ast::MacroDef { body, legacy: false })
1602 } else if self.check_keyword(sym::macro_rules) &&
1603 self.look_ahead(1, |t| *t == token::Not) &&
1604 self.look_ahead(2, |t| t.is_ident()) {
1605 let prev_span = self.prev_span;
1606 self.complain_if_pub_macro(&vis.node, prev_span);
1610 let ident = self.parse_ident()?;
1611 let body = self.parse_mac_args()?;
1612 if body.need_semicolon() && !self.eat(&token::Semi) {
1613 self.report_invalid_macro_expansion_item();
1616 (ident, ast::MacroDef { body, legacy: true })
1621 let span = lo.to(self.prev_span);
1624 self.sess.gated_spans.gate(sym::decl_macro, span);
1627 Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
1630 fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
1632 VisibilityKind::Inherited => {}
1634 let mut err = if self.token.is_keyword(sym::macro_rules) {
1635 let mut err = self.diagnostic()
1636 .struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
1637 err.span_suggestion(
1639 "try exporting the macro",
1640 "#[macro_export]".to_owned(),
1641 Applicability::MaybeIncorrect // speculative
1645 let mut err = self.diagnostic()
1646 .struct_span_err(sp, "can't qualify macro invocation with `pub`");
1647 err.help("try adjusting the macro to put `pub` inside the invocation");
1655 fn report_invalid_macro_expansion_item(&self) {
1656 let has_close_delim = self.sess.source_map()
1657 .span_to_snippet(self.prev_span)
1658 .map(|s| s.ends_with(")") || s.ends_with("]"))
1660 let right_brace_span = if has_close_delim {
1661 // it's safe to peel off one character only when it has the close delim
1662 self.prev_span.with_lo(self.prev_span.hi() - BytePos(1))
1664 self.sess.source_map().next_point(self.prev_span)
1667 self.struct_span_err(
1669 "macros that expand to items must be delimited with braces or followed by a semicolon",
1670 ).multipart_suggestion(
1671 "change the delimiters to curly braces",
1673 (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()),
1674 (right_brace_span, '}'.to_string()),
1676 Applicability::MaybeIncorrect,
1678 self.sess.source_map().next_point(self.prev_span),
1681 Applicability::MaybeIncorrect,
1685 /// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
1686 /// it is, we try to parse the item and report error about nested types.
1687 fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
1688 if (self.token.is_keyword(kw::Enum) ||
1689 self.token.is_keyword(kw::Struct) ||
1690 self.token.is_keyword(kw::Union))
1691 && self.look_ahead(1, |t| t.is_ident())
1693 let kw_token = self.token.clone();
1694 let kw_str = pprust::token_to_string(&kw_token);
1695 let item = self.parse_item()?;
1697 self.struct_span_err(
1699 &format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
1702 &format!("consider creating a new `{}` definition instead of nesting", kw_str),
1704 Applicability::MaybeIncorrect,
1706 // We successfully parsed the item but we must inform the caller about nested problem.
1712 fn mk_item(&self, span: Span, ident: Ident, kind: ItemKind, vis: Visibility,
1713 attrs: Vec<Attribute>) -> P<Item> {
1726 /// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
1727 pub(super) struct ParamCfg {
1728 /// Is `self` is allowed as the first parameter?
1729 pub is_self_allowed: bool,
1730 /// `is_name_required` decides if, per-parameter,
1731 /// the parameter must have a pattern or just a type.
1732 pub is_name_required: fn(&token::Token) -> bool,
1735 /// Parsing of functions and methods.
1736 impl<'a> Parser<'a> {
1737 /// Parses an item-position function declaration.
1742 attrs: Vec<Attribute>,
1744 ) -> PResult<'a, Option<P<Item>>> {
1745 let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
1746 is_self_allowed: false,
1747 is_name_required: |_| true,
1749 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1750 let kind = ItemKind::Fn(FnSig { decl, header }, generics, body);
1751 self.mk_item_with_info(attrs, lo, vis, (ident, kind, Some(inner_attrs)))
1754 /// Parses a function declaration from a foreign module.
1755 fn parse_item_foreign_fn(
1757 vis: ast::Visibility,
1759 attrs: Vec<Attribute>,
1761 ) -> PResult<'a, ForeignItem> {
1762 self.expect_keyword(kw::Fn)?;
1763 let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
1764 is_self_allowed: false,
1765 is_name_required: |_| true,
1767 let span = lo.to(self.token.span);
1768 self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?;
1769 Ok(ast::ForeignItem {
1772 kind: ForeignItemKind::Fn(decl, generics),
1783 attrs: &mut Vec<Attribute>,
1784 is_name_required: fn(&token::Token) -> bool,
1785 ) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
1786 let header = self.parse_fn_front_matter()?;
1787 let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
1788 is_self_allowed: true,
1791 let sig = FnSig { header, decl };
1792 let body = self.parse_assoc_fn_body(at_end, attrs)?;
1793 Ok((ident, AssocItemKind::Fn(sig, body), generics))
1796 /// Parse the "body" of a method in an associated item definition.
1797 /// This can either be `;` when there's no body,
1798 /// or e.g. a block when the method is a provided one.
1799 fn parse_assoc_fn_body(
1802 attrs: &mut Vec<Attribute>,
1803 ) -> PResult<'a, Option<P<Block>>> {
1804 Ok(match self.token.kind {
1806 debug!("parse_assoc_fn_body(): parsing required method");
1811 token::OpenDelim(token::Brace) => {
1812 debug!("parse_assoc_fn_body(): parsing provided method");
1814 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1815 attrs.extend(inner_attrs.iter().cloned());
1818 token::Interpolated(ref nt) => {
1820 token::NtBlock(..) => {
1822 let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1823 attrs.extend(inner_attrs.iter().cloned());
1826 _ => return self.expected_semi_or_open_brace(),
1829 _ => return self.expected_semi_or_open_brace(),
1833 /// Parses all the "front matter" for a `fn` declaration, up to
1834 /// and including the `fn` keyword:
1838 /// - `const unsafe fn`
1841 fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
1842 let is_const_fn = self.eat_keyword(kw::Const);
1843 let const_span = self.prev_span;
1844 let asyncness = self.parse_asyncness();
1845 if let IsAsync::Async { .. } = asyncness {
1846 self.ban_async_in_2015(self.prev_span);
1848 let asyncness = respan(self.prev_span, asyncness);
1849 let unsafety = self.parse_unsafety();
1850 let (constness, unsafety, ext) = if is_const_fn {
1851 (respan(const_span, Constness::Const), unsafety, Extern::None)
1853 let ext = self.parse_extern()?;
1854 (respan(self.prev_span, Constness::NotConst), unsafety, ext)
1856 if !self.eat_keyword(kw::Fn) {
1857 // It is possible for `expect_one_of` to recover given the contents of
1858 // `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
1859 // account for this.
1860 if !self.expect_one_of(&[], &[])? { unreachable!() }
1862 Ok(FnHeader { constness, unsafety, asyncness, ext })
1865 /// Parse the "signature", including the identifier, parameters, and generics of a function.
1866 fn parse_fn_sig(&mut self, cfg: ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
1867 let ident = self.parse_ident()?;
1868 let mut generics = self.parse_generics()?;
1869 let decl = self.parse_fn_decl(cfg, true)?;
1870 generics.where_clause = self.parse_where_clause()?;
1871 Ok((ident, decl, generics))
1874 /// Parses the parameter list and result type of a function declaration.
1875 pub(super) fn parse_fn_decl(
1878 ret_allow_plus: bool,
1879 ) -> PResult<'a, P<FnDecl>> {
1881 inputs: self.parse_fn_params(cfg)?,
1882 output: self.parse_ret_ty(ret_allow_plus, true)?,
1886 /// Parses the parameter list of a function, including the `(` and `)` delimiters.
1887 fn parse_fn_params(&mut self, mut cfg: ParamCfg) -> PResult<'a, Vec<Param>> {
1888 let is_trait_item = cfg.is_self_allowed;
1889 // Parse the arguments, starting out with `self` being possibly allowed...
1890 let (mut params, _) = self.parse_paren_comma_seq(|p| {
1891 let param = p.parse_param_general(&cfg, is_trait_item).or_else(|mut e| {
1893 let lo = p.prev_span;
1894 // Skip every token until next possible arg or end.
1895 p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
1896 // Create a placeholder argument for proper arg count (issue #34264).
1897 Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
1899 // ...now that we've parsed the first argument, `self` is no longer allowed.
1900 cfg.is_self_allowed = false;
1903 // Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
1904 self.deduplicate_recovered_params_names(&mut params);
1908 /// Skips unexpected attributes and doc comments in this position and emits an appropriate
1910 /// This version of parse param doesn't necessarily require identifier names.
1911 fn parse_param_general(&mut self, cfg: &ParamCfg, is_trait_item: bool) -> PResult<'a, Param> {
1912 let lo = self.token.span;
1913 let attrs = self.parse_outer_attributes()?;
1915 // Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
1916 if let Some(mut param) = self.parse_self_param()? {
1917 param.attrs = attrs.into();
1918 return if cfg.is_self_allowed {
1921 self.recover_bad_self_param(param, is_trait_item)
1925 let is_name_required = match self.token.kind {
1926 token::DotDotDot => false,
1927 _ => (cfg.is_name_required)(&self.token),
1929 let (pat, ty) = if is_name_required || self.is_named_param() {
1930 debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
1932 let pat = self.parse_fn_param_pat()?;
1933 if let Err(mut err) = self.expect(&token::Colon) {
1934 return if let Some(ident) = self.parameter_without_type(
1938 cfg.is_self_allowed,
1942 Ok(dummy_arg(ident))
1948 self.eat_incorrect_doc_comment_for_param_type();
1949 (pat, self.parse_ty_for_param()?)
1951 debug!("parse_param_general ident_to_pat");
1952 let parser_snapshot_before_ty = self.clone();
1953 self.eat_incorrect_doc_comment_for_param_type();
1954 let mut ty = self.parse_ty_for_param();
1955 if ty.is_ok() && self.token != token::Comma &&
1956 self.token != token::CloseDelim(token::Paren) {
1957 // This wasn't actually a type, but a pattern looking like a type,
1958 // so we are going to rollback and re-parse for recovery.
1959 ty = self.unexpected();
1963 let ident = Ident::new(kw::Invalid, self.prev_span);
1964 let bm = BindingMode::ByValue(Mutability::Immutable);
1965 let pat = self.mk_pat_ident(ty.span, bm, ident);
1968 // If this is a C-variadic argument and we hit an error, return the error.
1969 Err(err) if self.token == token::DotDotDot => return Err(err),
1970 // Recover from attempting to parse the argument as a type without pattern.
1973 mem::replace(self, parser_snapshot_before_ty);
1974 self.recover_arg_parse()?
1979 let span = lo.to(self.token.span);
1982 attrs: attrs.into(),
1983 id: ast::DUMMY_NODE_ID,
1984 is_placeholder: false,
1991 /// Returns the parsed optional self parameter and whether a self shortcut was used.
1993 /// See `parse_self_param_with_attrs` to collect attributes.
1994 fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
1995 // Extract an identifier *after* having confirmed that the token is one.
1996 let expect_self_ident = |this: &mut Self| {
1997 match this.token.kind {
1998 // Preserve hygienic context.
1999 token::Ident(name, _) => {
2000 let span = this.token.span;
2002 Ident::new(name, span)
2004 _ => unreachable!(),
2007 // Is `self` `n` tokens ahead?
2008 let is_isolated_self = |this: &Self, n| {
2009 this.is_keyword_ahead(n, &[kw::SelfLower])
2010 && this.look_ahead(n + 1, |t| t != &token::ModSep)
2012 // Is `mut self` `n` tokens ahead?
2013 let is_isolated_mut_self = |this: &Self, n| {
2014 this.is_keyword_ahead(n, &[kw::Mut])
2015 && 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_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)
2033 .span_label(span, msg)
2036 Ok((SelfKind::Value(Mutability::Immutable), expect_self_ident(this), this.prev_span))
2039 // Parse optional `self` parameter of a method.
2040 // Only a limited set of initial token sequences is considered `self` parameters; anything
2041 // else is parsed as a normal function parameter list, so some lookahead is required.
2042 let eself_lo = self.token.span;
2043 let (eself, eself_ident, eself_hi) = match self.token.kind {
2044 token::BinOp(token::And) => {
2045 let eself = if is_isolated_self(self, 1) {
2048 SelfKind::Region(None, Mutability::Immutable)
2049 } else if is_isolated_mut_self(self, 1) {
2053 SelfKind::Region(None, Mutability::Mutable)
2054 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
2057 let lt = self.expect_lifetime();
2058 SelfKind::Region(Some(lt), Mutability::Immutable)
2059 } else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
2062 let lt = self.expect_lifetime();
2064 SelfKind::Region(Some(lt), Mutability::Mutable)
2069 (eself, expect_self_ident(self), self.prev_span)
2072 token::BinOp(token::Star) if is_isolated_self(self, 1) => {
2074 recover_self_ptr(self)?
2076 // `*mut self` and `*const self`
2077 token::BinOp(token::Star) if
2078 self.look_ahead(1, |t| t.is_mutability())
2079 && is_isolated_self(self, 2) =>
2083 recover_self_ptr(self)?
2085 // `self` and `self: TYPE`
2086 token::Ident(..) if is_isolated_self(self, 0) => {
2087 parse_self_possibly_typed(self, Mutability::Immutable)?
2089 // `mut self` and `mut self: TYPE`
2090 token::Ident(..) if is_isolated_mut_self(self, 0) => {
2092 parse_self_possibly_typed(self, Mutability::Mutable)?
2094 _ => return Ok(None),
2097 let eself = source_map::respan(eself_lo.to(eself_hi), eself);
2098 Ok(Some(Param::from_self(ThinVec::default(), eself, eself_ident)))
2101 fn is_named_param(&self) -> bool {
2102 let offset = match self.token.kind {
2103 token::Interpolated(ref nt) => match **nt {
2104 token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2107 token::BinOp(token::And) | token::AndAnd => 1,
2108 _ if self.token.is_keyword(kw::Mut) => 1,
2112 self.look_ahead(offset, |t| t.is_ident()) &&
2113 self.look_ahead(offset + 1, |t| t == &token::Colon)
2116 fn recover_first_param(&mut self) -> &'static str {
2117 match self.parse_outer_attributes()
2118 .and_then(|_| self.parse_self_param())
2119 .map_err(|mut e| e.cancel())
2121 Ok(Some(_)) => "method",