Skip to content

Commit 38f7f84

Browse files
committed
borrowck error E0499 migrate
1 parent c3a0fd0 commit 38f7f84

File tree

4 files changed

+98
-35
lines changed

4 files changed

+98
-35
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -40,51 +40,43 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
4040
pub(crate) fn cannot_mutably_borrow_multiply(
4141
&self,
4242
new_loan_span: Span,
43-
desc: &str,
44-
opt_via: &str,
43+
new_place_name: &str,
44+
place: &str,
4545
old_loan_span: Span,
46-
old_opt_via: &str,
46+
old_place: &str,
4747
old_load_end_span: Option<Span>,
4848
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
49-
let via =
50-
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
51-
let mut err = struct_span_err!(
52-
self,
53-
new_loan_span,
54-
E0499,
55-
"cannot borrow {}{} as mutable more than once at a time",
56-
desc,
57-
via(opt_via),
58-
);
59-
if old_loan_span == new_loan_span {
49+
use crate::session_diagnostics::MutBorrowMulti::*;
50+
let via = |msg: &str| msg.is_empty();
51+
self.infcx.tcx.sess.create_err(if old_loan_span == new_loan_span {
6052
// Both borrows are happening in the same place
6153
// Meaning the borrow is occurring in a loop
62-
err.span_label(
54+
SameSpan {
55+
new_place_name,
56+
place,
57+
old_place,
58+
is_place_empty: via(place),
6359
new_loan_span,
64-
format!(
65-
"{}{} was mutably borrowed here in the previous iteration of the loop{}",
66-
desc,
67-
via(opt_via),
68-
opt_via,
69-
),
70-
);
71-
if let Some(old_load_end_span) = old_load_end_span {
72-
err.span_label(old_load_end_span, "mutable borrow ends here");
60+
old_load_end_span,
61+
eager_label: crate::session_diagnostics::MutMultiLoopLabel {
62+
new_place_name,
63+
place,
64+
is_place_empty: via(place),
65+
new_loan_span,
66+
},
7367
}
7468
} else {
75-
err.span_label(
76-
old_loan_span,
77-
format!("first mutable borrow occurs here{}", via(old_opt_via)),
78-
);
79-
err.span_label(
69+
ChangedSpan {
70+
new_place_name,
71+
place,
72+
old_place,
73+
is_place_empty: via(place),
74+
is_old_place_empty: via(old_place),
8075
new_loan_span,
81-
format!("second mutable borrow occurs here{}", via(opt_via)),
82-
);
83-
if let Some(old_load_end_span) = old_load_end_span {
84-
err.span_label(old_load_end_span, "first borrow ends here");
76+
old_loan_span,
77+
old_load_end_span,
8578
}
86-
}
87-
err
79+
})
8880
}
8981

9082
pub(crate) fn cannot_uniquely_borrow_by_two_closures(

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13001300
/// cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
13011301
/// mutable (via `a.u.s.b`) [E0502]
13021302
/// ```
1303+
// FIXME: first 3 out of 4 can contain English word value, note for diagnostic migration
13031304
pub(crate) fn describe_place_for_conflicting_borrow(
13041305
&self,
13051306
first_borrowed_place: Place<'tcx>,

compiler/rustc_borrowck/src/session_diagnostics.rs

+42
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,45 @@ pub(crate) struct UseMutBorrowErr<'a> {
707707
#[label(borrow_span_label)]
708708
pub borrow_span: Span,
709709
}
710+
711+
#[derive(Diagnostic)]
712+
pub(crate) enum MutBorrowMulti<'a> {
713+
#[diag(borrowck_cannot_mutably_borrow_multiply_same_span, code = "E0499")]
714+
SameSpan {
715+
new_place_name: &'a str,
716+
place: &'a str,
717+
old_place: &'a str,
718+
is_place_empty: bool,
719+
#[primary_span]
720+
new_loan_span: Span,
721+
#[label]
722+
old_load_end_span: Option<Span>,
723+
#[subdiagnostic(eager)]
724+
eager_label: MutMultiLoopLabel<'a>,
725+
},
726+
#[diag(borrowck_cannot_mutably_borrow_multiply, code = "E0499")]
727+
ChangedSpan {
728+
new_place_name: &'a str,
729+
place: &'a str,
730+
old_place: &'a str,
731+
is_place_empty: bool,
732+
is_old_place_empty: bool,
733+
#[primary_span]
734+
#[label(second_mut_borrow_label)]
735+
new_loan_span: Span,
736+
#[label]
737+
old_loan_span: Span,
738+
#[label(first_mut_end_label)]
739+
old_load_end_span: Option<Span>,
740+
},
741+
}
742+
743+
#[derive(Subdiagnostic)]
744+
#[label(borrowck_mutably_borrow_multiply_loop_label)]
745+
pub(crate) struct MutMultiLoopLabel<'a> {
746+
pub new_place_name: &'a str,
747+
pub place: &'a str,
748+
pub is_place_empty: bool,
749+
#[primary_span]
750+
pub new_loan_span: Span,
751+
}

compiler/rustc_error_messages/locales/en-US/borrowck.ftl

+28
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,31 @@ borrowck_cannot_use_when_mutably_borrowed =
434434
[value] value
435435
*[other] {$borrow_desc}
436436
} occurs here
437+
438+
borrowck_cannot_mutably_borrow_multiply_same_span =
439+
cannot borrow {$new_place_name}{$is_place_empty ->
440+
*[true] {""}
441+
[false] {""} (via {$place})
442+
} as mutable more than once at a time
443+
.label = mutable borrow ends here
444+
445+
borrowck_mutably_borrow_multiply_loop_label =
446+
{$new_place_name}{$is_place_empty ->
447+
*[true] {""}
448+
[false] {""} (via {$place})
449+
} was mutably borrowed here in the previous iteration of the loop{$place}
450+
451+
borrowck_cannot_mutably_borrow_multiply =
452+
cannot borrow {$new_place_name}{$is_place_empty ->
453+
*[true] {""}
454+
[false] {""} (via {$place})
455+
} as mutable more than once at a time
456+
.label = first mutable borrow occurs here{$is_old_place_empty ->
457+
*[true] {""}
458+
[false] {""} (via {$old_place})
459+
}
460+
.second_mut_borrow_label = second mutable borrow occurs here{$is_place_empty ->
461+
*[true] {""}
462+
[false] {""} (via {$place})
463+
}
464+
.first_mut_end_label = first borrow ends here

0 commit comments

Comments
 (0)