const eval errors: display the current item instance if there are generics involved

This commit is contained in:
Rémy Rakic 2021-05-27 22:42:07 +02:00
parent 949aca6eeb
commit c31ca9a42c
9 changed files with 28 additions and 15 deletions

View file

@ -16,6 +16,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, subst::Subst, TyCtxt};
use rustc_span::source_map::Span;
use rustc_target::abi::{Abi, LayoutOf};
use std::borrow::Cow;
use std::convert::TryInto;
pub fn note_on_undefined_behavior_error() -> &'static str {
@ -328,11 +329,22 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
))
} else {
let msg = if is_static {
"could not evaluate static initializer"
Cow::from("could not evaluate static initializer")
} else {
"evaluation of constant value failed"
// If the current item has generics, we'd like to enrich the message with the
// instance and its substs: to show the actual compile-time values, in addition to
// the expression, leading to the const eval error.
let instance = &key.value.instance;
if !instance.substs.is_empty() {
let instance = with_no_trimmed_paths(|| instance.to_string());
let msg = format!("evaluation of `{}` failed", instance);
Cow::from(msg)
} else {
Cow::from("evaluation of constant value failed")
}
};
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), msg))
Err(err.report_as_error(ecx.tcx.at(ecx.cur_span()), &msg))
}
}
Ok(mplace) => {

View file

@ -2,7 +2,7 @@
#![allow(incomplete_features)]
fn test<const N: usize>() -> [u8; N - 1] {
//~^ ERROR evaluation of constant
//~^ ERROR evaluation of `test::<0_usize>::{constant#0}` failed
todo!()
}

View file

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

View file

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

View file

@ -8,7 +8,7 @@ LL | type Arr<const N: usize> = [u8; N - 1];
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
error: generic parameters may not be used in const operations
--> $DIR/simple_fail.rs:9:48
--> $DIR/simple_fail.rs:10:48
|
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
| ^ cannot perform const operation using `N`

View file

@ -3,12 +3,13 @@
#![cfg_attr(full, feature(const_evaluatable_checked))]
#![allow(incomplete_features)]
type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR evaluation of constant
type Arr<const N: usize> = [u8; N - 1];
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR evaluation of `Arr::<0_usize>::{constant#0}` failed
fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR evaluation of constant
//[full]~^^ ERROR evaluation of `test::<0_usize>::{constant#0}` failed
todo!()
}

View file

@ -10,7 +10,7 @@ LL | const BAR: usize = [5, 6, 7][T::BOO];
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
error[E0080]: evaluation of constant value failed
error[E0080]: evaluation of `foo::<()>` failed
--> $DIR/issue-50814-2.rs:19:6
|
LL | &<A<T> as Foo<T>>::BAR

View file

@ -10,7 +10,7 @@ LL | const MAX: u8 = A::MAX + B::MAX;
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
error[E0080]: evaluation of constant value failed
error[E0080]: evaluation of `foo::<i32>` failed
--> $DIR/issue-50814.rs:21:6
|
LL | &Sum::<U8,U8>::MAX

View file

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