@@ -8,21 +8,16 @@ use rustc_middle::ty::layout::{LayoutOf as _, ValidityRequirement};
88use rustc_middle:: ty:: GenericArgsRef ;
99use rustc_middle:: ty:: { Ty , TyCtxt } ;
1010use rustc_middle:: {
11- mir:: {
12- self ,
13- interpret:: {
14- Allocation , ConstAllocation , GlobalId , InterpResult , PointerArithmetic , Scalar ,
15- } ,
16- BinOp , ConstValue , NonDivergingIntrinsic ,
17- } ,
11+ mir:: { self , BinOp , ConstValue , NonDivergingIntrinsic } ,
1812 ty:: layout:: TyAndLayout ,
1913} ;
2014use rustc_span:: symbol:: { sym, Symbol } ;
2115use rustc_target:: abi:: Size ;
2216
2317use super :: {
24- memory:: MemoryKind , util:: ensure_monomorphic_enough, CheckInAllocMsg , ImmTy , InterpCx ,
25- MPlaceTy , Machine , OpTy , Pointer ,
18+ memory:: MemoryKind , util:: ensure_monomorphic_enough, Allocation , CheckInAllocMsg ,
19+ ConstAllocation , GlobalId , ImmTy , InterpCx , InterpResult , MPlaceTy , Machine , OpTy , Pointer ,
20+ PointerArithmetic , Scalar ,
2621} ;
2722
2823use crate :: fluent_generated as fluent;
@@ -249,22 +244,30 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
249244 match ( self . ptr_try_get_alloc_id ( a) , self . ptr_try_get_alloc_id ( b) ) {
250245 ( Err ( a) , Err ( b) ) => {
251246 // Neither pointer points to an allocation.
252- // If these are inequal or null, this *will* fail the deref check below.
247+ // This is okay only if they are the same.
248+ if a != b {
249+ // We'd catch this below in the "dereferenceable" check, but
250+ // show a nicer error for this particular case.
251+ throw_ub_custom ! (
252+ fluent:: const_eval_offset_from_different_integers,
253+ name = intrinsic_name,
254+ ) ;
255+ }
253256 ( a, b)
254257 }
255258 ( Err ( _) , _) | ( _, Err ( _) ) => {
256259 // We managed to find a valid allocation for one pointer, but not the other.
257260 // That means they are definitely not pointing to the same allocation.
258261 throw_ub_custom ! (
259- fluent:: const_eval_different_allocations ,
262+ fluent:: const_eval_offset_from_different_allocations ,
260263 name = intrinsic_name,
261264 ) ;
262265 }
263266 ( Ok ( ( a_alloc_id, a_offset, _) ) , Ok ( ( b_alloc_id, b_offset, _) ) ) => {
264267 // Found allocation for both. They must be into the same allocation.
265268 if a_alloc_id != b_alloc_id {
266269 throw_ub_custom ! (
267- fluent:: const_eval_different_allocations ,
270+ fluent:: const_eval_offset_from_different_allocations ,
268271 name = intrinsic_name,
269272 ) ;
270273 }
@@ -286,7 +289,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
286289 // a < b
287290 if intrinsic_name == sym:: ptr_offset_from_unsigned {
288291 throw_ub_custom ! (
289- fluent:: const_eval_unsigned_offset_from_overflow ,
292+ fluent:: const_eval_offset_from_unsigned_overflow ,
290293 a_offset = a_offset,
291294 b_offset = b_offset,
292295 ) ;
0 commit comments