allow static_mut_ref in some tests that specifically test mutable statics

This commit is contained in:
Ralf Jung 2024-02-12 15:14:58 +01:00
parent b381d3ab27
commit 6fe4d66e64
15 changed files with 30 additions and 212 deletions

View File

@ -1,6 +1,7 @@
// normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
// normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
#![feature(const_refs_to_static)]
#![allow(static_mut_ref)]
fn invalid() {
static S: i8 = 10;
@ -38,7 +39,6 @@ fn mutable() {
const C: &i32 = unsafe { &S_MUT };
//~^ERROR: undefined behavior
//~| encountered reference to mutable memory
//~| WARN shared reference of mutable static is discouraged
// This *must not build*, the constant we are matching against
// could change its value!

View File

@ -1,20 +1,5 @@
warning: shared reference of mutable static is discouraged
--> $DIR/const_refs_to_static_fail_invalid.rs:38:30
|
LL | const C: &i32 = unsafe { &S_MUT };
| ^^^^^^ shared reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
LL | const C: &i32 = unsafe { addr_of!(S_MUT) };
| ~~~~~~~~~~~~~~~
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refs_to_static_fail_invalid.rs:8:5
--> $DIR/const_refs_to_static_fail_invalid.rs:9:5
|
LL | const C: &bool = unsafe { std::mem::transmute(&S) };
| ^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered 0x0a, but expected a boolean
@ -25,13 +10,13 @@ LL | const C: &bool = unsafe { std::mem::transmute(&S) };
}
error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:14:9
--> $DIR/const_refs_to_static_fail_invalid.rs:15:9
|
LL | C => {}
| ^
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refs_to_static_fail_invalid.rs:24:5
--> $DIR/const_refs_to_static_fail_invalid.rs:25:5
|
LL | const C: &i8 = unsafe { &S };
| ^^^^^^^^^^^^ constructing invalid value: encountered reference to `extern` static in `const`
@ -42,13 +27,13 @@ LL | const C: &i8 = unsafe { &S };
}
error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:30:9
--> $DIR/const_refs_to_static_fail_invalid.rs:31:9
|
LL | C => {}
| ^
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refs_to_static_fail_invalid.rs:38:5
--> $DIR/const_refs_to_static_fail_invalid.rs:39:5
|
LL | const C: &i32 = unsafe { &S_MUT };
| ^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const`
@ -64,6 +49,6 @@ error: could not evaluate constant pattern
LL | C => {},
| ^
error: aborting due to 6 previous errors; 1 warning emitted
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0080`.

View File

@ -1,9 +1,10 @@
#![allow(static_mut_ref)]
const C1: &'static mut [usize] = &mut [];
//~^ ERROR: mutable references are not allowed
static mut S: usize = 3;
const C2: &'static mut usize = unsafe { &mut S };
//~^ ERROR: referencing statics in constants
//~| WARN mutable reference of mutable static is discouraged [static_mut_ref]
fn main() {}

View File

@ -1,26 +1,11 @@
warning: mutable reference of mutable static is discouraged
--> $DIR/issue-17718-const-bad-values.rs:5:41
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
LL | const C2: &'static mut usize = unsafe { addr_of_mut!(S) };
| ~~~~~~~~~~~~~~~
error[E0764]: mutable references are not allowed in the final value of constants
--> $DIR/issue-17718-const-bad-values.rs:1:34
--> $DIR/issue-17718-const-bad-values.rs:3:34
|
LL | const C1: &'static mut [usize] = &mut [];
| ^^^^^^^
error[E0658]: referencing statics in constants is unstable
--> $DIR/issue-17718-const-bad-values.rs:5:46
--> $DIR/issue-17718-const-bad-values.rs:7:46
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^
@ -31,7 +16,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.
error: aborting due to 2 previous errors; 1 warning emitted
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.

View File

@ -1,20 +1,5 @@
warning: shared reference of mutable static is discouraged
--> $DIR/const_refers_to_static_cross_crate.rs:12:14
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^^ shared reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
LL | unsafe { addr_of!(static_cross_crate::ZERO) }
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static_cross_crate.rs:10:1
--> $DIR/const_refers_to_static_cross_crate.rs:11:1
|
LL | const SLICE_MUT: &[u8; 1] = {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const`
@ -79,7 +64,7 @@ LL | U8_MUT3 => true,
warning: skipping const checks
|
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:12:15
--> $DIR/const_refers_to_static_cross_crate.rs:13:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
@ -99,6 +84,6 @@ help: skipping check for `const_refs_to_static` feature
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors; 2 warnings emitted
error: aborting due to 8 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0080`.

View File

@ -1,20 +1,5 @@
warning: shared reference of mutable static is discouraged
--> $DIR/const_refers_to_static_cross_crate.rs:12:14
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^^ shared reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
LL | unsafe { addr_of!(static_cross_crate::ZERO) }
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static_cross_crate.rs:10:1
--> $DIR/const_refers_to_static_cross_crate.rs:11:1
|
LL | const SLICE_MUT: &[u8; 1] = {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const`
@ -79,7 +64,7 @@ LL | U8_MUT3 => true,
warning: skipping const checks
|
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:12:15
--> $DIR/const_refers_to_static_cross_crate.rs:13:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
@ -99,6 +84,6 @@ help: skipping check for `const_refs_to_static` feature
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors; 2 warnings emitted
error: aborting due to 8 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0080`.

View File

@ -2,6 +2,7 @@
// aux-build:static_cross_crate.rs
// stderr-per-bitwidth
#![feature(exclusive_range_pattern, half_open_range_patterns_in_slices)]
#![allow(static_mut_ref)]
extern crate static_cross_crate;
@ -10,7 +11,6 @@
const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior
//~| encountered reference to mutable memory
unsafe { &static_cross_crate::ZERO }
//~^ WARN shared reference of mutable static is discouraged [static_mut_ref]
};
const U8_MUT: &u8 = { //~ ERROR undefined behavior

View File

@ -1,8 +1,8 @@
// build-pass (FIXME(62277): could be check-pass?)
#![allow(static_mut_ref)]
static mut STDERR_BUFFER_SPACE: [u8; 42] = [0u8; 42];
pub static mut STDERR_BUFFER: *mut [u8] = unsafe { &mut STDERR_BUFFER_SPACE };
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
fn main() {}

View File

@ -1,17 +0,0 @@
warning: mutable reference of mutable static is discouraged
--> $DIR/static_mut_containing_mut_ref.rs:5:52
|
LL | pub static mut STDERR_BUFFER: *mut [u8] = unsafe { &mut STDERR_BUFFER_SPACE };
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
LL | pub static mut STDERR_BUFFER: *mut [u8] = unsafe { addr_of_mut!(STDERR_BUFFER_SPACE) };
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
warning: 1 warning emitted

View File

@ -1,24 +1,9 @@
warning: mutable reference of mutable static is discouraged
--> $DIR/static_mut_containing_mut_ref2.rs:8:6
|
LL | *(&mut STDERR_BUFFER_SPACE) = 42;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
LL | *addr_of_mut!(STDERR_BUFFER_SPACE) = 42;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error[E0080]: could not evaluate static initializer
--> $DIR/static_mut_containing_mut_ref2.rs:8:5
|
LL | *(&mut STDERR_BUFFER_SPACE) = 42;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer
error: aborting due to 1 previous error; 1 warning emitted
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0080`.

View File

@ -1,5 +1,5 @@
// revisions: stock mut_refs
#![allow(static_mut_ref)]
#![cfg_attr(mut_refs, feature(const_mut_refs))]
static mut STDERR_BUFFER_SPACE: u8 = 0;
@ -8,8 +8,6 @@
*(&mut STDERR_BUFFER_SPACE) = 42;
//[mut_refs]~^ ERROR could not evaluate static initializer
//[stock]~^^ ERROR mutation through a reference is not allowed in statics
//[mut_refs]~^^^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//[stock]~^^^^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
};
fn main() {}

View File

@ -1,18 +1,3 @@
warning: mutable reference of mutable static is discouraged
--> $DIR/static_mut_containing_mut_ref2.rs:8:6
|
LL | *(&mut STDERR_BUFFER_SPACE) = 42;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
LL | *addr_of_mut!(STDERR_BUFFER_SPACE) = 42;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error[E0658]: mutation through a reference is not allowed in statics
--> $DIR/static_mut_containing_mut_ref2.rs:8:5
|
@ -23,6 +8,6 @@ LL | *(&mut STDERR_BUFFER_SPACE) = 42;
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 1 previous error; 1 warning emitted
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0658`.

View File

@ -2,14 +2,14 @@
#![feature(thread_local)]
#![feature(const_swap)]
#![allow(static_mut_ref)]
#[thread_local]
static mut STATIC_VAR_2: [u32; 8] = [4; 8];
const fn g(x: &mut [u32; 8]) {
//~^ ERROR mutable references are not allowed
std::mem::swap(x, &mut STATIC_VAR_2)
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^^ ERROR thread-local statics cannot be accessed
//~^ ERROR thread-local statics cannot be accessed
//~| ERROR mutable references are not allowed
//~| ERROR use of mutable static is unsafe
}

View File

@ -1,20 +1,5 @@
warning: mutable reference of mutable static is discouraged
--> $DIR/thread-local-static.rs:10:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
LL | std::mem::swap(x, addr_of_mut!(STATIC_VAR_2))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/thread-local-static.rs:10:28
--> $DIR/thread-local-static.rs:11:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^ use of mutable static
@ -22,7 +7,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:8:12
--> $DIR/thread-local-static.rs:9:12
|
LL | const fn g(x: &mut [u32; 8]) {
| ^
@ -32,13 +17,13 @@ LL | const fn g(x: &mut [u32; 8]) {
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0625]: thread-local statics cannot be accessed at compile-time
--> $DIR/thread-local-static.rs:10:28
--> $DIR/thread-local-static.rs:11:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:10:23
--> $DIR/thread-local-static.rs:11:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^
@ -47,7 +32,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 4 previous errors; 1 warning emitted
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0133, E0625, E0658.
For more information about an error, try `rustc --explain E0133`.

View File

@ -1,59 +0,0 @@
warning: mutable reference of mutable static is discouraged
--> $DIR/thread-local-static.rs:12:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: `#[warn(static_mut_ref)]` on by default
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
LL | std::mem::swap(x, addr_of_mut!(STATIC_VAR_2))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:10:12
|
LL | const fn g(x: &mut [u32; 8]) {
| ^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0625]: thread-local statics cannot be accessed at compile-time
--> $DIR/thread-local-static.rs:12:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^
error[E0013]: constant functions cannot refer to statics
--> $DIR/thread-local-static.rs:12:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that
error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:12:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/thread-local-static.rs:12:23
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^^^^^^ use of mutable static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
error: aborting due to 5 previous errors; 1 warning emitted
Some errors have detailed explanations: E0013, E0133, E0625, E0658.
For more information about an error, try `rustc --explain E0013`.