@@ -406,7 +406,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
406
406
407
407
p ! ( print( self_ty) ) ;
408
408
if let Some ( trait_ref) = trait_ref {
409
- p ! ( write( " as " ) , print( trait_ref) ) ;
409
+ p ! ( write( " as " ) , print( trait_ref. print_only_trait_path ( ) ) ) ;
410
410
}
411
411
Ok ( cx)
412
412
} )
@@ -425,7 +425,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
425
425
426
426
p ! ( write( "impl " ) ) ;
427
427
if let Some ( trait_ref) = trait_ref {
428
- p ! ( print( trait_ref) , write( " for " ) ) ;
428
+ p ! ( print( trait_ref. print_only_trait_path ( ) ) , write( " for " ) ) ;
429
429
}
430
430
p ! ( print( self_ty) ) ;
431
431
@@ -559,7 +559,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
559
559
560
560
p ! (
561
561
write( "{}" , if first { " " } else { "+" } ) ,
562
- print( trait_ref) ) ;
562
+ print( trait_ref. print_only_trait_path ( ) ) ) ;
563
563
first = false ;
564
564
}
565
565
}
@@ -1395,6 +1395,52 @@ impl<'gcx: 'tcx, 'tcx, T, U, P: PrettyPrinter<'gcx, 'tcx>> Print<'gcx, 'tcx, P>
1395
1395
}
1396
1396
}
1397
1397
1398
+ /// Wrapper type for `ty::TraitRef` which opts-in to pretty printing only
1399
+ /// the trait path. That is, it will print `Trait<U>` instead of
1400
+ /// `<T as Trait<U>>`.
1401
+ #[ derive( Copy , Clone , PartialEq , Eq , Hash , RustcEncodable , RustcDecodable , HashStable ) ]
1402
+ pub struct TraitRefPrintOnlyTraitPath < ' tcx > ( ty:: TraitRef < ' tcx > ) ;
1403
+
1404
+ impl TraitRefPrintOnlyTraitPath < ' tcx > {
1405
+ pub fn self_ty ( & self ) -> ty:: Ty < ' tcx > {
1406
+ self . 0 . self_ty ( )
1407
+ }
1408
+ }
1409
+
1410
+ impl fmt:: Debug for TraitRefPrintOnlyTraitPath < ' tcx > {
1411
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1412
+ fmt:: Display :: fmt ( self , f)
1413
+ }
1414
+ }
1415
+
1416
+ impl < ' a , ' tcx > ty:: Lift < ' tcx > for TraitRefPrintOnlyTraitPath < ' a > {
1417
+ type Lifted = TraitRefPrintOnlyTraitPath < ' tcx > ;
1418
+ fn lift_to_tcx < ' b , ' gcx > ( & self , tcx : TyCtxt < ' b , ' gcx , ' tcx > ) -> Option < Self :: Lifted > {
1419
+ tcx. lift ( & self . 0 . substs ) . map ( |substs| TraitRefPrintOnlyTraitPath ( ty:: TraitRef {
1420
+ def_id : self . 0 . def_id ,
1421
+ substs,
1422
+ } ) )
1423
+ }
1424
+ }
1425
+
1426
+ TupleStructTypeFoldableImpl ! {
1427
+ impl <' tcx> TypeFoldable <' tcx> for TraitRefPrintOnlyTraitPath <' tcx> {
1428
+ a
1429
+ }
1430
+ }
1431
+
1432
+ impl ty:: TraitRef < ' tcx > {
1433
+ pub fn print_only_trait_path ( self ) -> TraitRefPrintOnlyTraitPath < ' tcx > {
1434
+ TraitRefPrintOnlyTraitPath ( self )
1435
+ }
1436
+ }
1437
+
1438
+ impl ty:: PolyTraitRef < ' tcx > {
1439
+ pub fn print_only_trait_path ( self ) -> ty:: Binder < TraitRefPrintOnlyTraitPath < ' tcx > > {
1440
+ self . map_bound ( |x| TraitRefPrintOnlyTraitPath ( x) )
1441
+ }
1442
+ }
1443
+
1398
1444
macro_rules! forward_display_to_print {
1399
1445
( $( $ty: ty) ,+) => {
1400
1446
$( impl fmt:: Display for $ty {
@@ -1447,6 +1493,7 @@ forward_display_to_print! {
1447
1493
// because `for<'gcx: 'tcx, 'tcx>` isn't possible yet.
1448
1494
ty:: Binder <& ' tcx ty:: List <ty:: ExistentialPredicate <' tcx>>>,
1449
1495
ty:: Binder <ty:: TraitRef <' tcx>>,
1496
+ ty:: Binder <TraitRefPrintOnlyTraitPath <' tcx>>,
1450
1497
ty:: Binder <ty:: FnSig <' tcx>>,
1451
1498
ty:: Binder <ty:: TraitPredicate <' tcx>>,
1452
1499
ty:: Binder <ty:: SubtypePredicate <' tcx>>,
@@ -1482,7 +1529,7 @@ define_print_and_forward_display! {
1482
1529
// Use a type that can't appear in defaults of type parameters.
1483
1530
let dummy_self = cx. tcx( ) . mk_infer( ty:: FreshTy ( 0 ) ) ;
1484
1531
let trait_ref = self . with_self_ty( cx. tcx( ) , dummy_self) ;
1485
- p!( print( trait_ref) )
1532
+ p!( print( trait_ref. print_only_trait_path ( ) ) )
1486
1533
}
1487
1534
1488
1535
ty:: ExistentialProjection <' tcx> {
@@ -1528,7 +1575,11 @@ define_print_and_forward_display! {
1528
1575
}
1529
1576
1530
1577
ty:: TraitRef <' tcx> {
1531
- p!( print_def_path( self . def_id, self . substs) ) ;
1578
+ p!( write( "<{} as {}>" , self . self_ty( ) , self ) )
1579
+ }
1580
+
1581
+ TraitRefPrintOnlyTraitPath <' tcx> {
1582
+ p!( print_def_path( self . 0 . def_id, self . 0 . substs) ) ;
1532
1583
}
1533
1584
1534
1585
& ' tcx ty:: Const <' tcx> {
@@ -1553,7 +1604,7 @@ define_print_and_forward_display! {
1553
1604
}
1554
1605
1555
1606
ty:: TraitPredicate <' tcx> {
1556
- p!( print( self . trait_ref. self_ty( ) ) , write( ": " ) , print( self . trait_ref) )
1607
+ p!( print( self . trait_ref. self_ty( ) ) , write( ": " ) , print( self . trait_ref. print_only_trait_path ( ) ) )
1557
1608
}
1558
1609
1559
1610
ty:: ProjectionPredicate <' tcx> {
0 commit comments