@@ -1712,6 +1712,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1712
1712
where_clauses,
1713
1713
} = * ty_alias_kind;
1714
1714
let ty_opt = ty. as_ref ( ) ;
1715
+ let rhs_hi = ty
1716
+ . as_ref ( )
1717
+ . map_or ( where_clauses. before . span . hi ( ) , |ty| ty. span . hi ( ) ) ;
1715
1718
let ( ident, vis) = match visitor_kind {
1716
1719
Item ( i) => ( i. ident , & i. vis ) ,
1717
1720
AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
@@ -1726,17 +1729,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1726
1729
match ( visitor_kind, & op_ty) {
1727
1730
( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , Some ( op_bounds) ) => {
1728
1731
let op = OpaqueType { bounds : op_bounds } ;
1729
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , vis)
1732
+ rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , rhs_hi , vis)
1730
1733
}
1731
1734
( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , None ) => {
1732
- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1735
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
1733
1736
}
1734
1737
( AssocImplItem ( _) , _) => {
1735
1738
let result = if let Some ( op_bounds) = op_ty {
1736
1739
let op = OpaqueType { bounds : op_bounds } ;
1737
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , & DEFAULT_VISIBILITY )
1740
+ rewrite_ty (
1741
+ rw_info,
1742
+ Some ( bounds) ,
1743
+ Some ( & op) ,
1744
+ rhs_hi,
1745
+ & DEFAULT_VISIBILITY ,
1746
+ )
1738
1747
} else {
1739
- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1748
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
1740
1749
} ?;
1741
1750
match defaultness {
1742
1751
ast:: Defaultness :: Default ( ..) => Ok ( format ! ( "default {result}" ) ) ,
@@ -1750,6 +1759,8 @@ fn rewrite_ty<R: Rewrite>(
1750
1759
rw_info : & TyAliasRewriteInfo < ' _ , ' _ > ,
1751
1760
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1752
1761
rhs : Option < & R > ,
1762
+ // the span of the end of the RHS (or the end of the generics, if there is no RHS)
1763
+ rhs_hi : BytePos ,
1753
1764
vis : & ast:: Visibility ,
1754
1765
) -> RewriteResult {
1755
1766
let mut result = String :: with_capacity ( 128 ) ;
@@ -1758,9 +1769,6 @@ fn rewrite_ty<R: Rewrite>(
1758
1769
. where_clause
1759
1770
. predicates
1760
1771
. split_at ( where_clauses. split ) ;
1761
- if !after_where_predicates. is_empty ( ) {
1762
- return Err ( RewriteError :: Unknown ) ;
1763
- }
1764
1772
result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
1765
1773
let ident_str = rewrite_ident ( context, ident) ;
1766
1774
@@ -1796,7 +1804,7 @@ fn rewrite_ty<R: Rewrite>(
1796
1804
if rhs. is_none ( ) {
1797
1805
option. suppress_comma ( ) ;
1798
1806
}
1799
- let where_clause_str = rewrite_where_clause (
1807
+ let before_where_clause_str = rewrite_where_clause (
1800
1808
context,
1801
1809
before_where_predicates,
1802
1810
where_clauses. before . span ,
@@ -1808,14 +1816,20 @@ fn rewrite_ty<R: Rewrite>(
1808
1816
generics. span . hi ( ) ,
1809
1817
option,
1810
1818
) ?;
1811
- result. push_str ( & where_clause_str ) ;
1819
+ result. push_str ( & before_where_clause_str ) ;
1812
1820
1813
- if let Some ( ty) = rhs {
1814
- // If there's a where clause , add a newline before the assignment. Otherwise just add a
1815
- // space.
1816
- let has_where = !before_where_predicates . is_empty ( ) ;
1817
- if has_where {
1821
+ let mut result = if let Some ( ty) = rhs {
1822
+ // If there are any where clauses , add a newline before the assignment.
1823
+ // If there is a before where clause, do not indent, but if there is
1824
+ // only an after where clause, additionally indent the type.
1825
+ if !before_where_predicates . is_empty ( ) {
1818
1826
result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1827
+ } else if !after_where_predicates. is_empty ( ) {
1828
+ result. push_str (
1829
+ & indent
1830
+ . block_indent ( context. config )
1831
+ . to_string_with_newline ( context. config ) ,
1832
+ ) ;
1819
1833
} else {
1820
1834
result. push ( ' ' ) ;
1821
1835
}
@@ -1834,7 +1848,7 @@ fn rewrite_ty<R: Rewrite>(
1834
1848
. unknown_error ( ) ?,
1835
1849
) =>
1836
1850
{
1837
- let comment_shape = if has_where {
1851
+ let comment_shape = if !before_where_predicates . is_empty ( ) {
1838
1852
Shape :: indented ( indent, context. config )
1839
1853
} else {
1840
1854
let shape = Shape :: indented ( indent, context. config ) ;
@@ -1855,13 +1869,39 @@ fn rewrite_ty<R: Rewrite>(
1855
1869
_ => format ! ( "{result}=" ) ,
1856
1870
} ;
1857
1871
1858
- // 1 = `;`
1872
+ // 1 = `;` unless there's a trailing where clause
1859
1873
let shape = Shape :: indented ( indent, context. config ) ;
1860
- let shape = shape. sub_width ( 1 ) . max_width_error ( shape. width , span) ?;
1861
- rewrite_assign_rhs ( context, lhs, & * ty, & RhsAssignKind :: Ty , shape) . map ( |s| s + ";" )
1874
+ let shape = if after_where_predicates. is_empty ( ) {
1875
+ Shape :: indented ( indent, context. config )
1876
+ . sub_width ( 1 )
1877
+ . max_width_error ( shape. width , span) ?
1878
+ } else {
1879
+ shape
1880
+ } ;
1881
+ rewrite_assign_rhs ( context, lhs, & * ty, & RhsAssignKind :: Ty , shape) ?
1862
1882
} else {
1863
- Ok ( format ! ( "{result};" ) )
1883
+ result
1884
+ } ;
1885
+
1886
+ if !after_where_predicates. is_empty ( ) {
1887
+ let option = WhereClauseOption :: new ( true , WhereClauseSpace :: Newline ) ;
1888
+ let after_where_clause_str = rewrite_where_clause (
1889
+ context,
1890
+ after_where_predicates,
1891
+ where_clauses. after . span ,
1892
+ context. config . brace_style ( ) ,
1893
+ Shape :: indented ( indent, context. config ) ,
1894
+ false ,
1895
+ ";" ,
1896
+ None ,
1897
+ rhs_hi,
1898
+ option,
1899
+ ) ?;
1900
+ result. push_str ( & after_where_clause_str) ;
1864
1901
}
1902
+
1903
+ result += ";" ;
1904
+ Ok ( result)
1865
1905
}
1866
1906
1867
1907
fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
0 commit comments