Skip to content

Commit f46f4b5

Browse files
authored
Merge pull request #2675 from flodiebold/non-modrs-mods
Fix handling of modules in non_modrs_mods style
2 parents bd7ae5e + e65aa30 commit f46f4b5

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

src/modules.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::path::{Path, PathBuf};
1414

1515
use syntax::ast;
1616
use syntax::codemap::{self, FileName};
17-
use syntax::parse::parser;
17+
use syntax::parse::{parser, DirectoryOwnership};
1818

1919
use utils::contains_skip;
2020

@@ -31,7 +31,7 @@ pub fn list_files<'a>(
3131
FileName::Real(ref path) => path.parent().unwrap(),
3232
_ => Path::new(""),
3333
};
34-
list_submodules(&krate.module, parent, codemap, &mut result)?;
34+
list_submodules(&krate.module, parent, None, codemap, &mut result)?;
3535
}
3636
result.insert(root_filename, &krate.module);
3737
Ok(result)
@@ -41,6 +41,7 @@ pub fn list_files<'a>(
4141
fn list_submodules<'a>(
4242
module: &'a ast::Mod,
4343
search_dir: &Path,
44+
relative: Option<ast::Ident>,
4445
codemap: &codemap::CodeMap,
4546
result: &mut BTreeMap<FileName, &'a ast::Mod>,
4647
) -> Result<(), io::Error> {
@@ -50,15 +51,16 @@ fn list_submodules<'a>(
5051
if !contains_skip(&item.attrs) {
5152
let is_internal =
5253
codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
53-
let dir_path = if is_internal {
54-
search_dir.join(&item.ident.to_string())
54+
let (dir_path, relative) = if is_internal {
55+
(search_dir.join(&item.ident.to_string()), None)
5556
} else {
56-
let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap)?;
57+
let (mod_path, relative) =
58+
module_file(item.ident, &item.attrs, search_dir, relative, codemap)?;
5759
let dir_path = mod_path.parent().unwrap().to_owned();
5860
result.insert(FileName::Real(mod_path), sub_mod);
59-
dir_path
61+
(dir_path, relative)
6062
};
61-
list_submodules(sub_mod, &dir_path, codemap, result)?;
63+
list_submodules(sub_mod, &dir_path, relative, codemap, result)?;
6264
}
6365
}
6466
}
@@ -70,14 +72,26 @@ fn module_file(
7072
id: ast::Ident,
7173
attrs: &[ast::Attribute],
7274
dir_path: &Path,
75+
relative: Option<ast::Ident>,
7376
codemap: &codemap::CodeMap,
74-
) -> Result<PathBuf, io::Error> {
77+
) -> Result<(PathBuf, Option<ast::Ident>), io::Error> {
7578
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
76-
return Ok(path);
79+
return Ok((path, None));
7780
}
7881

79-
match parser::Parser::default_submod_path(id, None, dir_path, codemap).result {
80-
Ok(parser::ModulePathSuccess { path, .. }) => Ok(path),
82+
match parser::Parser::default_submod_path(id, relative, dir_path, codemap).result {
83+
Ok(parser::ModulePathSuccess {
84+
path,
85+
directory_ownership,
86+
..
87+
}) => {
88+
let relative = if let DirectoryOwnership::Owned { relative } = directory_ownership {
89+
relative
90+
} else {
91+
None
92+
};
93+
Ok((path, relative))
94+
}
8195
Err(_) => Err(io::Error::new(
8296
io::ErrorKind::Other,
8397
format!("Couldn't find module {}", id),

tests/config/skip_children.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
skip_children = true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-config: skip_children.toml
2+
mod bar;
3+
4+
mod baz {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn dummy() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(non_modrs_mods)]
2+
3+
// Test that submodules in non-mod.rs files work. This is just an idempotence
4+
// test since we just want to verify that rustfmt doesn't fail.
5+
6+
mod foo;

0 commit comments

Comments
 (0)