Skip to content

Commit c802fc7

Browse files
committed
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.
1 parent 5c126bd commit c802fc7

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -466,19 +466,33 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
466466
let mut err = match (new_loan.kind, old_loan.kind) {
467467
(ty::MutBorrow, ty::MutBorrow) => {
468468
let mut err = struct_span_err!(self.bccx, new_loan.span, E0499,
469-
"cannot borrow `{}`{} as mutable \
470-
more than once at a time",
471-
nl, new_loan_msg);
472-
err.span_label(
473-
old_loan.span,
474-
format!("first mutable borrow occurs here{}", old_loan_msg));
475-
err.span_label(
476-
new_loan.span,
477-
format!("second mutable borrow occurs here{}", new_loan_msg));
478-
err.span_label(
479-
previous_end_span,
480-
"first borrow ends here");
481-
err
469+
"cannot borrow `{}`{} as mutable \
470+
more than once at a time",
471+
nl, new_loan_msg);
472+
473+
if new_loan.span == old_loan.span {
474+
// Both borrows are happening in the same place
475+
// Meaning the borrow is occuring in a loop
476+
err.span_label(
477+
new_loan.span,
478+
format!("mutable borrow starts here in previous \
479+
iteration of loop{}", new_loan_msg));
480+
err.span_label(
481+
previous_end_span,
482+
"mutable borrow ends here");
483+
err
484+
} else {
485+
err.span_label(
486+
old_loan.span,
487+
format!("first mutable borrow occurs here{}", old_loan_msg));
488+
err.span_label(
489+
new_loan.span,
490+
format!("second mutable borrow occurs here{}", new_loan_msg));
491+
err.span_label(
492+
previous_end_span,
493+
"first borrow ends here");
494+
err
495+
}
482496
}
483497

484498
(ty::UniqueImmBorrow, ty::UniqueImmBorrow) => {

0 commit comments

Comments
 (0)