Skip to content

Commit 48961c8

Browse files
author
Julian Orth
committed
Fix module resolution in inner modules with paths
Path attributes with relative paths were previously not treated as relative to the containing file but as relative to the current working directory of rustfmt. The SubModKind enum had a dedicated variant for inner modules with paths. However, the ordinary variant for inner modules also handles the presence of path attributes and treats them correctly. Therefore the fix is to simply remove the dedicated enum variant. Fixes #3901 Fixes #4076
1 parent e952ff9 commit 48961c8

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

rustfmt-core/rustfmt-lib/src/formatting/modules.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ enum SubModKind<'a, 'ast> {
3737
External(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>),
3838
/// `mod foo;` with multiple sources.
3939
MultiExternal(Vec<(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>)>),
40-
/// `#[path = "..."] mod foo {}`
41-
InternalWithPath(PathBuf),
4240
/// `mod foo {}`
4341
Internal(&'a ast::Item),
4442
}
@@ -154,12 +152,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
154152
self.find_external_module(item.ident, &item.attrs, sub_mod)
155153
} else {
156154
// An internal module (`mod foo { /* ... */ }`);
157-
if let Some(path) = find_path_value(&item.attrs) {
158-
let path = Path::new(&*path.as_str()).to_path_buf();
159-
Ok(Some(SubModKind::InternalWithPath(path)))
160-
} else {
161-
Ok(Some(SubModKind::Internal(item)))
162-
}
155+
Ok(Some(SubModKind::Internal(item)))
163156
}
164157
}
165158

@@ -199,14 +192,6 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
199192
};
200193
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
201194
}
202-
SubModKind::InternalWithPath(mod_path) => {
203-
// All `#[path]` files are treated as though they are a `mod.rs` file.
204-
let directory = Directory {
205-
path: mod_path,
206-
ownership: DirectoryOwnership::Owned { relative: None },
207-
};
208-
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
209-
}
210195
SubModKind::Internal(ref item) => {
211196
self.push_inline_mod_directory(item.ident, &item.attrs);
212197
self.visit_sub_mod_after_directory_update(sub_mod, None)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-recursive: true
2+
3+
#[path = "."]
4+
mod a {
5+
mod b;
6+
}
7+
8+
mod c {
9+
mod d;
10+
}

0 commit comments

Comments
 (0)