2019-12-22 17:42:04 -05:00
|
|
|
use super::diagnostics::{dummy_arg, ConsumeClosingDelim, Error};
|
2020-01-29 01:57:24 +01:00
|
|
|
use super::ty::{AllowPlus, RecoverQPath};
|
2019-12-22 17:42:04 -05:00
|
|
|
use super::{FollowedByType, Parser, PathStyle};
|
2019-10-08 09:46:06 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
use crate::maybe_whole;
|
2019-10-15 22:48:13 +02:00
|
|
|
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pprust;
|
2019-12-31 21:25:16 +01:00
|
|
|
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, PResult, StashKey};
|
2020-01-30 05:31:04 +01:00
|
|
|
use rustc_span::source_map::{self, Span};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
|
|
|
use rustc_span::BytePos;
|
2019-12-22 17:42:04 -05:00
|
|
|
use syntax::ast::{self, AttrKind, AttrStyle, AttrVec, Attribute, Ident, DUMMY_NODE_ID};
|
2019-12-03 16:38:34 +01:00
|
|
|
use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind};
|
2020-01-30 13:02:06 +01:00
|
|
|
use syntax::ast::{Async, Const, Defaultness, IsAuto, PathSegment, StrLit, Unsafe};
|
2019-12-22 17:42:04 -05:00
|
|
|
use syntax::ast::{BindingMode, Block, FnDecl, FnSig, Mac, MacArgs, MacDelimiter, Param, SelfKind};
|
|
|
|
use syntax::ast::{EnumDef, Generics, StructField, TraitRef, Ty, TyKind, Variant, VariantData};
|
|
|
|
use syntax::ast::{FnHeader, ForeignItem, ForeignItemKind, Mutability, Visibility, VisibilityKind};
|
2019-10-15 22:48:13 +02:00
|
|
|
use syntax::ptr::P;
|
2019-12-22 17:42:04 -05:00
|
|
|
use syntax::token;
|
|
|
|
use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
use log::debug;
|
2019-10-11 10:54:26 +02:00
|
|
|
use std::mem;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
pub(super) type ItemInfo = (Ident, ItemKind, Option<Vec<Attribute>>);
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
|
|
|
self.parse_item_(attrs, true, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn parse_item_(
|
|
|
|
&mut self,
|
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
macros_allowed: bool,
|
|
|
|
attributes_allowed: bool,
|
|
|
|
) -> PResult<'a, Option<P<Item>>> {
|
|
|
|
let mut unclosed_delims = vec![];
|
|
|
|
let (ret, tokens) = self.collect_tokens(|this| {
|
|
|
|
let item = this.parse_item_implementation(attrs, macros_allowed, attributes_allowed);
|
|
|
|
unclosed_delims.append(&mut this.unclosed_delims);
|
|
|
|
item
|
|
|
|
})?;
|
|
|
|
self.unclosed_delims.append(&mut unclosed_delims);
|
|
|
|
|
|
|
|
// Once we've parsed an item and recorded the tokens we got while
|
|
|
|
// parsing we may want to store `tokens` into the item we're about to
|
|
|
|
// return. Note, though, that we specifically didn't capture tokens
|
|
|
|
// related to outer attributes. The `tokens` field here may later be
|
|
|
|
// used with procedural macros to convert this item back into a token
|
|
|
|
// stream, but during expansion we may be removing attributes as we go
|
|
|
|
// along.
|
|
|
|
//
|
|
|
|
// If we've got inner attributes then the `tokens` we've got above holds
|
|
|
|
// these inner attributes. If an inner attribute is expanded we won't
|
|
|
|
// actually remove it from the token stream, so we'll just keep yielding
|
|
|
|
// it (bad!). To work around this case for now we just avoid recording
|
|
|
|
// `tokens` if we detect any inner attributes. This should help keep
|
|
|
|
// expansion correct, but we should fix this bug one day!
|
|
|
|
Ok(ret.map(|item| {
|
|
|
|
item.map(|mut i| {
|
|
|
|
if !i.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
|
|
|
|
i.tokens = Some(tokens);
|
|
|
|
}
|
|
|
|
i
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses one of the items allowed by the flags.
|
|
|
|
fn parse_item_implementation(
|
|
|
|
&mut self,
|
2020-01-31 07:24:23 +01:00
|
|
|
mut attrs: Vec<Attribute>,
|
2019-08-11 18:34:42 +02:00
|
|
|
macros_allowed: bool,
|
|
|
|
attributes_allowed: bool,
|
|
|
|
) -> PResult<'a, Option<P<Item>>> {
|
|
|
|
maybe_whole!(self, NtItem, |item| {
|
2020-01-31 07:24:23 +01:00
|
|
|
let mut item = item;
|
2019-08-11 18:34:42 +02:00
|
|
|
mem::swap(&mut item.attrs, &mut attrs);
|
|
|
|
item.attrs.extend(attrs);
|
2020-01-31 07:24:23 +01:00
|
|
|
Some(item)
|
2019-08-11 18:34:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
2019-11-07 11:26:36 +01:00
|
|
|
let vis = self.parse_visibility(FollowedByType::No)?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
if self.eat_keyword(kw::Use) {
|
|
|
|
// USE ITEM
|
|
|
|
let item_ = ItemKind::Use(P(self.parse_use_tree()?));
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
let span = lo.to(self.prev_span);
|
2019-10-01 12:40:56 +02:00
|
|
|
let item = self.mk_item(span, Ident::invalid(), item_, vis, attrs);
|
2019-08-11 18:34:42 +02:00
|
|
|
return Ok(Some(item));
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:40:16 +01:00
|
|
|
if self.check_fn_front_matter() {
|
2020-01-30 13:02:06 +01:00
|
|
|
// FUNCTION ITEM
|
2020-02-10 15:35:05 +01:00
|
|
|
let (ident, sig, generics, body) = self.parse_fn(&mut false, &mut attrs, |_| true)?;
|
2020-01-31 03:31:12 +01:00
|
|
|
let kind = ItemKind::Fn(sig, generics, body);
|
|
|
|
return self.mk_item_with_info(attrs, lo, vis, (ident, kind, None));
|
2020-01-30 13:02:06 +01:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.eat_keyword(kw::Extern) {
|
|
|
|
if self.eat_keyword(kw::Crate) {
|
2020-01-30 13:02:06 +01:00
|
|
|
// EXTERN CRATE
|
2019-10-01 12:40:56 +02:00
|
|
|
return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2020-01-30 13:02:06 +01:00
|
|
|
// EXTERN BLOCK
|
2019-11-10 17:04:12 +03:00
|
|
|
let abi = self.parse_abi();
|
2020-01-30 13:02:06 +01:00
|
|
|
return Ok(Some(self.parse_item_foreign_mod(lo, abi, vis, attrs)?));
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_static_global() {
|
|
|
|
// STATIC ITEM
|
2020-01-30 13:02:06 +01:00
|
|
|
self.bump();
|
2019-08-11 18:34:42 +02:00
|
|
|
let m = self.parse_mutability();
|
2019-10-01 13:48:54 +02:00
|
|
|
let info = self.parse_item_const(Some(m))?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2020-01-30 13:02:06 +01:00
|
|
|
if let Const::Yes(const_span) = self.parse_constness() {
|
2019-08-11 18:34:42 +02:00
|
|
|
// CONST ITEM
|
|
|
|
if self.eat_keyword(kw::Mut) {
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
self.struct_span_err(prev_span, "const globals cannot be mutable")
|
|
|
|
.span_label(prev_span, "cannot be mutable")
|
|
|
|
.span_suggestion(
|
|
|
|
const_span,
|
|
|
|
"you might want to declare a static instead",
|
|
|
|
"static".to_owned(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
|
|
|
let info = self.parse_item_const(None)?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.check_keyword(kw::Unsafe) && self.is_keyword_ahead(1, &[kw::Trait, kw::Auto]) {
|
2019-08-11 18:34:42 +02:00
|
|
|
// UNSAFE TRAIT ITEM
|
2020-01-30 02:42:33 +01:00
|
|
|
let unsafety = self.parse_unsafety();
|
|
|
|
let info = self.parse_item_trait(lo, unsafety)?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
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])
|
2019-10-01 13:48:54 +02:00
|
|
|
{
|
2019-08-11 18:34:42 +02:00
|
|
|
// IMPL ITEM
|
|
|
|
let defaultness = self.parse_defaultness();
|
|
|
|
let unsafety = self.parse_unsafety();
|
|
|
|
self.expect_keyword(kw::Impl)?;
|
2019-10-01 13:48:54 +02:00
|
|
|
let info = self.parse_item_impl(unsafety, defaultness)?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.eat_keyword(kw::Mod) {
|
|
|
|
// MODULE ITEM
|
2019-10-01 13:48:54 +02:00
|
|
|
let info = self.parse_item_mod(&attrs[..])?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-11-07 18:19:41 +01:00
|
|
|
if self.eat_keyword(kw::Type) {
|
2019-08-11 18:34:42 +02:00
|
|
|
// TYPE ITEM
|
2019-11-07 18:19:41 +01:00
|
|
|
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));
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.eat_keyword(kw::Enum) {
|
|
|
|
// ENUM ITEM
|
2019-10-01 13:48:54 +02:00
|
|
|
let info = self.parse_item_enum()?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.check_keyword(kw::Trait)
|
2019-12-22 17:42:04 -05:00
|
|
|
|| (self.check_keyword(kw::Auto) && self.is_keyword_ahead(1, &[kw::Trait]))
|
2019-08-11 18:34:42 +02:00
|
|
|
{
|
2019-10-01 13:48:54 +02:00
|
|
|
// TRAIT ITEM
|
2020-01-30 02:42:33 +01:00
|
|
|
let info = self.parse_item_trait(lo, Unsafe::No)?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.eat_keyword(kw::Struct) {
|
|
|
|
// STRUCT ITEM
|
2019-10-01 13:48:54 +02:00
|
|
|
let info = self.parse_item_struct()?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.is_union_item() {
|
|
|
|
// UNION ITEM
|
|
|
|
self.bump();
|
2019-10-01 13:48:54 +02:00
|
|
|
let info = self.parse_item_union()?;
|
2019-10-01 12:40:56 +02:00
|
|
|
return self.mk_item_with_info(attrs, lo, vis, info);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-10-01 13:48:54 +02:00
|
|
|
|
2019-10-01 12:40:56 +02:00
|
|
|
if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
|
2019-08-11 18:34:42 +02:00
|
|
|
return Ok(Some(macro_def));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {}`
|
2019-12-22 17:42:04 -05:00
|
|
|
if vis.node.is_pub() && self.check_ident() && self.look_ahead(1, |t| *t != token::Not) {
|
2019-08-11 18:34:42 +02:00
|
|
|
// 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();
|
2019-12-22 17:42:04 -05:00
|
|
|
let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
|
2019-12-30 14:56:57 +01:00
|
|
|
let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_suggestion_short(
|
2019-12-22 17:42:04 -05:00
|
|
|
sp,
|
|
|
|
&msg,
|
|
|
|
" struct ".into(),
|
|
|
|
Applicability::MaybeIncorrect, // speculative
|
2019-08-11 18:34:42 +02:00
|
|
|
);
|
|
|
|
return Err(err);
|
|
|
|
} else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
|
|
|
|
let ident = self.parse_ident().unwrap();
|
2019-12-22 17:42:04 -05:00
|
|
|
self.bump(); // `(`
|
2019-09-29 09:26:02 +02:00
|
|
|
let kw_name = self.recover_first_param();
|
2019-10-25 18:30:02 -07:00
|
|
|
self.consume_block(token::Paren, ConsumeClosingDelim::Yes);
|
2019-08-11 18:34:42 +02:00
|
|
|
let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) {
|
|
|
|
self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.bump(); // `{`
|
2019-08-11 18:34:42 +02:00
|
|
|
("fn", kw_name, false)
|
|
|
|
} else if self.check(&token::OpenDelim(token::Brace)) {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.bump(); // `{`
|
2019-08-11 18:34:42 +02:00
|
|
|
("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);
|
2019-12-30 14:56:57 +01:00
|
|
|
let mut err = self.struct_span_err(sp, &msg);
|
2019-08-11 18:34:42 +02:00
|
|
|
if !ambiguous {
|
2019-10-25 18:30:02 -07:00
|
|
|
self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
|
2019-12-22 17:42:04 -05:00
|
|
|
let suggestion =
|
|
|
|
format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name);
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_suggestion_short(
|
2019-12-22 17:42:04 -05:00
|
|
|
sp,
|
|
|
|
&suggestion,
|
|
|
|
format!(" {} ", kw),
|
|
|
|
Applicability::MachineApplicable,
|
2019-08-11 18:34:42 +02:00
|
|
|
);
|
|
|
|
} 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
|
2019-12-22 17:42:04 -05:00
|
|
|
Applicability::MaybeIncorrect,
|
2019-08-11 18:34:42 +02:00
|
|
|
);
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
err.help(
|
|
|
|
"if you meant to call a macro, remove the `pub` \
|
|
|
|
and add a trailing `!` after the identifier",
|
|
|
|
);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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]);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.bump(); // `>`
|
2019-08-11 18:34:42 +02:00
|
|
|
let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) {
|
2019-09-29 09:26:02 +02:00
|
|
|
("fn", self.recover_first_param(), false)
|
2019-08-11 18:34:42 +02:00
|
|
|
} 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);
|
2019-12-30 14:56:57 +01:00
|
|
|
let mut err = self.struct_span_err(sp, &msg);
|
2019-08-11 18:34:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 12:40:56 +02:00
|
|
|
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
|
|
|
|
}
|
|
|
|
|
2019-10-11 10:54:26 +02:00
|
|
|
pub(super) fn mk_item_with_info(
|
2019-10-01 12:40:56 +02:00
|
|
|
&self,
|
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
lo: Span,
|
|
|
|
vis: Visibility,
|
2019-10-01 13:48:54 +02:00
|
|
|
info: ItemInfo,
|
2019-10-01 12:40:56 +02:00
|
|
|
) -> PResult<'a, Option<P<Item>>> {
|
2019-10-01 13:48:54 +02:00
|
|
|
let (ident, item, extra_attrs) = info;
|
2019-10-01 12:40:56 +02:00
|
|
|
let span = lo.to(self.prev_span);
|
2019-10-22 12:25:14 +08:00
|
|
|
let attrs = Self::maybe_append(attrs, extra_attrs);
|
2019-10-01 12:40:56 +02:00
|
|
|
Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-22 12:25:14 +08:00
|
|
|
fn maybe_append<T>(mut lhs: Vec<T>, mut rhs: Option<Vec<T>>) -> Vec<T> {
|
|
|
|
if let Some(ref mut rhs) = rhs {
|
|
|
|
lhs.append(rhs);
|
|
|
|
}
|
|
|
|
lhs
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
/// This is the fall-through for parsing items.
|
|
|
|
fn parse_macro_use_or_failure(
|
|
|
|
&mut self,
|
2019-12-22 17:42:04 -05:00
|
|
|
attrs: Vec<Attribute>,
|
2019-08-11 18:34:42 +02:00
|
|
|
macros_allowed: bool,
|
|
|
|
attributes_allowed: bool,
|
|
|
|
lo: Span,
|
2019-12-22 17:42:04 -05:00
|
|
|
visibility: Visibility,
|
2019-08-11 18:34:42 +02:00
|
|
|
) -> PResult<'a, Option<P<Item>>> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if macros_allowed
|
|
|
|
&& self.token.is_path_start()
|
|
|
|
&& !(self.is_async_fn() && self.token.span.rust_2015())
|
|
|
|
{
|
2019-08-11 18:34:42 +02:00
|
|
|
// MACRO INVOCATION ITEM
|
|
|
|
|
|
|
|
let prev_span = self.prev_span;
|
|
|
|
self.complain_if_pub_macro(&visibility.node, prev_span);
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
// Item macro
|
2019-08-11 18:34:42 +02:00
|
|
|
let path = self.parse_path(PathStyle::Mod)?;
|
|
|
|
self.expect(&token::Not)?;
|
2019-12-01 02:25:32 +03:00
|
|
|
let args = self.parse_mac_args()?;
|
|
|
|
if args.need_semicolon() && !self.eat(&token::Semi) {
|
2019-08-11 18:34:42 +02:00
|
|
|
self.report_invalid_macro_expansion_item();
|
|
|
|
}
|
|
|
|
|
|
|
|
let hi = self.prev_span;
|
2019-12-22 17:42:04 -05:00
|
|
|
let mac = Mac { path, args, prior_type_ascription: self.last_type_ascription };
|
2019-08-11 18:34:42 +02:00
|
|
|
let item =
|
|
|
|
self.mk_item(lo.to(hi), Ident::invalid(), ItemKind::Mac(mac), visibility, attrs);
|
|
|
|
return Ok(Some(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FAILURE TO PARSE ITEM
|
|
|
|
match visibility.node {
|
|
|
|
VisibilityKind::Inherited => {}
|
2019-12-31 00:20:41 +01:00
|
|
|
_ => return Err(self.struct_span_err(self.prev_span, "unmatched visibility `pub`")),
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !attributes_allowed && !attrs.is_empty() {
|
|
|
|
self.expected_item_err(&attrs)?;
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits an expected-item-after-attributes error.
|
2019-12-22 17:42:04 -05:00
|
|
|
fn expected_item_err(&mut self, attrs: &[Attribute]) -> PResult<'a, ()> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let message = match attrs.last() {
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(&Attribute { kind: AttrKind::DocComment(_), .. }) => {
|
|
|
|
"expected item after doc comment"
|
|
|
|
}
|
|
|
|
_ => "expected item after attributes",
|
2019-08-11 18:34:42 +02:00
|
|
|
};
|
|
|
|
|
2019-12-30 14:56:57 +01:00
|
|
|
let mut err = self.struct_span_err(self.prev_span, message);
|
2019-10-24 06:33:12 +11:00
|
|
|
if attrs.last().unwrap().is_doc_comment() {
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_label(self.prev_span, "this doc comment doesn't document anything");
|
|
|
|
}
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
|
2019-08-11 20:32:29 +02:00
|
|
|
pub(super) fn is_async_fn(&self) -> bool {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.token.is_keyword(kw::Async) && self.is_keyword_ahead(1, &[kw::Fn])
|
2019-08-11 20:32:29 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
/// Parses a macro invocation inside a `trait`, `impl` or `extern` block.
|
2019-11-25 12:37:07 -08:00
|
|
|
fn parse_assoc_macro_invoc(
|
|
|
|
&mut self,
|
|
|
|
item_kind: &str,
|
|
|
|
vis: Option<&Visibility>,
|
|
|
|
at_end: &mut bool,
|
|
|
|
) -> PResult<'a, Option<Mac>> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.token.is_path_start() && !(self.is_async_fn() && self.token.span.rust_2015()) {
|
2019-08-11 18:34:42 +02:00
|
|
|
let prev_span = self.prev_span;
|
|
|
|
let path = self.parse_path(PathStyle::Mod)?;
|
|
|
|
|
|
|
|
if path.segments.len() == 1 {
|
|
|
|
if !self.eat(&token::Not) {
|
|
|
|
return Err(self.missing_assoc_item_kind_err(item_kind, prev_span));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.expect(&token::Not)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(vis) = vis {
|
|
|
|
self.complain_if_pub_macro(&vis.node, prev_span);
|
|
|
|
}
|
|
|
|
|
|
|
|
*at_end = true;
|
|
|
|
|
|
|
|
// eat a matched-delimiter token tree:
|
2019-12-01 02:25:32 +03:00
|
|
|
let args = self.parse_mac_args()?;
|
|
|
|
if args.need_semicolon() {
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(Some(Mac { path, args, prior_type_ascription: self.last_type_ascription }))
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 12:37:07 -08:00
|
|
|
fn missing_assoc_item_kind_err(
|
|
|
|
&self,
|
|
|
|
item_type: &str,
|
|
|
|
prev_span: Span,
|
|
|
|
) -> DiagnosticBuilder<'a> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let expected_kinds = if item_type == "extern" {
|
|
|
|
"missing `fn`, `type`, or `static`"
|
|
|
|
} else {
|
|
|
|
"missing `fn`, `type`, or `const`"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Given this code `path(`, it seems like this is not
|
|
|
|
// setting the visibility of a macro invocation, but rather
|
|
|
|
// a mistyped method declaration.
|
|
|
|
// Create a diagnostic pointing out that `fn` is missing.
|
|
|
|
//
|
|
|
|
// x | pub path(&self) {
|
|
|
|
// | ^ missing `fn`, `type`, or `const`
|
|
|
|
// pub path(
|
|
|
|
// ^^ `sp` below will point to this
|
|
|
|
let sp = prev_span.between(self.prev_span);
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut err = self
|
|
|
|
.struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_label(sp, expected_kinds);
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an implementation item, `impl` keyword is already parsed.
|
|
|
|
///
|
|
|
|
/// impl<'a, T> TYPE { /* impl items */ }
|
|
|
|
/// impl<'a, T> TRAIT for TYPE { /* impl items */ }
|
|
|
|
/// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
|
2020-01-02 15:49:45 -08:00
|
|
|
/// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
|
2019-08-11 18:34:42 +02:00
|
|
|
///
|
|
|
|
/// We actually parse slightly more relaxed grammar for better error reporting and recovery.
|
2020-01-02 15:49:45 -08:00
|
|
|
/// `impl` GENERICS `const`? `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
|
|
|
|
/// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
|
2019-12-22 17:42:04 -05:00
|
|
|
fn parse_item_impl(
|
|
|
|
&mut self,
|
2020-01-30 02:42:33 +01:00
|
|
|
unsafety: Unsafe,
|
2019-12-22 17:42:04 -05:00
|
|
|
defaultness: Defaultness,
|
|
|
|
) -> PResult<'a, ItemInfo> {
|
2019-08-11 18:34:42 +02:00
|
|
|
// First, parse generic parameters if necessary.
|
|
|
|
let mut generics = if self.choose_generics_over_qpath() {
|
|
|
|
self.parse_generics()?
|
|
|
|
} else {
|
2020-01-09 13:46:37 -08:00
|
|
|
let mut generics = Generics::default();
|
|
|
|
// impl A for B {}
|
|
|
|
// /\ this is where `generics.span` should point when there are no type params.
|
|
|
|
generics.span = self.prev_span.shrink_to_hi();
|
|
|
|
generics
|
2019-08-11 18:34:42 +02:00
|
|
|
};
|
|
|
|
|
2020-01-30 02:42:33 +01:00
|
|
|
let constness = self.parse_constness();
|
|
|
|
if let Const::Yes(span) = constness {
|
2020-01-03 16:32:01 -08:00
|
|
|
self.sess.gated_spans.gate(sym::const_trait_impl, span);
|
2020-01-30 02:42:33 +01:00
|
|
|
}
|
2020-01-02 15:49:45 -08:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
// 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()) {
|
|
|
|
self.bump(); // `!`
|
|
|
|
ast::ImplPolarity::Negative
|
|
|
|
} else {
|
|
|
|
ast::ImplPolarity::Positive
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse both types and traits as a type, then reinterpret if necessary.
|
|
|
|
let err_path = |span| ast::Path::from_ident(Ident::new(kw::Invalid, span));
|
2019-12-22 17:42:04 -05:00
|
|
|
let ty_first = if self.token.is_keyword(kw::For) && self.look_ahead(1, |t| t != &token::Lt)
|
|
|
|
{
|
2019-08-11 18:34:42 +02:00
|
|
|
let span = self.prev_span.between(self.token.span);
|
|
|
|
self.struct_span_err(span, "missing trait in a trait impl").emit();
|
2019-09-26 17:25:31 +01:00
|
|
|
P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
|
|
|
self.parse_ty()?
|
|
|
|
};
|
|
|
|
|
|
|
|
// If `for` is missing we try to recover.
|
|
|
|
let has_for = self.eat_keyword(kw::For);
|
|
|
|
let missing_for_span = self.prev_span.between(self.token.span);
|
|
|
|
|
|
|
|
let ty_second = if self.token == token::DotDot {
|
|
|
|
// We need to report this error after `cfg` expansion for compatibility reasons
|
|
|
|
self.bump(); // `..`, do not add it to expected tokens
|
2019-10-08 14:39:58 +02:00
|
|
|
Some(self.mk_ty(self.prev_span, TyKind::Err))
|
2019-08-11 18:34:42 +02:00
|
|
|
} else if has_for || self.token.can_begin_type() {
|
|
|
|
Some(self.parse_ty()?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
2020-01-31 06:43:33 +01:00
|
|
|
let (impl_items, attrs) = self.parse_item_list(|p, at_end| p.parse_impl_item(at_end))?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
let item_kind = match ty_second {
|
|
|
|
Some(ty_second) => {
|
|
|
|
// impl Trait for Type
|
|
|
|
if !has_for {
|
|
|
|
self.struct_span_err(missing_for_span, "missing `for` in a trait impl")
|
|
|
|
.span_suggestion_short(
|
|
|
|
missing_for_span,
|
|
|
|
"add `for` here",
|
|
|
|
" for ".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.emit();
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let ty_first = ty_first.into_inner();
|
2019-09-26 17:25:31 +01:00
|
|
|
let path = match ty_first.kind {
|
2019-08-11 18:34:42 +02:00
|
|
|
// This notably includes paths passed through `ty` macro fragments (#46438).
|
|
|
|
TyKind::Path(None, path) => path,
|
|
|
|
_ => {
|
2019-12-30 15:09:42 +01:00
|
|
|
self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
|
2019-08-11 18:34:42 +02:00
|
|
|
err_path(ty_first.span)
|
|
|
|
}
|
|
|
|
};
|
2020-01-13 20:30:25 -08:00
|
|
|
let trait_ref = TraitRef { path, ref_id: ty_first.id };
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2020-01-13 20:30:20 -08:00
|
|
|
ItemKind::Impl {
|
2019-12-22 17:42:04 -05:00
|
|
|
unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness,
|
2020-01-13 20:30:23 -08:00
|
|
|
constness,
|
2019-12-22 17:42:04 -05:00
|
|
|
generics,
|
2020-01-13 20:30:20 -08:00
|
|
|
of_trait: Some(trait_ref),
|
|
|
|
self_ty: ty_second,
|
|
|
|
items: impl_items,
|
|
|
|
}
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// impl Type
|
2020-01-13 20:30:20 -08:00
|
|
|
ItemKind::Impl {
|
2019-12-22 17:42:04 -05:00
|
|
|
unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness,
|
2020-01-13 20:30:23 -08:00
|
|
|
constness,
|
2019-12-22 17:42:04 -05:00
|
|
|
generics,
|
2020-01-13 20:30:20 -08:00
|
|
|
of_trait: None,
|
|
|
|
self_ty: ty_first,
|
|
|
|
items: impl_items,
|
|
|
|
}
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((Ident::invalid(), item_kind, Some(attrs)))
|
|
|
|
}
|
|
|
|
|
2020-01-31 06:43:33 +01:00
|
|
|
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>)> {
|
2019-08-11 18:34:42 +02:00
|
|
|
self.expect(&token::OpenDelim(token::Brace))?;
|
|
|
|
let attrs = self.parse_inner_attributes()?;
|
|
|
|
|
2020-01-31 06:43:33 +01:00
|
|
|
let mut items = Vec::new();
|
2019-08-11 18:34:42 +02:00
|
|
|
while !self.eat(&token::CloseDelim(token::Brace)) {
|
2020-01-31 06:43:33 +01:00
|
|
|
if self.recover_doc_comment_before_brace() {
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-11 18:34:42 +02:00
|
|
|
let mut at_end = false;
|
2020-01-31 06:43:33 +01:00
|
|
|
match parse_item(self, &mut at_end) {
|
|
|
|
Ok(item) => items.push(item),
|
2019-08-11 18:34:42 +02:00
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
if !at_end {
|
2019-10-25 18:30:02 -07:00
|
|
|
self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
|
|
|
|
break;
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 06:43:33 +01:00
|
|
|
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
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses defaultness (i.e., `default` or nothing).
|
|
|
|
fn parse_defaultness(&mut self) -> Defaultness {
|
|
|
|
// `pub` is included for better error messages
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.check_keyword(kw::Default)
|
|
|
|
&& self.is_keyword_ahead(
|
|
|
|
1,
|
|
|
|
&[
|
|
|
|
kw::Impl,
|
|
|
|
kw::Const,
|
|
|
|
kw::Async,
|
|
|
|
kw::Fn,
|
|
|
|
kw::Unsafe,
|
|
|
|
kw::Extern,
|
|
|
|
kw::Type,
|
|
|
|
kw::Pub,
|
|
|
|
],
|
|
|
|
)
|
2019-08-11 18:34:42 +02:00
|
|
|
{
|
|
|
|
self.bump(); // `default`
|
|
|
|
Defaultness::Default
|
|
|
|
} else {
|
|
|
|
Defaultness::Final
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-01 14:19:08 +02:00
|
|
|
/// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
|
2020-01-30 02:42:33 +01:00
|
|
|
fn parse_item_trait(&mut self, lo: Span, unsafety: Unsafe) -> PResult<'a, ItemInfo> {
|
2019-10-01 14:19:08 +02:00
|
|
|
// Parse optional `auto` prefix.
|
2019-12-22 17:42:04 -05:00
|
|
|
let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
|
2019-10-01 14:19:08 +02:00
|
|
|
|
|
|
|
self.expect_keyword(kw::Trait)?;
|
2019-08-11 18:34:42 +02:00
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
let mut tps = self.parse_generics()?;
|
|
|
|
|
|
|
|
// Parse optional colon and supertrait bounds.
|
2019-09-21 17:18:08 +02:00
|
|
|
let had_colon = self.eat(&token::Colon);
|
|
|
|
let span_at_colon = self.prev_span;
|
2019-12-22 17:42:04 -05:00
|
|
|
let bounds =
|
|
|
|
if had_colon { self.parse_generic_bounds(Some(self.prev_span))? } else { Vec::new() };
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-09-21 17:18:08 +02:00
|
|
|
let span_before_eq = self.prev_span;
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.eat(&token::Eq) {
|
2019-09-06 03:56:45 +01:00
|
|
|
// It's a trait alias.
|
2019-09-21 17:18:08 +02:00
|
|
|
if had_colon {
|
|
|
|
let span = span_at_colon.to(span_before_eq);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.struct_span_err(span, "bounds are not allowed on trait aliases").emit();
|
2019-09-21 17:18:08 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
let bounds = self.parse_generic_bounds(None)?;
|
|
|
|
tps.where_clause = self.parse_where_clause()?;
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-09-21 17:18:08 +02:00
|
|
|
|
|
|
|
let whole_span = lo.to(self.prev_span);
|
2020-02-10 15:24:53 +01:00
|
|
|
if is_auto == IsAuto::Yes {
|
2019-08-11 18:34:42 +02:00
|
|
|
let msg = "trait aliases cannot be `auto`";
|
2019-12-22 17:42:04 -05:00
|
|
|
self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2020-01-30 02:42:33 +01:00
|
|
|
if let Unsafe::Yes(_) = unsafety {
|
2019-08-11 18:34:42 +02:00
|
|
|
let msg = "trait aliases cannot be `unsafe`";
|
2019-12-22 17:42:04 -05:00
|
|
|
self.struct_span_err(whole_span, msg).span_label(whole_span, msg).emit();
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2019-09-21 17:18:08 +02:00
|
|
|
|
2019-10-30 16:38:16 +01:00
|
|
|
self.sess.gated_spans.gate(sym::trait_alias, whole_span);
|
2019-09-21 17:40:50 +02:00
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
Ok((ident, ItemKind::TraitAlias(tps, bounds), None))
|
|
|
|
} else {
|
2019-09-06 03:56:45 +01:00
|
|
|
// It's a normal trait.
|
2019-08-11 18:34:42 +02:00
|
|
|
tps.where_clause = self.parse_where_clause()?;
|
2020-01-31 06:43:33 +01:00
|
|
|
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)))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 16:41:18 +11:00
|
|
|
pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
|
2019-12-01 17:29:13 +01:00
|
|
|
maybe_whole!(self, NtImplItem, |x| x);
|
|
|
|
self.parse_assoc_item(at_end, |_| true)
|
|
|
|
}
|
|
|
|
|
2019-12-12 16:41:18 +11:00
|
|
|
pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, P<AssocItem>> {
|
2019-08-11 18:34:42 +02:00
|
|
|
maybe_whole!(self, NtTraitItem, |x| x);
|
2019-12-01 17:29:13 +01:00
|
|
|
// This is somewhat dubious; We don't want to allow
|
|
|
|
// param names to be left off if there is a definition...
|
|
|
|
//
|
|
|
|
// We don't allow param names to be left off in edition 2018.
|
|
|
|
self.parse_assoc_item(at_end, |t| t.span.rust_2018())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses associated items.
|
|
|
|
fn parse_assoc_item(
|
|
|
|
&mut self,
|
|
|
|
at_end: &mut bool,
|
2020-02-10 15:35:05 +01:00
|
|
|
req_name: fn(&token::Token) -> bool,
|
2019-12-12 16:41:18 +11:00
|
|
|
) -> PResult<'a, P<AssocItem>> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let attrs = self.parse_outer_attributes()?;
|
|
|
|
let mut unclosed_delims = vec![];
|
|
|
|
let (mut item, tokens) = self.collect_tokens(|this| {
|
2020-02-10 15:35:05 +01:00
|
|
|
let item = this.parse_assoc_item_(at_end, attrs, req_name);
|
2019-08-11 18:34:42 +02:00
|
|
|
unclosed_delims.append(&mut this.unclosed_delims);
|
|
|
|
item
|
|
|
|
})?;
|
|
|
|
self.unclosed_delims.append(&mut unclosed_delims);
|
|
|
|
// See `parse_item` for why this clause is here.
|
|
|
|
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
|
|
|
|
item.tokens = Some(tokens);
|
|
|
|
}
|
2019-12-12 16:41:18 +11:00
|
|
|
Ok(P(item))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:29:13 +01:00
|
|
|
fn parse_assoc_item_(
|
2019-10-01 12:11:38 +02:00
|
|
|
&mut self,
|
|
|
|
at_end: &mut bool,
|
|
|
|
mut attrs: Vec<Attribute>,
|
2020-02-10 15:35:05 +01:00
|
|
|
req_name: fn(&token::Token) -> bool,
|
2019-12-01 11:30:04 +01:00
|
|
|
) -> PResult<'a, AssocItem> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let lo = self.token.span;
|
2019-11-07 11:26:36 +01:00
|
|
|
let vis = self.parse_visibility(FollowedByType::No)?;
|
2019-11-30 18:25:44 +01:00
|
|
|
let defaultness = self.parse_defaultness();
|
2020-01-31 04:21:16 +01:00
|
|
|
|
|
|
|
let (ident, kind, generics) = if self.eat_keyword(kw::Type) {
|
2019-12-01 10:55:41 +01:00
|
|
|
self.parse_assoc_ty()?
|
2020-02-11 08:40:16 +01:00
|
|
|
} else if self.check_fn_front_matter() {
|
2020-02-10 15:35:05 +01:00
|
|
|
let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, req_name)?;
|
2020-01-31 03:31:12 +01:00
|
|
|
(ident, AssocItemKind::Fn(sig, body), generics)
|
2019-12-01 11:30:04 +01:00
|
|
|
} else if let Some(mac) = self.parse_assoc_macro_invoc("associated", Some(&vis), at_end)? {
|
|
|
|
(Ident::invalid(), AssocItemKind::Macro(mac), Generics::default())
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
2020-01-30 08:31:31 +01:00
|
|
|
self.parse_assoc_const()?
|
2019-08-11 18:34:42 +02:00
|
|
|
};
|
|
|
|
|
2020-01-31 04:21:16 +01:00
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
let id = DUMMY_NODE_ID;
|
|
|
|
Ok(AssocItem { id, span, ident, attrs, vis, defaultness, generics, kind, tokens: None })
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-01 10:53:20 +01:00
|
|
|
/// This parses the grammar:
|
|
|
|
///
|
|
|
|
/// AssocConst = "const" Ident ":" Ty "=" Expr ";"
|
|
|
|
fn parse_assoc_const(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
|
2019-10-01 12:11:38 +02:00
|
|
|
self.expect_keyword(kw::Const)?;
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
let ty = self.parse_ty()?;
|
2019-12-22 17:42:04 -05:00
|
|
|
let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-12-01 10:53:20 +01:00
|
|
|
Ok((ident, AssocItemKind::Const(ty, expr), Generics::default()))
|
2019-10-01 12:11:38 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
/// Parses the following grammar:
|
|
|
|
///
|
2019-12-01 10:25:45 +01:00
|
|
|
/// AssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
|
2019-12-01 10:55:41 +01:00
|
|
|
fn parse_assoc_ty(&mut self) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
let mut generics = self.parse_generics()?;
|
|
|
|
|
|
|
|
// Parse optional colon and param bounds.
|
2019-12-22 17:42:04 -05:00
|
|
|
let bounds =
|
|
|
|
if self.eat(&token::Colon) { self.parse_generic_bounds(None)? } else { Vec::new() };
|
2019-08-11 18:34:42 +02:00
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-12-01 10:55:41 +01:00
|
|
|
Ok((ident, AssocItemKind::TyAlias(bounds, default), generics))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a `UseTree`.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// USE_TREE = [`::`] `*` |
|
|
|
|
/// [`::`] `{` USE_TREE_LIST `}` |
|
|
|
|
/// PATH `::` `*` |
|
|
|
|
/// PATH `::` `{` USE_TREE_LIST `}` |
|
|
|
|
/// PATH [`as` IDENT]
|
|
|
|
/// ```
|
|
|
|
fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
|
|
|
|
let lo = self.token.span;
|
|
|
|
|
|
|
|
let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
|
2019-12-22 17:42:04 -05:00
|
|
|
let kind = if self.check(&token::OpenDelim(token::Brace))
|
|
|
|
|| self.check(&token::BinOp(token::Star))
|
|
|
|
|| self.is_import_coupler()
|
|
|
|
{
|
2019-08-11 18:34:42 +02:00
|
|
|
// `use *;` or `use ::*;` or `use {...};` or `use ::{...};`
|
|
|
|
let mod_sep_ctxt = self.token.span.ctxt();
|
|
|
|
if self.eat(&token::ModSep) {
|
2019-12-22 17:42:04 -05:00
|
|
|
prefix
|
|
|
|
.segments
|
|
|
|
.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 06:31:02 +02:00
|
|
|
self.parse_use_tree_glob_or_nested()?
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
|
|
|
// `use path::*;` or `use path::{...};` or `use path;` or `use path as bar;`
|
|
|
|
prefix = self.parse_path(PathStyle::Mod)?;
|
|
|
|
|
|
|
|
if self.eat(&token::ModSep) {
|
2019-10-07 06:31:02 +02:00
|
|
|
self.parse_use_tree_glob_or_nested()?
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
2019-09-06 03:56:45 +01:00
|
|
|
UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
|
|
|
|
}
|
|
|
|
|
2019-10-07 06:31:02 +02:00
|
|
|
/// Parses `*` or `{...}`.
|
|
|
|
fn parse_use_tree_glob_or_nested(&mut self) -> PResult<'a, UseTreeKind> {
|
|
|
|
Ok(if self.eat(&token::BinOp(token::Star)) {
|
|
|
|
UseTreeKind::Glob
|
|
|
|
} else {
|
|
|
|
UseTreeKind::Nested(self.parse_use_tree_list()?)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
/// Parses a `UseTreeKind::Nested(list)`.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// USE_TREE_LIST = Ø | (USE_TREE `,`)* USE_TREE [`,`]
|
|
|
|
/// ```
|
|
|
|
fn parse_use_tree_list(&mut self) -> PResult<'a, Vec<(UseTree, ast::NodeId)>> {
|
2019-09-06 03:56:45 +01:00
|
|
|
self.parse_delim_comma_seq(token::Brace, |p| Ok((p.parse_use_tree()?, DUMMY_NODE_ID)))
|
2019-08-11 18:34:42 +02:00
|
|
|
.map(|(r, _)| r)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_rename(&mut self) -> PResult<'a, Option<Ident>> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.eat_keyword(kw::As) { self.parse_ident_or_underscore().map(Some) } else { Ok(None) }
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 20:00:38 +02:00
|
|
|
fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
|
|
|
|
match self.token.kind {
|
|
|
|
token::Ident(name, false) if name == kw::Underscore => {
|
|
|
|
let span = self.token.span;
|
|
|
|
self.bump();
|
|
|
|
Ok(Ident::new(name, span))
|
|
|
|
}
|
|
|
|
_ => self.parse_ident(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
/// Parses `extern crate` links.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// extern crate foo;
|
|
|
|
/// extern crate bar as foo;
|
|
|
|
/// ```
|
|
|
|
fn parse_item_extern_crate(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
visibility: Visibility,
|
2019-12-22 17:42:04 -05:00
|
|
|
attrs: Vec<Attribute>,
|
2019-08-11 18:34:42 +02:00
|
|
|
) -> PResult<'a, P<Item>> {
|
|
|
|
// 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()? {
|
|
|
|
(rename, Some(orig_name.name))
|
|
|
|
} else {
|
|
|
|
(orig_name, None)
|
|
|
|
};
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
Ok(self.mk_item(span, item_name, ItemKind::ExternCrate(orig_name), visibility, attrs))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_crate_name_with_dashes(&mut self) -> PResult<'a, ast::Ident> {
|
|
|
|
let error_msg = "crate name using dashes are not valid in `extern crate` statements";
|
|
|
|
let suggestion_msg = "if the original crate name uses dashes you need to use underscores \
|
|
|
|
in the code";
|
|
|
|
let mut ident = if self.token.is_keyword(kw::SelfLower) {
|
|
|
|
self.parse_path_segment_ident()
|
|
|
|
} else {
|
|
|
|
self.parse_ident()
|
|
|
|
}?;
|
|
|
|
let mut idents = vec![];
|
|
|
|
let mut replacement = vec![];
|
|
|
|
let mut fixed_crate_name = false;
|
2019-09-06 03:56:45 +01:00
|
|
|
// Accept `extern crate name-like-this` for better diagnostics.
|
2019-08-11 18:34:42 +02:00
|
|
|
let dash = token::BinOp(token::BinOpToken::Minus);
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.token == dash {
|
|
|
|
// Do not include `-` as part of the expected tokens list.
|
2019-08-11 18:34:42 +02:00
|
|
|
while self.eat(&dash) {
|
|
|
|
fixed_crate_name = true;
|
|
|
|
replacement.push((self.prev_span, "_".to_string()));
|
|
|
|
idents.push(self.parse_ident()?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fixed_crate_name {
|
|
|
|
let fixed_name_sp = ident.span.to(idents.last().unwrap().span);
|
|
|
|
let mut fixed_name = format!("{}", ident.name);
|
|
|
|
for part in idents {
|
|
|
|
fixed_name.push_str(&format!("_{}", part.name));
|
|
|
|
}
|
2019-09-14 21:16:51 +01:00
|
|
|
ident = Ident::from_str_and_span(&fixed_name, fixed_name_sp);
|
2019-08-11 18:34:42 +02:00
|
|
|
|
|
|
|
self.struct_span_err(fixed_name_sp, error_msg)
|
|
|
|
.span_label(fixed_name_sp, "dash-separated idents are not valid")
|
|
|
|
.multipart_suggestion(suggestion_msg, replacement, Applicability::MachineApplicable)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
Ok(ident)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses `extern` for foreign ABIs modules.
|
|
|
|
///
|
|
|
|
/// `extern` is expected to have been
|
|
|
|
/// consumed before calling this method.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```ignore (only-for-syntax-highlight)
|
|
|
|
/// extern "C" {}
|
|
|
|
/// extern {}
|
|
|
|
/// ```
|
|
|
|
fn parse_item_foreign_mod(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
2019-11-10 00:44:59 +03:00
|
|
|
abi: Option<StrLit>,
|
2020-01-31 06:43:33 +01:00
|
|
|
vis: Visibility,
|
2019-08-11 18:34:42 +02:00
|
|
|
mut attrs: Vec<Attribute>,
|
|
|
|
) -> PResult<'a, P<Item>> {
|
2020-01-31 06:43:33 +01:00
|
|
|
let (items, iattrs) = self.parse_item_list(|p, at_end| p.parse_foreign_item(at_end))?;
|
|
|
|
attrs.extend(iattrs);
|
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
let m = ast::ForeignMod { abi, items };
|
|
|
|
Ok(self.mk_item(span, Ident::invalid(), ItemKind::ForeignMod(m), vis, attrs))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
2020-01-31 04:21:16 +01:00
|
|
|
/// Parses a foreign item (one in an `extern { ... }` block).
|
2020-01-31 06:43:33 +01:00
|
|
|
pub fn parse_foreign_item(&mut self, at_end: &mut bool) -> PResult<'a, P<ForeignItem>> {
|
2019-08-11 18:34:42 +02:00
|
|
|
maybe_whole!(self, NtForeignItem, |ni| ni);
|
|
|
|
|
2020-01-31 03:31:12 +01:00
|
|
|
let mut attrs = self.parse_outer_attributes()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
let lo = self.token.span;
|
2020-01-30 08:31:31 +01:00
|
|
|
let vis = self.parse_visibility(FollowedByType::No)?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2020-01-31 04:21:16 +01:00
|
|
|
let (ident, kind) = if self.check_keyword(kw::Type) {
|
2020-01-30 08:31:31 +01:00
|
|
|
// FOREIGN TYPE ITEM
|
2020-01-31 04:21:16 +01:00
|
|
|
self.parse_item_foreign_type()?
|
2020-02-11 08:40:16 +01:00
|
|
|
} else if self.check_fn_front_matter() {
|
2020-01-30 08:31:31 +01:00
|
|
|
// FOREIGN FUNCTION ITEM
|
2020-01-31 06:43:33 +01:00
|
|
|
let (ident, sig, generics, body) = self.parse_fn(at_end, &mut attrs, |_| true)?;
|
2020-01-31 04:21:16 +01:00
|
|
|
(ident, ForeignItemKind::Fn(sig, generics, body))
|
2020-01-30 08:31:31 +01:00
|
|
|
} else if self.is_static_global() {
|
|
|
|
// FOREIGN STATIC ITEM
|
2020-01-30 00:18:54 +01:00
|
|
|
self.bump(); // `static`
|
2020-01-31 04:21:16 +01:00
|
|
|
self.parse_item_foreign_static()?
|
2020-01-30 08:31:31 +01:00
|
|
|
} else if self.token.is_keyword(kw::Const) {
|
|
|
|
// Treat `const` as `static` for error recovery, but don't add it to expected tokens.
|
2020-01-30 00:18:54 +01:00
|
|
|
self.bump(); // `const`
|
|
|
|
self.struct_span_err(self.prev_span, "extern items cannot be `const`")
|
|
|
|
.span_suggestion(
|
|
|
|
self.prev_span,
|
2019-12-22 17:42:04 -05:00
|
|
|
"try using a static value",
|
|
|
|
"static".to_owned(),
|
|
|
|
Applicability::MachineApplicable,
|
2020-01-30 00:18:54 +01:00
|
|
|
)
|
|
|
|
.emit();
|
2020-01-31 04:21:16 +01:00
|
|
|
self.parse_item_foreign_static()?
|
2020-01-31 06:43:33 +01:00
|
|
|
} else if let Some(mac) = self.parse_assoc_macro_invoc("extern", Some(&vis), at_end)? {
|
2020-01-31 04:21:16 +01:00
|
|
|
(Ident::invalid(), ForeignItemKind::Macro(mac))
|
2020-01-30 08:31:31 +01:00
|
|
|
} else {
|
|
|
|
if !attrs.is_empty() {
|
|
|
|
self.expected_item_err(&attrs)?;
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
2020-01-31 04:21:16 +01:00
|
|
|
self.unexpected()?
|
|
|
|
};
|
|
|
|
|
|
|
|
let span = lo.to(self.prev_span);
|
|
|
|
Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a static item from a foreign module.
|
|
|
|
/// Assumes that the `static` keyword is already parsed.
|
2020-01-31 04:21:16 +01:00
|
|
|
fn parse_item_foreign_static(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let mutbl = self.parse_mutability();
|
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
let ty = self.parse_ty()?;
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2020-01-31 04:21:16 +01:00
|
|
|
Ok((ident, ForeignItemKind::Static(ty, mutbl)))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a type from a foreign module.
|
2020-01-31 04:21:16 +01:00
|
|
|
fn parse_item_foreign_type(&mut self) -> PResult<'a, (Ident, ForeignItemKind)> {
|
2019-08-11 18:34:42 +02:00
|
|
|
self.expect_keyword(kw::Type)?;
|
|
|
|
let ident = self.parse_ident()?;
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2020-01-31 04:21:16 +01:00
|
|
|
Ok((ident, ForeignItemKind::Ty))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_static_global(&mut self) -> bool {
|
|
|
|
if self.check_keyword(kw::Static) {
|
2019-09-06 03:56:45 +01:00
|
|
|
// Check if this could be a closure.
|
2019-08-11 18:34:42 +02:00
|
|
|
!self.look_ahead(1, |token| {
|
|
|
|
if token.is_keyword(kw::Move) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
match token.kind {
|
|
|
|
token::BinOp(token::Or) | token::OrOr => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:48:22 +02:00
|
|
|
/// Parse `["const" | ("static" "mut"?)] $ident ":" $ty = $expr` with
|
|
|
|
/// `["const" | ("static" "mut"?)]` already parsed and stored in `m`.
|
|
|
|
///
|
|
|
|
/// When `m` is `"const"`, `$ident` may also be `"_"`.
|
2019-08-11 18:34:42 +02:00
|
|
|
fn parse_item_const(&mut self, m: Option<Mutability>) -> PResult<'a, ItemInfo> {
|
|
|
|
let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
|
2019-09-23 04:48:22 +02:00
|
|
|
|
|
|
|
// Parse the type of a `const` or `static mut?` item.
|
|
|
|
// That is, the `":" $ty` fragment.
|
|
|
|
let ty = if self.token == token::Eq {
|
|
|
|
self.recover_missing_const_type(id, m)
|
|
|
|
} else {
|
|
|
|
// Not `=` so expect `":"" $ty` as usual.
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
self.parse_ty()?
|
|
|
|
};
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
self.expect(&token::Eq)?;
|
|
|
|
let e = self.parse_expr()?;
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
let item = match m {
|
|
|
|
Some(m) => ItemKind::Static(ty, m, e),
|
|
|
|
None => ItemKind::Const(ty, e),
|
|
|
|
};
|
|
|
|
Ok((id, item, None))
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:48:22 +02:00
|
|
|
/// We were supposed to parse `:` but instead, we're already at `=`.
|
|
|
|
/// This means that the type is missing.
|
|
|
|
fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> {
|
|
|
|
// Construct the error and stash it away with the hope
|
|
|
|
// that typeck will later enrich the error with a type.
|
|
|
|
let kind = match m {
|
2019-12-16 17:28:40 +01:00
|
|
|
Some(Mutability::Mut) => "static mut",
|
|
|
|
Some(Mutability::Not) => "static",
|
2019-09-23 04:48:22 +02:00
|
|
|
None => "const",
|
|
|
|
};
|
|
|
|
let mut err = self.struct_span_err(id.span, &format!("missing type for `{}` item", kind));
|
|
|
|
err.span_suggestion(
|
|
|
|
id.span,
|
|
|
|
"provide a type for the item",
|
|
|
|
format!("{}: <type>", id),
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
);
|
|
|
|
err.stash(id.span, StashKey::ItemNoType);
|
|
|
|
|
|
|
|
// The user intended that the type be inferred,
|
|
|
|
// so treat this as if the user wrote e.g. `const A: _ = expr;`.
|
2019-12-22 17:42:04 -05:00
|
|
|
P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
|
2019-09-23 04:48:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-07 18:19:41 +01:00
|
|
|
/// Parses the grammar:
|
|
|
|
/// Ident ["<"...">"] ["where" ...] ("=" | ":") Ty ";"
|
|
|
|
fn parse_type_alias(&mut self) -> PResult<'a, (Ident, P<Ty>, Generics)> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let ident = self.parse_ident()?;
|
|
|
|
let mut tps = self.parse_generics()?;
|
|
|
|
tps.where_clause = self.parse_where_clause()?;
|
|
|
|
self.expect(&token::Eq)?;
|
2019-11-07 18:19:41 +01:00
|
|
|
let ty = self.parse_ty()?;
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-11-07 18:19:41 +01:00
|
|
|
Ok((ident, ty, tps))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an enum declaration.
|
|
|
|
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
|
|
|
|
let id = self.parse_ident()?;
|
|
|
|
let mut generics = self.parse_generics()?;
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let (variants, _) =
|
|
|
|
self.parse_delim_comma_seq(token::Brace, |p| p.parse_enum_variant()).map_err(|e| {
|
|
|
|
self.recover_stmt();
|
|
|
|
e
|
|
|
|
})?;
|
2019-11-24 22:33:00 +01:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let enum_definition =
|
|
|
|
EnumDef { variants: variants.into_iter().filter_map(|v| v).collect() };
|
2019-08-11 18:34:42 +02:00
|
|
|
Ok((id, ItemKind::Enum(enum_definition, generics), None))
|
|
|
|
}
|
|
|
|
|
2019-11-30 14:55:05 +01:00
|
|
|
fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
|
2019-11-24 22:33:00 +01:00
|
|
|
let variant_attrs = self.parse_outer_attributes()?;
|
|
|
|
let vlo = self.token.span;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-11-24 22:33:00 +01:00
|
|
|
let vis = self.parse_visibility(FollowedByType::No)?;
|
|
|
|
if !self.recover_nested_adt_item(kw::Enum)? {
|
2019-12-22 17:42:04 -05:00
|
|
|
return Ok(None);
|
2019-11-24 22:33:00 +01:00
|
|
|
}
|
|
|
|
let ident = self.parse_ident()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-11-24 22:33:00 +01:00
|
|
|
let struct_def = if self.check(&token::OpenDelim(token::Brace)) {
|
|
|
|
// Parse a struct variant.
|
|
|
|
let (fields, recovered) = self.parse_record_struct_body()?;
|
|
|
|
VariantData::Struct(fields, recovered)
|
|
|
|
} else if self.check(&token::OpenDelim(token::Paren)) {
|
2019-12-22 17:42:04 -05:00
|
|
|
VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID)
|
2019-11-24 22:33:00 +01:00
|
|
|
} else {
|
|
|
|
VariantData::Unit(DUMMY_NODE_ID)
|
|
|
|
};
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let disr_expr =
|
|
|
|
if self.eat(&token::Eq) { Some(self.parse_anon_const_expr()?) } else { None };
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-11-24 22:33:00 +01:00
|
|
|
let vr = ast::Variant {
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
id: DUMMY_NODE_ID,
|
|
|
|
attrs: variant_attrs,
|
|
|
|
data: struct_def,
|
|
|
|
disr_expr,
|
|
|
|
span: vlo.to(self.prev_span),
|
|
|
|
is_placeholder: false,
|
|
|
|
};
|
2019-08-11 18:34:42 +02:00
|
|
|
|
2019-11-24 22:33:00 +01:00
|
|
|
Ok(Some(vr))
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses `struct Foo { ... }`.
|
|
|
|
fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
|
|
|
|
let class_name = self.parse_ident()?;
|
|
|
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
|
|
|
|
|
|
|
// There is a special case worth noting here, as reported in issue #17904.
|
|
|
|
// If we are parsing a tuple struct it is the case that the where clause
|
|
|
|
// should follow the field list. Like so:
|
|
|
|
//
|
|
|
|
// struct Foo<T>(T) where T: Copy;
|
|
|
|
//
|
|
|
|
// If we are parsing a normal record-style struct it is the case
|
|
|
|
// that the where clause comes before the body, and after the generics.
|
|
|
|
// So if we look ahead and see a brace or a where-clause we begin
|
|
|
|
// parsing a record style struct.
|
|
|
|
//
|
|
|
|
// Otherwise if we look ahead and see a paren we parse a tuple-style
|
|
|
|
// struct.
|
|
|
|
|
|
|
|
let vdata = if self.token.is_keyword(kw::Where) {
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
if self.eat(&token::Semi) {
|
|
|
|
// If we see a: `struct Foo<T> where T: Copy;` style decl.
|
2019-09-06 03:56:45 +01:00
|
|
|
VariantData::Unit(DUMMY_NODE_ID)
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
|
|
|
// If we see: `struct Foo<T> where T: Copy { ... }`
|
|
|
|
let (fields, recovered) = self.parse_record_struct_body()?;
|
|
|
|
VariantData::Struct(fields, recovered)
|
|
|
|
}
|
|
|
|
// No `where` so: `struct Foo<T>;`
|
|
|
|
} else if self.eat(&token::Semi) {
|
2019-09-06 03:56:45 +01:00
|
|
|
VariantData::Unit(DUMMY_NODE_ID)
|
2019-08-11 18:34:42 +02:00
|
|
|
// Record-style struct definition
|
|
|
|
} else if self.token == token::OpenDelim(token::Brace) {
|
|
|
|
let (fields, recovered) = self.parse_record_struct_body()?;
|
|
|
|
VariantData::Struct(fields, recovered)
|
|
|
|
// Tuple-style struct definition with optional where-clause.
|
|
|
|
} else if self.token == token::OpenDelim(token::Paren) {
|
2019-09-06 03:56:45 +01:00
|
|
|
let body = VariantData::Tuple(self.parse_tuple_struct_body()?, DUMMY_NODE_ID);
|
2019-08-11 18:34:42 +02:00
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
2019-10-20 14:35:46 -07:00
|
|
|
self.expect_semi()?;
|
2019-08-11 18:34:42 +02:00
|
|
|
body
|
|
|
|
} else {
|
2019-12-07 03:07:35 +01:00
|
|
|
let token_str = super::token_descr(&self.token);
|
2019-12-31 01:19:53 +01:00
|
|
|
let msg = &format!(
|
2019-08-11 18:34:42 +02:00
|
|
|
"expected `where`, `{{`, `(`, or `;` after struct name, found {}",
|
|
|
|
token_str
|
2019-12-31 01:19:53 +01:00
|
|
|
);
|
|
|
|
let mut err = self.struct_span_err(self.token.span, msg);
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
|
|
|
|
return Err(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((class_name, ItemKind::Struct(vdata, generics), None))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses `union Foo { ... }`.
|
|
|
|
fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
|
|
|
|
let class_name = self.parse_ident()?;
|
|
|
|
|
|
|
|
let mut generics = self.parse_generics()?;
|
|
|
|
|
|
|
|
let vdata = if self.token.is_keyword(kw::Where) {
|
|
|
|
generics.where_clause = self.parse_where_clause()?;
|
|
|
|
let (fields, recovered) = self.parse_record_struct_body()?;
|
|
|
|
VariantData::Struct(fields, recovered)
|
|
|
|
} else if self.token == token::OpenDelim(token::Brace) {
|
|
|
|
let (fields, recovered) = self.parse_record_struct_body()?;
|
|
|
|
VariantData::Struct(fields, recovered)
|
|
|
|
} else {
|
2019-12-07 03:07:35 +01:00
|
|
|
let token_str = super::token_descr(&self.token);
|
2019-12-31 01:19:53 +01:00
|
|
|
let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
|
|
|
|
let mut err = self.struct_span_err(self.token.span, msg);
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_label(self.token.span, "expected `where` or `{` after union name");
|
|
|
|
return Err(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((class_name, ItemKind::Union(vdata, generics), None))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn is_union_item(&self) -> bool {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.token.is_keyword(kw::Union)
|
|
|
|
&& self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_record_struct_body(
|
|
|
|
&mut self,
|
|
|
|
) -> PResult<'a, (Vec<StructField>, /* recovered */ bool)> {
|
|
|
|
let mut fields = Vec::new();
|
|
|
|
let mut recovered = false;
|
|
|
|
if self.eat(&token::OpenDelim(token::Brace)) {
|
|
|
|
while self.token != token::CloseDelim(token::Brace) {
|
|
|
|
let field = self.parse_struct_decl_field().map_err(|e| {
|
2019-10-25 18:30:02 -07:00
|
|
|
self.consume_block(token::Brace, ConsumeClosingDelim::No);
|
2019-08-11 18:34:42 +02:00
|
|
|
recovered = true;
|
|
|
|
e
|
|
|
|
});
|
|
|
|
match field {
|
|
|
|
Ok(field) => fields.push(field),
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
2019-10-25 18:30:02 -07:00
|
|
|
break;
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.eat(&token::CloseDelim(token::Brace));
|
|
|
|
} else {
|
2019-12-07 03:07:35 +01:00
|
|
|
let token_str = super::token_descr(&self.token);
|
2019-12-31 01:19:53 +01:00
|
|
|
let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
|
|
|
|
let mut err = self.struct_span_err(self.token.span, msg);
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_label(self.token.span, "expected `where`, or `{` after struct name");
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((fields, recovered))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
|
|
|
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
|
|
|
|
// Unit like structs are handled in parse_item_struct function
|
|
|
|
self.parse_paren_comma_seq(|p| {
|
|
|
|
let attrs = p.parse_outer_attributes()?;
|
|
|
|
let lo = p.token.span;
|
2019-11-07 11:26:36 +01:00
|
|
|
let vis = p.parse_visibility(FollowedByType::Yes)?;
|
2019-08-11 18:34:42 +02:00
|
|
|
let ty = p.parse_ty()?;
|
|
|
|
Ok(StructField {
|
|
|
|
span: lo.to(ty.span),
|
|
|
|
vis,
|
|
|
|
ident: None,
|
2019-09-06 03:56:45 +01:00
|
|
|
id: DUMMY_NODE_ID,
|
2019-08-11 18:34:42 +02:00
|
|
|
ty,
|
|
|
|
attrs,
|
2019-09-09 09:26:25 -03:00
|
|
|
is_placeholder: false,
|
2019-08-11 18:34:42 +02:00
|
|
|
})
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
|
|
|
.map(|(r, _)| r)
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses an element of a struct declaration.
|
|
|
|
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
|
|
|
|
let attrs = self.parse_outer_attributes()?;
|
|
|
|
let lo = self.token.span;
|
2019-11-07 11:26:36 +01:00
|
|
|
let vis = self.parse_visibility(FollowedByType::No)?;
|
2019-08-11 18:34:42 +02:00
|
|
|
self.parse_single_struct_field(lo, vis, attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a structure field declaration.
|
2019-12-22 17:42:04 -05:00
|
|
|
fn parse_single_struct_field(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
vis: Visibility,
|
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
) -> PResult<'a, StructField> {
|
2019-08-11 18:34:42 +02:00
|
|
|
let mut seen_comma: bool = false;
|
|
|
|
let a_var = self.parse_name_and_ty(lo, vis, attrs)?;
|
|
|
|
if self.token == token::Comma {
|
|
|
|
seen_comma = true;
|
|
|
|
}
|
|
|
|
match self.token.kind {
|
|
|
|
token::Comma => {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
token::CloseDelim(token::Brace) => {}
|
|
|
|
token::DocComment(_) => {
|
|
|
|
let previous_span = self.prev_span;
|
|
|
|
let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
|
|
|
|
self.bump(); // consume the doc comment
|
|
|
|
let comma_after_doc_seen = self.eat(&token::Comma);
|
|
|
|
// `seen_comma` is always false, because we are inside doc block
|
|
|
|
// condition is here to make code more readable
|
|
|
|
if seen_comma == false && comma_after_doc_seen == true {
|
|
|
|
seen_comma = true;
|
|
|
|
}
|
|
|
|
if comma_after_doc_seen || self.token == token::CloseDelim(token::Brace) {
|
|
|
|
err.emit();
|
|
|
|
} else {
|
|
|
|
if seen_comma == false {
|
|
|
|
let sp = self.sess.source_map().next_point(previous_span);
|
|
|
|
err.span_suggestion(
|
|
|
|
sp,
|
|
|
|
"missing comma here",
|
|
|
|
",".into(),
|
2019-12-22 17:42:04 -05:00
|
|
|
Applicability::MachineApplicable,
|
2019-08-11 18:34:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-01-09 22:10:18 -08:00
|
|
|
let sp = self.prev_span.shrink_to_hi();
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut err = self.struct_span_err(
|
|
|
|
sp,
|
2019-12-07 03:07:35 +01:00
|
|
|
&format!("expected `,`, or `}}`, found {}", super::token_descr(&self.token)),
|
2019-12-22 17:42:04 -05:00
|
|
|
);
|
2019-08-11 18:34:42 +02:00
|
|
|
if self.token.is_ident() {
|
|
|
|
// This is likely another field; emit the diagnostic and keep going
|
|
|
|
err.span_suggestion(
|
|
|
|
sp,
|
|
|
|
"try adding a comma",
|
|
|
|
",".into(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
err.emit();
|
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
return Err(err);
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(a_var)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses a structure field.
|
|
|
|
fn parse_name_and_ty(
|
|
|
|
&mut self,
|
|
|
|
lo: Span,
|
|
|
|
vis: Visibility,
|
2019-12-22 17:42:04 -05:00
|
|
|
attrs: Vec<Attribute>,
|
2019-08-11 18:34:42 +02:00
|
|
|
) -> PResult<'a, StructField> {
|
|
|
|
let name = self.parse_ident()?;
|
|
|
|
self.expect(&token::Colon)?;
|
|
|
|
let ty = self.parse_ty()?;
|
|
|
|
Ok(StructField {
|
|
|
|
span: lo.to(self.prev_span),
|
|
|
|
ident: Some(name),
|
|
|
|
vis,
|
2019-09-06 03:56:45 +01:00
|
|
|
id: DUMMY_NODE_ID,
|
2019-08-11 18:34:42 +02:00
|
|
|
ty,
|
|
|
|
attrs,
|
2019-09-09 09:26:25 -03:00
|
|
|
is_placeholder: false,
|
2019-08-11 18:34:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn eat_macro_def(
|
|
|
|
&mut self,
|
|
|
|
attrs: &[Attribute],
|
|
|
|
vis: &Visibility,
|
2019-12-22 17:42:04 -05:00
|
|
|
lo: Span,
|
2019-08-11 18:34:42 +02:00
|
|
|
) -> PResult<'a, Option<P<Item>>> {
|
|
|
|
let (ident, def) = if self.eat_keyword(kw::Macro) {
|
|
|
|
let ident = self.parse_ident()?;
|
2019-12-01 17:53:59 +03:00
|
|
|
let body = if self.check(&token::OpenDelim(token::Brace)) {
|
|
|
|
self.parse_mac_args()?
|
2019-08-11 18:34:42 +02:00
|
|
|
} else if self.check(&token::OpenDelim(token::Paren)) {
|
2019-12-01 17:53:59 +03:00
|
|
|
let params = self.parse_token_tree();
|
|
|
|
let pspan = params.span();
|
2019-08-11 18:34:42 +02:00
|
|
|
let body = if self.check(&token::OpenDelim(token::Brace)) {
|
|
|
|
self.parse_token_tree()
|
|
|
|
} else {
|
2019-12-01 17:53:59 +03:00
|
|
|
return self.unexpected();
|
2019-08-11 18:34:42 +02:00
|
|
|
};
|
2019-12-01 17:53:59 +03:00
|
|
|
let bspan = body.span();
|
|
|
|
let tokens = TokenStream::new(vec![
|
|
|
|
params.into(),
|
|
|
|
TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
|
2019-08-11 18:34:42 +02:00
|
|
|
body.into(),
|
2019-12-01 17:53:59 +03:00
|
|
|
]);
|
|
|
|
let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
|
|
|
|
P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
2019-12-01 17:53:59 +03:00
|
|
|
return self.unexpected();
|
2019-08-11 18:34:42 +02:00
|
|
|
};
|
|
|
|
|
2019-12-01 17:53:59 +03:00
|
|
|
(ident, ast::MacroDef { body, legacy: false })
|
2019-12-22 17:42:04 -05:00
|
|
|
} else if self.check_keyword(sym::macro_rules)
|
|
|
|
&& self.look_ahead(1, |t| *t == token::Not)
|
|
|
|
&& self.look_ahead(2, |t| t.is_ident())
|
|
|
|
{
|
2019-08-11 18:34:42 +02:00
|
|
|
let prev_span = self.prev_span;
|
|
|
|
self.complain_if_pub_macro(&vis.node, prev_span);
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
|
|
|
|
let ident = self.parse_ident()?;
|
2019-12-01 17:53:59 +03:00
|
|
|
let body = self.parse_mac_args()?;
|
|
|
|
if body.need_semicolon() && !self.eat(&token::Semi) {
|
2019-08-11 18:34:42 +02:00
|
|
|
self.report_invalid_macro_expansion_item();
|
|
|
|
}
|
|
|
|
|
2019-12-01 17:53:59 +03:00
|
|
|
(ident, ast::MacroDef { body, legacy: true })
|
2019-08-11 18:34:42 +02:00
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
let span = lo.to(self.prev_span);
|
2019-09-21 19:18:41 +02:00
|
|
|
|
|
|
|
if !def.legacy {
|
2019-10-30 16:38:16 +01:00
|
|
|
self.sess.gated_spans.gate(sym::decl_macro, span);
|
2019-09-21 19:18:41 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 18:34:42 +02:00
|
|
|
Ok(Some(self.mk_item(span, ident, ItemKind::MacroDef(def), vis.clone(), attrs.to_vec())))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
|
|
|
|
match *vis {
|
|
|
|
VisibilityKind::Inherited => {}
|
|
|
|
_ => {
|
|
|
|
let mut err = if self.token.is_keyword(sym::macro_rules) {
|
2019-12-30 14:56:57 +01:00
|
|
|
let mut err =
|
|
|
|
self.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
|
2019-08-11 18:34:42 +02:00
|
|
|
err.span_suggestion(
|
|
|
|
sp,
|
|
|
|
"try exporting the macro",
|
|
|
|
"#[macro_export]".to_owned(),
|
2019-12-22 17:42:04 -05:00
|
|
|
Applicability::MaybeIncorrect, // speculative
|
2019-08-11 18:34:42 +02:00
|
|
|
);
|
|
|
|
err
|
|
|
|
} else {
|
2019-12-30 14:56:57 +01:00
|
|
|
let mut err =
|
|
|
|
self.struct_span_err(sp, "can't qualify macro invocation with `pub`");
|
2019-08-11 18:34:42 +02:00
|
|
|
err.help("try adjusting the macro to put `pub` inside the invocation");
|
|
|
|
err
|
|
|
|
};
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 04:55:51 +02:00
|
|
|
fn report_invalid_macro_expansion_item(&self) {
|
2019-12-22 17:42:04 -05:00
|
|
|
let has_close_delim = self
|
|
|
|
.sess
|
|
|
|
.source_map()
|
2019-11-10 15:38:09 +08:00
|
|
|
.span_to_snippet(self.prev_span)
|
|
|
|
.map(|s| s.ends_with(")") || s.ends_with("]"))
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
2020-01-29 18:02:58 +09:00
|
|
|
let mut err = self.struct_span_err(
|
2019-10-25 04:55:51 +02:00
|
|
|
self.prev_span,
|
|
|
|
"macros that expand to items must be delimited with braces or followed by a semicolon",
|
2020-01-29 18:02:58 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
// To avoid ICE, we shouldn't emit actual suggestions when it hasn't closing delims
|
|
|
|
if has_close_delim {
|
|
|
|
err.multipart_suggestion(
|
|
|
|
"change the delimiters to curly braces",
|
|
|
|
vec![
|
|
|
|
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), '{'.to_string()),
|
|
|
|
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
|
|
|
|
],
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_suggestion(
|
|
|
|
self.prev_span,
|
|
|
|
"change the delimiters to curly braces",
|
|
|
|
" { /* items */ }".to_string(),
|
2020-01-30 17:33:19 +09:00
|
|
|
Applicability::HasPlaceholders,
|
2020-01-29 18:02:58 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
err.span_suggestion(
|
2020-01-10 11:22:33 -08:00
|
|
|
self.prev_span.shrink_to_hi(),
|
2019-10-25 04:55:51 +02:00
|
|
|
"add a semicolon",
|
|
|
|
';'.to_string(),
|
|
|
|
Applicability::MaybeIncorrect,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.emit();
|
2019-10-25 04:55:51 +02:00
|
|
|
}
|
|
|
|
|
2019-11-23 03:41:12 +01:00
|
|
|
/// Checks if current token is one of tokens which cannot be nested like `kw::Enum`. In case
|
|
|
|
/// it is, we try to parse the item and report error about nested types.
|
|
|
|
fn recover_nested_adt_item(&mut self, keyword: Symbol) -> PResult<'a, bool> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if (self.token.is_keyword(kw::Enum)
|
|
|
|
|| self.token.is_keyword(kw::Struct)
|
|
|
|
|| self.token.is_keyword(kw::Union))
|
|
|
|
&& self.look_ahead(1, |t| t.is_ident())
|
2019-11-23 04:01:14 +01:00
|
|
|
{
|
|
|
|
let kw_token = self.token.clone();
|
|
|
|
let kw_str = pprust::token_to_string(&kw_token);
|
2019-11-23 03:41:12 +01:00
|
|
|
let item = self.parse_item()?;
|
|
|
|
|
2019-11-23 04:01:14 +01:00
|
|
|
self.struct_span_err(
|
|
|
|
kw_token.span,
|
|
|
|
&format!("`{}` definition cannot be nested inside `{}`", kw_str, keyword),
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.span_suggestion(
|
2019-11-23 03:41:12 +01:00
|
|
|
item.unwrap().span,
|
2019-11-23 04:01:14 +01:00
|
|
|
&format!("consider creating a new `{}` definition instead of nesting", kw_str),
|
2019-11-23 03:41:12 +01:00
|
|
|
String::new(),
|
|
|
|
Applicability::MaybeIncorrect,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.emit();
|
2019-11-23 03:41:12 +01:00
|
|
|
// We successfully parsed the item but we must inform the caller about nested problem.
|
2019-12-22 17:42:04 -05:00
|
|
|
return Ok(false);
|
2019-11-23 03:41:12 +01:00
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn mk_item(
|
|
|
|
&self,
|
|
|
|
span: Span,
|
|
|
|
ident: Ident,
|
|
|
|
kind: ItemKind,
|
|
|
|
vis: Visibility,
|
|
|
|
attrs: Vec<Attribute>,
|
|
|
|
) -> P<Item> {
|
|
|
|
P(Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None })
|
2019-08-11 18:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 10:54:26 +02:00
|
|
|
|
|
|
|
/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
|
2020-02-10 15:35:05 +01:00
|
|
|
///
|
|
|
|
/// The function decides if, per-parameter `p`, `p` must have a pattern or just a type.
|
|
|
|
type ReqName = fn(&token::Token) -> bool;
|
2020-01-31 03:20:46 +01:00
|
|
|
|
2019-10-11 10:54:26 +02:00
|
|
|
/// Parsing of functions and methods.
|
|
|
|
impl<'a> Parser<'a> {
|
2020-01-31 03:20:46 +01:00
|
|
|
/// 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>,
|
2020-02-10 15:35:05 +01:00
|
|
|
req_name: ReqName,
|
2020-01-31 03:20:46 +01:00
|
|
|
) -> 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, ...>`
|
2020-02-10 15:35:05 +01:00
|
|
|
let decl = self.parse_fn_decl(req_name, AllowPlus::Yes)?; // `(p: u8, ...)`
|
2020-01-31 03:20:46 +01:00
|
|
|
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))
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 00:18:54 +01:00
|
|
|
/// Parse the "body" of a function.
|
2019-10-11 10:54:26 +02:00
|
|
|
/// This can either be `;` when there's no body,
|
2020-01-30 00:18:54 +01:00
|
|
|
/// or e.g. a block when the function is a provided one.
|
|
|
|
fn parse_fn_body(
|
2019-10-11 10:54:26 +02:00
|
|
|
&mut self,
|
|
|
|
at_end: &mut bool,
|
|
|
|
attrs: &mut Vec<Attribute>,
|
|
|
|
) -> PResult<'a, Option<P<Block>>> {
|
2020-01-30 00:18:54 +01:00
|
|
|
let (inner_attrs, body) = match self.token.kind {
|
2019-10-11 10:54:26 +02:00
|
|
|
token::Semi => {
|
|
|
|
self.bump();
|
2020-01-30 00:18:54 +01:00
|
|
|
(Vec::new(), None)
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
token::OpenDelim(token::Brace) => {
|
2020-01-30 00:18:54 +01:00
|
|
|
let (attrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
(attrs, Some(body))
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
token::Interpolated(ref nt) => match **nt {
|
|
|
|
token::NtBlock(..) => {
|
2020-01-30 00:18:54 +01:00
|
|
|
let (attrs, body) = self.parse_inner_attrs_and_block()?;
|
|
|
|
(attrs, Some(body))
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => return self.expected_semi_or_open_brace(),
|
|
|
|
},
|
2019-10-11 10:54:26 +02:00
|
|
|
_ => return self.expected_semi_or_open_brace(),
|
2020-01-30 00:18:54 +01:00
|
|
|
};
|
|
|
|
attrs.extend(inner_attrs);
|
|
|
|
*at_end = true;
|
|
|
|
Ok(body)
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:02:06 +01:00
|
|
|
/// Is the current token the start of an `FnHeader` / not a valid parse?
|
2020-02-11 08:40:16 +01:00
|
|
|
fn check_fn_front_matter(&mut self) -> bool {
|
2020-01-30 08:31:31 +01:00
|
|
|
// We use an over-approximation here.
|
|
|
|
// `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
|
2020-01-30 13:02:06 +01:00
|
|
|
const QUALS: [Symbol; 4] = [kw::Const, kw::Async, kw::Unsafe, kw::Extern];
|
2020-01-30 08:31:31 +01:00
|
|
|
self.check_keyword(kw::Fn) // Definitely an `fn`.
|
|
|
|
// `$qual fn` or `$qual $qual`:
|
2020-01-30 13:02:06 +01:00
|
|
|
|| 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))
|
2020-01-30 08:31:31 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 02:42:33 +01:00
|
|
|
/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
|
|
|
|
/// up to and including the `fn` keyword. The formal grammar is:
|
2019-10-11 10:54:26 +02:00
|
|
|
///
|
2020-01-30 02:42:33 +01:00
|
|
|
/// ```
|
|
|
|
/// Extern = "extern" StringLit ;
|
|
|
|
/// FnQual = "const"? "async"? "unsafe"? Extern? ;
|
|
|
|
/// FnFrontMatter = FnQual? "fn" ;
|
|
|
|
/// ```
|
2019-10-11 10:54:26 +02:00
|
|
|
fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
|
2020-01-30 02:42:33 +01:00
|
|
|
let constness = self.parse_constness();
|
2019-10-11 10:54:26 +02:00
|
|
|
let asyncness = self.parse_asyncness();
|
2020-01-30 08:31:31 +01:00
|
|
|
let unsafety = self.parse_unsafety();
|
|
|
|
let ext = self.parse_extern()?;
|
|
|
|
|
2020-01-30 05:31:04 +01:00
|
|
|
if let Async::Yes { span, .. } = asyncness {
|
|
|
|
self.ban_async_in_2015(span);
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
2020-01-30 08:31:31 +01:00
|
|
|
|
2019-10-11 10:54:26 +02:00
|
|
|
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
|
|
|
|
// account for this.
|
2019-12-22 17:42:04 -05:00
|
|
|
if !self.expect_one_of(&[], &[])? {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
2020-01-30 08:31:31 +01:00
|
|
|
|
2019-11-09 22:05:20 +03:00
|
|
|
Ok(FnHeader { constness, unsafety, asyncness, ext })
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 10:36:29 +01:00
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 10:54:26 +02:00
|
|
|
/// Parses the parameter list and result type of a function declaration.
|
|
|
|
pub(super) fn parse_fn_decl(
|
|
|
|
&mut self,
|
2020-02-10 15:35:05 +01:00
|
|
|
req_name: ReqName,
|
2020-01-29 01:57:24 +01:00
|
|
|
ret_allow_plus: AllowPlus,
|
2019-10-11 10:54:26 +02:00
|
|
|
) -> PResult<'a, P<FnDecl>> {
|
|
|
|
Ok(P(FnDecl {
|
2020-02-10 15:35:05 +01:00
|
|
|
inputs: self.parse_fn_params(req_name)?,
|
2020-01-29 01:57:24 +01:00
|
|
|
output: self.parse_ret_ty(ret_allow_plus, RecoverQPath::Yes)?,
|
2019-10-11 10:54:26 +02:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses the parameter list of a function, including the `(` and `)` delimiters.
|
2020-02-10 15:35:05 +01:00
|
|
|
fn parse_fn_params(&mut self, req_name: ReqName) -> PResult<'a, Vec<Param>> {
|
2020-01-29 01:30:01 +01:00
|
|
|
let mut first_param = true;
|
|
|
|
// Parse the arguments, starting out with `self` being allowed...
|
2019-12-02 03:16:12 +01:00
|
|
|
let (mut params, _) = self.parse_paren_comma_seq(|p| {
|
2020-02-10 15:35:05 +01:00
|
|
|
let param = p.parse_param_general(req_name, first_param).or_else(|mut e| {
|
2019-12-02 03:16:12 +01:00
|
|
|
e.emit();
|
|
|
|
let lo = p.prev_span;
|
|
|
|
// Skip every token until next possible arg or end.
|
|
|
|
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(token::Paren)]);
|
|
|
|
// Create a placeholder argument for proper arg count (issue #34264).
|
|
|
|
Ok(dummy_arg(Ident::new(kw::Invalid, lo.to(p.prev_span))))
|
|
|
|
});
|
2019-10-11 10:54:26 +02:00
|
|
|
// ...now that we've parsed the first argument, `self` is no longer allowed.
|
2020-01-29 01:30:01 +01:00
|
|
|
first_param = false;
|
2019-12-02 03:16:12 +01:00
|
|
|
param
|
2019-10-11 10:54:26 +02:00
|
|
|
})?;
|
2019-11-26 22:19:54 -05:00
|
|
|
// Replace duplicated recovered params with `_` pattern to avoid unnecessary errors.
|
2019-10-11 10:54:26 +02:00
|
|
|
self.deduplicate_recovered_params_names(&mut params);
|
|
|
|
Ok(params)
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:30:01 +01:00
|
|
|
/// Parses a single function parameter.
|
|
|
|
///
|
|
|
|
/// - `self` is syntactically allowed when `first_param` holds.
|
2020-02-10 15:35:05 +01:00
|
|
|
fn parse_param_general(&mut self, req_name: ReqName, first_param: bool) -> PResult<'a, Param> {
|
2019-10-11 10:54:26 +02:00
|
|
|
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();
|
2020-02-02 11:10:27 +01:00
|
|
|
return if first_param { Ok(param) } else { self.recover_bad_self_param(param) };
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let is_name_required = match self.token.kind {
|
|
|
|
token::DotDotDot => false,
|
2020-02-10 15:35:05 +01:00
|
|
|
_ => req_name(&self.token),
|
2019-10-11 10:54:26 +02:00
|
|
|
};
|
|
|
|
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) {
|
2020-02-02 11:10:27 +01:00
|
|
|
return if let Some(ident) =
|
|
|
|
self.parameter_without_type(&mut err, pat, is_name_required, first_param)
|
|
|
|
{
|
2019-10-11 10:54:26 +02:00
|
|
|
err.emit();
|
|
|
|
Ok(dummy_arg(ident))
|
|
|
|
} else {
|
|
|
|
Err(err)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
self.eat_incorrect_doc_comment_for_param_type();
|
2019-12-02 02:38:33 +01:00
|
|
|
(pat, self.parse_ty_for_param()?)
|
2019-10-11 10:54:26 +02:00
|
|
|
} else {
|
|
|
|
debug!("parse_param_general ident_to_pat");
|
|
|
|
let parser_snapshot_before_ty = self.clone();
|
|
|
|
self.eat_incorrect_doc_comment_for_param_type();
|
2019-12-02 02:38:33 +01:00
|
|
|
let mut ty = self.parse_ty_for_param();
|
2019-12-22 17:42:04 -05:00
|
|
|
if ty.is_ok()
|
|
|
|
&& self.token != token::Comma
|
|
|
|
&& self.token != token::CloseDelim(token::Paren)
|
|
|
|
{
|
2019-10-11 10:54:26 +02:00
|
|
|
// This wasn't actually a type, but a pattern looking like a type,
|
|
|
|
// so we are going to rollback and re-parse for recovery.
|
|
|
|
ty = self.unexpected();
|
|
|
|
}
|
|
|
|
match ty {
|
|
|
|
Ok(ty) => {
|
|
|
|
let ident = Ident::new(kw::Invalid, self.prev_span);
|
2019-12-16 17:28:40 +01:00
|
|
|
let bm = BindingMode::ByValue(Mutability::Not);
|
2019-10-11 10:54:26 +02:00
|
|
|
let pat = self.mk_pat_ident(ty.span, bm, ident);
|
|
|
|
(pat, ty)
|
|
|
|
}
|
|
|
|
// If this is a C-variadic argument and we hit an error, return the error.
|
|
|
|
Err(err) if self.token == token::DotDotDot => return Err(err),
|
|
|
|
// Recover from attempting to parse the argument as a type without pattern.
|
|
|
|
Err(mut err) => {
|
|
|
|
err.cancel();
|
|
|
|
mem::replace(self, parser_snapshot_before_ty);
|
|
|
|
self.recover_arg_parse()?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let span = lo.to(self.token.span);
|
|
|
|
|
|
|
|
Ok(Param {
|
|
|
|
attrs: attrs.into(),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
is_placeholder: false,
|
|
|
|
pat,
|
|
|
|
span,
|
|
|
|
ty,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the parsed optional self parameter and whether a self shortcut was used.
|
|
|
|
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| {
|
|
|
|
match this.token.kind {
|
|
|
|
// Preserve hygienic context.
|
|
|
|
token::Ident(name, _) => {
|
|
|
|
let span = this.token.span;
|
|
|
|
this.bump();
|
|
|
|
Ident::new(name, span)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Is `self` `n` tokens ahead?
|
|
|
|
let is_isolated_self = |this: &Self, n| {
|
|
|
|
this.is_keyword_ahead(n, &[kw::SelfLower])
|
2019-12-22 17:42:04 -05:00
|
|
|
&& this.look_ahead(n + 1, |t| t != &token::ModSep)
|
2019-10-11 10:54:26 +02:00
|
|
|
};
|
|
|
|
// Is `mut self` `n` tokens ahead?
|
2019-12-22 17:42:04 -05:00
|
|
|
let is_isolated_mut_self =
|
|
|
|
|this: &Self, n| this.is_keyword_ahead(n, &[kw::Mut]) && is_isolated_self(this, n + 1);
|
2019-10-11 10:54:26 +02:00
|
|
|
// Parse `self` or `self: TYPE`. We already know the current token is `self`.
|
|
|
|
let parse_self_possibly_typed = |this: &mut Self, m| {
|
|
|
|
let eself_ident = expect_self_ident(this);
|
|
|
|
let eself_hi = this.prev_span;
|
|
|
|
let eself = if this.eat(&token::Colon) {
|
|
|
|
SelfKind::Explicit(this.parse_ty()?, m)
|
|
|
|
} else {
|
|
|
|
SelfKind::Value(m)
|
|
|
|
};
|
|
|
|
Ok((eself, eself_ident, eself_hi))
|
|
|
|
};
|
|
|
|
// Recover for the grammar `*self`, `*const self`, and `*mut self`.
|
|
|
|
let recover_self_ptr = |this: &mut Self| {
|
|
|
|
let msg = "cannot pass `self` by raw pointer";
|
|
|
|
let span = this.token.span;
|
2019-12-22 17:42:04 -05:00
|
|
|
this.struct_span_err(span, msg).span_label(span, msg).emit();
|
2019-10-11 10:54:26 +02:00
|
|
|
|
2019-12-16 17:28:40 +01:00
|
|
|
Ok((SelfKind::Value(Mutability::Not), expect_self_ident(this), this.prev_span))
|
2019-10-11 10:54:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Parse optional `self` parameter of a method.
|
|
|
|
// Only a limited set of initial token sequences is considered `self` parameters; anything
|
|
|
|
// else is parsed as a normal function parameter list, so some lookahead is required.
|
|
|
|
let eself_lo = self.token.span;
|
|
|
|
let (eself, eself_ident, eself_hi) = match self.token.kind {
|
|
|
|
token::BinOp(token::And) => {
|
|
|
|
let eself = if is_isolated_self(self, 1) {
|
|
|
|
// `&self`
|
|
|
|
self.bump();
|
2019-12-16 17:28:40 +01:00
|
|
|
SelfKind::Region(None, Mutability::Not)
|
2019-10-11 10:54:26 +02:00
|
|
|
} else if is_isolated_mut_self(self, 1) {
|
|
|
|
// `&mut self`
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
2019-12-16 17:28:40 +01:00
|
|
|
SelfKind::Region(None, Mutability::Mut)
|
2019-10-11 10:54:26 +02:00
|
|
|
} else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_self(self, 2) {
|
|
|
|
// `&'lt self`
|
|
|
|
self.bump();
|
|
|
|
let lt = self.expect_lifetime();
|
2019-12-16 17:28:40 +01:00
|
|
|
SelfKind::Region(Some(lt), Mutability::Not)
|
2019-10-11 10:54:26 +02:00
|
|
|
} else if self.look_ahead(1, |t| t.is_lifetime()) && is_isolated_mut_self(self, 2) {
|
|
|
|
// `&'lt mut self`
|
|
|
|
self.bump();
|
|
|
|
let lt = self.expect_lifetime();
|
|
|
|
self.bump();
|
2019-12-16 17:28:40 +01:00
|
|
|
SelfKind::Region(Some(lt), Mutability::Mut)
|
2019-10-11 10:54:26 +02:00
|
|
|
} else {
|
|
|
|
// `¬_self`
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
(eself, expect_self_ident(self), self.prev_span)
|
|
|
|
}
|
|
|
|
// `*self`
|
|
|
|
token::BinOp(token::Star) if is_isolated_self(self, 1) => {
|
|
|
|
self.bump();
|
|
|
|
recover_self_ptr(self)?
|
|
|
|
}
|
|
|
|
// `*mut self` and `*const self`
|
2019-12-22 17:42:04 -05:00
|
|
|
token::BinOp(token::Star)
|
|
|
|
if self.look_ahead(1, |t| t.is_mutability()) && is_isolated_self(self, 2) =>
|
2019-10-11 10:54:26 +02:00
|
|
|
{
|
|
|
|
self.bump();
|
|
|
|
self.bump();
|
|
|
|
recover_self_ptr(self)?
|
|
|
|
}
|
|
|
|
// `self` and `self: TYPE`
|
|
|
|
token::Ident(..) if is_isolated_self(self, 0) => {
|
2019-12-16 17:28:40 +01:00
|
|
|
parse_self_possibly_typed(self, Mutability::Not)?
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
// `mut self` and `mut self: TYPE`
|
|
|
|
token::Ident(..) if is_isolated_mut_self(self, 0) => {
|
|
|
|
self.bump();
|
2019-12-16 17:28:40 +01:00
|
|
|
parse_self_possibly_typed(self, Mutability::Mut)?
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let eself = source_map::respan(eself_lo.to(eself_hi), eself);
|
2019-12-03 16:38:34 +01:00
|
|
|
Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident)))
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_named_param(&self) -> bool {
|
|
|
|
let offset = match self.token.kind {
|
|
|
|
token::Interpolated(ref nt) => match **nt {
|
|
|
|
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
|
|
|
|
_ => 0,
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
2019-10-11 10:54:26 +02:00
|
|
|
token::BinOp(token::And) | token::AndAnd => 1,
|
|
|
|
_ if self.token.is_keyword(kw::Mut) => 1,
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
self.look_ahead(offset, |t| t.is_ident())
|
|
|
|
&& self.look_ahead(offset + 1, |t| t == &token::Colon)
|
2019-10-11 10:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn recover_first_param(&mut self) -> &'static str {
|
2019-12-22 17:42:04 -05:00
|
|
|
match self
|
|
|
|
.parse_outer_attributes()
|
2019-10-11 10:54:26 +02:00
|
|
|
.and_then(|_| self.parse_self_param())
|
|
|
|
.map_err(|mut e| e.cancel())
|
|
|
|
{
|
|
|
|
Ok(Some(_)) => "method",
|
|
|
|
_ => "function",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|