@@ -271,11 +271,9 @@ fn const_not_var(err: &mut DiagnosticBuilder<'_>, tcx: TyCtxt<'_>, pat: &Pat, pa
271
271
fn check_for_bindings_named_same_as_variants ( cx : & MatchVisitor < ' _ , ' _ > , pat : & Pat ) {
272
272
pat. walk ( |p| {
273
273
if let hir:: PatKind :: Binding ( _, _, ident, None ) = p. kind {
274
- if let Some ( & bm) = cx. tables . pat_binding_modes ( ) . get ( p. hir_id ) {
275
- if bm != ty:: BindByValue ( hir:: Mutability :: Not ) {
276
- // Nothing to check.
277
- return true ;
278
- }
274
+ if let Some ( ty:: BindByValue ( hir:: Mutability :: Not ) ) =
275
+ cx. tables . extract_binding_mode ( cx. tcx . sess , p. hir_id , p. span )
276
+ {
279
277
let pat_ty = cx. tables . pat_ty ( p) ;
280
278
if let ty:: Adt ( edef, _) = pat_ty. kind {
281
279
if edef. is_enum ( )
@@ -303,8 +301,6 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
303
301
err. emit ( ) ;
304
302
}
305
303
}
306
- } else {
307
- cx. tcx . sess . delay_span_bug ( p. span , "missing binding mode" ) ;
308
304
}
309
305
}
310
306
true
@@ -581,15 +577,14 @@ fn maybe_point_at_variant(ty: Ty<'_>, patterns: &[super::Pat<'_>]) -> Vec<Span>
581
577
582
578
// Check the legality of legality of by-move bindings.
583
579
fn check_legality_of_move_bindings ( cx : & mut MatchVisitor < ' _ , ' _ > , has_guard : bool , pat : & Pat ) {
580
+ let sess = cx. tcx . sess ;
581
+ let tables = cx. tables ;
582
+
584
583
// Find all by-ref spans.
585
584
let mut by_ref_spans = Vec :: new ( ) ;
586
585
pat. each_binding ( |_, hir_id, span, _| {
587
- if let Some ( & bm) = cx. tables . pat_binding_modes ( ) . get ( hir_id) {
588
- if let ty:: BindByReference ( ..) = bm {
589
- by_ref_spans. push ( span) ;
590
- }
591
- } else {
592
- cx. tcx . sess . delay_span_bug ( pat. span , "missing binding mode" ) ;
586
+ if let Some ( ty:: BindByReference ( _) ) = tables. extract_binding_mode ( sess, hir_id, span) {
587
+ by_ref_spans. push ( span) ;
593
588
}
594
589
} ) ;
595
590
@@ -600,7 +595,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
600
595
//
601
596
// `x @ Foo(..)` is legal, but `x @ Foo(y)` isn't.
602
597
if sub. map_or ( false , |p| p. contains_bindings ( ) ) {
603
- struct_span_err ! ( cx . tcx . sess, p. span, E0007 , "cannot bind by-move with sub-bindings" )
598
+ struct_span_err ! ( sess, p. span, E0007 , "cannot bind by-move with sub-bindings" )
604
599
. span_label ( p. span , "binds an already bound by-move value by moving it" )
605
600
. emit ( ) ;
606
601
} else if !has_guard && !by_ref_spans. is_empty ( ) {
@@ -609,15 +604,11 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
609
604
} ;
610
605
pat. walk ( |p| {
611
606
if let hir:: PatKind :: Binding ( .., sub) = & p. kind {
612
- if let Some ( & bm) = cx. tables . pat_binding_modes ( ) . get ( p. hir_id ) {
613
- if let ty:: BindByValue ( ..) = bm {
614
- let pat_ty = cx. tables . node_type ( p. hir_id ) ;
615
- if !pat_ty. is_copy_modulo_regions ( cx. tcx , cx. param_env , pat. span ) {
616
- check_move ( p, sub. as_deref ( ) ) ;
617
- }
607
+ if let Some ( ty:: BindByValue ( _) ) = tables. extract_binding_mode ( sess, p. hir_id , p. span ) {
608
+ let pat_ty = tables. node_type ( p. hir_id ) ;
609
+ if !pat_ty. is_copy_modulo_regions ( cx. tcx , cx. param_env , pat. span ) {
610
+ check_move ( p, sub. as_deref ( ) ) ;
618
611
}
619
- } else {
620
- cx. tcx . sess . delay_span_bug ( pat. span , "missing binding mode" ) ;
621
612
}
622
613
}
623
614
true
@@ -626,7 +617,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
626
617
// Found some bad by-move spans, error!
627
618
if !by_move_spans. is_empty ( ) {
628
619
let mut err = struct_span_err ! (
629
- cx . tcx . sess,
620
+ sess,
630
621
MultiSpan :: from_spans( by_move_spans. clone( ) ) ,
631
622
E0009 ,
632
623
"cannot bind by-move and by-ref in the same pattern" ,
@@ -642,14 +633,12 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
642
633
}
643
634
644
635
fn check_borrow_conflicts_in_at_patterns ( cx : & MatchVisitor < ' _ , ' _ > , pat : & Pat ) {
636
+ let tab = cx. tables ;
637
+ let sess = cx. tcx . sess ;
645
638
// Get the mutability of `p` if it's by-ref.
646
- let extract_binding_mut = |hir_id, span| match cx. tables . pat_binding_modes ( ) . get ( hir_id) {
647
- None => {
648
- cx. tcx . sess . delay_span_bug ( span, "missing binding mode" ) ;
649
- None
650
- }
651
- Some ( ty:: BindByValue ( ..) ) => None ,
652
- Some ( ty:: BindByReference ( m) ) => Some ( * m) ,
639
+ let extract_binding_mut = |hir_id, span| match tab. extract_binding_mode ( sess, hir_id, span) ? {
640
+ ty:: BindByValue ( _) => None ,
641
+ ty:: BindByReference ( m) => Some ( m) ,
653
642
} ;
654
643
pat. walk ( |pat| {
655
644
// Extract `sub` in `binding @ sub`.
@@ -671,8 +660,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
671
660
sub. each_binding ( |_, hir_id, span, _| {
672
661
if let Some ( mut_inner) = extract_binding_mut ( hir_id, span) {
673
662
match ( mut_outer, mut_inner) {
674
- ( Mutability :: Immutable , Mutability :: Immutable ) => { }
675
- ( Mutability :: Mutable , Mutability :: Mutable ) => conflicts_mut_mut. push ( span) ,
663
+ ( Mutability :: Not , Mutability :: Not ) => { }
664
+ ( Mutability :: Mut , Mutability :: Mut ) => conflicts_mut_mut. push ( span) ,
676
665
_ => conflicts_mut_ref. push ( span) ,
677
666
}
678
667
}
@@ -683,7 +672,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
683
672
if !conflicts_mut_mut. is_empty ( ) {
684
673
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
685
674
let msg = & format ! ( "cannot borrow `{}` as mutable more than once at a time" , name) ;
686
- let mut err = cx . tcx . sess . struct_span_err ( pat. span , msg) ;
675
+ let mut err = sess. struct_span_err ( pat. span , msg) ;
687
676
err. span_label ( binding_span, "first mutable borrow occurs here" ) ;
688
677
for sp in conflicts_mut_mut {
689
678
err. span_label ( sp, "another mutable borrow occurs here" ) ;
@@ -695,14 +684,14 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
695
684
} else if !conflicts_mut_ref. is_empty ( ) {
696
685
// Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
697
686
let ( primary, also) = match mut_outer {
698
- Mutability :: Mutable => ( "mutable" , "immutable" ) ,
699
- Mutability :: Immutable => ( "immutable" , "mutable" ) ,
687
+ Mutability :: Mut => ( "mutable" , "immutable" ) ,
688
+ Mutability :: Not => ( "immutable" , "mutable" ) ,
700
689
} ;
701
690
let msg = & format ! (
702
691
"cannot borrow `{}` as {} because it is also borrowed as {}" ,
703
692
name, primary, also,
704
693
) ;
705
- let mut err = cx . tcx . sess . struct_span_err ( pat. span , msg) ;
694
+ let mut err = sess. struct_span_err ( pat. span , msg) ;
706
695
err. span_label ( binding_span, & format ! ( "{} borrow occurs here" , primary) ) ;
707
696
for sp in conflicts_mut_ref {
708
697
err. span_label ( sp, & format ! ( "{} borrow occurs here" , also) ) ;
0 commit comments