@@ -1510,6 +1510,7 @@ fn rewrite_type_prefix(
1510
1510
prefix : & str ,
1511
1511
ident : symbol:: Ident ,
1512
1512
generics : & ast:: Generics ,
1513
+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
1513
1514
) -> Option < String > {
1514
1515
let mut result = String :: with_capacity ( 128 ) ;
1515
1516
result. push_str ( prefix) ;
@@ -1526,6 +1527,19 @@ fn rewrite_type_prefix(
1526
1527
result. push_str ( & generics_str) ;
1527
1528
}
1528
1529
1530
+ let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1531
+ if bounds. is_empty ( ) {
1532
+ String :: new ( )
1533
+ } else {
1534
+ // 2 = `: `
1535
+ let shape = Shape :: indented ( indent, context. config ) . offset_left ( result. len ( ) + 2 ) ?;
1536
+ bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1537
+ }
1538
+ } else {
1539
+ String :: new ( )
1540
+ } ;
1541
+ result. push_str ( & type_bounds_str) ;
1542
+
1529
1543
let where_budget = context. budget ( last_line_width ( & result) ) ;
1530
1544
let option = WhereClauseOption :: snuggled ( & result) ;
1531
1545
let where_clause_str = rewrite_where_clause (
@@ -1552,6 +1566,7 @@ fn rewrite_type_item<R: Rewrite>(
1552
1566
ident : symbol:: Ident ,
1553
1567
rhs : & R ,
1554
1568
generics : & ast:: Generics ,
1569
+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
1555
1570
vis : & ast:: Visibility ,
1556
1571
) -> Option < String > {
1557
1572
let mut result = String :: with_capacity ( 128 ) ;
@@ -1561,6 +1576,7 @@ fn rewrite_type_item<R: Rewrite>(
1561
1576
& format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1562
1577
ident,
1563
1578
generics,
1579
+ generic_bounds_opt,
1564
1580
) ?) ;
1565
1581
1566
1582
if generics. where_clause . predicates . is_empty ( ) {
@@ -1575,17 +1591,6 @@ fn rewrite_type_item<R: Rewrite>(
1575
1591
rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1576
1592
}
1577
1593
1578
- pub ( crate ) fn rewrite_type_alias (
1579
- context : & RewriteContext < ' _ > ,
1580
- indent : Indent ,
1581
- ident : symbol:: Ident ,
1582
- ty : & ast:: Ty ,
1583
- generics : & ast:: Generics ,
1584
- vis : & ast:: Visibility ,
1585
- ) -> Option < String > {
1586
- rewrite_type_item ( context, indent, "type" , " =" , ident, ty, generics, vis)
1587
- }
1588
-
1589
1594
pub ( crate ) fn rewrite_opaque_type (
1590
1595
context : & RewriteContext < ' _ > ,
1591
1596
indent : Indent ,
@@ -1603,6 +1608,7 @@ pub(crate) fn rewrite_opaque_type(
1603
1608
ident,
1604
1609
& opaque_type_bounds,
1605
1610
generics,
1611
+ Some ( generic_bounds) ,
1606
1612
vis,
1607
1613
)
1608
1614
}
@@ -1824,39 +1830,39 @@ fn rewrite_static(
1824
1830
}
1825
1831
}
1826
1832
1827
- pub ( crate ) fn rewrite_associated_type (
1833
+ pub ( crate ) fn rewrite_type_alias (
1828
1834
ident : symbol:: Ident ,
1829
1835
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1830
1836
generics : & ast:: Generics ,
1831
1837
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1832
1838
context : & RewriteContext < ' _ > ,
1833
1839
indent : Indent ,
1840
+ vis : & ast:: Visibility ,
1834
1841
) -> Option < String > {
1835
- let ident_str = rewrite_ident ( context, ident) ;
1836
- // 5 = "type "
1837
- let generics_shape = Shape :: indented ( indent, context. config ) . offset_left ( 5 ) ?;
1838
- let generics_str = rewrite_generics ( context, ident_str, generics, generics_shape) ?;
1839
- let prefix = format ! ( "type {}" , generics_str) ;
1840
-
1841
- let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1842
- if bounds. is_empty ( ) {
1843
- String :: new ( )
1844
- } else {
1845
- // 2 = ": ".len()
1846
- let shape = Shape :: indented ( indent, context. config ) . offset_left ( prefix. len ( ) + 2 ) ?;
1847
- bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1848
- }
1849
- } else {
1850
- String :: new ( )
1851
- } ;
1842
+ let mut prefix = rewrite_type_prefix (
1843
+ context,
1844
+ indent,
1845
+ & format ! ( "{}type " , format_visibility( context, vis) ) ,
1846
+ ident,
1847
+ generics,
1848
+ generic_bounds_opt,
1849
+ ) ?;
1852
1850
1853
1851
if let Some ( ty) = ty_opt {
1854
1852
// 1 = `;`
1855
1853
let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1856
- let lhs = format ! ( "{}{} =" , prefix, type_bounds_str) ;
1854
+
1855
+ // If there's a where clause, add a newline before the assignment. Otherwise just add a
1856
+ // space.
1857
+ if !generics. where_clause . predicates . is_empty ( ) {
1858
+ prefix. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1859
+ } else {
1860
+ prefix. push ( ' ' ) ;
1861
+ }
1862
+ let lhs = format ! ( "{}=" , prefix) ;
1857
1863
rewrite_assign_rhs ( context, lhs, & * * ty, shape) . map ( |s| s + ";" )
1858
1864
} else {
1859
- Some ( format ! ( "{}{} ;" , prefix, type_bounds_str ) )
1865
+ Some ( format ! ( "{};" , prefix) )
1860
1866
}
1861
1867
}
1862
1868
@@ -1900,13 +1906,14 @@ pub(crate) fn rewrite_opaque_impl_type(
1900
1906
1901
1907
pub ( crate ) fn rewrite_associated_impl_type (
1902
1908
ident : symbol:: Ident ,
1909
+ vis : & ast:: Visibility ,
1903
1910
defaultness : ast:: Defaultness ,
1904
1911
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1905
1912
generics : & ast:: Generics ,
1906
1913
context : & RewriteContext < ' _ > ,
1907
1914
indent : Indent ,
1908
1915
) -> Option < String > {
1909
- let result = rewrite_associated_type ( ident, ty_opt, generics, None , context, indent) ?;
1916
+ let result = rewrite_type_alias ( ident, ty_opt, generics, None , context, indent, vis ) ?;
1910
1917
1911
1918
match defaultness {
1912
1919
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
@@ -3088,7 +3095,7 @@ impl Rewrite for ast::ForeignItem {
3088
3095
// FIXME: this may be a faulty span from libsyntax.
3089
3096
let span = mk_sp ( self . span . lo ( ) , self . span . hi ( ) - BytePos ( 1 ) ) ;
3090
3097
3091
- let item_str = match self . kind {
3098
+ let item_str: String = match self . kind {
3092
3099
ast:: ForeignItemKind :: Fn ( _, ref fn_sig, ref generics, _) => rewrite_fn_base (
3093
3100
context,
3094
3101
shape. indent ,
@@ -3112,14 +3119,20 @@ impl Rewrite for ast::ForeignItem {
3112
3119
// 1 = ;
3113
3120
rewrite_assign_rhs ( context, prefix, & * * ty, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
3114
3121
}
3115
- ast:: ForeignItemKind :: TyAlias ( ..) => {
3116
- let vis = format_visibility ( context, & self . vis ) ;
3117
- Some ( format ! (
3118
- "{}type {};" ,
3119
- vis,
3120
- rewrite_ident( context, self . ident)
3121
- ) )
3122
- }
3122
+ ast:: ForeignItemKind :: TyAlias (
3123
+ _,
3124
+ ref generics,
3125
+ ref generic_bounds,
3126
+ ref type_default,
3127
+ ) => rewrite_type_alias (
3128
+ self . ident ,
3129
+ type_default. as_ref ( ) ,
3130
+ generics,
3131
+ Some ( generic_bounds) ,
3132
+ & context,
3133
+ shape. indent ,
3134
+ & self . vis ,
3135
+ ) ,
3123
3136
ast:: ForeignItemKind :: MacCall ( ref mac) => {
3124
3137
rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
3125
3138
}
0 commit comments