Rollup merge of #56916 - oli-obk:static_mut_beta_regression, r=davidtwco
Fix mutable references in `static mut` fixes #56903
This commit is contained in:
commit
84bc34e70e
8 changed files with 69 additions and 6 deletions
|
|
@ -518,7 +518,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
|
|||
|
||||
// Only allow statics (not consts) to refer to other statics.
|
||||
if self.mode == Mode::Static || self.mode == Mode::StaticMut {
|
||||
if context.is_mutating_use() {
|
||||
if self.mode == Mode::Static && context.is_mutating_use() {
|
||||
// this is not strictly necessary as miri will also bail out
|
||||
// For interior mutability we can't really catch this statically as that
|
||||
// goes through raw pointers and intermediate temporaries, so miri has
|
||||
|
|
|
|||
7
src/test/ui/consts/static_mut_containing_mut_ref.rs
Normal file
7
src/test/ui/consts/static_mut_containing_mut_ref.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// compile-pass
|
||||
|
||||
static mut STDERR_BUFFER_SPACE: [u8; 42] = [0u8; 42];
|
||||
|
||||
pub static mut STDERR_BUFFER: *mut [u8] = unsafe { &mut STDERR_BUFFER_SPACE };
|
||||
|
||||
fn main() {}
|
||||
9
src/test/ui/consts/static_mut_containing_mut_ref2.rs
Normal file
9
src/test/ui/consts/static_mut_containing_mut_ref2.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#![feature(const_let)]
|
||||
|
||||
static mut STDERR_BUFFER_SPACE: u8 = 0;
|
||||
|
||||
pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
|
||||
//~^ ERROR references in statics may only refer to immutable values
|
||||
//~| ERROR static contains unimplemented expression type
|
||||
|
||||
fn main() {}
|
||||
16
src/test/ui/consts/static_mut_containing_mut_ref2.stderr
Normal file
16
src/test/ui/consts/static_mut_containing_mut_ref2.stderr
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
error[E0017]: references in statics may only refer to immutable values
|
||||
--> $DIR/static_mut_containing_mut_ref2.rs:5:46
|
||||
|
|
||||
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ statics require immutable values
|
||||
|
||||
error[E0019]: static contains unimplemented expression type
|
||||
--> $DIR/static_mut_containing_mut_ref2.rs:5:45
|
||||
|
|
||||
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Some errors occurred: E0017, E0019.
|
||||
For more information about an error, try `rustc --explain E0017`.
|
||||
8
src/test/ui/consts/static_mut_containing_mut_ref3.rs
Normal file
8
src/test/ui/consts/static_mut_containing_mut_ref3.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#![feature(const_let)]
|
||||
|
||||
static mut FOO: (u8, u8) = (42, 43);
|
||||
|
||||
static mut BAR: () = unsafe { FOO.0 = 99; };
|
||||
//~^ ERROR could not evaluate static initializer
|
||||
|
||||
fn main() {}
|
||||
9
src/test/ui/consts/static_mut_containing_mut_ref3.stderr
Normal file
9
src/test/ui/consts/static_mut_containing_mut_ref3.stderr
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
error[E0080]: could not evaluate static initializer
|
||||
--> $DIR/static_mut_containing_mut_ref3.rs:5:31
|
||||
|
|
||||
LL | static mut BAR: () = unsafe { FOO.0 = 99; };
|
||||
| ^^^^^^^^^^ tried to modify a static's initial value from another static's initializer
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0080`.
|
||||
|
|
@ -12,10 +12,10 @@
|
|||
|
||||
pub static mut A: u32 = 0;
|
||||
pub static mut B: () = unsafe { A = 1; };
|
||||
//~^ ERROR cannot mutate statics in the initializer of another static
|
||||
//~^ ERROR could not evaluate static initializer
|
||||
|
||||
pub static mut C: u32 = unsafe { C = 1; 0 };
|
||||
//~^ ERROR cannot mutate statics in the initializer of another static
|
||||
//~^ ERROR cycle detected
|
||||
|
||||
pub static D: u32 = D;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,28 @@
|
|||
error: cannot mutate statics in the initializer of another static
|
||||
error[E0080]: could not evaluate static initializer
|
||||
--> $DIR/write-to-static-mut-in-static.rs:14:33
|
||||
|
|
||||
LL | pub static mut B: () = unsafe { A = 1; };
|
||||
| ^^^^^
|
||||
| ^^^^^ tried to modify a static's initial value from another static's initializer
|
||||
|
||||
error: cannot mutate statics in the initializer of another static
|
||||
error[E0391]: cycle detected when const-evaluating `C`
|
||||
--> $DIR/write-to-static-mut-in-static.rs:17:34
|
||||
|
|
||||
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
|
||||
| ^^^^^
|
||||
|
|
||||
note: ...which requires const-evaluating `C`...
|
||||
--> $DIR/write-to-static-mut-in-static.rs:17:1
|
||||
|
|
||||
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
= note: ...which again requires const-evaluating `C`, completing the cycle
|
||||
note: cycle used when const-evaluating + checking `C`
|
||||
--> $DIR/write-to-static-mut-in-static.rs:17:1
|
||||
|
|
||||
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Some errors occurred: E0080, E0391.
|
||||
For more information about an error, try `rustc --explain E0080`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue