Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: more cleanups in item and function signature parsing #65040

Merged
merged 7 commits into from
Oct 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 38 additions & 58 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ mod stmt;
mod generics;

use crate::ast::{
self, DUMMY_NODE_ID, AttrStyle, Attribute, BindingMode, CrateSugar, FnDecl, Ident,
self, DUMMY_NODE_ID, AttrStyle, Attribute, BindingMode, CrateSugar, Ident,
IsAsync, MacDelimiter, Mutability, Param, StrStyle, SelfKind, TyKind, Visibility,
VisibilityKind, Unsafety,
};
@@ -56,6 +56,17 @@ crate enum BlockMode {
Ignore,
}

/// The parsing configuration used to parse a parameter list (see `parse_fn_params`).
struct ParamCfg {
/// Is `self` is allowed as the first parameter?
is_self_allowed: bool,
/// Is `...` allowed as the tail of the parameter list?
allow_c_variadic: bool,
/// `is_name_required` decides if, per-parameter,
/// the parameter must have a pattern or just a type.
is_name_required: fn(&token::Token) -> bool,
}

/// Like `maybe_whole_expr`, but for things other than expressions.
#[macro_export]
macro_rules! maybe_whole {
@@ -1094,26 +1105,18 @@ impl<'a> Parser<'a> {
res
}

fn parse_fn_params(
&mut self,
named_params: bool,
allow_c_variadic: bool,
) -> PResult<'a, Vec<Param>> {
/// 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 sp = self.token.span;
let do_not_enforce_named_params_for_c_variadic = |token: &token::Token| {
match token.kind {
token::DotDotDot => false,
_ => named_params,
}
};
let is_trait_item = cfg.is_self_allowed;
let mut c_variadic = false;
// Parse the arguments, starting out with `self` being possibly allowed...
let (params, _) = self.parse_paren_comma_seq(|p| {
match p.parse_param_general(
false,
false,
allow_c_variadic,
do_not_enforce_named_params_for_c_variadic,
) {
let param = p.parse_param_general(&cfg, is_trait_item);
// ...now that we've parsed the first argument, `self` is no longer allowed.
cfg.is_self_allowed = false;

match param {
Ok(param) => Ok(
if let TyKind::CVarArgs = param.ty.kind {
c_variadic = true;
@@ -1144,7 +1147,10 @@ impl<'a> Parser<'a> {
}
})?;

let params: Vec<_> = params.into_iter().filter_map(|x| x).collect();
let mut params: Vec<_> = params.into_iter().filter_map(|x| x).collect();

// Replace duplicated recovered params with `_` pattern to avoid unecessary errors.
self.deduplicate_recovered_params_names(&mut params);

if c_variadic && params.len() <= 1 {
self.span_err(
@@ -1156,79 +1162,53 @@ impl<'a> Parser<'a> {
Ok(params)
}

/// Parses the parameter list and result type of a function that may have a `self` parameter.
fn parse_fn_decl_with_self(
&mut self,
is_name_required: impl Copy + Fn(&token::Token) -> bool,
) -> PResult<'a, P<FnDecl>> {
// Parse the arguments, starting out with `self` being allowed...
let mut is_self_allowed = true;
let (mut inputs, _): (Vec<_>, _) = self.parse_paren_comma_seq(|p| {
let res = p.parse_param_general(is_self_allowed, true, false, is_name_required);
// ...but now that we've parsed the first argument, `self` is no longer allowed.
is_self_allowed = false;
res
})?;

// Replace duplicated recovered params with `_` pattern to avoid unecessary errors.
self.deduplicate_recovered_params_names(&mut inputs);

Ok(P(FnDecl {
inputs,
output: self.parse_ret_ty(true)?,
}))
}

/// 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,
is_self_allowed: bool,
is_trait_item: bool,
allow_c_variadic: bool,
is_name_required: impl Fn(&token::Token) -> bool,
) -> PResult<'a, Param> {
fn parse_param_general(&mut self, cfg: &ParamCfg, is_trait_item: 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 is_self_allowed {
return if cfg.is_self_allowed {
Ok(param)
} else {
self.recover_bad_self_param(param, is_trait_item)
};
}

let is_name_required = is_name_required(&self.token);
let is_name_required = match self.token.kind {
token::DotDotDot => false,
_ => (cfg.is_name_required)(&self.token),
};
let (pat, ty) = if is_name_required || self.is_named_param() {
debug!("parse_param_general parse_pat (is_name_required:{})", is_name_required);

let pat = self.parse_fn_param_pat()?;
if let Err(mut err) = self.expect(&token::Colon) {
if let Some(ident) = self.parameter_without_type(
return if let Some(ident) = self.parameter_without_type(
&mut err,
pat,
is_name_required,
is_self_allowed,
cfg.is_self_allowed,
is_trait_item,
) {
err.emit();
return Ok(dummy_arg(ident));
Ok(dummy_arg(ident))
} else {
return Err(err);
}
Err(err)
};
}

self.eat_incorrect_doc_comment_for_param_type();
(pat, self.parse_ty_common(true, true, allow_c_variadic)?)
(pat, self.parse_ty_common(true, true, cfg.allow_c_variadic)?)
} else {
debug!("parse_param_general ident_to_pat");
let parser_snapshot_before_ty = self.clone();
self.eat_incorrect_doc_comment_for_param_type();
let mut ty = self.parse_ty_common(true, true, allow_c_variadic);
let mut ty = self.parse_ty_common(true, true, cfg.allow_c_variadic);
if ty.is_ok() && self.token != token::Comma &&
self.token != token::CloseDelim(token::Paren) {
// This wasn't actually a type, but a pattern looking like a type,
293 changes: 152 additions & 141 deletions src/libsyntax/parse/parser/item.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::{Parser, PResult, PathStyle, SemiColonMode, BlockMode};
use super::{Parser, PResult, PathStyle, SemiColonMode, BlockMode, ParamCfg};

use crate::maybe_whole;
use crate::ptr::P;
use crate::ast::{
self, DUMMY_NODE_ID, Ident, Attribute, AttrStyle,
Item, ItemKind, ImplItem, TraitItem, TraitItemKind,
Item, ItemKind, ImplItem, ImplItemKind, TraitItem, TraitItemKind,
UseTree, UseTreeKind, PathSegment,
IsAuto, Constness, IsAsync, Unsafety, Defaultness,
Visibility, VisibilityKind, Mutability, FnDecl, FnHeader, MethodSig, Block,
@@ -98,23 +98,22 @@ impl<'a> Parser<'a> {

let lo = self.token.span;

let visibility = self.parse_visibility(false)?;
let vis = self.parse_visibility(false)?;

if self.eat_keyword(kw::Use) {
// USE ITEM
let item_ = ItemKind::Use(P(self.parse_use_tree()?));
self.expect(&token::Semi)?;

let span = lo.to(self.prev_span);
let item =
self.mk_item(span, Ident::invalid(), item_, visibility, attrs);
let item = self.mk_item(span, Ident::invalid(), item_, vis, attrs);
return Ok(Some(item));
}

if self.eat_keyword(kw::Extern) {
let extern_sp = self.prev_span;
if self.eat_keyword(kw::Crate) {
return Ok(Some(self.parse_item_extern_crate(lo, visibility, attrs)?));
return Ok(Some(self.parse_item_extern_crate(lo, vis, attrs)?));
}

let opt_abi = self.parse_opt_abi()?;
@@ -128,10 +127,10 @@ impl<'a> Parser<'a> {
constness: respan(fn_span, Constness::NotConst),
abi: opt_abi.unwrap_or(Abi::C),
};
return self.parse_item_fn(lo, visibility, attrs, header);
return self.parse_item_fn(lo, vis, attrs, header);
} else if self.check(&token::OpenDelim(token::Brace)) {
return Ok(Some(
self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs, extern_sp)?,
self.parse_item_foreign_mod(lo, opt_abi, vis, attrs, extern_sp)?,
));
}

@@ -142,16 +141,14 @@ impl<'a> Parser<'a> {
self.bump();
// STATIC ITEM
let m = self.parse_mutability();
let (ident, item_, extra_attrs) = self.parse_item_const(Some(m))?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_const(Some(m))?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

if self.eat_keyword(kw::Const) {
let const_span = self.prev_span;
if [kw::Fn, kw::Unsafe, kw::Extern].iter().any(|k| self.check_keyword(*k)) {
// CONST FUNCTION ITEM

let unsafety = self.parse_unsafety();

if self.check_keyword(kw::Extern) {
@@ -160,15 +157,15 @@ impl<'a> Parser<'a> {
);
}
let abi = self.parse_extern_abi()?;
self.bump(); // 'fn'
self.bump(); // `fn`

let header = FnHeader {
unsafety,
asyncness: respan(const_span, IsAsync::NotAsync),
constness: respan(const_span, Constness::Const),
abi,
};
return self.parse_item_fn(lo, visibility, attrs, header);
return self.parse_item_fn(lo, vis, attrs, header);
}

// CONST ITEM
@@ -184,10 +181,9 @@ impl<'a> Parser<'a> {
)
.emit();
}
let (ident, item_, extra_attrs) = self.parse_item_const(None)?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));

let info = self.parse_item_const(None)?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

// Parses `async unsafe? fn`.
@@ -212,40 +208,33 @@ impl<'a> Parser<'a> {
constness: respan(fn_span, Constness::NotConst),
abi: Abi::Rust,
};
return self.parse_item_fn(lo, visibility, attrs, header);
return self.parse_item_fn(lo, vis, attrs, header);
}
}

if self.check_keyword(kw::Unsafe) &&
self.is_keyword_ahead(1, &[kw::Trait, kw::Auto])
{
// UNSAFE TRAIT ITEM
self.bump(); // `unsafe`
let is_auto = if self.eat_keyword(kw::Trait) {
IsAuto::No
} else {
self.expect_keyword(kw::Auto)?;
self.expect_keyword(kw::Trait)?;
IsAuto::Yes
};
let (ident, item_, extra_attrs) = self.parse_item_trait(is_auto, Unsafety::Unsafe)?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_trait(Unsafety::Unsafe)?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

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]) {
self.is_keyword_ahead(1, &[kw::Impl, kw::Unsafe])
{
// IMPL ITEM
let defaultness = self.parse_defaultness();
let unsafety = self.parse_unsafety();
self.expect_keyword(kw::Impl)?;
let (ident, item_, extra_attrs) = self.parse_item_impl(unsafety, defaultness)?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_impl(unsafety, defaultness)?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

if self.check_keyword(kw::Fn) {
// FUNCTION ITEM
self.bump();
@@ -256,10 +245,12 @@ impl<'a> Parser<'a> {
constness: respan(fn_span, Constness::NotConst),
abi: Abi::Rust,
};
return self.parse_item_fn(lo, visibility, attrs, header);
return self.parse_item_fn(lo, vis, attrs, header);
}

if self.check_keyword(kw::Unsafe)
&& self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace)) {
&& self.look_ahead(1, |t| *t != token::OpenDelim(token::Brace))
{
// UNSAFE FUNCTION ITEM
self.bump(); // `unsafe`
// `{` is also expected after `unsafe`; in case of error, include it in the diagnostic.
@@ -273,15 +264,15 @@ impl<'a> Parser<'a> {
constness: respan(fn_span, Constness::NotConst),
abi,
};
return self.parse_item_fn(lo, visibility, attrs, header);
return self.parse_item_fn(lo, vis, attrs, header);
}

if self.eat_keyword(kw::Mod) {
// MODULE ITEM
let (ident, item_, extra_attrs) = self.parse_item_mod(&attrs[..])?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_mod(&attrs[..])?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

if let Some(type_) = self.eat_type() {
let (ident, alias, generics) = type_?;
// TYPE ITEM
@@ -290,54 +281,44 @@ impl<'a> Parser<'a> {
AliasKind::OpaqueTy(bounds) => ItemKind::OpaqueTy(bounds, generics),
};
let span = lo.to(self.prev_span);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
return Ok(Some(self.mk_item(span, ident, item_, vis, attrs)));
}

if self.eat_keyword(kw::Enum) {
// ENUM ITEM
let (ident, item_, extra_attrs) = self.parse_item_enum()?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_enum()?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

if self.check_keyword(kw::Trait)
|| (self.check_keyword(kw::Auto)
&& self.is_keyword_ahead(1, &[kw::Trait]))
{
let is_auto = if self.eat_keyword(kw::Trait) {
IsAuto::No
} else {
self.expect_keyword(kw::Auto)?;
self.expect_keyword(kw::Trait)?;
IsAuto::Yes
};
// TRAIT ITEM
let (ident, item_, extra_attrs) = self.parse_item_trait(is_auto, Unsafety::Normal)?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_trait(Unsafety::Normal)?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

if self.eat_keyword(kw::Struct) {
// STRUCT ITEM
let (ident, item_, extra_attrs) = self.parse_item_struct()?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_struct()?;
return self.mk_item_with_info(attrs, lo, vis, info);
}

if self.is_union_item() {
// UNION ITEM
self.bump();
let (ident, item_, extra_attrs) = self.parse_item_union()?;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
return Ok(Some(self.mk_item(span, ident, item_, visibility, attrs)));
let info = self.parse_item_union()?;
return self.mk_item_with_info(attrs, lo, vis, info);
}
if let Some(macro_def) = self.eat_macro_def(&attrs, &visibility, lo)? {

if let Some(macro_def) = self.eat_macro_def(&attrs, &vis, lo)? {
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 {}`
if visibility.node.is_pub() &&
if vis.node.is_pub() &&
self.check_ident() &&
self.look_ahead(1, |t| *t != token::Not)
{
@@ -428,7 +409,20 @@ impl<'a> Parser<'a> {
return Err(err);
}
}
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility)
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, vis)
}

fn mk_item_with_info(
&self,
attrs: Vec<Attribute>,
lo: Span,
vis: Visibility,
info: ItemInfo,
) -> PResult<'a, Option<P<Item>>> {
let (ident, item, extra_attrs) = info;
let span = lo.to(self.prev_span);
let attrs = maybe_append(attrs, extra_attrs);
Ok(Some(self.mk_item(span, ident, item, vis, attrs)))
}

fn recover_first_param(&mut self) -> &'static str {
@@ -727,16 +721,7 @@ impl<'a> Parser<'a> {
};
(name, kind, generics)
} else if self.is_const_item() {
// This parses the grammar:
// ImplItemConst = "const" Ident ":" Ty "=" Expr ";"
self.expect_keyword(kw::Const)?;
let name = self.parse_ident()?;
self.expect(&token::Colon)?;
let typ = self.parse_ty()?;
self.expect(&token::Eq)?;
let expr = self.parse_expr()?;
self.expect(&token::Semi)?;
(name, ast::ImplItemKind::Const(typ, expr), Generics::default())
self.parse_impl_const()?
} else {
let (name, inner_attrs, generics, kind) = self.parse_impl_method(&vis, at_end)?;
attrs.extend(inner_attrs);
@@ -785,12 +770,25 @@ impl<'a> Parser<'a> {
!self.is_keyword_ahead(1, &[kw::Fn, kw::Unsafe])
}

/// This parses the grammar:
/// ImplItemConst = "const" Ident ":" Ty "=" Expr ";"
fn parse_impl_const(&mut self) -> PResult<'a, (Ident, ImplItemKind, Generics)> {
self.expect_keyword(kw::Const)?;
let name = self.parse_ident()?;
self.expect(&token::Colon)?;
let typ = self.parse_ty()?;
self.expect(&token::Eq)?;
let expr = self.parse_expr()?;
self.expect(&token::Semi)?;
Ok((name, ImplItemKind::Const(typ, expr), Generics::default()))
}

/// Parses a method or a macro invocation in a trait impl.
fn parse_impl_method(
&mut self,
vis: &Visibility,
at_end: &mut bool
) -> PResult<'a, (Ident, Vec<Attribute>, Generics, ast::ImplItemKind)> {
) -> PResult<'a, (Ident, Vec<Attribute>, Generics, ImplItemKind)> {
// FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(vis), at_end)? {
// method macro
@@ -807,14 +805,15 @@ impl<'a> Parser<'a> {
/// of a method. The body is not parsed as that differs between `trait`s and `impl`s.
fn parse_method_sig(
&mut self,
is_name_required: impl Copy + Fn(&token::Token) -> bool,
is_name_required: fn(&token::Token) -> bool,
) -> PResult<'a, (Ident, MethodSig, Generics)> {
let header = self.parse_fn_front_matter()?;
let (ident, mut generics) = self.parse_fn_header()?;
let decl = self.parse_fn_decl_with_self(is_name_required)?;
let sig = MethodSig { header, decl };
generics.where_clause = self.parse_where_clause()?;
Ok((ident, sig, generics))
let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
is_self_allowed: true,
allow_c_variadic: false,
is_name_required,
})?;
Ok((ident, MethodSig { header, decl }, generics))
}

/// Parses all the "front matter" for a `fn` declaration, up to
@@ -849,8 +848,16 @@ impl<'a> Parser<'a> {
Ok(FnHeader { constness, unsafety, asyncness, abi })
}

/// Parses `trait Foo { ... }` or `trait Foo = Bar;`.
fn parse_item_trait(&mut self, is_auto: IsAuto, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
/// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
fn parse_item_trait(&mut self, unsafety: Unsafety) -> PResult<'a, ItemInfo> {
// Parse optional `auto` prefix.
let is_auto = if self.eat_keyword(kw::Auto) {
IsAuto::Yes
} else {
IsAuto::No
};

self.expect_keyword(kw::Trait)?;
let ident = self.parse_ident()?;
let mut tps = self.parse_generics()?;

@@ -935,38 +942,28 @@ impl<'a> Parser<'a> {
Ok(item)
}

fn parse_trait_item_(&mut self,
at_end: &mut bool,
mut attrs: Vec<Attribute>) -> PResult<'a, TraitItem> {
fn parse_trait_item_(
&mut self,
at_end: &mut bool,
mut attrs: Vec<Attribute>,
) -> PResult<'a, TraitItem> {
let lo = self.token.span;
self.eat_bad_pub();
let (name, kind, generics) = if self.eat_keyword(kw::Type) {
self.parse_trait_item_assoc_ty()?
} else if self.is_const_item() {
self.expect_keyword(kw::Const)?;
let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
let default = if self.eat(&token::Eq) {
let expr = self.parse_expr()?;
self.expect(&token::Semi)?;
Some(expr)
} else {
self.expect(&token::Semi)?;
None
};
(ident, TraitItemKind::Const(ty, default), Generics::default())
self.parse_trait_item_const()?
} else if let Some(mac) = self.parse_assoc_macro_invoc("trait", None, &mut false)? {
// trait item macro.
(Ident::invalid(), ast::TraitItemKind::Macro(mac), Generics::default())
(Ident::invalid(), TraitItemKind::Macro(mac), Generics::default())
} else {
// This is somewhat dubious; We don't want to allow
// argument names to be left off if there is a definition...
//
// We don't allow argument names to be left off in edition 2018.
let (ident, sig, generics) = self.parse_method_sig(|t| t.span.rust_2018())?;
let body = self.parse_trait_method_body(at_end, &mut attrs)?;
(ident, ast::TraitItemKind::Method(sig, body), generics)
(ident, TraitItemKind::Method(sig, body), generics)
};

Ok(TraitItem {
@@ -980,6 +977,20 @@ impl<'a> Parser<'a> {
})
}

fn parse_trait_item_const(&mut self) -> PResult<'a, (Ident, TraitItemKind, Generics)> {
self.expect_keyword(kw::Const)?;
let ident = self.parse_ident()?;
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;
let default = if self.eat(&token::Eq) {
Some(self.parse_expr()?)
} else {
None
};
self.expect(&token::Semi)?;
Ok((ident, TraitItemKind::Const(ty, default), Generics::default()))
}

/// Parse the "body" of a method in a trait item definition.
/// This can either be `;` when there's no body,
/// or e.g. a block when the method is a provided one.
@@ -1020,8 +1031,7 @@ impl<'a> Parser<'a> {
/// Parses the following grammar:
///
/// TraitItemAssocTy = Ident ["<"...">"] [":" [GenericBounds]] ["where" ...] ["=" Ty]
fn parse_trait_item_assoc_ty(&mut self)
-> PResult<'a, (Ident, TraitItemKind, Generics)> {
fn parse_trait_item_assoc_ty(&mut self) -> PResult<'a, (Ident, TraitItemKind, Generics)> {
let ident = self.parse_ident()?;
let mut generics = self.parse_generics()?;

@@ -1067,21 +1077,13 @@ impl<'a> Parser<'a> {
);
}

if self.eat(&token::BinOp(token::Star)) {
UseTreeKind::Glob
} else {
UseTreeKind::Nested(self.parse_use_tree_list()?)
}
self.parse_use_tree_glob_or_nested()?
} 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) {
if self.eat(&token::BinOp(token::Star)) {
UseTreeKind::Glob
} else {
UseTreeKind::Nested(self.parse_use_tree_list()?)
}
self.parse_use_tree_glob_or_nested()?
} else {
UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
}
@@ -1090,6 +1092,15 @@ impl<'a> Parser<'a> {
Ok(UseTree { prefix, kind, span: lo.to(self.prev_span) })
}

/// 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()?)
})
}

/// Parses a `UseTreeKind::Nested(list)`.
///
/// ```
@@ -1191,38 +1202,34 @@ impl<'a> Parser<'a> {
attrs: Vec<Attribute>,
header: FnHeader,
) -> PResult<'a, Option<P<Item>>> {
let allow_c_variadic = header.abi == Abi::C && header.unsafety == Unsafety::Unsafe;
let (ident, decl, generics) = self.parse_fn_sig(allow_c_variadic)?;
let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
is_self_allowed: false,
allow_c_variadic: header.abi == Abi::C && header.unsafety == Unsafety::Unsafe,
is_name_required: |_| true,
})?;
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
let span = lo.to(self.prev_span);
let kind = ItemKind::Fn(decl, header, generics, body);
let attrs = maybe_append(attrs, Some(inner_attrs));
Ok(Some(self.mk_item(span, ident, kind, vis, attrs)))
self.mk_item_with_info(attrs, lo, vis, (ident, kind, Some(inner_attrs)))
}

/// Parse the "signature", including the identifier, parameters, and generics of a function.
fn parse_fn_sig(
&mut self,
allow_c_variadic: bool,
) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
let (ident, mut generics) = self.parse_fn_header()?;
let decl = self.parse_fn_decl(allow_c_variadic)?;
fn parse_fn_sig(&mut self, cfg: ParamCfg) -> PResult<'a, (Ident, P<FnDecl>, Generics)> {
let ident = self.parse_ident()?;
let mut generics = self.parse_generics()?;
let decl = self.parse_fn_decl(cfg, true)?;
generics.where_clause = self.parse_where_clause()?;
Ok((ident, decl, generics))
}

/// Parses the name and optional generic types of a function header.
fn parse_fn_header(&mut self) -> PResult<'a, (Ident, Generics)> {
let id = self.parse_ident()?;
let generics = self.parse_generics()?;
Ok((id, generics))
}

/// Parses the parameter list and result type of a function declaration.
fn parse_fn_decl(&mut self, allow_c_variadic: bool) -> PResult<'a, P<FnDecl>> {
pub(super) fn parse_fn_decl(
&mut self,
cfg: ParamCfg,
ret_allow_plus: bool,
) -> PResult<'a, P<FnDecl>> {
Ok(P(FnDecl {
inputs: self.parse_fn_params(true, allow_c_variadic)?,
output: self.parse_ret_ty(true)?,
inputs: self.parse_fn_params(cfg)?,
output: self.parse_ret_ty(ret_allow_plus)?,
}))
}

@@ -1346,7 +1353,11 @@ impl<'a> Parser<'a> {
extern_sp: Span,
) -> PResult<'a, ForeignItem> {
self.expect_keyword(kw::Fn)?;
let (ident, decl, generics) = self.parse_fn_sig(true)?;
let (ident, decl, generics) = self.parse_fn_sig(super::ParamCfg {
is_self_allowed: false,
allow_c_variadic: true,
is_name_required: |_| true,
})?;
let span = lo.to(self.token.span);
self.parse_semi_or_incorrect_foreign_fn_body(&ident, extern_sp)?;
Ok(ast::ForeignItem {
23 changes: 8 additions & 15 deletions src/libsyntax/parse/parser/ty.rs
Original file line number Diff line number Diff line change
@@ -4,13 +4,11 @@ use crate::{maybe_whole, maybe_recover_from_interpolated_ty_qpath};
use crate::ptr::P;
use crate::ast::{self, Ty, TyKind, MutTy, BareFnTy, FunctionRetTy, GenericParam, Lifetime, Ident};
use crate::ast::{TraitBoundModifier, TraitObjectSyntax, GenericBound, GenericBounds, PolyTraitRef};
use crate::ast::{Mutability, AnonConst, FnDecl, Mac};
use crate::ast::{Mutability, AnonConst, Mac};
use crate::parse::token::{self, Token};
use crate::source_map::Span;
use crate::symbol::{kw};

use rustc_target::spec::abi::Abi;

use errors::{Applicability, pluralise};

/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
@@ -281,19 +279,14 @@ impl<'a> Parser<'a> {
*/

let unsafety = self.parse_unsafety();
let abi = if self.eat_keyword(kw::Extern) {
self.parse_opt_abi()?.unwrap_or(Abi::C)
} else {
Abi::Rust
};

let abi = self.parse_extern_abi()?;
self.expect_keyword(kw::Fn)?;
let inputs = self.parse_fn_params(false, true)?;
let ret_ty = self.parse_ret_ty(false)?;
let decl = P(FnDecl {
inputs,
output: ret_ty,
});
let cfg = super::ParamCfg {
is_self_allowed: false,
allow_c_variadic: true,
is_name_required: |_| false,
};
let decl = self.parse_fn_decl(cfg, false)?;
Ok(TyKind::BareFn(P(BareFnTy {
abi,
unsafety,
1 change: 1 addition & 0 deletions src/test/ui/parser/issue-33413.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ struct S;
impl S {
fn f(*, a: u8) -> u8 {}
//~^ ERROR expected parameter name, found `*`
//~| ERROR mismatched types
}

fn main() {}
14 changes: 13 additions & 1 deletion src/test/ui/parser/issue-33413.stderr
Original file line number Diff line number Diff line change
@@ -4,5 +4,17 @@ error: expected parameter name, found `*`
LL | fn f(*, a: u8) -> u8 {}
| ^ expected parameter name

error: aborting due to previous error
error[E0308]: mismatched types
--> $DIR/issue-33413.rs:4:23
|
LL | fn f(*, a: u8) -> u8 {}
| - ^^ expected u8, found ()
| |
| implicitly returns `()` as its body has no tail or `return` expression
|
= note: expected type `u8`
found type `()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.