@@ -1185,18 +1185,6 @@ pub(crate) fn format_trait(
1185
1185
}
1186
1186
}
1187
1187
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
-
1200
1188
pub ( crate ) struct TraitAliasBounds < ' a > {
1201
1189
generic_bounds : & ' a ast:: GenericBounds ,
1202
1190
generics : & ' a ast:: Generics ,
@@ -1518,17 +1506,79 @@ fn format_tuple_struct(
1518
1506
Some ( result)
1519
1507
}
1520
1508
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 > ,
1523
1527
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 < ' _ , ' _ > ,
1527
1576
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1528
1577
rhs : Option < & R > ,
1529
- span : Span ,
1578
+ vis : & ast :: Visibility ,
1530
1579
) -> Option < String > {
1531
1580
let mut result = String :: with_capacity ( 128 ) ;
1581
+ let TyAliasRewriteInfo ( context, indent, generics, ident, span) = * rw_info;
1532
1582
result. push_str ( & format ! ( "{}type " , format_visibility( context, vis) ) ) ;
1533
1583
let ident_str = rewrite_ident ( context, ident) ;
1534
1584
@@ -1616,28 +1666,6 @@ pub(crate) fn rewrite_type<R: Rewrite>(
1616
1666
}
1617
1667
}
1618
1668
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
-
1641
1669
fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
1642
1670
(
1643
1671
if config. space_before_colon ( ) { " " } else { "" } ,
@@ -1863,54 +1891,18 @@ fn rewrite_static(
1863
1891
}
1864
1892
}
1865
1893
struct OpaqueType < ' a > {
1866
- bounds : & ' a ast:: GenericBounds ,
1894
+ generic_bounds : & ' a ast:: GenericBounds ,
1867
1895
}
1868
1896
1869
1897
impl < ' a > Rewrite for OpaqueType < ' a > {
1870
1898
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1871
1899
let shape = shape. offset_left ( 5 ) ?; // `impl `
1872
- self . bounds
1900
+ self . generic_bounds
1873
1901
. rewrite ( context, shape)
1874
1902
. map ( |s| format ! ( "impl {}" , s) )
1875
1903
}
1876
1904
}
1877
1905
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
-
1914
1906
impl Rewrite for ast:: FnRetTy {
1915
1907
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1916
1908
match * self {
@@ -3176,19 +3168,9 @@ impl Rewrite for ast::ForeignItem {
3176
3168
// 1 = ;
3177
3169
rewrite_assign_rhs ( context, prefix, & * * ty, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
3178
3170
}
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)
3192
3174
}
3193
3175
ast:: ForeignItemKind :: MacCall ( ref mac) => {
3194
3176
rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
0 commit comments