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