@@ -1657,6 +1657,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1657
1657
where_predicates_split,
1658
1658
} = * ty_alias_kind;
1659
1659
let ty_opt = ty. as_ref ( ) ;
1660
+ let rhs_hi = ty
1661
+ . as_ref ( )
1662
+ . map_or ( where_clauses. 0 . 1 . hi ( ) , |ty| ty. span . hi ( ) ) ;
1660
1663
let ( ident, vis) = match visitor_kind {
1661
1664
Item ( i) => ( i. ident , & i. vis ) ,
1662
1665
AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
@@ -1679,17 +1682,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1679
1682
match ( visitor_kind, & op_ty) {
1680
1683
( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , Some ( op_bounds) ) => {
1681
1684
let op = OpaqueType { bounds : op_bounds } ;
1682
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , vis)
1685
+ rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , rhs_hi , vis)
1683
1686
}
1684
1687
( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , None ) => {
1685
- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1688
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
1686
1689
}
1687
1690
( AssocImplItem ( _) , _) => {
1688
1691
let result = if let Some ( op_bounds) = op_ty {
1689
1692
let op = OpaqueType { bounds : op_bounds } ;
1690
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , & DEFAULT_VISIBILITY )
1693
+ rewrite_ty (
1694
+ rw_info,
1695
+ Some ( bounds) ,
1696
+ Some ( & op) ,
1697
+ rhs_hi,
1698
+ & DEFAULT_VISIBILITY ,
1699
+ )
1691
1700
} else {
1692
- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1701
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
1693
1702
} ?;
1694
1703
match defaultness {
1695
1704
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {result}" ) ) ,
@@ -1703,6 +1712,8 @@ fn rewrite_ty<R: Rewrite>(
1703
1712
rw_info : & TyAliasRewriteInfo < ' _ , ' _ > ,
1704
1713
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1705
1714
rhs : Option < & R > ,
1715
+ // the span of the end of the RHS (or the end of the generics, if there is no RHS)
1716
+ rhs_hi : BytePos ,
1706
1717
vis : & ast:: Visibility ,
1707
1718
) -> Option < String > {
1708
1719
let mut result = String :: with_capacity ( 128 ) ;
@@ -1719,9 +1730,6 @@ fn rewrite_ty<R: Rewrite>(
1719
1730
. where_clause
1720
1731
. predicates
1721
1732
. split_at ( where_predicates_split) ;
1722
- if !after_where_predicates. is_empty ( ) {
1723
- return None ;
1724
- }
1725
1733
result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
1726
1734
let ident_str = rewrite_ident ( context, ident) ;
1727
1735
@@ -1750,7 +1758,7 @@ fn rewrite_ty<R: Rewrite>(
1750
1758
if rhs. is_none ( ) {
1751
1759
option. suppress_comma ( ) ;
1752
1760
}
1753
- let where_clause_str = rewrite_where_clause (
1761
+ let before_where_clause_str = rewrite_where_clause (
1754
1762
context,
1755
1763
before_where_predicates,
1756
1764
where_clauses. 0 . 1 ,
@@ -1762,14 +1770,20 @@ fn rewrite_ty<R: Rewrite>(
1762
1770
generics. span . hi ( ) ,
1763
1771
option,
1764
1772
) ?;
1765
- result. push_str ( & where_clause_str ) ;
1773
+ result. push_str ( & before_where_clause_str ) ;
1766
1774
1767
- if let Some ( ty) = rhs {
1768
- // If there's a where clause , add a newline before the assignment. Otherwise just add a
1769
- // space.
1770
- let has_where = !before_where_predicates . is_empty ( ) ;
1771
- if has_where {
1775
+ let mut result = if let Some ( ty) = rhs {
1776
+ // If there are any where clauses , add a newline before the assignment.
1777
+ // If there is a before where clause, do not indent, but if there is
1778
+ // only an after where clause, additionally indent the type.
1779
+ if !before_where_predicates . is_empty ( ) {
1772
1780
result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1781
+ } else if !after_where_predicates. is_empty ( ) {
1782
+ result. push_str (
1783
+ & indent
1784
+ . block_indent ( context. config )
1785
+ . to_string_with_newline ( context. config ) ,
1786
+ ) ;
1773
1787
} else {
1774
1788
result. push ( ' ' ) ;
1775
1789
}
@@ -1783,7 +1797,7 @@ fn rewrite_ty<R: Rewrite>(
1783
1797
Some ( comment_span)
1784
1798
if contains_comment ( context. snippet_provider . span_to_snippet ( comment_span) ?) =>
1785
1799
{
1786
- let comment_shape = if has_where {
1800
+ let comment_shape = if !before_where_predicates . is_empty ( ) {
1787
1801
Shape :: indented ( indent, context. config )
1788
1802
} else {
1789
1803
Shape :: indented ( indent, context. config )
@@ -1802,12 +1816,36 @@ fn rewrite_ty<R: Rewrite>(
1802
1816
_ => format ! ( "{result}=" ) ,
1803
1817
} ;
1804
1818
1805
- // 1 = `;`
1806
- let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1807
- rewrite_assign_rhs ( context, lhs, & * ty, & RhsAssignKind :: Ty , shape) . map ( |s| s + ";" )
1819
+ // 1 = `;` unless there's a trailing where clause
1820
+ let shape = if after_where_predicates. is_empty ( ) {
1821
+ Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?
1822
+ } else {
1823
+ Shape :: indented ( indent, context. config )
1824
+ } ;
1825
+ rewrite_assign_rhs ( context, lhs, & * ty, & RhsAssignKind :: Ty , shape) ?
1808
1826
} else {
1809
- Some ( format ! ( "{result};" ) )
1827
+ result
1828
+ } ;
1829
+
1830
+ if !after_where_predicates. is_empty ( ) {
1831
+ let option = WhereClauseOption :: new ( true , WhereClauseSpace :: Newline ) ;
1832
+ let after_where_clause_str = rewrite_where_clause (
1833
+ context,
1834
+ after_where_predicates,
1835
+ where_clauses. 1 . 1 ,
1836
+ context. config . brace_style ( ) ,
1837
+ Shape :: indented ( indent, context. config ) ,
1838
+ false ,
1839
+ ";" ,
1840
+ None ,
1841
+ rhs_hi,
1842
+ option,
1843
+ ) ?;
1844
+ result. push_str ( & after_where_clause_str) ;
1810
1845
}
1846
+
1847
+ result += ";" ;
1848
+ Some ( result)
1811
1849
}
1812
1850
1813
1851
fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
0 commit comments