]> git.lizzy.rs Git - rust.git/commitdiff
Clean up array/slice of primitive validation
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 14 Nov 2018 10:45:10 +0000 (11:45 +0100)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Sat, 24 Nov 2018 10:36:32 +0000 (11:36 +0100)
src/librustc_mir/interpret/validity.rs
src/test/ui/consts/validate_never_arrays.rs [new file with mode: 0644]
src/test/ui/consts/validate_never_arrays.stderr [new file with mode: 0644]

index b3a82cd70232aa35db9b3ef1068980fbbdf48b8d..4b9ded4c17ee91b708aeca4d0d3603754cbd983f 100644 (file)
@@ -21,7 +21,7 @@
 };
 
 use super::{
-    OpTy, MPlaceTy, Machine, EvalContext, ValueVisitor, Operand,
+    OpTy, Machine, EvalContext, ValueVisitor,
 };
 
 macro_rules! validation_failure {
@@ -522,25 +522,22 @@ fn visit_aggregate(
                     _ => 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
diff --git a/src/test/ui/consts/validate_never_arrays.rs b/src/test/ui/consts/validate_never_arrays.rs
new file mode 100644 (file)
index 0000000..9610b7b
--- /dev/null
@@ -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() {}
diff --git a/src/test/ui/consts/validate_never_arrays.stderr b/src/test/ui/consts/validate_never_arrays.stderr
new file mode 100644 (file)
index 0000000..0b63924
--- /dev/null
@@ -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`.