@@ -170,6 +170,10 @@ impl<Tag> From<Double> for Scalar<Tag> {
170170}
171171
172172impl 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).
173177 #[ inline( always) ]
174178 fn check_data ( data : u128 , size : u8 ) {
175179 debug_assert_eq ! (
@@ -364,10 +368,10 @@ impl<'tcx, Tag> Scalar<Tag> {
364368 target_size : Size ,
365369 cx : & impl HasDataLayout ,
366370 ) -> Result < u128 , Pointer < Tag > > {
371+ assert_ne ! ( target_size. bytes( ) , 0 , "you should never look at the bits of a ZST" ) ;
367372 match self {
368373 Scalar :: Raw { data, size } => {
369374 assert_eq ! ( target_size. bytes( ) , size as u64 ) ;
370- assert_ne ! ( size, 0 , "you should never look at the bits of a ZST" ) ;
371375 Scalar :: check_data ( data, size) ;
372376 Ok ( data)
373377 }
@@ -378,19 +382,15 @@ impl<'tcx, Tag> Scalar<Tag> {
378382 }
379383 }
380384
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.
389387 #[ 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" ) ;
391390 match self {
392391 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) ;
394394 Ok ( data)
395395 }
396396 Scalar :: Ptr ( _) => throw_unsup ! ( ReadPointerAsBytes ) ,
@@ -402,22 +402,14 @@ impl<'tcx, Tag> Scalar<Tag> {
402402 self . to_bits ( target_size) . expect ( "expected Raw bits but got a Pointer" )
403403 }
404404
405- /// Do not call this method! Use either `assert_ptr` or `force_ptr`.
406- /// This method is intentionally private, do not make it public.
407405 #[ inline]
408- fn to_ptr ( self ) -> InterpResult < ' tcx , Pointer < Tag > > {
406+ pub fn assert_ptr ( self ) -> Pointer < Tag > {
409407 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" ) ,
413410 }
414411 }
415412
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-
421413 /// Do not call this method! Dispatch based on the type instead.
422414 #[ inline]
423415 pub fn is_bits ( self ) -> bool {
@@ -595,12 +587,6 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
595587 }
596588 }
597589
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-
604590 #[ inline( always) ]
605591 pub fn to_bool ( self ) -> InterpResult < ' tcx , bool > {
606592 self . not_undef ( ) ?. to_bool ( )
0 commit comments