about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2020-02-23 11:52:57 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2020-02-24 00:59:38 +0100
commite66a39bb65eb4a7fb1813993e10fc1af5bdac9bc (patch)
treec75175d4a7f6765c01c19ff515e95cfd6b4d5a51
parent62930d31514d6d9b4b0df3755d643f06f0d4212a (diff)
downloadrust-e66a39bb65eb4a7fb1813993e10fc1af5bdac9bc.tar.gz
rust-e66a39bb65eb4a7fb1813993e10fc1af5bdac9bc.zip
parse: tweak `parse_item_` for more reuse.
-rw-r--r--src/librustc_parse/parser/item.rs24
-rw-r--r--src/librustc_parse/parser/stmt.rs4
2 files changed, 9 insertions, 19 deletions
diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs
index 451f1fdf14a..c85b4c22d01 100644
--- a/src/librustc_parse/parser/item.rs
+++ b/src/librustc_parse/parser/item.rs
@@ -25,21 +25,15 @@ pub(super) type ItemInfo = (Ident, ItemKind);
 
 impl<'a> Parser<'a> {
     pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
-        let attrs = self.parse_outer_attributes()?;
-        self.parse_item_(attrs, true, false)
+        self.parse_item_(|_| true).map(|i| i.map(P))
     }
 
-    pub(super) fn parse_item_(
-        &mut self,
-        attrs: Vec<Attribute>,
-        macros_allowed: bool,
-        attributes_allowed: bool,
-    ) -> PResult<'a, Option<P<Item>>> {
-        let item = self.parse_item_common(attrs, macros_allowed, attributes_allowed, |_| true)?;
-        Ok(item.map(P))
+    fn parse_item_(&mut self, req_name: ReqName) -> PResult<'a, Option<Item>> {
+        let attrs = self.parse_outer_attributes()?;
+        self.parse_item_common(attrs, true, false, req_name)
     }
 
-    fn parse_item_common(
+    pub(super) fn parse_item_common(
         &mut self,
         mut attrs: Vec<Attribute>,
         mac_allowed: bool,
@@ -653,9 +647,7 @@ impl<'a> Parser<'a> {
 
     /// Parses associated items.
     fn parse_assoc_item(&mut self, req_name: ReqName) -> PResult<'a, Option<Option<P<AssocItem>>>> {
-        let attrs = self.parse_outer_attributes()?;
-        let it = self.parse_item_common(attrs, true, false, req_name)?;
-        Ok(it.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
+        Ok(self.parse_item_(req_name)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
             let kind = match kind {
                 ItemKind::Mac(a) => AssocItemKind::Macro(a),
                 ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
@@ -844,9 +836,7 @@ impl<'a> Parser<'a> {
     pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
         maybe_whole!(self, NtForeignItem, |item| Some(Some(item)));
 
-        let attrs = self.parse_outer_attributes()?;
-        let item = self.parse_item_common(attrs, true, false, |_| true)?;
-        Ok(item.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
+        Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
             let kind = match kind {
                 ItemKind::Mac(a) => ForeignItemKind::Macro(a),
                 ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),
diff --git a/src/librustc_parse/parser/stmt.rs b/src/librustc_parse/parser/stmt.rs
index 0ce0e0df66a..bbfbe9c20df 100644
--- a/src/librustc_parse/parser/stmt.rs
+++ b/src/librustc_parse/parser/stmt.rs
@@ -81,11 +81,11 @@ impl<'a> Parser<'a> {
         // FIXME: Bad copy of attrs
         let old_directory_ownership =
             mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
-        let item = self.parse_item_(attrs.clone(), false, true)?;
+        let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
         self.directory.ownership = old_directory_ownership;
 
         if let Some(item) = item {
-            return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item))));
+            return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))));
         }
 
         // Do not attempt to parse an expression if we're done here.