Skip to content

Commit 51ccdeb

Browse files
committed
Unify associated item parsing more.
1 parent 0d8a9d7 commit 51ccdeb

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -669,25 +669,6 @@ impl<'a> Parser<'a> {
669669
Ok((impl_items, attrs))
670670
}
671671

672-
/// Parses an impl item.
673-
pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
674-
maybe_whole!(self, NtImplItem, |x| x);
675-
let attrs = self.parse_outer_attributes()?;
676-
let mut unclosed_delims = vec![];
677-
let (mut item, tokens) = self.collect_tokens(|this| {
678-
let item = this.parse_assoc_item(at_end, attrs, |_| true);
679-
unclosed_delims.append(&mut this.unclosed_delims);
680-
item
681-
})?;
682-
self.unclosed_delims.append(&mut unclosed_delims);
683-
684-
// See `parse_item` for why this clause is here.
685-
if !item.attrs.iter().any(|attr| attr.style == AttrStyle::Inner) {
686-
item.tokens = Some(tokens);
687-
}
688-
Ok(item)
689-
}
690-
691672
/// Parses defaultness (i.e., `default` or nothing).
692673
fn parse_defaultness(&mut self) -> Defaultness {
693674
// `pub` is included for better error messages
@@ -802,20 +783,30 @@ impl<'a> Parser<'a> {
802783
}
803784
}
804785

805-
/// Parses the items in a trait declaration.
786+
pub fn parse_impl_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
787+
maybe_whole!(self, NtImplItem, |x| x);
788+
self.parse_assoc_item(at_end, |_| true)
789+
}
790+
806791
pub fn parse_trait_item(&mut self, at_end: &mut bool) -> PResult<'a, AssocItem> {
807792
maybe_whole!(self, NtTraitItem, |x| x);
793+
// This is somewhat dubious; We don't want to allow
794+
// param names to be left off if there is a definition...
795+
//
796+
// We don't allow param names to be left off in edition 2018.
797+
self.parse_assoc_item(at_end, |t| t.span.rust_2018())
798+
}
799+
800+
/// Parses associated items.
801+
fn parse_assoc_item(
802+
&mut self,
803+
at_end: &mut bool,
804+
is_name_required: fn(&token::Token) -> bool,
805+
) -> PResult<'a, AssocItem> {
808806
let attrs = self.parse_outer_attributes()?;
809807
let mut unclosed_delims = vec![];
810808
let (mut item, tokens) = self.collect_tokens(|this| {
811-
// This is somewhat dubious; We don't want to allow
812-
// param names to be left off if there is a definition...
813-
//
814-
// We don't allow param names to be left off in edition 2018.
815-
//
816-
// FIXME(Centril): bake closure into param parsing.
817-
// Also add semantic restrictions and add tests.
818-
let item = this.parse_assoc_item(at_end, attrs, |t| t.span.rust_2018());
809+
let item = this.parse_assoc_item_(at_end, attrs, is_name_required);
819810
unclosed_delims.append(&mut this.unclosed_delims);
820811
item
821812
})?;
@@ -827,7 +818,7 @@ impl<'a> Parser<'a> {
827818
Ok(item)
828819
}
829820

830-
fn parse_assoc_item(
821+
fn parse_assoc_item_(
831822
&mut self,
832823
at_end: &mut bool,
833824
mut attrs: Vec<Attribute>,

0 commit comments

Comments
 (0)