Skip to content

Commit 19d230b

Browse files
committed
E0500 E0501
1 parent 38f7f84 commit 19d230b

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+14-32
Original file line numberDiff line numberDiff line change
@@ -104,38 +104,30 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
104104
pub(crate) fn cannot_uniquely_borrow_by_one_closure(
105105
&self,
106106
new_loan_span: Span,
107-
container_name: &str,
107+
is_generator: bool,
108108
desc_new: &str,
109109
opt_via: &str,
110110
old_loan_span: Span,
111111
noun_old: &str,
112112
old_opt_via: &str,
113113
previous_end_span: Option<Span>,
114114
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
115-
let mut err = struct_span_err!(
116-
self,
115+
self.infcx.tcx.sess.create_err(crate::session_diagnostics::ClosureUniquelyBorrowErr {
117116
new_loan_span,
118-
E0500,
119-
"closure requires unique access to {} but {} is already borrowed{}",
117+
is_generator,
120118
desc_new,
119+
opt_via,
120+
old_loan_span,
121121
noun_old,
122122
old_opt_via,
123-
);
124-
err.span_label(
125-
new_loan_span,
126-
format!("{} construction occurs here{}", container_name, opt_via),
127-
);
128-
err.span_label(old_loan_span, format!("borrow occurs here{}", old_opt_via));
129-
if let Some(previous_end_span) = previous_end_span {
130-
err.span_label(previous_end_span, "borrow ends here");
131-
}
132-
err
123+
previous_end_span,
124+
})
133125
}
134126

135127
pub(crate) fn cannot_reborrow_already_uniquely_borrowed(
136128
&self,
137129
new_loan_span: Span,
138-
container_name: &str,
130+
is_generator: &str,
139131
desc_new: &str,
140132
opt_via: &str,
141133
kind_new: &str,
@@ -144,27 +136,17 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
144136
previous_end_span: Option<Span>,
145137
second_borrow_desc: &str,
146138
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
147-
let mut err = struct_span_err!(
148-
self,
139+
self.infcx.tcx.sess.create_err(crate::session_diagnostics::ClosureReBorrowErr {
149140
new_loan_span,
150-
E0501,
151-
"cannot borrow {}{} as {} because previous closure requires unique access",
141+
is_generator,
152142
desc_new,
153143
opt_via,
154144
kind_new,
155-
);
156-
err.span_label(
157-
new_loan_span,
158-
format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
159-
);
160-
err.span_label(
161145
old_loan_span,
162-
format!("{} construction occurs here{}", container_name, old_opt_via),
163-
);
164-
if let Some(previous_end_span) = previous_end_span {
165-
err.span_label(previous_end_span, "borrow from closure ends here");
166-
}
167-
err
146+
old_opt_via,
147+
previous_end_span,
148+
second_borrow_desc,
149+
})
168150
}
169151

170152
pub(crate) fn cannot_reborrow_already_borrowed(

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -923,11 +923,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
923923
let borrow_spans = self.borrow_spans(span, location);
924924
let span = borrow_spans.args_or_use();
925925

926+
//FIXME: depercate when done diag migration
926927
let container_name = if issued_spans.for_generator() || borrow_spans.for_generator() {
927928
"generator"
928929
} else {
929930
"closure"
930931
};
932+
let is_generator = issued_spans.for_generator() || borrow_spans.for_generator();
931933

932934
let (desc_place, msg_place, msg_borrow, union_type_name) =
933935
self.describe_place_for_conflicting_borrow(place, issued_borrow.borrowed_place);
@@ -1042,7 +1044,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10421044
first_borrow_desc = "first ";
10431045
self.cannot_uniquely_borrow_by_one_closure(
10441046
span,
1045-
container_name,
1047+
is_generator,
10461048
&desc_place,
10471049
"",
10481050
issued_span,

compiler/rustc_borrowck/src/session_diagnostics.rs

+35
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,38 @@ pub(crate) struct MutMultiLoopLabel<'a> {
749749
#[primary_span]
750750
pub new_loan_span: Span,
751751
}
752+
753+
#[derive(Diagnostic)]
754+
#[diag(borrowck_cannot_uniquely_borrow_by_one_closure, code = "E0500")]
755+
pub(crate) struct ClosureUniquelyBorrowErr<'a> {
756+
#[primary_span]
757+
#[label]
758+
pub new_loan_span: Span,
759+
pub is_generator: bool,
760+
pub desc_new: &'a str,
761+
pub opt_via: &'a str,
762+
#[label(occurs_label)]
763+
pub old_loan_span: Span,
764+
pub noun_old: &'a str,
765+
pub old_opt_via: &'a str,
766+
#[label(ends_label)]
767+
pub previous_end_span: Option<Span>,
768+
}
769+
770+
#[derive(Diagnostic)]
771+
#[diag(borrowck_cannot_reborrow_already_uniquely_borrowed, code = "E0501")]
772+
pub(crate) struct ClosureReBorrowErr<'a> {
773+
#[primary_span]
774+
#[label]
775+
pub new_loan_span: Span,
776+
pub is_generator: &'a str,
777+
pub desc_new: &'a str,
778+
pub opt_via: &'a str,
779+
pub kind_new: &'a str,
780+
#[label(occurs_label)]
781+
pub old_loan_span: Span,
782+
pub old_opt_via: &'a str,
783+
#[label(ends_label)]
784+
pub previous_end_span: Option<Span>,
785+
pub second_borrow_desc: &'a str,
786+
}

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

+18
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,21 @@ borrowck_cannot_mutably_borrow_multiply =
462462
[false] {""} (via {$place})
463463
}
464464
.first_mut_end_label = first borrow ends here
465+
466+
borrowck_cannot_uniquely_borrow_by_one_closure =
467+
closure requires unique access to {$desc_new} but {$noun_old} is already borrowed{$old_opt_via}
468+
.label = {$is_generator ->
469+
[true] generator
470+
*[false] closure
471+
} construction occurs here{$opt_via}
472+
.occurs_label = borrow occurs here{$old_opt_via}
473+
.ends_label = borrow ends here
474+
475+
borrowck_cannot_reborrow_already_uniquely_borrowed =
476+
cannot borrow {$desc_new}{$opt_via} as {$kind_new} because previous closure requires unique access
477+
.label = {$second_borrow_desc}borrow occurs here{$opt_via}
478+
.occurs_label = {$is_generator ->
479+
[true] generator
480+
*[false] closure
481+
} construction occurs here{$old_opt_via}
482+
.ends_label = borrow from closure ends here

0 commit comments

Comments
 (0)