Skip to content

Fix handling of modules in non_modrs_mods style #2675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::path::{Path, PathBuf};

use syntax::ast;
use syntax::codemap::{self, FileName};
use syntax::parse::parser;
use syntax::parse::{parser, DirectoryOwnership};

use utils::contains_skip;

Expand All @@ -31,7 +31,7 @@ pub fn list_files<'a>(
FileName::Real(ref path) => path.parent().unwrap(),
_ => Path::new(""),
};
list_submodules(&krate.module, parent, codemap, &mut result)?;
list_submodules(&krate.module, parent, None, codemap, &mut result)?;
}
result.insert(root_filename, &krate.module);
Ok(result)
Expand All @@ -41,6 +41,7 @@ pub fn list_files<'a>(
fn list_submodules<'a>(
module: &'a ast::Mod,
search_dir: &Path,
relative: Option<ast::Ident>,
codemap: &codemap::CodeMap,
result: &mut BTreeMap<FileName, &'a ast::Mod>,
) -> Result<(), io::Error> {
Expand All @@ -50,15 +51,16 @@ fn list_submodules<'a>(
if !contains_skip(&item.attrs) {
let is_internal =
codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
let dir_path = if is_internal {
search_dir.join(&item.ident.to_string())
let (dir_path, relative) = if is_internal {
(search_dir.join(&item.ident.to_string()), None)
} else {
let mod_path = module_file(item.ident, &item.attrs, search_dir, codemap)?;
let (mod_path, relative) =
module_file(item.ident, &item.attrs, search_dir, relative, codemap)?;
let dir_path = mod_path.parent().unwrap().to_owned();
result.insert(FileName::Real(mod_path), sub_mod);
dir_path
(dir_path, relative)
};
list_submodules(sub_mod, &dir_path, codemap, result)?;
list_submodules(sub_mod, &dir_path, relative, codemap, result)?;
}
}
}
Expand All @@ -70,14 +72,26 @@ fn module_file(
id: ast::Ident,
attrs: &[ast::Attribute],
dir_path: &Path,
relative: Option<ast::Ident>,
codemap: &codemap::CodeMap,
) -> Result<PathBuf, io::Error> {
) -> Result<(PathBuf, Option<ast::Ident>), io::Error> {
if let Some(path) = parser::Parser::submod_path_from_attr(attrs, dir_path) {
return Ok(path);
return Ok((path, None));
}

match parser::Parser::default_submod_path(id, None, dir_path, codemap).result {
Ok(parser::ModulePathSuccess { path, .. }) => Ok(path),
match parser::Parser::default_submod_path(id, relative, dir_path, codemap).result {
Ok(parser::ModulePathSuccess {
path,
directory_ownership,
..
}) => {
let relative = if let DirectoryOwnership::Owned { relative } = directory_ownership {
relative
} else {
None
};
Ok((path, relative))
}
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
format!("Couldn't find module {}", id),
Expand Down
1 change: 1 addition & 0 deletions tests/config/skip_children.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
skip_children = true
4 changes: 4 additions & 0 deletions tests/target/issue-2673-nonmodrs-mods/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-config: skip_children.toml
mod bar;

mod baz {}
1 change: 1 addition & 0 deletions tests/target/issue-2673-nonmodrs-mods/foo/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn dummy() {}
6 changes: 6 additions & 0 deletions tests/target/issue-2673-nonmodrs-mods/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(non_modrs_mods)]

// Test that submodules in non-mod.rs files work. This is just an idempotence
// test since we just want to verify that rustfmt doesn't fail.

mod foo;