@@ -1712,6 +1712,9 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
17121712 where_clauses,
17131713 } = * ty_alias_kind;
17141714 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 ( ) ) ;
17151718 let ( ident, vis) = match visitor_kind {
17161719 Item ( i) => ( i. ident , & i. vis ) ,
17171720 AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
@@ -1726,17 +1729,23 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
17261729 match ( visitor_kind, & op_ty) {
17271730 ( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , Some ( op_bounds) ) => {
17281731 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)
17301733 }
17311734 ( 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)
17331736 }
17341737 ( AssocImplItem ( _) , _) => {
17351738 let result = if let Some ( op_bounds) = op_ty {
17361739 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+ )
17381747 } else {
1739- rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1748+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, rhs_hi , vis)
17401749 } ?;
17411750 match defaultness {
17421751 ast:: Defaultness :: Default ( ..) => Ok ( format ! ( "default {result}" ) ) ,
@@ -1750,6 +1759,8 @@ fn rewrite_ty<R: Rewrite>(
17501759 rw_info : & TyAliasRewriteInfo < ' _ , ' _ > ,
17511760 generic_bounds_opt : Option < & ast:: GenericBounds > ,
17521761 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 ,
17531764 vis : & ast:: Visibility ,
17541765) -> RewriteResult {
17551766 let mut result = String :: with_capacity ( 128 ) ;
@@ -1758,9 +1769,6 @@ fn rewrite_ty<R: Rewrite>(
17581769 . where_clause
17591770 . predicates
17601771 . split_at ( where_clauses. split ) ;
1761- if !after_where_predicates. is_empty ( ) {
1762- return Err ( RewriteError :: Unknown ) ;
1763- }
17641772 result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
17651773 let ident_str = rewrite_ident ( context, ident) ;
17661774
@@ -1796,7 +1804,7 @@ fn rewrite_ty<R: Rewrite>(
17961804 if rhs. is_none ( ) {
17971805 option. suppress_comma ( ) ;
17981806 }
1799- let where_clause_str = rewrite_where_clause (
1807+ let before_where_clause_str = rewrite_where_clause (
18001808 context,
18011809 before_where_predicates,
18021810 where_clauses. before . span ,
@@ -1808,14 +1816,20 @@ fn rewrite_ty<R: Rewrite>(
18081816 generics. span . hi ( ) ,
18091817 option,
18101818 ) ?;
1811- result. push_str ( & where_clause_str ) ;
1819+ result. push_str ( & before_where_clause_str ) ;
18121820
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 ( ) {
18181826 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+ ) ;
18191833 } else {
18201834 result. push ( ' ' ) ;
18211835 }
@@ -1834,7 +1848,7 @@ fn rewrite_ty<R: Rewrite>(
18341848 . unknown_error ( ) ?,
18351849 ) =>
18361850 {
1837- let comment_shape = if has_where {
1851+ let comment_shape = if !before_where_predicates . is_empty ( ) {
18381852 Shape :: indented ( indent, context. config )
18391853 } else {
18401854 let shape = Shape :: indented ( indent, context. config ) ;
@@ -1855,13 +1869,39 @@ fn rewrite_ty<R: Rewrite>(
18551869 _ => format ! ( "{result}=" ) ,
18561870 } ;
18571871
1858- // 1 = `;`
1872+ // 1 = `;` unless there's a trailing where clause
18591873 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) ?
18621882 } 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) ;
18641901 }
1902+
1903+ result += ";" ;
1904+ Ok ( result)
18651905}
18661906
18671907fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
0 commit comments