about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-01-15 21:51:50 +0900
committerGitHub <noreply@github.com>2020-01-15 21:51:50 +0900
commit610ea807107c5d958d5b6e19d08c377ebffb300a (patch)
tree0e2817675e2c2ba69d8311e34b781b58b74e81bc
parent20c49fc79755fbe12fac48485582ce8d9b367175 (diff)
parent0e14b9ff268b6987f935847d7857969a69b7ee7d (diff)
downloadrust-610ea807107c5d958d5b6e19d08c377ebffb300a.tar.gz
rust-610ea807107c5d958d5b6e19d08c377ebffb300a.zip
Rollup merge of #68219 - oli-obk:fix_miri, r=RalfJung,wesleywiser
Untangle ZST validation from integer validation and generalize it to all zsts

cc @RalfJung

r? @wesleywiser
-rw-r--r--src/librustc_mir/interpret/validity.rs18
-rw-r--r--src/test/ui/consts/huge-values.rs6
-rw-r--r--src/test/ui/consts/validate_never_arrays.rs6
-rw-r--r--src/test/ui/consts/validate_never_arrays.stderr22
4 files changed, 37 insertions, 15 deletions
diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs
index 2f0fb81fffd..6934ec0bdb6 100644
--- a/src/librustc_mir/interpret/validity.rs
+++ b/src/librustc_mir/interpret/validity.rs
@@ -587,12 +587,6 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
                     // padding.
                     match tys.kind {
                         ty::Int(..) | ty::Uint(..) | ty::Float(..) => true,
-                        ty::Tuple(tys) if tys.len() == 0 => true,
-                        ty::Adt(adt_def, _)
-                            if adt_def.is_struct() && adt_def.all_fields().next().is_none() =>
-                        {
-                            true
-                        }
                         _ => false,
                     }
                 } =>
@@ -609,11 +603,6 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
                 }
                 // This is the element type size.
                 let layout = self.ecx.layout_of(tys)?;
-                // Empty tuples and fieldless structs (the only ZSTs that allow reaching this code)
-                // have no data to be checked.
-                if layout.is_zst() {
-                    return Ok(());
-                }
                 // This is the size in bytes of the whole array.
                 let size = layout.size * len;
                 // Size is not 0, get a pointer.
@@ -656,6 +645,13 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
                     }
                 }
             }
+            // Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
+            // of an array and not all of them, because there's only a single value of a specific
+            // ZST type, so either validation fails for all elements or none.
+            ty::Array(tys, ..) | ty::Slice(tys) if self.ecx.layout_of(tys)?.is_zst() => {
+                // Validate just the first element
+                self.walk_aggregate(op, fields.take(1))?
+            }
             _ => {
                 self.walk_aggregate(op, fields)? // default handler
             }
diff --git a/src/test/ui/consts/huge-values.rs b/src/test/ui/consts/huge-values.rs
index ab09922d761..70a5b10e9be 100644
--- a/src/test/ui/consts/huge-values.rs
+++ b/src/test/ui/consts/huge-values.rs
@@ -1,6 +1,10 @@
 // build-pass
 // ignore-32bit
 
+// This test is a canary test that will essentially not compile in a reasonable time frame
+// (so it'll take hours) if any of the optimizations regress. With the optimizations, these compile
+// in milliseconds just as if the length were set to `1`.
+
 #[derive(Clone, Copy)]
 struct Foo;
 
@@ -8,4 +12,6 @@ fn main() {
     let _ = [(); 4_000_000_000];
     let _ = [0u8; 4_000_000_000];
     let _ = [Foo; 4_000_000_000];
+    let _ = [(Foo, (), Foo, ((), Foo, [0; 0])); 4_000_000_000];
+    let _ = [[0; 0]; 4_000_000_000];
 }
diff --git a/src/test/ui/consts/validate_never_arrays.rs b/src/test/ui/consts/validate_never_arrays.rs
index 9610b7b22f1..c7144f05ec7 100644
--- a/src/test/ui/consts/validate_never_arrays.rs
+++ b/src/test/ui/consts/validate_never_arrays.rs
@@ -1,5 +1,9 @@
 #![feature(const_raw_ptr_deref, never_type)]
 
-const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
+const _: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
+const _: &[!; 0] = unsafe { &*(1_usize as *const [!; 0]) }; // ok
+const _: &[!] = unsafe { &*(1_usize as *const [!; 0]) }; // ok
+const _: &[!] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
+const _: &[!] = unsafe { &*(1_usize as *const [!; 42]) }; //~ 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
index c4c7a337182..cb995b8216f 100644
--- a/src/test/ui/consts/validate_never_arrays.stderr
+++ b/src/test/ui/consts/validate_never_arrays.stderr
@@ -1,11 +1,27 @@
 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]) };
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>
+LL | const _: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>
    |
    = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
 
-error: aborting due to previous error
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/validate_never_arrays.rs:6:1
+   |
+LL | const _: &[!] = unsafe { &*(1_usize as *const [!; 1]) };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 rustc repository if you believe it should not be considered undefined behavior.
+
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/validate_never_arrays.rs:7:1
+   |
+LL | const _: &[!] = unsafe { &*(1_usize as *const [!; 42]) };
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 rustc repository if you believe it should not be considered undefined behavior.
+
+error: aborting due to 3 previous errors
 
 For more information about this error, try `rustc --explain E0080`.