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