Skip to content

Commit 1560a75

Browse files
committed
Refer to the second borrow as the "second borrow".
1 parent 3e90a12 commit 1560a75

File tree

9 files changed

+30
-11
lines changed

9 files changed

+30
-11
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
615615
let new_loan_str = &new_loan.kind.to_user_str();
616616
self.bccx.cannot_reborrow_already_uniquely_borrowed(
617617
new_loan.span, "closure", &nl, &new_loan_msg, new_loan_str,
618-
old_loan.span, &old_loan_msg, previous_end_span, Origin::Ast)
618+
old_loan.span, &old_loan_msg, previous_end_span, "", Origin::Ast)
619619
}
620620
(..) =>
621621
self.bccx.cannot_reborrow_already_borrowed(

src/librustc_mir/borrow_check/error_reporting.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
344344

345345
let first_borrow_desc;
346346

347+
let explanation = self.explain_why_borrow_contains_point(context, issued_borrow, None);
348+
let second_borrow_desc = if explanation.is_explained() {
349+
"second "
350+
} else {
351+
""
352+
};
353+
347354
// FIXME: supply non-"" `opt_via` when appropriate
348355
let mut err = match (
349356
gen_borrow_kind,
@@ -454,6 +461,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
454461
issued_span,
455462
"",
456463
None,
464+
second_borrow_desc,
457465
Origin::Mir,
458466
)
459467
}
@@ -469,6 +477,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
469477
issued_span,
470478
"",
471479
None,
480+
second_borrow_desc,
472481
Origin::Mir,
473482
)
474483
}
@@ -513,7 +522,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
513522
);
514523
}
515524

516-
self.explain_why_borrow_contains_point(context, issued_borrow, None)
525+
explanation
517526
.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, first_borrow_desc);
518527

519528
err.buffer(&mut self.errors_buffer);

src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ pub(in borrow_check) enum LaterUseKind {
5252
}
5353

5454
impl BorrowExplanation {
55+
pub(in borrow_check) fn is_explained(&self) -> bool {
56+
match self {
57+
BorrowExplanation::Unexplained => false,
58+
_ => true,
59+
}
60+
}
5561
pub(in borrow_check) fn add_explanation_to_diagnostic<'cx, 'gcx, 'tcx>(
5662
&self,
5763
tcx: TyCtxt<'cx, 'gcx, 'tcx>,

src/librustc_mir/util/borrowck_errors.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
261261
old_loan_span: Span,
262262
old_opt_via: &str,
263263
previous_end_span: Option<Span>,
264+
second_borrow_desc: &str,
264265
o: Origin,
265266
) -> DiagnosticBuilder<'cx> {
266267
let mut err = struct_span_err!(
@@ -274,7 +275,10 @@ pub trait BorrowckErrors<'cx>: Sized + Copy {
274275
kind_new,
275276
OGN = o
276277
);
277-
err.span_label(new_loan_span, format!("borrow occurs here{}", opt_via));
278+
err.span_label(
279+
new_loan_span,
280+
format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
281+
);
278282
err.span_label(
279283
old_loan_span,
280284
format!("{} construction occurs here{}", container_name, old_opt_via),

src/test/ui/E0501.ast.nll.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | inside_closure(a)
77
| - first borrow occurs due to use of `a` in closure
88
LL | };
99
LL | outside_closure_1(a); //[ast]~ ERROR cannot borrow `*a` as mutable because previous closure requires unique access
10-
| ^ borrow occurs here
10+
| ^ second borrow occurs here
1111
...
1212
LL | drop(bar);
1313
| --- first borrow later used here
@@ -21,7 +21,7 @@ LL | inside_closure(a)
2121
| - first borrow occurs due to use of `a` in closure
2222
...
2323
LL | outside_closure_2(a); //[ast]~ ERROR cannot borrow `*a` as immutable because previous closure requires unique access
24-
| ^ borrow occurs here
24+
| ^ second borrow occurs here
2525
...
2626
LL | drop(bar);
2727
| --- first borrow later used here

src/test/ui/E0501.mir.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | inside_closure(a)
77
| - first borrow occurs due to use of `a` in closure
88
LL | };
99
LL | outside_closure_1(a); //[ast]~ ERROR cannot borrow `*a` as mutable because previous closure requires unique access
10-
| ^ borrow occurs here
10+
| ^ second borrow occurs here
1111
...
1212
LL | drop(bar);
1313
| --- first borrow later used here
@@ -21,7 +21,7 @@ LL | inside_closure(a)
2121
| - first borrow occurs due to use of `a` in closure
2222
...
2323
LL | outside_closure_2(a); //[ast]~ ERROR cannot borrow `*a` as immutable because previous closure requires unique access
24-
| ^ borrow occurs here
24+
| ^ second borrow occurs here
2525
...
2626
LL | drop(bar);
2727
| --- first borrow later used here

src/test/ui/borrowck/borrowck-insert-during-each.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | | |a| { //~ ERROR closure requires unique access to `f`
1010
LL | | f.n.insert(*a);
1111
| | - first borrow occurs due to use of `f` in closure
1212
LL | | })
13-
| |__________^ borrow occurs here
13+
| |__________^ second borrow occurs here
1414

1515
error[E0500]: closure requires unique access to `f` but it is already borrowed
1616
--> $DIR/borrowck-insert-during-each.rs:27:9

src/test/ui/generator/yield-while-ref-reborrowed.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let a = &mut *x;
77
| - first borrow occurs due to use of `x` in generator
88
...
99
LL | println!("{}", x); //~ ERROR
10-
| ^ borrow occurs here
10+
| ^ second borrow occurs here
1111
LL | b.resume();
1212
| - first borrow later used here
1313

src/test/ui/nll/closure-borrow-spans.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ LL | let f = || *x = 0;
126126
| |
127127
| closure construction occurs here
128128
LL | let y = &x; //~ ERROR
129-
| ^^ borrow occurs here
129+
| ^^ second borrow occurs here
130130
LL | f.use_ref();
131131
| - first borrow later used here
132132

@@ -138,7 +138,7 @@ LL | let f = || *x = 0;
138138
| |
139139
| closure construction occurs here
140140
LL | let y = &mut x; //~ ERROR
141-
| ^^^^^^ borrow occurs here
141+
| ^^^^^^ second borrow occurs here
142142
LL | f.use_ref();
143143
| - first borrow later used here
144144

0 commit comments

Comments
 (0)