@@ -28,6 +28,7 @@ use crate::shape::{Indent, Shape};
28
28
use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
29
29
use crate :: spanned:: Spanned ;
30
30
use crate :: stmt:: Stmt ;
31
+ use crate :: types:: opaque_ty;
31
32
use crate :: utils:: * ;
32
33
use crate :: vertical:: rewrite_with_alignment;
33
34
use crate :: visitor:: FmtVisitor ;
@@ -581,13 +582,10 @@ impl<'a> FmtVisitor<'a> {
581
582
if self . get_context ( ) . config . reorder_impl_items ( ) {
582
583
type TyOpt = Option < ptr:: P < ast:: Ty > > ;
583
584
use crate :: ast:: AssocItemKind :: * ;
584
- let is_type = |ty : & TyOpt | {
585
- ty. as_ref ( )
586
- . map_or ( true , |t| !matches ! ( t. kind, ast:: TyKind :: ImplTrait ( ..) ) )
587
- } ;
588
- let is_opaque = |ty : & TyOpt | !is_type ( ty) ;
589
- let both_type = |left : & TyOpt , right : & TyOpt | is_type ( left) && is_type ( right) ;
590
- let both_opaque = |left : & TyOpt , right : & TyOpt | is_opaque ( left) && is_opaque ( right) ;
585
+ let is_type = |ty : & TyOpt | opaque_ty ( ty) . is_none ( ) ;
586
+ let is_opaque = |ty : & TyOpt | opaque_ty ( ty) . is_some ( ) ;
587
+ let both_type = |l : & TyOpt , r : & TyOpt | is_type ( l) && is_type ( r) ;
588
+ let both_opaque = |l : & TyOpt , r : & TyOpt | is_opaque ( l) && is_opaque ( r) ;
591
589
let need_empty_line = |a : & ast:: AssocItemKind , b : & ast:: AssocItemKind | match ( a, b) {
592
590
( TyAlias ( lty) , TyAlias ( rty) )
593
591
if both_type ( & lty. ty , & rty. ty ) || both_opaque ( & lty. ty , & rty. ty ) =>
@@ -1508,43 +1506,38 @@ pub(crate) fn rewrite_type_alias<'a, 'b>(
1508
1506
ref bounds,
1509
1507
ref ty,
1510
1508
} = * ty_alias_kind;
1511
- let ty_opt = ty. as_ref ( ) . map ( |t| & * * t ) ;
1509
+ let ty_opt = ty. as_ref ( ) ;
1512
1510
let ( ident, vis) = match visitor_kind {
1513
1511
Item ( i) => ( i. ident , & i. vis ) ,
1514
1512
AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
1515
1513
ForeignItem ( i) => ( i. ident , & i. vis ) ,
1516
1514
} ;
1517
1515
let rw_info = & TyAliasRewriteInfo ( context, indent, generics, ident, span) ;
1518
-
1516
+ let op_ty = opaque_ty ( ty ) ;
1519
1517
// Type Aliases are formatted slightly differently depending on the context
1520
1518
// in which they appear, whether they are opaque, and whether they are associated.
1521
1519
// https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
1522
1520
// https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
1523
- match ( visitor_kind, ty_opt) {
1524
- ( Item ( _) , None ) => {
1525
- let op_ty = OpaqueType { bounds } ;
1526
- rewrite_ty ( rw_info, Some ( bounds) , Some ( & op_ty) , vis)
1521
+ match ( visitor_kind, & op_ty) {
1522
+ ( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , Some ( ref op_bounds) ) => {
1523
+ let op = OpaqueType { bounds : op_bounds } ;
1524
+ rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , vis)
1525
+ }
1526
+ ( Item ( _) | AssocTraitItem ( _) | ForeignItem ( _) , None ) => {
1527
+ rewrite_ty ( rw_info, Some ( bounds) , ty_opt, vis)
1527
1528
}
1528
- ( Item ( _) , Some ( ty) ) => rewrite_ty ( rw_info, Some ( bounds) , Some ( & * ty) , vis) ,
1529
1529
( AssocImplItem ( _) , _) => {
1530
- let result = if let Some ( ast:: Ty {
1531
- kind : ast:: TyKind :: ImplTrait ( _, ref bounds) ,
1532
- ..
1533
- } ) = ty_opt
1534
- {
1535
- let op_ty = OpaqueType { bounds } ;
1536
- rewrite_ty ( rw_info, None , Some ( & op_ty) , & DEFAULT_VISIBILITY )
1530
+ let result = if let Some ( ref op_bounds) = op_ty {
1531
+ let op = OpaqueType { bounds : op_bounds } ;
1532
+ rewrite_ty ( rw_info, Some ( bounds) , Some ( & op) , & DEFAULT_VISIBILITY )
1537
1533
} else {
1538
- rewrite_ty ( rw_info, None , ty . as_ref ( ) , vis)
1534
+ rewrite_ty ( rw_info, Some ( bounds ) , ty_opt , vis)
1539
1535
} ?;
1540
1536
match defaultness {
1541
1537
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
1542
1538
_ => Some ( result) ,
1543
1539
}
1544
1540
}
1545
- ( AssocTraitItem ( _) , _) | ( ForeignItem ( _) , _) => {
1546
- rewrite_ty ( rw_info, Some ( bounds) , ty. as_ref ( ) , vis)
1547
- }
1548
1541
}
1549
1542
}
1550
1543
@@ -1867,6 +1860,12 @@ fn rewrite_static(
1867
1860
Some ( format ! ( "{}{};" , prefix, ty_str) )
1868
1861
}
1869
1862
}
1863
+
1864
+ // FIXME(calebcartwright) - This is a hack around a bug in the handling of TyKind::ImplTrait.
1865
+ // This should be removed once that bug is resolved, with the type alias formatting using the
1866
+ // defined Ty for the RHS directly.
1867
+ // https://github.com/rust-lang/rustfmt/issues/4373
1868
+ // https://github.com/rust-lang/rustfmt/issues/5027
1870
1869
struct OpaqueType < ' a > {
1871
1870
bounds : & ' a ast:: GenericBounds ,
1872
1871
}
0 commit comments