From 45b9569186aa74044285d9c281943c07857d411d Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Tue, 19 Oct 2021 01:14:51 -0400 Subject: [PATCH] Retain trailing comments in module when using rustfmt::skip attribute Resolves 5033 Trailing comments at the end of the root Module were removed because the module span did not extend until the end of the file. The root Module's span now encompasses the entire file, which ensures that no comments are lost when using ``#![rustfmt::skip]`` --- src/modules.rs | 6 ++++-- tests/target/issue-5033/minimum_example.rs | 8 ++++++++ tests/target/issue-5033/nested_modules.rs | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/target/issue-5033/minimum_example.rs create mode 100644 tests/target/issue-5033/nested_modules.rs diff --git a/src/modules.rs b/src/modules.rs index 5de0575b5cd..e13cbd5ecaa 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -16,7 +16,7 @@ use crate::syntux::parser::{ Directory, DirectoryOwnership, ModError, ModulePathSuccess, Parser, ParserError, }; use crate::syntux::session::ParseSess; -use crate::utils::contains_skip; +use crate::utils::{contains_skip, mk_sp}; mod visitor; @@ -137,10 +137,12 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> { self.visit_mod_from_ast(&krate.items)?; } + let snippet_provider = self.parse_sess.snippet_provider(krate.span); + self.file_map.insert( root_filename, Module::new( - krate.span, + mk_sp(snippet_provider.start_pos(), snippet_provider.end_pos()), None, Cow::Borrowed(&krate.items), Cow::Borrowed(&krate.attrs), diff --git a/tests/target/issue-5033/minimum_example.rs b/tests/target/issue-5033/minimum_example.rs new file mode 100644 index 00000000000..0e7df41deb2 --- /dev/null +++ b/tests/target/issue-5033/minimum_example.rs @@ -0,0 +1,8 @@ +// leading comment + +#![rustfmt::skip] +fn main() { + println!("main"); // commented +} + +// post comment diff --git a/tests/target/issue-5033/nested_modules.rs b/tests/target/issue-5033/nested_modules.rs new file mode 100644 index 00000000000..7a11133b60b --- /dev/null +++ b/tests/target/issue-5033/nested_modules.rs @@ -0,0 +1,11 @@ +#![rustfmt::skip] + +mod a { + mod b { + + } + + // trailing comment b +} + +// trailing comment a