From d2f2237d3127f2da13d6bc610de2af09253c531a Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 17 Jun 2021 22:35:19 -0500 Subject: [PATCH] fix(rustfmt): load nested out-of-line mods correctly --- src/tools/rustfmt/src/modules.rs | 2 +- src/tools/rustfmt/src/test/mod.rs | 1 + src/tools/rustfmt/src/test/mod_resolver.rs | 25 +++++++++++++++++++ .../tests/mod-resolver/issue-4874/bar/baz.rs | 5 ++++ .../tests/mod-resolver/issue-4874/foo.rs | 1 + .../tests/mod-resolver/issue-4874/foo/qux.rs | 5 ++++ .../tests/mod-resolver/issue-4874/main.rs | 8 ++++++ 7 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/tools/rustfmt/src/test/mod_resolver.rs create mode 100644 src/tools/rustfmt/tests/mod-resolver/issue-4874/bar/baz.rs create mode 100644 src/tools/rustfmt/tests/mod-resolver/issue-4874/foo.rs create mode 100644 src/tools/rustfmt/tests/mod-resolver/issue-4874/foo/qux.rs create mode 100644 src/tools/rustfmt/tests/mod-resolver/issue-4874/main.rs diff --git a/src/tools/rustfmt/src/modules.rs b/src/tools/rustfmt/src/modules.rs index c3f4406860135..5de0575b5cd66 100644 --- a/src/tools/rustfmt/src/modules.rs +++ b/src/tools/rustfmt/src/modules.rs @@ -318,7 +318,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { self.directory = directory; } match (sub_mod.ast_mod_kind, sub_mod.items) { - (Some(Cow::Borrowed(ast::ModKind::Loaded(items, ast::Inline::No, _))), _) => { + (Some(Cow::Borrowed(ast::ModKind::Loaded(items, _, _))), _) => { self.visit_mod_from_ast(&items) } (Some(Cow::Owned(..)), Cow::Owned(items)) => self.visit_mod_outside_ast(items), diff --git a/src/tools/rustfmt/src/test/mod.rs b/src/tools/rustfmt/src/test/mod.rs index ce56a223f2b04..cb52346a13a41 100644 --- a/src/tools/rustfmt/src/test/mod.rs +++ b/src/tools/rustfmt/src/test/mod.rs @@ -16,6 +16,7 @@ use crate::source_file; use crate::{is_nightly_channel, FormatReport, FormatReportFormatterBuilder, Input, Session}; mod configuration_snippet; +mod mod_resolver; mod parser; const DIFF_CONTEXT_SIZE: usize = 3; diff --git a/src/tools/rustfmt/src/test/mod_resolver.rs b/src/tools/rustfmt/src/test/mod_resolver.rs new file mode 100644 index 0000000000000..e0b55e3efb2c4 --- /dev/null +++ b/src/tools/rustfmt/src/test/mod_resolver.rs @@ -0,0 +1,25 @@ +use std::io; +use std::path::PathBuf; + +use super::read_config; + +use crate::{FileName, Input, Session}; + +#[test] +fn nested_out_of_line_mods_loaded() { + // See also https://github.com/rust-lang/rustfmt/issues/4874 + let filename = "tests/mod-resolver/issue-4874/main.rs"; + let input_file = PathBuf::from(filename); + let config = read_config(&input_file); + let mut session = Session::::new(config, None); + let report = session + .format(Input::File(filename.into())) + .expect("Should not have had any execution errors"); + let errors_by_file = &report.internal.borrow().0; + assert!(errors_by_file.contains_key(&FileName::Real(PathBuf::from( + "tests/mod-resolver/issue-4874/bar/baz.rs", + )))); + assert!(errors_by_file.contains_key(&FileName::Real(PathBuf::from( + "tests/mod-resolver/issue-4874/foo/qux.rs", + )))); +} diff --git a/src/tools/rustfmt/tests/mod-resolver/issue-4874/bar/baz.rs b/src/tools/rustfmt/tests/mod-resolver/issue-4874/bar/baz.rs new file mode 100644 index 0000000000000..d31b675ea260d --- /dev/null +++ b/src/tools/rustfmt/tests/mod-resolver/issue-4874/bar/baz.rs @@ -0,0 +1,5 @@ +fn + fail_fmt_check + ( + + ) {} \ No newline at end of file diff --git a/src/tools/rustfmt/tests/mod-resolver/issue-4874/foo.rs b/src/tools/rustfmt/tests/mod-resolver/issue-4874/foo.rs new file mode 100644 index 0000000000000..246d847869a12 --- /dev/null +++ b/src/tools/rustfmt/tests/mod-resolver/issue-4874/foo.rs @@ -0,0 +1 @@ +mod qux; \ No newline at end of file diff --git a/src/tools/rustfmt/tests/mod-resolver/issue-4874/foo/qux.rs b/src/tools/rustfmt/tests/mod-resolver/issue-4874/foo/qux.rs new file mode 100644 index 0000000000000..d8bb610a64db1 --- /dev/null +++ b/src/tools/rustfmt/tests/mod-resolver/issue-4874/foo/qux.rs @@ -0,0 +1,5 @@ + fn + badly_formatted + ( + + ) {} \ No newline at end of file diff --git a/src/tools/rustfmt/tests/mod-resolver/issue-4874/main.rs b/src/tools/rustfmt/tests/mod-resolver/issue-4874/main.rs new file mode 100644 index 0000000000000..3609415b1468b --- /dev/null +++ b/src/tools/rustfmt/tests/mod-resolver/issue-4874/main.rs @@ -0,0 +1,8 @@ +fn main() { + println!("Hello, world!"); +} + +mod foo; +mod bar { + mod baz; +} \ No newline at end of file