Skip to content

Commit ae7330e

Browse files
committed
leave pre comment for self
1 parent 6a75fee commit ae7330e

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/items.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use crate::expr::{
2020
ExprType, RhsTactics,
2121
};
2222
use crate::lists::{
23-
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
23+
definitive_tactic, extract_pre_comment, itemize_list, write_list, ListFormatting, ListItem,
24+
Separator,
2425
};
2526
use crate::macros::{rewrite_macro, MacroPosition};
2627
use crate::overflow;
@@ -2291,9 +2292,15 @@ fn rewrite_args(
22912292
// Account for sugary self.
22922293
// FIXME: the comment for the self argument is dropped. This is blocked
22932294
// on rust issue #27522.
2295+
let mut comment_str = "";
22942296
let min_args = explicit_self
22952297
.and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
22962298
.map_or(1, |self_str| {
2299+
let comment_span = mk_sp(span.lo(), args[0].pat.span.lo());
2300+
comment_str = context
2301+
.snippet_provider
2302+
.span_to_snippet(comment_span)
2303+
.unwrap();
22972304
arg_item_strs[0] = self_str;
22982305
2
22992306
});
@@ -2368,7 +2375,12 @@ fn rewrite_args(
23682375
&& (arg_items.is_empty()
23692376
|| arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
23702377

2371-
for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
2378+
for (index, (item, arg)) in arg_items.iter_mut().zip(arg_item_strs).enumerate() {
2379+
if index == 0 && explicit_self.is_some() {
2380+
let (pre_comment, pre_comment_style) = extract_pre_comment(comment_str);
2381+
item.pre_comment = pre_comment;
2382+
item.pre_comment_style = pre_comment_style;
2383+
}
23722384
item.item = Some(arg);
23732385
}
23742386

tests/source/issue-3198.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
impl TestTrait {
2+
fn foo_one(/* Important comment1 */
3+
self) {
4+
}
5+
6+
fn foo(
7+
/* Important comment1 */
8+
self,
9+
/* Important comment2 */
10+
a: i32,
11+
) {
12+
}
13+
14+
fn bar(
15+
/* Important comment1 */
16+
&mut self,
17+
/* Important comment2 */
18+
a: i32,
19+
) {
20+
}
21+
22+
fn baz(
23+
/* Important comment1 */
24+
self: X< 'a , 'b >,
25+
/* Important comment2 */
26+
a: i32,
27+
) {
28+
}
29+
30+
fn baz_tree(
31+
/* Important comment1 */
32+
33+
self: X< 'a , 'b >,
34+
/* Important comment2 */
35+
a: i32,
36+
/* Important comment3 */
37+
b: i32,
38+
) {
39+
}
40+
}

tests/target/issue-3198.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl TestTrait {
2+
fn foo_one(/* Important comment1 */ self) {}
3+
4+
fn foo(/* Important comment1 */ self, /* Important comment2 */ a: i32) {}
5+
6+
fn bar(/* Important comment1 */ &mut self, /* Important comment2 */ a: i32) {}
7+
8+
fn baz(/* Important comment1 */ self: X<'a, 'b>, /* Important comment2 */ a: i32) {}
9+
10+
fn baz_tree(
11+
/* Important comment1 */
12+
self: X<'a, 'b>,
13+
/* Important comment2 */
14+
a: i32,
15+
/* Important comment3 */
16+
b: i32,
17+
) {
18+
}
19+
}

0 commit comments

Comments
 (0)