@@ -20,16 +20,16 @@ use std::ops::{Deref, DerefMut};
2020use super :: * ;
2121
2222macro_rules! nest {
23- ( $closure : expr) => {
24- scoped_cx!( ) = scoped_cx!( ) . nest ( $closure ) ?
23+ ( $e : expr) => {
24+ scoped_cx!( ) = PrintCx :: new ( scoped_cx!( ) . tcx , $e? )
2525 }
2626}
2727macro_rules! print_inner {
2828 ( write ( $( $data: expr) ,+) ) => {
2929 write!( scoped_cx!( ) , $( $data) ,+) ?
3030 } ;
3131 ( $kind: ident ( $data: expr) ) => {
32- nest!( |cx| $data. $kind( cx ) )
32+ nest!( $data. $kind( scoped_cx! ( ) ) )
3333 } ;
3434}
3535macro_rules! p {
@@ -180,15 +180,6 @@ pub trait PrettyPrinter:
180180 > +
181181 fmt:: Write
182182{
183- /// Enter a nested print context, for pretty-printing
184- /// nested components in some larger context.
185- fn nest < ' a , ' gcx , ' tcx , E > (
186- self : PrintCx < ' a , ' gcx , ' tcx , Self > ,
187- f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
188- ) -> Result < PrintCx < ' a , ' gcx , ' tcx , Self > , E > {
189- Ok ( PrintCx :: new ( self . tcx , f ( self ) ?) )
190- }
191-
192183 /// Like `print_def_path` but for value paths.
193184 fn print_value_path (
194185 self : PrintCx < ' _ , ' _ , ' tcx , Self > ,
@@ -214,11 +205,13 @@ pub trait PrettyPrinter:
214205 ) -> Result < Self , Self :: Error >
215206 where T : Print < ' tcx , Self , Output = Self , Error = Self :: Error >
216207 {
208+ define_scoped_cx ! ( self ) ;
209+
217210 if let Some ( first) = elems. next ( ) {
218- self = self . nest ( |cx| first. print ( cx ) ) ? ;
211+ nest ! ( first. print( self ) ) ;
219212 for elem in elems {
220213 self . write_str ( ", " ) ?;
221- self = self . nest ( |cx| elem. print ( cx ) ) ? ;
214+ nest ! ( elem. print( self ) ) ;
222215 }
223216 }
224217 self . ok ( )
@@ -355,9 +348,9 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
355348 // the entire path will succeed or not. To support printers that do not
356349 // implement `PrettyPrinter`, a `Vec` or linked list on the stack would
357350 // need to be built, before starting to print anything.
358- let mut prefix_success = false ;
359- nest ! ( |cx| {
360- let ( path, success) = cx . try_print_visible_def_path( visible_parent) ?;
351+ let prefix_success;
352+ nest ! ( {
353+ let ( path, success) = self . try_print_visible_def_path( visible_parent) ?;
361354 prefix_success = success;
362355 Ok ( path)
363356 } ) ;
@@ -470,7 +463,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
470463 self_ty : Ty < ' tcx > ,
471464 trait_ref : Option < ty:: TraitRef < ' tcx > > ,
472465 ) -> Result < P :: Path , P :: Error > {
473- self = self . nest ( print_prefix) ? ;
466+ self = PrintCx :: new ( self . tcx , print_prefix ( self ) ? ) ;
474467
475468 self . generic_delimiters ( |mut cx| {
476469 define_scoped_cx ! ( cx) ;
@@ -492,7 +485,7 @@ pub struct FmtPrinter<F>(Box<FmtPrinterData<F>>);
492485pub struct FmtPrinterData < F > {
493486 fmt : F ,
494487
495- empty : bool ,
488+ empty_path : bool ,
496489 in_value : bool ,
497490
498491 used_region_names : FxHashSet < InternedString > ,
@@ -519,7 +512,7 @@ impl<F> FmtPrinter<F> {
519512 pub fn new ( fmt : F , ns : Namespace ) -> Self {
520513 FmtPrinter ( Box :: new ( FmtPrinterData {
521514 fmt,
522- empty : true ,
515+ empty_path : false ,
523516 in_value : ns == Namespace :: ValueNS ,
524517 used_region_names : Default :: default ( ) ,
525518 region_index : 0 ,
@@ -531,7 +524,6 @@ impl<F> FmtPrinter<F> {
531524
532525impl < F : fmt:: Write > fmt:: Write for FmtPrinter < F > {
533526 fn write_str ( & mut self , s : & str ) -> fmt:: Result {
534- self . empty &= s. is_empty ( ) ;
535527 self . fmt . write_str ( s)
536528 }
537529}
@@ -549,16 +541,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
549541 def_id : DefId ,
550542 substs : Option < SubstsRef < ' tcx > > ,
551543 ) -> Result < Self :: Path , Self :: Error > {
544+ define_scoped_cx ! ( self ) ;
545+
552546 // FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
553547 // both here and in `default_print_def_path`.
554548 let generics = substs. map ( |_| self . tcx . generics_of ( def_id) ) ;
555549 if generics. as_ref ( ) . and_then ( |g| g. parent ) . is_none ( ) {
556- let mut visible_path_success = false ;
557- self = self . nest ( |cx| {
558- let ( path, success) = cx . try_print_visible_def_path ( def_id) ?;
550+ let visible_path_success;
551+ nest ! ( {
552+ let ( path, success) = self . try_print_visible_def_path( def_id) ?;
559553 visible_path_success = success;
560554 Ok ( path)
561- } ) ? ;
555+ } ) ;
562556 if visible_path_success {
563557 return if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
564558 let args = self . generic_args_to_print ( generics, substs) ;
@@ -621,15 +615,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
621615 mut self : PrintCx < ' _ , ' _ , ' _ , Self > ,
622616 cnum : CrateNum ,
623617 ) -> Result < Self :: Path , Self :: Error > {
618+ self . empty_path = true ;
624619 if cnum == LOCAL_CRATE {
625620 if self . tcx . sess . rust_2018 ( ) {
626621 // We add the `crate::` keyword on Rust 2018, only when desired.
627622 if SHOULD_PREFIX_WITH_CRATE . with ( |flag| flag. get ( ) ) {
628623 write ! ( self , "{}" , keywords:: Crate . name( ) ) ?;
624+ self . empty_path = false ;
629625 }
630626 }
631627 } else {
632628 write ! ( self , "{}" , self . tcx. crate_name( cnum) ) ?;
629+ self . empty_path = false ;
633630 }
634631 self . ok ( )
635632 }
@@ -638,7 +635,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
638635 self_ty : Ty < ' tcx > ,
639636 trait_ref : Option < ty:: TraitRef < ' tcx > > ,
640637 ) -> Result < Self :: Path , Self :: Error > {
641- self . pretty_path_qualified ( self_ty, trait_ref)
638+ let mut path = self . pretty_path_qualified ( self_ty, trait_ref) ?;
639+ path. empty_path = false ;
640+ Ok ( path)
642641 }
643642
644643 fn path_append_impl < ' gcx , ' tcx > (
@@ -649,17 +648,16 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
649648 self_ty : Ty < ' tcx > ,
650649 trait_ref : Option < ty:: TraitRef < ' tcx > > ,
651650 ) -> Result < Self :: Path , Self :: Error > {
652- self . pretty_path_append_impl ( |cx| {
651+ let mut path = self . pretty_path_append_impl ( |cx| {
653652 let mut path = print_prefix ( cx) ?;
654-
655- // HACK(eddyb) this accounts for `generic_delimiters`
656- // printing `::<` instead of `<` if `in_value` is set.
657- if !path. empty && !path. in_value {
653+ if !path. empty_path {
658654 write ! ( path, "::" ) ?;
659655 }
660656
661657 Ok ( path)
662- } , self_ty, trait_ref)
658+ } , self_ty, trait_ref) ?;
659+ path. empty_path = false ;
660+ Ok ( path)
663661 }
664662 fn path_append < ' gcx , ' tcx > (
665663 self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
@@ -673,10 +671,11 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
673671 // FIXME(eddyb) `text` should never be empty, but it
674672 // currently is for `extern { ... }` "foreign modules".
675673 if !text. is_empty ( ) {
676- if !path. empty {
674+ if !path. empty_path {
677675 write ! ( path, "::" ) ?;
678676 }
679677 write ! ( path, "{}" , text) ?;
678+ path. empty_path = false ;
680679 }
681680
682681 Ok ( path)
@@ -688,7 +687,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
688687 ) -> Result < Self :: Path , Self :: Error > ,
689688 args : & [ Kind < ' tcx > ] ,
690689 ) -> Result < Self :: Path , Self :: Error > {
691- self = self . nest ( print_prefix) ?;
690+ define_scoped_cx ! ( self ) ;
691+
692+ nest ! ( print_prefix( self ) ) ;
692693
693694 // Don't print `'_` if there's no unerased regions.
694695 let print_regions = args. iter ( ) . any ( |arg| {
@@ -705,6 +706,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
705706 } ) ;
706707
707708 if args. clone ( ) . next ( ) . is_some ( ) {
709+ if self . in_value {
710+ write ! ( self , "::" ) ?;
711+ }
708712 self . generic_delimiters ( |cx| cx. comma_sep ( args) )
709713 } else {
710714 self . ok ( )
@@ -713,17 +717,6 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
713717}
714718
715719impl < F : fmt:: Write > PrettyPrinter for FmtPrinter < F > {
716- fn nest < ' a , ' gcx , ' tcx , E > (
717- mut self : PrintCx < ' a , ' gcx , ' tcx , Self > ,
718- f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
719- ) -> Result < PrintCx < ' a , ' gcx , ' tcx , Self > , E > {
720- let tcx = self . tcx ;
721- let was_empty = std:: mem:: replace ( & mut self . empty , true ) ;
722- let mut inner = f ( self ) ?;
723- inner. empty &= was_empty;
724- Ok ( PrintCx :: new ( tcx, inner) )
725- }
726-
727720 fn print_value_path (
728721 mut self : PrintCx < ' _ , ' _ , ' tcx , Self > ,
729722 def_id : DefId ,
@@ -749,11 +742,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
749742 mut self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
750743 f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
751744 ) -> Result < Self , Self :: Error > {
752- if !self . empty && self . in_value {
753- write ! ( self , "::<" ) ?;
754- } else {
755- write ! ( self , "<" ) ?;
756- }
745+ write ! ( self , "<" ) ?;
757746
758747 let was_in_value = std:: mem:: replace ( & mut self . in_value , false ) ;
759748 let mut inner = f ( self ) ?;
@@ -957,7 +946,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
957946 ty:: FnDef ( def_id, substs) => {
958947 let sig = self . tcx . fn_sig ( def_id) . subst ( self . tcx , substs) ;
959948 p ! ( print( sig) , write( " {{" ) ) ;
960- nest ! ( |cx| cx . print_value_path( def_id, Some ( substs) ) ) ;
949+ nest ! ( self . print_value_path( def_id, Some ( substs) ) ) ;
961950 p ! ( write( "}}" ) )
962951 }
963952 ty:: FnPtr ( ref bare_fn) => {
@@ -980,7 +969,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
980969 }
981970 }
982971 ty:: Adt ( def, substs) => {
983- nest ! ( |cx| cx . print_def_path( def. did, Some ( substs) ) ) ;
972+ nest ! ( self . print_def_path( def. did, Some ( substs) ) ) ;
984973 }
985974 ty:: Dynamic ( data, r) => {
986975 let print_r = self . region_should_not_be_omitted ( r) ;
@@ -993,7 +982,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
993982 }
994983 }
995984 ty:: Foreign ( def_id) => {
996- nest ! ( |cx| cx . print_def_path( def_id, None ) ) ;
985+ nest ! ( self . print_def_path( def_id, None ) ) ;
997986 }
998987 ty:: Projection ( ref data) => p ! ( print( data) ) ,
999988 ty:: UnnormalizedProjection ( ref data) => {
@@ -1094,7 +1083,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
10941083 p ! ( write( " " ) , print( witness) , write( "]" ) )
10951084 } ,
10961085 ty:: GeneratorWitness ( types) => {
1097- nest ! ( |cx| cx . in_binder( & types) )
1086+ nest ! ( self . in_binder( & types) )
10981087 }
10991088 ty:: Closure ( did, substs) => {
11001089 let upvar_tys = substs. upvar_tys ( did, self . tcx ) ;
@@ -1179,7 +1168,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
11791168 let mut first = true ;
11801169
11811170 if let Some ( principal) = predicates. principal ( ) {
1182- nest ! ( |cx| cx . print_def_path( principal. def_id, None ) ) ;
1171+ nest ! ( self . print_def_path( principal. def_id, None ) ) ;
11831172
11841173 let mut resugared = false ;
11851174
@@ -1189,7 +1178,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
11891178 if let ty:: Tuple ( ref args) = principal. substs . type_at ( 0 ) . sty {
11901179 let mut projections = predicates. projection_bounds ( ) ;
11911180 if let ( Some ( proj) , None ) = ( projections. next ( ) , projections. next ( ) ) {
1192- nest ! ( |cx| cx . pretty_fn_sig( args, false , proj. ty) ) ;
1181+ nest ! ( self . pretty_fn_sig( args, false , proj. ty) ) ;
11931182 resugared = true ;
11941183 }
11951184 }
@@ -1228,8 +1217,8 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12281217 let args = arg0. into_iter ( ) . chain ( args) ;
12291218 let projections = projection0. into_iter ( ) . chain ( projections) ;
12301219
1231- nest ! ( |cx| cx . generic_delimiters( |mut cx| {
1232- cx = cx . nest ( |cx| cx. comma_sep( args) ) ? ;
1220+ nest ! ( self . generic_delimiters( |mut cx| {
1221+ cx = PrintCx :: new ( cx . tcx , cx. comma_sep( args) ? ) ;
12331222 if arg0. is_some( ) && projection0. is_some( ) {
12341223 write!( cx, ", " ) ?;
12351224 }
@@ -1262,7 +1251,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12621251 }
12631252 first = false ;
12641253
1265- nest ! ( |cx| cx . print_def_path( def_id, None ) ) ;
1254+ nest ! ( self . print_def_path( def_id, None ) ) ;
12661255 }
12671256
12681257 self . ok ( )
@@ -1531,7 +1520,7 @@ define_print_and_forward_display! {
15311520 ty:: ExistentialPredicate :: Trait ( x) => p!( print( x) ) ,
15321521 ty:: ExistentialPredicate :: Projection ( x) => p!( print( x) ) ,
15331522 ty:: ExistentialPredicate :: AutoTrait ( def_id) => {
1534- nest!( |cx| cx. print_def_path( def_id, None ) )
1523+ nest!( cx. print_def_path( def_id, None ) )
15351524 }
15361525 }
15371526 }
@@ -1546,7 +1535,7 @@ define_print_and_forward_display! {
15461535 }
15471536
15481537 p!( write( "fn" ) ) ;
1549- nest!( |cx| cx. pretty_fn_sig( self . inputs( ) , self . c_variadic, self . output( ) ) ) ;
1538+ nest!( cx. pretty_fn_sig( self . inputs( ) , self . c_variadic, self . output( ) ) ) ;
15501539 }
15511540
15521541 ty:: InferTy {
@@ -1565,7 +1554,7 @@ define_print_and_forward_display! {
15651554 }
15661555
15671556 ty:: TraitRef <' tcx> {
1568- nest!( |cx| cx. print_def_path( self . def_id, Some ( self . substs) ) ) ;
1557+ nest!( cx. print_def_path( self . def_id, Some ( self . substs) ) ) ;
15691558 }
15701559
15711560 ConstValue <' tcx> {
@@ -1609,7 +1598,7 @@ define_print_and_forward_display! {
16091598 }
16101599
16111600 ty:: ProjectionTy <' tcx> {
1612- nest!( |cx| cx. print_def_path( self . item_def_id, Some ( self . substs) ) ) ;
1601+ nest!( cx. print_def_path( self . item_def_id, Some ( self . substs) ) ) ;
16131602 }
16141603
16151604 ty:: ClosureKind {
@@ -1630,17 +1619,17 @@ define_print_and_forward_display! {
16301619 ty:: Predicate :: WellFormed ( ty) => p!( print( ty) , write( " well-formed" ) ) ,
16311620 ty:: Predicate :: ObjectSafe ( trait_def_id) => {
16321621 p!( write( "the trait `" ) ) ;
1633- nest!( |cx| cx. print_def_path( trait_def_id, None ) ) ;
1622+ nest!( cx. print_def_path( trait_def_id, None ) ) ;
16341623 p!( write( "` is object-safe" ) )
16351624 }
16361625 ty:: Predicate :: ClosureKind ( closure_def_id, _closure_substs, kind) => {
16371626 p!( write( "the closure `" ) ) ;
1638- nest!( |cx| cx. print_value_path( closure_def_id, None ) ) ;
1627+ nest!( cx. print_value_path( closure_def_id, None ) ) ;
16391628 p!( write( "` implements the trait `{}`" , kind) )
16401629 }
16411630 ty:: Predicate :: ConstEvaluatable ( def_id, substs) => {
16421631 p!( write( "the constant `" ) ) ;
1643- nest!( |cx| cx. print_value_path( def_id, Some ( substs) ) ) ;
1632+ nest!( cx. print_value_path( def_id, Some ( substs) ) ) ;
16441633 p!( write( "` can be evaluated" ) )
16451634 }
16461635 }
0 commit comments