about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2020-03-08 09:54:19 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2020-03-18 15:08:25 +0100
commit53a633fb445af045493f832fdce4f9d9ce48fbd4 (patch)
treeafc7295706bfbbf7419db622e670db13b1b9a8e5
parentca098b16a4114fd96a4059ba3f807d33dde5ef07 (diff)
downloadrust-53a633fb445af045493f832fdce4f9d9ce48fbd4.tar.gz
rust-53a633fb445af045493f832fdce4f9d9ce48fbd4.zip
decouple push_directory from Parser
-rw-r--r--src/librustc_parse/parser/module.rs41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/librustc_parse/parser/module.rs b/src/librustc_parse/parser/module.rs
index a30d6da281a..53c0c9154bc 100644
--- a/src/librustc_parse/parser/module.rs
+++ b/src/librustc_parse/parser/module.rs
@@ -58,7 +58,7 @@ impl<'a> Parser<'a> {
             }
         } else {
             let old_directory = self.directory.clone();
-            self.push_directory(id, &attrs);
+            push_directory(id, &attrs, &mut self.directory.ownership, &mut self.directory.path);
 
             self.expect(&token::OpenDelim(token::Brace))?;
             let module = self.parse_mod(&token::CloseDelim(token::Brace))?;
@@ -142,26 +142,31 @@ impl<'a> Parser<'a> {
         }
         Ok(())
     }
+}
 
-    fn push_directory(&mut self, id: Ident, attrs: &[Attribute]) {
-        if let Some(path) = attr::first_attr_value_str_by_name(attrs, sym::path) {
-            self.directory.path.push(&*path.as_str());
-            self.directory.ownership = DirectoryOwnership::Owned { relative: None };
-        } else {
-            // We have to push on the current module name in the case of relative
-            // paths in order to ensure that any additional module paths from inline
-            // `mod x { ... }` come after the relative extension.
-            //
-            // For example, a `mod z { ... }` inside `x/y.rs` should set the current
-            // directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
-            if let DirectoryOwnership::Owned { relative } = &mut self.directory.ownership {
-                if let Some(ident) = relative.take() {
-                    // remove the relative offset
-                    self.directory.path.push(&*ident.as_str());
-                }
+fn push_directory(
+    id: Ident,
+    attrs: &[Attribute],
+    dir_ownership: &mut DirectoryOwnership,
+    dir_path: &mut PathBuf,
+) {
+    if let Some(path) = attr::first_attr_value_str_by_name(attrs, sym::path) {
+        dir_path.push(&*path.as_str());
+        *dir_ownership = DirectoryOwnership::Owned { relative: None };
+    } else {
+        // We have to push on the current module name in the case of relative
+        // paths in order to ensure that any additional module paths from inline
+        // `mod x { ... }` come after the relative extension.
+        //
+        // For example, a `mod z { ... }` inside `x/y.rs` should set the current
+        // directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
+        if let DirectoryOwnership::Owned { relative } = dir_ownership {
+            if let Some(ident) = relative.take() {
+                // Remove the relative offset.
+                dir_path.push(&*ident.as_str());
             }
-            self.directory.path.push(&*id.as_str());
         }
+        dir_path.push(&*id.as_str());
     }
 }