Skip to content

Commit dc820e0

Browse files
committed
report const_panic diagnostics identically to compiler_error invocations
1 parent e9dd18c commit dc820e0

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::sync::Lock;
77
use rustc_errors::{pluralize, struct_span_err, DiagnosticBuilder, ErrorReported};
88
use rustc_macros::HashStable;
99
use rustc_session::CtfeBacktrace;
10-
use rustc_span::def_id::DefId;
10+
use rustc_span::{def_id::DefId, Symbol};
1111
use rustc_target::abi::{Align, Size};
1212
use std::{any::Any, backtrace::Backtrace, fmt, mem};
1313

@@ -439,6 +439,8 @@ pub enum InterpError<'tcx> {
439439
/// The program exhausted the interpreter's resources (stack/heap too big,
440440
/// execution takes too long, ...).
441441
ResourceExhaustion(ResourceExhaustionInfo),
442+
/// The program terminated immediately from a panic.
443+
Panic(Symbol),
442444
/// Stop execution for a machine-controlled reason. This is never raised by
443445
/// the core engine itself.
444446
MachineStop(Box<dyn MachineStopType>),
@@ -454,6 +456,7 @@ impl fmt::Display for InterpError<'_> {
454456
InvalidProgram(ref msg) => write!(f, "{}", msg),
455457
UndefinedBehavior(ref msg) => write!(f, "{}", msg),
456458
ResourceExhaustion(ref msg) => write!(f, "{}", msg),
459+
Panic(ref msg) => write!(f, "{}", msg),
457460
MachineStop(ref msg) => write!(f, "{}", msg),
458461
}
459462
}

compiler/rustc_mir/src/const_eval/error.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ pub enum ConstEvalErrKind {
2222
Panic { msg: Symbol, line: u32, col: u32, file: Symbol },
2323
}
2424

25-
// The errors become `MachineStop` with plain strings when being raised.
25+
// Non-panic errors become `MachineStop` with plain strings when being raised.
2626
// `ConstEvalErr` (in `librustc_middle/mir/interpret/error.rs`) knows to
2727
// handle these.
2828
impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind {
2929
fn into(self) -> InterpErrorInfo<'tcx> {
30-
err_machine_stop!(self.to_string()).into()
30+
match self {
31+
ConstEvalErrKind::Panic { msg, .. } => InterpError::Panic(msg).into(),
32+
_ => err_machine_stop!(self.to_string()).into(),
33+
}
3134
}
3235
}
3336

@@ -150,13 +153,14 @@ impl<'tcx> ConstEvalErr<'tcx> {
150153
};
151154
trace!("reporting const eval failure at {:?}", self.span);
152155

153-
let err_msg = match &self.error {
156+
let (err_msg, is_panic) = match &self.error {
154157
InterpError::MachineStop(msg) => {
155158
// A custom error (`ConstEvalErrKind` in `librustc_mir/interp/const_eval/error.rs`).
156159
// Should be turned into a string by now.
157-
msg.downcast_ref::<String>().expect("invalid MachineStop payload").clone()
160+
(msg.downcast_ref::<String>().expect("invalid MachineStop payload").clone(), false)
158161
}
159-
err => err.to_string(),
162+
InterpError::Panic(msg) => (msg.to_string(), true),
163+
err => (err.to_string(), false),
160164
};
161165

162166
let finish = |mut err: DiagnosticBuilder<'_>, span_msg: Option<String>| {
@@ -193,7 +197,13 @@ impl<'tcx> ConstEvalErr<'tcx> {
193197
rustc_session::lint::builtin::CONST_ERR,
194198
hir_id,
195199
tcx.span,
196-
|lint| finish(lint.build(message), Some(err_msg)),
200+
|lint| {
201+
if is_panic {
202+
finish(lint.build(&err_msg), None)
203+
} else {
204+
finish(lint.build(message), Some(err_msg))
205+
}
206+
},
197207
);
198208
ErrorHandled::Linted
199209
} else {

0 commit comments

Comments
 (0)