Skip to content

Commit 044b362

Browse files
committed
Move some hard error logic to InterpError
1 parent 4fe4ff9 commit 044b362

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,14 @@ impl InterpError<'_> {
502502
_ => false,
503503
}
504504
}
505+
506+
/// Should this error be reported as a hard error, preventing compilation, or a soft error,
507+
/// causing a deny-by-default lint?
508+
pub fn is_hard_err(&self) -> bool {
509+
use InterpError::*;
510+
match *self {
511+
MachineStop(ref err) => err.is_hard_err(),
512+
_ => false,
513+
}
514+
}
505515
}

compiler/rustc_mir/src/const_eval/eval_queries.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, Memory
22
use crate::interpret::eval_nullary_intrinsic;
33
use crate::interpret::{
44
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
5-
Immediate, InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy,
6-
RefTracking, Scalar, ScalarMaybeUninit, StackPopCleanup,
5+
Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
6+
ScalarMaybeUninit, StackPopCleanup,
77
};
88
use crate::util::pretty::display_allocation;
99

@@ -312,23 +312,17 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
312312
let err = ConstEvalErr::new(&ecx, error, None);
313313
// Some CTFE errors raise just a lint, not a hard error; see
314314
// <https://github.com/rust-lang/rust/issues/71800>.
315-
let emit_as_lint = if let Some(def) = def.as_local() {
315+
let is_hard_err = if let Some(def) = def.as_local() {
316316
// (Associated) consts only emit a lint, since they might be unused.
317-
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
318-
&& !matches!(&err.error, InterpError::MachineStop(err) if err.is_hard_err())
317+
!matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
318+
// check if the inner InterpError is hard
319+
|| err.error.is_hard_err()
319320
} else {
320321
// use of broken constant from other crate: always an error
321-
false
322+
true
322323
};
323-
if emit_as_lint {
324-
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
325-
Err(err.report_as_lint(
326-
tcx.at(tcx.def_span(def.did)),
327-
"any use of this value will cause an error",
328-
hir_id,
329-
Some(err.span),
330-
))
331-
} else {
324+
325+
if is_hard_err {
332326
let msg = if is_static {
333327
Cow::from("could not evaluate static initializer")
334328
} else {
@@ -346,6 +340,14 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
346340
};
347341

348342
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
343+
} else {
344+
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
345+
Err(err.report_as_lint(
346+
tcx.at(tcx.def_span(def.did)),
347+
"any use of this value will cause an error",
348+
hir_id,
349+
Some(err.span),
350+
))
349351
}
350352
}
351353
Ok(mplace) => {

0 commit comments

Comments
 (0)