@@ -476,7 +476,10 @@ impl<'a> CheckLoanCtxt<'a> {
476476 span : Span ,
477477 move_path : & LoanPath ,
478478 move_kind : move_data:: MoveKind ) {
479- match self . analyze_move_out_from ( id, move_path) {
479+ // We want to detect if there are any loans at all, so we search for
480+ // any loans incompatible with MutBorrrow, since all other kinds of
481+ // loans are incompatible with that.
482+ match self . analyze_move_out_from ( id, move_path, ty:: MutBorrow ) {
480483 MoveOk => { }
481484 MoveWhileBorrowed ( loan_path, loan_span) => {
482485 let err_message = match move_kind {
@@ -865,7 +868,8 @@ impl<'a> CheckLoanCtxt<'a> {
865868
866869 pub fn analyze_move_out_from ( & self ,
867870 expr_id : ast:: NodeId ,
868- move_path : & LoanPath )
871+ move_path : & LoanPath ,
872+ borrow_kind : ty:: BorrowKind )
869873 -> MoveError {
870874 debug ! ( "analyze_move_out_from(expr_id={:?}, move_path={})" ,
871875 self . tcx( ) . map. node_to_str( expr_id) ,
@@ -881,9 +885,12 @@ impl<'a> CheckLoanCtxt<'a> {
881885 // let y = a; // Conflicts with restriction
882886
883887 self . each_in_scope_restriction ( expr_id, move_path, |loan, _restr| {
884- // Any restriction prevents moves.
885- ret = MoveWhileBorrowed ( loan. loan_path . clone ( ) , loan. span ) ;
886- false
888+ if incompatible ( loan. kind , borrow_kind) {
889+ ret = MoveWhileBorrowed ( loan. loan_path . clone ( ) , loan. span ) ;
890+ false
891+ } else {
892+ true
893+ }
887894 } ) ;
888895
889896 // Next, we must check for *loans* (not restrictions) on the path P or
@@ -901,8 +908,8 @@ impl<'a> CheckLoanCtxt<'a> {
901908 let mut loan_path = move_path;
902909 loop {
903910 self . each_in_scope_loan ( expr_id, |loan| {
904- // Any restriction prevents moves.
905- if * loan. loan_path == * loan_path {
911+ if * loan . loan_path == * loan_path &&
912+ incompatible ( loan. kind , borrow_kind ) {
906913 ret = MoveWhileBorrowed ( loan. loan_path . clone ( ) , loan. span ) ;
907914 false
908915 } else {
@@ -921,5 +928,11 @@ impl<'a> CheckLoanCtxt<'a> {
921928 }
922929
923930 return ret;
931+
932+ fn incompatible ( borrow_kind1 : ty:: BorrowKind ,
933+ borrow_kind2 : ty:: BorrowKind )
934+ -> bool {
935+ borrow_kind1 != ty:: ImmBorrow || borrow_kind2 != ty:: ImmBorrow
936+ }
924937 }
925938}
0 commit comments