From 1560a75f6a7dfd51e4d7f6603e3959388d4bcb03 Mon Sep 17 00:00:00 2001 From: Daan de Graaf Date: Fri, 30 Nov 2018 14:55:51 +0100 Subject: [PATCH] Refer to the second borrow as the "second borrow". --- src/librustc_borrowck/borrowck/check_loans.rs | 2 +- src/librustc_mir/borrow_check/error_reporting.rs | 11 ++++++++++- .../borrow_check/nll/explain_borrow/mod.rs | 6 ++++++ src/librustc_mir/util/borrowck_errors.rs | 6 +++++- src/test/ui/E0501.ast.nll.stderr | 4 ++-- src/test/ui/E0501.mir.stderr | 4 ++-- .../borrowck/borrowck-insert-during-each.nll.stderr | 2 +- .../generator/yield-while-ref-reborrowed.nll.stderr | 2 +- src/test/ui/nll/closure-borrow-spans.stderr | 4 ++-- 9 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs index a802729e3fb..e47e95afb16 100644 --- a/src/librustc_borrowck/borrowck/check_loans.rs +++ b/src/librustc_borrowck/borrowck/check_loans.rs @@ -615,7 +615,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, let new_loan_str = &new_loan.kind.to_user_str(); self.bccx.cannot_reborrow_already_uniquely_borrowed( new_loan.span, "closure", &nl, &new_loan_msg, new_loan_str, - old_loan.span, &old_loan_msg, previous_end_span, Origin::Ast) + old_loan.span, &old_loan_msg, previous_end_span, "", Origin::Ast) } (..) => self.bccx.cannot_reborrow_already_borrowed( diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs index 4ccd26bee8b..ba26ed36c20 100644 --- a/src/librustc_mir/borrow_check/error_reporting.rs +++ b/src/librustc_mir/borrow_check/error_reporting.rs @@ -344,6 +344,13 @@ pub(super) fn report_conflicting_borrow( let first_borrow_desc; + let explanation = self.explain_why_borrow_contains_point(context, issued_borrow, None); + let second_borrow_desc = if explanation.is_explained() { + "second " + } else { + "" + }; + // FIXME: supply non-"" `opt_via` when appropriate let mut err = match ( gen_borrow_kind, @@ -454,6 +461,7 @@ pub(super) fn report_conflicting_borrow( issued_span, "", None, + second_borrow_desc, Origin::Mir, ) } @@ -469,6 +477,7 @@ pub(super) fn report_conflicting_borrow( issued_span, "", None, + second_borrow_desc, Origin::Mir, ) } @@ -513,7 +522,7 @@ pub(super) fn report_conflicting_borrow( ); } - self.explain_why_borrow_contains_point(context, issued_borrow, None) + explanation .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, first_borrow_desc); err.buffer(&mut self.errors_buffer); diff --git a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs index bb9a29b055b..477b7892608 100644 --- a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs +++ b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs @@ -52,6 +52,12 @@ pub(in borrow_check) enum LaterUseKind { } impl BorrowExplanation { + pub(in borrow_check) fn is_explained(&self) -> bool { + match self { + BorrowExplanation::Unexplained => false, + _ => true, + } + } pub(in borrow_check) fn add_explanation_to_diagnostic<'cx, 'gcx, 'tcx>( &self, tcx: TyCtxt<'cx, 'gcx, 'tcx>, diff --git a/src/librustc_mir/util/borrowck_errors.rs b/src/librustc_mir/util/borrowck_errors.rs index ae0483e3c14..8566f845f23 100644 --- a/src/librustc_mir/util/borrowck_errors.rs +++ b/src/librustc_mir/util/borrowck_errors.rs @@ -261,6 +261,7 @@ fn cannot_reborrow_already_uniquely_borrowed( old_loan_span: Span, old_opt_via: &str, previous_end_span: Option, + second_borrow_desc: &str, o: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( @@ -274,7 +275,10 @@ fn cannot_reborrow_already_uniquely_borrowed( kind_new, OGN = o ); - err.span_label(new_loan_span, format!("borrow occurs here{}", opt_via)); + err.span_label( + new_loan_span, + format!("{}borrow occurs here{}", second_borrow_desc, opt_via), + ); err.span_label( old_loan_span, format!("{} construction occurs here{}", container_name, old_opt_via), diff --git a/src/test/ui/E0501.ast.nll.stderr b/src/test/ui/E0501.ast.nll.stderr index 72fda9079d6..8a3fce3a554 100644 --- a/src/test/ui/E0501.ast.nll.stderr +++ b/src/test/ui/E0501.ast.nll.stderr @@ -7,7 +7,7 @@ LL | inside_closure(a) | - first borrow occurs due to use of `a` in closure LL | }; LL | outside_closure_1(a); //[ast]~ ERROR cannot borrow `*a` as mutable because previous closure requires unique access - | ^ borrow occurs here + | ^ second borrow occurs here ... LL | drop(bar); | --- first borrow later used here @@ -21,7 +21,7 @@ LL | inside_closure(a) | - first borrow occurs due to use of `a` in closure ... LL | outside_closure_2(a); //[ast]~ ERROR cannot borrow `*a` as immutable because previous closure requires unique access - | ^ borrow occurs here + | ^ second borrow occurs here ... LL | drop(bar); | --- first borrow later used here diff --git a/src/test/ui/E0501.mir.stderr b/src/test/ui/E0501.mir.stderr index 72fda9079d6..8a3fce3a554 100644 --- a/src/test/ui/E0501.mir.stderr +++ b/src/test/ui/E0501.mir.stderr @@ -7,7 +7,7 @@ LL | inside_closure(a) | - first borrow occurs due to use of `a` in closure LL | }; LL | outside_closure_1(a); //[ast]~ ERROR cannot borrow `*a` as mutable because previous closure requires unique access - | ^ borrow occurs here + | ^ second borrow occurs here ... LL | drop(bar); | --- first borrow later used here @@ -21,7 +21,7 @@ LL | inside_closure(a) | - first borrow occurs due to use of `a` in closure ... LL | outside_closure_2(a); //[ast]~ ERROR cannot borrow `*a` as immutable because previous closure requires unique access - | ^ borrow occurs here + | ^ second borrow occurs here ... LL | drop(bar); | --- first borrow later used here diff --git a/src/test/ui/borrowck/borrowck-insert-during-each.nll.stderr b/src/test/ui/borrowck/borrowck-insert-during-each.nll.stderr index 123d475f1c0..84fc69465cb 100644 --- a/src/test/ui/borrowck/borrowck-insert-during-each.nll.stderr +++ b/src/test/ui/borrowck/borrowck-insert-during-each.nll.stderr @@ -10,7 +10,7 @@ LL | | |a| { //~ ERROR closure requires unique access to `f` LL | | f.n.insert(*a); | | - first borrow occurs due to use of `f` in closure LL | | }) - | |__________^ borrow occurs here + | |__________^ second borrow occurs here error[E0500]: closure requires unique access to `f` but it is already borrowed --> $DIR/borrowck-insert-during-each.rs:27:9 diff --git a/src/test/ui/generator/yield-while-ref-reborrowed.nll.stderr b/src/test/ui/generator/yield-while-ref-reborrowed.nll.stderr index 8dabb3c2505..60163d78e78 100644 --- a/src/test/ui/generator/yield-while-ref-reborrowed.nll.stderr +++ b/src/test/ui/generator/yield-while-ref-reborrowed.nll.stderr @@ -7,7 +7,7 @@ LL | let a = &mut *x; | - first borrow occurs due to use of `x` in generator ... LL | println!("{}", x); //~ ERROR - | ^ borrow occurs here + | ^ second borrow occurs here LL | b.resume(); | - first borrow later used here diff --git a/src/test/ui/nll/closure-borrow-spans.stderr b/src/test/ui/nll/closure-borrow-spans.stderr index 3e423dadd19..98a261657b1 100644 --- a/src/test/ui/nll/closure-borrow-spans.stderr +++ b/src/test/ui/nll/closure-borrow-spans.stderr @@ -126,7 +126,7 @@ LL | let f = || *x = 0; | | | closure construction occurs here LL | let y = &x; //~ ERROR - | ^^ borrow occurs here + | ^^ second borrow occurs here LL | f.use_ref(); | - first borrow later used here @@ -138,7 +138,7 @@ LL | let f = || *x = 0; | | | closure construction occurs here LL | let y = &mut x; //~ ERROR - | ^^^^^^ borrow occurs here + | ^^^^^^ second borrow occurs here LL | f.use_ref(); | - first borrow later used here