@@ -1619,6 +1619,7 @@ fn rewrite_type<R: Rewrite>(
1619
1619
generics : & ast:: Generics ,
1620
1620
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1621
1621
rhs : Option < & R > ,
1622
+ span : Span ,
1622
1623
) -> Option < String > {
1623
1624
let mut result = String :: with_capacity ( 128 ) ;
1624
1625
result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
@@ -1665,12 +1666,40 @@ fn rewrite_type<R: Rewrite>(
1665
1666
if let Some ( ty) = rhs {
1666
1667
// If there's a where clause, add a newline before the assignment. Otherwise just add a
1667
1668
// space.
1668
- if !generics. where_clause . predicates . is_empty ( ) {
1669
+ let has_where = !generics. where_clause . predicates . is_empty ( ) ;
1670
+ if has_where {
1669
1671
result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1670
1672
} else {
1671
1673
result. push ( ' ' ) ;
1672
1674
}
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
+ } ;
1674
1703
1675
1704
// 1 = `;`
1676
1705
let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
@@ -1687,6 +1716,7 @@ pub(crate) fn rewrite_opaque_type(
1687
1716
generic_bounds : & ast:: GenericBounds ,
1688
1717
generics : & ast:: Generics ,
1689
1718
vis : & ast:: Visibility ,
1719
+ span : Span ,
1690
1720
) -> Option < String > {
1691
1721
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds } ;
1692
1722
rewrite_type (
@@ -1697,6 +1727,7 @@ pub(crate) fn rewrite_opaque_type(
1697
1727
generics,
1698
1728
Some ( generic_bounds) ,
1699
1729
Some ( & opaque_type_bounds) ,
1730
+ span,
1700
1731
)
1701
1732
}
1702
1733
@@ -1954,6 +1985,7 @@ pub(crate) fn rewrite_type_alias(
1954
1985
context : & RewriteContext < ' _ > ,
1955
1986
indent : Indent ,
1956
1987
vis : & ast:: Visibility ,
1988
+ span : Span ,
1957
1989
) -> Option < String > {
1958
1990
rewrite_type (
1959
1991
context,
@@ -1963,6 +1995,7 @@ pub(crate) fn rewrite_type_alias(
1963
1995
generics,
1964
1996
generic_bounds_opt,
1965
1997
ty_opt,
1998
+ span,
1966
1999
)
1967
2000
}
1968
2001
@@ -2012,8 +2045,9 @@ pub(crate) fn rewrite_associated_impl_type(
2012
2045
generics : & ast:: Generics ,
2013
2046
context : & RewriteContext < ' _ > ,
2014
2047
indent : Indent ,
2048
+ span : Span ,
2015
2049
) -> 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 ) ?;
2017
2051
2018
2052
match defaultness {
2019
2053
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
@@ -3250,6 +3284,7 @@ impl Rewrite for ast::ForeignItem {
3250
3284
& context,
3251
3285
shape. indent ,
3252
3286
& self . vis ,
3287
+ self . span ,
3253
3288
) ,
3254
3289
ast:: ForeignItemKind :: MacCall ( ref mac) => {
3255
3290
rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
0 commit comments