mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-29 20:46:07 +03:00
parser: move restrictions re. self to ast_validation.
This commit is contained in:
@@ -1336,8 +1336,8 @@ pub(super) fn parameter_without_type(
|
||||
err: &mut DiagnosticBuilder<'_>,
|
||||
pat: P<ast::Pat>,
|
||||
require_name: bool,
|
||||
is_self_allowed: bool,
|
||||
is_trait_item: bool,
|
||||
is_self_semantic: bool,
|
||||
in_assoc_item: bool,
|
||||
) -> Option<Ident> {
|
||||
// If we find a pattern followed by an identifier, it could be an (incorrect)
|
||||
// C-style parameter declaration.
|
||||
@@ -1357,13 +1357,13 @@ pub(super) fn parameter_without_type(
|
||||
return Some(ident);
|
||||
} else if let PatKind::Ident(_, ident, _) = pat.kind {
|
||||
if require_name
|
||||
&& (is_trait_item
|
||||
&& (in_assoc_item
|
||||
|| self.token == token::Comma
|
||||
|| self.token == token::Lt
|
||||
|| self.token == token::CloseDelim(token::Paren))
|
||||
{
|
||||
// `fn foo(a, b) {}`, `fn foo(a<x>, b<y>) {}` or `fn foo(usize, usize) {}`
|
||||
if is_self_allowed {
|
||||
if is_self_semantic {
|
||||
err.span_suggestion(
|
||||
pat.span,
|
||||
"if this is a `self` type, give it a parameter name",
|
||||
@@ -1423,12 +1423,12 @@ pub(super) fn recover_arg_parse(&mut self) -> PResult<'a, (P<ast::Pat>, P<ast::T
|
||||
pub(super) fn recover_bad_self_param(
|
||||
&mut self,
|
||||
mut param: ast::Param,
|
||||
is_trait_item: bool,
|
||||
in_assoc_item: bool,
|
||||
) -> PResult<'a, ast::Param> {
|
||||
let sp = param.pat.span;
|
||||
param.ty.kind = TyKind::Err;
|
||||
let mut err = self.struct_span_err(sp, "unexpected `self` parameter in function");
|
||||
if is_trait_item {
|
||||
if in_assoc_item {
|
||||
err.span_label(sp, "must be the first associated function parameter");
|
||||
} else {
|
||||
err.span_label(sp, "not valid as function parameter");
|
||||
|
||||
@@ -1715,8 +1715,9 @@ fn mk_item(
|
||||
|
||||
/// 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 `self` is *semantically* allowed as the first parameter?
|
||||
/// This is only used for diagnostics.
|
||||
pub in_assoc_item: 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,
|
||||
@@ -1732,8 +1733,8 @@ fn parse_item_fn(
|
||||
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 cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| true };
|
||||
let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
|
||||
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)))
|
||||
@@ -1747,20 +1748,13 @@ fn parse_item_foreign_fn(
|
||||
attrs: Vec<Attribute>,
|
||||
extern_sp: Span,
|
||||
) -> PResult<'a, P<ForeignItem>> {
|
||||
let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| true };
|
||||
self.expect_keyword(kw::Fn)?;
|
||||
let (ident, decl, generics) =
|
||||
self.parse_fn_sig(ParamCfg { is_self_allowed: false, is_name_required: |_| true })?;
|
||||
let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
|
||||
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,
|
||||
}))
|
||||
let kind = ForeignItemKind::Fn(decl, generics);
|
||||
Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
|
||||
}
|
||||
|
||||
fn parse_assoc_fn(
|
||||
@@ -1769,9 +1763,9 @@ fn parse_assoc_fn(
|
||||
attrs: &mut Vec<Attribute>,
|
||||
is_name_required: fn(&token::Token) -> bool,
|
||||
) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
|
||||
let cfg = ParamCfg { in_assoc_item: true, is_name_required };
|
||||
let header = self.parse_fn_front_matter()?;
|
||||
let (ident, decl, generics) =
|
||||
self.parse_fn_sig(ParamCfg { is_self_allowed: true, is_name_required })?;
|
||||
let (ident, decl, generics) = self.parse_fn_sig(&cfg)?;
|
||||
let sig = FnSig { header, decl };
|
||||
let body = self.parse_assoc_fn_body(at_end, attrs)?;
|
||||
Ok((ident, AssocItemKind::Fn(sig, body), generics))
|
||||
@@ -1847,7 +1841,7 @@ fn parse_fn_front_matter(&mut self) -> PResult<'a, FnHeader> {
|
||||
}
|
||||
|
||||
/// 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)> {
|
||||
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)?;
|
||||
@@ -1858,7 +1852,7 @@ fn parse_fn_sig(&mut self, cfg: ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Gene
|
||||
/// Parses the parameter list and result type of a function declaration.
|
||||
pub(super) fn parse_fn_decl(
|
||||
&mut self,
|
||||
cfg: ParamCfg,
|
||||
cfg: &ParamCfg,
|
||||
ret_allow_plus: bool,
|
||||
) -> PResult<'a, P<FnDecl>> {
|
||||
Ok(P(FnDecl {
|
||||
@@ -1868,11 +1862,11 @@ pub(super) fn parse_fn_decl(
|
||||
}
|
||||
|
||||
/// 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, cfg: &ParamCfg) -> 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(&cfg, first_param).or_else(|mut e| {
|
||||
e.emit();
|
||||
let lo = p.prev_span;
|
||||
// Skip every token until next possible arg or end.
|
||||
@@ -1881,7 +1875,7 @@ fn parse_fn_params(&mut self, mut cfg: ParamCfg) -> PResult<'a, Vec<Param>> {
|
||||
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.
|
||||
@@ -1889,20 +1883,20 @@ fn parse_fn_params(&mut self, mut cfg: ParamCfg) -> PResult<'a, Vec<Param>> {
|
||||
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, cfg: &ParamCfg, 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 {
|
||||
return if first_param {
|
||||
Ok(param)
|
||||
} else {
|
||||
self.recover_bad_self_param(param, is_trait_item)
|
||||
self.recover_bad_self_param(param, cfg.in_assoc_item)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1919,8 +1913,8 @@ fn parse_param_general(&mut self, cfg: &ParamCfg, is_trait_item: bool) -> PResul
|
||||
&mut err,
|
||||
pat,
|
||||
is_name_required,
|
||||
cfg.is_self_allowed,
|
||||
is_trait_item,
|
||||
first_param && cfg.in_assoc_item,
|
||||
cfg.in_assoc_item,
|
||||
) {
|
||||
err.emit();
|
||||
Ok(dummy_arg(ident))
|
||||
@@ -1975,8 +1969,6 @@ fn parse_param_general(&mut self, cfg: &ParamCfg, is_trait_item: bool) -> PResul
|
||||
}
|
||||
|
||||
/// 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| {
|
||||
|
||||
@@ -288,8 +288,8 @@ fn parse_ty_bare_fn(&mut self, generic_params: Vec<GenericParam>) -> PResult<'a,
|
||||
let unsafety = self.parse_unsafety();
|
||||
let ext = self.parse_extern()?;
|
||||
self.expect_keyword(kw::Fn)?;
|
||||
let cfg = ParamCfg { is_self_allowed: false, is_name_required: |_| false };
|
||||
let decl = self.parse_fn_decl(cfg, false)?;
|
||||
let cfg = ParamCfg { in_assoc_item: false, is_name_required: |_| false };
|
||||
let decl = self.parse_fn_decl(&cfg, false)?;
|
||||
Ok(TyKind::BareFn(P(BareFnTy { ext, unsafety, generic_params, decl })))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user