@@ -1680,6 +1680,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1680
1680
where_predicates_split,
1681
1681
} = * ty_alias_kind;
1682
1682
let ty_opt = ty. as_ref ( ) ;
1683
+ let rhs_hi = ty
1684
+ . as_ref ( )
1685
+ . map_or ( where_clauses. 0 . 1 . hi ( ) , |ty| ty. span . hi ( ) ) ;
1683
1686
let ( ident, vis) = match visitor_kind {
1684
1687
Item ( i) => ( i. ident , & i. vis ) ,
1685
1688
AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
@@ -1702,17 +1705,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1702
1705
match ( visitor_kind, & op_ty) {
1703
1706
( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , Some ( op_bounds) ) => {
1704
1707
let op = OpaqueType { bounds : op_bounds } ;
1705
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , vis)
1708
+ rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , rhs_hi , vis)
1706
1709
}
1707
1710
( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , None ) => {
1708
- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1711
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
1709
1712
}
1710
1713
( AssocImplItem ( _) , _) => {
1711
1714
let result = if let Some ( op_bounds) = op_ty {
1712
1715
let op = OpaqueType { bounds : op_bounds } ;
1713
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , & DEFAULT_VISIBILITY )
1716
+ rewrite_ty (
1717
+ rw_info,
1718
+ Some ( bounds) ,
1719
+ Some ( & op) ,
1720
+ rhs_hi,
1721
+ & DEFAULT_VISIBILITY ,
1722
+ )
1714
1723
} else {
1715
- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1724
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
1716
1725
} ?;
1717
1726
match defaultness {
1718
1727
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {result}" ) ) ,
@@ -1726,6 +1735,8 @@ fn rewrite_ty<R: Rewrite>(
1726
1735
rw_info : & TyAliasRewriteInfo < ' _ , ' _ > ,
1727
1736
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1728
1737
rhs : Option < & R > ,
1738
+ // the span of the end of the RHS (or the end of the generics, if there is no RHS)
1739
+ rhs_hi : BytePos ,
1729
1740
vis : & ast:: Visibility ,
1730
1741
) -> Option < String > {
1731
1742
let mut result = String :: with_capacity ( 128 ) ;
@@ -1742,9 +1753,6 @@ fn rewrite_ty<R: Rewrite>(
1742
1753
. where_clause
1743
1754
. predicates
1744
1755
. split_at ( where_predicates_split) ;
1745
- if !after_where_predicates. is_empty ( ) {
1746
- return None ;
1747
- }
1748
1756
result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
1749
1757
let ident_str = rewrite_ident ( context, ident) ;
1750
1758
@@ -1773,7 +1781,7 @@ fn rewrite_ty<R: Rewrite>(
1773
1781
if rhs. is_none ( ) {
1774
1782
option. suppress_comma ( ) ;
1775
1783
}
1776
- let where_clause_str = rewrite_where_clause (
1784
+ let before_where_clause_str = rewrite_where_clause (
1777
1785
context,
1778
1786
before_where_predicates,
1779
1787
where_clauses. 0 . 1 ,
@@ -1785,14 +1793,20 @@ fn rewrite_ty<R: Rewrite>(
1785
1793
generics. span . hi ( ) ,
1786
1794
option,
1787
1795
) ?;
1788
- result. push_str ( & where_clause_str ) ;
1796
+ result. push_str ( & before_where_clause_str ) ;
1789
1797
1790
- if let Some ( ty) = rhs {
1791
- // If there's a where clause , add a newline before the assignment. Otherwise just add a
1792
- // space.
1793
- let has_where = !before_where_predicates . is_empty ( ) ;
1794
- if has_where {
1798
+ let mut result = if let Some ( ty) = rhs {
1799
+ // If there are any where clauses , add a newline before the assignment.
1800
+ // If there is a before where clause, do not indent, but if there is
1801
+ // only an after where clause, additionally indent the type.
1802
+ if !before_where_predicates . is_empty ( ) {
1795
1803
result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1804
+ } else if !after_where_predicates. is_empty ( ) {
1805
+ result. push_str (
1806
+ & indent
1807
+ . block_indent ( context. config )
1808
+ . to_string_with_newline ( context. config ) ,
1809
+ ) ;
1796
1810
} else {
1797
1811
result. push ( ' ' ) ;
1798
1812
}
@@ -1806,7 +1820,7 @@ fn rewrite_ty<R: Rewrite>(
1806
1820
Some ( comment_span)
1807
1821
if contains_comment ( context. snippet_provider . span_to_snippet ( comment_span) ?) =>
1808
1822
{
1809
- let comment_shape = if has_where {
1823
+ let comment_shape = if !before_where_predicates . is_empty ( ) {
1810
1824
Shape :: indented ( indent, context. config )
1811
1825
} else {
1812
1826
Shape :: indented ( indent, context. config )
@@ -1825,12 +1839,36 @@ fn rewrite_ty<R: Rewrite>(
1825
1839
_ => format ! ( "{result}=" ) ,
1826
1840
} ;
1827
1841
1828
- // 1 = `;`
1829
- let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1830
- rewrite_assign_rhs ( context, lhs, & * ty, & RhsAssignKind :: Ty , shape) . map ( |s| s + ";" )
1842
+ // 1 = `;` unless there's a trailing where clause
1843
+ let shape = if after_where_predicates. is_empty ( ) {
1844
+ Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?
1845
+ } else {
1846
+ Shape :: indented ( indent, context. config )
1847
+ } ;
1848
+ rewrite_assign_rhs ( context, lhs, & * ty, & RhsAssignKind :: Ty , shape) ?
1831
1849
} else {
1832
- Some ( format ! ( "{result};" ) )
1850
+ result
1851
+ } ;
1852
+
1853
+ if !after_where_predicates. is_empty ( ) {
1854
+ let option = WhereClauseOption :: new ( true , WhereClauseSpace :: Newline ) ;
1855
+ let after_where_clause_str = rewrite_where_clause (
1856
+ context,
1857
+ after_where_predicates,
1858
+ where_clauses. 1 . 1 ,
1859
+ context. config . brace_style ( ) ,
1860
+ Shape :: indented ( indent, context. config ) ,
1861
+ false ,
1862
+ ";" ,
1863
+ None ,
1864
+ rhs_hi,
1865
+ option,
1866
+ ) ?;
1867
+ result. push_str ( & after_where_clause_str) ;
1833
1868
}
1869
+
1870
+ result += ";" ;
1871
+ Some ( result)
1834
1872
}
1835
1873
1836
1874
fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
0 commit comments