Skip to content

Commit d9bad38

Browse files
authored
Rollup merge of rust-lang#56916 - oli-obk:static_mut_beta_regression, r=davidtwco
Fix mutable references in `static mut` fixes rust-lang#56903
2 parents cd93b16 + 6ed596e commit d9bad38

8 files changed

+69
-6
lines changed

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
518518

519519
// Only allow statics (not consts) to refer to other statics.
520520
if self.mode == Mode::Static || self.mode == Mode::StaticMut {
521-
if context.is_mutating_use() {
521+
if self.mode == Mode::Static && context.is_mutating_use() {
522522
// this is not strictly necessary as miri will also bail out
523523
// For interior mutability we can't really catch this statically as that
524524
// goes through raw pointers and intermediate temporaries, so miri has
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-pass
2+
3+
static mut STDERR_BUFFER_SPACE: [u8; 42] = [0u8; 42];
4+
5+
pub static mut STDERR_BUFFER: *mut [u8] = unsafe { &mut STDERR_BUFFER_SPACE };
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(const_let)]
2+
3+
static mut STDERR_BUFFER_SPACE: u8 = 0;
4+
5+
pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
6+
//~^ ERROR references in statics may only refer to immutable values
7+
//~| ERROR static contains unimplemented expression type
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0017]: references in statics may only refer to immutable values
2+
--> $DIR/static_mut_containing_mut_ref2.rs:5:46
3+
|
4+
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ statics require immutable values
6+
7+
error[E0019]: static contains unimplemented expression type
8+
--> $DIR/static_mut_containing_mut_ref2.rs:5:45
9+
|
10+
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
Some errors occurred: E0017, E0019.
16+
For more information about an error, try `rustc --explain E0017`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(const_let)]
2+
3+
static mut FOO: (u8, u8) = (42, 43);
4+
5+
static mut BAR: () = unsafe { FOO.0 = 99; };
6+
//~^ ERROR could not evaluate static initializer
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0080]: could not evaluate static initializer
2+
--> $DIR/static_mut_containing_mut_ref3.rs:5:31
3+
|
4+
LL | static mut BAR: () = unsafe { FOO.0 = 99; };
5+
| ^^^^^^^^^^ tried to modify a static's initial value from another static's initializer
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/write-to-static-mut-in-static.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
pub static mut A: u32 = 0;
1414
pub static mut B: () = unsafe { A = 1; };
15-
//~^ ERROR cannot mutate statics in the initializer of another static
15+
//~^ ERROR could not evaluate static initializer
1616

1717
pub static mut C: u32 = unsafe { C = 1; 0 };
18-
//~^ ERROR cannot mutate statics in the initializer of another static
18+
//~^ ERROR cycle detected
1919

2020
pub static D: u32 = D;
2121

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
error: cannot mutate statics in the initializer of another static
1+
error[E0080]: could not evaluate static initializer
22
--> $DIR/write-to-static-mut-in-static.rs:14:33
33
|
44
LL | pub static mut B: () = unsafe { A = 1; };
5-
| ^^^^^
5+
| ^^^^^ tried to modify a static's initial value from another static's initializer
66

7-
error: cannot mutate statics in the initializer of another static
7+
error[E0391]: cycle detected when const-evaluating `C`
88
--> $DIR/write-to-static-mut-in-static.rs:17:34
99
|
1010
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
1111
| ^^^^^
12+
|
13+
note: ...which requires const-evaluating `C`...
14+
--> $DIR/write-to-static-mut-in-static.rs:17:1
15+
|
16+
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
= note: ...which again requires const-evaluating `C`, completing the cycle
19+
note: cycle used when const-evaluating + checking `C`
20+
--> $DIR/write-to-static-mut-in-static.rs:17:1
21+
|
22+
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1224

1325
error: aborting due to 2 previous errors
1426

27+
Some errors occurred: E0080, E0391.
28+
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)