Skip to content

Commit 75694d5

Browse files
lzcuntmejrs
authored andcommitted
Migrate "constant pattern depends on generic parameter" diagnostic
1 parent 7d0d2de commit 75694d5

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

compiler/rustc_error_messages/locales/en-US/mir_build.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,6 @@ mir_build_non_const_path = runtime values cannot be referenced in patterns
195195
mir_build_unreachable_pattern = unreachable pattern
196196
.label = unreachable pattern
197197
.catchall_label = matches any value
198+
199+
mir_build_const_pattern_depends_on_generic_parameter =
200+
constant pattern depends on a generic parameter

compiler/rustc_mir_build/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,10 @@ pub struct UnreachablePattern {
466466
#[label(mir_build::catchall_label)]
467467
pub catchall: Option<Span>,
468468
}
469+
470+
#[derive(SessionDiagnostic)]
471+
#[diag(mir_build::const_pattern_depends_on_generic_parameter)]
472+
pub struct ConstPatternDependsOnGenericParameter {
473+
#[primary_span]
474+
pub span: Span,
475+
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod usefulness;
88
pub(crate) use self::check_match::check_match;
99
pub(crate) use self::usefulness::MatchCheckCtxt;
1010

11+
use crate::errors::ConstPatternDependsOnGenericParameter;
1112
use crate::thir::util::UserAnnotatedTyHelpers;
1213

1314
use rustc_errors::struct_span_err;
@@ -549,7 +550,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
549550
Err(ErrorHandled::TooGeneric) => {
550551
// While `Reported | Linted` cases will have diagnostics emitted already
551552
// it is not true for TooGeneric case, so we need to give user more information.
552-
self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter");
553+
self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
553554
pat_from_kind(PatKind::Wild)
554555
}
555556
Err(_) => {
@@ -583,9 +584,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
583584
_ => bug!("Expected ConstKind::Param"),
584585
},
585586
mir::ConstantKind::Val(_, _) => self.const_to_pat(value, id, span, false).kind,
586-
mir::ConstantKind::Unevaluated(..) => {
587+
mir::ConstKind::Unevaluated(_) => {
587588
// If we land here it means the const can't be evaluated because it's `TooGeneric`.
588-
self.tcx.sess.span_err(span, "constant pattern depends on a generic parameter");
589+
self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
589590
return PatKind::Wild;
590591
}
591592
}

0 commit comments

Comments
 (0)