From 48961c8861eb0eb7b4d3f101072ed13b55e3be45 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Thu, 21 May 2020 12:39:12 +0200 Subject: [PATCH] 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 --- .../rustfmt-lib/src/formatting/modules.rs | 17 +---------------- .../tests/target/inner-module-path/b.rs | 1 + .../tests/target/inner-module-path/c/d.rs | 1 + .../tests/target/inner-module-path/lib.rs | 10 ++++++++++ 4 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 rustfmt-core/rustfmt-lib/tests/target/inner-module-path/b.rs create mode 100644 rustfmt-core/rustfmt-lib/tests/target/inner-module-path/c/d.rs create mode 100644 rustfmt-core/rustfmt-lib/tests/target/inner-module-path/lib.rs diff --git a/rustfmt-core/rustfmt-lib/src/formatting/modules.rs b/rustfmt-core/rustfmt-lib/src/formatting/modules.rs index cb49b62094c..eee19502906 100644 --- a/rustfmt-core/rustfmt-lib/src/formatting/modules.rs +++ b/rustfmt-core/rustfmt-lib/src/formatting/modules.rs @@ -37,8 +37,6 @@ enum SubModKind<'a, 'ast> { External(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>), /// `mod foo;` with multiple sources. MultiExternal(Vec<(PathBuf, DirectoryOwnership, Cow<'ast, ast::Mod>)>), - /// `#[path = "..."] mod foo {}` - InternalWithPath(PathBuf), /// `mod foo {}` Internal(&'a ast::Item), } @@ -154,12 +152,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { self.find_external_module(item.ident, &item.attrs, sub_mod) } else { // An internal module (`mod foo { /* ... */ }`); - if let Some(path) = find_path_value(&item.attrs) { - let path = Path::new(&*path.as_str()).to_path_buf(); - Ok(Some(SubModKind::InternalWithPath(path))) - } else { - Ok(Some(SubModKind::Internal(item))) - } + Ok(Some(SubModKind::Internal(item))) } } @@ -199,14 +192,6 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { }; self.visit_sub_mod_after_directory_update(sub_mod, Some(directory)) } - SubModKind::InternalWithPath(mod_path) => { - // All `#[path]` files are treated as though they are a `mod.rs` file. - let directory = Directory { - path: mod_path, - ownership: DirectoryOwnership::Owned { relative: None }, - }; - self.visit_sub_mod_after_directory_update(sub_mod, Some(directory)) - } SubModKind::Internal(ref item) => { self.push_inline_mod_directory(item.ident, &item.attrs); self.visit_sub_mod_after_directory_update(sub_mod, None) diff --git a/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/b.rs b/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/b.rs new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/b.rs @@ -0,0 +1 @@ + diff --git a/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/c/d.rs b/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/c/d.rs new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/c/d.rs @@ -0,0 +1 @@ + diff --git a/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/lib.rs b/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/lib.rs new file mode 100644 index 00000000000..fb75d70cd93 --- /dev/null +++ b/rustfmt-core/rustfmt-lib/tests/target/inner-module-path/lib.rs @@ -0,0 +1,10 @@ +// rustfmt-recursive: true + +#[path = "."] +mod a { + mod b; +} + +mod c { + mod d; +}