@@ -1545,11 +1545,11 @@ pub trait PrettyPrinter<'tcx>:
15451545}
15461546
15471547// 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 > > ) ;
15491549
1550- pub struct FmtPrinterData < ' a , ' tcx , F > {
1550+ pub struct FmtPrinterData < ' a , ' tcx > {
15511551 tcx : TyCtxt < ' tcx > ,
1552- fmt : F ,
1552+ fmt : String ,
15531553
15541554 empty_path : bool ,
15551555 in_value : bool ,
@@ -1566,24 +1566,24 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
15661566 pub const_infer_name_resolver : Option < Box < dyn Fn ( ty:: ConstVid < ' tcx > ) -> Option < String > + ' a > > ,
15671567}
15681568
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 > ;
15711571 fn deref ( & self ) -> & Self :: Target {
15721572 & self . 0
15731573 }
15741574}
15751575
1576- impl < F > DerefMut for FmtPrinter < ' _ , ' _ , F > {
1576+ impl DerefMut for FmtPrinter < ' _ , ' _ > {
15771577 fn deref_mut ( & mut self ) -> & mut Self :: Target {
15781578 & mut self . 0
15791579 }
15801580}
15811581
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 {
15841584 FmtPrinter ( Box :: new ( FmtPrinterData {
15851585 tcx,
1586- fmt,
1586+ fmt : String :: new ( ) ,
15871587 empty_path : false ,
15881588 in_value : ns == Namespace :: ValueNS ,
15891589 print_alloc_ids : false ,
@@ -1596,6 +1596,10 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
15961596 const_infer_name_resolver : None ,
15971597 } ) )
15981598 }
1599+
1600+ pub fn into_buffer ( self ) -> String {
1601+ self . 0 . fmt
1602+ }
15991603}
16001604
16011605// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
@@ -1627,19 +1631,18 @@ impl<'t> TyCtxt<'t> {
16271631 pub fn def_path_str_with_substs ( self , def_id : DefId , substs : & ' t [ GenericArg < ' t > ] ) -> String {
16281632 let ns = guess_def_namespace ( self , def_id) ;
16291633 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 ( )
16331635 }
16341636}
16351637
1636- impl < F : fmt:: Write > fmt :: Write for FmtPrinter < ' _ , ' _ , F > {
1638+ impl fmt:: Write for FmtPrinter < ' _ , ' _ > {
16371639 fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1638- self . fmt . write_str ( s)
1640+ self . fmt . push_str ( s) ;
1641+ Ok ( ( ) )
16391642 }
16401643}
16411644
1642- impl < ' tcx , F : fmt :: Write > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1645+ impl < ' tcx > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx > {
16431646 type Error = fmt:: Error ;
16441647
16451648 type Path = Self ;
@@ -1847,7 +1850,7 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
18471850 }
18481851}
18491852
1850- impl < ' tcx , F : fmt :: Write > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1853+ impl < ' tcx > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx > {
18511854 fn ty_infer_name ( & self , id : ty:: TyVid ) -> Option < String > {
18521855 self . 0 . ty_infer_name_resolver . as_ref ( ) . and_then ( |func| func ( id) )
18531856 }
@@ -1983,7 +1986,7 @@ impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
19831986}
19841987
19851988// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
1986- impl < F : fmt :: Write > FmtPrinter < ' _ , ' _ , F > {
1989+ impl FmtPrinter < ' _ , ' _ > {
19871990 pub fn pretty_print_region ( mut self , region : ty:: Region < ' _ > ) -> Result < Self , fmt:: Error > {
19881991 define_scoped_cx ! ( self ) ;
19891992
@@ -2117,7 +2120,7 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
21172120
21182121// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
21192122// `region_index` and `used_region_names`.
2120- impl < ' tcx , F : fmt :: Write > FmtPrinter < ' _ , ' tcx , F > {
2123+ impl < ' tcx > FmtPrinter < ' _ , ' tcx > {
21212124 pub fn name_all_regions < T > (
21222125 mut self ,
21232126 value : & ty:: Binder < ' tcx , T > ,
@@ -2369,9 +2372,10 @@ macro_rules! forward_display_to_print {
23692372 $( #[ allow( unused_lifetimes) ] impl <' tcx> fmt:: Display for $ty {
23702373 fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
23712374 ty:: tls:: with( |tcx| {
2372- tcx. lift( * self )
2375+ let cx = tcx. lift( * self )
23732376 . 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( ) ) ?;
23752379 Ok ( ( ) )
23762380 } )
23772381 }
@@ -2402,8 +2406,7 @@ macro_rules! define_print_and_forward_display {
24022406impl < ' tcx > fmt:: Display for ty:: Region < ' tcx > {
24032407 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
24042408 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 ( ) )
24072410 } )
24082411 }
24092412}
0 commit comments