Auto merge of #85767 - lqd:stackless_span_stacks, r=oli-obk

A bit more polish on const eval errors

This PR adds a bit more polish to the const eval errors:
- a slight improvement to the PME messages from #85633: I mentioned there that the erroneous item's paths were dependent on the environment, and could be displayed fully qualified or not. This can obscure the items when they come from a dependency. This PR uses the pretty-printing code ensuring the items' paths are not trimmed.
- whenever there are generics involved in an item where const evaluation errors out, the error message now displays the instance and its const arguments, so that we can see which instantiated item and compile-time values lead to the error.

So we get this slight improvement for our beloved `stdarch` example, on nightly:
```
error[E0080]: evaluation of constant value failed
 --> ./stdarch/crates/core_arch/src/macros.rs:8:9
  |
8 |         assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range");
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'IMM value not in expected range', /rustc/9111b8ae9793f18179a1336417618fc07a9cac85/library/core/src/../../stdarch/crates/core_arch/src/macros.rs:8:9
  |
```

to this PR's:

```
error[E0080]: evaluation of `core::core_arch::macros::ValidateConstImm::<51_i32, 0_i32, 15_i32>::VALID` failed
 --> ./stdarch/crates/core_arch/src/macros.rs:8:9
  |
8 |         assert!(IMM >= MIN && IMM <= MAX, "IMM value not in expected range");
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'IMM value not in expected range', ./stdarch/crates/core_arch/src/macros.rs:8:9
  |
```

with this PR.

Of course this is an idea from Oli, so maybe r? `@oli-obk` if they have the time.
This commit is contained in:
bors 2021-05-29 13:34:20 +00:00
commit 9f75dbfa69
11 changed files with 33 additions and 21 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

@ -194,6 +194,7 @@ use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
use rustc_middle::mir::visit::Visitor as MirVisitor;
use rustc_middle::mir::{self, Local, Location};
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCast};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::{self, GenericParamDefKind, Instance, Ty, TyCtxt, TypeFoldable};
use rustc_middle::{middle::codegen_fn_attrs::CodegenFnAttrFlags, mir::visit::TyContext};
@ -444,12 +445,10 @@ fn collect_items_rec<'tcx>(
// defined in the local crate.
if tcx.sess.diagnostic().err_count() > error_count && starting_point.node.krate() != LOCAL_CRATE
{
let formatted_item = with_no_trimmed_paths(|| starting_point.node.to_string());
tcx.sess.span_note_without_error(
starting_point.span,
&format!(
"the above error was encountered while instantiating `{}`",
starting_point.node
),
&format!("the above error was encountered while instantiating `{}`", formatted_item),
);
}

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,10 +1,10 @@
error[E0080]: evaluation of constant value failed
error[E0080]: evaluation of `post_monomorphization_error::ValidateConstImm::<2_i32, 0_i32, 1_i32>::VALID` failed
--> $DIR/auxiliary/post_monomorphization_error.rs:7:17
|
LL | let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `1_usize` by zero
note: the above error was encountered while instantiating `fn stdarch_intrinsic::<2_i32>`
note: the above error was encountered while instantiating `fn post_monomorphization_error::stdarch_intrinsic::<2_i32>`
--> $DIR/issue-85155.rs:19:5
|
LL | post_monomorphization_error::stdarch_intrinsic::<2>();

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>()