@@ -1562,6 +1562,7 @@ fn rewrite_type_prefix(
15621562 prefix : & str ,
15631563 ident : ast:: Ident ,
15641564 generics : & ast:: Generics ,
1565+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
15651566) -> Option < String > {
15661567 let mut result = String :: with_capacity ( 128 ) ;
15671568 result. push_str ( prefix) ;
@@ -1578,6 +1579,19 @@ fn rewrite_type_prefix(
15781579 result. push_str ( & generics_str) ;
15791580 }
15801581
1582+ let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1583+ if bounds. is_empty ( ) {
1584+ String :: new ( )
1585+ } else {
1586+ // 2 = `: `
1587+ let shape = Shape :: indented ( indent, context. config ) . offset_left ( result. len ( ) + 2 ) ?;
1588+ bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1589+ }
1590+ } else {
1591+ String :: new ( )
1592+ } ;
1593+ result. push_str ( & type_bounds_str) ;
1594+
15811595 let where_budget = context. budget ( last_line_width ( & result) ) ;
15821596 let option = WhereClauseOption :: snuggled ( & result) ;
15831597 let where_clause_str = rewrite_where_clause (
@@ -1604,6 +1618,7 @@ fn rewrite_type_item<R: Rewrite>(
16041618 ident : ast:: Ident ,
16051619 rhs : & R ,
16061620 generics : & ast:: Generics ,
1621+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
16071622 vis : & ast:: Visibility ,
16081623) -> Option < String > {
16091624 let mut result = String :: with_capacity ( 128 ) ;
@@ -1613,6 +1628,7 @@ fn rewrite_type_item<R: Rewrite>(
16131628 & format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
16141629 ident,
16151630 generics,
1631+ generic_bounds_opt,
16161632 ) ?) ;
16171633
16181634 if generics. where_clause . predicates . is_empty ( ) {
@@ -1627,17 +1643,6 @@ fn rewrite_type_item<R: Rewrite>(
16271643 rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
16281644}
16291645
1630- pub ( crate ) fn rewrite_type_alias (
1631- context : & RewriteContext < ' _ > ,
1632- indent : Indent ,
1633- ident : ast:: Ident ,
1634- ty : & ast:: Ty ,
1635- generics : & ast:: Generics ,
1636- vis : & ast:: Visibility ,
1637- ) -> Option < String > {
1638- rewrite_type_item ( context, indent, "type" , " =" , ident, ty, generics, vis)
1639- }
1640-
16411646pub ( crate ) fn rewrite_opaque_type (
16421647 context : & RewriteContext < ' _ > ,
16431648 indent : Indent ,
@@ -1655,6 +1660,7 @@ pub(crate) fn rewrite_opaque_type(
16551660 ident,
16561661 & opaque_type_bounds,
16571662 generics,
1663+ Some ( generic_bounds) ,
16581664 vis,
16591665 )
16601666}
@@ -1897,39 +1903,39 @@ fn rewrite_static(
18971903 }
18981904}
18991905
1900- pub ( crate ) fn rewrite_associated_type (
1906+ pub ( crate ) fn rewrite_type_alias (
19011907 ident : ast:: Ident ,
19021908 ty_opt : Option < & ptr:: P < ast:: Ty > > ,
19031909 generics : & ast:: Generics ,
19041910 generic_bounds_opt : Option < & ast:: GenericBounds > ,
19051911 context : & RewriteContext < ' _ > ,
19061912 indent : Indent ,
1913+ vis : & ast:: Visibility ,
19071914) -> Option < String > {
1908- let ident_str = rewrite_ident ( context, ident) ;
1909- // 5 = "type "
1910- let generics_shape = Shape :: indented ( indent, context. config ) . offset_left ( 5 ) ?;
1911- let generics_str = rewrite_generics ( context, ident_str, generics, generics_shape) ?;
1912- let prefix = format ! ( "type {}" , generics_str) ;
1913-
1914- let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1915- if bounds. is_empty ( ) {
1916- String :: new ( )
1917- } else {
1918- // 2 = ": ".len()
1919- let shape = Shape :: indented ( indent, context. config ) . offset_left ( prefix. len ( ) + 2 ) ?;
1920- bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1921- }
1922- } else {
1923- String :: new ( )
1924- } ;
1915+ let mut prefix = rewrite_type_prefix (
1916+ context,
1917+ indent,
1918+ & format ! ( "{}type " , format_visibility( context, vis) ) ,
1919+ ident,
1920+ generics,
1921+ generic_bounds_opt,
1922+ ) ?;
19251923
19261924 if let Some ( ty) = ty_opt {
19271925 // 1 = `;`
19281926 let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1929- let lhs = format ! ( "{}{} =" , prefix, type_bounds_str) ;
1927+
1928+ // If there's a where clause, add a newline before the assignment. Otherwise just add a
1929+ // space.
1930+ if !generics. where_clause . predicates . is_empty ( ) {
1931+ prefix. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1932+ } else {
1933+ prefix. push ( ' ' ) ;
1934+ }
1935+ let lhs = format ! ( "{}=" , prefix) ;
19301936 rewrite_assign_rhs ( context, lhs, & * * ty, shape) . map ( |s| s + ";" )
19311937 } else {
1932- Some ( format ! ( "{}{} ;" , prefix, type_bounds_str ) )
1938+ Some ( format ! ( "{};" , prefix) )
19331939 }
19341940}
19351941
@@ -1973,13 +1979,14 @@ pub(crate) fn rewrite_opaque_impl_type(
19731979
19741980pub ( crate ) fn rewrite_associated_impl_type (
19751981 ident : ast:: Ident ,
1982+ vis : & ast:: Visibility ,
19761983 defaultness : ast:: Defaultness ,
19771984 ty_opt : Option < & ptr:: P < ast:: Ty > > ,
19781985 generics : & ast:: Generics ,
19791986 context : & RewriteContext < ' _ > ,
19801987 indent : Indent ,
19811988) -> Option < String > {
1982- let result = rewrite_associated_type ( ident, ty_opt, generics, None , context, indent) ?;
1989+ let result = rewrite_type_alias ( ident, ty_opt, generics, None , context, indent, vis ) ?;
19831990
19841991 match defaultness {
19851992 ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
@@ -3132,7 +3139,7 @@ impl Rewrite for ast::ForeignItem {
31323139 // FIXME: this may be a faulty span from libsyntax.
31333140 let span = mk_sp ( self . span . lo ( ) , self . span . hi ( ) - BytePos ( 1 ) ) ;
31343141
3135- let item_str = match self . kind {
3142+ let item_str: String = match self . kind {
31363143 ast:: ForeignItemKind :: Fn ( _, ref fn_sig, ref generics, _) => rewrite_fn_base (
31373144 context,
31383145 shape. indent ,
@@ -3156,14 +3163,20 @@ impl Rewrite for ast::ForeignItem {
31563163 // 1 = ;
31573164 rewrite_assign_rhs ( context, prefix, & * * ty, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
31583165 }
3159- ast:: ForeignItemKind :: TyAlias ( ..) => {
3160- let vis = format_visibility ( context, & self . vis ) ;
3161- Some ( format ! (
3162- "{}type {};" ,
3163- vis,
3164- rewrite_ident( context, self . ident)
3165- ) )
3166- }
3166+ ast:: ForeignItemKind :: TyAlias (
3167+ _,
3168+ ref generics,
3169+ ref generic_bounds,
3170+ ref type_default,
3171+ ) => rewrite_type_alias (
3172+ self . ident ,
3173+ type_default. as_ref ( ) ,
3174+ generics,
3175+ Some ( generic_bounds) ,
3176+ & context,
3177+ shape. indent ,
3178+ & self . vis ,
3179+ ) ,
31673180 ast:: ForeignItemKind :: MacCall ( ref mac) => {
31683181 rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
31693182 }
0 commit comments