Move some hard error logic to InterpError

This commit is contained in:
Smitty 2021-06-16 18:23:34 -04:00
parent 4fe4ff95f6
commit 044b3620e7
2 changed files with 27 additions and 15 deletions

View File

@ -502,4 +502,14 @@ pub fn formatted_string(&self) -> bool {
_ => false, _ => 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,
}
}
} }

View File

@ -2,8 +2,8 @@
use crate::interpret::eval_nullary_intrinsic; use crate::interpret::eval_nullary_intrinsic;
use crate::interpret::{ use crate::interpret::{
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId, intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
Immediate, InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, Immediate, InternKind, InterpCx, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, Scalar,
RefTracking, Scalar, ScalarMaybeUninit, StackPopCleanup, ScalarMaybeUninit, StackPopCleanup,
}; };
use crate::util::pretty::display_allocation; use crate::util::pretty::display_allocation;
@ -312,23 +312,17 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
let err = ConstEvalErr::new(&ecx, error, None); let err = ConstEvalErr::new(&ecx, error, None);
// Some CTFE errors raise just a lint, not a hard error; see // Some CTFE errors raise just a lint, not a hard error; see
// <https://github.com/rust-lang/rust/issues/71800>. // <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. // (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!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
&& !matches!(&err.error, InterpError::MachineStop(err) if err.is_hard_err()) // check if the inner InterpError is hard
|| err.error.is_hard_err()
} else { } else {
// use of broken constant from other crate: always an error // 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); if is_hard_err {
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 {
let msg = if is_static { let msg = if is_static {
Cow::from("could not evaluate static initializer") Cow::from("could not evaluate static initializer")
} else { } else {
@ -346,6 +340,14 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
}; };
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg)) 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) => { Ok(mplace) => {