Skip to content

A bit more polish on const eval errors #85767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_mir/src/monomorphize/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0080]: evaluation of constant value failed
error[E0080]: evaluation of `test::<0_usize>::{constant#0}` failed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already report such paths (with {constant#0}) in other situations. It's not ideal, but maybe better than not mentioning the path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, there are indeed a few cases in the ui tests where this happens today, for example

::: $DIR/issue-80742.rs:23:10
|
LL | [u8; size_of::<T>() + 1]: ,
| -------------- inside `Inline::<dyn Debug>::{constant#0}` at $DIR/issue-80742.rs:23:10

(After/if this PR is merged, I'll work on changing these messages: to keep the originating span, but not the full instance path, so that they are not duplicated)

--> $DIR/from-sig-fail.rs:4:35
|
LL | fn test<const N: usize>() -> [u8; N - 1] {
Expand Down
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/issue-50814-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/consts/const-eval/issue-85155.stderr
Original file line number Diff line number Diff line change
@@ -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>();
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/mir/issue-80742.stderr
Original file line number Diff line number Diff line change
@@ -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>()
Expand Down Expand Up @@ -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>()
Expand Down