Skip to content

Commit b0d4261

Browse files
authored
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`.
2 parents 0a45b13 + 4e963d5 commit b0d4261

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/librustc_mir_build/hair/pattern/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1616
use rustc_hir::RangeEnd;
1717
use rustc_index::vec::Idx;
1818
use rustc_middle::mir::interpret::{get_slice_bytes, sign_extend, ConstValue};
19-
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
19+
use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
2020
use rustc_middle::mir::UserTypeProjection;
2121
use rustc_middle::mir::{BorrowKind, Field, Mutability};
2222
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
@@ -834,6 +834,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
834834
pattern
835835
}
836836
}
837+
Err(ErrorHandled::TooGeneric) => {
838+
// While `Reported | Linted` cases will have diagnostics emitted already
839+
// it is not true for TooGeneric case, so we need to give user more information.
840+
self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter");
841+
pat_from_kind(PatKind::Wild)
842+
}
837843
Err(_) => {
838844
self.tcx.sess.span_err(span, "could not evaluate constant pattern");
839845
pat_from_kind(PatKind::Wild)

src/test/ui/consts/issue-73976-polymorphic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ impl<T: 'static> GetTypeId<T> {
1717

1818
const fn check_type_id<T: 'static>() -> bool {
1919
matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
20-
//~^ ERROR could not evaluate constant pattern
21-
//~| ERROR could not evaluate constant pattern
20+
//~^ ERROR constant pattern depends on a generic parameter
21+
//~| ERROR constant pattern depends on a generic parameter
2222
}
2323

2424
pub struct GetTypeNameLen<T>(T);
@@ -29,8 +29,8 @@ impl<T: 'static> GetTypeNameLen<T> {
2929

3030
const fn check_type_name_len<T: 'static>() -> bool {
3131
matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
32-
//~^ ERROR could not evaluate constant pattern
33-
//~| ERROR could not evaluate constant pattern
32+
//~^ ERROR constant pattern depends on a generic parameter
33+
//~| ERROR constant pattern depends on a generic parameter
3434
}
3535

3636
fn main() {

src/test/ui/consts/issue-73976-polymorphic.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error: could not evaluate constant pattern
1+
error: constant pattern depends on a generic parameter
22
--> $DIR/issue-73976-polymorphic.rs:19:37
33
|
44
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
55
| ^^^^^^^^^^^^^^^^^^^^^
66

7-
error: could not evaluate constant pattern
7+
error: constant pattern depends on a generic parameter
88
--> $DIR/issue-73976-polymorphic.rs:31:42
99
|
1010
LL | matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: could not evaluate constant pattern
13+
error: constant pattern depends on a generic parameter
1414
--> $DIR/issue-73976-polymorphic.rs:19:37
1515
|
1616
LL | matches!(GetTypeId::<T>::VALUE, GetTypeId::<T>::VALUE)
1717
| ^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: could not evaluate constant pattern
19+
error: constant pattern depends on a generic parameter
2020
--> $DIR/issue-73976-polymorphic.rs:31:42
2121
|
2222
LL | matches!(GetTypeNameLen::<T>::VALUE, GetTypeNameLen::<T>::VALUE)

0 commit comments

Comments
 (0)