@@ -19,6 +19,7 @@ use super::{
19
19
} ;
20
20
pub use rustc:: mir:: interpret:: ScalarMaybeUndef ;
21
21
use rustc_macros:: HashStable ;
22
+ use syntax:: ast;
22
23
23
24
/// An `Immediate` represents a single immediate self-contained Rust value.
24
25
///
@@ -93,6 +94,42 @@ pub struct ImmTy<'tcx, Tag=()> {
93
94
pub layout : TyLayout < ' tcx > ,
94
95
}
95
96
97
+ // `Tag: Copy` because some methods on `Scalar` consume them by value
98
+ impl < Tag : Copy > std:: fmt:: Display for ImmTy < ' tcx , Tag > {
99
+ fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
100
+ match & self . imm {
101
+ Immediate :: Scalar ( ScalarMaybeUndef :: Scalar ( s) ) => match s. to_bits ( self . layout . size ) {
102
+ Ok ( s) => {
103
+ match self . layout . ty . kind {
104
+ ty:: Int ( _) => return write ! (
105
+ fmt, "{}" ,
106
+ super :: sign_extend( s, self . layout. size) as i128 ,
107
+ ) ,
108
+ ty:: Uint ( _) => return write ! ( fmt, "{}" , s) ,
109
+ ty:: Bool if s == 0 => return fmt. write_str ( "false" ) ,
110
+ ty:: Bool if s == 1 => return fmt. write_str ( "true" ) ,
111
+ ty:: Char => if let Some ( c) =
112
+ u32:: try_from ( s) . ok ( ) . and_then ( std:: char:: from_u32) {
113
+ return write ! ( fmt, "{}" , c) ;
114
+ } ,
115
+ ty:: Float ( ast:: FloatTy :: F32 ) => if let Ok ( u) = u32:: try_from ( s) {
116
+ return write ! ( fmt, "{}" , f32 :: from_bits( u) ) ;
117
+ } ,
118
+ ty:: Float ( ast:: FloatTy :: F64 ) => if let Ok ( u) = u64:: try_from ( s) {
119
+ return write ! ( fmt, "{}" , f64 :: from_bits( u) ) ;
120
+ } ,
121
+ _ => { } ,
122
+ }
123
+ write ! ( fmt, "{:x}" , s)
124
+ } ,
125
+ Err ( _) => fmt. write_str ( "{pointer}" ) ,
126
+ } ,
127
+ Immediate :: Scalar ( ScalarMaybeUndef :: Undef ) => fmt. write_str ( "{undef}" ) ,
128
+ Immediate :: ScalarPair ( ..) => fmt. write_str ( "{wide pointer or tuple}" ) ,
129
+ }
130
+ }
131
+ }
132
+
96
133
impl < ' tcx , Tag > :: std:: ops:: Deref for ImmTy < ' tcx , Tag > {
97
134
type Target = Immediate < Tag > ;
98
135
#[ inline( always) ]
0 commit comments