Skip to content

Commit 86d1178

Browse files
committed
Auto merge of #45603 - joshleeb:iss42106, r=estebank
Fix duplicate display of error E0502 Ref. Repeated "mutable/immutable borrow" error messages #42106. This PR modifies the return type of [`report_error_if_loan_conflicts_with_restriction`](https://github.com/rust-lang/rust/blob/0f0f5db465de96b6c12e71f0c7d3e475f618b104/src/librustc_borrowck/borrowck/check_loans.rs#L398-L403) so the result can be checked in [`report_error_if_loans_conflict`](https://github.com/rust-lang/rust/blob/0f0f5db465de96b6c12e71f0c7d3e475f618b104/src/librustc_borrowck/borrowck/check_loans.rs#L377-L396). This is done to prevent displaying a duplicate of the error message E0502 which is referenced in #42106. The output of compiling: ```rust fn do_something<T>(collection: &mut Vec<T>) { let _a = &collection; collection.swap(1, 2); } fn main() {} ``` is now ```bash $ rustc src/test/compile-fail/issue-42106.rs error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable --> src/test/compile-fail/issue-42106.rs:13:5 | 12 | let _a = &collection; | ---------- immutable borrow occurs here 13 | collection.swap(1, 2); | ^^^^^^^^^^ mutable borrow occurs here 14 | } | - immutable borrow ends here error: aborting due to 2 previous errors ``` r? @estebank
2 parents 875ec8d + cf10bcf commit 86d1178

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,29 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
395395
assert!(self.bccx.region_scope_tree.scopes_intersect(old_loan.kill_scope,
396396
new_loan.kill_scope));
397397

398-
self.report_error_if_loan_conflicts_with_restriction(
399-
old_loan, new_loan, old_loan, new_loan) &&
400-
self.report_error_if_loan_conflicts_with_restriction(
401-
new_loan, old_loan, old_loan, new_loan)
398+
let err_old_new = self.report_error_if_loan_conflicts_with_restriction(
399+
old_loan, new_loan, old_loan, new_loan).err();
400+
let err_new_old = self.report_error_if_loan_conflicts_with_restriction(
401+
new_loan, old_loan, old_loan, new_loan).err();
402+
403+
match (err_old_new, err_new_old) {
404+
(Some(mut err), None) | (None, Some(mut err)) => err.emit(),
405+
(Some(mut err_old), Some(mut err_new)) => {
406+
err_old.emit();
407+
err_new.cancel();
408+
}
409+
(None, None) => return true,
410+
}
411+
412+
false
402413
}
403414

404415
pub fn report_error_if_loan_conflicts_with_restriction(&self,
405416
loan1: &Loan<'tcx>,
406417
loan2: &Loan<'tcx>,
407418
old_loan: &Loan<'tcx>,
408419
new_loan: &Loan<'tcx>)
409-
-> bool {
420+
-> Result<(), DiagnosticBuilder<'a>> {
410421
//! Checks whether the restrictions introduced by `loan1` would
411422
//! prohibit `loan2`. Returns false if an error is reported.
412423
@@ -416,7 +427,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
416427
loan2);
417428

418429
if compatible_borrow_kinds(loan1.kind, loan2.kind) {
419-
return true;
430+
return Ok(());
420431
}
421432

422433
let loan2_base_path = owned_ptr_base_path_rc(&loan2.loan_path);
@@ -520,11 +531,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
520531
_ => { }
521532
}
522533

523-
err.emit();
524-
return false;
534+
return Err(err);
525535
}
526536

527-
true
537+
Ok(())
528538
}
529539

530540
fn consume_common(&self,

src/test/ui/issue-42106.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn do_something<T>(collection: &mut Vec<T>) {
12+
let _a = &collection;
13+
collection.swap(1, 2);
14+
}
15+
16+
fn main() {}

src/test/ui/issue-42106.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable
2+
--> $DIR/issue-42106.rs:13:5
3+
|
4+
12 | let _a = &collection;
5+
| ---------- immutable borrow occurs here
6+
13 | collection.swap(1, 2);
7+
| ^^^^^^^^^^ mutable borrow occurs here
8+
14 | }
9+
| - immutable borrow ends here
10+
11+
error: aborting due to 2 previous errors
12+

0 commit comments

Comments
 (0)