Skip to content

Commit 9f75dbf

Browse files
committed
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.
2 parents f77b1a5 + c31ca9a commit 9f75dbf

File tree

11 files changed

+33
-21
lines changed

11 files changed

+33
-21
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) => {

compiler/rustc_mir/src/monomorphize/collector.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ use rustc_middle::mir::mono::{InstantiationMode, MonoItem};
194194
use rustc_middle::mir::visit::Visitor as MirVisitor;
195195
use rustc_middle::mir::{self, Local, Location};
196196
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCast};
197+
use rustc_middle::ty::print::with_no_trimmed_paths;
197198
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
198199
use rustc_middle::ty::{self, GenericParamDefKind, Instance, Ty, TyCtxt, TypeFoldable};
199200
use rustc_middle::{middle::codegen_fn_attrs::CodegenFnAttrFlags, mir::visit::TyContext};
@@ -444,12 +445,10 @@ fn collect_items_rec<'tcx>(
444445
// defined in the local crate.
445446
if tcx.sess.diagnostic().err_count() > error_count && starting_point.node.krate() != LOCAL_CRATE
446447
{
448+
let formatted_item = with_no_trimmed_paths(|| starting_point.node.to_string());
447449
tcx.sess.span_note_without_error(
448450
starting_point.span,
449-
&format!(
450-
"the above error was encountered while instantiating `{}`",
451-
starting_point.node
452-
),
451+
&format!("the above error was encountered while instantiating `{}`", formatted_item),
453452
);
454453
}
455454

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/consts/const-eval/issue-85155.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0080]: evaluation of constant value failed
1+
error[E0080]: evaluation of `post_monomorphization_error::ValidateConstImm::<2_i32, 0_i32, 1_i32>::VALID` failed
22
--> $DIR/auxiliary/post_monomorphization_error.rs:7:17
33
|
44
LL | let _ = 1 / ((IMM >= MIN && IMM <= MAX) as usize);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `1_usize` by zero
66

7-
note: the above error was encountered while instantiating `fn stdarch_intrinsic::<2_i32>`
7+
note: the above error was encountered while instantiating `fn post_monomorphization_error::stdarch_intrinsic::<2_i32>`
88
--> $DIR/issue-85155.rs:19:5
99
|
1010
LL | post_monomorphization_error::stdarch_intrinsic::<2>();

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)