1
- use smallvec:: SmallVec ;
2
1
use std:: fmt;
3
2
use std:: iter:: once;
4
3
@@ -28,8 +27,7 @@ use crate::constructor::Constructor::*;
28
27
pub type Constructor < ' p , ' tcx > = crate :: constructor:: Constructor < RustcMatchCheckCtxt < ' p , ' tcx > > ;
29
28
pub type ConstructorSet < ' p , ' tcx > =
30
29
crate :: constructor:: ConstructorSet < RustcMatchCheckCtxt < ' p , ' tcx > > ;
31
- pub type DeconstructedPat < ' p , ' tcx > =
32
- crate :: pat:: DeconstructedPat < ' p , RustcMatchCheckCtxt < ' p , ' tcx > > ;
30
+ pub type DeconstructedPat < ' p , ' tcx > = crate :: pat:: DeconstructedPat < RustcMatchCheckCtxt < ' p , ' tcx > > ;
33
31
pub type MatchArm < ' p , ' tcx > = crate :: MatchArm < ' p , RustcMatchCheckCtxt < ' p , ' tcx > > ;
34
32
pub type Usefulness < ' p , ' tcx > = crate :: usefulness:: Usefulness < ' p , RustcMatchCheckCtxt < ' p , ' tcx > > ;
35
33
pub type UsefulnessReport < ' p , ' tcx > =
@@ -452,21 +450,20 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
452
450
/// Note: the input patterns must have been lowered through
453
451
/// `rustc_mir_build::thir::pattern::check_match::MatchVisitor::lower_pattern`.
454
452
pub fn lower_pat ( & self , pat : & ' p Pat < ' tcx > ) -> DeconstructedPat < ' p , ' tcx > {
455
- let singleton = |pat| std:: slice:: from_ref ( self . pattern_arena . alloc ( pat) ) ;
456
453
let cx = self ;
457
454
let ty = cx. reveal_opaque_ty ( pat. ty ) ;
458
455
let ctor;
459
- let fields: & [ _ ] ;
456
+ let mut fields: Vec < _ > ;
460
457
match & pat. kind {
461
458
PatKind :: AscribeUserType { subpattern, .. }
462
459
| PatKind :: InlineConstant { subpattern, .. } => return self . lower_pat ( subpattern) ,
463
460
PatKind :: Binding { subpattern : Some ( subpat) , .. } => return self . lower_pat ( subpat) ,
464
461
PatKind :: Binding { subpattern : None , .. } | PatKind :: Wild => {
465
462
ctor = Wildcard ;
466
- fields = & [ ] ;
463
+ fields = vec ! [ ] ;
467
464
}
468
465
PatKind :: Deref { subpattern } => {
469
- fields = singleton ( self . lower_pat ( subpattern) ) ;
466
+ fields = vec ! [ self . lower_pat( subpattern) ] ;
470
467
ctor = match ty. kind ( ) {
471
468
// This is a box pattern.
472
469
ty:: Adt ( adt, ..) if adt. is_box ( ) => Struct ,
@@ -478,15 +475,14 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
478
475
match ty. kind ( ) {
479
476
ty:: Tuple ( fs) => {
480
477
ctor = Struct ;
481
- let mut wilds : SmallVec < [ _ ; 2 ] > = fs
478
+ fields = fs
482
479
. iter ( )
483
480
. map ( |ty| cx. reveal_opaque_ty ( ty) )
484
481
. map ( |ty| DeconstructedPat :: wildcard ( ty) )
485
482
. collect ( ) ;
486
483
for pat in subpatterns {
487
- wilds [ pat. field . index ( ) ] = self . lower_pat ( & pat. pattern ) ;
484
+ fields [ pat. field . index ( ) ] = self . lower_pat ( & pat. pattern ) ;
488
485
}
489
- fields = cx. pattern_arena . alloc_from_iter ( wilds) ;
490
486
}
491
487
ty:: Adt ( adt, args) if adt. is_box ( ) => {
492
488
// The only legal patterns of type `Box` (outside `std`) are `_` and box
@@ -508,7 +504,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
508
504
DeconstructedPat :: wildcard ( self . reveal_opaque_ty ( args. type_at ( 0 ) ) )
509
505
} ;
510
506
ctor = Struct ;
511
- fields = singleton ( pat) ;
507
+ fields = vec ! [ pat] ;
512
508
}
513
509
ty:: Adt ( adt, _) => {
514
510
ctor = match pat. kind {
@@ -528,14 +524,12 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
528
524
ty
529
525
} ,
530
526
) ;
531
- let mut wilds: SmallVec < [ _ ; 2 ] > =
532
- tys. map ( |ty| DeconstructedPat :: wildcard ( ty) ) . collect ( ) ;
527
+ fields = tys. map ( |ty| DeconstructedPat :: wildcard ( ty) ) . collect ( ) ;
533
528
for pat in subpatterns {
534
529
if let Some ( i) = field_id_to_id[ pat. field . index ( ) ] {
535
- wilds [ i] = self . lower_pat ( & pat. pattern ) ;
530
+ fields [ i] = self . lower_pat ( & pat. pattern ) ;
536
531
}
537
532
}
538
- fields = cx. pattern_arena . alloc_from_iter ( wilds) ;
539
533
}
540
534
_ => bug ! ( "pattern has unexpected type: pat: {:?}, ty: {:?}" , pat, ty) ,
541
535
}
@@ -547,7 +541,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
547
541
Some ( b) => Bool ( b) ,
548
542
None => Opaque ( OpaqueId :: new ( ) ) ,
549
543
} ;
550
- fields = & [ ] ;
544
+ fields = vec ! [ ] ;
551
545
}
552
546
ty:: Char | ty:: Int ( _) | ty:: Uint ( _) => {
553
547
ctor = match value. try_eval_bits ( cx. tcx , cx. param_env ) {
@@ -563,7 +557,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
563
557
}
564
558
None => Opaque ( OpaqueId :: new ( ) ) ,
565
559
} ;
566
- fields = & [ ] ;
560
+ fields = vec ! [ ] ;
567
561
}
568
562
ty:: Float ( ty:: FloatTy :: F32 ) => {
569
563
ctor = match value. try_eval_bits ( cx. tcx , cx. param_env ) {
@@ -574,7 +568,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
574
568
}
575
569
None => Opaque ( OpaqueId :: new ( ) ) ,
576
570
} ;
577
- fields = & [ ] ;
571
+ fields = vec ! [ ] ;
578
572
}
579
573
ty:: Float ( ty:: FloatTy :: F64 ) => {
580
574
ctor = match value. try_eval_bits ( cx. tcx , cx. param_env ) {
@@ -585,7 +579,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
585
579
}
586
580
None => Opaque ( OpaqueId :: new ( ) ) ,
587
581
} ;
588
- fields = & [ ] ;
582
+ fields = vec ! [ ] ;
589
583
}
590
584
ty:: Ref ( _, t, _) if t. is_str ( ) => {
591
585
// We want a `&str` constant to behave like a `Deref` pattern, to be compatible
@@ -596,16 +590,16 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
596
590
// subfields.
597
591
// Note: `t` is `str`, not `&str`.
598
592
let ty = self . reveal_opaque_ty ( * t) ;
599
- let subpattern = DeconstructedPat :: new ( Str ( * value) , & [ ] , ty, pat) ;
593
+ let subpattern = DeconstructedPat :: new ( Str ( * value) , Vec :: new ( ) , ty, pat) ;
600
594
ctor = Ref ;
601
- fields = singleton ( subpattern)
595
+ fields = vec ! [ subpattern]
602
596
}
603
597
// All constants that can be structurally matched have already been expanded
604
598
// into the corresponding `Pat`s by `const_to_pat`. Constants that remain are
605
599
// opaque.
606
600
_ => {
607
601
ctor = Opaque ( OpaqueId :: new ( ) ) ;
608
- fields = & [ ] ;
602
+ fields = vec ! [ ] ;
609
603
}
610
604
}
611
605
}
@@ -642,7 +636,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
642
636
}
643
637
_ => bug ! ( "invalid type for range pattern: {}" , ty. inner( ) ) ,
644
638
} ;
645
- fields = & [ ] ;
639
+ fields = vec ! [ ] ;
646
640
}
647
641
PatKind :: Array { prefix, slice, suffix } | PatKind :: Slice { prefix, slice, suffix } => {
648
642
let array_len = match ty. kind ( ) {
@@ -658,25 +652,22 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
658
652
SliceKind :: FixedLen ( prefix. len ( ) + suffix. len ( ) )
659
653
} ;
660
654
ctor = Slice ( Slice :: new ( array_len, kind) ) ;
661
- fields = cx. pattern_arena . alloc_from_iter (
662
- prefix. iter ( ) . chain ( suffix. iter ( ) ) . map ( |p| self . lower_pat ( & * p) ) ,
663
- )
655
+ fields = prefix. iter ( ) . chain ( suffix. iter ( ) ) . map ( |p| self . lower_pat ( & * p) ) . collect ( ) ;
664
656
}
665
657
PatKind :: Or { .. } => {
666
658
ctor = Or ;
667
659
let pats = expand_or_pat ( pat) ;
668
- fields =
669
- cx. pattern_arena . alloc_from_iter ( pats. into_iter ( ) . map ( |p| self . lower_pat ( p) ) )
660
+ fields = pats. into_iter ( ) . map ( |p| self . lower_pat ( p) ) . collect ( ) ;
670
661
}
671
662
PatKind :: Never => {
672
663
// FIXME(never_patterns): handle `!` in exhaustiveness. This is a sane default
673
664
// in the meantime.
674
665
ctor = Wildcard ;
675
- fields = & [ ] ;
666
+ fields = vec ! [ ] ;
676
667
}
677
668
PatKind :: Error ( _) => {
678
669
ctor = Opaque ( OpaqueId :: new ( ) ) ;
679
- fields = & [ ] ;
670
+ fields = vec ! [ ] ;
680
671
}
681
672
}
682
673
DeconstructedPat :: new ( ctor, fields, ty, pat)
@@ -849,7 +840,7 @@ impl<'p, 'tcx> RustcMatchCheckCtxt<'p, 'tcx> {
849
840
/// Best-effort `Debug` implementation.
850
841
pub ( crate ) fn debug_pat (
851
842
f : & mut fmt:: Formatter < ' _ > ,
852
- pat : & crate :: pat:: DeconstructedPat < ' _ , Self > ,
843
+ pat : & crate :: pat:: DeconstructedPat < Self > ,
853
844
) -> fmt:: Result {
854
845
let mut first = true ;
855
846
let mut start_or_continue = |s| {
@@ -975,7 +966,7 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
975
966
976
967
fn debug_pat (
977
968
f : & mut fmt:: Formatter < ' _ > ,
978
- pat : & crate :: pat:: DeconstructedPat < ' _ , Self > ,
969
+ pat : & crate :: pat:: DeconstructedPat < Self > ,
979
970
) -> fmt:: Result {
980
971
Self :: debug_pat ( f, pat)
981
972
}
@@ -985,9 +976,9 @@ impl<'p, 'tcx> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
985
976
986
977
fn lint_overlapping_range_endpoints (
987
978
& self ,
988
- pat : & crate :: pat:: DeconstructedPat < ' _ , Self > ,
979
+ pat : & crate :: pat:: DeconstructedPat < Self > ,
989
980
overlaps_on : IntRange ,
990
- overlaps_with : & [ & crate :: pat:: DeconstructedPat < ' _ , Self > ] ,
981
+ overlaps_with : & [ & crate :: pat:: DeconstructedPat < Self > ] ,
991
982
) {
992
983
let overlap_as_pat = self . hoist_pat_range ( & overlaps_on, * pat. ty ( ) ) ;
993
984
let overlaps: Vec < _ > = overlaps_with
0 commit comments