Skip to content

Commit 1e1d574

Browse files
committed
expand: Share some code between inline and out-of-line module treatment
1 parent 29a9ef2 commit 1e1d574

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

compiler/rustc_expand/src/module.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,26 @@ crate fn mod_dir_path(
9898
module: &ModuleData,
9999
mut dir_ownership: DirOwnership,
100100
) -> (PathBuf, DirOwnership) {
101+
if let Some(file_path) = mod_file_path_from_attr(sess, attrs, &module.dir_path) {
102+
// For inline modules file path from `#[path]` is actually the directory path
103+
// for historical reasons, so we don't pop the last segment here.
104+
return (file_path, DirOwnership::Owned { relative: None });
105+
}
106+
107+
// We have to push on the current module name in the case of relative
108+
// paths in order to ensure that any additional module paths from inline
109+
// `mod x { ... }` come after the relative extension.
110+
//
111+
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
112+
// directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
101113
let mut dir_path = module.dir_path.clone();
102-
if let Some(file_path) = sess.first_attr_value_str_by_name(attrs, sym::path) {
103-
dir_path.push(&*file_path.as_str());
104-
dir_ownership = DirOwnership::Owned { relative: None };
105-
} else {
106-
// We have to push on the current module name in the case of relative
107-
// paths in order to ensure that any additional module paths from inline
108-
// `mod x { ... }` come after the relative extension.
109-
//
110-
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
111-
// directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
112-
if let DirOwnership::Owned { relative } = &mut dir_ownership {
113-
if let Some(ident) = relative.take() {
114-
// Remove the relative offset.
115-
dir_path.push(&*ident.as_str());
116-
}
114+
if let DirOwnership::Owned { relative } = &mut dir_ownership {
115+
if let Some(ident) = relative.take() {
116+
// Remove the relative offset.
117+
dir_path.push(&*ident.as_str());
117118
}
118-
dir_path.push(&*ident.as_str());
119119
}
120+
dir_path.push(&*ident.as_str());
120121

121122
(dir_path, dir_ownership)
122123
}
@@ -179,8 +180,7 @@ fn mod_file_path_from_attr(
179180
dir_path: &Path,
180181
) -> Option<PathBuf> {
181182
// Extract path string from first `#[path = "path_string"]` attribute.
182-
let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?;
183-
let path_string = path_string.as_str();
183+
let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?.as_str();
184184

185185
// On windows, the base path might have the form
186186
// `\\?\foo\bar` in which case it does not tolerate

0 commit comments

Comments
 (0)