@@ -511,7 +511,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
511
511
opt_match_place : Option < ( Option < & Place < ' tcx > > , Span ) > ,
512
512
) -> Option < SourceScope > {
513
513
debug ! ( "declare_bindings: pattern={:?}" , pattern) ;
514
- self . visit_bindings (
514
+ self . visit_primary_bindings (
515
515
& pattern,
516
516
UserTypeProjections :: none ( ) ,
517
517
& mut |this, mutability, name, mode, var, span, ty, user_ty| {
@@ -563,7 +563,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
563
563
self . schedule_drop ( span, region_scope, local_id, DropKind :: Value ) ;
564
564
}
565
565
566
- pub ( super ) fn visit_bindings (
566
+ /// Visit all of the primary bindings in a patterns, that is, visit the
567
+ /// leftmost occurrence of each variable bound in a pattern. A variable
568
+ /// will occur more than once in an or-pattern.
569
+ pub ( super ) fn visit_primary_bindings (
567
570
& mut self ,
568
571
pattern : & Pat < ' tcx > ,
569
572
pattern_user_ty : UserTypeProjections ,
@@ -578,12 +581,26 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
578
581
UserTypeProjections ,
579
582
) ,
580
583
) {
581
- debug ! ( "visit_bindings: pattern={:?} pattern_user_ty={:?}" , pattern, pattern_user_ty) ;
584
+ debug ! (
585
+ "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}" ,
586
+ pattern, pattern_user_ty
587
+ ) ;
582
588
match * pattern. kind {
583
- PatKind :: Binding { mutability, name, mode, var, ty, ref subpattern, .. } => {
584
- f ( self , mutability, name, mode, var, pattern. span , ty, pattern_user_ty. clone ( ) ) ;
589
+ PatKind :: Binding {
590
+ mutability,
591
+ name,
592
+ mode,
593
+ var,
594
+ ty,
595
+ ref subpattern,
596
+ is_primary,
597
+ ..
598
+ } => {
599
+ if is_primary {
600
+ f ( self , mutability, name, mode, var, pattern. span , ty, pattern_user_ty. clone ( ) ) ;
601
+ }
585
602
if let Some ( subpattern) = subpattern. as_ref ( ) {
586
- self . visit_bindings ( subpattern, pattern_user_ty, f) ;
603
+ self . visit_primary_bindings ( subpattern, pattern_user_ty, f) ;
587
604
}
588
605
}
589
606
@@ -592,20 +609,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
592
609
let from = u32:: try_from ( prefix. len ( ) ) . unwrap ( ) ;
593
610
let to = u32:: try_from ( suffix. len ( ) ) . unwrap ( ) ;
594
611
for subpattern in prefix {
595
- self . visit_bindings ( subpattern, pattern_user_ty. clone ( ) . index ( ) , f) ;
612
+ self . visit_primary_bindings ( subpattern, pattern_user_ty. clone ( ) . index ( ) , f) ;
596
613
}
597
614
for subpattern in slice {
598
- self . visit_bindings ( subpattern, pattern_user_ty. clone ( ) . subslice ( from, to) , f) ;
615
+ self . visit_primary_bindings (
616
+ subpattern,
617
+ pattern_user_ty. clone ( ) . subslice ( from, to) ,
618
+ f,
619
+ ) ;
599
620
}
600
621
for subpattern in suffix {
601
- self . visit_bindings ( subpattern, pattern_user_ty. clone ( ) . index ( ) , f) ;
622
+ self . visit_primary_bindings ( subpattern, pattern_user_ty. clone ( ) . index ( ) , f) ;
602
623
}
603
624
}
604
625
605
626
PatKind :: Constant { .. } | PatKind :: Range { .. } | PatKind :: Wild => { }
606
627
607
628
PatKind :: Deref { ref subpattern } => {
608
- self . visit_bindings ( subpattern, pattern_user_ty. deref ( ) , f) ;
629
+ self . visit_primary_bindings ( subpattern, pattern_user_ty. deref ( ) , f) ;
609
630
}
610
631
611
632
PatKind :: AscribeUserType {
@@ -630,26 +651,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
630
651
projs : Vec :: new ( ) ,
631
652
} ;
632
653
let subpattern_user_ty = pattern_user_ty. push_projection ( & projection, user_ty_span) ;
633
- self . visit_bindings ( subpattern, subpattern_user_ty, f)
654
+ self . visit_primary_bindings ( subpattern, subpattern_user_ty, f)
634
655
}
635
656
636
657
PatKind :: Leaf { ref subpatterns } => {
637
658
for subpattern in subpatterns {
638
659
let subpattern_user_ty = pattern_user_ty. clone ( ) . leaf ( subpattern. field ) ;
639
- debug ! ( "visit_bindings : subpattern_user_ty={:?}" , subpattern_user_ty) ;
640
- self . visit_bindings ( & subpattern. pattern , subpattern_user_ty, f) ;
660
+ debug ! ( "visit_primary_bindings : subpattern_user_ty={:?}" , subpattern_user_ty) ;
661
+ self . visit_primary_bindings ( & subpattern. pattern , subpattern_user_ty, f) ;
641
662
}
642
663
}
643
664
644
665
PatKind :: Variant { adt_def, substs : _, variant_index, ref subpatterns } => {
645
666
for subpattern in subpatterns {
646
667
let subpattern_user_ty =
647
668
pattern_user_ty. clone ( ) . variant ( adt_def, variant_index, subpattern. field ) ;
648
- self . visit_bindings ( & subpattern. pattern , subpattern_user_ty, f) ;
669
+ self . visit_primary_bindings ( & subpattern. pattern , subpattern_user_ty, f) ;
649
670
}
650
671
}
651
672
PatKind :: Or { ref pats } => {
652
- self . visit_bindings ( & pats[ 0 ] , pattern_user_ty, f) ;
673
+ // In cases where we recover from errors the primary bindings
674
+ // may not all be in the leftmost subpattern. For example in
675
+ // `let (x | y) = ...`, the primary binding of `y` occurs in
676
+ // the right subpattern
677
+ for subpattern in pats {
678
+ self . visit_primary_bindings ( subpattern, pattern_user_ty. clone ( ) , f) ;
679
+ }
653
680
}
654
681
}
655
682
}
@@ -1955,7 +1982,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1955
1982
is_block_tail : None ,
1956
1983
local_info : Some ( box LocalInfo :: User ( ClearCrossCrate :: Set ( BindingForm :: Var ( VarBindingForm {
1957
1984
binding_mode,
1958
- // hypothetically, `visit_bindings ` could try to unzip
1985
+ // hypothetically, `visit_primary_bindings ` could try to unzip
1959
1986
// an outermost hir::Ty as we descend, matching up
1960
1987
// idents in pat; but complex w/ unclear UI payoff.
1961
1988
// Instead, just abandon providing diagnostic info.
0 commit comments