Skip to content

Commit

Permalink
Rollup merge of rust-lang#74934 - nbdd0121:issue-73976, r=ecstatic-morse
Browse files Browse the repository at this point in the history
Improve diagnostics when constant pattern is too generic

This PR is a follow-up to PR rust-lang#74538 and issue rust-lang#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`.
  • Loading branch information
Manishearth committed Jul 30, 2020
2 parents 0a45b13 + 4e963d5 commit b0d4261
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/librustc_mir_build/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/issue-73976-polymorphic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/consts/issue-73976-polymorphic.stderr
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit b0d4261

Please sign in to comment.