@@ -232,7 +232,7 @@ impl<'tcx, Tag> Scalar<Tag> {
232232 }
233233 }
234234
235- /// Returns this pointers offset from the allocation base, or from NULL (for
235+ /// Returns this pointer's offset from the allocation base, or from NULL (for
236236 /// integer pointers).
237237 #[ inline]
238238 pub fn get_ptr_offset ( self , cx : & impl HasDataLayout ) -> Size {
@@ -269,7 +269,7 @@ impl<'tcx, Tag> Scalar<Tag> {
269269 #[ inline]
270270 pub fn from_uint ( i : impl Into < u128 > , size : Size ) -> Self {
271271 let i = i. into ( ) ;
272- debug_assert_eq ! ( truncate( i, size) , i,
272+ assert_eq ! ( truncate( i, size) , i,
273273 "Unsigned value {} does not fit in {} bits" , i, size. bits( ) ) ;
274274 Scalar :: Raw { data : i, size : size. bytes ( ) as u8 }
275275 }
@@ -279,7 +279,7 @@ impl<'tcx, Tag> Scalar<Tag> {
279279 let i = i. into ( ) ;
280280 // `into` performed sign extension, we have to truncate
281281 let truncated = truncate ( i as u128 , size) ;
282- debug_assert_eq ! ( sign_extend( truncated, size) as i128 , i,
282+ assert_eq ! ( sign_extend( truncated, size) as i128 , i,
283283 "Signed value {} does not fit in {} bits" , i, size. bits( ) ) ;
284284 Scalar :: Raw { data : truncated, size : size. bytes ( ) as u8 }
285285 }
@@ -294,12 +294,35 @@ impl<'tcx, Tag> Scalar<Tag> {
294294 Scalar :: Raw { data : f. to_bits ( ) as u128 , size : 8 }
295295 }
296296
297+ #[ inline]
298+ pub fn to_bits_or_ptr (
299+ self ,
300+ target_size : Size ,
301+ cx : & impl HasDataLayout ,
302+ ) -> Result < u128 , Pointer < Tag > > {
303+ match self {
304+ Scalar :: Raw { data, size } => {
305+ assert_eq ! ( target_size. bytes( ) , size as u64 ) ;
306+ assert_ne ! ( size, 0 , "to_bits cannot be used with zsts" ) ;
307+ assert_eq ! ( truncate( data, target_size) , data,
308+ "Scalar value {:#x} exceeds size of {} bytes" , data, size) ;
309+ Ok ( data)
310+ }
311+ Scalar :: Ptr ( ptr) => {
312+ assert_eq ! ( target_size, cx. data_layout( ) . pointer_size) ;
313+ Err ( ptr)
314+ }
315+ }
316+ }
317+
297318 #[ inline]
298319 pub fn to_bits ( self , target_size : Size ) -> EvalResult < ' tcx , u128 > {
299320 match self {
300321 Scalar :: Raw { data, size } => {
301322 assert_eq ! ( target_size. bytes( ) , size as u64 ) ;
302323 assert_ne ! ( size, 0 , "to_bits cannot be used with zsts" ) ;
324+ assert_eq ! ( truncate( data, target_size) , data,
325+ "Scalar value {:#x} exceeds size of {} bytes" , data, size) ;
303326 Ok ( data)
304327 }
305328 Scalar :: Ptr ( _) => err ! ( ReadPointerAsBytes ) ,
@@ -350,58 +373,51 @@ impl<'tcx, Tag> Scalar<Tag> {
350373 pub fn to_u8 ( self ) -> EvalResult < ' static , u8 > {
351374 let sz = Size :: from_bits ( 8 ) ;
352375 let b = self . to_bits ( sz) ?;
353- assert_eq ! ( b as u8 as u128 , b) ;
354376 Ok ( b as u8 )
355377 }
356378
357379 pub fn to_u32 ( self ) -> EvalResult < ' static , u32 > {
358380 let sz = Size :: from_bits ( 32 ) ;
359381 let b = self . to_bits ( sz) ?;
360- assert_eq ! ( b as u32 as u128 , b) ;
361382 Ok ( b as u32 )
362383 }
363384
364385 pub fn to_u64 ( self ) -> EvalResult < ' static , u64 > {
365386 let sz = Size :: from_bits ( 64 ) ;
366387 let b = self . to_bits ( sz) ?;
367- assert_eq ! ( b as u64 as u128 , b) ;
368388 Ok ( b as u64 )
369389 }
370390
371391 pub fn to_usize ( self , cx : & impl HasDataLayout ) -> EvalResult < ' static , u64 > {
372392 let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ?;
373- assert_eq ! ( b as u64 as u128 , b) ;
374393 Ok ( b as u64 )
375394 }
376395
377396 pub fn to_i8 ( self ) -> EvalResult < ' static , i8 > {
378397 let sz = Size :: from_bits ( 8 ) ;
379398 let b = self . to_bits ( sz) ?;
380399 let b = sign_extend ( b, sz) as i128 ;
381- assert_eq ! ( b as i8 as i128 , b) ;
382400 Ok ( b as i8 )
383401 }
384402
385403 pub fn to_i32 ( self ) -> EvalResult < ' static , i32 > {
386404 let sz = Size :: from_bits ( 32 ) ;
387405 let b = self . to_bits ( sz) ?;
388406 let b = sign_extend ( b, sz) as i128 ;
389- assert_eq ! ( b as i32 as i128 , b) ;
390407 Ok ( b as i32 )
391408 }
392409
393410 pub fn to_i64 ( self ) -> EvalResult < ' static , i64 > {
394411 let sz = Size :: from_bits ( 64 ) ;
395412 let b = self . to_bits ( sz) ?;
396413 let b = sign_extend ( b, sz) as i128 ;
397- assert_eq ! ( b as i64 as i128 , b) ;
398414 Ok ( b as i64 )
399415 }
400416
401417 pub fn to_isize ( self , cx : & impl HasDataLayout ) -> EvalResult < ' static , i64 > {
402- let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ? ;
403- let b = sign_extend ( b , cx . data_layout ( ) . pointer_size ) as i128 ;
404- assert_eq ! ( b as i64 as i128 , b ) ;
418+ let sz = cx. data_layout ( ) . pointer_size ;
419+ let b = self . to_bits ( sz ) ? ;
420+ let b = sign_extend ( b , sz ) as i128 ;
405421 Ok ( b as i64 )
406422 }
407423
0 commit comments