@@ -19,16 +19,16 @@ use std::ops::{Deref, DerefMut};
19
19
use super :: * ;
20
20
21
21
macro_rules! nest {
22
- ( $closure : expr) => {
23
- scoped_cx!( ) = scoped_cx!( ) . nest ( $closure ) ?
22
+ ( $e : expr) => {
23
+ scoped_cx!( ) = PrintCx :: new ( scoped_cx!( ) . tcx , $e? )
24
24
}
25
25
}
26
26
macro_rules! print_inner {
27
27
( write ( $( $data: expr) ,+) ) => {
28
28
write!( scoped_cx!( ) , $( $data) ,+) ?
29
29
} ;
30
30
( $kind: ident ( $data: expr) ) => {
31
- nest!( |cx| $data. $kind( cx ) )
31
+ nest!( $data. $kind( scoped_cx! ( ) ) )
32
32
} ;
33
33
}
34
34
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 ( )
@@ -354,9 +347,9 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
354
347
// the entire path will succeed or not. To support printers that do not
355
348
// implement `PrettyPrinter`, a `Vec` or linked list on the stack would
356
349
// need to be built, before starting to print anything.
357
- let mut prefix_success = false ;
358
- nest ! ( |cx| {
359
- let ( path, success) = cx . try_print_visible_def_path( visible_parent) ?;
350
+ let prefix_success;
351
+ nest ! ( {
352
+ let ( path, success) = self . try_print_visible_def_path( visible_parent) ?;
360
353
prefix_success = success;
361
354
Ok ( path)
362
355
} ) ;
@@ -469,7 +462,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
469
462
self_ty : Ty < ' tcx > ,
470
463
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
471
464
) -> Result < P :: Path , P :: Error > {
472
- self = self . nest ( print_prefix) ? ;
465
+ self = PrintCx :: new ( self . tcx , print_prefix ( self ) ? ) ;
473
466
474
467
self . generic_delimiters ( |mut cx| {
475
468
define_scoped_cx ! ( cx) ;
@@ -491,7 +484,7 @@ pub struct FmtPrinter<F>(Box<FmtPrinterData<F>>);
491
484
pub struct FmtPrinterData < F > {
492
485
fmt : F ,
493
486
494
- empty : bool ,
487
+ empty_path : bool ,
495
488
in_value : bool ,
496
489
497
490
used_region_names : FxHashSet < InternedString > ,
@@ -518,7 +511,7 @@ impl<F> FmtPrinter<F> {
518
511
pub fn new ( fmt : F , ns : Namespace ) -> Self {
519
512
FmtPrinter ( Box :: new ( FmtPrinterData {
520
513
fmt,
521
- empty : true ,
514
+ empty_path : false ,
522
515
in_value : ns == Namespace :: ValueNS ,
523
516
used_region_names : Default :: default ( ) ,
524
517
region_index : 0 ,
@@ -530,7 +523,6 @@ impl<F> FmtPrinter<F> {
530
523
531
524
impl < F : fmt:: Write > fmt:: Write for FmtPrinter < F > {
532
525
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
533
- self . empty &= s. is_empty ( ) ;
534
526
self . fmt . write_str ( s)
535
527
}
536
528
}
@@ -548,16 +540,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
548
540
def_id : DefId ,
549
541
substs : Option < & ' tcx Substs < ' tcx > > ,
550
542
) -> Result < Self :: Path , Self :: Error > {
543
+ define_scoped_cx ! ( self ) ;
544
+
551
545
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
552
546
// both here and in `default_print_def_path`.
553
547
let generics = substs. map ( |_| self . tcx . generics_of ( def_id) ) ;
554
548
if generics. as_ref ( ) . and_then ( |g| g. parent ) . is_none ( ) {
555
- let mut visible_path_success = false ;
556
- self = self . nest ( |cx| {
557
- let ( path, success) = cx . try_print_visible_def_path ( def_id) ?;
549
+ let visible_path_success;
550
+ nest ! ( {
551
+ let ( path, success) = self . try_print_visible_def_path( def_id) ?;
558
552
visible_path_success = success;
559
553
Ok ( path)
560
- } ) ? ;
554
+ } ) ;
561
555
if visible_path_success {
562
556
return if let ( Some ( generics) , Some ( substs) ) = ( generics, substs) {
563
557
let args = self . generic_args_to_print ( generics, substs) ;
@@ -620,15 +614,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
620
614
mut self : PrintCx < ' _ , ' _ , ' _ , Self > ,
621
615
cnum : CrateNum ,
622
616
) -> Result < Self :: Path , Self :: Error > {
617
+ self . empty_path = true ;
623
618
if cnum == LOCAL_CRATE {
624
619
if self . tcx . sess . rust_2018 ( ) {
625
620
// We add the `crate::` keyword on Rust 2018, only when desired.
626
621
if SHOULD_PREFIX_WITH_CRATE . with ( |flag| flag. get ( ) ) {
627
622
write ! ( self , "{}" , keywords:: Crate . name( ) ) ?;
623
+ self . empty_path = false ;
628
624
}
629
625
}
630
626
} else {
631
627
write ! ( self , "{}" , self . tcx. crate_name( cnum) ) ?;
628
+ self . empty_path = false ;
632
629
}
633
630
self . ok ( )
634
631
}
@@ -637,7 +634,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
637
634
self_ty : Ty < ' tcx > ,
638
635
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
639
636
) -> Result < Self :: Path , Self :: Error > {
640
- self . pretty_path_qualified ( self_ty, trait_ref)
637
+ let mut path = self . pretty_path_qualified ( self_ty, trait_ref) ?;
638
+ path. empty_path = false ;
639
+ Ok ( path)
641
640
}
642
641
643
642
fn path_append_impl < ' gcx , ' tcx > (
@@ -648,17 +647,16 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
648
647
self_ty : Ty < ' tcx > ,
649
648
trait_ref : Option < ty:: TraitRef < ' tcx > > ,
650
649
) -> Result < Self :: Path , Self :: Error > {
651
- self . pretty_path_append_impl ( |cx| {
650
+ let mut path = self . pretty_path_append_impl ( |cx| {
652
651
let mut path = print_prefix ( cx) ?;
653
-
654
- // HACK(eddyb) this accounts for `generic_delimiters`
655
- // printing `::<` instead of `<` if `in_value` is set.
656
- if !path. empty && !path. in_value {
652
+ if !path. empty_path {
657
653
write ! ( path, "::" ) ?;
658
654
}
659
655
660
656
Ok ( path)
661
- } , self_ty, trait_ref)
657
+ } , self_ty, trait_ref) ?;
658
+ path. empty_path = false ;
659
+ Ok ( path)
662
660
}
663
661
fn path_append < ' gcx , ' tcx > (
664
662
self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
@@ -672,10 +670,11 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
672
670
// FIXME(eddyb) `text` should never be empty, but it
673
671
// currently is for `extern { ... }` "foreign modules".
674
672
if !text. is_empty ( ) {
675
- if !path. empty {
673
+ if !path. empty_path {
676
674
write ! ( path, "::" ) ?;
677
675
}
678
676
write ! ( path, "{}" , text) ?;
677
+ path. empty_path = false ;
679
678
}
680
679
681
680
Ok ( path)
@@ -687,7 +686,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
687
686
) -> Result < Self :: Path , Self :: Error > ,
688
687
args : & [ Kind < ' tcx > ] ,
689
688
) -> Result < Self :: Path , Self :: Error > {
690
- self = self . nest ( print_prefix) ?;
689
+ define_scoped_cx ! ( self ) ;
690
+
691
+ nest ! ( print_prefix( self ) ) ;
691
692
692
693
// Don't print `'_` if there's no unerased regions.
693
694
let print_regions = args. iter ( ) . any ( |arg| {
@@ -704,6 +705,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
704
705
} ) ;
705
706
706
707
if args. clone ( ) . next ( ) . is_some ( ) {
708
+ if self . in_value {
709
+ write ! ( self , "::" ) ?;
710
+ }
707
711
self . generic_delimiters ( |cx| cx. comma_sep ( args) )
708
712
} else {
709
713
self . ok ( )
@@ -712,17 +716,6 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
712
716
}
713
717
714
718
impl < F : fmt:: Write > PrettyPrinter for FmtPrinter < F > {
715
- fn nest < ' a , ' gcx , ' tcx , E > (
716
- mut self : PrintCx < ' a , ' gcx , ' tcx , Self > ,
717
- f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , E > ,
718
- ) -> Result < PrintCx < ' a , ' gcx , ' tcx , Self > , E > {
719
- let tcx = self . tcx ;
720
- let was_empty = std:: mem:: replace ( & mut self . empty , true ) ;
721
- let mut inner = f ( self ) ?;
722
- inner. empty &= was_empty;
723
- Ok ( PrintCx :: new ( tcx, inner) )
724
- }
725
-
726
719
fn print_value_path (
727
720
mut self : PrintCx < ' _ , ' _ , ' tcx , Self > ,
728
721
def_id : DefId ,
@@ -748,11 +741,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
748
741
mut self : PrintCx < ' _ , ' gcx , ' tcx , Self > ,
749
742
f : impl FnOnce ( PrintCx < ' _ , ' gcx , ' tcx , Self > ) -> Result < Self , Self :: Error > ,
750
743
) -> Result < Self , Self :: Error > {
751
- if !self . empty && self . in_value {
752
- write ! ( self , "::<" ) ?;
753
- } else {
754
- write ! ( self , "<" ) ?;
755
- }
744
+ write ! ( self , "<" ) ?;
756
745
757
746
let was_in_value = std:: mem:: replace ( & mut self . in_value , false ) ;
758
747
let mut inner = f ( self ) ?;
@@ -956,7 +945,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
956
945
ty:: FnDef ( def_id, substs) => {
957
946
let sig = self . tcx . fn_sig ( def_id) . subst ( self . tcx , substs) ;
958
947
p ! ( print( sig) , write( " {{" ) ) ;
959
- nest ! ( |cx| cx . print_value_path( def_id, Some ( substs) ) ) ;
948
+ nest ! ( self . print_value_path( def_id, Some ( substs) ) ) ;
960
949
p ! ( write( "}}" ) )
961
950
}
962
951
ty:: FnPtr ( ref bare_fn) => {
@@ -979,7 +968,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
979
968
}
980
969
}
981
970
ty:: Adt ( def, substs) => {
982
- nest ! ( |cx| cx . print_def_path( def. did, Some ( substs) ) ) ;
971
+ nest ! ( self . print_def_path( def. did, Some ( substs) ) ) ;
983
972
}
984
973
ty:: Dynamic ( data, r) => {
985
974
let print_r = self . region_should_not_be_omitted ( r) ;
@@ -992,7 +981,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
992
981
}
993
982
}
994
983
ty:: Foreign ( def_id) => {
995
- nest ! ( |cx| cx . print_def_path( def_id, None ) ) ;
984
+ nest ! ( self . print_def_path( def_id, None ) ) ;
996
985
}
997
986
ty:: Projection ( ref data) => p ! ( print( data) ) ,
998
987
ty:: UnnormalizedProjection ( ref data) => {
@@ -1093,7 +1082,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1093
1082
p ! ( write( " " ) , print( witness) , write( "]" ) )
1094
1083
} ,
1095
1084
ty:: GeneratorWitness ( types) => {
1096
- nest ! ( |cx| cx . in_binder( & types) )
1085
+ nest ! ( self . in_binder( & types) )
1097
1086
}
1098
1087
ty:: Closure ( did, substs) => {
1099
1088
let upvar_tys = substs. upvar_tys ( did, self . tcx ) ;
@@ -1165,7 +1154,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1165
1154
let mut first = true ;
1166
1155
1167
1156
if let Some ( principal) = predicates. principal ( ) {
1168
- nest ! ( |cx| cx . print_def_path( principal. def_id, None ) ) ;
1157
+ nest ! ( self . print_def_path( principal. def_id, None ) ) ;
1169
1158
1170
1159
let mut resugared = false ;
1171
1160
@@ -1175,7 +1164,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1175
1164
if let ty:: Tuple ( ref args) = principal. substs . type_at ( 0 ) . sty {
1176
1165
let mut projections = predicates. projection_bounds ( ) ;
1177
1166
if let ( Some ( proj) , None ) = ( projections. next ( ) , projections. next ( ) ) {
1178
- nest ! ( |cx| cx . pretty_fn_sig( args, false , proj. ty) ) ;
1167
+ nest ! ( self . pretty_fn_sig( args, false , proj. ty) ) ;
1179
1168
resugared = true ;
1180
1169
}
1181
1170
}
@@ -1214,8 +1203,8 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1214
1203
let args = arg0. into_iter ( ) . chain ( args) ;
1215
1204
let projections = projection0. into_iter ( ) . chain ( projections) ;
1216
1205
1217
- nest ! ( |cx| cx . generic_delimiters( |mut cx| {
1218
- cx = cx . nest ( |cx| cx. comma_sep( args) ) ? ;
1206
+ nest ! ( self . generic_delimiters( |mut cx| {
1207
+ cx = PrintCx :: new ( cx . tcx , cx. comma_sep( args) ? ) ;
1219
1208
if arg0. is_some( ) && projection0. is_some( ) {
1220
1209
write!( cx, ", " ) ?;
1221
1210
}
@@ -1248,7 +1237,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
1248
1237
}
1249
1238
first = false ;
1250
1239
1251
- nest ! ( |cx| cx . print_def_path( def_id, None ) ) ;
1240
+ nest ! ( self . print_def_path( def_id, None ) ) ;
1252
1241
}
1253
1242
1254
1243
self . ok ( )
@@ -1517,7 +1506,7 @@ define_print_and_forward_display! {
1517
1506
ty:: ExistentialPredicate :: Trait ( x) => p!( print( x) ) ,
1518
1507
ty:: ExistentialPredicate :: Projection ( x) => p!( print( x) ) ,
1519
1508
ty:: ExistentialPredicate :: AutoTrait ( def_id) => {
1520
- nest!( |cx| cx. print_def_path( def_id, None ) )
1509
+ nest!( cx. print_def_path( def_id, None ) )
1521
1510
}
1522
1511
}
1523
1512
}
@@ -1532,7 +1521,7 @@ define_print_and_forward_display! {
1532
1521
}
1533
1522
1534
1523
p!( write( "fn" ) ) ;
1535
- nest!( |cx| cx. pretty_fn_sig( self . inputs( ) , self . variadic, self . output( ) ) ) ;
1524
+ nest!( cx. pretty_fn_sig( self . inputs( ) , self . variadic, self . output( ) ) ) ;
1536
1525
}
1537
1526
1538
1527
ty:: InferTy {
@@ -1551,7 +1540,7 @@ define_print_and_forward_display! {
1551
1540
}
1552
1541
1553
1542
ty:: TraitRef <' tcx> {
1554
- nest!( |cx| cx. print_def_path( self . def_id, Some ( self . substs) ) ) ;
1543
+ nest!( cx. print_def_path( self . def_id, Some ( self . substs) ) ) ;
1555
1544
}
1556
1545
1557
1546
ty:: ParamTy {
@@ -1571,7 +1560,7 @@ define_print_and_forward_display! {
1571
1560
}
1572
1561
1573
1562
ty:: ProjectionTy <' tcx> {
1574
- nest!( |cx| cx. print_def_path( self . item_def_id, Some ( self . substs) ) ) ;
1563
+ nest!( cx. print_def_path( self . item_def_id, Some ( self . substs) ) ) ;
1575
1564
}
1576
1565
1577
1566
ty:: ClosureKind {
@@ -1592,17 +1581,17 @@ define_print_and_forward_display! {
1592
1581
ty:: Predicate :: WellFormed ( ty) => p!( print( ty) , write( " well-formed" ) ) ,
1593
1582
ty:: Predicate :: ObjectSafe ( trait_def_id) => {
1594
1583
p!( write( "the trait `" ) ) ;
1595
- nest!( |cx| cx. print_def_path( trait_def_id, None ) ) ;
1584
+ nest!( cx. print_def_path( trait_def_id, None ) ) ;
1596
1585
p!( write( "` is object-safe" ) )
1597
1586
}
1598
1587
ty:: Predicate :: ClosureKind ( closure_def_id, _closure_substs, kind) => {
1599
1588
p!( write( "the closure `" ) ) ;
1600
- nest!( |cx| cx. print_value_path( closure_def_id, None ) ) ;
1589
+ nest!( cx. print_value_path( closure_def_id, None ) ) ;
1601
1590
p!( write( "` implements the trait `{}`" , kind) )
1602
1591
}
1603
1592
ty:: Predicate :: ConstEvaluatable ( def_id, substs) => {
1604
1593
p!( write( "the constant `" ) ) ;
1605
- nest!( |cx| cx. print_value_path( def_id, Some ( substs) ) ) ;
1594
+ nest!( cx. print_value_path( def_id, Some ( substs) ) ) ;
1606
1595
p!( write( "` can be evaluated" ) )
1607
1596
}
1608
1597
}
0 commit comments