about summary refs log tree commit diff
path: root/compiler/rustc_expand/src/module.rs
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2021-02-22 20:56:49 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2021-03-05 01:33:43 +0300
commit1e1d574aeac3fae44dac430ecfe8086abcdf9a5f (patch)
treecc2d10f3e200bff6fcb5e1cf6a974fc86f3ed74b /compiler/rustc_expand/src/module.rs
parent29a9ef28188e2ebdd3c56177672cd437dd11e25d (diff)
downloadrust-1e1d574aeac3fae44dac430ecfe8086abcdf9a5f.tar.gz
rust-1e1d574aeac3fae44dac430ecfe8086abcdf9a5f.zip
expand: Share some code between inline and out-of-line module treatment
Diffstat (limited to 'compiler/rustc_expand/src/module.rs')
-rw-r--r--compiler/rustc_expand/src/module.rs36
1 files changed, 18 insertions, 18 deletions
diff --git a/compiler/rustc_expand/src/module.rs b/compiler/rustc_expand/src/module.rs
index 607c68f82df..8f5200804e0 100644
--- a/compiler/rustc_expand/src/module.rs
+++ b/compiler/rustc_expand/src/module.rs
@@ -98,25 +98,26 @@ crate fn mod_dir_path(
     module: &ModuleData,
     mut dir_ownership: DirOwnership,
 ) -> (PathBuf, DirOwnership) {
+    if let Some(file_path) = mod_file_path_from_attr(sess, attrs, &module.dir_path) {
+        // For inline modules file path from `#[path]` is actually the directory path
+        // for historical reasons, so we don't pop the last segment here.
+        return (file_path, DirOwnership::Owned { relative: None });
+    }
+
+    // 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`.
     let mut dir_path = module.dir_path.clone();
-    if let Some(file_path) = sess.first_attr_value_str_by_name(attrs, sym::path) {
-        dir_path.push(&*file_path.as_str());
-        dir_ownership = DirOwnership::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 DirOwnership::Owned { relative } = &mut dir_ownership {
-            if let Some(ident) = relative.take() {
-                // Remove the relative offset.
-                dir_path.push(&*ident.as_str());
-            }
+    if let DirOwnership::Owned { relative } = &mut dir_ownership {
+        if let Some(ident) = relative.take() {
+            // Remove the relative offset.
+            dir_path.push(&*ident.as_str());
         }
-        dir_path.push(&*ident.as_str());
     }
+    dir_path.push(&*ident.as_str());
 
     (dir_path, dir_ownership)
 }
@@ -179,8 +180,7 @@ fn mod_file_path_from_attr(
     dir_path: &Path,
 ) -> Option<PathBuf> {
     // Extract path string from first `#[path = "path_string"]` attribute.
-    let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?;
-    let path_string = path_string.as_str();
+    let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?.as_str();
 
     // On windows, the base path might have the form
     // `\\?\foo\bar` in which case it does not tolerate