Rollup merge of #72934 - christianpoveda:mut-borrows-in-consts, r=oli-obk

forbid mutable references in all constant contexts except for const-fns

PR to address #71212

cc: @ecstatic-morse
This commit is contained in:
Manish Goregaokar 2020-06-19 19:42:51 -07:00 committed by GitHub
commit dac512e04a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 193 additions and 141 deletions

View file

@ -2,10 +2,10 @@ static X: i32 = 1;
const C: i32 = 2;
static mut M: i32 = 3;
const CR: &'static mut i32 = &mut C; //~ ERROR E0658
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
//~| ERROR E0019
//~| ERROR cannot borrow
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0658
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0658
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
fn main() {}

View file

@ -1,11 +1,8 @@
error[E0658]: references in constants may only refer to immutable values
error[E0764]: mutable references are not allowed in constants
--> $DIR/E0017.rs:5:30
|
LL | const CR: &'static mut i32 = &mut C;
| ^^^^^^ constants require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error[E0019]: static contains unimplemented expression type
--> $DIR/E0017.rs:6:39
@ -15,14 +12,11 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0658]: references in statics may only refer to immutable values
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:6:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ statics require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0017.rs:6:39
@ -30,25 +24,19 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
error[E0658]: references in statics may only refer to immutable values
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:9:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ statics require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error[E0658]: references in statics may only refer to immutable values
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0017.rs:10:52
|
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
| ^^^^^^ statics require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error: aborting due to 6 previous errors
Some errors have detailed explanations: E0019, E0596, E0658.
Some errors have detailed explanations: E0019, E0596, E0764.
For more information about an error, try `rustc --explain E0019`.

View file

@ -1,10 +1,10 @@
static X: i32 = 1;
const C: i32 = 2;
const CR: &'static mut i32 = &mut C; //~ ERROR E0658
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
//~| ERROR cannot borrow
//~| ERROR E0019
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0658
//~| ERROR E0764
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
fn main() {}

View file

@ -1,11 +1,8 @@
error[E0658]: references in constants may only refer to immutable values
error[E0764]: mutable references are not allowed in constants
--> $DIR/E0388.rs:4:30
|
LL | const CR: &'static mut i32 = &mut C;
| ^^^^^^ constants require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error[E0019]: static contains unimplemented expression type
--> $DIR/E0388.rs:5:39
@ -15,14 +12,11 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
error[E0658]: references in statics may only refer to immutable values
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0388.rs:5:39
|
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ statics require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error[E0596]: cannot borrow immutable static item `X` as mutable
--> $DIR/E0388.rs:5:39
@ -30,16 +24,13 @@ error[E0596]: cannot borrow immutable static item `X` as mutable
LL | static STATIC_REF: &'static mut i32 = &mut X;
| ^^^^^^ cannot borrow as mutable
error[E0658]: references in statics may only refer to immutable values
error[E0764]: mutable references are not allowed in statics
--> $DIR/E0388.rs:8:38
|
LL | static CONST_REF: &'static mut i32 = &mut C;
| ^^^^^^ statics require immutable values
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^ `&mut` is only allowed in `const fn`
error: aborting due to 5 previous errors
Some errors have detailed explanations: E0019, E0596, E0658.
Some errors have detailed explanations: E0019, E0596, E0764.
For more information about an error, try `rustc --explain E0019`.