1
Fork 0

Clean up array/slice of primitive validation

This commit is contained in:
Oliver Scherer 2018-11-14 11:45:10 +01:00
parent 65b702c6b1
commit b820cc79a9
3 changed files with 26 additions and 13 deletions

View file

@ -21,7 +21,7 @@ use rustc::mir::interpret::{
};
use super::{
OpTy, MPlaceTy, Machine, EvalContext, ValueVisitor, Operand,
OpTy, Machine, EvalContext, ValueVisitor,
};
macro_rules! validation_failure {
@ -522,25 +522,22 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>>
_ => false,
}
} => {
let mplace = match *op {
// it's a ZST, the memory content cannot matter
Operand::Immediate(_) if op.layout.is_zst() =>
// invent an aligned mplace
MPlaceTy::dangling(op.layout, self.ecx),
// FIXME: what about single element arrays? They can be Scalar layout I think
Operand::Immediate(_) => bug!("non-ZST array/slice cannot be immediate"),
Operand::Indirect(_) => op.to_mem_place(),
};
if op.layout.is_zst() {
return Ok(());
}
// non-ZST array cannot be immediate, slices are never immediate
let mplace = op.to_mem_place();
// This is the length of the array/slice.
let len = mplace.len(self.ecx)?;
// zero length slices have nothing to be checked
if len == 0 {
return Ok(());
}
// This is the element type size.
let ty_size = self.ecx.layout_of(tys)?.size;
// This is the size in bytes of the whole array.
let size = ty_size * len;
if op.layout.is_zst() {
return self.ecx.memory.check_align(mplace.ptr, op.layout.align);
}
let ptr = mplace.ptr.to_ptr()?;
// NOTE: Keep this in sync with the handling of integer and float

View file

@ -0,0 +1,5 @@
#![feature(const_raw_ptr_deref, never_type)]
const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
fn main() {}

View file

@ -0,0 +1,11 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/validate_never_arrays.rs:3:1
|
LL | const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>[0]
|
= 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
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.