Skip to content

Commit 67f455a

Browse files
committed
Suppress "erroneous constant used" for constants tainted by errors
When constant evaluation fails because its MIR is tainted by errors, suppress note indicating that erroneous constant was used, since those errors have to be fixed regardless of the constant being used or not.
1 parent 2913ad6 commit 67f455a

33 files changed

+76
-248
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ impl<'tcx> ConstEvalErr<'tcx> {
169169
// See <https://github.com/rust-lang/rust/pull/63152>.
170170
let mut err = struct_error(tcx, &self.error.to_string());
171171
self.decorate(&mut err, decorate);
172-
ErrorHandled::Reported(err.emit())
172+
ErrorHandled::Reported(err.emit().into())
173173
}
174174
_ => {
175175
// Report as hard error.
176176
let mut err = struct_error(tcx, message);
177177
err.span_label(self.span, self.error.to_string());
178178
self.decorate(&mut err, decorate);
179-
ErrorHandled::Reported(err.emit())
179+
ErrorHandled::Reported(err.emit().into())
180180
}
181181
}
182182
}

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
382382
rustc_span::DUMMY_SP,
383383
"This is likely a const item that is missing from its impl",
384384
);
385-
throw_inval!(AlreadyReported(guar));
385+
throw_inval!(AlreadyReported(guar.into()));
386386
} else {
387387
// `find_mir_or_eval_fn` checks that this is a const fn before even calling us,
388388
// so this should be unreachable.

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use either::{Either, Left, Right};
77
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
88
use rustc_index::IndexVec;
99
use rustc_middle::mir;
10-
use rustc_middle::mir::interpret::{ErrorHandled, InterpError};
10+
use rustc_middle::mir::interpret::{ErrorHandled, InterpError, ReportedErrorInfo};
1111
use rustc_middle::ty::layout::{
1212
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
1313
TyAndLayout,
@@ -470,7 +470,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
470470
};
471471
// do not continue if typeck errors occurred (can only occur in local crate)
472472
if let Some(err) = body.tainted_by_errors {
473-
throw_inval!(AlreadyReported(err));
473+
throw_inval!(AlreadyReported(ReportedErrorInfo::tainted_by_errors(err)));
474474
}
475475
Ok(body)
476476
}
@@ -517,7 +517,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
517517
Ok(None) => throw_inval!(TooGeneric),
518518

519519
// FIXME(eddyb) this could be a bit more specific than `AlreadyReported`.
520-
Err(error_reported) => throw_inval!(AlreadyReported(error_reported)),
520+
Err(error_reported) => throw_inval!(AlreadyReported(error_reported.into())),
521521
}
522522
}
523523

@@ -905,7 +905,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
905905
query(self.tcx.at(span.unwrap_or_else(|| self.cur_span()))).map_err(|err| {
906906
match err {
907907
ErrorHandled::Reported(err) => {
908-
if let Some(span) = span {
908+
if !err.is_tainted_by_errors() && let Some(span) = span {
909909
// To make it easier to figure out where this error comes from, also add a note at the current location.
910910
self.tcx.sess.span_note_without_error(span, "erroneous constant used");
911911
}

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
595595
// FIXME(generic_const_exprs): `ConstKind::Expr` should be able to be evaluated
596596
ty::ConstKind::Expr(_) => throw_inval!(TooGeneric),
597597
ty::ConstKind::Error(reported) => {
598-
throw_inval!(AlreadyReported(reported))
598+
throw_inval!(AlreadyReported(reported.into()))
599599
}
600600
ty::ConstKind::Unevaluated(uv) => {
601601
let instance = self.resolve(uv.def, uv.substs)?;

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ impl<'tcx> InferCtxt<'tcx> {
15331533
if let Some(ct) = tcx.thir_abstract_const(unevaluated.def)? {
15341534
let ct = tcx.expand_abstract_consts(ct.subst(tcx, substs));
15351535
if let Err(e) = ct.error_reported() {
1536-
return Err(ErrorHandled::Reported(e));
1536+
return Err(ErrorHandled::Reported(e.into()));
15371537
} else if ct.has_non_region_infer() || ct.has_non_region_param() {
15381538
return Err(ErrorHandled::TooGeneric);
15391539
} else {

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,49 @@ use std::{any::Any, backtrace::Backtrace, fmt};
1515
pub enum ErrorHandled {
1616
/// Already reported an error for this evaluation, and the compilation is
1717
/// *guaranteed* to fail. Warnings/lints *must not* produce `Reported`.
18-
Reported(ErrorGuaranteed),
18+
Reported(ReportedErrorInfo),
1919
/// Don't emit an error, the evaluation failed because the MIR was generic
2020
/// and the substs didn't fully monomorphize it.
2121
TooGeneric,
2222
}
2323

2424
impl From<ErrorGuaranteed> for ErrorHandled {
25-
fn from(err: ErrorGuaranteed) -> ErrorHandled {
26-
ErrorHandled::Reported(err)
25+
#[inline]
26+
fn from(error: ErrorGuaranteed) -> ErrorHandled {
27+
ErrorHandled::Reported(error.into())
28+
}
29+
}
30+
31+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
32+
pub struct ReportedErrorInfo {
33+
error: ErrorGuaranteed,
34+
is_tainted_by_errors: bool,
35+
}
36+
37+
impl ReportedErrorInfo {
38+
#[inline]
39+
pub fn tainted_by_errors(error: ErrorGuaranteed) -> ReportedErrorInfo {
40+
ReportedErrorInfo { is_tainted_by_errors: true, error }
41+
}
42+
43+
/// Returns true if evaluation failed because MIR was tainted by errors.
44+
#[inline]
45+
pub fn is_tainted_by_errors(self) -> bool {
46+
self.is_tainted_by_errors
47+
}
48+
}
49+
50+
impl From<ErrorGuaranteed> for ReportedErrorInfo {
51+
#[inline]
52+
fn from(error: ErrorGuaranteed) -> ReportedErrorInfo {
53+
ReportedErrorInfo { is_tainted_by_errors: false, error }
54+
}
55+
}
56+
57+
impl Into<ErrorGuaranteed> for ReportedErrorInfo {
58+
#[inline]
59+
fn into(self) -> ErrorGuaranteed {
60+
self.error
2761
}
2862
}
2963

@@ -89,7 +123,7 @@ fn print_backtrace(backtrace: &Backtrace) {
89123

90124
impl From<ErrorGuaranteed> for InterpErrorInfo<'_> {
91125
fn from(err: ErrorGuaranteed) -> Self {
92-
InterpError::InvalidProgram(InvalidProgramInfo::AlreadyReported(err)).into()
126+
InterpError::InvalidProgram(InvalidProgramInfo::AlreadyReported(err.into())).into()
93127
}
94128
}
95129

@@ -125,7 +159,7 @@ pub enum InvalidProgramInfo<'tcx> {
125159
/// Resolution can fail if we are in a too generic context.
126160
TooGeneric,
127161
/// Abort in case errors are already reported.
128-
AlreadyReported(ErrorGuaranteed),
162+
AlreadyReported(ReportedErrorInfo),
129163
/// An error occurred during layout computation.
130164
Layout(layout::LayoutError<'tcx>),
131165
/// An error occurred during FnAbi computation: the passed --target lacks FFI support
@@ -144,7 +178,7 @@ impl fmt::Display for InvalidProgramInfo<'_> {
144178
use InvalidProgramInfo::*;
145179
match self {
146180
TooGeneric => write!(f, "encountered overly generic constant"),
147-
AlreadyReported(ErrorGuaranteed { .. }) => {
181+
AlreadyReported(_) => {
148182
write!(
149183
f,
150184
"an error has already been reported elsewhere (this should not usually be printed)"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ use crate::ty::{self, Instance, Ty, TyCtxt};
120120
pub use self::error::{
121121
struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult,
122122
EvalToValTreeResult, InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo,
123-
MachineStopType, ResourceExhaustionInfo, ScalarSizeMismatch, UndefinedBehaviorInfo,
124-
UninitBytesAccess, UnsupportedOpInfo,
123+
MachineStopType, ReportedErrorInfo, ResourceExhaustionInfo, ScalarSizeMismatch,
124+
UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo,
125125
};
126126

127127
pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> TyCtxt<'tcx> {
6161
self.const_eval_global_id(param_env, cid, span)
6262
}
6363
Ok(None) => Err(ErrorHandled::TooGeneric),
64-
Err(error_reported) => Err(ErrorHandled::Reported(error_reported)),
64+
Err(err) => Err(ErrorHandled::Reported(err.into())),
6565
}
6666
}
6767

@@ -110,7 +110,7 @@ impl<'tcx> TyCtxt<'tcx> {
110110
})
111111
}
112112
Ok(None) => Err(ErrorHandled::TooGeneric),
113-
Err(error_reported) => Err(ErrorHandled::Reported(error_reported)),
113+
Err(err) => Err(ErrorHandled::Reported(err.into())),
114114
}
115115
}
116116

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,7 @@ impl<'tcx> ConstantKind<'tcx> {
23422342
match tcx.const_eval_resolve(param_env, uneval, None) {
23432343
Ok(val) => Self::Val(val, ty),
23442344
Err(ErrorHandled::TooGeneric) => self,
2345-
Err(ErrorHandled::Reported(guar)) => Self::Ty(tcx.const_error(ty, guar)),
2345+
Err(ErrorHandled::Reported(guar)) => Self::Ty(tcx.const_error(ty, guar.into())),
23462346
}
23472347
}
23482348
}

compiler/rustc_middle/src/ty/consts/kind.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'tcx> ConstKind<'tcx> {
245245
// can leak through `val` into the const we return.
246246
Ok(val) => Some(Ok(EvalResult::ValTree(val?))),
247247
Err(ErrorHandled::TooGeneric) => None,
248-
Err(ErrorHandled::Reported(e)) => Some(Err(e)),
248+
Err(ErrorHandled::Reported(e)) => Some(Err(e.into())),
249249
}
250250
}
251251
EvalMode::Mir => {
@@ -256,7 +256,7 @@ impl<'tcx> ConstKind<'tcx> {
256256
// can leak through `val` into the const we return.
257257
Ok(val) => Some(Ok(EvalResult::ConstVal(val))),
258258
Err(ErrorHandled::TooGeneric) => None,
259-
Err(ErrorHandled::Reported(e)) => Some(Err(e)),
259+
Err(ErrorHandled::Reported(e)) => Some(Err(e.into())),
260260
}
261261
}
262262
}

0 commit comments

Comments
 (0)