Skip to content

Commit 6245b95

Browse files
authored
Rollup merge of #77420 - ecstatic-morse:const-checking-raw-mut-ref, r=davidtwco
Unify const-checking structured errors for `&mut` and `&raw mut` Resolves #77414 as well as a FIXME.
2 parents 1eaadeb + c1494d6 commit 6245b95

File tree

6 files changed

+32
-54
lines changed

6 files changed

+32
-54
lines changed

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

+11-27
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ impl NonConstOp for CellBorrow {
224224
}
225225

226226
#[derive(Debug)]
227-
pub struct MutBorrow;
227+
pub struct MutBorrow(pub hir::BorrowKind);
228+
228229
impl NonConstOp for MutBorrow {
229230
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
230231
// Forbid everywhere except in const fn with a feature gate
@@ -236,22 +237,28 @@ impl NonConstOp for MutBorrow {
236237
}
237238

238239
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
240+
let raw = match self.0 {
241+
hir::BorrowKind::Raw => "raw ",
242+
hir::BorrowKind::Ref => "",
243+
};
244+
239245
let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn {
240246
feature_err(
241247
&ccx.tcx.sess.parse_sess,
242248
sym::const_mut_refs,
243249
span,
244-
&format!("mutable references are not allowed in {}s", ccx.const_kind()),
250+
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
245251
)
246252
} else {
247253
let mut err = struct_span_err!(
248254
ccx.tcx.sess,
249255
span,
250256
E0764,
251-
"mutable references are not allowed in {}s",
257+
"{}mutable references are not allowed in {}s",
258+
raw,
252259
ccx.const_kind(),
253260
);
254-
err.span_label(span, format!("`&mut` is only allowed in `const fn`"));
261+
err.span_label(span, format!("`&{}mut` is only allowed in `const fn`", raw));
255262
err
256263
};
257264
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
@@ -270,29 +277,6 @@ impl NonConstOp for MutBorrow {
270277
}
271278
}
272279

273-
// FIXME(ecstaticmorse): Unify this with `MutBorrow`. It has basically the same issues.
274-
#[derive(Debug)]
275-
pub struct MutAddressOf;
276-
impl NonConstOp for MutAddressOf {
277-
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
278-
// Forbid everywhere except in const fn with a feature gate
279-
if ccx.const_kind() == hir::ConstContext::ConstFn {
280-
Status::Unstable(sym::const_mut_refs)
281-
} else {
282-
Status::Forbidden
283-
}
284-
}
285-
286-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
287-
feature_err(
288-
&ccx.tcx.sess.parse_sess,
289-
sym::const_mut_refs,
290-
span,
291-
&format!("`&raw mut` is not allowed in {}s", ccx.const_kind()),
292-
)
293-
}
294-
}
295-
296280
#[derive(Debug)]
297281
pub struct MutDeref;
298282
impl NonConstOp for MutDeref {

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,16 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
525525

526526
if !is_allowed {
527527
if let BorrowKind::Mut { .. } = kind {
528-
self.check_op(ops::MutBorrow);
528+
self.check_op(ops::MutBorrow(hir::BorrowKind::Ref));
529529
} else {
530530
self.check_op(ops::CellBorrow);
531531
}
532532
}
533533
}
534534

535-
Rvalue::AddressOf(Mutability::Mut, _) => self.check_op(ops::MutAddressOf),
535+
Rvalue::AddressOf(Mutability::Mut, _) => {
536+
self.check_op(ops::MutBorrow(hir::BorrowKind::Raw))
537+
}
536538

537539
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place)
538540
| Rvalue::AddressOf(Mutability::Not, ref place) => {
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![feature(raw_ref_op)]
22

3-
const A: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed
3+
const A: () = { let mut x = 2; &raw mut x; }; //~ mutable reference
44

5-
static B: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed
5+
static B: () = { let mut x = 2; &raw mut x; }; //~ mutable reference
66

7-
static mut C: () = { let mut x = 2; &raw mut x; }; //~ ERROR `&raw mut` is not allowed
7+
static mut C: () = { let mut x = 2; &raw mut x; }; //~ mutable reference
88

99
const fn foo() {
1010
let mut x = 0;
11-
let y = &raw mut x; //~ ERROR `&raw mut` is not allowed
11+
let y = &raw mut x; //~ mutable reference
1212
}
1313

1414
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
error[E0658]: `&raw mut` is not allowed in constants
1+
error[E0764]: 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-
| ^^^^^^^^^^
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+
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
96

10-
error[E0658]: `&raw mut` is not allowed in statics
7+
error[E0764]: raw mutable references are not allowed in statics
118
--> $DIR/const-address-of-mut.rs:5:33
129
|
1310
LL | static B: () = { let mut x = 2; &raw mut x; };
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
11+
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
1812

19-
error[E0658]: `&raw mut` is not allowed in statics
13+
error[E0764]: raw mutable references are not allowed in statics
2014
--> $DIR/const-address-of-mut.rs:7:37
2115
|
2216
LL | static mut C: () = { let mut x = 2; &raw mut x; };
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
17+
| ^^^^^^^^^^ `&raw mut` is only allowed in `const fn`
2718

28-
error[E0658]: `&raw mut` is not allowed in constant functions
19+
error[E0658]: raw mutable references are not allowed in constant functions
2920
--> $DIR/const-address-of-mut.rs:11:13
3021
|
3122
LL | let y = &raw mut x;
@@ -36,4 +27,5 @@ LL | let y = &raw mut x;
3627

3728
error: aborting due to 4 previous errors
3829

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

src/test/ui/consts/min_const_fn/address_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
const fn mutable_address_of_in_const() {
44
let mut a = 0;
5-
let b = &raw mut a; //~ ERROR `&raw mut` is not allowed
5+
let b = &raw mut a; //~ ERROR mutable reference
66
}
77

88
struct X;
99

1010
impl X {
1111
const fn inherent_mutable_address_of_in_const() {
1212
let mut a = 0;
13-
let b = &raw mut a; //~ ERROR `&raw mut` is not allowed
13+
let b = &raw mut a; //~ ERROR mutable reference
1414
}
1515
}
1616

src/test/ui/consts/min_const_fn/address_of.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `&raw mut` is not allowed in constant functions
1+
error[E0658]: raw mutable references are not allowed in constant functions
22
--> $DIR/address_of.rs:5:13
33
|
44
LL | let b = &raw mut a;
@@ -7,7 +7,7 @@ LL | let b = &raw mut a;
77
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
88
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
99

10-
error[E0658]: `&raw mut` is not allowed in constant functions
10+
error[E0658]: raw mutable references are not allowed in constant functions
1111
--> $DIR/address_of.rs:13:17
1212
|
1313
LL | let b = &raw mut a;

0 commit comments

Comments
 (0)