Skip to content

Commit

Permalink
Rollup merge of #120973 - RalfJung:static_mut_ref, r=compiler-errors
Browse files Browse the repository at this point in the history
allow static_mut_ref in some tests that specifically test mutable statics

The lint just distracts from what these tests are about.
  • Loading branch information
matthiaskrgr authored Feb 13, 2024
2 parents 134de26 + 6fe4d66 commit d95f000
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 212 deletions.
2 changes: 1 addition & 1 deletion tests/ui/consts/const_refs_to_static_fail_invalid.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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!
Expand Down
27 changes: 6 additions & 21 deletions tests/ui/consts/const_refs_to_static_fail_invalid.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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`.
3 changes: 2 additions & 1 deletion tests/ui/consts/issue-17718-const-bad-values.rs
Original file line number Diff line number Diff line change
@@ -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() {}
21 changes: 3 additions & 18 deletions tests/ui/consts/issue-17718-const-bad-values.stderr
Original file line number Diff line number Diff line change
@@ -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 };
| ^
Expand All @@ -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`.
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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 }
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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`.
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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 }
| ^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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`.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -10,7 +11,6 @@ extern crate static_cross_crate;
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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/static_mut_containing_mut_ref.rs
Original file line number Diff line number Diff line change
@@ -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() {}
17 changes: 0 additions & 17 deletions tests/ui/consts/static_mut_containing_mut_ref.stderr

This file was deleted.

17 changes: 1 addition & 16 deletions tests/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 1 addition & 3 deletions tests/ui/consts/static_mut_containing_mut_ref2.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,8 +8,6 @@ pub static mut STDERR_BUFFER: () = unsafe {
*(&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() {}
17 changes: 1 addition & 16 deletions tests/ui/consts/static_mut_containing_mut_ref2.stock.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand All @@ -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`.
4 changes: 2 additions & 2 deletions tests/ui/thread-local/thread-local-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 5 additions & 20 deletions tests/ui/thread-local/thread-local-static.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
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
|
= 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]) {
| ^
Expand All @@ -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)
| ^^^^^^^^^^^^^^^^^
Expand All @@ -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`.
Loading

0 comments on commit d95f000

Please sign in to comment.