about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2020-07-30 13:04:42 -0700
committerGitHub <noreply@github.com>2020-07-30 13:04:42 -0700
commitb0d426165f871f4e50d2fae47e6a5e9c54d1ba12 (patch)
tree2f8a1f06c031b50b87bac239c716527a4f4c9477
parent0a45b1303cafc39458ffd3944dcf74c8b4200df6 (diff)
parent4e963d58c7d94270697c2765f23993e40757292f (diff)
downloadrust-b0d426165f871f4e50d2fae47e6a5e9c54d1ba12.tar.gz
rust-b0d426165f871f4e50d2fae47e6a5e9c54d1ba12.zip
Rollup merge of #74934 - nbdd0121:issue-73976, r=ecstatic-morse
Improve diagnostics when constant pattern is too generic

This PR is a follow-up to PR #74538 and issue #73976

When constants queries Layout, TypeId or type_name of a generic parameter, instead of emitting `could not evaluate constant pattern`, we will instead emit a more detailed message `constant pattern depends on a generic parameter`.
-rw-r--r--src/librustc_mir_build/hair/pattern/mod.rs8
-rw-r--r--src/test/ui/consts/issue-73976-polymorphic.rs8
-rw-r--r--src/test/ui/consts/issue-73976-polymorphic.stderr8
3 files changed, 15 insertions, 9 deletions
diff --git a/src/librustc_mir_build/hair/pattern/mod.rs b/src/librustc_mir_build/hair/pattern/mod.rs
index a5c87bc963f..f813ba0c077 100644
--- a/src/librustc_mir_build/hair/pattern/mod.rs
+++ b/src/librustc_mir_build/hair/pattern/mod.rs
@@ -16,7 +16,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
 use rustc_hir::RangeEnd;
 use rustc_index::vec::Idx;
 use rustc_middle::mir::interpret::{get_slice_bytes, sign_extend, ConstValue};
-use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
+use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
 use rustc_middle::mir::UserTypeProjection;
 use rustc_middle::mir::{BorrowKind, Field, Mutability};
 use rustc_middle::ty::subst::{GenericArg, SubstsRef};
@@ -834,6 +834,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
                     pattern
                 }
             }
+            Err(ErrorHandled::TooGeneric) => {
+                // While `Reported | Linted` cases will have diagnostics emitted already
+                // it is not true for TooGeneric case, so we need to give user more information.
+                self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter");
+                pat_from_kind(PatKind::Wild)
+            }
             Err(_) => {
                 self.tcx.sess.span_err(span, "could not evaluate constant pattern");
                 pat_from_kind(PatKind::Wild)
diff --git a/src/test/ui/consts/issue-73976-polymorphic.rs b/src/test/ui/consts/issue-73976-polymorphic.rs
index 7cf20296062..518036c9dbe 100644
--- a/src/test/ui/consts/issue-73976-polymorphic.rs
+++ b/src/test/ui/consts/issue-73976-polymorphic.rs
@@ -17,8 +17,8 @@ impl<T: 'static> GetTypeId<T> {
 
 const fn check_type_id<T: 'static>() -> bool {
     matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
-    //~^ ERROR could not evaluate constant pattern
-    //~| ERROR could not evaluate constant pattern
+    //~^ ERROR constant pattern depends on a generic parameter
+    //~| ERROR constant pattern depends on a generic parameter
 }
 
 pub struct GetTypeNameLen<T>(T);
@@ -29,8 +29,8 @@ impl<T: 'static> GetTypeNameLen<T> {
 
 const fn check_type_name_len<T: 'static>() -> bool {
     matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
-    //~^ ERROR could not evaluate constant pattern
-    //~| ERROR could not evaluate constant pattern
+    //~^ ERROR constant pattern depends on a generic parameter
+    //~| ERROR constant pattern depends on a generic parameter
 }
 
 fn main() {
diff --git a/src/test/ui/consts/issue-73976-polymorphic.stderr b/src/test/ui/consts/issue-73976-polymorphic.stderr
index 971573e14aa..250f1536d85 100644
--- a/src/test/ui/consts/issue-73976-polymorphic.stderr
+++ b/src/test/ui/consts/issue-73976-polymorphic.stderr
@@ -1,22 +1,22 @@
-error: could not evaluate constant pattern
+error: constant pattern depends on a generic parameter
   --> $DIR/issue-73976-polymorphic.rs:19:37
    |
 LL |     matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
    |                                     ^^^^^^^^^^^^^^^^^^^^^
 
-error: could not evaluate constant pattern
+error: constant pattern depends on a generic parameter
   --> $DIR/issue-73976-polymorphic.rs:31:42
    |
 LL |     matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
    |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: could not evaluate constant pattern
+error: constant pattern depends on a generic parameter
   --> $DIR/issue-73976-polymorphic.rs:19:37
    |
 LL |     matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
    |                                     ^^^^^^^^^^^^^^^^^^^^^
 
-error: could not evaluate constant pattern
+error: constant pattern depends on a generic parameter
   --> $DIR/issue-73976-polymorphic.rs:31:42
    |
 LL |     matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)