Skip to content

Commit d118021

Browse files
committed
Permit mutable references in all const contexts
1 parent 1986b58 commit d118021

35 files changed

+298
-171
lines changed

compiler/rustc_mir/src/transform/check_consts/ops.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -268,43 +268,41 @@ impl NonConstOp for CellBorrow {
268268
}
269269

270270
#[derive(Debug)]
271+
/// This op is for `&mut` borrows in the trailing expression of a constant
272+
/// which uses the "enclosing scopes rule" to leak its locals into anonymous
273+
/// static or const items.
271274
pub struct MutBorrow(pub hir::BorrowKind);
272275

273276
impl NonConstOp for MutBorrow {
274277
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
275-
// Forbid everywhere except in const fn with a feature gate
276-
if ccx.const_kind() == hir::ConstContext::ConstFn {
277-
Status::Unstable(sym::const_mut_refs)
278-
} else {
279-
Status::Forbidden
278+
match ccx.const_kind() {
279+
// Mutable statics can handle mutable references in their final value
280+
hir::ConstContext::Static(hir::Mutability::Mut) => Status::Allowed,
281+
_ => Status::Forbidden,
280282
}
281283
}
282284

285+
fn importance(&self) -> DiagnosticImportance {
286+
// If there were primary errors (like non-const function calls), do not emit further
287+
// errors about mutable references.
288+
DiagnosticImportance::Secondary
289+
}
290+
283291
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
284292
let raw = match self.0 {
285293
hir::BorrowKind::Raw => "raw ",
286294
hir::BorrowKind::Ref => "",
287295
};
288296

289-
let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn {
290-
feature_err(
291-
&ccx.tcx.sess.parse_sess,
292-
sym::const_mut_refs,
293-
span,
294-
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
295-
)
296-
} else {
297-
let mut err = struct_span_err!(
298-
ccx.tcx.sess,
299-
span,
300-
E0764,
301-
"{}mutable references are not allowed in {}s",
302-
raw,
303-
ccx.const_kind(),
304-
);
305-
err.span_label(span, format!("`&{}mut` is only allowed in `const fn`", raw));
306-
err
307-
};
297+
let mut err = struct_span_err!(
298+
ccx.tcx.sess,
299+
span,
300+
E0764,
301+
"{}mutable references are not allowed in final value of {}s",
302+
raw,
303+
ccx.const_kind(),
304+
);
305+
308306
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
309307
err.note(
310308
"References in statics and constants may only refer \
@@ -321,6 +319,29 @@ impl NonConstOp for MutBorrow {
321319
}
322320
}
323321

322+
#[derive(Debug)]
323+
pub struct TransientMutBorrow(pub hir::BorrowKind);
324+
325+
impl NonConstOp for TransientMutBorrow {
326+
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
327+
Status::Unstable(sym::const_mut_refs)
328+
}
329+
330+
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
331+
let raw = match self.0 {
332+
hir::BorrowKind::Raw => "raw ",
333+
hir::BorrowKind::Ref => "",
334+
};
335+
336+
feature_err(
337+
&ccx.tcx.sess.parse_sess,
338+
sym::const_mut_refs,
339+
span,
340+
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
341+
)
342+
}
343+
}
344+
324345
#[derive(Debug)]
325346
pub struct MutDeref;
326347
impl NonConstOp for MutDeref {
@@ -329,7 +350,7 @@ impl NonConstOp for MutDeref {
329350
}
330351

331352
fn importance(&self) -> DiagnosticImportance {
332-
// Usually a side-effect of a `MutBorrow` somewhere.
353+
// Usually a side-effect of a `TransientMutBorrow` somewhere.
333354
DiagnosticImportance::Secondary
334355
}
335356

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,29 @@ impl Validator<'mir, 'tcx> {
466466
}
467467
}
468468
}
469+
470+
fn check_mut_borrow(&mut self, local: Local, kind: hir::BorrowKind) {
471+
match self.const_kind() {
472+
// In a const fn all borrows are transient or point to the places given via
473+
// references in the arguments (so we already checked them with
474+
// TransientMutBorrow/MutBorrow as appropriate).
475+
// The borrow checker guarantees that no new non-transient borrows are created.
476+
// NOTE: Once we have heap allocations during CTFE we need to figure out
477+
// how to prevent `const fn` to create long-lived allocations that point
478+
// to mutable memory.
479+
hir::ConstContext::ConstFn => self.check_op(ops::TransientMutBorrow(kind)),
480+
_ => {
481+
// Locals with StorageDead do not live beyond the evaluation and can
482+
// thus safely be borrowed without being able to be leaked to the final
483+
// value of the constant.
484+
if self.local_has_storage_dead(local) {
485+
self.check_op(ops::TransientMutBorrow(kind));
486+
} else {
487+
self.check_op(ops::MutBorrow(kind));
488+
}
489+
}
490+
}
491+
}
469492
}
470493

471494
impl Visitor<'tcx> for Validator<'mir, 'tcx> {
@@ -562,15 +585,15 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
562585

563586
if !is_allowed {
564587
if let BorrowKind::Mut { .. } = kind {
565-
self.check_op(ops::MutBorrow(hir::BorrowKind::Ref));
588+
self.check_mut_borrow(place.local, hir::BorrowKind::Ref)
566589
} else {
567590
self.check_op(ops::CellBorrow);
568591
}
569592
}
570593
}
571594

572-
Rvalue::AddressOf(Mutability::Mut, _) => {
573-
self.check_op(ops::MutBorrow(hir::BorrowKind::Raw))
595+
Rvalue::AddressOf(Mutability::Mut, ref place) => {
596+
self.check_mut_borrow(place.local, hir::BorrowKind::Raw)
574597
}
575598

576599
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place)
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 mutable references are not allowed in statics
4+
//~^ ERROR mutable references are not allowed
55

66
pub fn main() { }

src/test/ui/check-static-immutable-mut-slices.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0764]: mutable references are not allowed in statics
1+
error[E0764]: mutable references are not allowed in final value of statics
22
--> $DIR/check-static-immutable-mut-slices.rs:3:37
33
|
44
LL | static TEST: &'static mut [isize] = &mut [];
5-
| ^^^^^^^ `&mut` is only allowed in `const fn`
5+
| ^^^^^^^
66

77
error: aborting due to previous error
88

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
error[E0764]: raw mutable references are not allowed in constants
1+
error[E0658]: raw mutable references are not allowed in constants
22
--> $DIR/const-address-of-mut.rs:3:32
33
|
44
LL | const A: () = { let mut x = 2; &raw mut x; };
5-
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
5+
| ^^^^^^^^^^
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
69

7-
error[E0764]: raw mutable references are not allowed in statics
10+
error[E0658]: raw mutable references are not allowed in statics
811
--> $DIR/const-address-of-mut.rs:5:33
912
|
1013
LL | static B: () = { let mut x = 2; &raw mut x; };
11-
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
14+
| ^^^^^^^^^^
15+
|
16+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
17+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
1218

13-
error[E0764]: raw mutable references are not allowed in statics
19+
error[E0658]: raw mutable references are not allowed in statics
1420
--> $DIR/const-address-of-mut.rs:7:37
1521
|
1622
LL | static mut C: () = { let mut x = 2; &raw mut x; };
17-
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
23+
| ^^^^^^^^^^
24+
|
25+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
26+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
1827

1928
error[E0658]: raw mutable references are not allowed in constant functions
2029
--> $DIR/const-address-of-mut.rs:11:13
@@ -27,5 +36,4 @@ LL | let y = &raw mut x;
2736

2837
error: aborting due to 4 previous errors
2938

30-
Some errors have detailed explanations: E0658, E0764.
31-
For more information about an error, try `rustc --explain E0658`.
39+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/consts/const-eval/issue-65394.stderr

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error[E0764]: mutable references are not allowed in constants
1+
error[E0658]: mutable references are not allowed in constants
22
--> $DIR/issue-65394.rs:8:13
33
|
44
LL | let r = &mut x;
5-
| ^^^^^^ `&mut` is only allowed in `const fn`
5+
| ^^^^^^
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
69

710
error[E0493]: destructors cannot be evaluated at compile-time
811
--> $DIR/issue-65394.rs:7:9
@@ -15,5 +18,5 @@ LL | };
1518

1619
error: aborting due to 2 previous errors
1720

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

src/test/ui/consts/const-multi-ref.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error[E0764]: mutable references are not allowed in constants
1+
error[E0658]: mutable references are not allowed in constants
22
--> $DIR/const-multi-ref.rs:6:13
33
|
44
LL | let p = &mut a;
5-
| ^^^^^^ `&mut` is only allowed in `const fn`
5+
| ^^^^^^
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
69

710
error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
811
--> $DIR/const-multi-ref.rs:16:13
@@ -15,5 +18,4 @@ LL | let p = &a;
1518

1619
error: aborting due to 2 previous errors
1720

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// check-pass
12
#![feature(const_mut_refs)]
23
#![feature(const_fn)]
34
#![feature(raw_ref_op)]
@@ -22,9 +23,7 @@ const fn baz(foo: &mut Foo)-> *mut usize {
2223

2324
const _: () = {
2425
foo().bar();
25-
//~^ ERROR mutable references are not allowed in constants
2626
baz(&mut foo());
27-
//~^ ERROR mutable references are not allowed in constants
2827
};
2928

3029
fn main() {}

src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// check-pass
12
#![feature(const_mut_refs)]
23

34
struct Foo {
@@ -29,9 +30,6 @@ const fn bazz(foo: &mut Foo) -> usize {
2930

3031
fn main() {
3132
let _: [(); foo().bar()] = [(); 1];
32-
//~^ ERROR mutable references are not allowed in constants
3333
let _: [(); baz(&mut foo())] = [(); 2];
34-
//~^ ERROR mutable references are not allowed in constants
3534
let _: [(); bazz(&mut foo())] = [(); 3];
36-
//~^ ERROR mutable references are not allowed in constants
3735
}

0 commit comments

Comments
 (0)