Skip to content

Commit 0c5a964

Browse files
authored
Unrolled build for #141698
Rollup merge of #141698 - oli-obk:ctfe-err-flip, r=RalfJung Use the informative error as the main const eval error message r? `@RalfJung` I only did the minimal changes necessary to the const eval error machinery. I'd prefer not to mix test changes with refactorings 😆
2 parents 59aa1e8 + 020216c commit 0c5a964

File tree

445 files changed

+4132
-4299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

445 files changed

+4132
-4299
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ const_eval_dyn_call_not_a_method =
8989
`dyn` call trying to call something that is not a method
9090
9191
const_eval_error = {$error_kind ->
92-
[static] could not evaluate static initializer
93-
[const] evaluation of constant value failed
94-
[const_with_path] evaluation of `{$instance}` failed
92+
[static] evaluation of static initializer failed here
93+
[const] evaluation of constant value failed here
94+
[const_with_path] evaluation of `{$instance}` failed here
9595
*[other] {""}
9696
}
9797

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::mem;
22

3-
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg};
3+
use rustc_errors::{Diag, DiagArgName, DiagArgValue, DiagMessage, IntoDiagArg};
44
use rustc_middle::mir::AssertKind;
55
use rustc_middle::mir::interpret::{Provenance, ReportedErrorInfo};
66
use rustc_middle::query::TyCtxtAt;
@@ -131,10 +131,10 @@ pub fn get_span_and_frames<'tcx>(
131131

132132
/// Create a diagnostic for a const eval error.
133133
///
134-
/// This will use the `mk` function for creating the error which will get passed labels according to
135-
/// the `InterpError` and the span and a stacktrace of current execution according to
136-
/// `get_span_and_frames`.
137-
pub(super) fn report<'tcx, C, F, E>(
134+
/// This will use the `mk` function for adding more information to the error.
135+
/// You can use it to add a stacktrace of current execution according to
136+
/// `get_span_and_frames` or just give context on where the const eval error happened.
137+
pub(super) fn report<'tcx, C, F>(
138138
tcx: TyCtxt<'tcx>,
139139
error: InterpErrorKind<'tcx>,
140140
span: Span,
@@ -143,8 +143,7 @@ pub(super) fn report<'tcx, C, F, E>(
143143
) -> ErrorHandled
144144
where
145145
C: FnOnce() -> (Span, Vec<FrameNote>),
146-
F: FnOnce(Span, Vec<FrameNote>) -> E,
147-
E: Diagnostic<'tcx>,
146+
F: FnOnce(&mut Diag<'_>, Span, Vec<FrameNote>),
148147
{
149148
// Special handling for certain errors
150149
match error {
@@ -163,20 +162,17 @@ where
163162
_ => {
164163
let (our_span, frames) = get_span_and_frames();
165164
let span = span.substitute_dummy(our_span);
166-
let err = mk(span, frames);
167-
let mut err = tcx.dcx().create_err(err);
165+
let mut err = tcx.dcx().struct_span_err(our_span, error.diagnostic_message());
168166
// We allow invalid programs in infallible promoteds since invalid layouts can occur
169167
// anyway (e.g. due to size overflow). And we allow OOM as that can happen any time.
170168
let allowed_in_infallible = matches!(
171169
error,
172170
InterpErrorKind::ResourceExhaustion(_) | InterpErrorKind::InvalidProgram(_)
173171
);
174172

175-
let msg = error.diagnostic_message();
176173
error.add_args(&mut err);
177174

178-
// Use *our* span to label the interp error
179-
err.span_label(our_span, msg);
175+
mk(&mut err, span, frames);
180176
let g = err.emit();
181177
let reported = if allowed_in_infallible {
182178
ReportedErrorInfo::allowed_in_infallible(g)

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::atomic::Ordering::Relaxed;
22

33
use either::{Left, Right};
44
use rustc_abi::{self as abi, BackendRepr};
5+
use rustc_errors::E0080;
56
use rustc_hir::def::DefKind;
67
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo, ReportedErrorInfo};
78
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
@@ -290,12 +291,18 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
290291
|error| {
291292
let span = tcx.def_span(def_id);
292293

294+
// FIXME(oli-obk): why don't we have any tests for this code path?
293295
super::report(
294296
tcx,
295297
error.into_kind(),
296298
span,
297299
|| (span, vec![]),
298-
|span, _| errors::NullaryIntrinsicError { span },
300+
|diag, span, _| {
301+
diag.span_label(
302+
span,
303+
crate::fluent_generated::const_eval_nullary_intrinsic_fail,
304+
);
305+
},
299306
)
300307
},
301308
);
@@ -443,11 +450,15 @@ fn report_eval_error<'tcx>(
443450
error,
444451
DUMMY_SP,
445452
|| super::get_span_and_frames(ecx.tcx, ecx.stack()),
446-
|span, frames| errors::ConstEvalError {
447-
span,
448-
error_kind: kind,
449-
instance,
450-
frame_notes: frames,
453+
|diag, span, frames| {
454+
// FIXME(oli-obk): figure out how to use structured diagnostics again.
455+
diag.code(E0080);
456+
diag.span_label(span, crate::fluent_generated::const_eval_error);
457+
diag.arg("instance", instance);
458+
diag.arg("error_kind", kind);
459+
for frame in frames {
460+
diag.subdiagnostic(frame);
461+
}
451462
},
452463
)
453464
}
@@ -477,6 +488,15 @@ fn report_validation_error<'tcx>(
477488
error,
478489
DUMMY_SP,
479490
|| crate::const_eval::get_span_and_frames(ecx.tcx, ecx.stack()),
480-
move |span, frames| errors::ValidationFailure { span, ub_note: (), frames, raw_bytes },
491+
move |diag, span, frames| {
492+
// FIXME(oli-obk): figure out how to use structured diagnostics again.
493+
diag.code(E0080);
494+
diag.span_label(span, crate::fluent_generated::const_eval_validation_failure);
495+
diag.note(crate::fluent_generated::const_eval_validation_failure_note);
496+
for frame in frames {
497+
diag.subdiagnostic(frame);
498+
}
499+
diag.subdiagnostic(raw_bytes);
500+
},
481501
)
482502
}

compiler/rustc_const_eval/src/errors.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -439,38 +439,6 @@ pub struct LiveDrop<'tcx> {
439439
pub dropped_at: Span,
440440
}
441441

442-
#[derive(Diagnostic)]
443-
#[diag(const_eval_error, code = E0080)]
444-
pub struct ConstEvalError {
445-
#[primary_span]
446-
pub span: Span,
447-
/// One of "const", "const_with_path", and "static"
448-
pub error_kind: &'static str,
449-
pub instance: String,
450-
#[subdiagnostic]
451-
pub frame_notes: Vec<FrameNote>,
452-
}
453-
454-
#[derive(Diagnostic)]
455-
#[diag(const_eval_nullary_intrinsic_fail)]
456-
pub struct NullaryIntrinsicError {
457-
#[primary_span]
458-
pub span: Span,
459-
}
460-
461-
#[derive(Diagnostic)]
462-
#[diag(const_eval_validation_failure, code = E0080)]
463-
pub struct ValidationFailure {
464-
#[primary_span]
465-
pub span: Span,
466-
#[note(const_eval_validation_failure_note)]
467-
pub ub_note: (),
468-
#[subdiagnostic]
469-
pub frames: Vec<FrameNote>,
470-
#[subdiagnostic]
471-
pub raw_bytes: RawBytesNote,
472-
}
473-
474442
pub trait ReportErrorExt {
475443
/// Returns the diagnostic message for this error.
476444
fn diagnostic_message(&self) -> DiagMessage;

src/tools/clippy/tests/ui/indexing_slicing_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
// This should be linted, since `suppress-restriction-lint-in-const` default is false.
6969
const { &ARR[idx4()] };
7070
//~^ ERROR: indexing may panic
71-
//~| ERROR: evaluation of `main
71+
//~| ERROR: index out of bounds
7272

7373
let y = &x;
7474
// Ok, referencing shouldn't affect this lint. See the issue 6021

src/tools/clippy/tests/ui/indexing_slicing_index.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-re
99
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
1010
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
1111

12-
error[E0080]: evaluation of `main::{constant#3}` failed
12+
error[E0080]: index out of bounds: the length is 2 but the index is 4
1313
--> tests/ui/indexing_slicing_index.rs:69:14
1414
|
1515
LL | const { &ARR[idx4()] };
16-
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
16+
| ^^^^^^^^^^^ evaluation of `main::{constant#3}` failed here
1717

1818
note: erroneous constant encountered
1919
--> tests/ui/indexing_slicing_index.rs:69:5

src/tools/miri/tests/fail/const-ub-checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const UNALIGNED_READ: () = unsafe {
22
let x = &[0u8; 4];
33
let ptr = x.as_ptr().cast::<u32>();
4-
ptr.read(); //~ERROR: evaluation of constant value failed
4+
ptr.read(); //~ERROR: accessing memory based on pointer with alignment 1, but alignment 4 is required
55
};
66

77
fn main() {

src/tools/miri/tests/fail/const-ub-checks.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> tests/fail/const-ub-checks.rs:LL:CC
33
|
44
LL | ptr.read();
5-
| ^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^^^^^^^ evaluation of constant value failed here
66

77
note: erroneous constant encountered
88
--> tests/fail/const-ub-checks.rs:LL:CC

src/tools/miri/tests/fail/erroneous_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
struct PrintName<T>(T);
66
impl<T> PrintName<T> {
7-
const VOID: ! = panic!(); //~ERROR: evaluation of `PrintName::<i32>::VOID` failed
7+
const VOID: ! = panic!(); //~ERROR: explicit panic
88
}
99

1010
fn no_codegen<T>() {

src/tools/miri/tests/fail/erroneous_const.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of `PrintName::<i32>::VOID` failed
1+
error[E0080]: evaluation panicked: explicit panic
22
--> tests/fail/erroneous_const.rs:LL:CC
33
|
44
LL | const VOID: ! = panic!();
5-
| ^^^^^^^^ evaluation panicked: explicit panic
5+
| ^^^^^^^^ evaluation of `PrintName::<i32>::VOID` failed here
66

77
note: erroneous constant encountered
88
--> tests/fail/erroneous_const.rs:LL:CC

src/tools/miri/tests/fail/erroneous_const2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const X: u32 = 5;
22
const Y: u32 = 6;
33
const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
4-
//~^ERROR: evaluation of constant value failed
4+
//~^ERROR: overflow
55

66
#[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/5391
77
fn main() {

src/tools/miri/tests/fail/erroneous_const2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: attempt to compute `5_u32 - 6_u32`, which would overflow
22
--> tests/fail/erroneous_const2.rs:LL:CC
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
5-
| ^^^^^ attempt to compute `5_u32 - 6_u32`, which would overflow
5+
| ^^^^^ evaluation of constant value failed here
66

77
note: erroneous constant encountered
88
--> tests/fail/erroneous_const2.rs:LL:CC

tests/rustdoc-ui/const-evalutation-ice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::cell::Cell;
44
use std::mem;
55

66
pub struct S {
7-
s: Cell<usize>
7+
s: Cell<usize>,
88
}
99

1010
pub const N: usize = 0 - (mem::size_of::<S>() != 400) as usize;
11-
//~^ ERROR evaluation of constant value failed
11+
//~^ ERROR overflow

tests/rustdoc-ui/const-evalutation-ice.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: attempt to compute `0_usize - 1_usize`, which would overflow
22
--> $DIR/const-evalutation-ice.rs:10:22
33
|
44
LL | pub const N: usize = 0 - (mem::size_of::<S>() != 400) as usize;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of constant value failed here
66

77
error: aborting due to 1 previous error
88

tests/ui/array-slice-vec/array_const_index-0.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const A: &'static [i32] = &[];
22
const B: i32 = (&A)[1];
3-
//~^ NOTE index out of bounds: the length is 0 but the index is 1
4-
//~| ERROR evaluation of constant value failed
3+
//~^ ERROR index out of bounds: the length is 0 but the index is 1
54

65
fn main() {
76
let _ = B;

tests/ui/array-slice-vec/array_const_index-0.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: index out of bounds: the length is 0 but the index is 1
22
--> $DIR/array_const_index-0.rs:2:16
33
|
44
LL | const B: i32 = (&A)[1];
5-
| ^^^^^^^ index out of bounds: the length is 0 but the index is 1
5+
| ^^^^^^^ evaluation of constant value failed here
66

77
error: aborting due to 1 previous error
88

tests/ui/array-slice-vec/array_const_index-1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const A: [i32; 0] = [];
22
const B: i32 = A[1];
3-
//~^ NOTE index out of bounds: the length is 0 but the index is 1
4-
//~| ERROR evaluation of constant value failed
3+
//~^ ERROR index out of bounds: the length is 0 but the index is 1
54

65
fn main() {
76
let _ = B;

tests/ui/array-slice-vec/array_const_index-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: index out of bounds: the length is 0 but the index is 1
22
--> $DIR/array_const_index-1.rs:2:16
33
|
44
LL | const B: i32 = A[1];
5-
| ^^^^ index out of bounds: the length is 0 but the index is 1
5+
| ^^^^ evaluation of constant value failed here
66

77
error: aborting due to 1 previous error
88

tests/ui/asm/fail-const-eval-issue-121099.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error[E0080]: evaluation of `{global_asm#0}::{constant#0}` failed
1+
error[E0080]: attempt to shift left by `500_i32`, which would overflow
22
--> $DIR/fail-const-eval-issue-121099.rs:8:31
33
|
44
LL | global_asm!("/* {} */", const 1 << 500);
5-
| ^^^^^^^^ attempt to shift left by `500_i32`, which would overflow
5+
| ^^^^^^^^ evaluation of `{global_asm#0}::{constant#0}` failed here
66

7-
error[E0080]: evaluation of `{global_asm#1}::{constant#0}` failed
7+
error[E0080]: attempt to divide `1_i32` by zero
88
--> $DIR/fail-const-eval-issue-121099.rs:10:31
99
|
1010
LL | global_asm!("/* {} */", const 1 / 0);
11-
| ^^^^^ attempt to divide `1_i32` by zero
11+
| ^^^^^ evaluation of `{global_asm#1}::{constant#0}` failed here
1212

1313
error: aborting due to 2 previous errors
1414

tests/ui/associated-consts/defaults-not-assumed-fail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Tr {
77
// This should not be a constant evaluation error (overflow). The value of
88
// `Self::A` must not be assumed to hold inside the trait.
99
const B: u8 = Self::A + 1;
10-
//~^ ERROR evaluation of `<() as Tr>::B` failed
10+
//~^ ERROR overflow
1111
}
1212

1313
// An impl that doesn't override any constant will NOT cause a const eval error

tests/ui/associated-consts/defaults-not-assumed-fail.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of `<() as Tr>::B` failed
1+
error[E0080]: attempt to compute `u8::MAX + 1_u8`, which would overflow
22
--> $DIR/defaults-not-assumed-fail.rs:9:19
33
|
44
LL | const B: u8 = Self::A + 1;
5-
| ^^^^^^^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow
5+
| ^^^^^^^^^^^ evaluation of `<() as Tr>::B` failed here
66

77
note: erroneous constant encountered
88
--> $DIR/defaults-not-assumed-fail.rs:34:16

tests/ui/borrowck/issue-81899.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//@ dont-require-annotations: NOTE
55

6-
const _CONST: &[u8] = &f(&[], |_| {}); //~ ERROR evaluation of constant value failed
6+
const _CONST: &[u8] = &f(&[], |_| {}); //~ ERROR explicit panic
77
//~^ NOTE constant
88

99
const fn f<F>(_: &[u8], _: F) -> &[u8]

tests/ui/borrowck/issue-81899.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: evaluation panicked: explicit panic
22
--> $DIR/issue-81899.rs:6:24
33
|
44
LL | const _CONST: &[u8] = &f(&[], |_| {});
5-
| ^^^^^^^^^^^^^^ evaluation panicked: explicit panic
5+
| ^^^^^^^^^^^^^^ evaluation of constant value failed here
66
|
77
note: inside `f::<{closure@$DIR/issue-81899.rs:6:31: 6:34}>`
88
--> $DIR/issue-81899.rs:13:5

tests/ui/borrowck/issue-88434-minimal-example.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//@ dont-require-annotations: NOTE
44

5-
const _CONST: &() = &f(&|_| {}); //~ ERROR evaluation of constant value failed
5+
const _CONST: &() = &f(&|_| {}); //~ ERROR explicit panic
66
//~^ NOTE constant
77

88
const fn f<F>(_: &F)
@@ -12,4 +12,4 @@ where
1212
panic!() //~ NOTE inside `f
1313
}
1414

15-
fn main() { }
15+
fn main() {}

0 commit comments

Comments
 (0)