Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use crate::interpret::{
Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
ScalarMaybeUninit, StackPopCleanup,
};
use crate::util::pretty::display_allocation;

use rustc_errors::ErrorReported;
use rustc_hir::def::DefKind;
@@ -360,6 +361,15 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
"it is undefined behavior to use this value",
|mut diag| {
diag.note(note_on_undefined_behavior_error());
diag.note(&format!(
"the raw bytes of the constant ({}",
display_allocation(
*ecx.tcx,
ecx.tcx
.global_alloc(mplace.ptr.assert_ptr().alloc_id)
.unwrap_memory()
)
));
diag.emit();
},
))
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:28:21
--> $DIR/invalid-patterns.rs:29:21
|
LL | get_flag::<false, 0xFF>();
| ^^^^ expected `char`, found `u8`

error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:30:14
--> $DIR/invalid-patterns.rs:31:14
|
LL | get_flag::<7, 'c'>();
| ^ expected `bool`, found integer

error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:32:14
--> $DIR/invalid-patterns.rs:33:14
|
LL | get_flag::<42, 0x5ad>();
| ^^ expected `bool`, found integer

error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:32:18
--> $DIR/invalid-patterns.rs:33:18
|
LL | get_flag::<42, 0x5ad>();
| ^^^^^ expected `char`, found `u8`

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:37:21
--> $DIR/invalid-patterns.rs:38:21
|
LL | get_flag::<false, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= 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) {
__ __ __ __ │ ░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:39:14
--> $DIR/invalid-patterns.rs:40:14
|
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
|
= 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) {
42 │ B
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:41:14
--> $DIR/invalid-patterns.rs:42:14
|
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
|
= 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) {
42 │ B
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:41:47
--> $DIR/invalid-patterns.rs:42:47
|
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= 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) {
__ __ __ __ │ ░░░░
}

error: aborting due to 8 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:29:21
|
LL | get_flag::<false, 0xFF>();
| ^^^^ expected `char`, found `u8`

error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:31:14
|
LL | get_flag::<7, 'c'>();
| ^ expected `bool`, found integer

error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:14
|
LL | get_flag::<42, 0x5ad>();
| ^^ expected `bool`, found integer

error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:18
|
LL | get_flag::<42, 0x5ad>();
| ^^^^^ expected `char`, found `u8`

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:38:21
|
LL | get_flag::<false, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= 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) {
__ __ __ __ │ ░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:40:14
|
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
|
= 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) {
42 │ B
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:42:14
|
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
|
= 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) {
42 │ B
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/invalid-patterns.rs:42:47
|
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= 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) {
__ __ __ __ │ ░░░░
}

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0080, E0308.
For more information about an error, try `rustc --explain E0080`.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
use std::mem::transmute;

fn get_flag<const FlagSet: bool, const ShortName: char>() -> Option<char> {
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const-err4.rs:8:11
--> $DIR/const-err4.rs:9:11
|
LL | Boo = [unsafe { Foo { b: () }.a }; 4][3],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
__ __ __ __ │ ░░░░
}

error: aborting due to previous error

14 changes: 14 additions & 0 deletions src/test/ui/consts/const-err4.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const-err4.rs:9:11
|
LL | Boo = [unsafe { Foo { b: () }.a }; 4][3],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
__ __ __ __ __ __ __ __ │ ░░░░░░░░
}

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-err4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#[derive(Copy, Clone)]
union Foo {
a: isize,

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// only-x86_64
// stderr-per-bitwidth

#[repr(C)]
union Nonsense {
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/alloc_intrinsic_uninit.rs:8:1
--> $DIR/alloc_intrinsic_uninit.rs:9:1
|
LL | const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<deref>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc1──╼ │ ╾──╼
}

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/alloc_intrinsic_uninit.rs:9:1
|
LL | const BAR: &i32 = unsafe { &*(intrinsics::const_allocate(4, 4) as *mut i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<deref>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc1────────╼ │ ╾──────╼
}

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
// compile-test
#![feature(core_intrinsics)]
#![feature(const_heap)]
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ref_to_int_match.rs:25:1
--> $DIR/ref_to_int_match.rs:26:1
|
LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc2, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc2──╼ │ ╾──╼
}

error: could not evaluate constant pattern
--> $DIR/ref_to_int_match.rs:7:14
--> $DIR/ref_to_int_match.rs:8:14
|
LL | 10..=BAR => {},
| ^^^

error: could not evaluate constant pattern
--> $DIR/ref_to_int_match.rs:7:14
--> $DIR/ref_to_int_match.rs:8:14
|
LL | 10..=BAR => {},
| ^^^
26 changes: 26 additions & 0 deletions src/test/ui/consts/const-eval/ref_to_int_match.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ref_to_int_match.rs:26:1
|
LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc2, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc2────────╼ │ ╾──────╼
}

error: could not evaluate constant pattern
--> $DIR/ref_to_int_match.rs:8:14
|
LL | 10..=BAR => {},
| ^^^

error: could not evaluate constant pattern
--> $DIR/ref_to_int_match.rs:8:14
|
LL | 10..=BAR => {},
| ^^^

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/ref_to_int_match.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#![feature(const_fn_union)]

fn main() {
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/transmute-const.rs:3:1
--> $DIR/transmute-const.rs:4:1
|
LL | static FOO: bool = unsafe { mem::transmute(3u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x03, but expected a boolean
|
= 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 │ .
}

error: aborting due to previous error

14 changes: 14 additions & 0 deletions src/test/ui/consts/const-eval/transmute-const.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/transmute-const.rs:4:1
|
LL | static FOO: bool = unsafe { mem::transmute(3u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x03, but expected a boolean
|
= 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 │ .
}

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/transmute-const.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
use std::mem;

static FOO: bool = unsafe { mem::transmute(3u8) };
Original file line number Diff line number Diff line change
@@ -5,6 +5,9 @@ LL | const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x00000001 at .<enum-tag>, but expected a valid enum tag
|
= 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 │ ....
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:27:1
@@ -13,6 +16,9 @@ LL | const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc8 at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc8──╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:30:1
@@ -21,6 +27,9 @@ LL | const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc12 at .0.<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc12─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:42:1
@@ -29,6 +38,9 @@ LL | const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x00000000 at .<enum-tag>, but expected a valid enum tag
|
= 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 │ ....
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:44:1
@@ -37,6 +49,9 @@ LL | const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc18 at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc18─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:47:1
@@ -45,6 +60,9 @@ LL | const BAD_ENUM2_WRAPPED: Wrap<Enum2> = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc22 at .0.<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc22─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:56:1
@@ -53,6 +71,9 @@ LL | const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
__ __ __ __ │ ░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:60:1
@@ -61,6 +82,9 @@ LL | const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc28 at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc28─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:77:1
@@ -69,6 +93,9 @@ LL | const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of the never type `!` at .<enum-variant(B)>.0
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
01 │ .
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:79:1
@@ -77,6 +104,9 @@ LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Never at .<enum-variant(D)>.0
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
03 │ .
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:87:1
@@ -85,6 +115,9 @@ LL | const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::tran
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0xffffffff at .<enum-variant(Some)>.0.1, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= 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.......
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:92:1
@@ -93,6 +126,9 @@ LL | const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Never at .<enum-variant(Ok)>.0.1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 4) {
00 00 00 00 00 00 00 00 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:94:1
@@ -101,6 +137,9 @@ LL | const BAD_UNINHABITED_WITH_DATA2: Result<(i32, !), (i32, Never)> = unsafe {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of the never type `!` at .<enum-variant(Ok)>.0.1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 4) {
00 00 00 00 00 00 00 00 │ ........
}

error: aborting due to 13 previous errors

146 changes: 146 additions & 0 deletions src/test/ui/consts/const-eval/ub-enum.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:24:1
|
LL | const BAD_ENUM: Enum = unsafe { mem::transmute(1usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x0000000000000001 at .<enum-tag>, but expected a valid enum tag
|
= 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 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:27:1
|
LL | const BAD_ENUM_PTR: Enum = unsafe { mem::transmute(&1) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc8 at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc8────────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:30:1
|
LL | const BAD_ENUM_WRAPPED: Wrap<Enum> = unsafe { mem::transmute(&1) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc12 at .0.<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc12───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:42:1
|
LL | const BAD_ENUM2: Enum2 = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x0000000000000000 at .<enum-tag>, but expected a valid enum tag
|
= 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 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:44:1
|
LL | const BAD_ENUM2_PTR: Enum2 = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc18 at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc18───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:47:1
|
LL | const BAD_ENUM2_WRAPPED: Wrap<Enum2> = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc22 at .0.<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc22───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:56:1
|
LL | const BAD_ENUM2_UNDEF : Enum2 = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
__ __ __ __ __ __ __ __ │ ░░░░░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:60:1
|
LL | const BAD_ENUM2_OPTION_PTR: Option<Enum2> = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc28 at .<enum-tag>, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc28───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:77:1
|
LL | const BAD_UNINHABITED_VARIANT1: UninhDiscriminant = unsafe { mem::transmute(1u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of the never type `!` at .<enum-variant(B)>.0
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
01 │ .
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:79:1
|
LL | const BAD_UNINHABITED_VARIANT2: UninhDiscriminant = unsafe { mem::transmute(3u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Never at .<enum-variant(D)>.0
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
03 │ .
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:87:1
|
LL | const BAD_OPTION_CHAR: Option<(char, char)> = Some(('x', unsafe { mem::transmute(!0u32) }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0xffffffff at .<enum-variant(Some)>.0.1, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
|
= 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.......
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:92:1
|
LL | const BAD_UNINHABITED_WITH_DATA1: Result<(i32, Never), (i32, !)> = unsafe { mem::transmute(0u64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Never at .<enum-variant(Ok)>.0.1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 4) {
00 00 00 00 00 00 00 00 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum.rs:94:1
|
LL | const BAD_UNINHABITED_WITH_DATA2: Result<(i32, !), (i32, Never)> = unsafe { mem::transmute(0u64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of the never type `!` at .<enum-variant(Ok)>.0.1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 4) {
00 00 00 00 00 00 00 00 │ ........
}

error: aborting due to 13 previous errors

For more information about this error, try `rustc --explain E0080`.
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/ub-enum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// normalize-stderr-64bit "0x0000000000" -> "0x00"
// stderr-per-bitwidth
#![feature(never_type)]
#![allow(const_err)] // make sure we cannot allow away the errors tested here

Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@ LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [0]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 12, align: 4) {
__ __ __ __ 01 00 00 00 02 00 00 00 │ ░░░░........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:23:1
@@ -25,6 +28,9 @@ LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [1]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 12, align: 4) {
00 00 00 00 01 __ 01 01 02 02 __ 02 │ .....░....░.
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:43:1
@@ -39,6 +45,9 @@ LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [2]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 12, align: 4) {
00 00 00 00 01 01 01 01 02 02 02 __ │ ...........░
}

error: aborting due to 3 previous errors

54 changes: 54 additions & 0 deletions src/test/ui/consts/const-eval/ub-int-array.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:14:1
|
LL | / const UNINIT_INT_0: [u32; 3] = unsafe {
LL | |
LL | |
LL | | [
... |
LL | | ]
LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [0]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 12, align: 4) {
__ __ __ __ 01 00 00 00 02 00 00 00 │ ░░░░........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:23:1
|
LL | / const UNINIT_INT_1: [u32; 3] = unsafe {
LL | |
LL | |
LL | | mem::transmute(
... |
LL | | )
LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [1]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 12, align: 4) {
00 00 00 00 01 __ 01 01 02 02 __ 02 │ .....░....░.
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-int-array.rs:43:1
|
LL | / const UNINIT_INT_2: [u32; 3] = unsafe {
LL | |
LL | |
LL | | mem::transmute(
... |
LL | | )
LL | | };
| |__^ type validation failed: encountered uninitialized bytes at [2]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 12, align: 4) {
00 00 00 00 01 01 01 01 02 02 02 __ │ ...........░
}

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0080`.
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/ub-int-array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(const_err)] // make sure we cannot allow away the errors tested here

// stderr-per-bitwidth
//! Test the "array of int" fast path in validity checking, and in particular whether it
//! points at the right array element.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:11:1
--> $DIR/ub-nonnull.rs:12:1
|
LL | const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
00 00 00 00 │ ....
}

error: any use of this value will cause an error
--> $DIR/ub-nonnull.rs:18:30
--> $DIR/ub-nonnull.rs:19:30
|
LL | / const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
LL | | let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
@@ -20,52 +23,67 @@ LL | | } };
| |____-
|
note: the lint level is defined here
--> $DIR/ub-nonnull.rs:14:8
--> $DIR/ub-nonnull.rs:15:8
|
LL | #[deny(const_err)] // this triggers a `const_err` so validation does not even happen
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:23:1
--> $DIR/ub-nonnull.rs:24:1
|
LL | const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
00 │ .
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:25:1
--> $DIR/ub-nonnull.rs:26:1
|
LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
00 00 00 00 │ ....
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:33:1
--> $DIR/ub-nonnull.rs:34:1
|
LL | const UNINIT: NonZeroU8 = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .0, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
__ │ ░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:41:1
--> $DIR/ub-nonnull.rs:42:1
|
LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 10..=30
|
= 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) {
2a 00 00 00 │ *...
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:47:1
--> $DIR/ub-nonnull.rs:48:1
|
LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 20, but expected something less or equal to 10, or greater or equal to 30
|
= 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) {
14 00 00 00 │ ....
}

error: aborting due to 7 previous errors

90 changes: 90 additions & 0 deletions src/test/ui/consts/const-eval/ub-nonnull.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:12:1
|
LL | const NULL_PTR: NonNull<u8> = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
00 00 00 00 00 00 00 00 │ ........
}

error: any use of this value will cause an error
--> $DIR/ub-nonnull.rs:19:30
|
LL | / const OUT_OF_BOUNDS_PTR: NonNull<u8> = { unsafe {
LL | | let ptr: &[u8; 256] = mem::transmute(&0u8); // &0 gets promoted so it does not dangle
LL | | // Use address-of-element for pointer arithmetic. This could wrap around to NULL!
LL | | let out_of_bounds_ptr = &ptr[255];
| | ^^^^^^^^ memory access failed: pointer must be in-bounds at offset 256, but is outside bounds of alloc10 which has size 1
LL | |
LL | | mem::transmute(out_of_bounds_ptr)
LL | | } };
| |____-
|
note: the lint level is defined here
--> $DIR/ub-nonnull.rs:15:8
|
LL | #[deny(const_err)] // this triggers a `const_err` so validation does not even happen
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:24:1
|
LL | const NULL_U8: NonZeroU8 = unsafe { mem::transmute(0u8) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
00 │ .
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:26:1
|
LL | const NULL_USIZE: NonZeroUsize = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0, but expected something greater or equal to 1
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
00 00 00 00 00 00 00 00 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:34:1
|
LL | const UNINIT: NonZeroU8 = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes at .0, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
__ │ ░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:42:1
|
LL | const BAD_RANGE1: RestrictedRange1 = unsafe { RestrictedRange1(42) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 42, but expected something in the range 10..=30
|
= 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) {
2a 00 00 00 │ *...
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-nonnull.rs:48:1
|
LL | const BAD_RANGE2: RestrictedRange2 = unsafe { RestrictedRange2(20) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 20, but expected something less or equal to 10, or greater or equal to 30
|
= 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) {
14 00 00 00 │ ....
}

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/ub-nonnull.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#![feature(rustc_attrs)]
#![allow(const_err, invalid_value)] // make sure we cannot allow away the errors tested here

Original file line number Diff line number Diff line change
@@ -1,90 +1,123 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:12:1
--> $DIR/ub-ref-ptr.rs:13:1
|
LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc2──╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:16:1
--> $DIR/ub-ref-ptr.rs:17:1
|
LL | const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc6──╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:20:1
--> $DIR/ub-ref-ptr.rs:21:1
|
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL reference
|
= 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 │ ....
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:23:1
--> $DIR/ub-ref-ptr.rs:24:1
|
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL box
|
= 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 │ ....
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:29:1
--> $DIR/ub-ref-ptr.rs:30:1
|
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc14, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc14─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:32:1
--> $DIR/ub-ref-ptr.rs:33:1
|
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc20─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:35:1
--> $DIR/ub-ref-ptr.rs:36:1
|
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
╾─alloc25─╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:38:1
--> $DIR/ub-ref-ptr.rs:39:1
|
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (created from integer)
|
= 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...
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:41:1
--> $DIR/ub-ref-ptr.rs:42:1
|
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (created from integer)
|
= 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...
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:44:1
--> $DIR/ub-ref-ptr.rs:45:1
|
LL | const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
|
= 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) {
__ __ __ __ │ ░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:46:1
--> $DIR/ub-ref-ptr.rs:47:1
|
LL | const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a function pointer
|
= 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) {
__ __ __ __ │ ░░░░
}

error: aborting due to 11 previous errors

124 changes: 124 additions & 0 deletions src/test/ui/consts/const-eval/ub-ref-ptr.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:13:1
|
LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc2────────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:17:1
|
LL | const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc6────────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:21:1
|
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL reference
|
= 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 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:24:1
|
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL box
|
= 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 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:30:1
|
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc14, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc14───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:33:1
|
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc20───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:36:1
|
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
╾───────alloc25───────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:39:1
|
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (created from integer)
|
= 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.......
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:42:1
|
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (created from integer)
|
= 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.......
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:45:1
|
LL | const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
|
= 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) {
__ __ __ __ __ __ __ __ │ ░░░░░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:47:1
|
LL | const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a function pointer
|
= 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) {
__ __ __ __ __ __ __ __ │ ░░░░░░░░
}

error: aborting due to 11 previous errors

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/ub-ref-ptr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ignore-tidy-linelength
// stderr-per-bitwidth
#![allow(const_err, invalid_value)] // make sure we cannot allow away the errors tested here

use std::mem;
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-uninhabit.rs:14:1
--> $DIR/ub-uninhabit.rs:15:1
|
LL | const BAD_BAD_BAD: Bar = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Bar
|
= 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: 0, align: 1) {}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-uninhabit.rs:17:1
--> $DIR/ub-uninhabit.rs:18:1
|
LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Bar at .<deref>
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 4, align: 4) {
01 00 00 00 │ ....
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-uninhabit.rs:20:1
--> $DIR/ub-uninhabit.rs:21:1
|
LL | const BAD_BAD_ARRAY: [Bar; 1] = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Bar at [0]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 0, align: 1) {}

error: aborting due to 3 previous errors

32 changes: 32 additions & 0 deletions src/test/ui/consts/const-eval/ub-uninhabit.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-uninhabit.rs:15:1
|
LL | const BAD_BAD_BAD: Bar = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Bar
|
= 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: 0, align: 1) {}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-uninhabit.rs:18:1
|
LL | const BAD_BAD_REF: &Bar = unsafe { mem::transmute(1usize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Bar at .<deref>
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
01 00 00 00 00 00 00 00 │ ........
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-uninhabit.rs:21:1
|
LL | const BAD_BAD_ARRAY: [Bar; 1] = unsafe { MaybeUninit { uninit: () }.init };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Bar at [0]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 0, align: 1) {}

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/ub-uninhabit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#![allow(const_err)] // make sure we cannot allow away the errors tested here

use std::mem;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-upvars.rs:5:1
--> $DIR/ub-upvars.rs:6:1
|
LL | / const BAD_UPVAR: &dyn FnOnce() = &{
LL | | let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) };
@@ -9,6 +9,9 @@ LL | | };
| |__^ type validation failed: encountered a NULL reference at .<deref>.<dyn-downcast>.<captured-var(bad_ref)>
|
= 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──╼ │ ╾──╼╾──╼
}

error: aborting due to previous error

18 changes: 18 additions & 0 deletions src/test/ui/consts/const-eval/ub-upvars.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-upvars.rs:6:1
|
LL | / const BAD_UPVAR: &dyn FnOnce() = &{
LL | | let bad_ref: &'static u16 = unsafe { mem::transmute(0usize) };
LL | | let another_var = 13;
LL | | move || { let _ = bad_ref; let _ = another_var; }
LL | | };
| |__^ type validation failed: encountered a NULL reference at .<deref>.<dyn-downcast>.<captured-var(bad_ref)>
|
= 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────────╼ │ ╾──────╼╾──────╼
}

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/ub-upvars.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#![allow(const_err, invalid_value)] // make sure we cannot allow away the errors tested here

use std::mem;

Large diffs are not rendered by default.

309 changes: 309 additions & 0 deletions src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/ub-wide-ptr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
// ignore-tidy-linelength
#![allow(unused)]
#![allow(const_err)] // make sure we cannot allow away the errors tested here
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/union-const-eval-field.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// only-x86_64
#![feature(const_fn)]

type Field1 = i32;
5 changes: 4 additions & 1 deletion src/test/ui/consts/const-eval/union-const-eval-field.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/union-const-eval-field.rs:28:5
--> $DIR/union-const-eval-field.rs:29:5
|
LL | const FIELD3: Field3 = unsafe { UNION.field3 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
__ __ __ __ __ __ __ __ │ ░░░░░░░░
}

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/union-ice.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// only-x86_64
#![feature(const_fn)]

type Field1 = i32;
17 changes: 14 additions & 3 deletions src/test/ui/consts/const-eval/union-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ice.rs:14:1
--> $DIR/union-ice.rs:15:1
|
LL | const FIELD3: Field3 = unsafe { UNION.field3 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 8, align: 8) {
__ __ __ __ __ __ __ __ │ ░░░░░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ice.rs:16:1
--> $DIR/union-ice.rs:17:1
|
LL | / const FIELD_PATH: Struct = Struct {
LL | | a: 42,
@@ -16,9 +19,12 @@ LL | | };
| |__^ type validation failed: encountered uninitialized bytes at .b, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 16, align: 8) {
__ __ __ __ __ __ __ __ 2a __ __ __ __ __ __ __ │ ░░░░░░░░*░░░░░░░
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ice.rs:26:1
--> $DIR/union-ice.rs:27:1
|
LL | / const FIELD_PATH2: Struct2 = Struct2 {
LL | | b: [
@@ -30,6 +36,11 @@ LL | | };
| |__^ type validation failed: encountered uninitialized bytes at .b[1]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 40, align: 8) {
0x00 │ 15 00 00 00 00 00 00 00 __ __ __ __ __ __ __ __ │ ........░░░░░░░░
0x10 │ 17 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 │ ................
0x20 │ 2a __ __ __ __ __ __ __ │ *░░░░░░░
}

error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ub.rs:32:1
--> $DIR/union-ub.rs:33:1
|
LL | const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x2a, but expected a boolean
|
= 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) {
2a │ *
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ub.rs:34:1
--> $DIR/union-ub.rs:35:1
|
LL | const UNINIT_BOOL: bool = unsafe { DummyUnion { unit: () }.bool};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a boolean
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
__ │ ░
}

error: aborting due to 2 previous errors

25 changes: 25 additions & 0 deletions src/test/ui/consts/const-eval/union-ub.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ub.rs:33:1
|
LL | const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x2a, but expected a boolean
|
= 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) {
2a │ *
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ub.rs:35:1
|
LL | const UNINIT_BOOL: bool = unsafe { DummyUnion { unit: () }.bool};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a boolean
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
__ │ ░
}

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/union-ub.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#![allow(const_err)] // make sure we cannot allow away the errors tested here

#[repr(C)]
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
warning: any use of this value will cause an error
--> $DIR/validate_uninhabited_zsts.rs:5:14
--> $DIR/validate_uninhabited_zsts.rs:6:14
|
LL | unsafe { std::mem::transmute(()) }
| ^^^^^^^^^^^^^^^^^^^^^^^
| |
| transmuting to uninhabited type
| inside `foo` at $DIR/validate_uninhabited_zsts.rs:5:14
| inside `FOO` at $DIR/validate_uninhabited_zsts.rs:15:26
| inside `foo` at $DIR/validate_uninhabited_zsts.rs:6:14
| inside `FOO` at $DIR/validate_uninhabited_zsts.rs:16:26
...
LL | const FOO: [Empty; 3] = [foo(); 3];
| -----------------------------------
|
note: the lint level is defined here
--> $DIR/validate_uninhabited_zsts.rs:14:8
--> $DIR/validate_uninhabited_zsts.rs:15:8
|
LL | #[warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: it is undefined behavior to use this value
--> $DIR/validate_uninhabited_zsts.rs:18:1
--> $DIR/validate_uninhabited_zsts.rs:19:1
|
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Empty at [0]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 0, align: 1) {}

warning: the type `!` does not permit zero-initialization
--> $DIR/validate_uninhabited_zsts.rs:5:14
--> $DIR/validate_uninhabited_zsts.rs:6:14
|
LL | unsafe { std::mem::transmute(()) }
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -40,7 +41,7 @@ LL | unsafe { std::mem::transmute(()) }
= note: the `!` type has no valid value

warning: the type `Empty` does not permit zero-initialization
--> $DIR/validate_uninhabited_zsts.rs:18:35
--> $DIR/validate_uninhabited_zsts.rs:19:35
|
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
| ^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
warning: any use of this value will cause an error
--> $DIR/validate_uninhabited_zsts.rs:6:14
|
LL | unsafe { std::mem::transmute(()) }
| ^^^^^^^^^^^^^^^^^^^^^^^
| |
| transmuting to uninhabited type
| inside `foo` at $DIR/validate_uninhabited_zsts.rs:6:14
| inside `FOO` at $DIR/validate_uninhabited_zsts.rs:16:26
...
LL | const FOO: [Empty; 3] = [foo(); 3];
| -----------------------------------
|
note: the lint level is defined here
--> $DIR/validate_uninhabited_zsts.rs:15:8
|
LL | #[warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error[E0080]: it is undefined behavior to use this value
--> $DIR/validate_uninhabited_zsts.rs:19:1
|
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of uninhabited type Empty at [0]
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 0, align: 1) {}

warning: the type `!` does not permit zero-initialization
--> $DIR/validate_uninhabited_zsts.rs:6:14
|
LL | unsafe { std::mem::transmute(()) }
| ^^^^^^^^^^^^^^^^^^^^^^^
| |
| this code causes undefined behavior when executed
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
|
= note: `#[warn(invalid_value)]` on by default
= note: the `!` type has no valid value

warning: the type `Empty` does not permit zero-initialization
--> $DIR/validate_uninhabited_zsts.rs:19:35
|
LL | const BAR: [Empty; 3] = [unsafe { std::mem::transmute(()) }; 3];
| ^^^^^^^^^^^^^^^^^^^^^^^
| |
| this code causes undefined behavior when executed
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
|
= note: enums with no variants have no valid value

error: aborting due to previous error; 3 warnings emitted

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/const-eval/validate_uninhabited_zsts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// stderr-per-bitwidth
#![feature(const_fn)]
#![feature(const_fn_transmute)]

Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const-points-to-static.rs:5:1
--> $DIR/const-points-to-static.rs:6:1
|
LL | const TEST: &u8 = &MY_STATIC;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a reference pointing to a static variable
|
= 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) {
╾─alloc0──╼ │ ╾──╼
}

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/const-points-to-static.rs:5:20
--> $DIR/const-points-to-static.rs:6:20
|
LL | const TEST: &u8 = &MY_STATIC;
| ^^^^^^^^^
22 changes: 22 additions & 0 deletions src/test/ui/consts/const-points-to-static.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const-points-to-static.rs:6:1
|
LL | const TEST: &u8 = &MY_STATIC;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a reference pointing to a static variable
|
= 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────────╼ │ ╾──────╼
}

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/const-points-to-static.rs:6:20
|
LL | const TEST: &u8 = &MY_STATIC;
| ^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0080`.
4 changes: 3 additions & 1 deletion src/test/ui/consts/const-points-to-static.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// compile-flags: -Zunleash-the-miri-inside-of-you
// stderr-per-bitwidth

#![allow(dead_code)]

const TEST: &u8 = &MY_STATIC;
//~^ ERROR it is undefined behavior to use this value
//~| NOTE encountered a reference pointing to a static variable
//~| NOTE
//~| NOTE undefined behavior
//~| NOTE the raw bytes of the constant

static MY_STATIC: u8 = 4;

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/issue-63952.rs:16:1
--> $DIR/issue-63952.rs:17:1
|
LL | / const SLICE_WAY_TOO_LONG: &[u8] = unsafe {
LL | | SliceTransmute {
@@ -11,6 +11,9 @@ LL | | };
| |__^ type validation failed: encountered invalid reference metadata: slice is bigger than largest supported object
|
= 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) {
╾─alloc3──╼ ff ff ff ff │ ╾──╼....
}

error: aborting due to previous error

20 changes: 20 additions & 0 deletions src/test/ui/consts/issue-63952.64bit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/issue-63952.rs:17:1
|
LL | / const SLICE_WAY_TOO_LONG: &[u8] = unsafe {
LL | | SliceTransmute {
LL | | repr: SliceRepr {
LL | | ptr: &42,
... |
LL | | .slice
LL | | };
| |__^ type validation failed: encountered invalid reference metadata: slice is bigger than largest supported object
|
= 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) {
╾───────alloc3────────╼ ff ff ff ff ff ff ff ff │ ╾──────╼........
}

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
1 change: 1 addition & 0 deletions src/test/ui/consts/issue-63952.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Regression test for #63952, shouldn't hang.
// stderr-per-bitwidth

#[repr(C)]
#[derive(Copy, Clone)]
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/issue-79690.rs:29:1
--> $DIR/issue-79690.rs:30:1
|
LL | const G: Fat = unsafe { Transmute { t: FOO }.u };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered (potentially part of) a pointer at .1.<deref>.size.foo, but expected plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 16, align: 8) {
╾───────alloc2────────╼ ╾───────alloc3────────╼ │ ╾──────╼╾──────╼
}

error: aborting due to previous error

1 change: 1 addition & 0 deletions src/test/ui/consts/issue-79690.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore-32bit
// This test gives a different error on 32-bit architectures.
// stderr-per-bitwidth

union Transmute<T: Copy, U: Copy> {
t: T,
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static2.rs:10:1
--> $DIR/const_refers_to_static2.rs:11:1
|
LL | / const REF_INTERIOR_MUT: &usize = {
LL | |
LL | |
LL | |
LL | | static FOO: AtomicUsize = AtomicUsize::new(0);
LL | | unsafe { &*(&FOO as *const _ as *const usize) }
LL | | };
| |__^ type validation failed: encountered a reference pointing to a static variable
|
= 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) {
╾─alloc0──╼ │ ╾──╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static2.rs:18:1
--> $DIR/const_refers_to_static2.rs:20:1
|
LL | / const READ_IMMUT: &usize = {
LL | |
LL | |
LL | |
LL | | static FOO: usize = 0;
LL | | &FOO
LL | | };
| |__^ type validation failed: encountered a reference pointing to a static variable
|
= 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) {
╾─alloc1──╼ │ ╾──╼
}

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static2.rs:14:18
--> $DIR/const_refers_to_static2.rs:16:18
|
LL | unsafe { &*(&FOO as *const _ as *const usize) }
| ^^^
help: skipping check for `const_raw_ptr_deref` feature
--> $DIR/const_refers_to_static2.rs:14:14
--> $DIR/const_refers_to_static2.rs:16:14
|
LL | unsafe { &*(&FOO as *const _ as *const usize) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static2.rs:22:6
--> $DIR/const_refers_to_static2.rs:25:6
|
LL | &FOO
| ^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static2.rs:11:1
|
LL | / const REF_INTERIOR_MUT: &usize = {
LL | |
LL | |
LL | |
LL | | static FOO: AtomicUsize = AtomicUsize::new(0);
LL | | unsafe { &*(&FOO as *const _ as *const usize) }
LL | | };
| |__^ type validation failed: encountered a reference pointing to a static variable
|
= 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────────╼ │ ╾──────╼
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static2.rs:20:1
|
LL | / const READ_IMMUT: &usize = {
LL | |
LL | |
LL | |
LL | | static FOO: usize = 0;
LL | | &FOO
LL | | };
| |__^ type validation failed: encountered a reference pointing to a static variable
|
= 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) {
╾───────alloc1────────╼ │ ╾──────╼
}

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static2.rs:16:18
|
LL | unsafe { &*(&FOO as *const _ as *const usize) }
| ^^^
help: skipping check for `const_raw_ptr_deref` feature
--> $DIR/const_refers_to_static2.rs:16:14
|
LL | unsafe { &*(&FOO as *const _ as *const usize) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static2.rs:25:6
|
LL | &FOO
| ^^^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0080`.
7 changes: 5 additions & 2 deletions src/test/ui/consts/miri_unleashed/const_refers_to_static2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// compile-flags: -Zunleash-the-miri-inside-of-you
// stderr-per-bitwidth
#![allow(const_err)]

use std::sync::atomic::AtomicUsize;
@@ -9,15 +10,17 @@ use std::sync::atomic::Ordering;

const REF_INTERIOR_MUT: &usize = { //~ ERROR undefined behavior to use this value
//~| NOTE encountered a reference pointing to a static variable
//~| NOTE
//~| NOTE undefined behavior
//~| NOTE the raw bytes of the constant
static FOO: AtomicUsize = AtomicUsize::new(0);
unsafe { &*(&FOO as *const _ as *const usize) }
};

// ok some day perhaps
const READ_IMMUT: &usize = { //~ ERROR it is undefined behavior to use this value
//~| NOTE encountered a reference pointing to a static variable
//~| NOTE
//~| NOTE undefined behavior
//~| NOTE the raw bytes of the constant
static FOO: usize = 0;
&FOO
};
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static_cross_crate.rs:11:1
--> $DIR/const_refers_to_static_cross_crate.rs:12:1
|
LL | / const SLICE_MUT: &[u8; 1] = {
LL | |
LL | |
LL | |
LL | | unsafe { &static_cross_crate::ZERO }
LL | | };
| |__^ type validation failed: encountered a reference pointing to a static variable
|
= 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) {
╾─alloc0──╼ │ ╾──╼
}

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:44:9
--> $DIR/const_refers_to_static_cross_crate.rs:47:9
|
LL | SLICE_MUT => true,
| ^^^^^^^^^

error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static_cross_crate.rs:17:1
--> $DIR/const_refers_to_static_cross_crate.rs:19:1
|
LL | / const U8_MUT: &u8 = {
LL | |
LL | |
LL | |
LL | | unsafe { &static_cross_crate::ZERO[0] }
LL | | };
| |__^ type validation failed: encountered a reference pointing to a static variable
|
= 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) {
╾─alloc0──╼ │ ╾──╼
}

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:53:9
--> $DIR/const_refers_to_static_cross_crate.rs:56:9
|
LL | U8_MUT => true,
| ^^^^^^

warning: any use of this value will cause an error
--> $DIR/const_refers_to_static_cross_crate.rs:26:15
--> $DIR/const_refers_to_static_cross_crate.rs:29:15
|
LL | / const U8_MUT2: &u8 = {
LL | | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
@@ -48,21 +56,21 @@ LL | | };
| |__-
|
note: the lint level is defined here
--> $DIR/const_refers_to_static_cross_crate.rs:24:8
--> $DIR/const_refers_to_static_cross_crate.rs:27:8
|
LL | #[warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:64:9
--> $DIR/const_refers_to_static_cross_crate.rs:67:9
|
LL | U8_MUT2 => true,
| ^^^^^^^

warning: any use of this value will cause an error
--> $DIR/const_refers_to_static_cross_crate.rs:34:51
--> $DIR/const_refers_to_static_cross_crate.rs:37:51
|
LL | / const U8_MUT3: &u8 = {
LL | | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
@@ -75,97 +83,97 @@ LL | | };
| |__-
|
note: the lint level is defined here
--> $DIR/const_refers_to_static_cross_crate.rs:32:8
--> $DIR/const_refers_to_static_cross_crate.rs:35:8
|
LL | #[warn(const_err)]
| ^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:72:9
--> $DIR/const_refers_to_static_cross_crate.rs:75:9
|
LL | U8_MUT3 => true,
| ^^^^^^^

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:44:9
--> $DIR/const_refers_to_static_cross_crate.rs:47:9
|
LL | SLICE_MUT => true,
| ^^^^^^^^^

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:53:9
--> $DIR/const_refers_to_static_cross_crate.rs:56:9
|
LL | U8_MUT => true,
| ^^^^^^

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:64:9
--> $DIR/const_refers_to_static_cross_crate.rs:67:9
|
LL | U8_MUT2 => true,
| ^^^^^^^

error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:72:9
--> $DIR/const_refers_to_static_cross_crate.rs:75:9
|
LL | U8_MUT3 => true,
| ^^^^^^^

warning: skipping const checks
|
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:14:15
--> $DIR/const_refers_to_static_cross_crate.rs:16:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:14:15
--> $DIR/const_refers_to_static_cross_crate.rs:16:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:20:15
--> $DIR/const_refers_to_static_cross_crate.rs:23:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:20:15
--> $DIR/const_refers_to_static_cross_crate.rs:23:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:20:15
--> $DIR/const_refers_to_static_cross_crate.rs:23:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:26:17
--> $DIR/const_refers_to_static_cross_crate.rs:29:17
|
LL | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:34:20
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
|
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:34:20
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
|
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:34:20
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
|
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_panic` feature
--> $DIR/const_refers_to_static_cross_crate.rs:34:77
--> $DIR/const_refers_to_static_cross_crate.rs:37:77
|
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| ^^^^^^^^
help: skipping check that does not even have a feature gate
--> $DIR/const_refers_to_static_cross_crate.rs:34:20
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
|
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Loading