1
- use super :: _match:: { MatchCheckCtxt , Matrix , expand_pattern, is_useful} ;
1
+ use super :: _match:: { MatchCheckCtxt , Matrix , Witness , expand_pattern, is_useful} ;
2
2
use super :: _match:: Usefulness :: * ;
3
3
use super :: _match:: WitnessPreference :: * ;
4
4
@@ -56,6 +56,38 @@ pub(crate) fn check_match<'a, 'tcx>(
56
56
} )
57
57
}
58
58
59
+ pub fn is_refutable < ' a , ' tcx > (
60
+ pat : & ' tcx Pat ,
61
+ tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
62
+ tables : & ' a ty:: TypeckTables < ' tcx > ,
63
+ param_env : ty:: ParamEnv < ' tcx > ,
64
+ identity_substs : & ' tcx Substs < ' tcx > ,
65
+ ) -> Option < Vec < Witness < ' tcx > > > {
66
+ let module = tcx. hir ( ) . get_module_parent ( pat. id ) ;
67
+ MatchCheckCtxt :: create_and_enter ( tcx, param_env, module, |ref mut cx| {
68
+ let mut patcx = PatternContext :: new ( tcx,
69
+ param_env. and ( identity_substs) ,
70
+ tables) ;
71
+ let pattern = patcx. lower_pattern ( pat) ;
72
+ let pattern_ty = pattern. ty ;
73
+ let pats: Matrix < ' _ , ' _ > = vec ! [ smallvec![
74
+ expand_pattern( cx, pattern)
75
+ ] ] . into_iter ( ) . collect ( ) ;
76
+
77
+ let wild_pattern = Pattern {
78
+ ty : pattern_ty,
79
+ span : DUMMY_SP ,
80
+ kind : box PatternKind :: Wild ,
81
+ } ;
82
+
83
+ match is_useful ( cx, & pats, & [ & wild_pattern] , ConstructWitness ) {
84
+ UsefulWithWitness ( witness) => Some ( witness) ,
85
+ NotUseful => None ,
86
+ Useful => bug ! ( )
87
+ }
88
+ } )
89
+ }
90
+
59
91
fn create_e0004 < ' a > ( sess : & ' a Session , sp : Span , error_message : String ) -> DiagnosticBuilder < ' a > {
60
92
struct_span_err ! ( sess, sp, E0004 , "{}" , & error_message)
61
93
}
@@ -241,28 +273,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
241
273
}
242
274
243
275
fn check_irrefutable ( & self , pat : & ' tcx Pat , origin : & str ) {
244
- let module = self . tcx . hir ( ) . get_module_parent ( pat. id ) ;
245
- MatchCheckCtxt :: create_and_enter ( self . tcx , self . param_env , module, |ref mut cx| {
246
- let mut patcx = PatternContext :: new ( self . tcx ,
247
- self . param_env . and ( self . identity_substs ) ,
248
- self . tables ) ;
249
- let pattern = patcx. lower_pattern ( pat) ;
250
- let pattern_ty = pattern. ty ;
251
- let pats: Matrix < ' _ , ' _ > = vec ! [ smallvec![
252
- expand_pattern( cx, pattern)
253
- ] ] . into_iter ( ) . collect ( ) ;
254
-
255
- let wild_pattern = Pattern {
256
- ty : pattern_ty,
257
- span : DUMMY_SP ,
258
- kind : box PatternKind :: Wild ,
259
- } ;
260
- let witness = match is_useful ( cx, & pats, & [ & wild_pattern] , ConstructWitness ) {
261
- UsefulWithWitness ( witness) => witness,
262
- NotUseful => return ,
263
- Useful => bug ! ( )
264
- } ;
265
-
276
+ if let Some ( witness) = is_refutable ( pat, self . tcx , self . tables , self . param_env , self . identity_substs ) {
266
277
let pattern_string = witness[ 0 ] . single_pattern ( ) . to_string ( ) ;
267
278
let mut diag = struct_span_err ! (
268
279
self . tcx. sess, pat. span, E0005 ,
@@ -279,7 +290,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
279
290
} ;
280
291
diag. span_label ( pat. span , label_msg) ;
281
292
diag. emit ( ) ;
282
- } ) ;
293
+ } ;
283
294
}
284
295
}
285
296
0 commit comments