@@ -133,6 +133,20 @@ pub macro with_no_queries($e:expr) {{
133
133
) )
134
134
} }
135
135
136
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
137
+ pub enum WrapBinderMode {
138
+ ForAll ,
139
+ Unsafe ,
140
+ }
141
+ impl WrapBinderMode {
142
+ pub fn start_str ( self ) -> & ' static str {
143
+ match self {
144
+ WrapBinderMode :: ForAll => "for<" ,
145
+ WrapBinderMode :: Unsafe => "unsafe<" ,
146
+ }
147
+ }
148
+ }
149
+
136
150
/// The "region highlights" are used to control region printing during
137
151
/// specific error messages. When a "region highlight" is enabled, it
138
152
/// gives an alternate way to print specific regions. For now, we
@@ -219,7 +233,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
219
233
self . print_def_path ( def_id, args)
220
234
}
221
235
222
- fn in_binder < T > ( & mut self , value : & ty:: Binder < ' tcx , T > ) -> Result < ( ) , PrintError >
236
+ fn print_in_binder < T > ( & mut self , value : & ty:: Binder < ' tcx , T > ) -> Result < ( ) , PrintError >
223
237
where
224
238
T : Print < ' tcx , Self > + TypeFoldable < TyCtxt < ' tcx > > ,
225
239
{
@@ -229,6 +243,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
229
243
fn wrap_binder < T , F : FnOnce ( & T , & mut Self ) -> Result < ( ) , fmt:: Error > > (
230
244
& mut self ,
231
245
value : & ty:: Binder < ' tcx , T > ,
246
+ _mode : WrapBinderMode ,
232
247
f : F ,
233
248
) -> Result < ( ) , PrintError >
234
249
where
@@ -703,8 +718,9 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
703
718
}
704
719
ty:: FnPtr ( ref sig_tys, hdr) => p ! ( print( sig_tys. with( hdr) ) ) ,
705
720
ty:: UnsafeBinder ( ref bound_ty) => {
706
- // FIXME(unsafe_binders): Make this print `unsafe<>` rather than `for<>`.
707
- self . wrap_binder ( bound_ty, |ty, cx| cx. pretty_print_type ( * ty) ) ?;
721
+ self . wrap_binder ( bound_ty, WrapBinderMode :: Unsafe , |ty, cx| {
722
+ cx. pretty_print_type ( * ty)
723
+ } ) ?;
708
724
}
709
725
ty:: Infer ( infer_ty) => {
710
726
if self . should_print_verbose ( ) {
@@ -1067,29 +1083,33 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
1067
1083
} ;
1068
1084
1069
1085
if let Some ( return_ty) = entry. return_ty {
1070
- self . wrap_binder ( & bound_args_and_self_ty, |( args, _) , cx| {
1071
- define_scoped_cx ! ( cx) ;
1072
- p ! ( write( "{}" , tcx. item_name( trait_def_id) ) ) ;
1073
- p ! ( "(" ) ;
1074
-
1075
- for ( idx, ty) in args. iter ( ) . enumerate ( ) {
1076
- if idx > 0 {
1077
- p ! ( ", " ) ;
1086
+ self . wrap_binder (
1087
+ & bound_args_and_self_ty,
1088
+ WrapBinderMode :: ForAll ,
1089
+ |( args, _) , cx| {
1090
+ define_scoped_cx ! ( cx) ;
1091
+ p ! ( write( "{}" , tcx. item_name( trait_def_id) ) ) ;
1092
+ p ! ( "(" ) ;
1093
+
1094
+ for ( idx, ty) in args. iter ( ) . enumerate ( ) {
1095
+ if idx > 0 {
1096
+ p ! ( ", " ) ;
1097
+ }
1098
+ p ! ( print( ty) ) ;
1078
1099
}
1079
- p ! ( print( ty) ) ;
1080
- }
1081
1100
1082
- p ! ( ")" ) ;
1083
- if let Some ( ty) = return_ty. skip_binder ( ) . as_type ( ) {
1084
- if !ty. is_unit ( ) {
1085
- p ! ( " -> " , print( return_ty) ) ;
1101
+ p ! ( ")" ) ;
1102
+ if let Some ( ty) = return_ty. skip_binder ( ) . as_type ( ) {
1103
+ if !ty. is_unit ( ) {
1104
+ p ! ( " -> " , print( return_ty) ) ;
1105
+ }
1086
1106
}
1087
- }
1088
- p ! ( write( "{}" , if paren_needed { ")" } else { "" } ) ) ;
1107
+ p ! ( write( "{}" , if paren_needed { ")" } else { "" } ) ) ;
1089
1108
1090
- first = false ;
1091
- Ok ( ( ) )
1092
- } ) ?;
1109
+ first = false ;
1110
+ Ok ( ( ) )
1111
+ } ,
1112
+ ) ?;
1093
1113
} else {
1094
1114
// Otherwise, render this like a regular trait.
1095
1115
traits. insert (
@@ -1110,7 +1130,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
1110
1130
for ( trait_pred, assoc_items) in traits {
1111
1131
write ! ( self , "{}" , if first { "" } else { " + " } ) ?;
1112
1132
1113
- self . wrap_binder ( & trait_pred, |trait_pred, cx| {
1133
+ self . wrap_binder ( & trait_pred, WrapBinderMode :: ForAll , |trait_pred, cx| {
1114
1134
define_scoped_cx ! ( cx) ;
1115
1135
1116
1136
if trait_pred. polarity == ty:: PredicatePolarity :: Negative {
@@ -1302,7 +1322,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
1302
1322
let mut first = true ;
1303
1323
1304
1324
if let Some ( bound_principal) = predicates. principal ( ) {
1305
- self . wrap_binder ( & bound_principal, |principal, cx| {
1325
+ self . wrap_binder ( & bound_principal, WrapBinderMode :: ForAll , |principal, cx| {
1306
1326
define_scoped_cx ! ( cx) ;
1307
1327
p ! ( print_def_path( principal. def_id, & [ ] ) ) ;
1308
1328
@@ -1927,7 +1947,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
1927
1947
let kind = closure. kind_ty ( ) . to_opt_closure_kind ( ) . unwrap_or ( ty:: ClosureKind :: Fn ) ;
1928
1948
1929
1949
write ! ( self , "impl " ) ?;
1930
- self . wrap_binder ( & sig, |sig, cx| {
1950
+ self . wrap_binder ( & sig, WrapBinderMode :: ForAll , |sig, cx| {
1931
1951
define_scoped_cx ! ( cx) ;
1932
1952
1933
1953
p ! ( write( "{kind}(" ) ) ;
@@ -2367,22 +2387,23 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
2367
2387
Ok ( ( ) )
2368
2388
}
2369
2389
2370
- fn in_binder < T > ( & mut self , value : & ty:: Binder < ' tcx , T > ) -> Result < ( ) , PrintError >
2390
+ fn print_in_binder < T > ( & mut self , value : & ty:: Binder < ' tcx , T > ) -> Result < ( ) , PrintError >
2371
2391
where
2372
2392
T : Print < ' tcx , Self > + TypeFoldable < TyCtxt < ' tcx > > ,
2373
2393
{
2374
- self . pretty_in_binder ( value)
2394
+ self . pretty_print_in_binder ( value)
2375
2395
}
2376
2396
2377
2397
fn wrap_binder < T , C : FnOnce ( & T , & mut Self ) -> Result < ( ) , PrintError > > (
2378
2398
& mut self ,
2379
2399
value : & ty:: Binder < ' tcx , T > ,
2400
+ mode : WrapBinderMode ,
2380
2401
f : C ,
2381
2402
) -> Result < ( ) , PrintError >
2382
2403
where
2383
2404
T : TypeFoldable < TyCtxt < ' tcx > > ,
2384
2405
{
2385
- self . pretty_wrap_binder ( value, f)
2406
+ self . pretty_wrap_binder ( value, mode , f)
2386
2407
}
2387
2408
2388
2409
fn typed_value (
@@ -2632,6 +2653,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2632
2653
pub fn name_all_regions < T > (
2633
2654
& mut self ,
2634
2655
value : & ty:: Binder < ' tcx , T > ,
2656
+ mode : WrapBinderMode ,
2635
2657
) -> Result < ( T , UnordMap < ty:: BoundRegion , ty:: Region < ' tcx > > ) , fmt:: Error >
2636
2658
where
2637
2659
T : TypeFoldable < TyCtxt < ' tcx > > ,
@@ -2705,9 +2727,13 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2705
2727
// anyways.
2706
2728
let ( new_value, map) = if self . should_print_verbose ( ) {
2707
2729
for var in value. bound_vars ( ) . iter ( ) {
2708
- start_or_continue ( self , "for<" , ", " ) ;
2730
+ start_or_continue ( self , mode . start_str ( ) , ", " ) ;
2709
2731
write ! ( self , "{var:?}" ) ?;
2710
2732
}
2733
+ // Unconditionally render `unsafe<>`.
2734
+ if value. bound_vars ( ) . is_empty ( ) && mode == WrapBinderMode :: Unsafe {
2735
+ start_or_continue ( self , mode. start_str ( ) , "" ) ;
2736
+ }
2711
2737
start_or_continue ( self , "" , "> " ) ;
2712
2738
( value. clone ( ) . skip_binder ( ) , UnordMap :: default ( ) )
2713
2739
} else {
@@ -2772,8 +2798,9 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2772
2798
}
2773
2799
} ;
2774
2800
2775
- if !trim_path {
2776
- start_or_continue ( self , "for<" , ", " ) ;
2801
+ // Unconditionally render `unsafe<>`.
2802
+ if !trim_path || mode == WrapBinderMode :: Unsafe {
2803
+ start_or_continue ( self , mode. start_str ( ) , ", " ) ;
2777
2804
do_continue ( self , name) ;
2778
2805
}
2779
2806
ty:: Region :: new_bound ( tcx, ty:: INNERMOST , ty:: BoundRegion { var : br. var , kind } )
@@ -2786,9 +2813,12 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2786
2813
} ;
2787
2814
let new_value = value. clone ( ) . skip_binder ( ) . fold_with ( & mut folder) ;
2788
2815
let region_map = folder. region_map ;
2789
- if !trim_path {
2790
- start_or_continue ( self , "" , "> " ) ;
2816
+
2817
+ if mode == WrapBinderMode :: Unsafe && region_map. is_empty ( ) {
2818
+ start_or_continue ( self , mode. start_str ( ) , "" ) ;
2791
2819
}
2820
+ start_or_continue ( self , "" , "> " ) ;
2821
+
2792
2822
( new_value, region_map)
2793
2823
} ;
2794
2824
@@ -2797,12 +2827,15 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2797
2827
Ok ( ( new_value, map) )
2798
2828
}
2799
2829
2800
- pub fn pretty_in_binder < T > ( & mut self , value : & ty:: Binder < ' tcx , T > ) -> Result < ( ) , fmt:: Error >
2830
+ pub fn pretty_print_in_binder < T > (
2831
+ & mut self ,
2832
+ value : & ty:: Binder < ' tcx , T > ,
2833
+ ) -> Result < ( ) , fmt:: Error >
2801
2834
where
2802
2835
T : Print < ' tcx , Self > + TypeFoldable < TyCtxt < ' tcx > > ,
2803
2836
{
2804
2837
let old_region_index = self . region_index ;
2805
- let ( new_value, _) = self . name_all_regions ( value) ?;
2838
+ let ( new_value, _) = self . name_all_regions ( value, WrapBinderMode :: ForAll ) ?;
2806
2839
new_value. print ( self ) ?;
2807
2840
self . region_index = old_region_index;
2808
2841
self . binder_depth -= 1 ;
@@ -2812,13 +2845,14 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
2812
2845
pub fn pretty_wrap_binder < T , C : FnOnce ( & T , & mut Self ) -> Result < ( ) , fmt:: Error > > (
2813
2846
& mut self ,
2814
2847
value : & ty:: Binder < ' tcx , T > ,
2848
+ mode : WrapBinderMode ,
2815
2849
f : C ,
2816
2850
) -> Result < ( ) , fmt:: Error >
2817
2851
where
2818
2852
T : TypeFoldable < TyCtxt < ' tcx > > ,
2819
2853
{
2820
2854
let old_region_index = self . region_index ;
2821
- let ( new_value, _) = self . name_all_regions ( value) ?;
2855
+ let ( new_value, _) = self . name_all_regions ( value, mode ) ?;
2822
2856
f ( & new_value, self ) ?;
2823
2857
self . region_index = old_region_index;
2824
2858
self . binder_depth -= 1 ;
@@ -2877,7 +2911,7 @@ where
2877
2911
T : Print < ' tcx , P > + TypeFoldable < TyCtxt < ' tcx > > ,
2878
2912
{
2879
2913
fn print ( & self , cx : & mut P ) -> Result < ( ) , PrintError > {
2880
- cx. in_binder ( self )
2914
+ cx. print_in_binder ( self )
2881
2915
}
2882
2916
}
2883
2917
0 commit comments