about summary refs log tree commit diff
path: root/compiler/rustc_mir/src/const_eval
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-05-29 13:34:20 +0000
committerbors <bors@rust-lang.org>2021-05-29 13:34:20 +0000
commit9f75dbfa69ba8508fa7765305f75cd96dff06078 (patch)
tree49cd57bf8b95fb49d72978f90670027826a4e49d /compiler/rustc_mir/src/const_eval
parentf77b1a5973521bce26d92d346304cfaca05ae79e (diff)
parentc31ca9a42c0dd8a8c16b74c9db3f5376aabfcc81 (diff)
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.
Diffstat (limited to 'compiler/rustc_mir/src/const_eval')
-rw-r--r--compiler/rustc_mir/src/const_eval/eval_queries.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs
index a12185393de..460fea37461 100644
--- a/compiler/rustc_mir/src/const_eval/eval_queries.rs
+++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs
@@ -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) => {