@@ -211,15 +211,20 @@ pub trait PrettyPrinter<'tcx>:
211
211
Ok ( self )
212
212
}
213
213
214
- /// Prints `{... }` around what `f` (and optionally `t`) print
215
- fn type_ascribed_value (
214
+ /// Prints `{f: t }` or `{f as t}` depending on the `cast` argument
215
+ fn typed_value (
216
216
mut self ,
217
217
f : impl FnOnce ( Self ) -> Result < Self , Self :: Error > ,
218
218
t : impl FnOnce ( Self ) -> Result < Self , Self :: Error > ,
219
+ cast : bool ,
219
220
) -> Result < Self :: Const , Self :: Error > {
220
221
self . write_str ( "{" ) ?;
221
222
self = f ( self ) ?;
222
- self . write_str ( ": " ) ?;
223
+ if cast {
224
+ self . write_str ( " as " ) ?;
225
+ } else {
226
+ self . write_str ( ": " ) ?;
227
+ }
223
228
self = t ( self ) ?;
224
229
self . write_str ( "}" ) ?;
225
230
Ok ( self )
@@ -990,7 +995,14 @@ pub trait PrettyPrinter<'tcx>:
990
995
}
991
996
// Raw pointers
992
997
( Scalar :: Raw { data, .. } , ty:: RawPtr ( _) ) => {
993
- p ! ( write( "{{0x{:x} as " , data) , print( ty) , write( "}}" ) )
998
+ self = self . typed_value (
999
+ |mut this| {
1000
+ write ! ( this, "0x{:x}" , data) ?;
1001
+ Ok ( this)
1002
+ } ,
1003
+ |this| this. print_type ( ty) ,
1004
+ true ,
1005
+ ) ?;
994
1006
}
995
1007
( Scalar :: Ptr ( ptr) , ty:: FnPtr ( _) ) => {
996
1008
let instance = {
@@ -1025,7 +1037,7 @@ pub trait PrettyPrinter<'tcx>:
1025
1037
Ok ( this)
1026
1038
} ;
1027
1039
self = if print_ty {
1028
- self . type_ascribed_value ( print, |this| this. print_type ( ty) ) ?
1040
+ self . typed_value ( print, |this| this. print_type ( ty) , false ) ?
1029
1041
} else {
1030
1042
print ( self ) ?
1031
1043
} ;
@@ -1047,12 +1059,13 @@ pub trait PrettyPrinter<'tcx>:
1047
1059
print_ty : bool ,
1048
1060
) -> Result < Self :: Const , Self :: Error > {
1049
1061
if print_ty {
1050
- self . type_ascribed_value (
1062
+ self . typed_value (
1051
1063
|mut this| {
1052
1064
this. write_str ( "&_" ) ?;
1053
1065
Ok ( this)
1054
1066
} ,
1055
1067
|this| this. print_type ( ty) ,
1068
+ false ,
1056
1069
)
1057
1070
} else {
1058
1071
self . write_str ( "&_" ) ?;
@@ -1449,14 +1462,19 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1449
1462
self . pretty_in_binder ( value)
1450
1463
}
1451
1464
1452
- fn type_ascribed_value (
1465
+ fn typed_value (
1453
1466
mut self ,
1454
1467
f : impl FnOnce ( Self ) -> Result < Self , Self :: Error > ,
1455
1468
t : impl FnOnce ( Self ) -> Result < Self , Self :: Error > ,
1469
+ cast : bool ,
1456
1470
) -> Result < Self :: Const , Self :: Error > {
1457
1471
self . write_str ( "{" ) ?;
1458
1472
self = f ( self ) ?;
1459
- self . write_str ( ": " ) ?;
1473
+ if cast {
1474
+ self . write_str ( " as " ) ?;
1475
+ } else {
1476
+ self . write_str ( ": " ) ?;
1477
+ }
1460
1478
let was_in_value = std:: mem:: replace ( & mut self . in_value , false ) ;
1461
1479
self = t ( self ) ?;
1462
1480
self . in_value = was_in_value;
@@ -1537,7 +1555,7 @@ impl<F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1537
1555
Ok ( this)
1538
1556
} ;
1539
1557
if print_ty {
1540
- self . type_ascribed_value ( print, |this| this. print_type ( ty) )
1558
+ self . typed_value ( print, |this| this. print_type ( ty) , false )
1541
1559
} else {
1542
1560
print ( self )
1543
1561
}
0 commit comments