@@ -93,7 +93,7 @@ impl<'tcx> ConstValue<'tcx> {
9393/// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
9494/// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
9595/// 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 ,
9797 RustcEncodable , RustcDecodable , Hash , HashStable ) ]
9898pub enum Scalar < Tag =( ) , Id =AllocId > {
9999 /// The raw bytes of a simple value.
@@ -113,6 +113,27 @@ pub enum Scalar<Tag=(), Id=AllocId> {
113113#[ cfg( target_arch = "x86_64" ) ]
114114static_assert_size ! ( Scalar , 24 ) ;
115115
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 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+
116137impl < Tag > fmt:: Display for Scalar < Tag > {
117138 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
118139 match self {
@@ -412,7 +433,7 @@ impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
412433 }
413434}
414435
415- #[ derive( Clone , Copy , Debug , Eq , PartialEq , Ord , PartialOrd , RustcEncodable , RustcDecodable , Hash ) ]
436+ #[ derive( Clone , Copy , Eq , PartialEq , Ord , PartialOrd , RustcEncodable , RustcDecodable , Hash ) ]
416437pub enum ScalarMaybeUndef < Tag =( ) , Id =AllocId > {
417438 Scalar ( Scalar < Tag , Id > ) ,
418439 Undef ,
@@ -425,6 +446,15 @@ impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
425446 }
426447}
427448
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+
428458impl < Tag > fmt:: Display for ScalarMaybeUndef < Tag > {
429459 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
430460 match self {
0 commit comments