@@ -3,7 +3,7 @@ use rustc_index::vec::Idx;
3
3
use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
4
4
use rustc_middle:: mir:: Field ;
5
5
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 } ;
7
7
use rustc_session:: lint;
8
8
use rustc_span:: Span ;
9
9
use rustc_trait_selection:: traits:: predicate_for_trait_def;
@@ -89,18 +89,20 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
89
89
self . infcx . tcx
90
90
}
91
91
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
+
92
101
fn search_for_structural_match_violation ( & self , ty : Ty < ' tcx > ) -> Option < String > {
93
102
traits:: search_for_structural_match_violation ( self . id , self . span , self . tcx ( ) , ty) . map (
94
103
|non_sm_ty| {
95
104
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) ,
104
106
traits:: NonStructuralMatchTy :: Dynamic => {
105
107
"trait objects cannot be used in patterns" . to_string ( )
106
108
}
@@ -412,12 +414,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
412
414
&& !self . saw_const_match_lint . get ( )
413
415
{
414
416
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) ;
421
418
self . tcx ( ) . struct_span_lint_hir (
422
419
lint:: builtin:: INDIRECT_STRUCTURAL_MATCH ,
423
420
self . id ,
@@ -429,12 +426,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
429
426
} else {
430
427
if !self . saw_const_match_error . get ( ) {
431
428
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) ;
438
430
if self . include_lint_checks {
439
431
tcx. sess . span_err ( span, & msg) ;
440
432
} else {
0 commit comments