Skip to content

Commit

Permalink
Move some hard error logic to InterpError
Browse files Browse the repository at this point in the history
  • Loading branch information
syvb committed Jun 16, 2021
1 parent 4fe4ff9 commit 044b362
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,14 @@ impl InterpError<'_> {
_ => false,
}
}

/// Should this error be reported as a hard error, preventing compilation, or a soft error,
/// causing a deny-by-default lint?
pub fn is_hard_err(&self) -> bool {
use InterpError::*;
match *self {
MachineStop(ref err) => err.is_hard_err(),
_ => false,
}
}
}
32 changes: 17 additions & 15 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, Memory
use crate::interpret::eval_nullary_intrinsic;
use crate::interpret::{
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
Immediate, InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy,
RefTracking, Scalar, ScalarMaybeUninit, StackPopCleanup,
Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
ScalarMaybeUninit, StackPopCleanup,
};
use crate::util::pretty::display_allocation;

Expand Down Expand Up @@ -312,23 +312,17 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
let err = ConstEvalErr::new(&ecx, error, None);
// Some CTFE errors raise just a lint, not a hard error; see
// <https://github.com/rust-lang/rust/issues/71800>.
let emit_as_lint = if let Some(def) = def.as_local() {
let is_hard_err = if let Some(def) = def.as_local() {
// (Associated) consts only emit a lint, since they might be unused.
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
&& !matches!(&err.error, InterpError::MachineStop(err) if err.is_hard_err())
!matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
// check if the inner InterpError is hard
|| err.error.is_hard_err()
} else {
// use of broken constant from other crate: always an error
false
true
};
if emit_as_lint {
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
Err(err.report_as_lint(
tcx.at(tcx.def_span(def.did)),
"any use of this value will cause an error",
hir_id,
Some(err.span),
))
} else {

if is_hard_err {
let msg = if is_static {
Cow::from("could not evaluate static initializer")
} else {
Expand All @@ -346,6 +340,14 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
};

Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
} else {
let hir_id = tcx.hir().local_def_id_to_hir_id(def.as_local().unwrap().did);
Err(err.report_as_lint(
tcx.at(tcx.def_span(def.did)),
"any use of this value will cause an error",
hir_id,
Some(err.span),
))
}
}
Ok(mplace) => {
Expand Down

0 comments on commit 044b362

Please sign in to comment.