From fa3976fce0def16fa9bfec4722c59df8e8078803 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 16 Jul 2025 14:54:40 +0000 Subject: [PATCH 1/2] Add regression test --- .../consts/promotion-in-const-validation.rs | 9 ++++++ .../promotion-in-const-validation.stderr | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/ui/consts/promotion-in-const-validation.rs create mode 100644 tests/ui/consts/promotion-in-const-validation.stderr diff --git a/tests/ui/consts/promotion-in-const-validation.rs b/tests/ui/consts/promotion-in-const-validation.rs new file mode 100644 index 0000000000000..24e44bcc9aa59 --- /dev/null +++ b/tests/ui/consts/promotion-in-const-validation.rs @@ -0,0 +1,9 @@ +fn foo() {} + +const _: &usize = unsafe { &std::mem::transmute(foo as fn()) }; +//~^ ERROR: constructing invalid value at .: encountered a pointer, but expected an integer + +const _: usize = unsafe { std::mem::transmute(foo as fn()) }; +//~^ ERROR: unable to turn pointer into integer + +fn main() {} diff --git a/tests/ui/consts/promotion-in-const-validation.stderr b/tests/ui/consts/promotion-in-const-validation.stderr new file mode 100644 index 0000000000000..5b5da2a37183d --- /dev/null +++ b/tests/ui/consts/promotion-in-const-validation.stderr @@ -0,0 +1,31 @@ +error[E0080]: constructing invalid value at .: encountered a pointer, but expected an integer + --> $DIR/promotion-in-const-validation.rs:3:1 + | +LL | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) }; + | ^^^^^^^^^^^^^^^ it is undefined behavior to use this value + | + = 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 + = 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: 8, align: 8) { + ╾ALLOC0╼ │ ╾──────╼ + } + +note: erroneous constant encountered + --> $DIR/promotion-in-const-validation.rs:3:28 + | +LL | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0080]: unable to turn pointer into integer + --> $DIR/promotion-in-const-validation.rs:6:1 + | +LL | const _: usize = unsafe { std::mem::transmute(foo as fn()) }; + | ^^^^^^^^^^^^^^ evaluation of `_` failed here + | + = 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 2 previous errors + +For more information about this error, try `rustc --explain E0080`. From 1aa2951c3e2fb8049bc0e60786d41a2da9658664 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 16 Jul 2025 15:56:56 +0000 Subject: [PATCH 2/2] Validate transmutes in CTFE --- .../src/const_eval/eval_queries.rs | 2 +- .../rustc_const_eval/src/interpret/cast.rs | 9 + .../consts/const-eval/raw-bytes.32bit.stderr | 312 +++++------------- .../consts/const-eval/raw-bytes.64bit.stderr | 312 +++++------------- tests/ui/consts/const-eval/raw-bytes.rs | 2 +- .../consts/const-eval/transmute-const.stderr | 9 +- tests/ui/consts/const-eval/ub-enum.rs | 10 +- tests/ui/consts/const-eval/ub-enum.stderr | 77 ++--- .../ub-incorrect-vtable.32bit.stderr | 65 ++-- .../ub-incorrect-vtable.64bit.stderr | 65 ++-- .../consts/const-eval/ub-incorrect-vtable.rs | 10 +- tests/ui/consts/const-eval/ub-int-array.rs | 6 +- .../ui/consts/const-eval/ub-int-array.stderr | 50 +-- tests/ui/consts/const-eval/ub-nonnull.rs | 5 +- tests/ui/consts/const-eval/ub-nonnull.stderr | 52 +-- tests/ui/consts/const-eval/ub-ref-ptr.rs | 6 +- tests/ui/consts/const-eval/ub-ref-ptr.stderr | 85 ++--- .../ui/consts/const-eval/ub-uninhabit.stderr | 9 +- .../consts/const-eval/ub-upvars.32bit.stderr | 13 +- .../consts/const-eval/ub-upvars.64bit.stderr | 13 +- tests/ui/consts/const-eval/ub-upvars.rs | 9 +- tests/ui/consts/const-eval/ub-wide-ptr.rs | 16 +- tests/ui/consts/const-eval/ub-wide-ptr.stderr | 248 +++++--------- .../detect-extra-ub.no_flag.stderr | 42 +++ .../consts/extra-const-ub/detect-extra-ub.rs | 11 +- .../detect-extra-ub.with_flag.stderr | 16 +- tests/ui/consts/miri_unleashed/ptr_arith.rs | 2 +- .../ui/consts/miri_unleashed/ptr_arith.stderr | 8 +- .../consts/promotion-in-const-validation.rs | 4 +- .../promotion-in-const-validation.stderr | 16 +- tests/ui/type/pattern_types/validity.rs | 10 +- tests/ui/type/pattern_types/validity.stderr | 71 ++-- 32 files changed, 511 insertions(+), 1054 deletions(-) create mode 100644 tests/ui/consts/extra-const-ub/detect-extra-ub.no_flag.stderr diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index 4bd4b49300900..32209ff436791 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -93,7 +93,7 @@ fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>( // Since evaluation had no errors, validate the resulting constant. const_validate_mplace(ecx, &ret, cid)?; - // Only report this after validation, as validaiton produces much better diagnostics. + // Only report this after validation, as validation produces much better diagnostics. // FIXME: ensure validation always reports this and stop making interning care about it. match intern_result { diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index 7a73d70fc85dc..e83cb906589a0 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -141,6 +141,15 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { } self.copy_op_allow_transmute(src, dest)?; + + // Even if general validation is disabled, transmutes should always check their result. + if !M::enforce_validity(self, dest.layout) { + self.validate_operand( + &dest, + M::enforce_validity_recursively(self, dest.layout), + /*reset_provenance_and_padding*/ true, + )?; + } } } interp_ok(()) diff --git a/tests/ui/consts/const-eval/raw-bytes.32bit.stderr b/tests/ui/consts/const-eval/raw-bytes.32bit.stderr index 36183e2892101..ad06fd06fdd7b 100644 --- a/tests/ui/consts/const-eval/raw-bytes.32bit.stderr +++ b/tests/ui/consts/const-eval/raw-bytes.32bit.stderr @@ -1,90 +1,50 @@ error[E0080]: constructing invalid value at .: encountered 0x00000001, but expected a valid enum tag - --> $DIR/raw-bytes.rs:23:1 + --> $DIR/raw-bytes.rs:23:33 | LL | const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) }; - | ^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 01 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM` failed here error[E0080]: constructing invalid value at .: encountered 0x00000000, but expected a valid enum tag - --> $DIR/raw-bytes.rs:31:1 + --> $DIR/raw-bytes.rs:31:35 | LL | const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 00 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/raw-bytes.rs:45:1 + --> $DIR/raw-bytes.rs:45:62 | LL | const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 01 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINHABITED_VARIANT1` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/raw-bytes.rs:47:1 + --> $DIR/raw-bytes.rs:47:62 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 03 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINHABITED_VARIANT2` failed here -error[E0080]: constructing invalid value at ..0.1: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) - --> $DIR/raw-bytes.rs:53:1 +error[E0080]: constructing invalid value: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) + --> $DIR/raw-bytes.rs:53:67 | LL | const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) })); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - 78 00 00 00 ff ff ff ff │ x....... - } + | ^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_OPTION_CHAR` failed here error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:58:1 + --> $DIR/raw-bytes.rs:58:40 | LL | const NULL_PTR: NonNull = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 00 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_PTR` failed here error[E0080]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:61:1 + --> $DIR/raw-bytes.rs:61:39 | LL | const NULL_U8: NonZero = unsafe { mem::transmute(0u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 00 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_U8` failed here error[E0080]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:63:1 + --> $DIR/raw-bytes.rs:63:45 | LL | const NULL_USIZE: NonZero = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 00 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_USIZE` failed here error[E0080]: constructing invalid value: encountered 42, but expected something in the range 10..=30 --> $DIR/raw-bytes.rs:69:1 @@ -109,92 +69,52 @@ LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; } error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:78:1 + --> $DIR/raw-bytes.rs:81:5 | -LL | const NULL_FAT_PTR: NonNull = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - 00 00 00 00 ╾ALLOC_ID╼ │ ....╾──╼ - } +LL | mem::transmute((0_usize, meta)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_FAT_PTR` failed here error[E0080]: constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1) - --> $DIR/raw-bytes.rs:85:1 + --> $DIR/raw-bytes.rs:85:34 | LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) }; - | ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - ╾ALLOC_ID╼ │ ╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED` failed here error[E0080]: constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1) - --> $DIR/raw-bytes.rs:88:1 + --> $DIR/raw-bytes.rs:88:42 | LL | const UNALIGNED_BOX: Box = unsafe { mem::transmute(&[0u8; 4]) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - ╾ALLOC_ID╼ │ ╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED_BOX` failed here error[E0080]: constructing invalid value: encountered a null reference - --> $DIR/raw-bytes.rs:91:1 + --> $DIR/raw-bytes.rs:91:29 | LL | const NULL: &u16 = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 00 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL` failed here error[E0080]: constructing invalid value: encountered a null box - --> $DIR/raw-bytes.rs:94:1 + --> $DIR/raw-bytes.rs:94:37 | LL | const NULL_BOX: Box = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 00 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_BOX` failed here error[E0080]: constructing invalid value: encountered a dangling reference (0x539[noalloc] has no provenance) - --> $DIR/raw-bytes.rs:97:1 + --> $DIR/raw-bytes.rs:97:44 | LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 39 05 00 00 │ 9... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `USIZE_AS_REF` failed here error[E0080]: constructing invalid value: encountered a dangling box (0x539[noalloc] has no provenance) - --> $DIR/raw-bytes.rs:100:1 + --> $DIR/raw-bytes.rs:100:40 | LL | const USIZE_AS_BOX: Box = unsafe { mem::transmute(1337usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 39 05 00 00 │ 9... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `USIZE_AS_BOX` failed here -error[E0080]: constructing invalid value: encountered null pointer, but expected a function pointer - --> $DIR/raw-bytes.rs:103:1 +error[E0080]: constructing invalid value: encountered a null function pointer + --> $DIR/raw-bytes.rs:103:36 | LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 00 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_FN_PTR` failed here error[E0080]: constructing invalid value: encountered 0xd[noalloc], but expected a function pointer --> $DIR/raw-bytes.rs:105:1 @@ -207,7 +127,7 @@ LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) }; 0d 00 00 00 │ .... } -error[E0080]: constructing invalid value: encountered ALLOC3, but expected a function pointer +error[E0080]: constructing invalid value: encountered ALLOC0, but expected a function pointer --> $DIR/raw-bytes.rs:107:1 | LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; @@ -219,48 +139,28 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; } error[E0080]: constructing invalid value: encountered a reference pointing to uninhabited type Bar - --> $DIR/raw-bytes.rs:113:1 + --> $DIR/raw-bytes.rs:113:36 | LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - 01 00 00 00 │ .... - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_BAD_REF` failed here error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) - --> $DIR/raw-bytes.rs:137:1 + --> $DIR/raw-bytes.rs:137:37 | LL | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ e7 03 00 00 │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `STR_TOO_LONG` failed here -error[E0080]: constructing invalid value at .0: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/raw-bytes.rs:139:1 +error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object + --> $DIR/raw-bytes.rs:139:53 | LL | const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ff ff ff ff │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NESTED_STR_MUCH_TOO_LONG` failed here error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/raw-bytes.rs:141:1 + --> $DIR/raw-bytes.rs:141:47 | LL | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ff ff ff ff │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `MY_STR_MUCH_TOO_LONG` failed here error[E0080]: constructing invalid value at .: encountered uninitialized memory, but expected a string --> $DIR/raw-bytes.rs:144:1 @@ -298,48 +198,28 @@ LL | const MYSTR_NO_INIT_ISSUE83182: &MyStr = unsafe { mem::transmute::<&[_], _> } error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) - --> $DIR/raw-bytes.rs:152:1 + --> $DIR/raw-bytes.rs:152:40 | LL | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ e7 03 00 00 │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG` failed here error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/raw-bytes.rs:154:1 + --> $DIR/raw-bytes.rs:154:50 | LL | const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ff ff ff 7f │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG_OVERFLOW` failed here error[E0080]: constructing invalid value: encountered a dangling box (going beyond the bounds of its allocation) - --> $DIR/raw-bytes.rs:157:1 + --> $DIR/raw-bytes.rs:157:48 | LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ e7 03 00 00 │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG_BOX` failed here -error[E0080]: constructing invalid value at .[0]: encountered 0x03, but expected a boolean - --> $DIR/raw-bytes.rs:160:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/raw-bytes.rs:160:51 | LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - ╾ALLOC_ID╼ │ ╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_CONTENT_INVALID` failed here note: erroneous constant encountered --> $DIR/raw-bytes.rs:160:40 @@ -347,16 +227,11 @@ note: erroneous constant encountered LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at ..0: encountered 0x03, but expected a boolean - --> $DIR/raw-bytes.rs:164:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/raw-bytes.rs:164:60 | LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - ╾ALLOC_ID╼ │ ╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `MYSLICE_PREFIX_BAD` failed here note: erroneous constant encountered --> $DIR/raw-bytes.rs:164:42 @@ -364,16 +239,11 @@ note: erroneous constant encountered LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at ..1[0]: encountered 0x03, but expected a boolean - --> $DIR/raw-bytes.rs:167:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/raw-bytes.rs:167:67 | LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - ╾ALLOC_ID╼ │ ╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `MYSLICE_SUFFIX_BAD` failed here note: erroneous constant encountered --> $DIR/raw-bytes.rs:167:42 @@ -381,49 +251,29 @@ note: erroneous constant encountered LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at .0: encountered ALLOC17, but expected a vtable pointer - --> $DIR/raw-bytes.rs:171:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC13, but expected a vtable pointer + --> $DIR/raw-bytes.rs:171:58 | LL | const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──╼╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_SHORT_VTABLE_1` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC19, but expected a vtable pointer - --> $DIR/raw-bytes.rs:174:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC14, but expected a vtable pointer + --> $DIR/raw-bytes.rs:174:58 | LL | const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──╼╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_SHORT_VTABLE_2` failed here error[E0080]: constructing invalid value at .0: encountered 0x4[noalloc], but expected a vtable pointer - --> $DIR/raw-bytes.rs:177:1 + --> $DIR/raw-bytes.rs:177:54 | LL | const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ 04 00 00 00 │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_INT_VTABLE` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC22, but expected a vtable pointer - --> $DIR/raw-bytes.rs:179:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC15, but expected a vtable pointer + --> $DIR/raw-bytes.rs:179:66 | LL | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──╼╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR` failed here error[E0080]: constructing invalid value at ..: encountered 0x03, but expected a boolean --> $DIR/raw-bytes.rs:182:1 @@ -437,26 +287,16 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, } error[E0080]: constructing invalid value: encountered null pointer, but expected a vtable pointer - --> $DIR/raw-bytes.rs:185:1 + --> $DIR/raw-bytes.rs:185:62 | LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ 00 00 00 00 │ ╾──╼.... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_NULL` failed here -error[E0080]: constructing invalid value: encountered ALLOC27, but expected a vtable pointer - --> $DIR/raw-bytes.rs:187:1 +error[E0080]: constructing invalid value: encountered ALLOC16, but expected a vtable pointer + --> $DIR/raw-bytes.rs:187:65 | LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──╼╾──╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_INVALID` failed here error[E0080]: constructing invalid value: encountered a reference pointing to uninhabited type [!; 1] --> $DIR/raw-bytes.rs:191:1 diff --git a/tests/ui/consts/const-eval/raw-bytes.64bit.stderr b/tests/ui/consts/const-eval/raw-bytes.64bit.stderr index c53326534fdc5..d533de5029a9c 100644 --- a/tests/ui/consts/const-eval/raw-bytes.64bit.stderr +++ b/tests/ui/consts/const-eval/raw-bytes.64bit.stderr @@ -1,90 +1,50 @@ error[E0080]: constructing invalid value at .: encountered 0x0000000000000001, but expected a valid enum tag - --> $DIR/raw-bytes.rs:23:1 + --> $DIR/raw-bytes.rs:23:33 | LL | const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) }; - | ^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 01 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM` failed here error[E0080]: constructing invalid value at .: encountered 0x0000000000000000, but expected a valid enum tag - --> $DIR/raw-bytes.rs:31:1 + --> $DIR/raw-bytes.rs:31:35 | LL | const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 00 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/raw-bytes.rs:45:1 + --> $DIR/raw-bytes.rs:45:62 | LL | const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 01 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINHABITED_VARIANT1` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/raw-bytes.rs:47:1 + --> $DIR/raw-bytes.rs:47:62 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 03 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINHABITED_VARIANT2` failed here -error[E0080]: constructing invalid value at ..0.1: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) - --> $DIR/raw-bytes.rs:53:1 +error[E0080]: constructing invalid value: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) + --> $DIR/raw-bytes.rs:53:67 | LL | const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) })); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - 78 00 00 00 ff ff ff ff │ x....... - } + | ^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_OPTION_CHAR` failed here error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:58:1 + --> $DIR/raw-bytes.rs:58:40 | LL | const NULL_PTR: NonNull = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 00 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_PTR` failed here error[E0080]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:61:1 + --> $DIR/raw-bytes.rs:61:39 | LL | const NULL_U8: NonZero = unsafe { mem::transmute(0u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 00 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_U8` failed here error[E0080]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:63:1 + --> $DIR/raw-bytes.rs:63:45 | LL | const NULL_USIZE: NonZero = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 00 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_USIZE` failed here error[E0080]: constructing invalid value: encountered 42, but expected something in the range 10..=30 --> $DIR/raw-bytes.rs:69:1 @@ -109,92 +69,52 @@ LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; } error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/raw-bytes.rs:78:1 - | -LL | const NULL_FAT_PTR: NonNull = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value + --> $DIR/raw-bytes.rs:81:5 | - = 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: 16, align: 8) { - 00 00 00 00 00 00 00 00 ╾ALLOC_ID╼ │ ........╾──────╼ - } +LL | mem::transmute((0_usize, meta)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_FAT_PTR` failed here error[E0080]: constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1) - --> $DIR/raw-bytes.rs:85:1 + --> $DIR/raw-bytes.rs:85:34 | LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) }; - | ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - ╾ALLOC_ID╼ │ ╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED` failed here error[E0080]: constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1) - --> $DIR/raw-bytes.rs:88:1 + --> $DIR/raw-bytes.rs:88:42 | LL | const UNALIGNED_BOX: Box = unsafe { mem::transmute(&[0u8; 4]) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - ╾ALLOC_ID╼ │ ╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED_BOX` failed here error[E0080]: constructing invalid value: encountered a null reference - --> $DIR/raw-bytes.rs:91:1 + --> $DIR/raw-bytes.rs:91:29 | LL | const NULL: &u16 = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 00 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL` failed here error[E0080]: constructing invalid value: encountered a null box - --> $DIR/raw-bytes.rs:94:1 + --> $DIR/raw-bytes.rs:94:37 | LL | const NULL_BOX: Box = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 00 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_BOX` failed here error[E0080]: constructing invalid value: encountered a dangling reference (0x539[noalloc] has no provenance) - --> $DIR/raw-bytes.rs:97:1 + --> $DIR/raw-bytes.rs:97:44 | LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 39 05 00 00 00 00 00 00 │ 9....... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `USIZE_AS_REF` failed here error[E0080]: constructing invalid value: encountered a dangling box (0x539[noalloc] has no provenance) - --> $DIR/raw-bytes.rs:100:1 + --> $DIR/raw-bytes.rs:100:40 | LL | const USIZE_AS_BOX: Box = unsafe { mem::transmute(1337usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 39 05 00 00 00 00 00 00 │ 9....... - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `USIZE_AS_BOX` failed here -error[E0080]: constructing invalid value: encountered null pointer, but expected a function pointer - --> $DIR/raw-bytes.rs:103:1 +error[E0080]: constructing invalid value: encountered a null function pointer + --> $DIR/raw-bytes.rs:103:36 | LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 00 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_FN_PTR` failed here error[E0080]: constructing invalid value: encountered 0xd[noalloc], but expected a function pointer --> $DIR/raw-bytes.rs:105:1 @@ -207,7 +127,7 @@ LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) }; 0d 00 00 00 00 00 00 00 │ ........ } -error[E0080]: constructing invalid value: encountered ALLOC3, but expected a function pointer +error[E0080]: constructing invalid value: encountered ALLOC0, but expected a function pointer --> $DIR/raw-bytes.rs:107:1 | LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; @@ -219,48 +139,28 @@ LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; } error[E0080]: constructing invalid value: encountered a reference pointing to uninhabited type Bar - --> $DIR/raw-bytes.rs:113:1 + --> $DIR/raw-bytes.rs:113:36 | LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - 01 00 00 00 00 00 00 00 │ ........ - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_BAD_REF` failed here error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) - --> $DIR/raw-bytes.rs:137:1 + --> $DIR/raw-bytes.rs:137:37 | LL | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ e7 03 00 00 00 00 00 00 │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `STR_TOO_LONG` failed here -error[E0080]: constructing invalid value at .0: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/raw-bytes.rs:139:1 +error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object + --> $DIR/raw-bytes.rs:139:53 | LL | const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ff ff ff ff ff ff ff ff │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NESTED_STR_MUCH_TOO_LONG` failed here error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/raw-bytes.rs:141:1 + --> $DIR/raw-bytes.rs:141:47 | LL | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ff ff ff ff ff ff ff ff │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `MY_STR_MUCH_TOO_LONG` failed here error[E0080]: constructing invalid value at .: encountered uninitialized memory, but expected a string --> $DIR/raw-bytes.rs:144:1 @@ -298,48 +198,28 @@ LL | const MYSTR_NO_INIT_ISSUE83182: &MyStr = unsafe { mem::transmute::<&[_], _> } error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) - --> $DIR/raw-bytes.rs:152:1 + --> $DIR/raw-bytes.rs:152:40 | LL | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ e7 03 00 00 00 00 00 00 │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG` failed here error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/raw-bytes.rs:154:1 + --> $DIR/raw-bytes.rs:154:50 | LL | const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ff ff ff ff ff ff ff 7f │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG_OVERFLOW` failed here error[E0080]: constructing invalid value: encountered a dangling box (going beyond the bounds of its allocation) - --> $DIR/raw-bytes.rs:157:1 + --> $DIR/raw-bytes.rs:157:48 | LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ e7 03 00 00 00 00 00 00 │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG_BOX` failed here -error[E0080]: constructing invalid value at .[0]: encountered 0x03, but expected a boolean - --> $DIR/raw-bytes.rs:160:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/raw-bytes.rs:160:51 | LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - ╾ALLOC_ID╼ │ ╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_CONTENT_INVALID` failed here note: erroneous constant encountered --> $DIR/raw-bytes.rs:160:40 @@ -347,16 +227,11 @@ note: erroneous constant encountered LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at ..0: encountered 0x03, but expected a boolean - --> $DIR/raw-bytes.rs:164:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/raw-bytes.rs:164:60 | LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - ╾ALLOC_ID╼ │ ╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `MYSLICE_PREFIX_BAD` failed here note: erroneous constant encountered --> $DIR/raw-bytes.rs:164:42 @@ -364,16 +239,11 @@ note: erroneous constant encountered LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at ..1[0]: encountered 0x03, but expected a boolean - --> $DIR/raw-bytes.rs:167:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/raw-bytes.rs:167:67 | LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 8) { - ╾ALLOC_ID╼ │ ╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `MYSLICE_SUFFIX_BAD` failed here note: erroneous constant encountered --> $DIR/raw-bytes.rs:167:42 @@ -381,49 +251,29 @@ note: erroneous constant encountered LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at .0: encountered ALLOC17, but expected a vtable pointer - --> $DIR/raw-bytes.rs:171:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC13, but expected a vtable pointer + --> $DIR/raw-bytes.rs:171:58 | LL | const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──────╼╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_SHORT_VTABLE_1` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC19, but expected a vtable pointer - --> $DIR/raw-bytes.rs:174:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC14, but expected a vtable pointer + --> $DIR/raw-bytes.rs:174:58 | LL | const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──────╼╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_SHORT_VTABLE_2` failed here error[E0080]: constructing invalid value at .0: encountered 0x4[noalloc], but expected a vtable pointer - --> $DIR/raw-bytes.rs:177:1 + --> $DIR/raw-bytes.rs:177:54 | LL | const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_INT_VTABLE` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC22, but expected a vtable pointer - --> $DIR/raw-bytes.rs:179:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC15, but expected a vtable pointer + --> $DIR/raw-bytes.rs:179:66 | LL | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──────╼╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR` failed here error[E0080]: constructing invalid value at ..: encountered 0x03, but expected a boolean --> $DIR/raw-bytes.rs:182:1 @@ -437,26 +287,16 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, } error[E0080]: constructing invalid value: encountered null pointer, but expected a vtable pointer - --> $DIR/raw-bytes.rs:185:1 + --> $DIR/raw-bytes.rs:185:62 | LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ 00 00 00 00 00 00 00 00 │ ╾──────╼........ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_NULL` failed here -error[E0080]: constructing invalid value: encountered ALLOC27, but expected a vtable pointer - --> $DIR/raw-bytes.rs:187:1 +error[E0080]: constructing invalid value: encountered ALLOC16, but expected a vtable pointer + --> $DIR/raw-bytes.rs:187:65 | LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC_ID╼ ╾ALLOC_ID╼ │ ╾──────╼╾──────╼ - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_INVALID` failed here error[E0080]: constructing invalid value: encountered a reference pointing to uninhabited type [!; 1] --> $DIR/raw-bytes.rs:191:1 diff --git a/tests/ui/consts/const-eval/raw-bytes.rs b/tests/ui/consts/const-eval/raw-bytes.rs index 58ae763e017f0..b41066be59923 100644 --- a/tests/ui/consts/const-eval/raw-bytes.rs +++ b/tests/ui/consts/const-eval/raw-bytes.rs @@ -76,10 +76,10 @@ const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; //~^ ERROR constructing invalid value const NULL_FAT_PTR: NonNull = unsafe { - //~^ ERROR constructing invalid value let x: &dyn Send = &42; let meta = std::ptr::metadata(x); mem::transmute((0_usize, meta)) + //~^ ERROR constructing invalid value }; const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) }; diff --git a/tests/ui/consts/const-eval/transmute-const.stderr b/tests/ui/consts/const-eval/transmute-const.stderr index ed3b3df70dd5d..0c49ac9205b7a 100644 --- a/tests/ui/consts/const-eval/transmute-const.stderr +++ b/tests/ui/consts/const-eval/transmute-const.stderr @@ -1,13 +1,8 @@ error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean - --> $DIR/transmute-const.rs:4:1 + --> $DIR/transmute-const.rs:4:29 | LL | static FOO: bool = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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) { - 03 │ . - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `FOO` failed here error: aborting due to 1 previous error diff --git a/tests/ui/consts/const-eval/ub-enum.rs b/tests/ui/consts/const-eval/ub-enum.rs index 52fc994506837..50131a03cf54e 100644 --- a/tests/ui/consts/const-eval/ub-enum.rs +++ b/tests/ui/consts/const-eval/ub-enum.rs @@ -30,10 +30,10 @@ const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) }; //~^ ERROR expected a valid enum tag const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer const BAD_ENUM_WRAPPED: Wrap = unsafe { mem::transmute(&1) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer // # simple enum with discriminant 2 @@ -47,10 +47,10 @@ enum Enum2 { const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) }; //~^ ERROR expected a valid enum tag const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer // something wrapping the enum so that we test layout first, not enum const BAD_ENUM2_WRAPPED: Wrap = unsafe { mem::transmute(&0) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer // Undef enum discriminant. #[repr(C)] @@ -63,7 +63,7 @@ const BAD_ENUM2_UNDEF: Enum2 = unsafe { MaybeUninit { uninit: () }.init }; // Pointer value in an enum with a niche that is not just 0. const BAD_ENUM2_OPTION_PTR: Option = unsafe { mem::transmute(&0) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer // # valid discriminant for uninhabited variant diff --git a/tests/ui/consts/const-eval/ub-enum.stderr b/tests/ui/consts/const-eval/ub-enum.stderr index 29f7a1f051a71..37b8c23f6861b 100644 --- a/tests/ui/consts/const-eval/ub-enum.stderr +++ b/tests/ui/consts/const-eval/ub-enum.stderr @@ -1,57 +1,47 @@ error[E0080]: constructing invalid value at .: encountered 0x01, but expected a valid enum tag - --> $DIR/ub-enum.rs:29:1 + --> $DIR/ub-enum.rs:29:33 | LL | const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) }; - | ^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-enum.rs:32:1 +error[E0080]: constructing invalid value at .: encountered a pointer, but expected an integer + --> $DIR/ub-enum.rs:32:37 | LL | const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM_PTR` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM_PTR` failed here | = 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]: unable to turn pointer into integer - --> $DIR/ub-enum.rs:35:1 +error[E0080]: constructing invalid value at .0.: encountered a pointer, but expected an integer + --> $DIR/ub-enum.rs:35:47 | LL | const BAD_ENUM_WRAPPED: Wrap = unsafe { mem::transmute(&1) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM_WRAPPED` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM_WRAPPED` failed here | = 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]: constructing invalid value at .: encountered 0x0, but expected a valid enum tag - --> $DIR/ub-enum.rs:47:1 + --> $DIR/ub-enum.rs:47:35 | LL | const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-enum.rs:49:1 +error[E0080]: constructing invalid value at .: encountered a pointer, but expected an integer + --> $DIR/ub-enum.rs:49:39 | LL | const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_PTR` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_PTR` failed here | = 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]: unable to turn pointer into integer - --> $DIR/ub-enum.rs:52:1 +error[E0080]: constructing invalid value at .0.: encountered a pointer, but expected an integer + --> $DIR/ub-enum.rs:52:49 | LL | const BAD_ENUM2_WRAPPED: Wrap = unsafe { mem::transmute(&0) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_WRAPPED` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_WRAPPED` failed here | = 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 @@ -62,47 +52,32 @@ error[E0080]: using uninitialized data, but this operation requires initialized LL | const BAD_ENUM2_UNDEF: Enum2 = unsafe { MaybeUninit { uninit: () }.init }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_UNDEF` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-enum.rs:65:1 +error[E0080]: constructing invalid value at .: encountered a pointer, but expected an integer + --> $DIR/ub-enum.rs:65:54 | LL | const BAD_ENUM2_OPTION_PTR: Option = unsafe { mem::transmute(&0) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_OPTION_PTR` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `BAD_ENUM2_OPTION_PTR` failed here | = 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]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/ub-enum.rs:82:1 + --> $DIR/ub-enum.rs:82:62 | LL | const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINHABITED_VARIANT1` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/ub-enum.rs:84:1 + --> $DIR/ub-enum.rs:84:62 | LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINHABITED_VARIANT2` failed here -error[E0080]: constructing invalid value at ..0.1: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) - --> $DIR/ub-enum.rs:92:1 +error[E0080]: constructing invalid value: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) + --> $DIR/ub-enum.rs:92:67 | LL | const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) })); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_OPTION_CHAR` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant --> $DIR/ub-enum.rs:97:77 diff --git a/tests/ui/consts/const-eval/ub-incorrect-vtable.32bit.stderr b/tests/ui/consts/const-eval/ub-incorrect-vtable.32bit.stderr index 86d6f8c52bc70..bf8f8652213c9 100644 --- a/tests/ui/consts/const-eval/ub-incorrect-vtable.32bit.stderr +++ b/tests/ui/consts/const-eval/ub-incorrect-vtable.32bit.stderr @@ -1,57 +1,32 @@ -error[E0080]: constructing invalid value: encountered ALLOC1, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:18:1 +error[E0080]: constructing invalid value: encountered ALLOC2, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:19:14 | -LL | const INVALID_VTABLE_ALIGNMENT: &dyn Trait = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC0╼ ╾ALLOC1╼ │ ╾──╼╾──╼ - } +LL | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_ALIGNMENT` failed here error[E0080]: constructing invalid value: encountered ALLOC3, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:22:1 - | -LL | const INVALID_VTABLE_SIZE: &dyn Trait = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value + --> $DIR/ub-incorrect-vtable.rs:23:14 | - = 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: 8, align: 4) { - ╾ALLOC2╼ ╾ALLOC3╼ │ ╾──╼╾──╼ - } +LL | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_SIZE` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC5, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:31:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC4, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:32:14 | -LL | const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC4╼ ╾ALLOC5╼ │ ╾──╼╾──╼ - } +LL | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_ALIGNMENT_UB` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC7, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:35:1 - | -LL | const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value +error[E0080]: constructing invalid value at .0: encountered ALLOC5, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:36:14 | - = 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: 8, align: 4) { - ╾ALLOC6╼ ╾ALLOC7╼ │ ╾──╼╾──╼ - } +LL | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_SIZE_UB` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC9, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:40:1 - | -LL | const INVALID_VTABLE_UB: W<&dyn Trait> = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value +error[E0080]: constructing invalid value at .0: encountered ALLOC6, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:41:14 | - = 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: 8, align: 4) { - ╾ALLOC8╼ ╾ALLOC9╼ │ ╾──╼╾──╼ - } +LL | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1usize))) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_UB` failed here error[E0080]: constructing invalid value at .1: encountered a dangling reference (going beyond the bounds of its allocation) --> $DIR/ub-incorrect-vtable.rs:86:1 @@ -61,7 +36,7 @@ LL | const G: Wide = unsafe { Transmute { t: FOO }.u }; | = 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: 8, align: 4) { - ╾ALLOC10╼ ╾ALLOC11╼ │ ╾──╼╾──╼ + ╾ALLOC0╼ ╾ALLOC1╼ │ ╾──╼╾──╼ } error: aborting due to 6 previous errors diff --git a/tests/ui/consts/const-eval/ub-incorrect-vtable.64bit.stderr b/tests/ui/consts/const-eval/ub-incorrect-vtable.64bit.stderr index a9518216dbdb7..957ce76e3f2c4 100644 --- a/tests/ui/consts/const-eval/ub-incorrect-vtable.64bit.stderr +++ b/tests/ui/consts/const-eval/ub-incorrect-vtable.64bit.stderr @@ -1,57 +1,32 @@ -error[E0080]: constructing invalid value: encountered ALLOC1, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:18:1 +error[E0080]: constructing invalid value: encountered ALLOC2, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:19:14 | -LL | const INVALID_VTABLE_ALIGNMENT: &dyn Trait = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC0╼ ╾ALLOC1╼ │ ╾──────╼╾──────╼ - } +LL | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_ALIGNMENT` failed here error[E0080]: constructing invalid value: encountered ALLOC3, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:22:1 - | -LL | const INVALID_VTABLE_SIZE: &dyn Trait = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value + --> $DIR/ub-incorrect-vtable.rs:23:14 | - = 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: 16, align: 8) { - ╾ALLOC2╼ ╾ALLOC3╼ │ ╾──────╼╾──────╼ - } +LL | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_SIZE` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC5, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:31:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC4, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:32:14 | -LL | const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC4╼ ╾ALLOC5╼ │ ╾──────╼╾──────╼ - } +LL | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_ALIGNMENT_UB` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC7, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:35:1 - | -LL | const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value +error[E0080]: constructing invalid value at .0: encountered ALLOC5, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:36:14 | - = 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: 16, align: 8) { - ╾ALLOC6╼ ╾ALLOC7╼ │ ╾──────╼╾──────╼ - } +LL | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_SIZE_UB` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC9, but expected a vtable pointer - --> $DIR/ub-incorrect-vtable.rs:40:1 - | -LL | const INVALID_VTABLE_UB: W<&dyn Trait> = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value +error[E0080]: constructing invalid value at .0: encountered ALLOC6, but expected a vtable pointer + --> $DIR/ub-incorrect-vtable.rs:41:14 | - = 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: 16, align: 8) { - ╾ALLOC8╼ ╾ALLOC9╼ │ ╾──────╼╾──────╼ - } +LL | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1usize))) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `INVALID_VTABLE_UB` failed here error[E0080]: constructing invalid value at .1: encountered a dangling reference (going beyond the bounds of its allocation) --> $DIR/ub-incorrect-vtable.rs:86:1 @@ -61,7 +36,7 @@ LL | const G: Wide = unsafe { Transmute { t: FOO }.u }; | = 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: 16, align: 8) { - ╾ALLOC10╼ ╾ALLOC11╼ │ ╾──────╼╾──────╼ + ╾ALLOC0╼ ╾ALLOC1╼ │ ╾──────╼╾──────╼ } error: aborting due to 6 previous errors diff --git a/tests/ui/consts/const-eval/ub-incorrect-vtable.rs b/tests/ui/consts/const-eval/ub-incorrect-vtable.rs index 4185b0261b296..efc179660df35 100644 --- a/tests/ui/consts/const-eval/ub-incorrect-vtable.rs +++ b/tests/ui/consts/const-eval/ub-incorrect-vtable.rs @@ -17,11 +17,11 @@ trait Trait {} const INVALID_VTABLE_ALIGNMENT: &dyn Trait = unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) }; -//~^^ ERROR vtable +//~^ ERROR vtable const INVALID_VTABLE_SIZE: &dyn Trait = unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) }; -//~^^ ERROR vtable +//~^ ERROR vtable #[repr(transparent)] struct W(T); @@ -30,16 +30,16 @@ fn drop_me(_: *mut usize) {} const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> = unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) }; -//~^^ ERROR expected a vtable pointer +//~^ ERROR expected a vtable pointer const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> = unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) }; -//~^^ ERROR expected a vtable pointer +//~^ ERROR expected a vtable pointer // Even if the vtable has a fn ptr and a reasonable size+align, it still does not work. const INVALID_VTABLE_UB: W<&dyn Trait> = unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1usize))) }; -//~^^ ERROR expected a vtable pointer +//~^ ERROR expected a vtable pointer // Trying to access the data in a vtable does not work, either. diff --git a/tests/ui/consts/const-eval/ub-int-array.rs b/tests/ui/consts/const-eval/ub-int-array.rs index eebbccaa7c175..7e292eb3fccf4 100644 --- a/tests/ui/consts/const-eval/ub-int-array.rs +++ b/tests/ui/consts/const-eval/ub-int-array.rs @@ -18,8 +18,8 @@ impl MaybeUninit { } const UNINIT_INT_0: [u32; 3] = unsafe { - //~^ ERROR invalid value at [0] mem::transmute([ + //~^ ERROR invalid value at [0] MaybeUninit { uninit: () }, // Constants chosen to achieve endianness-independent hex dump. MaybeUninit::new(0x11111111), @@ -27,8 +27,8 @@ const UNINIT_INT_0: [u32; 3] = unsafe { ]) }; const UNINIT_INT_1: [u32; 3] = unsafe { - //~^ ERROR invalid value at [1] mem::transmute([ + //~^ ERROR invalid value at [1] MaybeUninit::new(0u8), MaybeUninit::new(0u8), MaybeUninit::new(0u8), @@ -44,8 +44,8 @@ const UNINIT_INT_1: [u32; 3] = unsafe { ]) }; const UNINIT_INT_2: [u32; 3] = unsafe { - //~^ ERROR invalid value at [2] mem::transmute([ + //~^ ERROR invalid value at [2] MaybeUninit::new(0u8), MaybeUninit::new(0u8), MaybeUninit::new(0u8), diff --git a/tests/ui/consts/const-eval/ub-int-array.stderr b/tests/ui/consts/const-eval/ub-int-array.stderr index 10eb7c46c928c..7ed29167b95d7 100644 --- a/tests/ui/consts/const-eval/ub-int-array.stderr +++ b/tests/ui/consts/const-eval/ub-int-array.stderr @@ -1,35 +1,37 @@ error[E0080]: constructing invalid value at [0]: encountered uninitialized memory, but expected an integer - --> $DIR/ub-int-array.rs:20:1 + --> $DIR/ub-int-array.rs:21:5 | -LL | const UNINIT_INT_0: [u32; 3] = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 12, align: 4) { - __ __ __ __ 11 11 11 11 22 22 22 22 │ ░░░░...."""" - } +LL | / mem::transmute([ +LL | | +LL | | MaybeUninit { uninit: () }, +... | +LL | | MaybeUninit::new(0x22222222), +LL | | ]) + | |______^ evaluation of `UNINIT_INT_0` failed here error[E0080]: constructing invalid value at [1]: encountered uninitialized memory, but expected an integer - --> $DIR/ub-int-array.rs:29:1 - | -LL | const UNINIT_INT_1: [u32; 3] = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value + --> $DIR/ub-int-array.rs:30:5 | - = 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: 12, align: 4) { - 00 00 00 00 01 __ 01 01 02 02 __ 02 │ .....░....░. - } +LL | / mem::transmute([ +LL | | +LL | | MaybeUninit::new(0u8), +LL | | MaybeUninit::new(0u8), +... | +LL | | MaybeUninit::new(2u8), +LL | | ]) + | |______^ evaluation of `UNINIT_INT_1` failed here error[E0080]: constructing invalid value at [2]: encountered uninitialized memory, but expected an integer - --> $DIR/ub-int-array.rs:46:1 - | -LL | const UNINIT_INT_2: [u32; 3] = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value + --> $DIR/ub-int-array.rs:47:5 | - = 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: 12, align: 4) { - 00 00 00 00 01 01 01 01 02 02 02 __ │ ...........░ - } +LL | / mem::transmute([ +LL | | +LL | | MaybeUninit::new(0u8), +LL | | MaybeUninit::new(0u8), +... | +LL | | MaybeUninit { uninit: () }, +LL | | ]) + | |______^ evaluation of `UNINIT_INT_2` failed here error: aborting due to 3 previous errors diff --git a/tests/ui/consts/const-eval/ub-nonnull.rs b/tests/ui/consts/const-eval/ub-nonnull.rs index 9164684262480..d6d51f7a2111a 100644 --- a/tests/ui/consts/const-eval/ub-nonnull.rs +++ b/tests/ui/consts/const-eval/ub-nonnull.rs @@ -18,8 +18,9 @@ const NULL_PTR: NonNull = unsafe { mem::transmute(0usize) }; const OUT_OF_BOUNDS_PTR: NonNull = { unsafe { let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle + //~^ ERROR going beyond the bounds of its allocation // Use address-of-element for pointer arithmetic. This could wrap around to null! - let out_of_bounds_ptr = &ptr[255]; //~ ERROR in-bounds pointer arithmetic failed + let out_of_bounds_ptr = &ptr[255]; mem::transmute(out_of_bounds_ptr) } }; @@ -51,10 +52,10 @@ const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; //~^ ERROR invalid value const NULL_FAT_PTR: NonNull = unsafe { -//~^ ERROR invalid value let x: &dyn Send = &42; let meta = std::ptr::metadata(x); mem::transmute((0_usize, meta)) +//~^ ERROR invalid value }; fn main() {} diff --git a/tests/ui/consts/const-eval/ub-nonnull.stderr b/tests/ui/consts/const-eval/ub-nonnull.stderr index 314141e48370d..c0fbc9fc7b4ee 100644 --- a/tests/ui/consts/const-eval/ub-nonnull.stderr +++ b/tests/ui/consts/const-eval/ub-nonnull.stderr @@ -1,50 +1,35 @@ error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/ub-nonnull.rs:16:1 + --> $DIR/ub-nonnull.rs:16:40 | LL | const NULL_PTR: NonNull = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_PTR` failed here -error[E0080]: in-bounds pointer arithmetic failed: attempting to offset pointer by 255 bytes, but got ALLOC1 which is only 1 byte from the end of the allocation - --> $DIR/ub-nonnull.rs:22:29 +error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) + --> $DIR/ub-nonnull.rs:20:27 | -LL | let out_of_bounds_ptr = &ptr[255]; - | ^^^^^^^^^ evaluation of `OUT_OF_BOUNDS_PTR` failed here +LL | let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle + | ^^^^^^^^^^^^^^^^^^^^ evaluation of `OUT_OF_BOUNDS_PTR` failed here error[E0080]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/ub-nonnull.rs:26:1 + --> $DIR/ub-nonnull.rs:27:39 | LL | const NULL_U8: NonZero = unsafe { mem::transmute(0u8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_U8` failed here error[E0080]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/ub-nonnull.rs:28:1 + --> $DIR/ub-nonnull.rs:29:45 | LL | const NULL_USIZE: NonZero = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_USIZE` failed here error[E0080]: using uninitialized data, but this operation requires initialized memory - --> $DIR/ub-nonnull.rs:36:38 + --> $DIR/ub-nonnull.rs:37:38 | LL | const UNINIT: NonZero = unsafe { MaybeUninit { uninit: () }.init }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNINIT` failed here error[E0080]: constructing invalid value: encountered 42, but expected something in the range 10..=30 - --> $DIR/ub-nonnull.rs:44:1 + --> $DIR/ub-nonnull.rs:45:1 | LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value @@ -55,7 +40,7 @@ LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) }; } error[E0080]: constructing invalid value: encountered 20, but expected something less or equal to 10, or greater or equal to 30 - --> $DIR/ub-nonnull.rs:50:1 + --> $DIR/ub-nonnull.rs:51:1 | LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value @@ -66,15 +51,10 @@ LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) }; } error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/ub-nonnull.rs:53:1 + --> $DIR/ub-nonnull.rs:57:5 | -LL | const NULL_FAT_PTR: NonNull = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } +LL | mem::transmute((0_usize, meta)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_FAT_PTR` failed here error: aborting due to 8 previous errors diff --git a/tests/ui/consts/const-eval/ub-ref-ptr.rs b/tests/ui/consts/const-eval/ub-ref-ptr.rs index 64b48939be6de..9d3a5e38d75ea 100644 --- a/tests/ui/consts/const-eval/ub-ref-ptr.rs +++ b/tests/ui/consts/const-eval/ub-ref-ptr.rs @@ -31,13 +31,13 @@ const NULL_BOX: Box = unsafe { mem::transmute(0usize) }; // but that would fail to compile; so we ended up breaking user code that would // have worked fine had we not promoted. const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }]; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected an integer const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) }; //~^ ERROR invalid value diff --git a/tests/ui/consts/const-eval/ub-ref-ptr.stderr b/tests/ui/consts/const-eval/ub-ref-ptr.stderr index d5ccc396b9036..cb72b8e7e5275 100644 --- a/tests/ui/consts/const-eval/ub-ref-ptr.stderr +++ b/tests/ui/consts/const-eval/ub-ref-ptr.stderr @@ -1,61 +1,41 @@ error[E0080]: constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1) - --> $DIR/ub-ref-ptr.rs:17:1 + --> $DIR/ub-ref-ptr.rs:17:34 | LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) }; - | ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED` failed here error[E0080]: constructing invalid value: encountered an unaligned box (required 2 byte alignment but found 1) - --> $DIR/ub-ref-ptr.rs:20:1 + --> $DIR/ub-ref-ptr.rs:20:42 | LL | const UNALIGNED_BOX: Box = unsafe { mem::transmute(&[0u8; 4]) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED_BOX` failed here error[E0080]: constructing invalid value: encountered a null reference - --> $DIR/ub-ref-ptr.rs:23:1 + --> $DIR/ub-ref-ptr.rs:23:29 | LL | const NULL: &u16 = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL` failed here error[E0080]: constructing invalid value: encountered a null box - --> $DIR/ub-ref-ptr.rs:26:1 + --> $DIR/ub-ref-ptr.rs:26:37 | LL | const NULL_BOX: Box = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_BOX` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-ref-ptr.rs:33:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/ub-ref-ptr.rs:33:38 | LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `REF_AS_USIZE` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `REF_AS_USIZE` failed here | = 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]: unable to turn pointer into integer - --> $DIR/ub-ref-ptr.rs:36:39 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/ub-ref-ptr.rs:36:49 | LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `REF_AS_USIZE_SLICE` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `REF_AS_USIZE_SLICE` failed here | = 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 @@ -66,11 +46,11 @@ note: erroneous constant encountered LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-ref-ptr.rs:39:86 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/ub-ref-ptr.rs:39:87 | LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) }; - | ^^^^^^^^^^^^^^^^^^^^ evaluation of `REF_AS_USIZE_BOX_SLICE` failed here + | ^^^^^^^^^^^^^^^^^^ evaluation of `REF_AS_USIZE_BOX_SLICE` failed here | = 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 @@ -82,26 +62,16 @@ LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[us | ^^^^^^^^^^^^^^^^^^^^^ error[E0080]: constructing invalid value: encountered a dangling reference (0x539[noalloc] has no provenance) - --> $DIR/ub-ref-ptr.rs:42:1 + --> $DIR/ub-ref-ptr.rs:42:44 | LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `USIZE_AS_REF` failed here error[E0080]: constructing invalid value: encountered a dangling box (0x539[noalloc] has no provenance) - --> $DIR/ub-ref-ptr.rs:45:1 + --> $DIR/ub-ref-ptr.rs:45:40 | LL | const USIZE_AS_BOX: Box = unsafe { mem::transmute(1337usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `USIZE_AS_BOX` failed here error[E0080]: using uninitialized data, but this operation requires initialized memory --> $DIR/ub-ref-ptr.rs:48:41 @@ -109,16 +79,11 @@ error[E0080]: using uninitialized data, but this operation requires initialized LL | const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNINIT_PTR` failed here -error[E0080]: constructing invalid value: encountered null pointer, but expected a function pointer - --> $DIR/ub-ref-ptr.rs:51:1 +error[E0080]: constructing invalid value: encountered a null function pointer + --> $DIR/ub-ref-ptr.rs:51:36 | LL | const NULL_FN_PTR: fn() = unsafe { mem::transmute(0usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NULL_FN_PTR` failed here error[E0080]: using uninitialized data, but this operation requires initialized memory --> $DIR/ub-ref-ptr.rs:53:38 @@ -137,7 +102,7 @@ LL | const DANGLING_FN_PTR: fn() = unsafe { mem::transmute(13usize) }; HEX_DUMP } -error[E0080]: constructing invalid value: encountered ALLOC2, but expected a function pointer +error[E0080]: constructing invalid value: encountered ALLOC0, but expected a function pointer --> $DIR/ub-ref-ptr.rs:57:1 | LL | const DATA_FN_PTR: fn() = unsafe { mem::transmute(&13) }; diff --git a/tests/ui/consts/const-eval/ub-uninhabit.stderr b/tests/ui/consts/const-eval/ub-uninhabit.stderr index b0f475fe93867..c04b077c5a1be 100644 --- a/tests/ui/consts/const-eval/ub-uninhabit.stderr +++ b/tests/ui/consts/const-eval/ub-uninhabit.stderr @@ -5,15 +5,10 @@ LL | const BAD_BAD_BAD: Bar = unsafe { MaybeUninit { uninit: () }.init }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_BAD_BAD` failed here error[E0080]: constructing invalid value: encountered a reference pointing to uninhabited type Bar - --> $DIR/ub-uninhabit.rs:23:1 + --> $DIR/ub-uninhabit.rs:23:36 | LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) }; - | ^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_BAD_REF` failed here error[E0080]: constructing invalid value at [0]: encountered a value of uninhabited type `Bar` --> $DIR/ub-uninhabit.rs:26:42 diff --git a/tests/ui/consts/const-eval/ub-upvars.32bit.stderr b/tests/ui/consts/const-eval/ub-upvars.32bit.stderr index ecd1c768c287f..dea449129490c 100644 --- a/tests/ui/consts/const-eval/ub-upvars.32bit.stderr +++ b/tests/ui/consts/const-eval/ub-upvars.32bit.stderr @@ -1,13 +1,8 @@ -error[E0080]: constructing invalid value at ...: encountered a null reference - --> $DIR/ub-upvars.rs:6:1 +error[E0080]: constructing invalid value: encountered a null reference + --> $DIR/ub-upvars.rs:7:42 | -LL | const BAD_UPVAR: &dyn FnOnce() = &{ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - ╾ALLOC0╼ ╾ALLOC1╼ │ ╾──╼╾──╼ - } +LL | let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) }; + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UPVAR` failed here error: aborting due to 1 previous error diff --git a/tests/ui/consts/const-eval/ub-upvars.64bit.stderr b/tests/ui/consts/const-eval/ub-upvars.64bit.stderr index 108dfe6b27be3..dea449129490c 100644 --- a/tests/ui/consts/const-eval/ub-upvars.64bit.stderr +++ b/tests/ui/consts/const-eval/ub-upvars.64bit.stderr @@ -1,13 +1,8 @@ -error[E0080]: constructing invalid value at ...: encountered a null reference - --> $DIR/ub-upvars.rs:6:1 +error[E0080]: constructing invalid value: encountered a null reference + --> $DIR/ub-upvars.rs:7:42 | -LL | const BAD_UPVAR: &dyn FnOnce() = &{ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 16, align: 8) { - ╾ALLOC0╼ ╾ALLOC1╼ │ ╾──────╼╾──────╼ - } +LL | let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) }; + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UPVAR` failed here error: aborting due to 1 previous error diff --git a/tests/ui/consts/const-eval/ub-upvars.rs b/tests/ui/consts/const-eval/ub-upvars.rs index 197222829842c..7464f37bb0630 100644 --- a/tests/ui/consts/const-eval/ub-upvars.rs +++ b/tests/ui/consts/const-eval/ub-upvars.rs @@ -3,10 +3,13 @@ use std::mem; -const BAD_UPVAR: &dyn FnOnce() = &{ //~ ERROR null reference - let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) }; +const BAD_UPVAR: &dyn FnOnce() = &{ + let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) }; //~ ERROR null reference let another_var = 13; - move || { let _ = bad_ref; let _ = another_var; } + move || { + let _ = bad_ref; + let _ = another_var; + } }; fn main() {} diff --git a/tests/ui/consts/const-eval/ub-wide-ptr.rs b/tests/ui/consts/const-eval/ub-wide-ptr.rs index 86235897e7e8d..afce28ec265d5 100644 --- a/tests/ui/consts/const-eval/ub-wide-ptr.rs +++ b/tests/ui/consts/const-eval/ub-wide-ptr.rs @@ -42,10 +42,10 @@ const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize:: //~^ ERROR slice is bigger than largest supported object // bad str const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected a reference // bad str in user-defined unsized type const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected a reference const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) }; //~^ ERROR slice is bigger than largest supported object @@ -61,9 +61,9 @@ const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit:: const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // bad slice: length uninit const SLICE_LENGTH_UNINIT: &[u8] = unsafe { -//~^ ERROR uninitialized let uninit_len = MaybeUninit:: { uninit: () }; mem::transmute((42, uninit_len)) +//~^ ERROR uninitialized }; // bad slice: length too big const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }; @@ -73,13 +73,13 @@ const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize:: //~^ ERROR slice is bigger than largest supported object // bad slice: length not an int const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected a reference // bad slice box: length too big const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }; //~^ ERROR dangling box (going beyond the bounds of its allocation) // bad slice box: length not an int const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) }; -//~^ ERROR unable to turn pointer into integer +//~^ ERROR encountered a pointer, but expected a box // bad data *inside* the slice const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; @@ -99,9 +99,9 @@ const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe { -//~^ ERROR uninitialized let uninit_len = MaybeUninit:: { uninit: () }; mem::transmute((42, uninit_len)) +//~^ ERROR uninitialized }; // # trait object @@ -139,11 +139,11 @@ const DYN_METADATA: ptr::DynMetadata = ptr::metadata::(ptr:: static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe { mem::transmute::<_, &dyn Trait>((&92u8, 0usize)) - //~^^ ERROR null pointer + //~^ ERROR null pointer }; static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe { mem::transmute::<_, &dyn Trait>((&92u8, &3u64)) - //~^^ ERROR vtable + //~^ ERROR vtable }; fn main() {} diff --git a/tests/ui/consts/const-eval/ub-wide-ptr.stderr b/tests/ui/consts/const-eval/ub-wide-ptr.stderr index 8724dd9a3c0d3..06601fbfb2dfc 100644 --- a/tests/ui/consts/const-eval/ub-wide-ptr.stderr +++ b/tests/ui/consts/const-eval/ub-wide-ptr.stderr @@ -1,53 +1,38 @@ error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) - --> $DIR/ub-wide-ptr.rs:39:1 + --> $DIR/ub-wide-ptr.rs:39:37 | LL | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `STR_TOO_LONG` failed here -error[E0080]: constructing invalid value at .0: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/ub-wide-ptr.rs:41:1 +error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object + --> $DIR/ub-wide-ptr.rs:41:53 | LL | const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `NESTED_STR_MUCH_TOO_LONG` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-wide-ptr.rs:44:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected a reference + --> $DIR/ub-wide-ptr.rs:44:39 | LL | const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `STR_LENGTH_PTR` failed here + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `STR_LENGTH_PTR` failed here | = 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]: unable to turn pointer into integer - --> $DIR/ub-wide-ptr.rs:47:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected a reference + --> $DIR/ub-wide-ptr.rs:47:44 | LL | const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `MY_STR_LENGTH_PTR` failed here + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `MY_STR_LENGTH_PTR` failed here | = 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]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/ub-wide-ptr.rs:49:1 + --> $DIR/ub-wide-ptr.rs:49:47 | LL | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `MY_STR_MUCH_TOO_LONG` failed here error[E0080]: constructing invalid value at .: encountered uninitialized memory, but expected a string --> $DIR/ub-wide-ptr.rs:53:1 @@ -71,73 +56,53 @@ LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUni HEX_DUMP } -error[E0080]: using uninitialized data, but this operation requires initialized memory - --> $DIR/ub-wide-ptr.rs:63:1 +error[E0080]: constructing invalid value: encountered uninitialized memory, but expected a reference + --> $DIR/ub-wide-ptr.rs:65:5 | -LL | const SLICE_LENGTH_UNINIT: &[u8] = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_LENGTH_UNINIT` failed here +LL | mem::transmute((42, uninit_len)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_LENGTH_UNINIT` failed here error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation) - --> $DIR/ub-wide-ptr.rs:69:1 + --> $DIR/ub-wide-ptr.rs:69:40 | LL | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG` failed here error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/ub-wide-ptr.rs:72:1 + --> $DIR/ub-wide-ptr.rs:72:50 | LL | const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG_OVERFLOW` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-wide-ptr.rs:75:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected a reference + --> $DIR/ub-wide-ptr.rs:75:42 | LL | const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_LENGTH_PTR` failed here + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_LENGTH_PTR` failed here | = 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]: constructing invalid value: encountered a dangling box (going beyond the bounds of its allocation) - --> $DIR/ub-wide-ptr.rs:78:1 + --> $DIR/ub-wide-ptr.rs:78:48 | LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_TOO_LONG_BOX` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ub-wide-ptr.rs:81:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected a box + --> $DIR/ub-wide-ptr.rs:81:50 | LL | const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_LENGTH_PTR_BOX` failed here + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_LENGTH_PTR_BOX` failed here | = 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]: constructing invalid value at .[0]: encountered 0x03, but expected a boolean - --> $DIR/ub-wide-ptr.rs:85:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/ub-wide-ptr.rs:85:51 | LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `SLICE_CONTENT_INVALID` failed here note: erroneous constant encountered --> $DIR/ub-wide-ptr.rs:85:40 @@ -145,16 +110,11 @@ note: erroneous constant encountered LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at ..0: encountered 0x03, but expected a boolean - --> $DIR/ub-wide-ptr.rs:91:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/ub-wide-ptr.rs:91:60 | LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `MYSLICE_PREFIX_BAD` failed here note: erroneous constant encountered --> $DIR/ub-wide-ptr.rs:91:42 @@ -162,16 +122,11 @@ note: erroneous constant encountered LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: constructing invalid value at ..1[0]: encountered 0x03, but expected a boolean - --> $DIR/ub-wide-ptr.rs:94:1 +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/ub-wide-ptr.rs:94:67 | LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^ evaluation of `MYSLICE_SUFFIX_BAD` failed here note: erroneous constant encountered --> $DIR/ub-wide-ptr.rs:94:42 @@ -179,88 +134,53 @@ note: erroneous constant encountered LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: using uninitialized data, but this operation requires initialized memory - --> $DIR/ub-wide-ptr.rs:101:1 +error[E0080]: constructing invalid value: encountered uninitialized memory, but expected a raw pointer + --> $DIR/ub-wide-ptr.rs:103:5 | -LL | const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_SLICE_LENGTH_UNINIT` failed here +LL | mem::transmute((42, uninit_len)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_SLICE_LENGTH_UNINIT` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC12, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:109:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC4, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:109:58 | LL | const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_SHORT_VTABLE_1` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC14, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:112:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC5, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:112:58 | LL | const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_SHORT_VTABLE_2` failed here error[E0080]: constructing invalid value at .0: encountered 0x4[noalloc], but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:115:1 + --> $DIR/ub-wide-ptr.rs:115:54 | LL | const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_INT_VTABLE` failed here -error[E0080]: constructing invalid value: encountered ALLOC17, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:117:1 +error[E0080]: constructing invalid value: encountered ALLOC6, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:117:57 | LL | const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_UNALIGNED_VTABLE` failed here -error[E0080]: constructing invalid value: encountered ALLOC19, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:119:1 +error[E0080]: constructing invalid value: encountered ALLOC7, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:119:57 | LL | const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_BAD_DROP_FN_NULL` failed here -error[E0080]: constructing invalid value: encountered ALLOC21, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:121:1 +error[E0080]: constructing invalid value: encountered ALLOC8, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:121:56 | LL | const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_BAD_DROP_FN_INT` failed here -error[E0080]: constructing invalid value at .0: encountered ALLOC23, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:123:1 +error[E0080]: constructing invalid value at .0: encountered ALLOC9, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:123:66 | LL | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR` failed here error[E0080]: constructing invalid value at ..: encountered 0x03, but expected a boolean --> $DIR/ub-wide-ptr.rs:127:1 @@ -274,48 +194,28 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, } error[E0080]: constructing invalid value: encountered null pointer, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:131:1 + --> $DIR/ub-wide-ptr.rs:131:62 | LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_NULL` failed here -error[E0080]: constructing invalid value: encountered ALLOC28, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:133:1 +error[E0080]: constructing invalid value: encountered ALLOC10, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:133:65 | LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_INVALID` failed here error[E0080]: constructing invalid value: encountered null pointer, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:140:1 - | -LL | static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value + --> $DIR/ub-wide-ptr.rs:141:5 | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } +LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF` failed here -error[E0080]: constructing invalid value: encountered ALLOC31, but expected a vtable pointer - --> $DIR/ub-wide-ptr.rs:144:1 - | -LL | static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value +error[E0080]: constructing invalid value: encountered ALLOC11, but expected a vtable pointer + --> $DIR/ub-wide-ptr.rs:145:5 | - = 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: $SIZE, align: $ALIGN) { - HEX_DUMP - } +LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF` failed here error: aborting due to 29 previous errors diff --git a/tests/ui/consts/extra-const-ub/detect-extra-ub.no_flag.stderr b/tests/ui/consts/extra-const-ub/detect-extra-ub.no_flag.stderr new file mode 100644 index 0000000000000..06ed9309af0c9 --- /dev/null +++ b/tests/ui/consts/extra-const-ub/detect-extra-ub.no_flag.stderr @@ -0,0 +1,42 @@ +error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean + --> $DIR/detect-extra-ub.rs:29:20 + | +LL | let _x: bool = transmute(3u8); + | ^^^^^^^^^^^^^^ evaluation of `INVALID_BOOL` failed here + +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/detect-extra-ub.rs:34:21 + | +LL | let _x: usize = transmute(&3u8); + | ^^^^^^^^^^^^^^^ evaluation of `INVALID_PTR_IN_INT` failed here + | + = 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]: constructing invalid value at .: encountered a pointer, but expected an integer + --> $DIR/detect-extra-ub.rs:39:28 + | +LL | let _x: PtrSizedEnum = transmute(&3u8); + | ^^^^^^^^^^^^^^^ evaluation of `INVALID_PTR_IN_ENUM` failed here + | + = 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]: constructing invalid value at .0: encountered a pointer, but expected an integer + --> $DIR/detect-extra-ub.rs:45:30 + | +LL | let _x: (usize, usize) = transmute(x); + | ^^^^^^^^^^^^ evaluation of `INVALID_SLICE_TO_USIZE_TRANSMUTE` failed here + | + = 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]: constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1) + --> $DIR/detect-extra-ub.rs:50:20 + | +LL | let _x: &u32 = transmute(&[0u8; 4]); + | ^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED_PTR` failed here + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0080`. 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 e17bac603b415..26bcf5f24650a 100644 --- a/tests/ui/consts/extra-const-ub/detect-extra-ub.rs +++ b/tests/ui/consts/extra-const-ub/detect-extra-ub.rs @@ -1,5 +1,4 @@ //@ revisions: no_flag with_flag -//@ [no_flag] check-pass //@ [with_flag] compile-flags: -Zextra-const-ub-checks #![feature(never_type)] #![allow(unnecessary_transmutes)] @@ -28,28 +27,28 @@ enum UninhDiscriminant { const INVALID_BOOL: () = unsafe { let _x: bool = transmute(3u8); - //[with_flag]~^ ERROR: invalid value + //~^ ERROR: invalid value }; const INVALID_PTR_IN_INT: () = unsafe { let _x: usize = transmute(&3u8); - //[with_flag]~^ ERROR: invalid value + //~^ ERROR: invalid value }; const INVALID_PTR_IN_ENUM: () = unsafe { let _x: PtrSizedEnum = transmute(&3u8); - //[with_flag]~^ ERROR: invalid value + //~^ ERROR: invalid value }; const INVALID_SLICE_TO_USIZE_TRANSMUTE: () = unsafe { let x: &[u8] = &[0; 32]; let _x: (usize, usize) = transmute(x); - //[with_flag]~^ ERROR: invalid value + //~^ ERROR: invalid value }; const UNALIGNED_PTR: () = unsafe { let _x: &u32 = transmute(&[0u8; 4]); - //[with_flag]~^ ERROR: invalid value + //~^ ERROR: invalid value }; const UNINHABITED_VARIANT: () = unsafe { 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 6af72868045ac..f9a0ba755b9ac 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,11 +1,11 @@ error[E0080]: constructing invalid value: encountered 0x03, but expected a boolean - --> $DIR/detect-extra-ub.rs:30:20 + --> $DIR/detect-extra-ub.rs:29:20 | LL | let _x: bool = transmute(3u8); | ^^^^^^^^^^^^^^ evaluation of `INVALID_BOOL` failed here error[E0080]: constructing invalid value: encountered a pointer, but expected an integer - --> $DIR/detect-extra-ub.rs:35:21 + --> $DIR/detect-extra-ub.rs:34:21 | LL | let _x: usize = transmute(&3u8); | ^^^^^^^^^^^^^^^ evaluation of `INVALID_PTR_IN_INT` failed here @@ -14,7 +14,7 @@ LL | let _x: usize = transmute(&3u8); = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: constructing invalid value at .: encountered a pointer, but expected an integer - --> $DIR/detect-extra-ub.rs:40:28 + --> $DIR/detect-extra-ub.rs:39:28 | LL | let _x: PtrSizedEnum = transmute(&3u8); | ^^^^^^^^^^^^^^^ evaluation of `INVALID_PTR_IN_ENUM` failed here @@ -23,7 +23,7 @@ LL | let _x: PtrSizedEnum = transmute(&3u8); = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: constructing invalid value at .0: encountered a pointer, but expected an integer - --> $DIR/detect-extra-ub.rs:46:30 + --> $DIR/detect-extra-ub.rs:45:30 | LL | let _x: (usize, usize) = transmute(x); | ^^^^^^^^^^^^ evaluation of `INVALID_SLICE_TO_USIZE_TRANSMUTE` failed here @@ -32,19 +32,19 @@ LL | let _x: (usize, usize) = transmute(x); = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1) - --> $DIR/detect-extra-ub.rs:51:20 + --> $DIR/detect-extra-ub.rs:50:20 | LL | let _x: &u32 = transmute(&[0u8; 4]); | ^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED_PTR` failed here error[E0080]: constructing invalid value at .: encountered an uninhabited enum variant - --> $DIR/detect-extra-ub.rs:58:13 + --> $DIR/detect-extra-ub.rs:57:13 | LL | let v = *addr_of!(data).cast::(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNINHABITED_VARIANT` failed here error[E0080]: constructing invalid value at [0]: encountered a partial pointer or a mix of pointers - --> $DIR/detect-extra-ub.rs:77:16 + --> $DIR/detect-extra-ub.rs:76:16 | LL | let _val = *(&mem as *const Align as *const [*const u8; 2]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `PARTIAL_POINTER` failed here @@ -53,7 +53,7 @@ LL | let _val = *(&mem as *const Align as *const [*const u8; 2]); = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object - --> $DIR/detect-extra-ub.rs:91:16 + --> $DIR/detect-extra-ub.rs:90:16 | LL | let _val = &*slice; | ^^^^^^^ evaluation of `OVERSIZED_REF` failed here diff --git a/tests/ui/consts/miri_unleashed/ptr_arith.rs b/tests/ui/consts/miri_unleashed/ptr_arith.rs index 3f34102833b0a..3ff0e580f018e 100644 --- a/tests/ui/consts/miri_unleashed/ptr_arith.rs +++ b/tests/ui/consts/miri_unleashed/ptr_arith.rs @@ -11,8 +11,8 @@ static PTR_INT_CAST: () = { static PTR_INT_TRANSMUTE: () = unsafe { let x: usize = std::mem::transmute(&0); + //~^ ERROR: constructing invalid value: encountered a pointer, but expected an integer let _v = x + 0; - //~^ ERROR unable to turn pointer into integer }; // I'd love to test pointer comparison, but that is not possible since diff --git a/tests/ui/consts/miri_unleashed/ptr_arith.stderr b/tests/ui/consts/miri_unleashed/ptr_arith.stderr index 661a8de6a15c8..8d8f15658d794 100644 --- a/tests/ui/consts/miri_unleashed/ptr_arith.stderr +++ b/tests/ui/consts/miri_unleashed/ptr_arith.stderr @@ -4,11 +4,11 @@ error[E0080]: exposing pointers is not possible at compile-time LL | let x = &0 as *const _ as usize; | ^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `PTR_INT_CAST` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/ptr_arith.rs:14:14 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/ptr_arith.rs:13:20 | -LL | let _v = x + 0; - | ^ evaluation of `PTR_INT_TRANSMUTE` failed here +LL | let x: usize = std::mem::transmute(&0); + | ^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `PTR_INT_TRANSMUTE` failed here | = 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 diff --git a/tests/ui/consts/promotion-in-const-validation.rs b/tests/ui/consts/promotion-in-const-validation.rs index 24e44bcc9aa59..0e881cb65a3cf 100644 --- a/tests/ui/consts/promotion-in-const-validation.rs +++ b/tests/ui/consts/promotion-in-const-validation.rs @@ -1,9 +1,9 @@ fn foo() {} const _: &usize = unsafe { &std::mem::transmute(foo as fn()) }; -//~^ ERROR: constructing invalid value at .: encountered a pointer, but expected an integer +//~^ ERROR: encountered a pointer, but expected an integer const _: usize = unsafe { std::mem::transmute(foo as fn()) }; -//~^ ERROR: unable to turn pointer into integer +//~^ ERROR: encountered a pointer, but expected an integer fn main() {} diff --git a/tests/ui/consts/promotion-in-const-validation.stderr b/tests/ui/consts/promotion-in-const-validation.stderr index 5b5da2a37183d..a2f89c937cb01 100644 --- a/tests/ui/consts/promotion-in-const-validation.stderr +++ b/tests/ui/consts/promotion-in-const-validation.stderr @@ -1,15 +1,11 @@ -error[E0080]: constructing invalid value at .: encountered a pointer, but expected an integer - --> $DIR/promotion-in-const-validation.rs:3:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/promotion-in-const-validation.rs:3:29 | LL | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) }; - | ^^^^^^^^^^^^^^^ it is undefined behavior to use this value + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_` failed here | = 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 - = 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: 8, align: 8) { - ╾ALLOC0╼ │ ╾──────╼ - } note: erroneous constant encountered --> $DIR/promotion-in-const-validation.rs:3:28 @@ -17,11 +13,11 @@ note: erroneous constant encountered LL | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0080]: unable to turn pointer into integer - --> $DIR/promotion-in-const-validation.rs:6:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/promotion-in-const-validation.rs:6:27 | LL | const _: usize = unsafe { std::mem::transmute(foo as fn()) }; - | ^^^^^^^^^^^^^^ evaluation of `_` failed here + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_` failed here | = 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 diff --git a/tests/ui/type/pattern_types/validity.rs b/tests/ui/type/pattern_types/validity.rs index a4e49692c98c6..6cce78e8ecb1e 100644 --- a/tests/ui/type/pattern_types/validity.rs +++ b/tests/ui/type/pattern_types/validity.rs @@ -11,24 +11,24 @@ const BAD: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) }; //~^ ERROR: constructing invalid value: encountered 0 const BAD_UNINIT: pattern_type!(u32 is 1..) = - //~^ ERROR: using uninitialized data, but this operation requires initialized memory unsafe { std::mem::transmute(std::mem::MaybeUninit::::uninit()) }; +//~^ ERROR: encountered uninitialized memory, but expected an integer const BAD_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) }; -//~^ ERROR: unable to turn pointer into integer +//~^ ERROR: encountered a pointer, but expected an integer const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0); -//~^ ERROR: constructing invalid value at .0: encountered 0 +//~^ ERROR: encountered 0, but expected something greater or equal to 1 struct Foo(Bar); struct Bar(pattern_type!(u32 is 1..)); const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) })); -//~^ ERROR: constructing invalid value at .0.0: encountered 0 +//~^ ERROR: encountered 0, but expected something greater or equal to 1 const CHAR_UNINIT: pattern_type!(char is 'A'..'Z') = - //~^ ERROR: using uninitialized data, but this operation requires initialized memory unsafe { std::mem::transmute(std::mem::MaybeUninit::::uninit()) }; +//~^ ERROR: encountered uninitialized memory, but expected a unicode scalar value const CHAR_OOB_PAT: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute('a') }; //~^ ERROR: constructing invalid value: encountered 97, but expected something in the range 65..=89 diff --git a/tests/ui/type/pattern_types/validity.stderr b/tests/ui/type/pattern_types/validity.stderr index 4f4c16028f6b7..8a69983c1cad6 100644 --- a/tests/ui/type/pattern_types/validity.stderr +++ b/tests/ui/type/pattern_types/validity.stderr @@ -1,78 +1,53 @@ error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 - --> $DIR/validity.rs:10:1 + --> $DIR/validity.rs:10:49 | LL | const BAD: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD` failed here -error[E0080]: using uninitialized data, but this operation requires initialized memory - --> $DIR/validity.rs:13:1 +error[E0080]: constructing invalid value: encountered uninitialized memory, but expected an integer + --> $DIR/validity.rs:14:14 | -LL | const BAD_UNINIT: pattern_type!(u32 is 1..) = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINIT` failed here +LL | unsafe { std::mem::transmute(std::mem::MaybeUninit::::uninit()) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_UNINIT` failed here -error[E0080]: unable to turn pointer into integer - --> $DIR/validity.rs:17:1 +error[E0080]: constructing invalid value: encountered a pointer, but expected an integer + --> $DIR/validity.rs:17:55 | LL | const BAD_PTR: pattern_type!(usize is 1..) = unsafe { std::mem::transmute(&42) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_PTR` failed here + | ^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_PTR` failed here | = 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]: constructing invalid value at .0: encountered 0, but expected something greater or equal to 1 - --> $DIR/validity.rs:20:1 +error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 + --> $DIR/validity.rs:20:67 | LL | const BAD_AGGREGATE: (pattern_type!(u32 is 1..), u32) = (unsafe { std::mem::transmute(0) }, 0); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 8, align: 4) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_AGGREGATE` failed here -error[E0080]: constructing invalid value at .0.0: encountered 0, but expected something greater or equal to 1 - --> $DIR/validity.rs:26:1 +error[E0080]: constructing invalid value: encountered 0, but expected something greater or equal to 1 + --> $DIR/validity.rs:26:39 | LL | const BAD_FOO: Foo = Foo(Bar(unsafe { std::mem::transmute(0) })); - | ^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `BAD_FOO` failed here -error[E0080]: using uninitialized data, but this operation requires initialized memory - --> $DIR/validity.rs:29:1 +error[E0080]: constructing invalid value: encountered uninitialized memory, but expected a unicode scalar value + --> $DIR/validity.rs:30:14 | -LL | const CHAR_UNINIT: pattern_type!(char is 'A'..'Z') = - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `CHAR_UNINIT` failed here +LL | unsafe { std::mem::transmute(std::mem::MaybeUninit::::uninit()) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `CHAR_UNINIT` failed here error[E0080]: constructing invalid value: encountered 97, but expected something in the range 65..=89 - --> $DIR/validity.rs:33:1 + --> $DIR/validity.rs:33:64 | LL | const CHAR_OOB_PAT: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute('a') }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `CHAR_OOB_PAT` failed here error[E0080]: constructing invalid value: encountered 0xffffffff, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`) - --> $DIR/validity.rs:36:1 + --> $DIR/validity.rs:36:60 | LL | const CHAR_OOB: pattern_type!(char is 'A'..'Z') = unsafe { std::mem::transmute(u32::MAX) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value - | - = 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: 4, align: 4) { - HEX_DUMP - } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `CHAR_OOB` failed here error: aborting due to 8 previous errors