1
Fork 0

Rollup merge of #86340 - Smittyvb:ctfe-hard-error-message, r=RalfJung

Use better error message for hard errors in CTFE

I noticed this while working on #86255: currently the same message is used for hard errors and soft errors in CTFE. This changes the error messages to make hard errors use a message that indicates the reality of the situation correctly, since usage of the constant is never allowed when there was a hard error evaluating it. This doesn't affect the behaviour of these error messages, only the content.

This changes the error logic to check if the error should be hard or soft where it is generated, instead of where it is emitted, to allow this distinction in error messages.
This commit is contained in:
Yuki Okushi 2021-06-17 21:56:43 +09:00 committed by GitHub
commit c062f3dddd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 80 additions and 107 deletions

View file

@ -157,7 +157,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
tcx: TyCtxtAt<'tcx>,
message: &str,
emit: impl FnOnce(DiagnosticBuilder<'_>),
mut lint_root: Option<hir::HirId>,
lint_root: Option<hir::HirId>,
) -> ErrorHandled {
let finish = |mut err: DiagnosticBuilder<'_>, span_msg: Option<String>| {
trace!("reporting const eval failure at {:?}", self.span);
@ -194,12 +194,6 @@ impl<'tcx> ConstEvalErr<'tcx> {
_ => {}
};
// If we have a 'hard error', then set `lint_root` to `None` so that we don't
// emit a lint.
if matches!(&self.error, InterpError::MachineStop(err) if err.is_hard_err()) {
lint_root = None;
}
let err_msg = self.error.to_string();
// Regular case - emit a lint.

View file

@ -312,22 +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!(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 {
@ -345,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) => {