Skip to content

Commit

Permalink
Rollup merge of rust-lang#56372 - wildarch:issue-55314-second-borrow-…
Browse files Browse the repository at this point in the history
…ref, r=davidtwco

Refer to the second borrow as the "second borrow" in E0501.rs

Fixes rust-lang#55314.

r? @davidtwco
  • Loading branch information
GuillaumeGomez authored Dec 4, 2018
2 parents 7eda308 + 1560a75 commit 42ad3ab
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
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(
Expand Down
11 changes: 10 additions & 1 deletion src/librustc_mir/borrow_check/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {

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,
Expand Down Expand Up @@ -454,6 +461,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
issued_span,
"",
None,
second_borrow_desc,
Origin::Mir,
)
}
Expand All @@ -469,6 +477,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
issued_span,
"",
None,
second_borrow_desc,
Origin::Mir,
)
}
Expand Down Expand Up @@ -513,7 +522,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
);
}

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);
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/util/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
old_loan_span: Span,
old_opt_via: &str,
previous_end_span: Option<Span>,
second_borrow_desc: &str,
o: Origin,
) -> DiagnosticBuilder<'cx> {
let mut err = struct_span_err!(
Expand All @@ -274,7 +275,10 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
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),
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/E0501.ast.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/E0501.mir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/nll/closure-borrow-spans.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 42ad3ab

Please sign in to comment.