Skip to content

Commit 3e34be0

Browse files
authored
Rollup merge of #111602 - tmiasko:erroneous-constant-used, r=oli-obk
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. Fixes #110891.
2 parents f6da357 + 67f455a commit 3e34be0

33 files changed

+76
-248
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+2-2
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

+1-1
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

+4-4
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

+1-1
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

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

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

+40-6
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

+2-2
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

+2-2
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

+1-1
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

+2-2
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
}

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
801801
span: tcx.def_span(unevaluated.def),
802802
unevaluated: unevaluated,
803803
});
804-
Err(ErrorHandled::Reported(reported))
804+
Err(ErrorHandled::Reported(reported.into()))
805805
}
806806
Err(err) => Err(err),
807807
}

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn is_const_evaluatable<'tcx>(
7979
"Missing value for constant, but no error reported?",
8080
)))
8181
}
82-
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
82+
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e.into())),
8383
Ok(_) => Ok(()),
8484
}
8585
}
@@ -147,7 +147,7 @@ pub fn is_const_evaluatable<'tcx>(
147147

148148
Err(err)
149149
}
150-
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e)),
150+
Err(ErrorHandled::Reported(e)) => Err(NotConstEvaluatable::Error(e.into())),
151151
Ok(_) => Ok(()),
152152
}
153153
}

compiler/rustc_trait_selection/src/traits/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
615615
(Err(ErrorHandled::Reported(reported)), _)
616616
| (_, Err(ErrorHandled::Reported(reported))) => ProcessResult::Error(
617617
CodeSelectionError(SelectionError::NotConstEvaluatable(
618-
NotConstEvaluatable::Error(reported),
618+
NotConstEvaluatable::Error(reported.into()),
619619
)),
620620
),
621621
(Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {

src/tools/miri/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn report_error<'tcx, 'mir>(
289289
(None, format!("see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information")),
290290
],
291291
InvalidProgram(
292-
InvalidProgramInfo::AlreadyReported(rustc_errors::ErrorGuaranteed { .. })
292+
InvalidProgramInfo::AlreadyReported(_)
293293
) => {
294294
// This got already reported. No point in reporting it again.
295295
return None;

src/tools/miri/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ extern crate rustc_ast;
4747
extern crate rustc_middle;
4848
extern crate rustc_const_eval;
4949
extern crate rustc_data_structures;
50-
extern crate rustc_errors;
5150
extern crate rustc_hir;
5251
extern crate rustc_index;
5352
extern crate rustc_session;

tests/ui/consts/const-eval/format.stderr

-56
Original file line numberDiff line numberDiff line change
@@ -43,62 +43,6 @@ LL | println!("{:?}", 0);
4343
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4444
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
4545

46-
note: erroneous constant used
47-
--> $DIR/format.rs:2:12
48-
|
49-
LL | panic!("{:?}", 0);
50-
| ^^^^^^
51-
52-
note: erroneous constant used
53-
--> $DIR/format.rs:2:12
54-
|
55-
LL | panic!("{:?}", 0);
56-
| ^^^^^^
57-
58-
note: erroneous constant used
59-
--> $DIR/format.rs:2:20
60-
|
61-
LL | panic!("{:?}", 0);
62-
| ^
63-
|
64-
= note: this note originates in the macro `$crate::const_format_args` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
65-
66-
note: erroneous constant used
67-
--> $DIR/format.rs:2:20
68-
|
69-
LL | panic!("{:?}", 0);
70-
| ^
71-
|
72-
= note: this note originates in the macro `$crate::const_format_args` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
73-
74-
note: erroneous constant used
75-
--> $DIR/format.rs:8:14
76-
|
77-
LL | println!("{:?}", 0);
78-
| ^^^^^^
79-
80-
note: erroneous constant used
81-
--> $DIR/format.rs:8:14
82-
|
83-
LL | println!("{:?}", 0);
84-
| ^^^^^^
85-
86-
note: erroneous constant used
87-
--> $DIR/format.rs:8:22
88-
|
89-
LL | println!("{:?}", 0);
90-
| ^
91-
|
92-
= note: this note originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
93-
94-
note: erroneous constant used
95-
--> $DIR/format.rs:8:22
96-
|
97-
LL | println!("{:?}", 0);
98-
| ^
99-
|
100-
= note: this note originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
101-
10246
error: aborting due to 5 previous errors
10347

10448
For more information about this error, try `rustc --explain E0015`.

tests/ui/consts/const-integer-bool-ops.rs

-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const X: usize = 42 && 39;
66
//~| ERROR mismatched types
77
//~| expected `usize`, found `bool`
88
const ARR: [i32; X] = [99; 34];
9-
//~^ constant
109

1110
const X1: usize = 42 || 39;
1211
//~^ ERROR mismatched types
@@ -16,7 +15,6 @@ const X1: usize = 42 || 39;
1615
//~| ERROR mismatched types
1716
//~| expected `usize`, found `bool`
1817
const ARR1: [i32; X1] = [99; 47];
19-
//~^ constant
2018

2119
const X2: usize = -42 || -39;
2220
//~^ ERROR mismatched types
@@ -26,7 +24,6 @@ const X2: usize = -42 || -39;
2624
//~| ERROR mismatched types
2725
//~| expected `usize`, found `bool`
2826
const ARR2: [i32; X2] = [99; 18446744073709551607];
29-
//~^ constant
3027

3128
const X3: usize = -42 && -39;
3229
//~^ ERROR mismatched types
@@ -36,43 +33,36 @@ const X3: usize = -42 && -39;
3633
//~| ERROR mismatched types
3734
//~| expected `usize`, found `bool`
3835
const ARR3: [i32; X3] = [99; 6];
39-
//~^ constant
4036

4137
const Y: usize = 42.0 == 42.0;
4238
//~^ ERROR mismatched types
4339
//~| expected `usize`, found `bool`
4440
const ARRR: [i32; Y] = [99; 1];
45-
//~^ constant
4641

4742
const Y1: usize = 42.0 >= 42.0;
4843
//~^ ERROR mismatched types
4944
//~| expected `usize`, found `bool`
5045
const ARRR1: [i32; Y1] = [99; 1];
51-
//~^ constant
5246

5347
const Y2: usize = 42.0 <= 42.0;
5448
//~^ ERROR mismatched types
5549
//~| expected `usize`, found `bool`
5650
const ARRR2: [i32; Y2] = [99; 1];
57-
//~^ constant
5851

5952
const Y3: usize = 42.0 > 42.0;
6053
//~^ ERROR mismatched types
6154
//~| expected `usize`, found `bool`
6255
const ARRR3: [i32; Y3] = [99; 0];
63-
//~^ constant
6456

6557
const Y4: usize = 42.0 < 42.0;
6658
//~^ ERROR mismatched types
6759
//~| expected `usize`, found `bool`
6860
const ARRR4: [i32; Y4] = [99; 0];
69-
//~^ constant
7061

7162
const Y5: usize = 42.0 != 42.0;
7263
//~^ ERROR mismatched types
7364
//~| expected `usize`, found `bool`
7465
const ARRR5: [i32; Y5] = [99; 0];
75-
//~^ constant
7666

7767
fn main() {
7868
let _ = ARR;

0 commit comments

Comments
 (0)