Skip to content

Commit 9550ca6

Browse files
committed
Deduplicate the "needs partialeq derive" message creation sites
1 parent e4928d7 commit 9550ca6

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_index::vec::Idx;
33
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
44
use rustc_middle::mir::Field;
55
use rustc_middle::ty::print::with_no_trimmed_paths;
6-
use rustc_middle::ty::{self, Ty, TyCtxt};
6+
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
77
use rustc_session::lint;
88
use rustc_span::Span;
99
use rustc_trait_selection::traits::predicate_for_trait_def;
@@ -89,18 +89,20 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
8989
self.infcx.tcx
9090
}
9191

92+
fn adt_derive_msg(&self, adt_def: &AdtDef) -> String {
93+
let path = self.tcx().def_path_str(adt_def.did);
94+
format!(
95+
"to use a constant of type `{}` in a pattern, \
96+
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
97+
path, path,
98+
)
99+
}
100+
92101
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
93102
traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty).map(
94103
|non_sm_ty| {
95104
with_no_trimmed_paths(|| match non_sm_ty {
96-
traits::NonStructuralMatchTy::Adt(adt_def) => {
97-
let path = self.tcx().def_path_str(adt_def.did);
98-
format!(
99-
"to use a constant of type `{}` in a pattern, \
100-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
101-
path, path,
102-
)
103-
}
105+
traits::NonStructuralMatchTy::Adt(adt) => self.adt_derive_msg(adt),
104106
traits::NonStructuralMatchTy::Dynamic => {
105107
"trait objects cannot be used in patterns".to_string()
106108
}
@@ -412,12 +414,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
412414
&& !self.saw_const_match_lint.get()
413415
{
414416
self.saw_const_match_lint.set(true);
415-
let path = self.tcx().def_path_str(adt_def.did);
416-
let msg = format!(
417-
"to use a constant of type `{}` in a pattern, \
418-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
419-
path, path,
420-
);
417+
let msg = self.adt_derive_msg(adt_def);
421418
self.tcx().struct_span_lint_hir(
422419
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
423420
self.id,
@@ -429,12 +426,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
429426
} else {
430427
if !self.saw_const_match_error.get() {
431428
self.saw_const_match_error.set(true);
432-
let path = self.tcx().def_path_str(adt_def.did);
433-
let msg = format!(
434-
"to use a constant of type `{}` in a pattern, \
435-
`{}` must be annotated with `#[derive(PartialEq, Eq)]`",
436-
path, path,
437-
);
429+
let msg = self.adt_derive_msg(adt_def);
438430
if self.include_lint_checks {
439431
tcx.sess.span_err(span, &msg);
440432
} else {

0 commit comments

Comments
 (0)