Skip to content

Commit c31ca9a

Browse files
committed
const eval errors: display the current item instance if there are generics involved
1 parent 949aca6 commit c31ca9a

File tree

9 files changed

+28
-15
lines changed

9 files changed

+28
-15
lines changed

compiler/rustc_mir/src/const_eval/eval_queries.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1616
use rustc_middle::ty::{self, subst::Subst, TyCtxt};
1717
use rustc_span::source_map::Span;
1818
use rustc_target::abi::{Abi, LayoutOf};
19+
use std::borrow::Cow;
1920
use std::convert::TryInto;
2021

2122
pub fn note_on_undefined_behavior_error() -> &'static str {
@@ -328,11 +329,22 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
328329
))
329330
} else {
330331
let msg = if is_static {
331-
"could not evaluate static initializer"
332+
Cow::from("could not evaluate static initializer")
332333
} else {
333-
"evaluation of constant value failed"
334+
// If the current item has generics, we'd like to enrich the message with the
335+
// instance and its substs: to show the actual compile-time values, in addition to
336+
// the expression, leading to the const eval error.
337+
let instance = &key.value.instance;
338+
if !instance.substs.is_empty() {
339+
let instance = with_no_trimmed_paths(|| instance.to_string());
340+
let msg = format!("evaluation of `{}` failed", instance);
341+
Cow::from(msg)
342+
} else {
343+
Cow::from("evaluation of constant value failed")
344+
}
334345
};
335-
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), msg))
346+
347+
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
336348
}
337349
}
338350
Ok(mplace) => {

src/test/ui/const-generics/const_evaluatable_checked/from-sig-fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(incomplete_features)]
33

44
fn test<const N: usize>() -> [u8; N - 1] {
5-
//~^ ERROR evaluation of constant
5+
//~^ ERROR evaluation of `test::<0_usize>::{constant#0}` failed
66
todo!()
77
}
88

src/test/ui/const-generics/const_evaluatable_checked/from-sig-fail.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: evaluation of `test::<0_usize>::{constant#0}` failed
22
--> $DIR/from-sig-fail.rs:4:35
33
|
44
LL | fn test<const N: usize>() -> [u8; N - 1] {

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0080]: evaluation of constant value failed
2-
--> $DIR/simple_fail.rs:9:48
1+
error[E0080]: evaluation of `test::<0_usize>::{constant#0}` failed
2+
--> $DIR/simple_fail.rs:10:48
33
|
44
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
55
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
66

7-
error[E0080]: evaluation of constant value failed
7+
error[E0080]: evaluation of `Arr::<0_usize>::{constant#0}` failed
88
--> $DIR/simple_fail.rs:6:33
99
|
1010
LL | type Arr<const N: usize> = [u8; N - 1];

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | type Arr<const N: usize> = [u8; N - 1];
88
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
11-
--> $DIR/simple_fail.rs:9:48
11+
--> $DIR/simple_fail.rs:10:48
1212
|
1313
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
1414
| ^ cannot perform const operation using `N`

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
#![cfg_attr(full, feature(const_evaluatable_checked))]
44
#![allow(incomplete_features)]
55

6-
type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR evaluation of constant
6+
type Arr<const N: usize> = [u8; N - 1];
77
//[min]~^ ERROR generic parameters may not be used in const operations
8+
//[full]~^^ ERROR evaluation of `Arr::<0_usize>::{constant#0}` failed
89

910
fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
1011
//[min]~^ ERROR generic parameters may not be used in const operations
11-
//[full]~^^ ERROR evaluation of constant
12+
//[full]~^^ ERROR evaluation of `test::<0_usize>::{constant#0}` failed
1213
todo!()
1314
}
1415

src/test/ui/consts/const-eval/issue-50814-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | const BAR: usize = [5, 6, 7][T::BOO];
1010
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1111
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1212

13-
error[E0080]: evaluation of constant value failed
13+
error[E0080]: evaluation of `foo::<()>` failed
1414
--> $DIR/issue-50814-2.rs:19:6
1515
|
1616
LL | &<A<T> as Foo<T>>::BAR

src/test/ui/consts/const-eval/issue-50814.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | const MAX: u8 = A::MAX + B::MAX;
1010
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1111
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1212

13-
error[E0080]: evaluation of constant value failed
13+
error[E0080]: evaluation of `foo::<i32>` failed
1414
--> $DIR/issue-50814.rs:21:6
1515
|
1616
LL | &Sum::<U8,U8>::MAX

src/test/ui/mir/issue-80742.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: evaluation of `Inline::<dyn std::fmt::Debug>::{constant#0}` failed
22
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
33
|
44
LL | intrinsics::size_of::<T>()
@@ -35,7 +35,7 @@ LL | pub trait Debug {
3535
= note: the following trait bounds were not satisfied:
3636
`dyn Debug: Sized`
3737

38-
error[E0080]: evaluation of constant value failed
38+
error[E0080]: evaluation of `Inline::<dyn std::fmt::Debug>::{constant#0}` failed
3939
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
4040
|
4141
LL | intrinsics::size_of::<T>()

0 commit comments

Comments
 (0)