about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJakob Degen <jakob.e.degen@gmail.com>2022-04-05 19:12:09 -0400
committerJakob Degen <jakob.e.degen@gmail.com>2022-04-11 09:26:26 -0400
commitf7ca97a209411ccd34f8e536b47d1027236121d3 (patch)
tree494fa1210e613bf5e92eaee23611dd071dbbe956
parent2a040284a5bc3bee1e78a7bea60e15a0033ef2b5 (diff)
downloadrust-f7ca97a209411ccd34f8e536b47d1027236121d3.tar.gz
rust-f7ca97a209411ccd34f8e536b47d1027236121d3.zip
Add const eval tests ensuring padding gets correctly marked as deinit on deaggregation
-rw-r--r--src/test/ui/consts/const-eval/ub-enum-overwrite.rs17
-rw-r--r--src/test/ui/consts/const-eval/ub-enum-overwrite.stderr20
2 files changed, 37 insertions, 0 deletions
diff --git a/src/test/ui/consts/const-eval/ub-enum-overwrite.rs b/src/test/ui/consts/const-eval/ub-enum-overwrite.rs
new file mode 100644
index 00000000000..c5677849229
--- /dev/null
+++ b/src/test/ui/consts/const-eval/ub-enum-overwrite.rs
@@ -0,0 +1,17 @@
+#![feature(const_mut_refs)]
+
+enum E {
+    A(u8),
+    B,
+}
+
+const _: u8 = {
+    //~^ ERROR is undefined behavior
+    let mut e = E::A(1);
+    let p = if let E::A(x) = &mut e { x as *mut u8 } else { unreachable!() };
+    // Make sure overwriting `e` uninitializes other bytes
+    e = E::B;
+    unsafe { *p }
+};
+
+fn main() {}
diff --git a/src/test/ui/consts/const-eval/ub-enum-overwrite.stderr b/src/test/ui/consts/const-eval/ub-enum-overwrite.stderr
new file mode 100644
index 00000000000..88de7acb496
--- /dev/null
+++ b/src/test/ui/consts/const-eval/ub-enum-overwrite.stderr
@@ -0,0 +1,20 @@
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/ub-enum-overwrite.rs:8:1
+   |
+LL | / const _: u8 = {
+LL | |
+LL | |     let mut e = E::A(1);
+LL | |     let p = if let E::A(x) = &mut e { x as *mut u8 } else { unreachable!() };
+...  |
+LL | |     unsafe { *p }
+LL | | };
+   | |__^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
+   |
+   = 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.
+   = note: the raw bytes of the constant (size: 1, align: 1) {
+               __                                              │ ░
+           }
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0080`.