From 2a2237e0aad6aeab86c81d704193558f1298c977 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 16 Feb 2024 10:06:23 +0100 Subject: [PATCH] add back some more attempts at having &mut in the final value of a const/static --- .../mut_ref_in_final_dynamic_check.rs | 20 ++++++++- .../mut_ref_in_final_dynamic_check.stderr | 44 ++++++++++++++++--- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs index 081353a47c872..331c2de55cc4d 100644 --- a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs +++ b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs @@ -14,9 +14,25 @@ static mut BUFFER: i32 = 42; const fn helper() -> Option<&'static mut i32> { unsafe { Some(&mut *std::ptr::addr_of_mut!(BUFFER)) } } -const A: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value +const MUT: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value //~^ encountered mutable reference -static A_STATIC: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value +static MUT_STATIC: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value //~^ encountered mutable reference +const fn helper_int2ptr() -> Option<&'static mut i32> { unsafe { + // Undefined behaviour (integer as pointer), who doesn't love tests like this. + Some(&mut *(42 as *mut i32)) +} } +const INT2PTR: Option<&mut i32> = helper_int2ptr(); //~ ERROR it is undefined behavior to use this value +//~^ encountered a dangling reference +static INT2PTR_STATIC: Option<&mut i32> = helper_int2ptr(); //~ ERROR it is undefined behavior to use this value +//~^ encountered a dangling reference + +const fn helper_dangling() -> Option<&'static mut i32> { unsafe { + // Undefined behaviour (dangling pointer), who doesn't love tests like this. + Some(&mut *(&mut 42 as *mut i32)) +} } +const DANGLING: Option<&mut i32> = helper_dangling(); //~ ERROR encountered dangling pointer +static DANGLING_STATIC: Option<&mut i32> = helper_dangling(); //~ ERROR encountered dangling pointer + fn main() {} diff --git a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr index 8625c445f99b4..a7cd32a7c5f0b 100644 --- a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr +++ b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.stderr @@ -1,8 +1,8 @@ error[E0080]: it is undefined behavior to use this value --> $DIR/mut_ref_in_final_dynamic_check.rs:17:1 | -LL | const A: Option<&mut i32> = helper(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at ..0: encountered mutable reference in a `const` or `static` +LL | const MUT: Option<&mut i32> = helper(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at ..0: encountered mutable reference in a `const` or `static` | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { @@ -12,14 +12,48 @@ LL | const A: Option<&mut i32> = helper(); error[E0080]: it is undefined behavior to use this value --> $DIR/mut_ref_in_final_dynamic_check.rs:19:1 | -LL | static A_STATIC: Option<&mut i32> = helper(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at ..0: encountered mutable reference in a `const` or `static` +LL | static MUT_STATIC: Option<&mut i32> = helper(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at ..0: encountered mutable reference in a `const` or `static` | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { HEX_DUMP } -error: aborting due to 2 previous errors +error[E0080]: it is undefined behavior to use this value + --> $DIR/mut_ref_in_final_dynamic_check.rs:26:1 + | +LL | const INT2PTR: Option<&mut i32> = helper_int2ptr(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at ..0: encountered a dangling reference (0x2a[noalloc] has no provenance) + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error[E0080]: it is undefined behavior to use this value + --> $DIR/mut_ref_in_final_dynamic_check.rs:28:1 + | +LL | static INT2PTR_STATIC: Option<&mut i32> = helper_int2ptr(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at ..0: encountered a dangling reference (0x2a[noalloc] has no provenance) + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error: encountered dangling pointer in final value of constant + --> $DIR/mut_ref_in_final_dynamic_check.rs:35:1 + | +LL | const DANGLING: Option<&mut i32> = helper_dangling(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: encountered dangling pointer in final value of static + --> $DIR/mut_ref_in_final_dynamic_check.rs:36:1 + | +LL | static DANGLING_STATIC: Option<&mut i32> = helper_dangling(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0080`.