@@ -20,16 +20,16 @@ use std::ops::{Deref, DerefMut};
20
20
use super :: * ;
21
21
22
22
macro_rules! nest {
23
- ( $closure : expr) => {
24
- scoped_cx!( ) = scoped_cx!( ) . nest ( $closure ) ?
23
+ ( $e : expr) => {
24
+ scoped_cx!( ) = PrintCx :: new ( scoped_cx!( ) . tcx , $e? )
25
25
}
26
26
}
27
27
macro_rules! print_inner {
28
28
( write ( $( $data: expr) ,+) ) => {
29
29
write!( scoped_cx!( ) , $( $data) ,+) ?
30
30
} ;
31
31
( $kind: ident ( $data: expr) ) => {
32
- nest!( |cx| $data. $kind( cx ) )
32
+ nest!( $data. $kind( scoped_cx! ( ) ) )
33
33
} ;
34
34
}
35
35
macro_rules! p {
@@ -180,15 +180,6 @@ pub trait PrettyPrinter:
180
180
> +
181
181
fmt:: Write
182
182
{
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
-
192
183
/// Like `print_def_path` but for value paths.
193
184
fn print_value_path (
194
185
self : PrintCx < ' _ , ' _ , ' tcx , Self > ,
@@ -214,11 +205,13 @@ pub trait PrettyPrinter:
214
205
) -> Result < Self , Self :: Error >
215
206
where T : Print < ' tcx , Self , Output = Self , Error = Self :: Error >
216
207
{
208
+ define_scoped_cx ! ( self ) ;
209
+
217
210
if let Some ( first) = elems. next ( ) {
218
- self = self . nest ( |cx| first. print ( cx ) ) ? ;
211
+ nest ! ( first. print( self ) ) ;
219
212
for elem in elems {
220
213
self . write_str ( ", " ) ?;
221
- self = self . nest ( |cx| elem. print ( cx ) ) ? ;
214
+ nest ! ( elem. print( self ) ) ;
222
215
}
223
216
}
224
217
self . ok ( )
@@ -355,9 +348,9 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
355
348
// the entire path will succeed or not. To support printers that do not
356
349
// implement `PrettyPrinter`, a `Vec` or linked list on the stack would
357
350
// 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) ?;
361
354
prefix_success = success;
362
355
Ok ( path)
363
356
} ) ;
@@ -470,7 +463,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
470
463
self_ty : Ty < ' tcx > ,
471
464
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
472
465
) -> Result < P :: Path , P :: Error > {
473
- self = self . nest ( print_prefix) ? ;
466
+ self = PrintCx :: new ( self . tcx , print_prefix ( self ) ? ) ;
474
467
475
468
self . generic_delimiters ( |mut cx| {
476
469
define_scoped_cx ! ( cx) ;
@@ -492,7 +485,7 @@ pub struct FmtPrinter<F>(Box<FmtPrinterData<F>>);
492
485
pub struct FmtPrinterData < F > {
493
486
fmt : F ,
494
487
495
- empty : bool ,
488
+ empty_path : bool ,
496
489
in_value : bool ,
497
490
498
491
used_region_names : FxHashSet < InternedString > ,
@@ -519,7 +512,7 @@ impl<F> FmtPrinter<F> {
519
512
pub fn new ( fmt : F , ns : Namespace ) -> Self {
520
513
FmtPrinter ( Box :: new ( FmtPrinterData {
521
514
fmt,
522
- empty : true ,
515
+ empty_path : false ,
523
516
in_value : ns == Namespace :: ValueNS ,
524
517
used_region_names : Default :: default ( ) ,
525
518
region_index : 0 ,
@@ -531,7 +524,6 @@ impl<F> FmtPrinter<F> {
531
524
532
525
impl < F : fmt:: Write > fmt:: Write for FmtPrinter < F > {
533
526
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
534
- self . empty &= s. is_empty ( ) ;
535
527
self . fmt . write_str ( s)
536
528
}
537
529
}
@@ -549,16 +541,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
549
541
def_id : DefId ,
550
542
substs : Option < SubstsRef < ' tcx > > ,
551
543
) -> Result < Self :: Path , Self :: Error > {
544
+ define_scoped_cx ! ( self ) ;
545
+
552
546
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
553
547
// both here and in `default_print_def_path`.
554
548
let generics = substs. map ( |_| self . tcx . generics_of ( def_id) ) ;
555
549
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) ?;
559
553
visible_path_success = success;
560
554
Ok ( path)
561
- } ) ? ;
555
+ } ) ;
562
556
if visible_path_success {
563
557
return if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
564
558
let args = self . generic_args_to_print ( generics, substs) ;
@@ -621,15 +615,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
621
615
mut self : PrintCx < ' _ , ' _ , ' _ , Self > ,
622
616
cnum : CrateNum ,
623
617
) -> Result < Self :: Path , Self :: Error > {
618
+ self . empty_path = true ;
624
619
if cnum == LOCAL_CRATE {
625
620
if self . tcx . sess . rust_2018 ( ) {
626
621
// We add the `crate::` keyword on Rust 2018, only when desired.
627
622
if SHOULD_PREFIX_WITH_CRATE . with ( |flag| flag. get ( ) ) {
628
623
write ! ( self , "{}" , keywords:: Crate . name( ) ) ?;
624
+ self . empty_path = false ;
629
625
}
630
626
}
631
627
} else {
632
628
write ! ( self , "{}" , self . tcx. crate_name( cnum) ) ?;
629
+ self . empty_path = false ;
633
630
}
634
631
self . ok ( )
635
632
}
@@ -638,7 +635,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
638
635
self_ty : Ty < ' tcx > ,
639
636
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
640
637
) -> 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)
642
641
}
643
642
644
643
fn path_append_impl < ' gcx , ' tcx > (
@@ -649,17 +648,16 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
649
648
self_ty : Ty < ' tcx > ,
650
649
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
651
650
) -> Result < Self :: Path , Self :: Error > {
652
- self . pretty_path_append_impl ( |cx| {
651
+ let mut path = self . pretty_path_append_impl ( |cx| {
653
652
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 {
658
654
write ! ( path, "::" ) ?;
659
655
}
660
656
661
657
Ok ( path)
662
- } , self_ty, trait_ref)
658
+ } , self_ty, trait_ref) ?;
659
+ path. empty_path = false ;
660
+ Ok ( path)
663
661
}
664
662
fn path_append < ' gcx , ' tcx > (
665
663
self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
@@ -673,10 +671,11 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
673
671
// FIXME(eddyb) `text` should never be empty, but it
674
672
// currently is for `extern { ... }` "foreign modules".
675
673
if !text. is_empty ( ) {
676
- if !path. empty {
674
+ if !path. empty_path {
677
675
write ! ( path, "::" ) ?;
678
676
}
679
677
write ! ( path, "{}" , text) ?;
678
+ path. empty_path = false ;
680
679
}
681
680
682
681
Ok ( path)
@@ -688,7 +687,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
688
687
) -> Result < Self :: Path , Self :: Error > ,
689
688
args : & [ Kind < ' tcx > ] ,
690
689
) -> Result < Self :: Path , Self :: Error > {
691
- self = self . nest ( print_prefix) ?;
690
+ define_scoped_cx ! ( self ) ;
691
+
692
+ nest ! ( print_prefix( self ) ) ;
692
693
693
694
// Don't print `'_` if there's no unerased regions.
694
695
let print_regions = args. iter ( ) . any ( |arg| {
@@ -705,6 +706,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
705
706
} ) ;
706
707
707
708
if args. clone ( ) . next ( ) . is_some ( ) {
709
+ if self . in_value {
710
+ write ! ( self , "::" ) ?;
711
+ }
708
712
self . generic_delimiters ( |cx| cx. comma_sep ( args) )
709
713
} else {
710
714
self . ok ( )
@@ -713,17 +717,6 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
713
717
}
714
718
715
719
impl < 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
-
727
720
fn print_value_path (
728
721
mut self : PrintCx < ' _ , ' _ , ' tcx , Self > ,
729
722
def_id : DefId ,
@@ -749,11 +742,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
749
742
mut self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
750
743
f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
751
744
) -> Result < Self , Self :: Error > {
752
- if !self . empty && self . in_value {
753
- write ! ( self , "::<" ) ?;
754
- } else {
755
- write ! ( self , "<" ) ?;
756
- }
745
+ write ! ( self , "<" ) ?;
757
746
758
747
let was_in_value = std:: mem:: replace ( & mut self . in_value , false ) ;
759
748
let mut inner = f ( self ) ?;
@@ -957,7 +946,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
957
946
ty:: FnDef ( def_id, substs) => {
958
947
let sig = self . tcx . fn_sig ( def_id) . subst ( self . tcx , substs) ;
959
948
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) ) ) ;
961
950
p ! ( write( "}}" ) )
962
951
}
963
952
ty:: FnPtr ( ref bare_fn) => {
@@ -980,7 +969,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
980
969
}
981
970
}
982
971
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) ) ) ;
984
973
}
985
974
ty:: Dynamic ( data, r) => {
986
975
let print_r = self . region_should_not_be_omitted ( r) ;
@@ -993,7 +982,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
993
982
}
994
983
}
995
984
ty:: Foreign ( def_id) => {
996
- nest ! ( |cx| cx . print_def_path( def_id, None ) ) ;
985
+ nest ! ( self . print_def_path( def_id, None ) ) ;
997
986
}
998
987
ty:: Projection ( ref data) => p ! ( print( data) ) ,
999
988
ty:: UnnormalizedProjection ( ref data) => {
@@ -1094,7 +1083,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1094
1083
p ! ( write( " " ) , print( witness) , write( "]" ) )
1095
1084
} ,
1096
1085
ty:: GeneratorWitness ( types) => {
1097
- nest ! ( |cx| cx . in_binder( & types) )
1086
+ nest ! ( self . in_binder( & types) )
1098
1087
}
1099
1088
ty:: Closure ( did, substs) => {
1100
1089
let upvar_tys = substs. upvar_tys ( did, self . tcx ) ;
@@ -1179,7 +1168,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1179
1168
let mut first = true ;
1180
1169
1181
1170
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 ) ) ;
1183
1172
1184
1173
let mut resugared = false ;
1185
1174
@@ -1189,7 +1178,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1189
1178
if let ty:: Tuple ( ref args) = principal. substs . type_at ( 0 ) . sty {
1190
1179
let mut projections = predicates. projection_bounds ( ) ;
1191
1180
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) ) ;
1193
1182
resugared = true ;
1194
1183
}
1195
1184
}
@@ -1228,8 +1217,8 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1228
1217
let args = arg0. into_iter ( ) . chain ( args) ;
1229
1218
let projections = projection0. into_iter ( ) . chain ( projections) ;
1230
1219
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) ? ) ;
1233
1222
if arg0. is_some( ) && projection0. is_some( ) {
1234
1223
write!( cx, ", " ) ?;
1235
1224
}
@@ -1262,7 +1251,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1262
1251
}
1263
1252
first = false ;
1264
1253
1265
- nest ! ( |cx| cx . print_def_path( def_id, None ) ) ;
1254
+ nest ! ( self . print_def_path( def_id, None ) ) ;
1266
1255
}
1267
1256
1268
1257
self . ok ( )
@@ -1531,7 +1520,7 @@ define_print_and_forward_display! {
1531
1520
ty:: ExistentialPredicate :: Trait ( x) => p!( print( x) ) ,
1532
1521
ty:: ExistentialPredicate :: Projection ( x) => p!( print( x) ) ,
1533
1522
ty:: ExistentialPredicate :: AutoTrait ( def_id) => {
1534
- nest!( |cx| cx. print_def_path( def_id, None ) )
1523
+ nest!( cx. print_def_path( def_id, None ) )
1535
1524
}
1536
1525
}
1537
1526
}
@@ -1546,7 +1535,7 @@ define_print_and_forward_display! {
1546
1535
}
1547
1536
1548
1537
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( ) ) ) ;
1550
1539
}
1551
1540
1552
1541
ty:: InferTy {
@@ -1565,7 +1554,7 @@ define_print_and_forward_display! {
1565
1554
}
1566
1555
1567
1556
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) ) ) ;
1569
1558
}
1570
1559
1571
1560
ConstValue <' tcx> {
@@ -1609,7 +1598,7 @@ define_print_and_forward_display! {
1609
1598
}
1610
1599
1611
1600
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) ) ) ;
1613
1602
}
1614
1603
1615
1604
ty:: ClosureKind {
@@ -1630,17 +1619,17 @@ define_print_and_forward_display! {
1630
1619
ty:: Predicate :: WellFormed ( ty) => p!( print( ty) , write( " well-formed" ) ) ,
1631
1620
ty:: Predicate :: ObjectSafe ( trait_def_id) => {
1632
1621
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 ) ) ;
1634
1623
p!( write( "` is object-safe" ) )
1635
1624
}
1636
1625
ty:: Predicate :: ClosureKind ( closure_def_id, _closure_substs, kind) => {
1637
1626
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 ) ) ;
1639
1628
p!( write( "` implements the trait `{}`" , kind) )
1640
1629
}
1641
1630
ty:: Predicate :: ConstEvaluatable ( def_id, substs) => {
1642
1631
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) ) ) ;
1644
1633
p!( write( "` can be evaluated" ) )
1645
1634
}
1646
1635
}
0 commit comments