33//! Currently, this pass only propagates scalar values.
44
55use rustc_const_eval:: interpret:: {
6- ImmTy , Immediate , InterpCx , OpTy , PlaceTy , Pointer , PointerArithmetic , Projectable ,
6+ ImmTy , Immediate , InterpCx , OpTy , PlaceTy , PointerArithmetic , Projectable ,
77} ;
88use rustc_data_structures:: fx:: FxHashMap ;
99use rustc_hir:: def:: DefKind ;
@@ -946,10 +946,12 @@ impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for Dumm
946946 use rustc_middle:: mir:: BinOp :: * ;
947947 Ok ( match bin_op {
948948 Eq | Ne | Lt | Le | Gt | Ge => {
949- assert_eq ! ( left. layout. abi, right. layout. abi) ; // types an differ, e.g. fn ptrs with different `for`
949+ // Types can differ, e.g. fn ptrs with different `for`.
950+ assert_eq ! ( left. layout. abi, right. layout. abi) ;
950951 let size = ecx. pointer_size ( ) ;
951952 // Just compare the bits. ScalarPairs are compared lexicographically.
952953 // We thus always compare pairs and simply fill scalars up with 0.
954+ // If the pointer has provenance, `to_bits` will return `Err` and we bail out.
953955 let left = match * * left {
954956 Immediate :: Scalar ( l) => ( l. to_bits ( size) ?, 0 ) ,
955957 Immediate :: ScalarPair ( l1, l2) => ( l1. to_bits ( size) ?, l2. to_bits ( size) ?) ,
@@ -975,23 +977,7 @@ impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for Dumm
975977 // Some more operations are possible with atomics.
976978 // The return value always has the provenance of the *left* operand.
977979 Add | Sub | BitOr | BitAnd | BitXor => {
978- assert ! ( left. layout. ty. is_unsafe_ptr( ) ) ;
979- assert ! ( right. layout. ty. is_unsafe_ptr( ) ) ;
980- let ptr = left. to_scalar ( ) . to_pointer ( ecx) ?;
981- // We do the actual operation with usize-typed scalars.
982- let usize_layout = ecx. layout_of ( ecx. tcx . types . usize ) . unwrap ( ) ;
983- let left = ImmTy :: from_uint ( ptr. addr ( ) . bytes ( ) , usize_layout) ;
984- let right = ImmTy :: from_uint ( right. to_scalar ( ) . to_target_usize ( ecx) ?, usize_layout) ;
985- let ( result, overflowing) = ecx. overflowing_binary_op ( bin_op, & left, & right) ?;
986- // Construct a new pointer with the provenance of `ptr` (the LHS).
987- let result_ptr = Pointer :: new (
988- ptr. provenance ,
989- Size :: from_bytes ( result. to_scalar ( ) . to_target_usize ( ecx) ?) ,
990- ) ;
991- (
992- ImmTy :: from_scalar ( Scalar :: from_maybe_pointer ( result_ptr, ecx) , left. layout ) ,
993- overflowing,
994- )
980+ throw_machine_stop_str ! ( "pointer arithmetic is not handled" )
995981 }
996982
997983 _ => span_bug ! ( ecx. cur_span( ) , "Invalid operator on pointers: {:?}" , bin_op) ,
0 commit comments