]> git.lizzy.rs Git - rust.git/commitdiff
test fast path offset reporting
authorRalf Jung <post@ralfj.de>
Thu, 16 Apr 2020 11:21:23 +0000 (13:21 +0200)
committerRalf Jung <post@ralfj.de>
Thu, 16 Apr 2020 11:21:23 +0000 (13:21 +0200)
src/test/ui/consts/const-eval/ub-int-array.rs [new file with mode: 0644]
src/test/ui/consts/const-eval/ub-int-array.stderr [new file with mode: 0644]
src/test/ui/consts/const-eval/ub-ref.rs

diff --git a/src/test/ui/consts/const-eval/ub-int-array.rs b/src/test/ui/consts/const-eval/ub-int-array.rs
new file mode 100644 (file)
index 0000000..8907b0c
--- /dev/null
@@ -0,0 +1,65 @@
+#![feature(const_transmute)]
+#![allow(const_err)] // make sure we cannot allow away the errors tested here
+
+//! Test the "array of int" fast path in validity checking, and in particular whether it
+//! points at the right array element.
+
+use std::mem;
+
+#[repr(C)]
+union MaybeUninit<T: Copy> {
+    uninit: (),
+    init: T,
+}
+
+const UNINIT_INT_0: [u32; 3] = unsafe {
+//~^ ERROR it is undefined behavior to use this value
+//~| type validation failed: encountered uninitialized bytes at [0]
+    [
+        MaybeUninit { uninit: () }.init,
+        1,
+        2,
+    ]
+};
+const UNINIT_INT_1: [u32; 3] = unsafe {
+//~^ ERROR it is undefined behavior to use this value
+//~| type validation failed: encountered uninitialized bytes at [1]
+    mem::transmute(
+        [
+            0u8,
+            0u8,
+            0u8,
+            0u8,
+            1u8,
+            MaybeUninit { uninit: () }.init,
+            1u8,
+            1u8,
+            2u8,
+            2u8,
+            MaybeUninit { uninit: () }.init,
+            2u8,
+        ]
+    )
+};
+const UNINIT_INT_2: [u32; 3] = unsafe {
+//~^ ERROR it is undefined behavior to use this value
+//~| type validation failed: encountered uninitialized bytes at [2]
+    mem::transmute(
+        [
+            0u8,
+            0u8,
+            0u8,
+            0u8,
+            1u8,
+            1u8,
+            1u8,
+            1u8,
+            2u8,
+            2u8,
+            2u8,
+            MaybeUninit { uninit: () }.init,
+        ]
+    )
+};
+
+fn main() {}
diff --git a/src/test/ui/consts/const-eval/ub-int-array.stderr b/src/test/ui/consts/const-eval/ub-int-array.stderr
new file mode 100644 (file)
index 0000000..b4a3c63
--- /dev/null
@@ -0,0 +1,45 @@
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/ub-int-array.rs:15:1
+   |
+LL | / const UNINIT_INT_0: [u32; 3] = unsafe {
+LL | |
+LL | |
+LL | |     [
+...  |
+LL | |     ]
+LL | | };
+   | |__^ type validation failed: encountered uninitialized bytes at [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/ub-int-array.rs:24:1
+   |
+LL | / const UNINIT_INT_1: [u32; 3] = unsafe {
+LL | |
+LL | |
+LL | |     mem::transmute(
+...  |
+LL | |     )
+LL | | };
+   | |__^ type validation failed: encountered uninitialized bytes at [1]
+   |
+   = 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/ub-int-array.rs:44:1
+   |
+LL | / const UNINIT_INT_2: [u32; 3] = unsafe {
+LL | |
+LL | |
+LL | |     mem::transmute(
+...  |
+LL | |     )
+LL | | };
+   | |__^ type validation failed: encountered uninitialized bytes at [2]
+   |
+   = 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`.
index 562ec99111b69f56b13b4a6bc49bcc145aad5b7c..10f4c8c03330ebdaa9498752c70161c9499b5c83 100644 (file)
@@ -6,11 +6,11 @@
 
 const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
 //~^ ERROR it is undefined behavior to use this value
-//~^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
+//~| type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
 
 const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
 //~^ ERROR it is undefined behavior to use this value
-//~^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
+//~| type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
 
 const NULL: &u16 = unsafe { mem::transmute(0usize) };
 //~^ ERROR it is undefined behavior to use this value