@@ -170,6 +170,10 @@ impl<Tag> From<Double> for Scalar<Tag> {
170
170
}
171
171
172
172
impl Scalar < ( ) > {
173
+ /// Make sure the `data` fits in `size`.
174
+ /// This is guaranteed by all constructors here, but since the enum variants are public,
175
+ /// it could still be violated (even though no code outside this file should
176
+ /// construct `Scalar`s).
173
177
#[ inline( always) ]
174
178
fn check_data ( data : u128 , size : u8 ) {
175
179
debug_assert_eq ! (
@@ -364,10 +368,10 @@ impl<'tcx, Tag> Scalar<Tag> {
364
368
target_size : Size ,
365
369
cx : & impl HasDataLayout ,
366
370
) -> Result < u128 , Pointer < Tag > > {
371
+ assert_ne ! ( target_size. bytes( ) , 0 , "you should never look at the bits of a ZST" ) ;
367
372
match self {
368
373
Scalar :: Raw { data, size } => {
369
374
assert_eq ! ( target_size. bytes( ) , size as u64 ) ;
370
- assert_ne ! ( size, 0 , "you should never look at the bits of a ZST" ) ;
371
375
Scalar :: check_data ( data, size) ;
372
376
Ok ( data)
373
377
}
@@ -378,19 +382,15 @@ impl<'tcx, Tag> Scalar<Tag> {
378
382
}
379
383
}
380
384
381
- #[ inline( always) ]
382
- pub fn check_raw ( data : u128 , size : u8 , target_size : Size ) {
383
- assert_eq ! ( target_size. bytes( ) , size as u64 ) ;
384
- assert_ne ! ( size, 0 , "you should never look at the bits of a ZST" ) ;
385
- Scalar :: check_data ( data, size) ;
386
- }
387
-
388
- /// Do not call this method! Use either `assert_bits` or `force_bits`.
385
+ /// This method is intentionally private!
386
+ /// It is just a helper for other methods in this file.
389
387
#[ inline]
390
- pub fn to_bits ( self , target_size : Size ) -> InterpResult < ' tcx , u128 > {
388
+ fn to_bits ( self , target_size : Size ) -> InterpResult < ' tcx , u128 > {
389
+ assert_ne ! ( target_size. bytes( ) , 0 , "you should never look at the bits of a ZST" ) ;
391
390
match self {
392
391
Scalar :: Raw { data, size } => {
393
- Self :: check_raw ( data, size, target_size) ;
392
+ assert_eq ! ( target_size. bytes( ) , size as u64 ) ;
393
+ Scalar :: check_data ( data, size) ;
394
394
Ok ( data)
395
395
}
396
396
Scalar :: Ptr ( _) => throw_unsup ! ( ReadPointerAsBytes ) ,
@@ -402,22 +402,14 @@ impl<'tcx, Tag> Scalar<Tag> {
402
402
self . to_bits ( target_size) . expect ( "expected Raw bits but got a Pointer" )
403
403
}
404
404
405
- /// Do not call this method! Use either `assert_ptr` or `force_ptr`.
406
- /// This method is intentionally private, do not make it public.
407
405
#[ inline]
408
- fn to_ptr ( self ) -> InterpResult < ' tcx , Pointer < Tag > > {
406
+ pub fn assert_ptr ( self ) -> Pointer < Tag > {
409
407
match self {
410
- Scalar :: Raw { data : 0 , .. } => throw_unsup ! ( InvalidNullPointerUsage ) ,
411
- Scalar :: Raw { .. } => throw_unsup ! ( ReadBytesAsPointer ) ,
412
- Scalar :: Ptr ( p) => Ok ( p) ,
408
+ Scalar :: Ptr ( p) => p,
409
+ Scalar :: Raw { .. } => bug ! ( "expected a Pointer but got Raw bits" ) ,
413
410
}
414
411
}
415
412
416
- #[ inline( always) ]
417
- pub fn assert_ptr ( self ) -> Pointer < Tag > {
418
- self . to_ptr ( ) . expect ( "expected a Pointer but got Raw bits" )
419
- }
420
-
421
413
/// Do not call this method! Dispatch based on the type instead.
422
414
#[ inline]
423
415
pub fn is_bits ( self ) -> bool {
@@ -595,12 +587,6 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
595
587
}
596
588
}
597
589
598
- /// Do not call this method! Use either `assert_bits` or `force_bits`.
599
- #[ inline( always) ]
600
- pub fn to_bits ( self , target_size : Size ) -> InterpResult < ' tcx , u128 > {
601
- self . not_undef ( ) ?. to_bits ( target_size)
602
- }
603
-
604
590
#[ inline( always) ]
605
591
pub fn to_bool ( self ) -> InterpResult < ' tcx , bool > {
606
592
self . not_undef ( ) ?. to_bool ( )
0 commit comments