diff options
| author | Isaac van Bakel <ivb@vanbakel.io> | 2017-07-25 01:13:18 +0100 |
|---|---|---|
| committer | Isaac van Bakel <ivb@vanbakel.io> | 2017-07-25 18:58:38 +0100 |
| commit | c802fc7103c35905e06e59e781a083f8ea36c0a2 (patch) | |
| tree | cf114954e0a31c2b438f1a2db780f1832a16636a | |
| parent | 5c126bdee6af3feef2d51956debab72f933078c6 (diff) | |
| download | rust-c802fc7103c35905e06e59e781a083f8ea36c0a2.tar.gz rust-c802fc7103c35905e06e59e781a083f8ea36c0a2.zip | |
Modified error for loop mut borrow conflicts
Error message now makes clear that mutable borrow conflicts on a single value in a loop body is caused by the borrow outlasting a single pass of the loop. Loop conflicts are detected by seeing when two borrow locations are the same - which indicates the same code being run more than once.
| -rw-r--r-- | src/librustc_borrowck/borrowck/check_loans.rs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs index b84cd212c4a..e70b7f89a67 100644 --- a/src/librustc_borrowck/borrowck/check_loans.rs +++ b/src/librustc_borrowck/borrowck/check_loans.rs @@ -466,19 +466,33 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> { let mut err = match (new_loan.kind, old_loan.kind) { (ty::MutBorrow, ty::MutBorrow) => { let mut err = struct_span_err!(self.bccx, new_loan.span, E0499, - "cannot borrow `{}`{} as mutable \ - more than once at a time", - nl, new_loan_msg); - err.span_label( - old_loan.span, - format!("first mutable borrow occurs here{}", old_loan_msg)); - err.span_label( - new_loan.span, - format!("second mutable borrow occurs here{}", new_loan_msg)); - err.span_label( - previous_end_span, - "first borrow ends here"); - err + "cannot borrow `{}`{} as mutable \ + more than once at a time", + nl, new_loan_msg); + + if new_loan.span == old_loan.span { + // Both borrows are happening in the same place + // Meaning the borrow is occuring in a loop + err.span_label( + new_loan.span, + format!("mutable borrow starts here in previous \ + iteration of loop{}", new_loan_msg)); + err.span_label( + previous_end_span, + "mutable borrow ends here"); + err + } else { + err.span_label( + old_loan.span, + format!("first mutable borrow occurs here{}", old_loan_msg)); + err.span_label( + new_loan.span, + format!("second mutable borrow occurs here{}", new_loan_msg)); + err.span_label( + previous_end_span, + "first borrow ends here"); + err + } } (ty::UniqueImmBorrow, ty::UniqueImmBorrow) => { |
