@@ -93,7 +93,7 @@ impl<'tcx> ConstValue<'tcx> {
93
93
/// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
94
94
/// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
95
95
/// of a simple value or a pointer into another `Allocation`
96
- #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd ,
96
+ #[ derive( Clone , Copy , Eq , PartialEq , Ord , PartialOrd ,
97
97
RustcEncodable , RustcDecodable , Hash , HashStable ) ]
98
98
pub enum Scalar < Tag =( ) , Id =AllocId > {
99
99
/// The raw bytes of a simple value.
@@ -113,6 +113,27 @@ pub enum Scalar<Tag=(), Id=AllocId> {
113
113
#[ cfg( target_arch = "x86_64" ) ]
114
114
static_assert_size ! ( Scalar , 24 ) ;
115
115
116
+ impl < Tag : fmt:: Debug , Id : fmt:: Debug > fmt:: Debug for Scalar < Tag , Id > {
117
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
118
+ match self {
119
+ Scalar :: Ptr ( ptr) =>
120
+ write ! ( f, "{:?}" , ptr) ,
121
+ & Scalar :: Bits { bits, size } => {
122
+ if size == 0 {
123
+ assert_eq ! ( bits, 0 , "ZST value must be 0" ) ;
124
+ write ! ( f, "<ZST>" )
125
+ } else {
126
+ assert_eq ! ( truncate( bits, Size :: from_bytes( size as u64 ) ) , bits,
127
+ "Scalar value {:#x} exceeds size of {} bytes" , bits, size) ;
128
+ // Format as hex number wide enough to fit any value of the given `size`.
129
+ // So e.g. bits=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
130
+ write ! ( f, "0x{:>0width$x}" , bits, width=( size* 2 ) as usize )
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+
116
137
impl < Tag > fmt:: Display for Scalar < Tag > {
117
138
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
118
139
match self {
@@ -412,7 +433,7 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
412
433
}
413
434
}
414
435
415
- #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd , RustcEncodable , RustcDecodable , Hash ) ]
436
+ #[ derive( Clone , Copy , Eq , PartialEq , Ord , PartialOrd , RustcEncodable , RustcDecodable , Hash ) ]
416
437
pub enum ScalarMaybeUndef < Tag =( ) , Id =AllocId > {
417
438
Scalar ( Scalar < Tag , Id > ) ,
418
439
Undef ,
@@ -425,6 +446,15 @@ impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
425
446
}
426
447
}
427
448
449
+ impl < Tag : fmt:: Debug , Id : fmt:: Debug > fmt:: Debug for ScalarMaybeUndef < Tag , Id > {
450
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
451
+ match self {
452
+ ScalarMaybeUndef :: Undef => write ! ( f, "Undef" ) ,
453
+ ScalarMaybeUndef :: Scalar ( s) => write ! ( f, "{:?}" , s) ,
454
+ }
455
+ }
456
+ }
457
+
428
458
impl < Tag > fmt:: Display for ScalarMaybeUndef < Tag > {
429
459
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
430
460
match self {
0 commit comments