summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/extra-const-ub
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
commit023939b627b7dc93b01471f7d41fb8553ddb4ffa (patch)
tree60fc59477c605c72b0a1051409062ddecc43f877 /tests/ui/consts/extra-const-ub
parentAdding debian version 1.72.1+dfsg1-1. (diff)
downloadrustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.tar.xz
rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/consts/extra-const-ub')
-rw-r--r--tests/ui/consts/extra-const-ub/detect-extra-ub.rs61
-rw-r--r--tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr38
2 files changed, 92 insertions, 7 deletions
diff --git a/tests/ui/consts/extra-const-ub/detect-extra-ub.rs b/tests/ui/consts/extra-const-ub/detect-extra-ub.rs
index 6a3c93ce7..37b37e965 100644
--- a/tests/ui/consts/extra-const-ub/detect-extra-ub.rs
+++ b/tests/ui/consts/extra-const-ub/detect-extra-ub.rs
@@ -1,8 +1,29 @@
// revisions: no_flag with_flag
// [no_flag] check-pass
// [with_flag] compile-flags: -Zextra-const-ub-checks
+#![feature(never_type, pointer_byte_offsets)]
use std::mem::transmute;
+use std::ptr::addr_of;
+
+#[derive(Clone, Copy)]
+enum E { A, B }
+
+#[derive(Clone, Copy)]
+enum Never {}
+
+#[repr(usize)]
+enum PtrSizedEnum { V }
+
+// An enum with uninhabited variants but also at least 2 inhabited variants -- so the uninhabited
+// variants *do* have a discriminant.
+#[derive(Clone, Copy)]
+enum UninhDiscriminant {
+ A,
+ B(!),
+ C,
+ D(Never),
+}
const INVALID_BOOL: () = unsafe {
let _x: bool = transmute(3u8);
@@ -13,12 +34,20 @@ const INVALID_BOOL: () = unsafe {
const INVALID_PTR_IN_INT: () = unsafe {
let _x: usize = transmute(&3u8);
//[with_flag]~^ ERROR: evaluation of constant value failed
+ //[with_flag]~| invalid value
+};
+
+const INVALID_PTR_IN_ENUM: () = unsafe {
+ let _x: PtrSizedEnum = transmute(&3u8);
+ //[with_flag]~^ ERROR: evaluation of constant value failed
+ //[with_flag]~| invalid value
};
const INVALID_SLICE_TO_USIZE_TRANSMUTE: () = unsafe {
let x: &[u8] = &[0; 32];
let _x: (usize, usize) = transmute(x);
//[with_flag]~^ ERROR: evaluation of constant value failed
+ //[with_flag]~| invalid value
};
const UNALIGNED_PTR: () = unsafe {
@@ -27,4 +56,36 @@ const UNALIGNED_PTR: () = unsafe {
//[with_flag]~| invalid value
};
+const UNINHABITED_VARIANT: () = unsafe {
+ let data = [1u8];
+ // Not using transmute, we want to hit the ImmTy code path.
+ let v = *addr_of!(data).cast::<UninhDiscriminant>();
+ //[with_flag]~^ ERROR: evaluation of constant value failed
+ //[with_flag]~| invalid value
+};
+
+const PARTIAL_POINTER: () = unsafe {
+ #[repr(C, packed)]
+ struct Packed {
+ pad1: u8,
+ ptr: *const u8,
+ pad2: [u8; 7],
+ }
+ // `Align` ensures that the entire thing has pointer alignment again.
+ #[repr(C)]
+ struct Align {
+ p: Packed,
+ align: usize,
+ }
+ let mem = Packed { pad1: 0, ptr: &0u8 as *const u8, pad2: [0; 7] };
+ let mem = Align { p: mem, align: 0 };
+ let _val = *(&mem as *const Align as *const [*const u8; 2]);
+ //[with_flag]~^ ERROR: evaluation of constant value failed
+ //[with_flag]~| invalid value
+};
+
+// Regression tests for an ICE (related to <https://github.com/rust-lang/rust/issues/113988>).
+const VALID_ENUM1: E = { let e = E::A; e };
+const VALID_ENUM2: Result<&'static [u8], ()> = { let e = Err(()); e };
+
fn main() {}
diff --git a/tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr b/tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr
index 3970baefc..4ee12d501 100644
--- a/tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr
+++ b/tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr
@@ -1,33 +1,57 @@
error[E0080]: evaluation of constant value failed
- --> $DIR/detect-extra-ub.rs:8:20
+ --> $DIR/detect-extra-ub.rs:29:20
|
LL | let _x: bool = transmute(3u8);
| ^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
error[E0080]: evaluation of constant value failed
- --> $DIR/detect-extra-ub.rs:14:21
+ --> $DIR/detect-extra-ub.rs:35:21
|
LL | let _x: usize = transmute(&3u8);
- | ^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
+ | ^^^^^^^^^^^^^^^ constructing invalid value: encountered a pointer, but expected an integer
|
= help: this code performed an operation that depends on the underlying bytes representing a pointer
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
error[E0080]: evaluation of constant value failed
- --> $DIR/detect-extra-ub.rs:20:30
+ --> $DIR/detect-extra-ub.rs:41:28
+ |
+LL | let _x: PtrSizedEnum = transmute(&3u8);
+ | ^^^^^^^^^^^^^^^ constructing invalid value at .<enum-tag>: encountered a pointer, but expected an integer
+ |
+ = help: this code performed an operation that depends on the underlying bytes representing a pointer
+ = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
+
+error[E0080]: evaluation of constant value failed
+ --> $DIR/detect-extra-ub.rs:48:30
|
LL | let _x: (usize, usize) = transmute(x);
- | ^^^^^^^^^^^^ unable to turn pointer into raw bytes
+ | ^^^^^^^^^^^^ constructing invalid value at .0: encountered a pointer, but expected an integer
|
= help: this code performed an operation that depends on the underlying bytes representing a pointer
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
error[E0080]: evaluation of constant value failed
- --> $DIR/detect-extra-ub.rs:25:20
+ --> $DIR/detect-extra-ub.rs:54:20
|
LL | let _x: &u32 = transmute(&[0u8; 4]);
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
-error: aborting due to 4 previous errors
+error[E0080]: evaluation of constant value failed
+ --> $DIR/detect-extra-ub.rs:62:13
+ |
+LL | let v = *addr_of!(data).cast::<UninhDiscriminant>();
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-tag>: encountered an uninhabited enum variant
+
+error[E0080]: evaluation of constant value failed
+ --> $DIR/detect-extra-ub.rs:82:16
+ |
+LL | let _val = *(&mem as *const Align as *const [*const u8; 2]);
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered a partial pointer or a mix of pointers
+ |
+ = help: this code performed an operation that depends on the underlying bytes representing a pointer
+ = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
+
+error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0080`.