Skip to content

Commit 7e31d5d

Browse files
authored
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 69d9a91 commit 7e31d5d

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/formatting/items.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ fn rewrite_type<R: Rewrite>(
16191619
generics: &ast::Generics,
16201620
generic_bounds_opt: Option<&ast::GenericBounds>,
16211621
rhs: Option<&R>,
1622+
span: Span,
16221623
) -> Option<String> {
16231624
let mut result = String::with_capacity(128);
16241625
result.push_str(&format!("{}type ", format_visibility(context, vis)));
@@ -1665,12 +1666,40 @@ fn rewrite_type<R: Rewrite>(
16651666
if let Some(ty) = rhs {
16661667
// If there's a where clause, add a newline before the assignment. Otherwise just add a
16671668
// space.
1668-
if !generics.where_clause.predicates.is_empty() {
1669+
let has_where = !generics.where_clause.predicates.is_empty();
1670+
if has_where {
16691671
result.push_str(&indent.to_string_with_newline(context.config));
16701672
} else {
16711673
result.push(' ');
16721674
}
1673-
let lhs = format!("{}=", result);
1675+
1676+
let comment_span = context
1677+
.snippet_provider
1678+
.opt_span_before(span, "=")
1679+
.map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo));
1680+
1681+
let lhs = match comment_span {
1682+
Some(comment_span)
1683+
if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
1684+
{
1685+
let comment_shape = if has_where {
1686+
Shape::indented(indent, context.config)
1687+
} else {
1688+
Shape::indented(indent, context.config)
1689+
.block_left(context.config.tab_spaces())?
1690+
};
1691+
1692+
combine_strs_with_missing_comments(
1693+
context,
1694+
result.trim_end(),
1695+
"=",
1696+
comment_span,
1697+
comment_shape,
1698+
true,
1699+
)?
1700+
}
1701+
_ => format!("{}=", result),
1702+
};
16741703

16751704
// 1 = `;`
16761705
let shape = Shape::indented(indent, context.config).sub_width(1)?;
@@ -1687,6 +1716,7 @@ pub(crate) fn rewrite_opaque_type(
16871716
generic_bounds: &ast::GenericBounds,
16881717
generics: &ast::Generics,
16891718
vis: &ast::Visibility,
1719+
span: Span,
16901720
) -> Option<String> {
16911721
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
16921722
rewrite_type(
@@ -1697,6 +1727,7 @@ pub(crate) fn rewrite_opaque_type(
16971727
generics,
16981728
Some(generic_bounds),
16991729
Some(&opaque_type_bounds),
1730+
span,
17001731
)
17011732
}
17021733

@@ -1954,6 +1985,7 @@ pub(crate) fn rewrite_type_alias(
19541985
context: &RewriteContext<'_>,
19551986
indent: Indent,
19561987
vis: &ast::Visibility,
1988+
span: Span,
19571989
) -> Option<String> {
19581990
rewrite_type(
19591991
context,
@@ -1963,6 +1995,7 @@ pub(crate) fn rewrite_type_alias(
19631995
generics,
19641996
generic_bounds_opt,
19651997
ty_opt,
1998+
span,
19661999
)
19672000
}
19682001

@@ -2012,8 +2045,9 @@ pub(crate) fn rewrite_associated_impl_type(
20122045
generics: &ast::Generics,
20132046
context: &RewriteContext<'_>,
20142047
indent: Indent,
2048+
span: Span,
20152049
) -> Option<String> {
2016-
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?;
2050+
let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?;
20172051

20182052
match defaultness {
20192053
ast::Defaultness::Default(..) => Some(format!("default {}", result)),
@@ -3250,6 +3284,7 @@ impl Rewrite for ast::ForeignItem {
32503284
&context,
32513285
shape.indent,
32523286
&self.vis,
3287+
self.span,
32533288
),
32543289
ast::ForeignItemKind::MacCall(ref mac) => {
32553290
rewrite_macro(mac, None, context, shape, MacroPosition::Item)

src/formatting/visitor.rs

+4
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
606606
&self.get_context(),
607607
self.block_indent,
608608
&item.vis,
609+
item.span,
609610
);
610611
self.push_rewrite(item.span, rewrite);
611612
}
@@ -617,6 +618,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
617618
generic_bounds,
618619
generics,
619620
&item.vis,
621+
item.span,
620622
);
621623
self.push_rewrite(item.span, rewrite);
622624
}
@@ -685,6 +687,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
685687
&self.get_context(),
686688
self.block_indent,
687689
&ti.vis,
690+
ti.span,
688691
);
689692
self.push_rewrite(ti.span, rewrite);
690693
}
@@ -734,6 +737,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
734737
generics,
735738
&self.get_context(),
736739
self.block_indent,
740+
ii.span,
737741
)
738742
};
739743
let rewrite = match ty {

tests/source/issue-4244.rs

+16
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

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