Skip to content

Commit

Permalink
Rollup merge of rust-lang#56916 - oli-obk:static_mut_beta_regression,…
Browse files Browse the repository at this point in the history
… r=davidtwco

Fix mutable references in `static mut`

fixes rust-lang#56903
  • Loading branch information
Centril committed Dec 22, 2018
2 parents 88fbd9f + 6ed596e commit 3627261
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/consts/static_mut_containing_mut_ref.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/test/ui/consts/static_mut_containing_mut_ref2.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions src/test/ui/consts/static_mut_containing_mut_ref2.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions src/test/ui/consts/static_mut_containing_mut_ref3.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/test/ui/consts/static_mut_containing_mut_ref3.stderr
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 2 additions & 2 deletions src/test/ui/write-to-static-mut-in-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
20 changes: 17 additions & 3 deletions src/test/ui/write-to-static-mut-in-static.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 3627261

Please sign in to comment.