@@ -956,32 +956,40 @@ pub trait PrettyPrinter<'tcx>:
956
956
}
957
957
958
958
fn pretty_print_const_scalar (
959
- mut self ,
959
+ self ,
960
960
scalar : Scalar ,
961
961
ty : Ty < ' tcx > ,
962
962
print_ty : bool ,
963
+ ) -> Result < Self :: Const , Self :: Error > {
964
+ match scalar {
965
+ Scalar :: Ptr ( ptr) => self . pretty_print_const_scalar_ptr ( ptr, ty, print_ty) ,
966
+ Scalar :: Int ( int) => self . pretty_print_const_scalar_int ( int, ty, print_ty) ,
967
+ }
968
+ }
969
+
970
+ fn pretty_print_const_scalar_ptr (
971
+ mut self ,
972
+ ptr : Pointer ,
973
+ ty : Ty < ' tcx > ,
974
+ print_ty : bool ,
963
975
) -> Result < Self :: Const , Self :: Error > {
964
976
define_scoped_cx ! ( self ) ;
965
977
966
- match ( scalar , & ty. kind ( ) ) {
978
+ match ty. kind ( ) {
967
979
// Byte strings (&[u8; N])
968
- (
969
- Scalar :: Ptr ( ptr) ,
970
- ty:: Ref (
971
- _,
972
- ty:: TyS {
973
- kind :
974
- ty:: Array (
975
- ty:: TyS { kind : ty:: Uint ( ty:: UintTy :: U8 ) , .. } ,
976
- ty:: Const {
977
- val : ty:: ConstKind :: Value ( ConstValue :: Scalar ( int) ) ,
978
- ..
979
- } ,
980
- ) ,
981
- ..
982
- } ,
983
- _,
984
- ) ,
980
+ ty:: Ref (
981
+ _,
982
+ ty:: TyS {
983
+ kind :
984
+ ty:: Array (
985
+ ty:: TyS { kind : ty:: Uint ( ty:: UintTy :: U8 ) , .. } ,
986
+ ty:: Const {
987
+ val : ty:: ConstKind :: Value ( ConstValue :: Scalar ( int) ) , ..
988
+ } ,
989
+ ) ,
990
+ ..
991
+ } ,
992
+ _,
985
993
) => match self . tcx ( ) . get_global_alloc ( ptr. alloc_id ) {
986
994
Some ( GlobalAlloc :: Memory ( alloc) ) => {
987
995
let bytes = int. assert_bits ( self . tcx ( ) . data_layout . pointer_size ) ;
@@ -997,28 +1005,59 @@ pub trait PrettyPrinter<'tcx>:
997
1005
Some ( GlobalAlloc :: Function ( _) ) => p ! ( "<function>" ) ,
998
1006
None => p ! ( "<dangling pointer>" ) ,
999
1007
} ,
1008
+ ty:: FnPtr ( _) => {
1009
+ // FIXME: We should probably have a helper method to share code with the "Byte strings"
1010
+ // printing above (which also has to handle pointers to all sorts of things).
1011
+ match self . tcx ( ) . get_global_alloc ( ptr. alloc_id ) {
1012
+ Some ( GlobalAlloc :: Function ( instance) ) => {
1013
+ self = self . typed_value (
1014
+ |this| this. print_value_path ( instance. def_id ( ) , instance. substs ) ,
1015
+ |this| this. print_type ( ty) ,
1016
+ " as " ,
1017
+ ) ?;
1018
+ }
1019
+ _ => self = self . pretty_print_const_pointer ( ptr, ty, print_ty) ?,
1020
+ }
1021
+ }
1022
+ // Any pointer values not covered by a branch above
1023
+ _ => {
1024
+ self = self . pretty_print_const_pointer ( ptr, ty, print_ty) ?;
1025
+ }
1026
+ }
1027
+ Ok ( self )
1028
+ }
1029
+
1030
+ fn pretty_print_const_scalar_int (
1031
+ mut self ,
1032
+ int : ScalarInt ,
1033
+ ty : Ty < ' tcx > ,
1034
+ print_ty : bool ,
1035
+ ) -> Result < Self :: Const , Self :: Error > {
1036
+ define_scoped_cx ! ( self ) ;
1037
+
1038
+ match ty. kind ( ) {
1000
1039
// Bool
1001
- ( Scalar :: Int ( int ) , ty:: Bool ) if int == ScalarInt :: FALSE => p ! ( "false" ) ,
1002
- ( Scalar :: Int ( int ) , ty:: Bool ) if int == ScalarInt :: TRUE => p ! ( "true" ) ,
1040
+ ty:: Bool if int == ScalarInt :: FALSE => p ! ( "false" ) ,
1041
+ ty:: Bool if int == ScalarInt :: TRUE => p ! ( "true" ) ,
1003
1042
// Float
1004
- ( Scalar :: Int ( int ) , ty:: Float ( ty:: FloatTy :: F32 ) ) => {
1043
+ ty:: Float ( ty:: FloatTy :: F32 ) => {
1005
1044
p ! ( write( "{}f32" , Single :: try_from( int) . unwrap( ) ) )
1006
1045
}
1007
- ( Scalar :: Int ( int ) , ty:: Float ( ty:: FloatTy :: F64 ) ) => {
1046
+ ty:: Float ( ty:: FloatTy :: F64 ) => {
1008
1047
p ! ( write( "{}f64" , Double :: try_from( int) . unwrap( ) ) )
1009
1048
}
1010
1049
// Int
1011
- ( Scalar :: Int ( int ) , ty:: Uint ( _) | ty:: Int ( _) ) => {
1050
+ ty:: Uint ( _) | ty:: Int ( _) => {
1012
1051
let int =
1013
1052
ConstInt :: new ( int, matches ! ( ty. kind( ) , ty:: Int ( _) ) , ty. is_ptr_sized_integral ( ) ) ;
1014
1053
if print_ty { p ! ( write( "{:#?}" , int) ) } else { p ! ( write( "{:?}" , int) ) }
1015
1054
}
1016
1055
// Char
1017
- ( Scalar :: Int ( int ) , ty:: Char ) if char:: try_from ( int) . is_ok ( ) => {
1056
+ ty:: Char if char:: try_from ( int) . is_ok ( ) => {
1018
1057
p ! ( write( "{:?}" , char :: try_from( int) . unwrap( ) ) )
1019
1058
}
1020
1059
// Raw pointers
1021
- ( Scalar :: Int ( int ) , ty:: RawPtr ( _) | ty:: FnPtr ( _) ) => {
1060
+ ty:: RawPtr ( _) | ty:: FnPtr ( _) => {
1022
1061
let data = int. assert_bits ( self . tcx ( ) . data_layout . pointer_size ) ;
1023
1062
self = self . typed_value (
1024
1063
|mut this| {
@@ -1029,26 +1068,12 @@ pub trait PrettyPrinter<'tcx>:
1029
1068
" as " ,
1030
1069
) ?;
1031
1070
}
1032
- ( Scalar :: Ptr ( ptr) , ty:: FnPtr ( _) ) => {
1033
- // FIXME: We should probably have a helper method to share code with the "Byte strings"
1034
- // printing above (which also has to handle pointers to all sorts of things).
1035
- match self . tcx ( ) . get_global_alloc ( ptr. alloc_id ) {
1036
- Some ( GlobalAlloc :: Function ( instance) ) => {
1037
- self = self . typed_value (
1038
- |this| this. print_value_path ( instance. def_id ( ) , instance. substs ) ,
1039
- |this| this. print_type ( ty) ,
1040
- " as " ,
1041
- ) ?;
1042
- }
1043
- _ => self = self . pretty_print_const_pointer ( ptr, ty, print_ty) ?,
1044
- }
1045
- }
1046
1071
// For function type zsts just printing the path is enough
1047
- ( Scalar :: Int ( int ) , ty:: FnDef ( d, s) ) if int == ScalarInt :: ZST => {
1072
+ ty:: FnDef ( d, s) if int == ScalarInt :: ZST => {
1048
1073
p ! ( print_value_path( * d, s) )
1049
1074
}
1050
1075
// Nontrivial types with scalar bit representation
1051
- ( Scalar :: Int ( int ) , _ ) => {
1076
+ _ => {
1052
1077
let print = |mut this : Self | {
1053
1078
if int. size ( ) == Size :: ZERO {
1054
1079
write ! ( this, "transmute(())" ) ?;
@@ -1063,10 +1088,6 @@ pub trait PrettyPrinter<'tcx>:
1063
1088
print ( self ) ?
1064
1089
} ;
1065
1090
}
1066
- // Any pointer values not covered by a branch above
1067
- ( Scalar :: Ptr ( p) , _) => {
1068
- self = self . pretty_print_const_pointer ( p, ty, print_ty) ?;
1069
- }
1070
1091
}
1071
1092
Ok ( self )
1072
1093
}
0 commit comments