about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2024-02-16 09:58:53 +0100
committerRalf Jung <post@ralfj.de>2024-02-16 10:09:12 +0100
commit0702701297d5ec7b49dd2debbd777531676c48e2 (patch)
tree218d5fad8f8c3a82fef9b5e4f2721836b5575fd9 /tests
parent0f806a9812b62c36bdab08d33c14cf2d3ecf4355 (diff)
downloadrust-0702701297d5ec7b49dd2debbd777531676c48e2.tar.gz
rust-0702701297d5ec7b49dd2debbd777531676c48e2.zip
allow mutable references in const values when they point to no memory
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/consts/const-mut-refs/const_mut_refs.rs6
-rw-r--r--tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.32bit.stderr17
-rw-r--r--tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.64bit.stderr17
-rw-r--r--tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs17
4 files changed, 35 insertions, 22 deletions
diff --git a/tests/ui/consts/const-mut-refs/const_mut_refs.rs b/tests/ui/consts/const-mut-refs/const_mut_refs.rs
index 544458dfcd8..96321a1defd 100644
--- a/tests/ui/consts/const-mut-refs/const_mut_refs.rs
+++ b/tests/ui/consts/const-mut-refs/const_mut_refs.rs
@@ -1,6 +1,8 @@
 // check-pass
 #![feature(const_mut_refs)]
 
+use std::sync::Mutex;
+
 struct Foo {
     x: usize
 }
@@ -28,6 +30,10 @@ const fn bazz(foo: &mut Foo) -> usize {
     foo.x
 }
 
+// Empty slices get promoted so this passes the static checks.
+// Make sure it also passes the dynamic checks.
+static MUTABLE_REFERENCE_HOLDER: Mutex<&mut [u8]> = Mutex::new(&mut []);
+
 fn main() {
     let _: [(); foo().bar()] = [(); 1];
     let _: [(); baz(&mut foo())] = [(); 2];
diff --git a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.32bit.stderr b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.32bit.stderr
index 87420a03751..acb6f121bbf 100644
--- a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.32bit.stderr
+++ b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.32bit.stderr
@@ -1,19 +1,24 @@
 error[E0080]: it is undefined behavior to use this value
-  --> $DIR/mut_ref_in_final_dynamic_check.rs:15:1
+  --> $DIR/mut_ref_in_final_dynamic_check.rs:16:1
    |
 LL | const A: Option<&mut i32> = helper();
    | ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.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: 4, align: 4) {
-               2a 00 00 00                                     │ *...
+               ╾ALLOC0╼                                     │ ╾──╼
            }
 
-error: encountered dangling pointer in final value of constant
-  --> $DIR/mut_ref_in_final_dynamic_check.rs:22:1
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/mut_ref_in_final_dynamic_check.rs:18:1
+   |
+LL | static A_STATIC: Option<&mut i32> = helper();
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered mutable reference in a `const` or `static`
    |
-LL | const B: Option<&mut i32> = helper2();
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^
+   = 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: aborting due to 2 previous errors
 
diff --git a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.64bit.stderr b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.64bit.stderr
index fc68207512c..6dc7d851391 100644
--- a/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.64bit.stderr
+++ b/tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.64bit.stderr
@@ -1,19 +1,24 @@
 error[E0080]: it is undefined behavior to use this value
-  --> $DIR/mut_ref_in_final_dynamic_check.rs:15:1
+  --> $DIR/mut_ref_in_final_dynamic_check.rs:16:1
    |
 LL | const A: Option<&mut i32> = helper();
    | ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.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: 8, align: 8) {
-               2a 00 00 00 00 00 00 00                         │ *.......
+               ╾ALLOC0╼                         │ ╾──────╼
            }
 
-error: encountered dangling pointer in final value of constant
-  --> $DIR/mut_ref_in_final_dynamic_check.rs:22:1
+error[E0080]: it is undefined behavior to use this value
+  --> $DIR/mut_ref_in_final_dynamic_check.rs:18:1
+   |
+LL | static A_STATIC: Option<&mut i32> = helper();
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered mutable reference in a `const` or `static`
    |
-LL | const B: Option<&mut i32> = helper2();
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^
+   = 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: aborting due to 2 previous errors
 
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 455b557b97c..26b4796c6c4 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
@@ -1,5 +1,5 @@
 // stderr-per-bitwidth
-#![feature(const_mut_refs)]
+#![feature(const_mut_refs, const_refs_to_static)]
 #![feature(raw_ref_op)]
 
 // This file checks that our dynamic checks catch things that the static checks miss.
@@ -8,17 +8,14 @@
 // do that without causing the borrow checker to complain (see the B4/helper test in
 // mut_ref_in_final.rs).
 
+static mut BUFFER: i32 = 42;
+
 const fn helper() -> Option<&'static mut i32> { unsafe {
-    // Undefined behaviour (integer as pointer), who doesn't love tests like this.
-    Some(&mut *(42 as *mut i32))
+    Some(&mut *std::ptr::addr_of_mut!(BUFFER))
 } }
 const A: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value
-//~^ encountered mutable reference in a `const`
-
-const fn helper2() -> Option<&'static mut i32> { unsafe {
-    // Undefined behaviour (dangling pointer), who doesn't love tests like this.
-    Some(&mut *(&mut 42 as *mut i32))
-} }
-const B: Option<&mut i32> = helper2(); //~ ERROR encountered dangling pointer in final value of constant
+//~^ encountered mutable reference
+static A_STATIC: Option<&mut i32> = helper(); //~ ERROR it is undefined behavior to use this value
+//~^ encountered mutable reference
 
 fn main() {}