Also catch static mutation at evaluation time
This commit is contained in:
parent
bc543d7e6c
commit
59eff14120
15 changed files with 82 additions and 35 deletions
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// New test for #53818: modifying static memory at compile-time is not allowed.
|
||||
// The test should never compile successfully
|
||||
|
||||
#![feature(const_raw_ptr_deref)]
|
||||
#![feature(const_let)]
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
|
||||
struct Foo(UnsafeCell<u32>);
|
||||
|
||||
unsafe impl Send for Foo {}
|
||||
unsafe impl Sync for Foo {}
|
||||
|
||||
static FOO: Foo = Foo(UnsafeCell::new(42));
|
||||
|
||||
static BAR: () = unsafe {
|
||||
*FOO.0.get() = 5; //~ ERROR could not evaluate static initializer
|
||||
};
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
error[E0080]: could not evaluate static initializer
|
||||
--> $DIR/assign-to-static-within-other-static-2.rs:27:5
|
||||
|
|
||||
LL | *FOO.0.get() = 5; //~ ERROR could not evaluate static initializer
|
||||
| ^^^^^^^^^^^^^^^^ 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`.
|
||||
|
|
@ -16,20 +16,9 @@
|
|||
|
||||
use std::cell::UnsafeCell;
|
||||
|
||||
struct Foo(UnsafeCell<u32>);
|
||||
|
||||
unsafe impl Send for Foo {}
|
||||
unsafe impl Sync for Foo {}
|
||||
|
||||
static FOO: Foo = Foo(UnsafeCell::new(42));
|
||||
|
||||
static BAR: () = unsafe {
|
||||
*FOO.0.get() = 5;
|
||||
};
|
||||
|
||||
static mut FOO2: u32 = 42;
|
||||
static BOO2: () = unsafe {
|
||||
FOO2 = 5;
|
||||
static mut FOO: u32 = 42;
|
||||
static BOO: () = unsafe {
|
||||
FOO = 5; //~ ERROR cannot mutate statics in the initializer of another static
|
||||
};
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
error: cannot mutate statics in the initializer of another static
|
||||
--> $DIR/assign-to-static-within-other-static.rs:32:5
|
||||
--> $DIR/assign-to-static-within-other-static.rs:21:5
|
||||
|
|
||||
LL | FOO2 = 5;
|
||||
| ^^^^^^^^
|
||||
LL | FOO = 5; //~ ERROR cannot mutate statics in the initializer of another static
|
||||
| ^^^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -14,5 +14,6 @@ const C: i32 = 2;
|
|||
const CR: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
|
||||
//~| ERROR cannot borrow
|
||||
//~| ERROR cannot mutate statics
|
||||
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,12 @@ error[E0017]: references in constants may only refer to immutable values
|
|||
LL | const CR: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
| ^^^^^^ constants require immutable values
|
||||
|
||||
error: cannot mutate statics in the initializer of another static
|
||||
--> $DIR/E0017.rs:15:39
|
||||
|
|
||||
LL | static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
|
||||
| ^^^^^^
|
||||
|
||||
error[E0017]: references in statics may only refer to immutable values
|
||||
--> $DIR/E0017.rs:15:39
|
||||
|
|
||||
|
|
@ -17,12 +23,12 @@ LL | static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
|
|||
| ^
|
||||
|
||||
error[E0017]: references in statics may only refer to immutable values
|
||||
--> $DIR/E0017.rs:17:38
|
||||
--> $DIR/E0017.rs:18:38
|
||||
|
|
||||
LL | static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
| ^^^^^^ statics require immutable values
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
Some errors occurred: E0017, E0596.
|
||||
For more information about an error, try `rustc --explain E0017`.
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ const C: i32 = 2;
|
|||
const CR: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
|
||||
//~| ERROR cannot borrow
|
||||
//~| ERROR cannot mutate statics
|
||||
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,12 @@ error[E0017]: references in constants may only refer to immutable values
|
|||
LL | const CR: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
| ^^^^^^ constants require immutable values
|
||||
|
||||
error: cannot mutate statics in the initializer of another static
|
||||
--> $DIR/E0388.rs:15:39
|
||||
|
|
||||
LL | static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
|
||||
| ^^^^^^
|
||||
|
||||
error[E0017]: references in statics may only refer to immutable values
|
||||
--> $DIR/E0388.rs:15:39
|
||||
|
|
||||
|
|
@ -17,12 +23,12 @@ LL | static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
|
|||
| ^
|
||||
|
||||
error[E0017]: references in statics may only refer to immutable values
|
||||
--> $DIR/E0388.rs:17:38
|
||||
--> $DIR/E0388.rs:18:38
|
||||
|
|
||||
LL | static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
|
||||
| ^^^^^^ statics require immutable values
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
Some errors occurred: E0017, E0596.
|
||||
For more information about an error, try `rustc --explain E0017`.
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@
|
|||
|
||||
pub static mut A: u32 = 0;
|
||||
pub static mut B: () = unsafe { A = 1; };
|
||||
//~^ ERROR statements in statics are unstable
|
||||
//~^ ERROR cannot mutate statics in the initializer of another static
|
||||
|
||||
pub static mut C: u32 = unsafe { C = 1; 0 };
|
||||
//~^ ERROR statements in statics are unstable
|
||||
//~^ ERROR cannot mutate statics in the initializer of another static
|
||||
|
||||
pub static D: u32 = D;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,19 +1,14 @@
|
|||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
error: cannot mutate statics in the initializer of another static
|
||||
--> $DIR/write-to-static-mut-in-static.rs:14:33
|
||||
|
|
||||
LL | pub static mut B: () = unsafe { A = 1; };
|
||||
| ^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
error: cannot mutate statics in the initializer of another static
|
||||
--> $DIR/write-to-static-mut-in-static.rs:17:34
|
||||
|
|
||||
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
|
||||
| ^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue