Skip to content

Commit e66a39b

Browse files
committed
parse: tweak parse_item_ for more reuse.
1 parent 62930d3 commit e66a39b

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/librustc_parse/parser/item.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,15 @@ pub(super) type ItemInfo = (Ident, ItemKind);
2525

2626
impl<'a> Parser<'a> {
2727
pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
28-
let attrs = self.parse_outer_attributes()?;
29-
self.parse_item_(attrs, true, false)
28+
self.parse_item_(|_| true).map(|i| i.map(P))
3029
}
3130

32-
pub(super) fn parse_item_(
33-
&mut self,
34-
attrs: Vec<Attribute>,
35-
macros_allowed: bool,
36-
attributes_allowed: bool,
37-
) -> PResult<'a, Option<P<Item>>> {
38-
let item = self.parse_item_common(attrs, macros_allowed, attributes_allowed, |_| true)?;
39-
Ok(item.map(P))
31+
fn parse_item_(&mut self, req_name: ReqName) -> PResult<'a, Option<Item>> {
32+
let attrs = self.parse_outer_attributes()?;
33+
self.parse_item_common(attrs, true, false, req_name)
4034
}
4135

42-
fn parse_item_common(
36+
pub(super) fn parse_item_common(
4337
&mut self,
4438
mut attrs: Vec<Attribute>,
4539
mac_allowed: bool,
@@ -653,9 +647,7 @@ impl<'a> Parser<'a> {
653647

654648
/// Parses associated items.
655649
fn parse_assoc_item(&mut self, req_name: ReqName) -> PResult<'a, Option<Option<P<AssocItem>>>> {
656-
let attrs = self.parse_outer_attributes()?;
657-
let it = self.parse_item_common(attrs, true, false, req_name)?;
658-
Ok(it.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
650+
Ok(self.parse_item_(req_name)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
659651
let kind = match kind {
660652
ItemKind::Mac(a) => AssocItemKind::Macro(a),
661653
ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
@@ -844,9 +836,7 @@ impl<'a> Parser<'a> {
844836
pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
845837
maybe_whole!(self, NtForeignItem, |item| Some(Some(item)));
846838

847-
let attrs = self.parse_outer_attributes()?;
848-
let item = self.parse_item_common(attrs, true, false, |_| true)?;
849-
Ok(item.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
839+
Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
850840
let kind = match kind {
851841
ItemKind::Mac(a) => ForeignItemKind::Macro(a),
852842
ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),

src/librustc_parse/parser/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ impl<'a> Parser<'a> {
8181
// FIXME: Bad copy of attrs
8282
let old_directory_ownership =
8383
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
84-
let item = self.parse_item_(attrs.clone(), false, true)?;
84+
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
8585
self.directory.ownership = old_directory_ownership;
8686

8787
if let Some(item) = item {
88-
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item))));
88+
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))));
8989
}
9090

9191
// Do not attempt to parse an expression if we're done here.

0 commit comments

Comments
 (0)