Skip to content

Commit b820cc7

Browse files
committed
Clean up array/slice of primitive validation
1 parent 65b702c commit b820cc7

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc::mir::interpret::{
2121
};
2222

2323
use super::{
24-
OpTy, MPlaceTy, Machine, EvalContext, ValueVisitor, Operand,
24+
OpTy, Machine, EvalContext, ValueVisitor,
2525
};
2626

2727
macro_rules! validation_failure {
@@ -522,25 +522,22 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
522522
_ => false,
523523
}
524524
} => {
525-
let mplace = match *op {
526-
// it's a ZST, the memory content cannot matter
527-
Operand::Immediate(_) if op.layout.is_zst() =>
528-
// invent an aligned mplace
529-
MPlaceTy::dangling(op.layout, self.ecx),
530-
// FIXME: what about single element arrays? They can be Scalar layout I think
531-
Operand::Immediate(_) => bug!("non-ZST array/slice cannot be immediate"),
532-
Operand::Indirect(_) => op.to_mem_place(),
533-
};
525+
if op.layout.is_zst() {
526+
return Ok(());
527+
}
528+
// non-ZST array cannot be immediate, slices are never immediate
529+
let mplace = op.to_mem_place();
534530
// This is the length of the array/slice.
535531
let len = mplace.len(self.ecx)?;
532+
// zero length slices have nothing to be checked
533+
if len == 0 {
534+
return Ok(());
535+
}
536536
// This is the element type size.
537537
let ty_size = self.ecx.layout_of(tys)?.size;
538538
// This is the size in bytes of the whole array.
539539
let size = ty_size * len;
540540

541-
if op.layout.is_zst() {
542-
return self.ecx.memory.check_align(mplace.ptr, op.layout.align);
543-
}
544541
let ptr = mplace.ptr.to_ptr()?;
545542

546543
// NOTE: Keep this in sync with the handling of integer and float
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(const_raw_ptr_deref, never_type)]
2+
3+
const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
4+
5+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/validate_never_arrays.rs:3:1
3+
|
4+
LL | const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>[0]
6+
|
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)