@@ -914,58 +914,55 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
914
914
sub. each_binding ( |_, hir_id, span, name| {
915
915
match typeck_results. extract_binding_mode ( sess, hir_id, span) {
916
916
Some ( ty:: BindByReference ( mut_inner) ) => match ( mut_outer, mut_inner) {
917
- ( Mutability :: Not , Mutability :: Not ) => { } // Both sides are `ref`.
918
- ( Mutability :: Mut , Mutability :: Mut ) => conflicts_mut_mut. push ( ( span, name) ) , // 2x `ref mut`.
919
- _ => conflicts_mut_ref. push ( ( span, name) ) , // `ref` + `ref mut` in either direction.
917
+ // Both sides are `ref`.
918
+ ( Mutability :: Not , Mutability :: Not ) => { }
919
+ // 2x `ref mut`.
920
+ ( Mutability :: Mut , Mutability :: Mut ) => {
921
+ conflicts_mut_mut. push ( Conflict :: Mut { span, name } )
922
+ }
923
+ ( Mutability :: Not , Mutability :: Mut ) => {
924
+ conflicts_mut_ref. push ( Conflict :: Mut { span, name } )
925
+ }
926
+ ( Mutability :: Mut , Mutability :: Not ) => {
927
+ conflicts_mut_ref. push ( Conflict :: Ref { span, name } )
928
+ }
920
929
} ,
921
930
Some ( ty:: BindByValue ( _) ) if is_binding_by_move ( cx, hir_id) => {
922
- conflicts_move. push ( ( span, name) ) // `ref mut?` + by-move conflict.
931
+ conflicts_move. push ( Conflict :: Moved { span, name } ) // `ref mut?` + by-move conflict.
923
932
}
924
933
Some ( ty:: BindByValue ( _) ) | None => { } // `ref mut?` + by-copy is fine.
925
934
}
926
935
} ) ;
927
936
937
+ let report_mut_mut = !conflicts_mut_mut. is_empty ( ) ;
938
+ let report_mut_ref = !conflicts_mut_ref. is_empty ( ) ;
939
+ let report_move_conflict = !conflicts_move. is_empty ( ) ;
940
+
941
+ let mut occurences = match mut_outer {
942
+ Mutability :: Mut => vec ! [ Conflict :: Mut { span: binding_span, name } ] ,
943
+ Mutability :: Not => vec ! [ Conflict :: Ref { span: binding_span, name } ] ,
944
+ } ;
945
+ occurences. extend ( conflicts_mut_mut) ;
946
+ occurences. extend ( conflicts_mut_ref) ;
947
+ occurences. extend ( conflicts_move) ;
948
+
928
949
// Report errors if any.
929
- if !conflicts_mut_mut . is_empty ( ) {
950
+ if report_mut_mut {
930
951
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
931
- let mut occurences = vec ! [ ] ;
932
-
933
- for ( span, name_mut) in conflicts_mut_mut {
934
- occurences. push ( MultipleMutBorrowOccurence :: Mutable { span, name_mut } ) ;
935
- }
936
- for ( span, name_immut) in conflicts_mut_ref {
937
- occurences. push ( MultipleMutBorrowOccurence :: Immutable { span, name_immut } ) ;
938
- }
939
- for ( span, name_moved) in conflicts_move {
940
- occurences. push ( MultipleMutBorrowOccurence :: Moved { span, name_moved } ) ;
941
- }
942
- sess. emit_err ( MultipleMutBorrows { span : pat. span , binding_span, occurences, name } ) ;
943
- } else if !conflicts_mut_ref. is_empty ( ) {
952
+ sess. emit_err ( MultipleMutBorrows { span : pat. span , occurences } ) ;
953
+ } else if report_mut_ref {
944
954
// Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
945
- let ( primary, also) = match mut_outer {
946
- Mutability :: Mut => ( "mutable" , "immutable" ) ,
947
- Mutability :: Not => ( "immutable" , "mutable" ) ,
955
+ match mut_outer {
956
+ Mutability :: Mut => {
957
+ sess. emit_err ( AlreadyMutBorrowed { span : pat. span , occurences } ) ;
958
+ }
959
+ Mutability :: Not => {
960
+ sess. emit_err ( AlreadyBorrowed { span : pat. span , occurences } ) ;
961
+ }
948
962
} ;
949
- let msg =
950
- format ! ( "cannot borrow value as {} because it is also borrowed as {}" , also, primary) ;
951
- let mut err = sess. struct_span_err ( pat. span , & msg) ;
952
- err. span_label ( binding_span, format ! ( "{} borrow, by `{}`, occurs here" , primary, name) ) ;
953
- for ( span, name) in conflicts_mut_ref {
954
- err. span_label ( span, format ! ( "{} borrow, by `{}`, occurs here" , also, name) ) ;
955
- }
956
- for ( span, name) in conflicts_move {
957
- err. span_label ( span, format ! ( "also moved into `{}` here" , name) ) ;
958
- }
959
- err. emit ( ) ;
960
- } else if !conflicts_move. is_empty ( ) {
963
+ } else if report_move_conflict {
961
964
// Report by-ref and by-move conflicts, e.g. `ref x @ y`.
962
- let mut err =
963
- sess. struct_span_err ( pat. span , "cannot move out of value because it is borrowed" ) ;
964
- err. span_label ( binding_span, format ! ( "value borrowed, by `{}`, here" , name) ) ;
965
- for ( span, name) in conflicts_move {
966
- err. span_label ( span, format ! ( "value moved into `{}` here" , name) ) ;
967
- }
968
- err. emit ( ) ;
965
+ sess. emit_err ( MovedWhileBorrowed { span : pat. span , occurences } ) ;
969
966
}
970
967
}
971
968
0 commit comments