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