@@ -18,7 +18,6 @@ use rustc_middle::mir::display_allocation;
1818use rustc_middle:: ty:: { self , Instance , ParamEnv , Ty , TyCtxt } ;
1919use rustc_target:: abi:: { Align , HasDataLayout , Size } ;
2020
21- use crate :: const_eval:: CheckAlignment ;
2221use crate :: fluent_generated as fluent;
2322
2423use super :: {
@@ -373,8 +372,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
373372 self . check_and_deref_ptr (
374373 ptr,
375374 size,
376- align,
377- M :: enforce_alignment ( self ) ,
375+ M :: enforce_alignment ( self ) . then_some ( align) ,
378376 CheckInAllocMsg :: MemoryAccessTest ,
379377 |alloc_id, offset, prov| {
380378 let ( size, align) = self
@@ -395,17 +393,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
395393 align : Align ,
396394 msg : CheckInAllocMsg ,
397395 ) -> InterpResult < ' tcx > {
398- self . check_and_deref_ptr (
399- ptr,
400- size,
401- align,
402- CheckAlignment :: Error ,
403- msg,
404- |alloc_id, _, _| {
405- let ( size, align) = self . get_live_alloc_size_and_align ( alloc_id, msg) ?;
406- Ok ( ( size, align, ( ) ) )
407- } ,
408- ) ?;
396+ self . check_and_deref_ptr ( ptr, size, Some ( align) , msg, |alloc_id, _, _| {
397+ let ( size, align) = self . get_live_alloc_size_and_align ( alloc_id, msg) ?;
398+ Ok ( ( size, align, ( ) ) )
399+ } ) ?;
409400 Ok ( ( ) )
410401 }
411402
@@ -419,8 +410,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
419410 & self ,
420411 ptr : Pointer < Option < M :: Provenance > > ,
421412 size : Size ,
422- align : Align ,
423- check : CheckAlignment ,
413+ align : Option < Align > ,
424414 msg : CheckInAllocMsg ,
425415 alloc_size : impl FnOnce (
426416 AllocId ,
@@ -436,8 +426,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
436426 throw_ub ! ( DanglingIntPointer ( addr, msg) ) ;
437427 }
438428 // Must be aligned.
439- if check . should_check ( ) {
440- self . check_offset_align ( addr, align, check ) ?;
429+ if let Some ( align ) = align {
430+ self . check_offset_align ( addr, align) ?;
441431 }
442432 None
443433 }
@@ -460,16 +450,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
460450 }
461451 // Test align. Check this last; if both bounds and alignment are violated
462452 // we want the error to be about the bounds.
463- if check . should_check ( ) {
453+ if let Some ( align ) = align {
464454 if M :: use_addr_for_alignment_check ( self ) {
465455 // `use_addr_for_alignment_check` can only be true if `OFFSET_IS_ADDR` is true.
466- self . check_offset_align ( ptr. addr ( ) . bytes ( ) , align, check ) ?;
456+ self . check_offset_align ( ptr. addr ( ) . bytes ( ) , align) ?;
467457 } else {
468458 // Check allocation alignment and offset alignment.
469459 if alloc_align. bytes ( ) < align. bytes ( ) {
470- M :: alignment_check_failed ( self , alloc_align, align, check ) ? ;
460+ throw_ub ! ( AlignmentCheckFailed { has : alloc_align, required : align } ) ;
471461 }
472- self . check_offset_align ( offset. bytes ( ) , align, check ) ?;
462+ self . check_offset_align ( offset. bytes ( ) , align) ?;
473463 }
474464 }
475465
@@ -480,18 +470,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
480470 } )
481471 }
482472
483- fn check_offset_align (
484- & self ,
485- offset : u64 ,
486- align : Align ,
487- check : CheckAlignment ,
488- ) -> InterpResult < ' tcx > {
473+ fn check_offset_align ( & self , offset : u64 , align : Align ) -> InterpResult < ' tcx > {
489474 if offset % align. bytes ( ) == 0 {
490475 Ok ( ( ) )
491476 } else {
492477 // The biggest power of two through which `offset` is divisible.
493478 let offset_pow2 = 1 << offset. trailing_zeros ( ) ;
494- M :: alignment_check_failed ( self , Align :: from_bytes ( offset_pow2) . unwrap ( ) , align, check)
479+ throw_ub ! ( AlignmentCheckFailed {
480+ has: Align :: from_bytes( offset_pow2) . unwrap( ) ,
481+ required: align
482+ } ) ;
495483 }
496484 }
497485}
@@ -609,8 +597,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
609597 let ptr_and_alloc = self . check_and_deref_ptr (
610598 ptr,
611599 size,
612- align,
613- M :: enforce_alignment ( self ) ,
600+ M :: enforce_alignment ( self ) . then_some ( align) ,
614601 CheckInAllocMsg :: MemoryAccessTest ,
615602 |alloc_id, offset, prov| {
616603 let alloc = self . get_alloc_raw ( alloc_id) ?;
0 commit comments