@@ -359,25 +359,6 @@ impl<'tcx> Witness<'tcx> {
359
359
}
360
360
}
361
361
362
- /// Return the set of constructors from the same type as the first column of `matrix`,
363
- /// that are matched only by wildcard patterns from that first column.
364
- ///
365
- /// Therefore, if there is some pattern that is unmatched by `matrix`, it will
366
- /// still be unmatched if the first constructor is replaced by any of the constructors
367
- /// in the return value.
368
- fn missing_constructors < ' a , ' tcx : ' a > ( cx : & mut MatchCheckCtxt < ' a , ' tcx > ,
369
- matrix : & Matrix ,
370
- pcx : PatternContext < ' tcx > ) -> Vec < Constructor > {
371
- let used_constructors: Vec < Constructor > =
372
- matrix. 0 . iter ( )
373
- . flat_map ( |row| pat_constructors ( cx, row[ 0 ] , pcx) . unwrap_or ( vec ! [ ] ) )
374
- . collect ( ) ;
375
- debug ! ( "used_constructors = {:?}" , used_constructors) ;
376
- all_constructors ( cx, pcx) . into_iter ( )
377
- . filter ( |c| !used_constructors. contains ( c) )
378
- . collect ( )
379
- }
380
-
381
362
/// This determines the set of all possible constructors of a pattern matching
382
363
/// values of type `left_ty`. For vectors, this would normally be an infinite set
383
364
///
@@ -586,10 +567,28 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
586
567
) . find ( |result| result. is_useful ( ) ) . unwrap_or ( NotUseful )
587
568
} else {
588
569
debug ! ( "is_useful - expanding wildcard" ) ;
589
- let constructors = missing_constructors ( cx, matrix, pcx) ;
590
- debug ! ( "is_useful - missing_constructors = {:?}" , constructors) ;
591
- if constructors. is_empty ( ) {
592
- all_constructors ( cx, pcx) . into_iter ( ) . map ( |c| {
570
+
571
+ let used_ctors: Vec < Constructor > = rows. iter ( ) . flat_map ( |row| {
572
+ pat_constructors ( cx, row[ 0 ] , pcx) . unwrap_or ( vec ! [ ] )
573
+ } ) . collect ( ) ;
574
+ debug ! ( "used_ctors = {:?}" , used_ctors) ;
575
+ let all_ctors = all_constructors ( cx, pcx) ;
576
+ debug ! ( "all_ctors = {:?}" , all_ctors) ;
577
+ let missing_ctors: Vec < Constructor > = all_ctors. iter ( ) . filter ( |c| {
578
+ !used_ctors. contains ( * c)
579
+ } ) . cloned ( ) . collect ( ) ;
580
+ debug ! ( "missing_ctors = {:?}" , missing_ctors) ;
581
+
582
+ // `missing_ctors` is the set of constructors from the same type as the
583
+ // first column of `matrix` that are matched only by wildcard patterns
584
+ // from the first column.
585
+ //
586
+ // Therefore, if there is some pattern that is unmatched by `matrix`,
587
+ // it will still be unmatched if the first constructor is replaced by
588
+ // any of the constructors in `missing_ctors`
589
+
590
+ if missing_ctors. is_empty ( ) {
591
+ all_ctors. into_iter ( ) . map ( |c| {
593
592
is_useful_specialized ( cx, matrix, v, c. clone ( ) , pcx. ty , witness)
594
593
} ) . find ( |result| result. is_useful ( ) ) . unwrap_or ( NotUseful )
595
594
} else {
@@ -603,11 +602,25 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
603
602
match is_useful ( cx, & matrix, & v[ 1 ..] , witness) {
604
603
UsefulWithWitness ( pats) => {
605
604
let cx = & * cx;
606
- UsefulWithWitness ( pats. into_iter ( ) . flat_map ( |witness| {
607
- constructors. iter ( ) . map ( move |ctor| {
608
- witness. clone ( ) . push_wild_constructor ( cx, ctor, pcx. ty )
609
- } )
610
- } ) . collect ( ) )
605
+ let new_witnesses = if used_ctors. is_empty ( ) {
606
+ // All constructors are unused. Add wild patterns
607
+ // rather than each individual constructor
608
+ pats. into_iter ( ) . map ( |mut witness| {
609
+ witness. 0 . push ( P ( hir:: Pat {
610
+ id : DUMMY_NODE_ID ,
611
+ node : PatKind :: Wild ,
612
+ span : DUMMY_SP ,
613
+ } ) ) ;
614
+ witness
615
+ } ) . collect ( )
616
+ } else {
617
+ pats. into_iter ( ) . flat_map ( |witness| {
618
+ missing_ctors. iter ( ) . map ( move |ctor| {
619
+ witness. clone ( ) . push_wild_constructor ( cx, ctor, pcx. ty )
620
+ } )
621
+ } ) . collect ( )
622
+ } ;
623
+ UsefulWithWitness ( new_witnesses)
611
624
}
612
625
result => result
613
626
}
0 commit comments