@@ -1545,11 +1545,11 @@ pub trait PrettyPrinter<'tcx>:
1545
1545
}
1546
1546
1547
1547
// HACK(eddyb) boxed to avoid moving around a large struct by-value.
1548
- pub struct FmtPrinter < ' a , ' tcx , F > ( Box < FmtPrinterData < ' a , ' tcx , F > > ) ;
1548
+ pub struct FmtPrinter < ' a , ' tcx > ( Box < FmtPrinterData < ' a , ' tcx > > ) ;
1549
1549
1550
- pub struct FmtPrinterData < ' a , ' tcx , F > {
1550
+ pub struct FmtPrinterData < ' a , ' tcx > {
1551
1551
tcx : TyCtxt < ' tcx > ,
1552
- fmt : F ,
1552
+ fmt : String ,
1553
1553
1554
1554
empty_path : bool ,
1555
1555
in_value : bool ,
@@ -1566,24 +1566,24 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
1566
1566
pub const_infer_name_resolver : Option < Box < dyn Fn ( ty:: ConstVid < ' tcx > ) -> Option < String > + ' a > > ,
1567
1567
}
1568
1568
1569
- impl < ' a , ' tcx , F > Deref for FmtPrinter < ' a , ' tcx , F > {
1570
- type Target = FmtPrinterData < ' a , ' tcx , F > ;
1569
+ impl < ' a , ' tcx > Deref for FmtPrinter < ' a , ' tcx > {
1570
+ type Target = FmtPrinterData < ' a , ' tcx > ;
1571
1571
fn deref ( & self ) -> & Self :: Target {
1572
1572
& self . 0
1573
1573
}
1574
1574
}
1575
1575
1576
- impl < F > DerefMut for FmtPrinter < ' _ , ' _ , F > {
1576
+ impl DerefMut for FmtPrinter < ' _ , ' _ > {
1577
1577
fn deref_mut ( & mut self ) -> & mut Self :: Target {
1578
1578
& mut self . 0
1579
1579
}
1580
1580
}
1581
1581
1582
- impl < ' a , ' tcx , F > FmtPrinter < ' a , ' tcx , F > {
1583
- pub fn new ( tcx : TyCtxt < ' tcx > , fmt : F , ns : Namespace ) -> Self {
1582
+ impl < ' a , ' tcx > FmtPrinter < ' a , ' tcx > {
1583
+ pub fn new ( tcx : TyCtxt < ' tcx > , ns : Namespace ) -> Self {
1584
1584
FmtPrinter ( Box :: new ( FmtPrinterData {
1585
1585
tcx,
1586
- fmt,
1586
+ fmt : String :: new ( ) ,
1587
1587
empty_path : false ,
1588
1588
in_value : ns == Namespace :: ValueNS ,
1589
1589
print_alloc_ids : false ,
@@ -1596,6 +1596,10 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
1596
1596
const_infer_name_resolver : None ,
1597
1597
} ) )
1598
1598
}
1599
+
1600
+ pub fn into_buffer ( self ) -> String {
1601
+ self . 0 . fmt
1602
+ }
1599
1603
}
1600
1604
1601
1605
// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
@@ -1627,19 +1631,18 @@ impl<'t> TyCtxt<'t> {
1627
1631
pub fn def_path_str_with_substs ( self , def_id : DefId , substs : & ' t [ GenericArg < ' t > ] ) -> String {
1628
1632
let ns = guess_def_namespace ( self , def_id) ;
1629
1633
debug ! ( "def_path_str: def_id={:?}, ns={:?}" , def_id, ns) ;
1630
- let mut s = String :: new ( ) ;
1631
- let _ = FmtPrinter :: new ( self , & mut s, ns) . print_def_path ( def_id, substs) ;
1632
- s
1634
+ FmtPrinter :: new ( self , ns) . print_def_path ( def_id, substs) . unwrap ( ) . into_buffer ( )
1633
1635
}
1634
1636
}
1635
1637
1636
- impl < F : fmt:: Write > fmt :: Write for FmtPrinter < ' _ , ' _ , F > {
1638
+ impl fmt:: Write for FmtPrinter < ' _ , ' _ > {
1637
1639
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1638
- self . fmt . write_str ( s)
1640
+ self . fmt . push_str ( s) ;
1641
+ Ok ( ( ) )
1639
1642
}
1640
1643
}
1641
1644
1642
- impl < ' tcx , F : fmt :: Write > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1645
+ impl < ' tcx > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx > {
1643
1646
type Error = fmt:: Error ;
1644
1647
1645
1648
type Path = Self ;
@@ -1847,7 +1850,7 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
1847
1850
}
1848
1851
}
1849
1852
1850
- impl < ' tcx , F : fmt :: Write > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1853
+ impl < ' tcx > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx > {
1851
1854
fn ty_infer_name ( & self , id : ty:: TyVid ) -> Option < String > {
1852
1855
self . 0 . ty_infer_name_resolver . as_ref ( ) . and_then ( |func| func ( id) )
1853
1856
}
@@ -1983,7 +1986,7 @@ impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1983
1986
}
1984
1987
1985
1988
// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
1986
- impl < F : fmt :: Write > FmtPrinter < ' _ , ' _ , F > {
1989
+ impl FmtPrinter < ' _ , ' _ > {
1987
1990
pub fn pretty_print_region ( mut self , region : ty:: Region < ' _ > ) -> Result < Self , fmt:: Error > {
1988
1991
define_scoped_cx ! ( self ) ;
1989
1992
@@ -2117,7 +2120,7 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
2117
2120
2118
2121
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
2119
2122
// `region_index` and `used_region_names`.
2120
- impl < ' tcx , F : fmt :: Write > FmtPrinter < ' _ , ' tcx , F > {
2123
+ impl < ' tcx > FmtPrinter < ' _ , ' tcx > {
2121
2124
pub fn name_all_regions < T > (
2122
2125
mut self ,
2123
2126
value : & ty:: Binder < ' tcx , T > ,
@@ -2369,9 +2372,10 @@ macro_rules! forward_display_to_print {
2369
2372
$( #[ allow( unused_lifetimes) ] impl <' tcx> fmt:: Display for $ty {
2370
2373
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
2371
2374
ty:: tls:: with( |tcx| {
2372
- tcx. lift( * self )
2375
+ let cx = tcx. lift( * self )
2373
2376
. expect( "could not lift for printing" )
2374
- . print( FmtPrinter :: new( tcx, f, Namespace :: TypeNS ) ) ?;
2377
+ . print( FmtPrinter :: new( tcx, Namespace :: TypeNS ) ) ?;
2378
+ f. write_str( & cx. into_buffer( ) ) ?;
2375
2379
Ok ( ( ) )
2376
2380
} )
2377
2381
}
@@ -2402,8 +2406,7 @@ macro_rules! define_print_and_forward_display {
2402
2406
impl < ' tcx > fmt:: Display for ty:: Region < ' tcx > {
2403
2407
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2404
2408
ty:: tls:: with ( |tcx| {
2405
- self . print ( FmtPrinter :: new ( tcx, f, Namespace :: TypeNS ) ) ?;
2406
- Ok ( ( ) )
2409
+ f. write_str ( & self . print ( FmtPrinter :: new ( tcx, Namespace :: TypeNS ) ) ?. into_buffer ( ) )
2407
2410
} )
2408
2411
}
2409
2412
}
0 commit comments