about summary refs log tree commit diff
diff options
context:
space:
mode:
authorrchaser53 <tayoshizawa29@gmail.com>2019-02-24 15:18:46 +0900
committerrchaser53 <tayoshizawa29@gmail.com>2019-02-24 22:05:08 +0900
commitae7330eea4100566f9f3826221cb3d694cfde5cc (patch)
tree3269afc4a911ec48e271a97b97084890caee334a
parent6a75feedacfc0a561a5847de58369ca9af5cf4ad (diff)
leave pre comment for self
-rw-r--r--src/items.rs16
-rw-r--r--tests/source/issue-3198.rs40
-rw-r--r--tests/target/issue-3198.rs19
3 files changed, 73 insertions, 2 deletions
diff --git a/src/items.rs b/src/items.rs
index c2568735ad8..d0b29b03ff3 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -20,7 +20,8 @@ use crate::expr::{
     ExprType, RhsTactics,
 };
 use crate::lists::{
-    definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
+    definitive_tactic, extract_pre_comment, itemize_list, write_list, ListFormatting, ListItem,
+    Separator,
 };
 use crate::macros::{rewrite_macro, MacroPosition};
 use crate::overflow;
@@ -2291,9 +2292,15 @@ fn rewrite_args(
     // Account for sugary self.
     // FIXME: the comment for the self argument is dropped. This is blocked
     // on rust issue #27522.
+    let mut comment_str = "";
     let min_args = explicit_self
         .and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
         .map_or(1, |self_str| {
+            let comment_span = mk_sp(span.lo(), args[0].pat.span.lo());
+            comment_str = context
+                .snippet_provider
+                .span_to_snippet(comment_span)
+                .unwrap();
             arg_item_strs[0] = self_str;
             2
         });
@@ -2368,7 +2375,12 @@ fn rewrite_args(
         && (arg_items.is_empty()
             || arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
 
-    for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
+    for (index, (item, arg)) in arg_items.iter_mut().zip(arg_item_strs).enumerate() {
+        if index == 0 && explicit_self.is_some() {
+            let (pre_comment, pre_comment_style) = extract_pre_comment(comment_str);
+            item.pre_comment = pre_comment;
+            item.pre_comment_style = pre_comment_style;
+        }
         item.item = Some(arg);
     }
 
diff --git a/tests/source/issue-3198.rs b/tests/source/issue-3198.rs
new file mode 100644
index 00000000000..517533ae562
--- /dev/null
+++ b/tests/source/issue-3198.rs
@@ -0,0 +1,40 @@
+impl TestTrait {
+    fn foo_one(/* Important comment1 */
+    self) {
+    }
+
+    fn foo(
+        /* Important comment1 */
+        self,
+        /* Important comment2 */
+        a: i32,
+    ) {
+    }
+
+    fn bar(
+            /* Important comment1 */
+    &mut self,
+        /* Important comment2 */
+            a: i32,
+    ) {
+    }
+
+    fn baz(
+    /* Important comment1 */
+            self: X< 'a ,  'b >,
+            /* Important comment2 */
+    a: i32,
+    ) {
+    }
+
+    fn baz_tree(
+    /* Important comment1 */
+
+            self: X< 'a ,  'b >,
+        /* Important comment2 */
+        a: i32,
+        /* Important comment3 */
+        b: i32,
+    ) {
+    }
+}
diff --git a/tests/target/issue-3198.rs b/tests/target/issue-3198.rs
new file mode 100644
index 00000000000..de307fed6b1
--- /dev/null
+++ b/tests/target/issue-3198.rs
@@ -0,0 +1,19 @@
+impl TestTrait {
+    fn foo_one(/* Important comment1 */ self) {}
+
+    fn foo(/* Important comment1 */ self, /* Important comment2 */ a: i32) {}
+
+    fn bar(/* Important comment1 */ &mut self, /* Important comment2 */ a: i32) {}
+
+    fn baz(/* Important comment1 */ self: X<'a, 'b>, /* Important comment2 */ a: i32) {}
+
+    fn baz_tree(
+        /* Important comment1 */
+        self: X<'a, 'b>,
+        /* Important comment2 */
+        a: i32,
+        /* Important comment3 */
+        b: i32,
+    ) {
+    }
+}