Skip to content

Commit b7c38c9

Browse files
whizsidcalebcartwright
authored andcommitted
Fixed comment dropped between & and type issue (#4482)
* Fixed comment dropped between & and type issue * Reduced nesting levels and avoided duplications * Removed extra allocations
1 parent 6455e9d commit b7c38c9

File tree

3 files changed

+122
-27
lines changed

3 files changed

+122
-27
lines changed

src/types.rs

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::iter::ExactSizeIterator;
22
use std::ops::Deref;
33

44
use rustc_ast::ast::{self, FnRetTy, Mutability};
5-
use rustc_span::{symbol::kw, BytePos, Span};
5+
use rustc_span::{symbol::kw, BytePos, Pos, Span};
66

77
use crate::config::lists::*;
88
use crate::config::{IndentStyle, TypeDensity, Version};
@@ -648,37 +648,72 @@ impl Rewrite for ast::Ty {
648648
ast::TyKind::Rptr(ref lifetime, ref mt) => {
649649
let mut_str = format_mutability(mt.mutbl);
650650
let mut_len = mut_str.len();
651-
Some(match *lifetime {
652-
Some(ref lifetime) => {
653-
let lt_budget = shape.width.checked_sub(2 + mut_len)?;
654-
let lt_str = lifetime.rewrite(
651+
let mut result = String::with_capacity(128);
652+
result.push_str("&");
653+
let ref_hi = context.snippet_provider.span_after(self.span(), "&");
654+
let mut cmnt_lo = ref_hi;
655+
656+
if let Some(ref lifetime) = *lifetime {
657+
let lt_budget = shape.width.checked_sub(2 + mut_len)?;
658+
let lt_str = lifetime.rewrite(
659+
context,
660+
Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
661+
)?;
662+
let before_lt_span = mk_sp(cmnt_lo, lifetime.ident.span.lo());
663+
if contains_comment(context.snippet(before_lt_span)) {
664+
result = combine_strs_with_missing_comments(
655665
context,
656-
Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
666+
&result,
667+
&lt_str,
668+
before_lt_span,
669+
shape,
670+
true,
657671
)?;
658-
let lt_len = lt_str.len();
659-
let budget = shape.width.checked_sub(2 + mut_len + lt_len)?;
660-
format!(
661-
"&{} {}{}",
662-
lt_str,
663-
mut_str,
664-
mt.ty.rewrite(
665-
context,
666-
Shape::legacy(budget, shape.indent + 2 + mut_len + lt_len)
667-
)?
668-
)
672+
} else {
673+
result.push_str(&lt_str);
669674
}
670-
None => {
671-
let budget = shape.width.checked_sub(1 + mut_len)?;
672-
format!(
673-
"&{}{}",
675+
result.push_str(" ");
676+
cmnt_lo = lifetime.ident.span.hi();
677+
}
678+
679+
if ast::Mutability::Mut == mt.mutbl {
680+
let mut_hi = context.snippet_provider.span_after(self.span(), "mut");
681+
let before_mut_span = mk_sp(cmnt_lo, mut_hi - BytePos::from_usize(3));
682+
if contains_comment(context.snippet(before_mut_span)) {
683+
result = combine_strs_with_missing_comments(
684+
context,
685+
result.trim_end(),
674686
mut_str,
675-
mt.ty.rewrite(
676-
context,
677-
Shape::legacy(budget, shape.indent + 1 + mut_len)
678-
)?
679-
)
687+
before_mut_span,
688+
shape,
689+
true,
690+
)?;
691+
} else {
692+
result.push_str(mut_str);
680693
}
681-
})
694+
cmnt_lo = mut_hi;
695+
}
696+
697+
let before_ty_span = mk_sp(cmnt_lo, mt.ty.span.lo());
698+
if contains_comment(context.snippet(before_ty_span)) {
699+
result = combine_strs_with_missing_comments(
700+
context,
701+
result.trim_end(),
702+
&mt.ty.rewrite(&context, shape)?,
703+
before_ty_span,
704+
shape,
705+
true,
706+
)?;
707+
} else {
708+
let used_width = last_line_width(&result);
709+
let budget = shape.width.checked_sub(used_width)?;
710+
let ty_str = mt
711+
.ty
712+
.rewrite(&context, Shape::legacy(budget, shape.indent + used_width))?;
713+
result.push_str(&ty_str);
714+
}
715+
716+
Some(result)
682717
}
683718
// FIXME: we drop any comments here, even though it's a silly place to put
684719
// comments.

tests/source/issue-4245.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
fn a(a: & // Comment
4+
// Another comment
5+
'a File) {}
6+
7+
fn b(b: & /* Another Comment */'a File) {}
8+
9+
fn c(c: &'a /*Comment */ mut /*Comment */ File){}
10+
11+
fn d(c: & // Comment
12+
'b // Multi Line
13+
// Comment
14+
mut // Multi Line
15+
// Comment
16+
File
17+
) {}
18+
19+
fn e(c: &// Comment
20+
File) {}
21+
22+
fn d(c: &// Comment
23+
mut // Multi Line
24+
// Comment
25+
File
26+
) {}

tests/target/issue-4245.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fn a(
2+
a: & // Comment
3+
// Another comment
4+
'a File,
5+
) {
6+
}
7+
8+
fn b(b: & /* Another Comment */ 'a File) {}
9+
10+
fn c(c: &'a /*Comment */ mut /*Comment */ File) {}
11+
12+
fn d(
13+
c: & // Comment
14+
'b // Multi Line
15+
// Comment
16+
mut // Multi Line
17+
// Comment
18+
File,
19+
) {
20+
}
21+
22+
fn e(
23+
c: & // Comment
24+
File,
25+
) {
26+
}
27+
28+
fn d(
29+
c: & // Comment
30+
mut // Multi Line
31+
// Comment
32+
File,
33+
) {
34+
}

0 commit comments

Comments
 (0)