@@ -1185,18 +1185,6 @@ pub(crate) fn format_trait(
11851185 }
11861186}
11871187
1188- struct OpaqueTypeBounds < ' a > {
1189- generic_bounds : & ' a ast:: GenericBounds ,
1190- }
1191-
1192- impl < ' a > Rewrite for OpaqueTypeBounds < ' a > {
1193- fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1194- self . generic_bounds
1195- . rewrite ( context, shape)
1196- . map ( |s| format ! ( "impl {}" , s) )
1197- }
1198- }
1199-
12001188pub ( crate ) struct TraitAliasBounds < ' a > {
12011189 generic_bounds : & ' a ast:: GenericBounds ,
12021190 generics : & ' a ast:: Generics ,
@@ -1518,17 +1506,79 @@ fn format_tuple_struct(
15181506 Some ( result)
15191507}
15201508
1521- pub ( crate ) fn rewrite_type < R : Rewrite > (
1522- context : & RewriteContext < ' _ > ,
1509+ pub ( crate ) enum ItemVisitorKind < ' a > {
1510+ Item ( & ' a ast:: Item ) ,
1511+ AssocTraitItem ( & ' a ast:: AssocItem ) ,
1512+ AssocImplItem ( & ' a ast:: AssocItem ) ,
1513+ ForeignItem ( & ' a ast:: ForeignItem ) ,
1514+ }
1515+
1516+ struct TyAliasRewriteInfo < ' c , ' g > (
1517+ & ' c RewriteContext < ' c > ,
1518+ Indent ,
1519+ & ' g ast:: Generics ,
1520+ symbol:: Ident ,
1521+ Span ,
1522+ ) ;
1523+
1524+ pub ( crate ) fn rewrite_type_alias < ' a , ' b > (
1525+ ty_alias_kind : & ast:: TyAliasKind ,
1526+ context : & RewriteContext < ' a > ,
15231527 indent : Indent ,
1524- ident : symbol:: Ident ,
1525- vis : & ast:: Visibility ,
1526- generics : & ast:: Generics ,
1528+ visitor_kind : & ItemVisitorKind < ' b > ,
1529+ span : Span ,
1530+ ) -> Option < String > {
1531+ use ItemVisitorKind :: * ;
1532+
1533+ let ast:: TyAliasKind ( defaultness, ref generics, ref generic_bounds, ref ty) = * ty_alias_kind;
1534+ let ty_opt = ty. as_ref ( ) . map ( |t| & * * t) ;
1535+ let ( ident, vis) = match visitor_kind {
1536+ Item ( i) => ( i. ident , & i. vis ) ,
1537+ AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
1538+ ForeignItem ( i) => ( i. ident , & i. vis ) ,
1539+ } ;
1540+ let rw_info = & TyAliasRewriteInfo ( context, indent, generics, ident, span) ;
1541+
1542+ // Type Aliases are formatted slightly differently depending on the context
1543+ // in which they appear, whether they are opaque, and whether they are associated.
1544+ // https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
1545+ // https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
1546+ match ( visitor_kind, ty_opt) {
1547+ ( Item ( _) , None ) => {
1548+ let op_ty = OpaqueType { generic_bounds } ;
1549+ rewrite_ty ( rw_info, Some ( generic_bounds) , Some ( & op_ty) , vis)
1550+ }
1551+ ( Item ( _) , Some ( ty) ) => rewrite_ty ( rw_info, Some ( generic_bounds) , Some ( & * ty) , vis) ,
1552+ ( AssocImplItem ( _) , _) => {
1553+ let result = if let Some ( ast:: Ty {
1554+ kind : ast:: TyKind :: ImplTrait ( _, ref generic_bounds) ,
1555+ ..
1556+ } ) = ty_opt
1557+ {
1558+ let op_ty = OpaqueType { generic_bounds } ;
1559+ rewrite_ty ( rw_info, None , Some ( & op_ty) , & DEFAULT_VISIBILITY )
1560+ } else {
1561+ rewrite_ty ( rw_info, None , ty. as_ref ( ) , vis)
1562+ } ?;
1563+ match defaultness {
1564+ ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
1565+ _ => Some ( result) ,
1566+ }
1567+ }
1568+ ( AssocTraitItem ( _) , _) | ( ForeignItem ( _) , _) => {
1569+ rewrite_ty ( rw_info, Some ( generic_bounds) , ty. as_ref ( ) , vis)
1570+ }
1571+ }
1572+ }
1573+
1574+ fn rewrite_ty < R : Rewrite > (
1575+ rw_info : & TyAliasRewriteInfo < ' _ , ' _ > ,
15271576 generic_bounds_opt : Option < & ast:: GenericBounds > ,
15281577 rhs : Option < & R > ,
1529- span : Span ,
1578+ vis : & ast :: Visibility ,
15301579) -> Option < String > {
15311580 let mut result = String :: with_capacity ( 128 ) ;
1581+ let TyAliasRewriteInfo ( context, indent, generics, ident, span) = * rw_info;
15321582 result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
15331583 let ident_str = rewrite_ident ( context, ident) ;
15341584
@@ -1616,28 +1666,6 @@ pub(crate) fn rewrite_type<R: Rewrite>(
16161666 }
16171667}
16181668
1619- pub ( crate ) fn rewrite_opaque_type (
1620- context : & RewriteContext < ' _ > ,
1621- indent : Indent ,
1622- ident : symbol:: Ident ,
1623- generic_bounds : & ast:: GenericBounds ,
1624- generics : & ast:: Generics ,
1625- vis : & ast:: Visibility ,
1626- span : Span ,
1627- ) -> Option < String > {
1628- let opaque_type_bounds = OpaqueTypeBounds { generic_bounds } ;
1629- rewrite_type (
1630- context,
1631- indent,
1632- ident,
1633- vis,
1634- generics,
1635- Some ( generic_bounds) ,
1636- Some ( & opaque_type_bounds) ,
1637- span,
1638- )
1639- }
1640-
16411669fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
16421670 (
16431671 if config. space_before_colon ( ) { " " } else { "" } ,
@@ -1863,54 +1891,18 @@ fn rewrite_static(
18631891 }
18641892}
18651893struct OpaqueType < ' a > {
1866- bounds : & ' a ast:: GenericBounds ,
1894+ generic_bounds : & ' a ast:: GenericBounds ,
18671895}
18681896
18691897impl < ' a > Rewrite for OpaqueType < ' a > {
18701898 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
18711899 let shape = shape. offset_left ( 5 ) ?; // `impl `
1872- self . bounds
1900+ self . generic_bounds
18731901 . rewrite ( context, shape)
18741902 . map ( |s| format ! ( "impl {}" , s) )
18751903 }
18761904}
18771905
1878- pub ( crate ) fn rewrite_impl_type (
1879- ident : symbol:: Ident ,
1880- vis : & ast:: Visibility ,
1881- defaultness : ast:: Defaultness ,
1882- ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1883- generics : & ast:: Generics ,
1884- context : & RewriteContext < ' _ > ,
1885- indent : Indent ,
1886- span : Span ,
1887- ) -> Option < String > {
1888- // Opaque type
1889- let result = if let Some ( rustc_ast:: ast:: Ty {
1890- kind : ast:: TyKind :: ImplTrait ( _, ref bounds) ,
1891- ..
1892- } ) = ty_opt. map ( |t| & * * t)
1893- {
1894- rewrite_type (
1895- context,
1896- indent,
1897- ident,
1898- & DEFAULT_VISIBILITY ,
1899- generics,
1900- None ,
1901- Some ( & OpaqueType { bounds } ) ,
1902- span,
1903- )
1904- } else {
1905- rewrite_type ( context, indent, ident, vis, generics, None , ty_opt, span)
1906- } ?;
1907-
1908- match defaultness {
1909- ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
1910- _ => Some ( result) ,
1911- }
1912- }
1913-
19141906impl Rewrite for ast:: FnRetTy {
19151907 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
19161908 match * self {
@@ -3176,19 +3168,9 @@ impl Rewrite for ast::ForeignItem {
31763168 // 1 = ;
31773169 rewrite_assign_rhs ( context, prefix, & * * ty, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
31783170 }
3179- ast:: ForeignItemKind :: TyAlias ( ref ty_alias_kind) => {
3180- let ast:: TyAliasKind ( _, ref generics, ref generic_bounds, ref type_default) =
3181- * * ty_alias_kind;
3182- rewrite_type (
3183- context,
3184- shape. indent ,
3185- self . ident ,
3186- & self . vis ,
3187- generics,
3188- Some ( generic_bounds) ,
3189- type_default. as_ref ( ) ,
3190- self . span ,
3191- )
3171+ ast:: ForeignItemKind :: TyAlias ( ref ty_alias) => {
3172+ let ( kind, span) = ( & ItemVisitorKind :: ForeignItem ( & self ) , self . span ) ;
3173+ rewrite_type_alias ( ty_alias, context, shape. indent , kind, span)
31923174 }
31933175 ast:: ForeignItemKind :: MacCall ( ref mac) => {
31943176 rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
0 commit comments