Skip to content

Commit dac512e

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

0 commit comments

Comments
 (0)