Skip to content

Commit dac512e

Browse files
authored
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

Lines changed: 1 addition & 0 deletions
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
Lines changed: 39 additions & 0 deletions
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

Lines changed: 23 additions & 11 deletions
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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 1 addition & 1 deletion
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() { }
Lines changed: 3 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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
};
Lines changed: 3 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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;
Lines changed: 3 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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() {}
Lines changed: 15 additions & 0 deletions
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

Lines changed: 3 additions & 2 deletions
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
}
Lines changed: 21 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 11 deletions
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`.
Lines changed: 9 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 6 deletions
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`.
Lines changed: 4 additions & 5 deletions
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
}
Lines changed: 9 additions & 0 deletions
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`.
Lines changed: 4 additions & 4 deletions
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`.

0 commit comments

Comments
 (0)