Skip to content

Commit dac512e

Browse files
authoredJun 20, 2020
Rollup merge of rust-lang#72934 - christianpoveda:mut-borrows-in-consts, r=oli-obk
forbid mutable references in all constant contexts except for const-fns PR to address rust-lang#71212 cc: @ecstatic-morse
2 parents c0a25be + 96031e2 commit dac512e

32 files changed

+193
-141
lines changed
 

‎src/librustc_error_codes/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ E0760: include_str!("./error_codes/E0760.md"),
444444
E0761: include_str!("./error_codes/E0761.md"),
445445
E0762: include_str!("./error_codes/E0762.md"),
446446
E0763: include_str!("./error_codes/E0763.md"),
447+
E0764: include_str!("./error_codes/E0764.md"),
447448
;
448449
// E0006, // merged with E0005
449450
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Mutable references (`&mut`) can only be used in constant functions, not statics
2+
or constants. This limitation exists to prevent the creation of constants that
3+
have a mutable reference in their final value. If you had a constant of `&mut
4+
i32` type, you could modify the value through that reference, making the
5+
constant essentially mutable. While there could be a more fine-grained scheme
6+
in the future that allows mutable references if they are not "leaked" to the
7+
final value, a more conservative approach was chosen for now. `const fn` do not
8+
have this problem, as the borrow checker will prevent the `const fn` from
9+
returning new mutable references.
10+
11+
Erroneous code example:
12+
13+
```compile_fail,E0764
14+
#![feature(const_fn)]
15+
#![feature(const_mut_refs)]
16+
17+
fn main() {
18+
const OH_NO: &'static mut usize = &mut 1; // error!
19+
}
20+
```
21+
22+
Remember: you cannot use a function call inside a constant or static. However,
23+
you can totally use it in constant functions:
24+
25+
```
26+
#![feature(const_fn)]
27+
#![feature(const_mut_refs)]
28+
29+
const fn foo(x: usize) -> usize {
30+
let mut y = 1;
31+
let z = &mut y;
32+
*z += x;
33+
y
34+
}
35+
36+
fn main() {
37+
const FOO: usize = foo(10); // ok!
38+
}
39+
```

‎src/librustc_mir/transform/check_consts/ops.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,34 @@ impl NonConstOp for CellBorrow {
205205
#[derive(Debug)]
206206
pub struct MutBorrow;
207207
impl NonConstOp for MutBorrow {
208+
fn is_allowed_in_item(&self, ccx: &ConstCx<'_, '_>) -> bool {
209+
// Forbid everywhere except in const fn
210+
ccx.const_kind() == hir::ConstContext::ConstFn
211+
&& ccx.tcx.features().enabled(Self::feature_gate().unwrap())
212+
}
213+
208214
fn feature_gate() -> Option<Symbol> {
209215
Some(sym::const_mut_refs)
210216
}
211217

212218
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
213-
let mut err = feature_err(
214-
&ccx.tcx.sess.parse_sess,
215-
sym::const_mut_refs,
216-
span,
217-
&format!(
218-
"references in {}s may only refer \
219-
to immutable values",
220-
ccx.const_kind()
221-
),
222-
);
223-
err.span_label(span, format!("{}s require immutable values", ccx.const_kind()));
219+
let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn {
220+
feature_err(
221+
&ccx.tcx.sess.parse_sess,
222+
sym::const_mut_refs,
223+
span,
224+
&format!("mutable references are not allowed in {}s", ccx.const_kind()),
225+
)
226+
} else {
227+
struct_span_err!(
228+
ccx.tcx.sess,
229+
span,
230+
E0764,
231+
"mutable references are not allowed in {}s",
232+
ccx.const_kind(),
233+
)
234+
};
235+
err.span_label(span, "`&mut` is only allowed in `const fn`".to_string());
224236
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
225237
err.note(
226238
"References in statics and constants may only refer \

‎src/test/compile-fail/issue-52443.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
[(); { for _ in 0usize.. {}; 0}];
1010
//~^ ERROR `for` is not allowed in a `const`
1111
//~| ERROR calls in constants are limited to constant functions
12-
//~| ERROR references in constants may only refer to immutable values
12+
//~| ERROR mutable references are not allowed in constants
1313
//~| ERROR calls in constants are limited to constant functions
1414
//~| ERROR evaluation of constant value failed
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Checks that immutable static items can't have mutable slices
22

33
static TEST: &'static mut [isize] = &mut [];
4-
//~^ ERROR references in statics may only refer to immutable values
4+
//~^ ERROR mutable references are not allowed in statics
55

66
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0658]: references in statics may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/check-static-immutable-mut-slices.rs:3:37
33
|
44
LL | static TEST: &'static mut [isize] = &mut [];
5-
| ^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error: aborting due to previous error
118

12-
For more information about this error, try `rustc --explain E0658`.
9+
For more information about this error, try `rustc --explain E0764`.

‎src/test/ui/consts/const-eval/issue-65394.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const _: Vec<i32> = {
77
let mut x = Vec::<i32>::new(); //~ ERROR destructors cannot be evaluated at compile-time
8-
let r = &mut x; //~ ERROR references in constants may only refer to immutable values
8+
let r = &mut x; //~ ERROR mutable references are not allowed in constants
99
let y = x;
1010
y
1111
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/issue-65394.rs:8:13
33
|
44
LL | let r = &mut x;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0493]: destructors cannot be evaluated at compile-time
118
--> $DIR/issue-65394.rs:7:9
@@ -15,5 +12,5 @@ LL | let mut x = Vec::<i32>::new();
1512

1613
error: aborting due to 2 previous errors
1714

18-
Some errors have detailed explanations: E0493, E0658.
15+
Some errors have detailed explanations: E0493, E0764.
1916
For more information about an error, try `rustc --explain E0493`.

‎src/test/ui/consts/const-multi-ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const _: i32 = {
55
let mut a = 5;
6-
let p = &mut a; //~ ERROR references in constants may only refer to immutable values
6+
let p = &mut a; //~ ERROR mutable references are not allowed in constants
77

88
let reborrow = {p};
99
let pp = &reborrow;
+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/const-multi-ref.rs:6:13
33
|
44
LL | let p = &mut a;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
118
--> $DIR/const-multi-ref.rs:16:13
@@ -15,5 +12,5 @@ LL | let p = &a;
1512

1613
error: aborting due to 2 previous errors
1714

18-
Some errors have detailed explanations: E0492, E0658.
15+
Some errors have detailed explanations: E0492, E0764.
1916
For more information about an error, try `rustc --explain E0492`.

‎src/test/ui/consts/const-mut-refs/const_mut_address_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// check-pass
2-
31
#![feature(const_mut_refs)]
42
#![feature(const_fn)]
53
#![feature(raw_ref_op)]
@@ -24,7 +22,9 @@ const fn baz(foo: &mut Foo)-> *mut usize {
2422

2523
const _: () = {
2624
foo().bar();
25+
//~^ ERROR mutable references are not allowed in constants
2726
baz(&mut foo());
27+
//~^ ERROR mutable references are not allowed in constants
2828
};
2929

3030
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0764]: mutable references are not allowed in constants
2+
--> $DIR/const_mut_address_of.rs:24:5
3+
|
4+
LL | foo().bar();
5+
| ^^^^^ `&mut` is only allowed in `const fn`
6+
7+
error[E0764]: mutable references are not allowed in constants
8+
--> $DIR/const_mut_address_of.rs:26:9
9+
|
10+
LL | baz(&mut foo());
11+
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0764`.

‎src/test/ui/consts/const-mut-refs/const_mut_refs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-pass
2-
31
#![feature(const_mut_refs)]
42

53
struct Foo {
@@ -31,6 +29,9 @@ const fn bazz(foo: &mut Foo) -> usize {
3129

3230
fn main() {
3331
let _: [(); foo().bar()] = [(); 1];
32+
//~^ ERROR mutable references are not allowed in constants
3433
let _: [(); baz(&mut foo())] = [(); 2];
34+
//~^ ERROR mutable references are not allowed in constants
3535
let _: [(); bazz(&mut foo())] = [(); 3];
36+
//~^ ERROR mutable references are not allowed in constants
3637
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0764]: mutable references are not allowed in constants
2+
--> $DIR/const_mut_refs.rs:31:17
3+
|
4+
LL | let _: [(); foo().bar()] = [(); 1];
5+
| ^^^^^ `&mut` is only allowed in `const fn`
6+
7+
error[E0764]: mutable references are not allowed in constants
8+
--> $DIR/const_mut_refs.rs:33:21
9+
|
10+
LL | let _: [(); baz(&mut foo())] = [(); 2];
11+
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
12+
13+
error[E0764]: mutable references are not allowed in constants
14+
--> $DIR/const_mut_refs.rs:35:22
15+
|
16+
LL | let _: [(); bazz(&mut foo())] = [(); 3];
17+
| ^^^^^^^^^^ `&mut` is only allowed in `const fn`
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0764`.

‎src/test/ui/consts/const_let_assign3.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ impl S {
1313

1414
const FOO: S = {
1515
let mut s = S { state: 42 };
16-
s.foo(3); //~ ERROR references in constants may only refer to immutable values
16+
s.foo(3); //~ ERROR mutable references are not allowed in constants
1717
s
1818
};
1919

2020
type Array = [u32; {
2121
let mut x = 2;
2222
let y = &mut x;
23-
//~^ ERROR references in constants may only refer to immutable values
23+
//~^ ERROR mutable references are not allowed in constants
2424
*y = 42;
2525
//~^ ERROR constant contains unimplemented expression type
2626
*y

‎src/test/ui/consts/const_let_assign3.stderr

+5-11
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@ LL | self.state = x;
66
|
77
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
88

9-
error[E0658]: references in constants may only refer to immutable values
9+
error[E0764]: mutable references are not allowed in constants
1010
--> $DIR/const_let_assign3.rs:16:5
1111
|
1212
LL | s.foo(3);
13-
| ^ constants require immutable values
14-
|
15-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
16-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
13+
| ^ `&mut` is only allowed in `const fn`
1714

18-
error[E0658]: references in constants may only refer to immutable values
15+
error[E0764]: mutable references are not allowed in constants
1916
--> $DIR/const_let_assign3.rs:22:13
2017
|
2118
LL | let y = &mut x;
22-
| ^^^^^^ constants require immutable values
23-
|
24-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
25-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
19+
| ^^^^^^ `&mut` is only allowed in `const fn`
2620

2721
error[E0019]: constant contains unimplemented expression type
2822
--> $DIR/const_let_assign3.rs:24:5
@@ -34,5 +28,5 @@ LL | *y = 42;
3428

3529
error: aborting due to 4 previous errors
3630

37-
Some errors have detailed explanations: E0019, E0658.
31+
Some errors have detailed explanations: E0019, E0764.
3832
For more information about an error, try `rustc --explain E0019`.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0764]: mutable references are not allowed in constants
2+
--> $DIR/projection_qualif.rs:10:27
3+
|
4+
LL | let b: *mut u32 = &mut a;
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
6+
17
error[E0658]: dereferencing raw pointers in constants is unstable
28
--> $DIR/projection_qualif.rs:11:18
39
|
@@ -7,6 +13,7 @@ LL | unsafe { *b = 5; }
713
= note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information
814
= help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable
915

10-
error: aborting due to previous error
16+
error: aborting due to 2 previous errors
1117

12-
For more information about this error, try `rustc --explain E0658`.
18+
Some errors have detailed explanations: E0658, E0764.
19+
For more information about an error, try `rustc --explain E0658`.

‎src/test/ui/consts/projection_qualif.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::cell::Cell;
77
const FOO: &u32 = {
88
let mut a = 42;
99
{
10-
let b: *mut u32 = &mut a; //[stock]~ ERROR may only refer to immutable values
10+
let b: *mut u32 = &mut a; //~ ERROR mutable references are not allowed in constants
1111
unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
1212
//[stock]~^ contains unimplemented expression
1313
}

‎src/test/ui/consts/projection_qualif.stock.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/projection_qualif.rs:10:27
33
|
44
LL | let b: *mut u32 = &mut a;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0658]: dereferencing raw pointers in constants is unstable
118
--> $DIR/projection_qualif.rs:11:18
@@ -26,5 +23,5 @@ LL | unsafe { *b = 5; }
2623

2724
error: aborting due to 3 previous errors
2825

29-
Some errors have detailed explanations: E0019, E0658.
26+
Some errors have detailed explanations: E0019, E0658, E0764.
3027
For more information about an error, try `rustc --explain E0019`.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// run-pass
1+
// We are keeping this test in case we decide to allow mutable references in statics again
22
#![feature(const_mut_refs)]
33
#![allow(const_err)]
44

5-
static OH_YES: &mut i32 = &mut 42;
6-
5+
static OH_NO: &mut i32 = &mut 42;
6+
//~^ ERROR mutable references are not allowed in statics
77
fn main() {
8-
// Make sure `OH_YES` can be read.
9-
assert_eq!(*OH_YES, 42);
8+
assert_eq!(*OH_NO, 42);
109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0764]: mutable references are not allowed in statics
2+
--> $DIR/read_from_static_mut_ref.rs:5:26
3+
|
4+
LL | static OH_NO: &mut i32 = &mut 42;
5+
| ^^^^^^^ `&mut` is only allowed in `const fn`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0764`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0080]: could not evaluate static initializer
2-
--> $DIR/static_mut_containing_mut_ref2.rs:7:45
1+
error[E0764]: mutable references are not allowed in statics
2+
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
33
|
44
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0080`.
9+
For more information about this error, try `rustc --explain E0764`.

‎src/test/ui/consts/static_mut_containing_mut_ref2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
static mut STDERR_BUFFER_SPACE: u8 = 0;
66

77
pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
8-
//[mut_refs]~^ ERROR could not evaluate static initializer
9-
//[stock]~^^ ERROR references in statics may only refer to immutable values
8+
//~^ ERROR mutable references are not allowed in statics
109
//[stock]~| ERROR static contains unimplemented expression type
1110

1211
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in statics may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/static_mut_containing_mut_ref2.rs:7:46
33
|
44
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0019]: static contains unimplemented expression type
118
--> $DIR/static_mut_containing_mut_ref2.rs:7:45
@@ -17,5 +14,5 @@ LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 4
1714

1815
error: aborting due to 2 previous errors
1916

20-
Some errors have detailed explanations: E0019, E0658.
17+
Some errors have detailed explanations: E0019, E0764.
2118
For more information about an error, try `rustc --explain E0019`.

‎src/test/ui/error-codes/E0017.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ static X: i32 = 1;
22
const C: i32 = 2;
33
static mut M: i32 = 3;
44

5-
const CR: &'static mut i32 = &mut C; //~ ERROR E0658
6-
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
5+
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
6+
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0764
77
//~| ERROR E0019
88
//~| ERROR cannot borrow
9-
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0658
10-
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0658
9+
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
10+
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR E0764
1111
fn main() {}

‎src/test/ui/error-codes/E0017.stderr

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/E0017.rs:5:30
33
|
44
LL | const CR: &'static mut i32 = &mut C;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0019]: static contains unimplemented expression type
118
--> $DIR/E0017.rs:6:39
@@ -15,40 +12,31 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
1512
|
1613
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
1714

18-
error[E0658]: references in statics may only refer to immutable values
15+
error[E0764]: mutable references are not allowed in statics
1916
--> $DIR/E0017.rs:6:39
2017
|
2118
LL | static STATIC_REF: &'static mut i32 = &mut X;
22-
| ^^^^^^ statics require immutable values
23-
|
24-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
25-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
19+
| ^^^^^^ `&mut` is only allowed in `const fn`
2620

2721
error[E0596]: cannot borrow immutable static item `X` as mutable
2822
--> $DIR/E0017.rs:6:39
2923
|
3024
LL | static STATIC_REF: &'static mut i32 = &mut X;
3125
| ^^^^^^ cannot borrow as mutable
3226

33-
error[E0658]: references in statics may only refer to immutable values
27+
error[E0764]: mutable references are not allowed in statics
3428
--> $DIR/E0017.rs:9:38
3529
|
3630
LL | static CONST_REF: &'static mut i32 = &mut C;
37-
| ^^^^^^ statics require immutable values
38-
|
39-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
40-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
31+
| ^^^^^^ `&mut` is only allowed in `const fn`
4132

42-
error[E0658]: references in statics may only refer to immutable values
33+
error[E0764]: mutable references are not allowed in statics
4334
--> $DIR/E0017.rs:10:52
4435
|
4536
LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M };
46-
| ^^^^^^ statics require immutable values
47-
|
48-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
49-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
37+
| ^^^^^^ `&mut` is only allowed in `const fn`
5038

5139
error: aborting due to 6 previous errors
5240

53-
Some errors have detailed explanations: E0019, E0596, E0658.
41+
Some errors have detailed explanations: E0019, E0596, E0764.
5442
For more information about an error, try `rustc --explain E0019`.

‎src/test/ui/error-codes/E0388.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
static X: i32 = 1;
22
const C: i32 = 2;
33

4-
const CR: &'static mut i32 = &mut C; //~ ERROR E0658
5-
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658
4+
const CR: &'static mut i32 = &mut C; //~ ERROR E0764
5+
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0019
66
//~| ERROR cannot borrow
7-
//~| ERROR E0019
8-
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0658
7+
//~| ERROR E0764
8+
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0764
99

1010
fn main() {}

‎src/test/ui/error-codes/E0388.stderr

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/E0388.rs:4:30
33
|
44
LL | const CR: &'static mut i32 = &mut C;
5-
| ^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0019]: static contains unimplemented expression type
118
--> $DIR/E0388.rs:5:39
@@ -15,31 +12,25 @@ LL | static STATIC_REF: &'static mut i32 = &mut X;
1512
|
1613
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
1714

18-
error[E0658]: references in statics may only refer to immutable values
15+
error[E0764]: mutable references are not allowed in statics
1916
--> $DIR/E0388.rs:5:39
2017
|
2118
LL | static STATIC_REF: &'static mut i32 = &mut X;
22-
| ^^^^^^ statics require immutable values
23-
|
24-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
25-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
19+
| ^^^^^^ `&mut` is only allowed in `const fn`
2620

2721
error[E0596]: cannot borrow immutable static item `X` as mutable
2822
--> $DIR/E0388.rs:5:39
2923
|
3024
LL | static STATIC_REF: &'static mut i32 = &mut X;
3125
| ^^^^^^ cannot borrow as mutable
3226

33-
error[E0658]: references in statics may only refer to immutable values
27+
error[E0764]: mutable references are not allowed in statics
3428
--> $DIR/E0388.rs:8:38
3529
|
3630
LL | static CONST_REF: &'static mut i32 = &mut C;
37-
| ^^^^^^ statics require immutable values
38-
|
39-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
40-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
31+
| ^^^^^^ `&mut` is only allowed in `const fn`
4132

4233
error: aborting due to 5 previous errors
4334

44-
Some errors have detailed explanations: E0019, E0596, E0658.
35+
Some errors have detailed explanations: E0019, E0596, E0764.
4536
For more information about an error, try `rustc --explain E0019`.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const C1: &'static mut [usize] = &mut [];
2-
//~^ ERROR: references in constants may only refer to immutable values
2+
//~^ ERROR: mutable references are not allowed in constants
33

44
static mut S: usize = 3;
55
const C2: &'static mut usize = unsafe { &mut S };
66
//~^ ERROR: constants cannot refer to statics
77
//~| ERROR: constants cannot refer to statics
8-
//~| ERROR: references in constants may only refer to immutable values
8+
//~| ERROR: mutable references are not allowed in constants
99

1010
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in constants may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in constants
22
--> $DIR/issue-17718-const-bad-values.rs:1:34
33
|
44
LL | const C1: &'static mut [usize] = &mut [];
5-
| ^^^^^^^ constants require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0013]: constants cannot refer to statics
118
--> $DIR/issue-17718-const-bad-values.rs:5:46
@@ -23,16 +20,13 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
2320
|
2421
= help: consider extracting the value of the `static` to a `const`, and referring to that
2522

26-
error[E0658]: references in constants may only refer to immutable values
23+
error[E0764]: mutable references are not allowed in constants
2724
--> $DIR/issue-17718-const-bad-values.rs:5:41
2825
|
2926
LL | const C2: &'static mut usize = unsafe { &mut S };
30-
| ^^^^^^ constants require immutable values
31-
|
32-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
33-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
27+
| ^^^^^^ `&mut` is only allowed in `const fn`
3428

3529
error: aborting due to 4 previous errors
3630

37-
Some errors have detailed explanations: E0013, E0658.
31+
Some errors have detailed explanations: E0013, E0764.
3832
For more information about an error, try `rustc --explain E0013`.

‎src/test/ui/issues/issue-46604.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0658
1+
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //~ ERROR E0764
22
fn write<T: AsRef<[u8]>>(buffer: T) { }
33

44
fn main() {

‎src/test/ui/issues/issue-46604.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0658]: references in statics may only refer to immutable values
1+
error[E0764]: mutable references are not allowed in statics
22
--> $DIR/issue-46604.rs:1:25
33
|
44
LL | static buf: &mut [u8] = &mut [1u8,2,3,4,5,7];
5-
| ^^^^^^^^^^^^^^^^^^^^ statics require immutable values
6-
|
7-
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8-
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
5+
| ^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn`
96

107
error[E0594]: cannot assign to `buf[_]`, as `buf` is an immutable static item
118
--> $DIR/issue-46604.rs:6:5
@@ -15,5 +12,5 @@ LL | buf[0]=2;
1512

1613
error: aborting due to 2 previous errors
1714

18-
Some errors have detailed explanations: E0594, E0658.
15+
Some errors have detailed explanations: E0594, E0764.
1916
For more information about an error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)
Please sign in to comment.