Skip to content

Commit 77e6c56

Browse files
Unify &mut and &raw mut const-checking errors
1 parent 2ad6187 commit 77e6c56

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ impl NonConstOp for CellBorrow {
235235
}
236236

237237
#[derive(Debug)]
238-
pub struct MutBorrow;
238+
pub struct MutBorrow(pub hir::BorrowKind);
239+
239240
impl NonConstOp for MutBorrow {
240241
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
241242
// Forbid everywhere except in const fn with a feature gate
@@ -247,22 +248,28 @@ impl NonConstOp for MutBorrow {
247248
}
248249

249250
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
251+
let raw = match self.0 {
252+
hir::BorrowKind::Raw => "raw ",
253+
hir::BorrowKind::Ref => "",
254+
};
255+
250256
let mut err = if ccx.const_kind() == hir::ConstContext::ConstFn {
251257
feature_err(
252258
&ccx.tcx.sess.parse_sess,
253259
sym::const_mut_refs,
254260
span,
255-
&format!("mutable references are not allowed in {}s", ccx.const_kind()),
261+
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
256262
)
257263
} else {
258264
let mut err = struct_span_err!(
259265
ccx.tcx.sess,
260266
span,
261267
E0764,
262-
"mutable references are not allowed in {}s",
268+
"{}mutable references are not allowed in {}s",
269+
raw,
263270
ccx.const_kind(),
264271
);
265-
err.span_label(span, format!("`&mut` is only allowed in `const fn`"));
272+
err.span_label(span, format!("`&{}mut` is only allowed in `const fn`", raw));
266273
err
267274
};
268275
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
@@ -281,29 +288,6 @@ impl NonConstOp for MutBorrow {
281288
}
282289
}
283290

284-
// FIXME(ecstaticmorse): Unify this with `MutBorrow`. It has basically the same issues.
285-
#[derive(Debug)]
286-
pub struct MutAddressOf;
287-
impl NonConstOp for MutAddressOf {
288-
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
289-
// Forbid everywhere except in const fn with a feature gate
290-
if ccx.const_kind() == hir::ConstContext::ConstFn {
291-
Status::Unstable(sym::const_mut_refs)
292-
} else {
293-
Status::Forbidden
294-
}
295-
}
296-
297-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
298-
feature_err(
299-
&ccx.tcx.sess.parse_sess,
300-
sym::const_mut_refs,
301-
span,
302-
&format!("`&raw mut` is not allowed in {}s", ccx.const_kind()),
303-
)
304-
}
305-
}
306-
307291
#[derive(Debug)]
308292
pub struct MutDeref;
309293
impl NonConstOp for MutDeref {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,16 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
522522

523523
if !is_allowed {
524524
if let BorrowKind::Mut { .. } = kind {
525-
self.check_op(ops::MutBorrow);
525+
self.check_op(ops::MutBorrow(hir::BorrowKind::Ref));
526526
} else {
527527
self.check_op(ops::CellBorrow);
528528
}
529529
}
530530
}
531531

532-
Rvalue::AddressOf(Mutability::Mut, _) => self.check_op(ops::MutAddressOf),
532+
Rvalue::AddressOf(Mutability::Mut, _) => {
533+
self.check_op(ops::MutBorrow(hir::BorrowKind::Raw))
534+
}
533535

534536
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place)
535537
| Rvalue::AddressOf(Mutability::Not, ref place) => {

0 commit comments

Comments
 (0)