about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYacin Tmimi <yacintmimi@gmail.com>2021-10-19 01:14:51 -0400
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-10-28 21:44:39 -0500
commitbc46af97423cef7484a806e8282c545d7607889e (patch)
tree51a94f2819d9fc187fa90abab60cd5ce23ca4e98
parent8b766f35bcf0b2c2dcd82bd4e4ec82af49c16367 (diff)
downloadrust-bc46af97423cef7484a806e8282c545d7607889e.tar.gz
rust-bc46af97423cef7484a806e8282c545d7607889e.zip
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]``
-rw-r--r--src/modules.rs6
-rw-r--r--tests/target/issue-5033/minimum_example.rs8
-rw-r--r--tests/target/issue-5033/nested_modules.rs11
3 files changed, 23 insertions, 2 deletions
diff --git a/src/modules.rs b/src/modules.rs
index 88d434d759d..9e75f41ae36 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;
 
@@ -135,10 +135,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