@@ -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
///
@@ -100,6 +101,42 @@ pub struct ImmTy<'tcx, Tag=()> {
100
101
pub layout : TyLayout < ' tcx > ,
101
102
}
102
103
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
+
103
140
impl < ' tcx , Tag > :: std:: ops:: Deref for ImmTy < ' tcx , Tag > {
104
141
type Target = Immediate < Tag > ;
105
142
#[ inline( always) ]
0 commit comments