@@ -28,6 +28,7 @@ use crate::shape::{Indent, Shape};
2828use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
2929use crate :: spanned:: Spanned ;
3030use crate :: stmt:: Stmt ;
31+ use crate :: types:: opaque_ty;
3132use crate :: utils:: * ;
3233use crate :: vertical:: rewrite_with_alignment;
3334use crate :: visitor:: FmtVisitor ;
@@ -581,13 +582,10 @@ impl<'a> FmtVisitor<'a> {
581582 if self . get_context ( ) . config . reorder_impl_items ( ) {
582583 type TyOpt = Option < ptr:: P < ast:: Ty > > ;
583584 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) ;
591589 let need_empty_line = |a : & ast:: AssocItemKind , b : & ast:: AssocItemKind | match ( a, b) {
592590 ( TyAlias ( lty) , TyAlias ( rty) )
593591 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>(
15081506 ref bounds,
15091507 ref ty,
15101508 } = * ty_alias_kind;
1511- let ty_opt = ty. as_ref ( ) . map ( |t| & * * t ) ;
1509+ let ty_opt = ty. as_ref ( ) ;
15121510 let ( ident, vis) = match visitor_kind {
15131511 Item ( i) => ( i. ident , & i. vis ) ,
15141512 AssocTraitItem ( i) | AssocImplItem ( i) => ( i. ident , & i. vis ) ,
15151513 ForeignItem ( i) => ( i. ident , & i. vis ) ,
15161514 } ;
15171515 let rw_info = & TyAliasRewriteInfo ( context, indent, generics, ident, span) ;
1518-
1516+ let op_ty = opaque_ty ( ty ) ;
15191517 // Type Aliases are formatted slightly differently depending on the context
15201518 // in which they appear, whether they are opaque, and whether they are associated.
15211519 // https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
15221520 // 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)
15271528 }
1528- ( Item ( _) , Some ( ty) ) => rewrite_ty ( rw_info, Some ( bounds) , Some ( & * ty) , vis) ,
15291529 ( 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 )
15371533 } else {
1538- rewrite_ty ( rw_info, None , ty . as_ref ( ) , vis)
1534+ rewrite_ty ( rw_info, Some ( bounds ) , ty_opt , vis)
15391535 } ?;
15401536 match defaultness {
15411537 ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
15421538 _ => Some ( result) ,
15431539 }
15441540 }
1545- ( AssocTraitItem ( _) , _) | ( ForeignItem ( _) , _) => {
1546- rewrite_ty ( rw_info, Some ( bounds) , ty. as_ref ( ) , vis)
1547- }
15481541 }
15491542}
15501543
@@ -1867,6 +1860,12 @@ fn rewrite_static(
18671860 Some ( format ! ( "{}{};" , prefix, ty_str) )
18681861 }
18691862}
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
18701869struct OpaqueType < ' a > {
18711870 bounds : & ' a ast:: GenericBounds ,
18721871}
0 commit comments