Skip to content

Commit 7672bff

Browse files
committed
Unify associated function parsing.
1 parent 10270bc commit 7672bff

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::maybe_whole;
66
use rustc_errors::{PResult, Applicability, DiagnosticBuilder, StashKey};
77
use rustc_error_codes::*;
88
use syntax::ast::{self, DUMMY_NODE_ID, Ident, Attribute, AttrKind, AttrStyle, AnonConst, Item};
9-
use syntax::ast::{ItemKind, ImplItem, ImplItemKind, TraitItem, TraitItemKind, UseTree, UseTreeKind};
9+
use syntax::ast::{ItemKind, ImplItem, TraitItem, TraitItemKind, UseTree, UseTreeKind};
1010
use syntax::ast::{AssocItemKind};
1111
use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
1212
use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
@@ -705,7 +705,7 @@ impl<'a> Parser<'a> {
705705
// FIXME: code copied from `parse_macro_use_or_failure` -- use abstraction!
706706
(Ident::invalid(), ast::ImplItemKind::Macro(mac), Generics::default())
707707
} else {
708-
self.parse_impl_method(at_end, &mut attrs)?
708+
self.parse_assoc_fn(at_end, &mut attrs, |_| true)?
709709
};
710710

711711
Ok(ImplItem {
@@ -876,7 +876,11 @@ impl<'a> Parser<'a> {
876876
// trait item macro.
877877
(Ident::invalid(), TraitItemKind::Macro(mac), Generics::default())
878878
} else {
879-
self.parse_trait_item_method(at_end, &mut attrs)?
879+
// This is somewhat dubious; We don't want to allow
880+
// param names to be left off if there is a definition...
881+
//
882+
// We don't allow param names to be left off in edition 2018.
883+
self.parse_assoc_fn(at_end, &mut attrs, |t| t.span.rust_2018())?
880884
};
881885

882886
Ok(TraitItem {
@@ -1823,48 +1827,40 @@ impl<'a> Parser<'a> {
18231827
})
18241828
}
18251829

1826-
/// Parses a method or a macro invocation in a trait impl.
1827-
fn parse_impl_method(
1828-
&mut self,
1829-
at_end: &mut bool,
1830-
attrs: &mut Vec<Attribute>,
1831-
) -> PResult<'a, (Ident, ImplItemKind, Generics)> {
1832-
let (ident, sig, generics) = self.parse_method_sig(|_| true)?;
1833-
let body = self.parse_trait_method_body(at_end, attrs)?;
1834-
Ok((ident, ast::ImplItemKind::Method(sig, body), generics))
1835-
}
1836-
1837-
fn parse_trait_item_method(
1830+
fn parse_assoc_fn(
18381831
&mut self,
18391832
at_end: &mut bool,
18401833
attrs: &mut Vec<Attribute>,
1841-
) -> PResult<'a, (Ident, TraitItemKind, Generics)> {
1842-
// This is somewhat dubious; We don't want to allow
1843-
// argument names to be left off if there is a definition...
1844-
//
1845-
// We don't allow argument names to be left off in edition 2018.
1846-
let (ident, sig, generics) = self.parse_method_sig(|t| t.span.rust_2018())?;
1847-
let body = self.parse_trait_method_body(at_end, attrs)?;
1848-
Ok((ident, TraitItemKind::Method(sig, body), generics))
1834+
is_name_required: fn(&token::Token) -> bool,
1835+
) -> PResult<'a, (Ident, AssocItemKind, Generics)> {
1836+
let header = self.parse_fn_front_matter()?;
1837+
let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
1838+
is_self_allowed: true,
1839+
allow_c_variadic: false,
1840+
is_name_required,
1841+
})?;
1842+
let sig = FnSig { header, decl };
1843+
let body = self.parse_assoc_fn_body(at_end, attrs)?;
1844+
Ok((ident, AssocItemKind::Method(sig, body), generics))
18491845
}
18501846

1851-
/// Parse the "body" of a method in a trait item definition.
1847+
/// Parse the "body" of a method in an associated item definition.
18521848
/// This can either be `;` when there's no body,
18531849
/// or e.g. a block when the method is a provided one.
1854-
fn parse_trait_method_body(
1850+
fn parse_assoc_fn_body(
18551851
&mut self,
18561852
at_end: &mut bool,
18571853
attrs: &mut Vec<Attribute>,
18581854
) -> PResult<'a, Option<P<Block>>> {
18591855
Ok(match self.token.kind {
18601856
token::Semi => {
1861-
debug!("parse_trait_method_body(): parsing required method");
1857+
debug!("parse_assoc_fn_body(): parsing required method");
18621858
self.bump();
18631859
*at_end = true;
18641860
None
18651861
}
18661862
token::OpenDelim(token::Brace) => {
1867-
debug!("parse_trait_method_body(): parsing provided method");
1863+
debug!("parse_assoc_fn_body(): parsing provided method");
18681864
*at_end = true;
18691865
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
18701866
attrs.extend(inner_attrs.iter().cloned());
@@ -1885,21 +1881,6 @@ impl<'a> Parser<'a> {
18851881
})
18861882
}
18871883

1888-
/// Parse the "signature", including the identifier, parameters, and generics
1889-
/// of a method. The body is not parsed as that differs between `trait`s and `impl`s.
1890-
fn parse_method_sig(
1891-
&mut self,
1892-
is_name_required: fn(&token::Token) -> bool,
1893-
) -> PResult<'a, (Ident, FnSig, Generics)> {
1894-
let header = self.parse_fn_front_matter()?;
1895-
let (ident, decl, generics) = self.parse_fn_sig(ParamCfg {
1896-
is_self_allowed: true,
1897-
allow_c_variadic: false,
1898-
is_name_required,
1899-
})?;
1900-
Ok((ident, FnSig { header, decl }, generics))
1901-
}
1902-
19031884
/// Parses all the "front matter" for a `fn` declaration, up to
19041885
/// and including the `fn` keyword:
19051886
///

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ pub enum AssocItemKind {
16401640
Const(P<Ty>, Option<P<Expr>>),
16411641

16421642
/// An associated function.
1643+
/// FIXME(Centril): Rename to `Fn`.
16431644
Method(FnSig, Option<P<Block>>),
16441645

16451646
/// An associated type.

0 commit comments

Comments
 (0)