use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
+use super::ty::{AllowPlus, RecoverQPath};
use super::{FollowedByType, Parser, PathStyle};
use crate::maybe_whole;
use rustc_ast_pretty::pprust;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey};
-use rustc_span::source_map::{self, respan, Span};
+use rustc_span::source_map::{self, Span};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::BytePos;
use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
+use syntax::ast::{Async, Const, Defaultness, IsAuto, PathSegment, Unsafe};
use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
-use syntax::ast::{Constness, Defaultness, Extern, IsAsync, IsAuto, PathSegment, StrLit, Unsafety};
use syntax::ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
use syntax::ast::{FnHeader, ForeignItem, ForeignItemKind, Mutability, Visibility, VisibilityKind};
use syntax::ptr::P;
/// Parses one of the items allowed by the flags.
fn parse_item_implementation(
&mut self,
- attrs: Vec<Attribute>,
+ mut attrs: Vec<Attribute>,
macros_allowed: bool,
attributes_allowed: bool,
) -> PResult<'a, Option<P<Item>>> {
maybe_whole!(self, NtItem, |item| {
- let mut item = item.into_inner();
- let mut attrs = attrs;
+ let mut item = item;
mem::swap(&mut item.attrs, &mut attrs);
item.attrs.extend(attrs);
- Some(P(item))
+ Some(item)
});
let lo = self.token.span;
-
let vis = self.parse_visibility(FollowedByType::No)?;
- if self.eat_keyword(kw::Use) {
- // USE ITEM
- let item_ = ItemKind::Use(P(self.parse_use_tree()?));
- self.expect_semi()?;
-
- let span = lo.to(self.prev_span);
- let item = self.mk_item(span, Ident::invalid(), item_, vis, attrs);
- return Ok(Some(item));
+ if let Some(info) = self.parse_item_kind(&mut attrs, macros_allowed, lo, &vis)? {
+ return Ok(Some(self.mk_item_with_info(attrs, lo, vis, info)));
}
- if self.eat_keyword(kw::Extern) {
- let extern_sp = self.prev_span;
- if self.eat_keyword(kw::Crate) {
- return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
- }
-
- let abi = self.parse_abi();
-
- if self.eat_keyword(kw::Fn) {
- // EXTERN FUNCTION ITEM
- let fn_span = self.prev_span;
- let header = FnHeader {
- unsafety: Unsafety::Normal,
- asyncness: respan(fn_span, IsAsync::NotAsync),
- constness: respan(fn_span, Constness::NotConst),
- ext: Extern::from_abi(abi),
- };
- return self.parse_item_fn(lo, vis, attrs, header);
- } else if self.check(&token::OpenDelim(token::Brace)) {
- return Ok(Some(self.parse_item_foreign_mod(lo, abi, vis, attrs, extern_sp)?));
+ // FAILURE TO PARSE ITEM
+ match vis.node {
+ VisibilityKind::Inherited => {}
+ _ => {
+ self.struct_span_err(vis.span, "unmatched visibility `pub`")
+ .span_label(vis.span, "the unmatched visibility")
+ .help("you likely meant to define an item, e.g., `pub fn foo() {}`")
+ .emit();
}
-
- self.unexpected()?;
}
- if self.is_static_global() {
- self.bump();
- // STATIC ITEM
- let m = self.parse_mutability();
- let info = self.parse_item_const(Some(m))?;
- return self.mk_item_with_info(attrs, lo, vis, info);
+ if !attributes_allowed && !attrs.is_empty() {
+ self.expected_item_err(&attrs)?;
}
+ Ok(None)
+ }
- if self.eat_keyword(kw::Const) {
- let const_span = self.prev_span;
- if [kw::Fn, kw::Unsafe, kw::Extern].iter().any(|k| self.check_keyword(*k)) {
- // CONST FUNCTION ITEM
- let unsafety = self.parse_unsafety();
-
- if self.check_keyword(kw::Extern) {
- self.sess.gated_spans.gate(sym::const_extern_fn, lo.to(self.token.span));
- }
- let ext = self.parse_extern()?;
- self.expect_keyword(kw::Fn)?;
-
- let header = FnHeader {
- unsafety,
- asyncness: respan(const_span, IsAsync::NotAsync),
- constness: respan(const_span, Constness::Const),
- ext,
- };
- return self.parse_item_fn(lo, vis, attrs, header);
+ /// Parses one of the items allowed by the flags.
+ fn parse_item_kind(
+ &mut self,
+ attrs: &mut Vec<Attribute>,
+ macros_allowed: bool,
+ lo: Span,
+ vis: &Visibility,
+ ) -> PResult<'a, Option<ItemInfo>> {
+ let info = if self.eat_keyword(kw::Use) {
+ // USE ITEM
+ let tree = self.parse_use_tree()?;
+ self.expect_semi()?;
+ (Ident::invalid(), ItemKind::Use(P(tree)), None)
+ } else if self.check_fn_front_matter() {
+ // FUNCTION ITEM
+ let (ident, sig, generics, body) = self.parse_fn(&mut false, attrs, |_| true)?;
+ (ident, ItemKind::Fn(sig, generics, body), None)
+ } else if self.eat_keyword(kw::Extern) {
+ if self.eat_keyword(kw::Crate) {
+ // EXTERN CRATE
+ self.parse_item_extern_crate()?
+ } else {
+ // EXTERN BLOCK
+ self.parse_item_foreign_mod()?
}
-
+ } else if self.is_static_global() {
+ // STATIC ITEM
+ self.bump(); // `static`
+ let m = self.parse_mutability();
+ self.parse_item_const(Some(m))?
+ } else if let Const::Yes(const_span) = self.parse_constness() {
// CONST ITEM
if self.eat_keyword(kw::Mut) {
let prev_span = self.prev_span;
.emit();
}
- let info = self.parse_item_const(None)?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- // Parses `async unsafe? fn`.
- if self.check_keyword(kw::Async) {
- let async_span = self.token.span;
- if self.is_keyword_ahead(1, &[kw::Fn]) || self.is_keyword_ahead(2, &[kw::Fn]) {
- // ASYNC FUNCTION ITEM
- self.bump(); // `async`
- let unsafety = self.parse_unsafety(); // `unsafe`?
- self.expect_keyword(kw::Fn)?; // `fn`
- let fn_span = self.prev_span;
- let asyncness = respan(
- async_span,
- IsAsync::Async {
- closure_id: DUMMY_NODE_ID,
- return_impl_trait_id: DUMMY_NODE_ID,
- },
- );
- self.ban_async_in_2015(async_span);
- let header = FnHeader {
- unsafety,
- asyncness,
- constness: respan(fn_span, Constness::NotConst),
- ext: Extern::None,
- };
- return self.parse_item_fn(lo, vis, attrs, header);
- }
- }
-
- if self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) {
+ self.parse_item_const(None)?
+ } else if self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
+ {
// UNSAFE TRAIT ITEM
- self.bump(); // `unsafe`
- let info = self.parse_item_trait(lo, Unsafety::Unsafe)?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- if self.check_keyword(kw::Impl)
+ let unsafety = self.parse_unsafety();
+ self.parse_item_trait(lo, unsafety)?
+ } else if self.check_keyword(kw::Impl)
|| self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Impl])
|| self.check_keyword(kw::Default) && self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
{
let defaultness = self.parse_defaultness();
let unsafety = self.parse_unsafety();
self.expect_keyword(kw::Impl)?;
- let info = self.parse_item_impl(unsafety, defaultness)?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- if self.check_keyword(kw::Fn) {
- // FUNCTION ITEM
- self.bump();
- let fn_span = self.prev_span;
- let header = FnHeader {
- unsafety: Unsafety::Normal,
- asyncness: respan(fn_span, IsAsync::NotAsync),
- constness: respan(fn_span, Constness::NotConst),
- ext: Extern::None,
- };
- return self.parse_item_fn(lo, vis, attrs, header);
- }
-
- if self.check_keyword(kw::Unsafe)
- && self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace))
- {
- // UNSAFE FUNCTION ITEM
- self.bump(); // `unsafe`
- // `{` is also expected after `unsafe`; in case of error, include it in the diagnostic.
- self.check(&token::OpenDelim(token::Brace));
- let ext = self.parse_extern()?;
- self.expect_keyword(kw::Fn)?;
- let fn_span = self.prev_span;
- let header = FnHeader {
- unsafety: Unsafety::Unsafe,
- asyncness: respan(fn_span, IsAsync::NotAsync),
- constness: respan(fn_span, Constness::NotConst),
- ext,
- };
- return self.parse_item_fn(lo, vis, attrs, header);
- }
-
- if self.eat_keyword(kw::Mod) {
+ self.parse_item_impl(unsafety, defaultness)?
+ } else if self.eat_keyword(kw::Mod) {
// MODULE ITEM
- let info = self.parse_item_mod(&attrs[..])?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- if self.eat_keyword(kw::Type) {
+ self.parse_item_mod(attrs)?
+ } else if self.eat_keyword(kw::Type) {
// TYPE ITEM
let (ident, ty, generics) = self.parse_type_alias()?;
- let kind = ItemKind::TyAlias(ty, generics);
- return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
- }
-
- if self.eat_keyword(kw::Enum) {
+ (ident, ItemKind::TyAlias(ty, generics), None)
+ } else if self.eat_keyword(kw::Enum) {
// ENUM ITEM
- let info = self.parse_item_enum()?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- if self.check_keyword(kw::Trait)
+ self.parse_item_enum()?
+ } else if self.check_keyword(kw::Trait)
|| (self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait]))
{
// TRAIT ITEM
- let info = self.parse_item_trait(lo, Unsafety::Normal)?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- if self.eat_keyword(kw::Struct) {
+ self.parse_item_trait(lo, Unsafe::No)?
+ } else if self.eat_keyword(kw::Struct) {
// STRUCT ITEM
- let info = self.parse_item_struct()?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
-
- if self.is_union_item() {
+ self.parse_item_struct()?
+ } else if self.is_union_item() {
// UNION ITEM
- self.bump();
- let info = self.parse_item_union()?;
- return self.mk_item_with_info(attrs, lo, vis, info);
- }
+ self.bump(); // `union`
+ self.parse_item_union()?
+ } else if self.eat_keyword(kw::Macro) {
+ // MACROS 2.0 ITEM
+ self.parse_item_decl_macro(lo)?
+ } else if self.is_macro_rules_item() {
+ // MACRO_RULES ITEM
+ self.parse_item_macro_rules(vis)?
+ } else if vis.node.is_pub()
+ && self.check_ident()
+ && self.look_ahead(1, |t| *t != token::Not)
+ {
+ self.recover_missing_kw_before_item()?;
+ return Ok(None);
+ } else if macros_allowed && self.token.is_path_start() {
+ // MACRO INVOCATION ITEM
+ self.parse_item_macro(vis)?
+ } else {
+ return Ok(None);
+ };
+ Ok(Some(info))
+ }
- if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
- return Ok(Some(macro_def));
- }
+ /// Recover on encountering a struct or method definition where the user
+ /// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
+ fn recover_missing_kw_before_item(&mut self) -> PResult<'a, ()> {
+ // Space between `pub` keyword and the identifier
+ //
+ // pub S {}
+ // ^^^ `sp` points here
+ let sp = self.prev_span.between(self.token.span);
+ let full_sp = self.prev_span.to(self.token.span);
+ let ident_sp = self.token.span;
+ if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
+ // possible public struct definition where `struct` was forgotten
+ let ident = self.parse_ident().unwrap();
+ let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
+ let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
+ err.span_suggestion_short(
+ sp,
+ &msg,
+ " struct ".into(),
+ Applicability::MaybeIncorrect, // speculative
+ );
+ return Err(err);
+ } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
+ let ident = self.parse_ident().unwrap();
+ self.bump(); // `(`
+ let kw_name = self.recover_first_param();
+ self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
+ let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
+ self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
+ self.bump(); // `{`
+ ("fn", kw_name, false)
+ } else if self.check(&token::OpenDelim(token::Brace)) {
+ self.bump(); // `{`
+ ("fn", kw_name, false)
+ } else if self.check(&token::Colon) {
+ let kw = "struct";
+ (kw, kw, false)
+ } else {
+ ("fn` or `struct", "function or struct", true)
+ };
- // Verify whether we have encountered a struct or method definition where the user forgot to
- // add the `struct` or `fn` keyword after writing `pub`: `pub S {}`
- if vis.node.is_pub() && self.check_ident() && self.look_ahead(1, |t| *t != token::Not) {
- // Space between `pub` keyword and the identifier
- //
- // pub S {}
- // ^^^ `sp` points here
- let sp = self.prev_span.between(self.token.span);
- let full_sp = self.prev_span.to(self.token.span);
- let ident_sp = self.token.span;
- if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
- // possible public struct definition where `struct` was forgotten
- let ident = self.parse_ident().unwrap();
- let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
- let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
+ let msg = format!("missing `{}` for {} definition", kw, kw_name);
+ let mut err = self.struct_span_err(sp, &msg);
+ if !ambiguous {
+ self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
+ let suggestion =
+ format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
err.span_suggestion_short(
sp,
- &msg,
- " struct ".into(),
- Applicability::MaybeIncorrect, // speculative
+ &suggestion,
+ format!(" {} ", kw),
+ Applicability::MachineApplicable,
);
- return Err(err);
- } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
- let ident = self.parse_ident().unwrap();
- self.bump(); // `(`
- let kw_name = self.recover_first_param();
- self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
- let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
- self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
- self.bump(); // `{`
- ("fn", kw_name, false)
- } else if self.check(&token::OpenDelim(token::Brace)) {
- self.bump(); // `{`
- ("fn", kw_name, false)
- } else if self.check(&token::Colon) {
- let kw = "struct";
- (kw, kw, false)
- } else {
- ("fn` or `struct", "function or struct", true)
- };
-
- let msg = format!("missing `{}` for {} definition", kw, kw_name);
- let mut err = self.struct_span_err(sp, &msg);
- if !ambiguous {
- self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
- let suggestion =
- format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
- err.span_suggestion_short(
- sp,
- &suggestion,
- format!(" {} ", kw),
- Applicability::MachineApplicable,
+ } else {
+ if let Ok(snippet) = self.span_to_snippet(ident_sp) {
+ err.span_suggestion(
+ full_sp,
+ "if you meant to call a macro, try",
+ format!("{}!", snippet),
+ // this is the `ambiguous` conditional branch
+ Applicability::MaybeIncorrect,
);
} else {
- if let Ok(snippet) = self.span_to_snippet(ident_sp) {
- err.span_suggestion(
- full_sp,
- "if you meant to call a macro, try",
- format!("{}!", snippet),
- // this is the `ambiguous` conditional branch
- Applicability::MaybeIncorrect,
- );
- } else {
- err.help(
- "if you meant to call a macro, remove the `pub` \
+ err.help(
+ "if you meant to call a macro, remove the `pub` \
and add a trailing `!` after the identifier",
- );
- }
- }
- return Err(err);
- } else if self.look_ahead(1, |t| *t == token::Lt) {
- let ident = self.parse_ident().unwrap();
- self.eat_to_tokens(&[&token::Gt]);
- self.bump(); // `>`
- let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
- ("fn", self.recover_first_param(), false)
- } else if self.check(&token::OpenDelim(token::Brace)) {
- ("struct", "struct", false)
- } else {
- ("fn` or `struct", "function or struct", true)
- };
- let msg = format!("missing `{}` for {} definition", kw, kw_name);
- let mut err = self.struct_span_err(sp, &msg);
- if !ambiguous {
- err.span_suggestion_short(
- sp,
- &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
- format!(" {} ", kw),
- Applicability::MachineApplicable,
);
}
- return Err(err);
}
+ return Err(err);
+ } else if self.look_ahead(1, |t| *t == token::Lt) {
+ let ident = self.parse_ident().unwrap();
+ self.eat_to_tokens(&[&token::Gt]);
+ self.bump(); // `>`
+ let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
+ ("fn", self.recover_first_param(), false)
+ } else if self.check(&token::OpenDelim(token::Brace)) {
+ ("struct", "struct", false)
+ } else {
+ ("fn` or `struct", "function or struct", true)
+ };
+ let msg = format!("missing `{}` for {} definition", kw, kw_name);
+ let mut err = self.struct_span_err(sp, &msg);
+ if !ambiguous {
+ err.span_suggestion_short(
+ sp,
+ &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
+ format!(" {} ", kw),
+ Applicability::MachineApplicable,
+ );
+ }
+ return Err(err);
+ } else {
+ Ok(())
}
- self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
}
pub(super) fn mk_item_with_info(
lo: Span,
vis: Visibility,
info: ItemInfo,
- ) -> PResult<'a, Option<P<Item>>> {
+ ) -> P<Item> {
let (ident, item, extra_attrs) = info;
let span = lo.to(self.prev_span);
let attrs = Self::maybe_append(attrs, extra_attrs);
- Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
+ self.mk_item(span, ident, item, vis, attrs)
}
fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
lhs
}
- /// This is the fall-through for parsing items.
- fn parse_macro_use_or_failure(
- &mut self,
- attrs: Vec<Attribute>,
- macros_allowed: bool,
- attributes_allowed: bool,
- lo: Span,
- visibility: Visibility,
- ) -> PResult<'a, Option<P<Item>>> {
- if macros_allowed
- && self.token.is_path_start()
- && !(self.is_async_fn() && self.token.span.rust_2015())
- {
- // MACRO INVOCATION ITEM
-
- let prev_span = self.prev_span;
- self.complain_if_pub_macro(&visibility.node, prev_span);
-
- // Item macro
- let path = self.parse_path(PathStyle::Mod)?;
- self.expect(&token::Not)?;
- let args = self.parse_mac_args()?;
- if args.need_semicolon() && !self.eat(&token::Semi) {
- self.report_invalid_macro_expansion_item();
- }
-
- let hi = self.prev_span;
- let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
- let item =
- self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
- return Ok(Some(item));
- }
+ /// Parses an item macro, e.g., `item!();`.
+ fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
+ self.complain_if_pub_macro(&vis.node, vis.span);
- // FAILURE TO PARSE ITEM
- match visibility.node {
- VisibilityKind::Inherited => {}
- _ => return Err(self.struct_span_err(self.prev_span, "unmatched visibility `pub`")),
+ // Item macro
+ let path = self.parse_path(PathStyle::Mod)?;
+ self.expect(&token::Not)?;
+ let args = self.parse_mac_args()?;
+ if args.need_semicolon() && !self.eat(&token::Semi) {
+ self.report_invalid_macro_expansion_item();
}
- if !attributes_allowed && !attrs.is_empty() {
- self.expected_item_err(&attrs)?;
- }
- Ok(None)
+ let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
+ Ok((Ident::invalid(), ItemKind::Mac(mac), None))
}
/// Emits an expected-item-after-attributes error.
/// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
fn parse_item_impl(
&mut self,
- unsafety: Unsafety,
+ unsafety: Unsafe,
defaultness: Defaultness,
) -> PResult<'a, ItemInfo> {
// First, parse generic parameters if necessary.
generics
};
- let constness = if self.eat_keyword(kw::Const) {
- let span = self.prev_span;
+ let constness = self.parse_constness();
+ if let Const::Yes(span) = constness {
self.sess.gated_spans.gate(sym::const_trait_impl, span);
- Constness::Const
- } else {
- Constness::NotConst
- };
+ }
// Disambiguate `impl !Trait for Type { ... }` and `impl ! { ... }` for the never type.
let polarity = if self.check(&token::Not) && self.look_ahead(1, |t| t.can_begin_type()) {
generics.where_clause = self.parse_where_clause()?;
- let (impl_items, attrs) = self.parse_impl_body()?;
+ let (impl_items, attrs) = self.parse_item_list(|p, at_end| p.parse_impl_item(at_end))?;
let item_kind = match ty_second {
Some(ty_second) => {
Ok((Ident::invalid(), item_kind, Some(attrs)))
}
- fn parse_impl_body(&mut self) -> PResult<'a, (Vec<P<AssocItem>>, Vec<Attribute>)> {
+ fn parse_item_list<T>(
+ &mut self,
+ mut parse_item: impl FnMut(&mut Parser<'a>, &mut bool) -> PResult<'a, T>,
+ ) -> PResult<'a, (Vec<T>, Vec<Attribute>)> {
self.expect(&token::OpenDelim(token::Brace))?;
let attrs = self.parse_inner_attributes()?;
- let mut impl_items = Vec::new();
+ let mut items = Vec::new();
while !self.eat(&token::CloseDelim(token::Brace)) {
+ if self.recover_doc_comment_before_brace() {
+ continue;
+ }
let mut at_end = false;
- match self.parse_impl_item(&mut at_end) {
- Ok(impl_item) => impl_items.push(impl_item),
+ match parse_item(self, &mut at_end) {
+ Ok(item) => items.push(item),
Err(mut err) => {
err.emit();
if !at_end {
}
}
}
- Ok((impl_items, attrs))
+ Ok((items, attrs))
+ }
+
+ /// Recover on a doc comment before `}`.
+ fn recover_doc_comment_before_brace(&mut self) -> bool {
+ if let token::DocComment(_) = self.token.kind {
+ if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
+ struct_span_err!(
+ self.diagnostic(),
+ self.token.span,
+ E0584,
+ "found a documentation comment that doesn't document anything",
+ )
+ .span_label(self.token.span, "this doc comment doesn't document anything")
+ .help(
+ "doc comments must come before what they document, maybe a \
+ comment was intended with `//`?",
+ )
+ .emit();
+ self.bump();
+ return true;
+ }
+ }
+ false
}
/// Parses defaultness (i.e., `default` or nothing).
}
/// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
- fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
+ fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafe) -> PResult<'a, ItemInfo> {
// Parse optional `auto` prefix.
let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
let msg = "trait aliases cannot be `auto`";
self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
}
- if unsafety != Unsafety::Normal {
+ if let Unsafe::Yes(_) = unsafety {
let msg = "trait aliases cannot be `unsafe`";
self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
}
} else {
// It's a normal trait.
tps.where_clause = self.parse_where_clause()?;
- self.expect(&token::OpenDelim(token::Brace))?;
- let mut trait_items = vec![];
- while !self.eat(&token::CloseDelim(token::Brace)) {
- if let token::DocComment(_) = self.token.kind {
- if self.look_ahead(1, |tok| tok == &token::CloseDelim(token::Brace)) {
- struct_span_err!(
- self.diagnostic(),
- self.token.span,
- E0584,
- "found a documentation comment that doesn't document anything",
- )
- .help(
- "doc comments must come before what they document, maybe a \
- comment was intended with `//`?",
- )
- .emit();
- self.bump();
- continue;
- }
- }
- let mut at_end = false;
- match self.parse_trait_item(&mut at_end) {
- Ok(item) => trait_items.push(item),
- Err(mut e) => {
- e.emit();
- if !at_end {
- self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
- break;
- }
- }
- }
- }
- Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, trait_items), None))
+ let (items, attrs) = self.parse_item_list(|p, at_end| p.parse_trait_item(at_end))?;
+ Ok((ident, ItemKind::Trait(is_auto, unsafety, tps, bounds, items), Some(attrs)))
}
}
fn parse_assoc_item(
&mut self,
at_end: &mut bool,
- is_name_required: fn(&token::Token) -> bool,
+ req_name: fn(&token::Token) -> bool,
) -> PResult<'a, P<AssocItem>> {
let attrs = self.parse_outer_attributes()?;
let mut unclosed_delims = vec![];
let (mut item, tokens) = self.collect_tokens(|this| {
- let item = this.parse_assoc_item_(at_end, attrs, is_name_required);
+ let item = this.parse_assoc_item_(at_end, attrs, req_name);
unclosed_delims.append(&mut this.unclosed_delims);
item
})?;
&mut self,
at_end: &mut bool,
mut attrs: Vec<Attribute>,
- is_name_required: fn(&token::Token) -> bool,
+ req_name: fn(&token::Token) -> bool,
) -> PResult<'a, AssocItem> {
let lo = self.token.span;
let vis = self.parse_visibility(FollowedByType::No)?;
let defaultness = self.parse_defaultness();
- let (name, kind, generics) = if self.eat_keyword(kw::Type) {
+
+ let (ident, kind, generics) = if self.eat_keyword(kw::Type) {
self.parse_assoc_ty()?
- } else if self.is_const_item() {
- self.parse_assoc_const()?
+ } else if self.check_fn_front_matter() {
+ let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
+ (ident, AssocItemKind::Fn(sig, body), generics)
} else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
(Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
} else {
- self.parse_assoc_fn(at_end, &mut attrs, is_name_required)?
+ self.parse_assoc_const()?
};
- Ok(AssocItem {
- id: DUMMY_NODE_ID,
- span: lo.to(self.prev_span),
- ident: name,
- attrs,
- vis,
- defaultness,
- generics,
- kind,
- tokens: None,
- })
- }
-
- /// Returns `true` if we are looking at `const ID`
- /// (returns `false` for things like `const fn`, etc.).
- fn is_const_item(&self) -> bool {
- self.token.is_keyword(kw::Const) && !self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe])
+ let span = lo.to(self.prev_span);
+ let id = DUMMY_NODE_ID;
+ Ok(AssocItem { id, span, ident, attrs, vis, defaultness, generics, kind, tokens: None })
}
/// This parses the grammar:
/// extern crate foo;
/// extern crate bar as foo;
/// ```
- fn parse_item_extern_crate(
- &mut self,
- lo: Span,
- visibility: Visibility,
- attrs: Vec<Attribute>,
- ) -> PResult<'a, P<Item>> {
+ fn parse_item_extern_crate(&mut self) -> PResult<'a, ItemInfo> {
// Accept `extern crate name-like-this` for better diagnostics
let orig_name = self.parse_crate_name_with_dashes()?;
let (item_name, orig_name) = if let Some(rename) = self.parse_rename()? {
(orig_name, None)
};
self.expect_semi()?;
-
- let span = lo.to(self.prev_span);
- Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
+ Ok((item_name, ItemKind::ExternCrate(orig_name), None))
}
fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
/// Parses `extern` for foreign ABIs modules.
///
- /// `extern` is expected to have been
- /// consumed before calling this method.
+ /// `extern` is expected to have been consumed before calling this method.
///
/// # Examples
///
/// extern "C" {}
/// extern {}
/// ```
- fn parse_item_foreign_mod(
- &mut self,
- lo: Span,
- abi: Option<StrLit>,
- visibility: Visibility,
- mut attrs: Vec<Attribute>,
- extern_sp: Span,
- ) -> PResult<'a, P<Item>> {
- self.expect(&token::OpenDelim(token::Brace))?;
-
- attrs.extend(self.parse_inner_attributes()?);
-
- let mut foreign_items = vec![];
- while !self.eat(&token::CloseDelim(token::Brace)) {
- foreign_items.push(self.parse_foreign_item(extern_sp)?);
- }
-
- let prev_span = self.prev_span;
- let m = ast::ForeignMod { abi, items: foreign_items };
- let invalid = Ident::invalid();
- Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs))
+ fn parse_item_foreign_mod(&mut self) -> PResult<'a, ItemInfo> {
+ let abi = self.parse_abi(); // ABI?
+ let (items, attrs) = self.parse_item_list(|p, at_end| p.parse_foreign_item(at_end))?;
+ let module = ast::ForeignMod { abi, items };
+ Ok((Ident::invalid(), ItemKind::ForeignMod(module), Some(attrs)))
}
- /// Parses a foreign item.
- pub fn parse_foreign_item(&mut self, extern_sp: Span) -> PResult<'a, P<ForeignItem>> {
+ /// Parses a foreign item (one in an `extern { ... }` block).
+ pub fn parse_foreign_item(&mut self, at_end: &mut bool) -> PResult<'a, P<ForeignItem>> {
maybe_whole!(self, NtForeignItem, |ni| ni);
- let attrs = self.parse_outer_attributes()?;
+ let mut attrs = self.parse_outer_attributes()?;
let lo = self.token.span;
- let visibility = self.parse_visibility(FollowedByType::No)?;
-
- // FOREIGN STATIC ITEM
- // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
- if self.check_keyword(kw::Static) || self.token.is_keyword(kw::Const) {
- if self.token.is_keyword(kw::Const) {
- let mut err =
- self.struct_span_err(self.token.span, "extern items cannot be `const`");
+ let vis = self.parse_visibility(FollowedByType::No)?;
- // The user wrote 'const fn'
- if self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe]) {
- err.emit();
- // Consume `const`
- self.bump();
- // Consume `unsafe` if present, since `extern` blocks
- // don't allow it. This will leave behind a plain 'fn'
- self.eat_keyword(kw::Unsafe);
- // Treat 'const fn` as a plain `fn` for error recovery purposes.
- // We've already emitted an error, so compilation is guaranteed
- // to fail
- return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?);
- }
- err.span_suggestion(
- self.token.span,
+ let (ident, kind) = if self.check_keyword(kw::Type) {
+ // FOREIGN TYPE ITEM
+ self.parse_item_foreign_type()?
+ } else if self.check_fn_front_matter() {
+ // FOREIGN FUNCTION ITEM
+ let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, |_| true)?;
+ (ident, ForeignItemKind::Fn(sig, generics, body))
+ } else if self.is_static_global() {
+ // FOREIGN STATIC ITEM
+ self.bump(); // `static`
+ self.parse_item_foreign_static()?
+ } else if self.token.is_keyword(kw::Const) {
+ // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
+ self.bump(); // `const`
+ self.struct_span_err(self.prev_span, "extern items cannot be `const`")
+ .span_suggestion(
+ self.prev_span,
"try using a static value",
"static".to_owned(),
Applicability::MachineApplicable,
- );
- err.emit();
+ )
+ .emit();
+ self.parse_item_foreign_static()?
+ } else if let Some(mac) = self.parse_assoc_macro_invoc("extern", Some(&vis), at_end)? {
+ (Ident::invalid(), ForeignItemKind::Macro(mac))
+ } else {
+ if !attrs.is_empty() {
+ self.expected_item_err(&attrs)?;
}
- self.bump(); // `static` or `const`
- return Ok(self.parse_item_foreign_static(visibility, lo, attrs)?);
- }
- // FOREIGN FUNCTION ITEM
- if self.check_keyword(kw::Fn) {
- return Ok(self.parse_item_foreign_fn(visibility, lo, attrs, extern_sp)?);
- }
- // FOREIGN TYPE ITEM
- if self.check_keyword(kw::Type) {
- return Ok(self.parse_item_foreign_type(visibility, lo, attrs)?);
- }
-
- match self.parse_assoc_macro_invoc("extern", Some(&visibility), &mut false)? {
- Some(mac) => Ok(P(ForeignItem {
- ident: Ident::invalid(),
- span: lo.to(self.prev_span),
- id: DUMMY_NODE_ID,
- attrs,
- vis: visibility,
- kind: ForeignItemKind::Macro(mac),
- tokens: None,
- })),
- None => {
- if !attrs.is_empty() {
- self.expected_item_err(&attrs)?;
- }
+ self.unexpected()?
+ };
- self.unexpected()
- }
- }
+ let span = lo.to(self.prev_span);
+ Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
}
/// Parses a static item from a foreign module.
/// Assumes that the `static` keyword is already parsed.
- fn parse_item_foreign_static(
- &mut self,
- vis: ast::Visibility,
- lo: Span,
- attrs: Vec<Attribute>,
- ) -> PResult<'a, P<ForeignItem>> {
+ fn parse_item_foreign_static(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
let mutbl = self.parse_mutability();
let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
- let hi = self.token.span;
self.expect_semi()?;
- Ok(P(ForeignItem {
- ident,
- attrs,
- kind: ForeignItemKind::Static(ty, mutbl),
- id: DUMMY_NODE_ID,
- span: lo.to(hi),
- vis,
- tokens: None,
- }))
+ Ok((ident, ForeignItemKind::Static(ty, mutbl)))
}
/// Parses a type from a foreign module.
- fn parse_item_foreign_type(
- &mut self,
- vis: ast::Visibility,
- lo: Span,
- attrs: Vec<Attribute>,
- ) -> PResult<'a, P<ForeignItem>> {
+ fn parse_item_foreign_type(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
self.expect_keyword(kw::Type)?;
-
let ident = self.parse_ident()?;
- let hi = self.token.span;
self.expect_semi()?;
- Ok(P(ast::ForeignItem {
- ident,
- attrs,
- kind: ForeignItemKind::Ty,
- id: DUMMY_NODE_ID,
- span: lo.to(hi),
- vis,
- tokens: None,
- }))
+ Ok((ident, ForeignItemKind::Ty))
}
fn is_static_global(&mut self) -> bool {
})
}
- pub(super) fn eat_macro_def(
- &mut self,
- attrs: &[Attribute],
- vis: &Visibility,
- lo: Span,
- ) -> PResult<'a, Option<P<Item>>> {
- let (ident, def) = if self.eat_keyword(kw::Macro) {
- let ident = self.parse_ident()?;
+ /// Parses a declarative macro 2.0 definition.
+ /// The `macro` keyword has already been parsed.
+ fn parse_item_decl_macro(&mut self, lo: Span) -> PResult<'a, ItemInfo> {
+ let ident = self.parse_ident()?;
+ let body = if self.check(&token::OpenDelim(token::Brace)) {
+ self.parse_mac_args()?
+ } else if self.check(&token::OpenDelim(token::Paren)) {
+ let params = self.parse_token_tree();
+ let pspan = params.span();
let body = if self.check(&token::OpenDelim(token::Brace)) {
- self.parse_mac_args()?
- } else if self.check(&token::OpenDelim(token::Paren)) {
- let params = self.parse_token_tree();
- let pspan = params.span();
- let body = if self.check(&token::OpenDelim(token::Brace)) {
- self.parse_token_tree()
- } else {
- return self.unexpected();
- };
- let bspan = body.span();
- let tokens = TokenStream::new(vec![
- params.into(),
- TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
- body.into(),
- ]);
- let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
- P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
+ self.parse_token_tree()
} else {
return self.unexpected();
};
+ let bspan = body.span();
+ let tokens = TokenStream::new(vec![
+ params.into(),
+ TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
+ body.into(),
+ ]);
+ let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
+ P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
+ } else {
+ return self.unexpected();
+ };
+
+ self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_span));
+ Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: false }), None))
+ }
- (ident, ast::MacroDef { body, legacy: false })
- } else if self.check_keyword(sym::macro_rules)
+ /// Is this unambiguously the start of a `macro_rules! foo` item defnition?
+ fn is_macro_rules_item(&mut self) -> bool {
+ self.check_keyword(sym::macro_rules)
&& self.look_ahead(1, |t| *t == token::Not)
&& self.look_ahead(2, |t| t.is_ident())
- {
- let prev_span = self.prev_span;
- self.complain_if_pub_macro(&vis.node, prev_span);
- self.bump();
- self.bump();
-
- let ident = self.parse_ident()?;
- let body = self.parse_mac_args()?;
- if body.need_semicolon() && !self.eat(&token::Semi) {
- self.report_invalid_macro_expansion_item();
- }
-
- (ident, ast::MacroDef { body, legacy: true })
- } else {
- return Ok(None);
- };
+ }
- let span = lo.to(self.prev_span);
+ /// Parses a legacy `macro_rules! foo { ... }` declarative macro.
+ fn parse_item_macro_rules(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
+ self.complain_if_pub_macro(&vis.node, vis.span);
+ self.expect_keyword(sym::macro_rules)?; // `macro_rules`
+ self.expect(&token::Not)?; // `!`
- if !def.legacy {
- self.sess.gated_spans.gate(sym::decl_macro, span);
+ let ident = self.parse_ident()?;
+ let body = self.parse_mac_args()?;
+ if body.need_semicolon() && !self.eat(&token::Semi) {
+ self.report_invalid_macro_expansion_item();
}
- Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
+ Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true }), None))
+ }
+
+ pub(super) fn eat_macro_def(
+ &mut self,
+ attrs: &[Attribute],
+ vis: &Visibility,
+ lo: Span,
+ ) -> PResult<'a, Option<P<Item>>> {
+ let info = if self.eat_keyword(kw::Macro) {
+ self.parse_item_decl_macro(lo)?
+ } else if self.is_macro_rules_item() {
+ self.parse_item_macro_rules(vis)?
+ } else {
+ return Ok(None);
+ };
+ Ok(Some(self.mk_item_with_info(attrs.to_vec(), lo, vis.clone(), info)))
}
fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
}
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
-pub(super) struct ParamCfg {
- /// Is `self` is allowed as the first parameter?
- pub is_self_allowed: bool,
- /// `is_name_required` decides if, per-parameter,
- /// the parameter must have a pattern or just a type.
- pub is_name_required: fn(&token::Token) -> bool,
-}
+///
+/// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
+type ReqName = fn(&token::Token) -> bool;
/// Parsing of functions and methods.
impl<'a> Parser<'a> {
- /// Parses an item-position function declaration.
- fn parse_item_fn(
- &mut self,
- lo: Span,
- vis: Visibility,
- attrs: Vec<Attribute>,
- header: FnHeader,
- ) -> PResult<'a, Option<P<Item>>> {
- let (ident, decl, generics) =
- self.parse_fn_sig(ParamCfg { is_self_allowed: false, is_name_required: |_| true })?;
- let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
- let kind = ItemKind::Fn(FnSig { decl, header }, generics, body);
- self.mk_item_with_info(attrs, lo, vis, (ident, kind, Some(inner_attrs)))
- }
-
- /// Parses a function declaration from a foreign module.
- fn parse_item_foreign_fn(
- &mut self,
- vis: ast::Visibility,
- lo: Span,
- attrs: Vec<Attribute>,
- extern_sp: Span,
- ) -> PResult<'a, P<ForeignItem>> {
- self.expect_keyword(kw::Fn)?;
- let (ident, decl, generics) =
- self.parse_fn_sig(ParamCfg { is_self_allowed: false, is_name_required: |_| true })?;
- let span = lo.to(self.token.span);
- self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?;
- Ok(P(ast::ForeignItem {
- ident,
- attrs,
- kind: ForeignItemKind::Fn(decl, generics),
- id: DUMMY_NODE_ID,
- span,
- vis,
- tokens: None,
- }))
- }
-
- fn parse_assoc_fn(
+ /// Parse a function starting from the front matter (`const ...`) to the body `{ ... }` or `;`.
+ fn parse_fn(
&mut self,
at_end: &mut bool,
attrs: &mut Vec<Attribute>,
- is_name_required: fn(&token::Token) -> bool,
- ) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
- let header = self.parse_fn_front_matter()?;
- let (ident, decl, generics) =
- self.parse_fn_sig(ParamCfg { is_self_allowed: true, is_name_required })?;
- let sig = FnSig { header, decl };
- let body = self.parse_assoc_fn_body(at_end, attrs)?;
- Ok((ident, AssocItemKind::Fn(sig, body), generics))
+ req_name: ReqName,
+ ) -> PResult<'a, (Ident, FnSig, Generics, Option<P<Block>>)> {
+ let header = self.parse_fn_front_matter()?; // `const ... fn`
+ let ident = self.parse_ident()?; // `foo`
+ let mut generics = self.parse_generics()?; // `<'a, T, ...>`
+ let decl = self.parse_fn_decl(req_name, AllowPlus::Yes)?; // `(p: u8, ...)`
+ generics.where_clause = self.parse_where_clause()?; // `where T: Ord`
+ let body = self.parse_fn_body(at_end, attrs)?; // `;` or `{ ... }`.
+ Ok((ident, FnSig { header, decl }, generics, body))
}
- /// Parse the "body" of a method in an associated item definition.
+ /// Parse the "body" of a function.
/// This can either be `;` when there's no body,
- /// or e.g. a block when the method is a provided one.
- fn parse_assoc_fn_body(
+ /// or e.g. a block when the function is a provided one.
+ fn parse_fn_body(
&mut self,
at_end: &mut bool,
attrs: &mut Vec<Attribute>,
) -> PResult<'a, Option<P<Block>>> {
- Ok(match self.token.kind {
+ let (inner_attrs, body) = match self.token.kind {
token::Semi => {
- debug!("parse_assoc_fn_body(): parsing required method");
self.bump();
- *at_end = true;
- None
+ (Vec::new(), None)
}
token::OpenDelim(token::Brace) => {
- debug!("parse_assoc_fn_body(): parsing provided method");
- *at_end = true;
- let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
- attrs.extend(inner_attrs.iter().cloned());
- Some(body)
+ let (attrs, body) = self.parse_inner_attrs_and_block()?;
+ (attrs, Some(body))
}
token::Interpolated(ref nt) => match **nt {
token::NtBlock(..) => {
- *at_end = true;
- let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
- attrs.extend(inner_attrs.iter().cloned());
- Some(body)
+ let (attrs, body) = self.parse_inner_attrs_and_block()?;
+ (attrs, Some(body))
}
_ => return self.expected_semi_or_open_brace(),
},
_ => return self.expected_semi_or_open_brace(),
- })
+ };
+ attrs.extend(inner_attrs);
+ *at_end = true;
+ Ok(body)
+ }
+
+ /// Is the current token the start of an `FnHeader` / not a valid parse?
+ fn check_fn_front_matter(&mut self) -> bool {
+ // We use an over-approximation here.
+ // `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
+ const QUALS: [Symbol; 4] = [kw::Const, kw::Async, kw::Unsafe, kw::Extern];
+ self.check_keyword(kw::Fn) // Definitely an `fn`.
+ // `$qual fn` or `$qual $qual`:
+ || QUALS.iter().any(|&kw| self.check_keyword(kw))
+ && self.look_ahead(1, |t| {
+ // ...qualified and then `fn`, e.g. `const fn`.
+ t.is_keyword(kw::Fn)
+ // Two qualifiers. This is enough. Due `async` we need to check that it's reserved.
+ || t.is_non_raw_ident_where(|i| QUALS.contains(&i.name) && i.is_reserved())
+ })
+ // `extern ABI fn`
+ || self.check_keyword(kw::Extern)
+ && self.look_ahead(1, |t| t.can_begin_literal_or_bool())
+ && self.look_ahead(2, |t| t.is_keyword(kw::Fn))
}
- /// Parses all the "front matter" for a `fn` declaration, up to
- /// and including the `fn` keyword:
+ /// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
+ /// up to and including the `fn` keyword. The formal grammar is:
///
- /// - `const fn`
- /// - `unsafe fn`
- /// - `const unsafe fn`
- /// - `extern fn`
- /// - etc.
+ /// ```
+ /// Extern = "extern" StringLit ;
+ /// FnQual = "const"? "async"? "unsafe"? Extern? ;
+ /// FnFrontMatter = FnQual? "fn" ;
+ /// ```
fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
- let is_const_fn = self.eat_keyword(kw::Const);
- let const_span = self.prev_span;
+ let constness = self.parse_constness();
let asyncness = self.parse_asyncness();
- if let IsAsync::Async { .. } = asyncness {
- self.ban_async_in_2015(self.prev_span);
- }
- let asyncness = respan(self.prev_span, asyncness);
let unsafety = self.parse_unsafety();
- let (constness, unsafety, ext) = if is_const_fn {
- (respan(const_span, Constness::Const), unsafety, Extern::None)
- } else {
- let ext = self.parse_extern()?;
- (respan(self.prev_span, Constness::NotConst), unsafety, ext)
- };
+ let ext = self.parse_extern()?;
+
+ if let Async::Yes { span, .. } = asyncness {
+ self.ban_async_in_2015(span);
+ }
+
if !self.eat_keyword(kw::Fn) {
// It is possible for `expect_one_of` to recover given the contents of
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
unreachable!()
}
}
+
Ok(FnHeader { constness, unsafety, asyncness, ext })
}
- /// Parse the "signature", including the identifier, parameters, and generics of a function.
- fn parse_fn_sig(&mut self, cfg: ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
- let ident = self.parse_ident()?;
- let mut generics = self.parse_generics()?;
- let decl = self.parse_fn_decl(cfg, true)?;
- generics.where_clause = self.parse_where_clause()?;
- Ok((ident, decl, generics))
+ /// We are parsing `async fn`. If we are on Rust 2015, emit an error.
+ fn ban_async_in_2015(&self, span: Span) {
+ if span.rust_2015() {
+ let diag = self.diagnostic();
+ struct_span_err!(diag, span, E0670, "`async fn` is not permitted in the 2015 edition")
+ .note("to use `async fn`, switch to Rust 2018")
+ .help("set `edition = \"2018\"` in `Cargo.toml`")
+ .note("for more on editions, read https://doc.rust-lang.org/edition-guide")
+ .emit();
+ }
}
/// Parses the parameter list and result type of a function declaration.
pub(super) fn parse_fn_decl(
&mut self,
- cfg: ParamCfg,
- ret_allow_plus: bool,
+ req_name: ReqName,
+ ret_allow_plus: AllowPlus,
) -> PResult<'a, P<FnDecl>> {
Ok(P(FnDecl {
- inputs: self.parse_fn_params(cfg)?,
- output: self.parse_ret_ty(ret_allow_plus, true)?,
+ inputs: self.parse_fn_params(req_name)?,
+ output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
}))
}
/// Parses the parameter list of a function, including the `(` and `)` delimiters.
- fn parse_fn_params(&mut self, mut cfg: ParamCfg) -> PResult<'a, Vec<Param>> {
- let is_trait_item = cfg.is_self_allowed;
- // Parse the arguments, starting out with `self` being possibly allowed...
+ fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
+ let mut first_param = true;
+ // Parse the arguments, starting out with `self` being allowed...
let (mut params, _) = self.parse_paren_comma_seq(|p| {
- let param = p.parse_param_general(&cfg, is_trait_item).or_else(|mut e| {
+ let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
e.emit();
let lo = p.prev_span;
// Skip every token until next possible arg or end.
Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
});
// ...now that we've parsed the first argument, `self` is no longer allowed.
- cfg.is_self_allowed = false;
+ first_param = false;
param
})?;
// Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
Ok(params)
}
- /// Skips unexpected attributes and doc comments in this position and emits an appropriate
- /// error.
- /// This version of parse param doesn't necessarily require identifier names.
- fn parse_param_general(&mut self, cfg: &ParamCfg, is_trait_item: bool) -> PResult<'a, Param> {
+ /// Parses a single function parameter.
+ ///
+ /// - `self` is syntactically allowed when `first_param` holds.
+ fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
let lo = self.token.span;
let attrs = self.parse_outer_attributes()?;
// Possibly parse `self`. Recover if we parsed it and it wasn't allowed here.
if let Some(mut param) = self.parse_self_param()? {
param.attrs = attrs.into();
- return if cfg.is_self_allowed {
- Ok(param)
- } else {
- self.recover_bad_self_param(param, is_trait_item)
- };
+ return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
}
let is_name_required = match self.token.kind {
token::DotDotDot => false,
- _ => (cfg.is_name_required)(&self.token),
+ _ => req_name(&self.token),
};
let (pat, ty) = if is_name_required || self.is_named_param() {
debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);
let pat = self.parse_fn_param_pat()?;
if let Err(mut err) = self.expect(&token::Colon) {
- return if let Some(ident) = self.parameter_without_type(
- &mut err,
- pat,
- is_name_required,
- cfg.is_self_allowed,
- is_trait_item,
- ) {
+ return if let Some(ident) =
+ self.parameter_without_type(&mut err, pat, is_name_required, first_param)
+ {
err.emit();
Ok(dummy_arg(ident))
} else {
}
/// Returns the parsed optional self parameter and whether a self shortcut was used.
- ///
- /// See `parse_self_param_with_attrs` to collect attributes.
fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
// Extract an identifier *after* having confirmed that the token is one.
let expect_self_ident = |this: &mut Self| {