@@ -1682,6 +1682,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16821682 where_clauses,
16831683 } = * ty_alias_kind;
16841684 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 ( ) ) ;
16851688 let ( ident, vis) = match visitor_kind {
16861689 Item ( i) => ( i. ident , & i. vis ) ,
16871690 AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
@@ -1696,17 +1699,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
16961699 match ( visitor_kind, & op_ty) {
16971700 ( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , Some ( op_bounds) ) => {
16981701 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)
17001703 }
17011704 ( 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)
17031706 }
17041707 ( AssocImplItem ( _) , _) => {
17051708 let result = if let Some ( op_bounds) = op_ty {
17061709 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+ )
17081717 } else {
1709- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1718+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
17101719 } ?;
17111720 match defaultness {
17121721 ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {result}" ) ) ,
@@ -1720,6 +1729,8 @@ fn rewrite_ty<R: Rewrite>(
17201729 rw_info : & TyAliasRewriteInfo < ' _ , ' _ > ,
17211730 generic_bounds_opt : Option < & ast:: GenericBounds > ,
17221731 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 ,
17231734 vis : & ast:: Visibility ,
17241735) -> Option < String > {
17251736 let mut result = String :: with_capacity ( 128 ) ;
@@ -1728,9 +1739,6 @@ fn rewrite_ty<R: Rewrite>(
17281739 . where_clause
17291740 . predicates
17301741 . split_at ( where_clauses. split ) ;
1731- if !after_where_predicates. is_empty ( ) {
1732- return None ;
1733- }
17341742 result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
17351743 let ident_str = rewrite_ident ( context, ident) ;
17361744
@@ -1759,7 +1767,7 @@ fn rewrite_ty<R: Rewrite>(
17591767 if rhs. is_none ( ) {
17601768 option. suppress_comma ( ) ;
17611769 }
1762- let where_clause_str = rewrite_where_clause (
1770+ let before_where_clause_str = rewrite_where_clause (
17631771 context,
17641772 before_where_predicates,
17651773 where_clauses. before . span ,
@@ -1771,14 +1779,20 @@ fn rewrite_ty<R: Rewrite>(
17711779 generics. span . hi ( ) ,
17721780 option,
17731781 ) ?;
1774- result. push_str ( & where_clause_str ) ;
1782+ result. push_str ( & before_where_clause_str ) ;
17751783
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 ( ) {
17811789 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+ ) ;
17821796 } else {
17831797 result. push ( ' ' ) ;
17841798 }
@@ -1792,7 +1806,7 @@ fn rewrite_ty<R: Rewrite>(
17921806 Some ( comment_span)
17931807 if contains_comment ( context. snippet_provider . span_to_snippet ( comment_span) ?) =>
17941808 {
1795- let comment_shape = if has_where {
1809+ let comment_shape = if !before_where_predicates . is_empty ( ) {
17961810 Shape :: indented ( indent, context. config )
17971811 } else {
17981812 Shape :: indented ( indent, context. config )
@@ -1811,12 +1825,36 @@ fn rewrite_ty<R: Rewrite>(
18111825 _ => format ! ( "{result}=" ) ,
18121826 } ;
18131827
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) ?
18171835 } 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) ;
18191854 }
1855+
1856+ result += ";" ;
1857+ Some ( result)
18201858}
18211859
18221860fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
0 commit comments