Skip to content

Commit 6455e9d

Browse files
whizsidcalebcartwright
authored andcommitted
Fixed 'Comment removed between type name and =' issue (#4448)
* Fixed Comment removed between type name and = issue * Fixed where clause issue and pass the full span * has_where condition inline * Fixed indentation error on where clause * Removed tmp file
1 parent ea712f1 commit 6455e9d

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/items.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,7 @@ fn rewrite_type<R: Rewrite>(
14951495
generics: &ast::Generics,
14961496
generic_bounds_opt: Option<&ast::GenericBounds>,
14971497
rhs: Option<&R>,
1498+
span: Span,
14981499
) -> Option<String> {
14991500
let mut result = String::with_capacity(128);
15001501
result.push_str(&format!("{}type ", format_visibility(context, vis)));
@@ -1541,12 +1542,40 @@ fn rewrite_type<R: Rewrite>(
15411542
if let Some(ty) = rhs {
15421543
// If there's a where clause, add a newline before the assignment. Otherwise just add a
15431544
// space.
1544-
if !generics.where_clause.predicates.is_empty() {
1545+
let has_where = !generics.where_clause.predicates.is_empty();
1546+
if has_where {
15451547
result.push_str(&indent.to_string_with_newline(context.config));
15461548
} else {
15471549
result.push(' ');
15481550
}
1549-
let lhs = format!("{}=", result);
1551+
1552+
let comment_span = context
1553+
.snippet_provider
1554+
.opt_span_before(span, "=")
1555+
.map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo));
1556+
1557+
let lhs = match comment_span {
1558+
Some(comment_span)
1559+
if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
1560+
{
1561+
let comment_shape = if has_where {
1562+
Shape::indented(indent, context.config)
1563+
} else {
1564+
Shape::indented(indent, context.config)
1565+
.block_left(context.config.tab_spaces())?
1566+
};
1567+
1568+
combine_strs_with_missing_comments(
1569+
context,
1570+
result.trim_end(),
1571+
"=",
1572+
comment_span,
1573+
comment_shape,
1574+
true,
1575+
)?
1576+
}
1577+
_ => format!("{}=", result),
1578+
};
15501579

15511580
// 1 = `;`
15521581
let shape = Shape::indented(indent, context.config).sub_width(1)?;
@@ -1563,6 +1592,7 @@ pub(crate) fn rewrite_opaque_type(
15631592
generic_bounds: &ast::GenericBounds,
15641593
generics: &ast::Generics,
15651594
vis: &ast::Visibility,
1595+
span: Span,
15661596
) -> Option<String> {
15671597
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
15681598
rewrite_type(
@@ -1573,6 +1603,7 @@ pub(crate) fn rewrite_opaque_type(
15731603
generics,
15741604
Some(generic_bounds),
15751605
Some(&opaque_type_bounds),
1606+
span,
15761607
)
15771608
}
15781609

@@ -1801,6 +1832,7 @@ pub(crate) fn rewrite_type_alias(
18011832
context: &RewriteContext<'_>,
18021833
indent: Indent,
18031834
vis: &ast::Visibility,
1835+
span: Span,
18041836
) -> Option<String> {
18051837
rewrite_type(
18061838
context,
@@ -1810,6 +1842,7 @@ pub(crate) fn rewrite_type_alias(
18101842
generics,
18111843
generic_bounds_opt,
18121844
ty_opt,
1845+
span,
18131846
)
18141847
}
18151848

@@ -1859,8 +1892,9 @@ pub(crate) fn rewrite_associated_impl_type(
18591892
generics: &ast::Generics,
18601893
context: &RewriteContext<'_>,
18611894
indent: Indent,
1895+
span: Span,
18621896
) -> Option<String> {
1863-
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?;
1897+
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?;
18641898

18651899
match defaultness {
18661900
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
@@ -3118,6 +3152,7 @@ impl Rewrite for ast::ForeignItem {
31183152
&context,
31193153
shape.indent,
31203154
&self.vis,
3155+
self.span,
31213156
),
31223157
ast::ForeignItemKind::MacCall(ref mac) => {
31233158
rewrite_macro(mac, None, context, shape, MacroPosition::Item)

src/visitor.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
572572
&self.get_context(),
573573
self.block_indent,
574574
&item.vis,
575+
item.span,
575576
);
576577
self.push_rewrite(item.span, rewrite);
577578
}
@@ -583,6 +584,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
583584
generic_bounds,
584585
generics,
585586
&item.vis,
587+
item.span,
586588
);
587589
self.push_rewrite(item.span, rewrite);
588590
}
@@ -649,6 +651,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
649651
&self.get_context(),
650652
self.block_indent,
651653
&ti.vis,
654+
ti.span,
652655
);
653656
self.push_rewrite(ti.span, rewrite);
654657
}
@@ -695,6 +698,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
695698
&generics,
696699
&self.get_context(),
697700
self.block_indent,
701+
ii.span,
698702
)
699703
};
700704
let rewrite = match ty {

tests/source/issue-4244.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub struct SS {}
2+
3+
pub type A /* A Comment */ = SS;
4+
5+
pub type B // Comment
6+
// B
7+
= SS;
8+
9+
pub type C
10+
/* Comment C */ = SS;
11+
12+
pub trait D <T> {
13+
type E /* Comment E */ = SS;
14+
}
15+
16+
type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq where T: 'static + Copy /* x */ = Vec<u8>;

tests/target/issue-4244.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pub struct SS {}
2+
3+
pub type A /* A Comment */ = SS;
4+
5+
pub type B // Comment
6+
// B
7+
= SS;
8+
9+
pub type C
10+
/* Comment C */
11+
= SS;
12+
13+
pub trait D<T> {
14+
type E /* Comment E */ = SS;
15+
}
16+
17+
type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq
18+
where
19+
T: 'static + Copy, /* x */
20+
= Vec<u8>;

0 commit comments

Comments
 (0)