Skip to content

Commit 4abdda1

Browse files
authored
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 f3ce2ce commit 4abdda1

File tree

3 files changed

+122
-27
lines changed

3 files changed

+122
-27
lines changed

Diff for: src/formatting/types.rs

+62-27
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::*, IndentStyle, TypeDensity};
88
use crate::formatting::{
@@ -653,37 +653,72 @@ impl Rewrite for ast::Ty {
653653
ast::TyKind::Rptr(ref lifetime, ref mt) => {
654654
let mut_str = format_mutability(mt.mutbl);
655655
let mut_len = mut_str.len();
656-
Some(match *lifetime {
657-
Some(ref lifetime) => {
658-
let lt_budget = shape.width.checked_sub(2 + mut_len)?;
659-
let lt_str = lifetime.rewrite(
656+
let mut result = String::with_capacity(128);
657+
result.push_str("&");
658+
let ref_hi = context.snippet_provider.span_after(self.span(), "&");
659+
let mut cmnt_lo = ref_hi;
660+
661+
if let Some(ref lifetime) = *lifetime {
662+
let lt_budget = shape.width.checked_sub(2 + mut_len)?;
663+
let lt_str = lifetime.rewrite(
664+
context,
665+
Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
666+
)?;
667+
let before_lt_span = mk_sp(cmnt_lo, lifetime.ident.span.lo());
668+
if contains_comment(context.snippet(before_lt_span)) {
669+
result = combine_strs_with_missing_comments(
660670
context,
661-
Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
671+
&result,
672+
&lt_str,
673+
before_lt_span,
674+
shape,
675+
true,
662676
)?;
663-
let lt_len = lt_str.len();
664-
let budget = shape.width.checked_sub(2 + mut_len + lt_len)?;
665-
format!(
666-
"&{} {}{}",
667-
lt_str,
668-
mut_str,
669-
mt.ty.rewrite(
670-
context,
671-
Shape::legacy(budget, shape.indent + 2 + mut_len + lt_len)
672-
)?
673-
)
677+
} else {
678+
result.push_str(&lt_str);
674679
}
675-
None => {
676-
let budget = shape.width.checked_sub(1 + mut_len)?;
677-
format!(
678-
"&{}{}",
680+
result.push_str(" ");
681+
cmnt_lo = lifetime.ident.span.hi();
682+
}
683+
684+
if ast::Mutability::Mut == mt.mutbl {
685+
let mut_hi = context.snippet_provider.span_after(self.span(), "mut");
686+
let before_mut_span = mk_sp(cmnt_lo, mut_hi - BytePos::from_usize(3));
687+
if contains_comment(context.snippet(before_mut_span)) {
688+
result = combine_strs_with_missing_comments(
689+
context,
690+
result.trim_end(),
679691
mut_str,
680-
mt.ty.rewrite(
681-
context,
682-
Shape::legacy(budget, shape.indent + 1 + mut_len)
683-
)?
684-
)
692+
before_mut_span,
693+
shape,
694+
true,
695+
)?;
696+
} else {
697+
result.push_str(mut_str);
685698
}
686-
})
699+
cmnt_lo = mut_hi;
700+
}
701+
702+
let before_ty_span = mk_sp(cmnt_lo, mt.ty.span.lo());
703+
if contains_comment(context.snippet(before_ty_span)) {
704+
result = combine_strs_with_missing_comments(
705+
context,
706+
result.trim_end(),
707+
&mt.ty.rewrite(&context, shape)?,
708+
before_ty_span,
709+
shape,
710+
true,
711+
)?;
712+
} else {
713+
let used_width = last_line_width(&result);
714+
let budget = shape.width.checked_sub(used_width)?;
715+
let ty_str = mt
716+
.ty
717+
.rewrite(&context, Shape::legacy(budget, shape.indent + used_width))?;
718+
result.push_str(&ty_str);
719+
}
720+
721+
Some(result)
687722
}
688723
// FIXME: we drop any comments here, even though it's a silly place to put
689724
// comments.

Diff for: tests/source/issue-4245.rs

+26
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+
) {}

Diff for: tests/target/issue-4245.rs

+34
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)