Skip to content

Commit f97f2a4

Browse files
Migrate MutDeref, TransientMutBorrow diagnostics
1 parent 584e5d4 commit f97f2a4

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

compiler/rustc_const_eval/src/errors.rs

+24
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,27 @@ pub(crate) struct PanicNonStrErr {
6363
#[primary_span]
6464
pub span: Span,
6565
}
66+
67+
#[derive(SessionDiagnostic)]
68+
#[error(const_eval::mut_deref, code = "E0658")]
69+
pub(crate) struct MutDerefErr {
70+
#[primary_span]
71+
pub span: Span,
72+
pub kind: ConstContext,
73+
}
74+
75+
#[derive(SessionDiagnostic)]
76+
#[error(const_eval::transient_mut_borrow, code = "E0658")]
77+
pub(crate) struct TransientMutBorrowErr {
78+
#[primary_span]
79+
pub span: Span,
80+
pub kind: ConstContext,
81+
}
82+
83+
#[derive(SessionDiagnostic)]
84+
#[error(const_eval::transient_mut_borrow_raw, code = "E0658")]
85+
pub(crate) struct TransientMutBorrowErrRaw {
86+
#[primary_span]
87+
pub span: Span,
88+
pub kind: ConstContext,
89+
}

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use rustc_trait_selection::traits::SelectionContext;
2323

2424
use super::ConstCx;
2525
use crate::errors::{
26-
NonConstOpErr, PanicNonStrErr, RawPtrComparisonErr, RawPtrToIntErr, StaticAccessErr,
26+
MutDerefErr, NonConstOpErr, PanicNonStrErr, RawPtrComparisonErr, RawPtrToIntErr,
27+
StaticAccessErr, TransientMutBorrowErr, TransientMutBorrowErrRaw,
2728
};
2829
use crate::util::{call_kind, CallDesugaringKind, CallKind};
2930

@@ -595,17 +596,17 @@ impl<'tcx> NonConstOp<'tcx> for TransientMutBorrow {
595596
ccx: &ConstCx<'_, 'tcx>,
596597
span: Span,
597598
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
598-
let raw = match self.0 {
599-
hir::BorrowKind::Raw => "raw ",
600-
hir::BorrowKind::Ref => "",
601-
};
602-
603-
feature_err(
604-
&ccx.tcx.sess.parse_sess,
605-
sym::const_mut_refs,
606-
span,
607-
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
608-
)
599+
let kind = ccx.const_kind();
600+
match self.0 {
601+
hir::BorrowKind::Raw => ccx
602+
.tcx
603+
.sess
604+
.create_feature_err(TransientMutBorrowErrRaw { span, kind }, sym::const_mut_refs),
605+
hir::BorrowKind::Ref => ccx
606+
.tcx
607+
.sess
608+
.create_feature_err(TransientMutBorrowErr { span, kind }, sym::const_mut_refs),
609+
}
609610
}
610611
}
611612

@@ -626,12 +627,9 @@ impl<'tcx> NonConstOp<'tcx> for MutDeref {
626627
ccx: &ConstCx<'_, 'tcx>,
627628
span: Span,
628629
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
629-
feature_err(
630-
&ccx.tcx.sess.parse_sess,
631-
sym::const_mut_refs,
632-
span,
633-
&format!("mutation through a reference is not allowed in {}s", ccx.const_kind()),
634-
)
630+
ccx.tcx
631+
.sess
632+
.create_feature_err(MutDerefErr { span, kind: ccx.const_kind() }, sym::const_mut_refs)
635633
}
636634
}
637635

compiler/rustc_error_messages/locales/en-US/const_eval.ftl

+19
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,22 @@ const-eval-raw-ptr-comparison =
2626
.note = see issue #53020 <https://github.com/rust-lang/rust/issues/53020> for more information
2727
2828
const-eval-panic-non-str = argument to `panic!()` in a const context must have type `&str`
29+
30+
const-eval-mut-deref =
31+
mutation through a reference is not allowed in { $kind ->
32+
[constant function] constant functions
33+
[static] statics
34+
*[constant] constants
35+
}
36+
37+
const-eval-transient-mut-borrow = mutable references are not allowed in { $kind ->
38+
[constant function] constant functions
39+
[static] statics
40+
*[constant] constants
41+
}
42+
43+
const-eval-transient-mut-borrow-raw = raw mutable references are not allowed in { $kind ->
44+
[constant function] constant functions
45+
[static] statics
46+
*[constant] constants
47+
}

compiler/rustc_session/src/session.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::cgu_reuse_tracker::CguReuseTracker;
22
use crate::code_stats::CodeStats;
33
pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
44
use crate::config::{self, CrateType, OutputType, SwitchWithOptPath};
5-
use crate::parse::ParseSess;
5+
use crate::parse::{add_feature_diagnostics, ParseSess};
66
use crate::search_paths::{PathKind, SearchPath};
77
use crate::{filesearch, lint};
88

@@ -458,6 +458,15 @@ impl Session {
458458
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
459459
self.parse_sess.create_err(err)
460460
}
461+
pub fn create_feature_err<'a>(
462+
&'a self,
463+
err: impl SessionDiagnostic<'a>,
464+
feature: Symbol,
465+
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
466+
let mut err = self.parse_sess.create_err(err);
467+
add_feature_diagnostics(&mut err, &self.parse_sess, feature);
468+
err
469+
}
461470
pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed {
462471
self.parse_sess.emit_err(err)
463472
}

0 commit comments

Comments
 (0)