Skip to content

Commit 19594e9

Browse files
committed
borrowck_errors.rs finish most
1 parent a39a982 commit 19594e9

File tree

3 files changed

+178
-79
lines changed

3 files changed

+178
-79
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+39-79
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ use rustc_span::Span;
99

1010
use crate::session_diagnostics::{
1111
ActMovedValueErr, AssignBorrowErr, AssignErr, BorrowAcrossDestructor,
12-
BorrowAcrossGeneratorYield, ClosureVarOutliveErr, ImmuteArgAssign, ImmuteVarReassign,
13-
InteriorDropMoveErr, MovedOutErr, MutateInImmute, PathShortLive, ReturnRefLocalErr,
14-
TemporaryDroppedErr, ThreadLocalOutliveErr,
12+
BorrowAcrossGeneratorYield, BorrowEscapeClosure, ClosureConstructLabel,
13+
ClosureUniquelyBorrowErr, ClosureVarOutliveErr, ImmuteArgAssign, ImmuteVarReassign,
14+
InteriorDropMoveErr, InteriorNoncopyMoveErr, MoveBorrowedErr, MovedOutErr, MutateInImmute,
15+
PathShortLive, ReturnRefLocalErr, TemporaryDroppedErr, ThreadLocalOutliveErr,
16+
TwoClosuresUniquelyBorrowErr, UniquelyBorrowReborrowErr, UseMutBorrowErr,
1517
};
1618

1719
impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
@@ -20,7 +22,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
2022
span: Span,
2123
desc: &str,
2224
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
23-
struct_span_err!(self, span, E0505, "cannot move out of {} because it is borrowed", desc,)
25+
self.infcx.tcx.sess.create_err(MoveBorrowedErr { desc, span })
2426
}
2527

2628
pub(crate) fn cannot_use_when_mutably_borrowed(
@@ -30,17 +32,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
3032
borrow_span: Span,
3133
borrow_desc: &str,
3234
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
33-
let mut err = struct_span_err!(
34-
self,
35-
span,
36-
E0503,
37-
"cannot use {} because it was mutably borrowed",
38-
desc,
39-
);
40-
41-
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
42-
err.span_label(span, format!("use of borrowed {}", borrow_desc));
43-
err
35+
self.infcx.tcx.sess.create_err(UseMutBorrowErr { desc, borrow_desc, span, borrow_span })
4436
}
4537

4638
pub(crate) fn cannot_mutably_borrow_multiply(
@@ -52,6 +44,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
5244
old_opt_via: &str,
5345
old_load_end_span: Option<Span>,
5446
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
47+
//FIXME: migrate later
5548
let via =
5649
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
5750
let mut err = struct_span_err!(
@@ -100,26 +93,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
10093
old_loan_span: Span,
10194
old_load_end_span: Option<Span>,
10295
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
103-
let mut err = struct_span_err!(
104-
self,
105-
new_loan_span,
106-
E0524,
107-
"two closures require unique access to {} at the same time",
108-
desc,
109-
);
96+
let case: ClosureConstructLabel;
97+
let diff_span: Option<Span>;
11098
if old_loan_span == new_loan_span {
111-
err.span_label(
112-
old_loan_span,
113-
"closures are constructed here in different iterations of loop",
114-
);
99+
case = ClosureConstructLabel::Both { old_loan_span };
100+
diff_span = None;
115101
} else {
116-
err.span_label(old_loan_span, "first closure is constructed here");
117-
err.span_label(new_loan_span, "second closure is constructed here");
102+
case = ClosureConstructLabel::First { old_loan_span };
103+
diff_span = Some(new_loan_span);
118104
}
119-
if let Some(old_load_end_span) = old_load_end_span {
120-
err.span_label(old_load_end_span, "borrow from first closure ends here");
121-
}
122-
err
105+
self.infcx.tcx.sess.create_err(TwoClosuresUniquelyBorrowErr {
106+
desc,
107+
case,
108+
new_loan_span,
109+
old_load_end_span,
110+
diff_span,
111+
})
123112
}
124113

125114
pub(crate) fn cannot_uniquely_borrow_by_one_closure(
@@ -133,24 +122,16 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
133122
old_opt_via: &str,
134123
previous_end_span: Option<Span>,
135124
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
136-
let mut err = struct_span_err!(
137-
self,
138-
new_loan_span,
139-
E0500,
140-
"closure requires unique access to {} but {} is already borrowed{}",
125+
self.infcx.tcx.sess.create_err(ClosureUniquelyBorrowErr {
126+
container_name,
141127
desc_new,
128+
opt_via,
142129
noun_old,
143130
old_opt_via,
144-
);
145-
err.span_label(
146131
new_loan_span,
147-
format!("{} construction occurs here{}", container_name, opt_via),
148-
);
149-
err.span_label(old_loan_span, format!("borrow occurs here{}", old_opt_via));
150-
if let Some(previous_end_span) = previous_end_span {
151-
err.span_label(previous_end_span, "borrow ends here");
152-
}
153-
err
132+
old_loan_span,
133+
previous_end_span,
134+
})
154135
}
155136

156137
pub(crate) fn cannot_reborrow_already_uniquely_borrowed(
@@ -165,27 +146,17 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
165146
previous_end_span: Option<Span>,
166147
second_borrow_desc: &str,
167148
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
168-
let mut err = struct_span_err!(
169-
self,
170-
new_loan_span,
171-
E0501,
172-
"cannot borrow {}{} as {} because previous closure requires unique access",
149+
self.infcx.tcx.sess.create_err(UniquelyBorrowReborrowErr {
150+
container_name,
173151
desc_new,
174152
opt_via,
175153
kind_new,
176-
);
177-
err.span_label(
154+
old_opt_via,
155+
second_borrow_desc,
178156
new_loan_span,
179-
format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
180-
);
181-
err.span_label(
182157
old_loan_span,
183-
format!("{} construction occurs here{}", container_name, old_opt_via),
184-
);
185-
if let Some(previous_end_span) = previous_end_span {
186-
err.span_label(previous_end_span, "borrow from closure ends here");
187-
}
188-
err
158+
previous_end_span,
159+
})
189160
}
190161

191162
pub(crate) fn cannot_reborrow_already_borrowed(
@@ -200,6 +171,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
200171
msg_old: &str,
201172
old_load_end_span: Option<Span>,
202173
) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
174+
//FIXME: would it be better for manual impl for this case?
203175
let via =
204176
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
205177
let mut err = struct_span_err!(
@@ -289,16 +261,10 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
289261
(&ty::Slice(_), _) => "slice",
290262
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
291263
};
292-
let mut err = struct_span_err!(
293-
self,
294-
move_from_span,
295-
E0508,
296-
"cannot move out of type `{}`, a non-copy {}",
297-
ty,
298-
type_name,
299-
);
300-
err.span_label(move_from_span, "cannot move out of here");
301-
err
264+
//FIXME: failed ui test diag-migration
265+
// - error[E0508]: cannot move out of type `[S; 1]`, a non-copy array
266+
// + error[E0508]: cannot move out of type `S`, a non-copy array
267+
self.infcx.tcx.sess.create_err(InteriorNoncopyMoveErr { ty, type_name, move_from_span })
302268
}
303269

304270
pub(crate) fn cannot_move_out_of_interior_of_drop(
@@ -435,11 +401,5 @@ pub(crate) fn borrowed_data_escapes_closure<'tcx>(
435401
escape_span: Span,
436402
escapes_from: &str,
437403
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
438-
struct_span_err!(
439-
tcx.sess,
440-
escape_span,
441-
E0521,
442-
"borrowed data escapes outside of {}",
443-
escapes_from,
444-
)
404+
tcx.sess.create_err(BorrowEscapeClosure { escapes_from, escape_span })
445405
}

compiler/rustc_borrowck/src/session_diagnostics.rs

+101
Original file line numberDiff line numberDiff line change
@@ -734,3 +734,104 @@ pub(crate) struct AssignBorrowErr<'a> {
734734
#[label(borrowck::borrow_here_label)]
735735
pub borrow_span: Span,
736736
}
737+
738+
#[derive(SessionDiagnostic)]
739+
#[diag(borrowck::cannot_move_out_of_interior_noncopy, code = "E0508")]
740+
pub(crate) struct InteriorNoncopyMoveErr<'a, 'b> {
741+
pub ty: Ty<'a>,
742+
pub type_name: &'b str,
743+
#[primary_span]
744+
#[label]
745+
pub move_from_span: Span,
746+
}
747+
748+
#[derive(SessionDiagnostic)]
749+
#[diag(borrowck::cannot_reborrow_already_uniquely_borrowed, code = "E0501")]
750+
pub(crate) struct UniquelyBorrowReborrowErr<'a> {
751+
pub container_name: &'a str,
752+
pub desc_new: &'a str,
753+
pub opt_via: &'a str,
754+
pub kind_new: &'a str,
755+
pub old_opt_via: &'a str,
756+
pub second_borrow_desc: &'a str,
757+
#[primary_span]
758+
#[label]
759+
pub new_loan_span: Span,
760+
#[label(borrowck::old_span_label)]
761+
pub old_loan_span: Span,
762+
#[label(borrowck::optional_label)]
763+
pub previous_end_span: Option<Span>,
764+
}
765+
766+
#[derive(SessionDiagnostic)]
767+
#[diag(borrowck::cannot_uniquely_borrow_by_one_closure, code = "E0501")]
768+
pub(crate) struct ClosureUniquelyBorrowErr<'a> {
769+
pub container_name: &'a str,
770+
pub desc_new: &'a str,
771+
pub opt_via: &'a str,
772+
pub noun_old: &'a str,
773+
pub old_opt_via: &'a str,
774+
#[primary_span]
775+
#[label]
776+
pub new_loan_span: Span,
777+
#[label(borrowck::old_span_label)]
778+
pub old_loan_span: Span,
779+
#[label(borrowck::optional_label)]
780+
pub previous_end_span: Option<Span>,
781+
}
782+
783+
#[derive(SessionDiagnostic)]
784+
#[diag(borrowck::borrowed_data_escapes_closure, code = "E0521")]
785+
pub(crate) struct BorrowEscapeClosure<'a> {
786+
pub escapes_from: &'a str,
787+
#[primary_span]
788+
pub escape_span: Span,
789+
}
790+
791+
#[derive(SessionDiagnostic)]
792+
#[diag(borrowck::cannot_uniquely_borrow_by_two_closures, code = "E0524")]
793+
pub(crate) struct TwoClosuresUniquelyBorrowErr<'a> {
794+
pub desc: &'a str,
795+
#[subdiagnostic]
796+
pub case: ClosureConstructLabel,
797+
#[primary_span]
798+
pub new_loan_span: Span,
799+
#[label]
800+
pub old_load_end_span: Option<Span>,
801+
#[label(borrowck::new_span_label)]
802+
pub diff_span: Option<Span>,
803+
}
804+
805+
#[derive(SessionSubdiagnostic)]
806+
pub(crate) enum ClosureConstructLabel {
807+
#[label(borrowck::first_closure_constructed_here)]
808+
First {
809+
#[primary_span]
810+
old_loan_span: Span,
811+
},
812+
#[label(borrowck::closures_constructed_here)]
813+
Both {
814+
#[primary_span]
815+
old_loan_span: Span,
816+
},
817+
}
818+
819+
#[derive(SessionDiagnostic)]
820+
#[diag(borrowck::cannot_use_when_mutably_borrowed, code = "E0503")]
821+
pub(crate) struct UseMutBorrowErr<'a> {
822+
pub desc: &'a str,
823+
pub borrow_desc: &'a str,
824+
#[primary_span]
825+
#[label]
826+
pub span: Span,
827+
#[label(borrowck::borrow_span_label)]
828+
pub borrow_span: Span,
829+
}
830+
831+
#[derive(SessionDiagnostic)]
832+
#[diag(borrowck::cannot_move_when_borrowed, code = "E0505")]
833+
pub(crate) struct MoveBorrowedErr<'a> {
834+
pub desc: &'a str,
835+
#[primary_span]
836+
pub span: Span,
837+
}

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

+38
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,41 @@ borrowck_cannot_assign_to_borrowed =
305305
cannot assign to {$desc} because it is borrowed
306306
.label = assignment to borrowed {$desc} occurs here
307307
.borrow_here_label = borrow of {$desc} occurs here
308+
309+
borrowck_cannot_move_out_of_interior_noncopy =
310+
cannot move out of type `{$ty}`, a non-copy {$type_name}
311+
.label = cannot move out of here
312+
313+
borrowck_cannot_reborrow_already_uniquely_borrowed =
314+
cannot borrow {$desc_new}{$opt_via} as {$kind_new} because previous closure requires unique access
315+
.label = {$second_borrow_desc}borrow occurs here{$opt_via}
316+
.old_span_label = {$container_name} construction occurs here{$old_opt_via}
317+
.optional_label = borrow from closure ends here
318+
319+
borrowck_cannot_uniquely_borrow_by_one_closure =
320+
closure requires unique access to {$desc_new} but {$noun_old} is already borrowed{$old_opt_via}
321+
.label = {$container_name} construction occurs here{$opt_via}
322+
.old_span_label = borrow occurs here{$old_opt_via}
323+
.optional_label = borrow ends here
324+
325+
borrowck_borrowed_data_escapes_closure =
326+
borrowed data escapes outside of {$escapes_from}
327+
328+
borrowck_cannot_uniquely_borrow_by_two_closures =
329+
two closures require unique access to {$desc} at the same time
330+
.label = borrow from first closure ends here
331+
.new_span_label = second closure is constructed here
332+
333+
borrowck_first_closure_constructed_here =
334+
first closure is constructed here
335+
336+
borrowck_closures_constructed_here =
337+
closures are constructed here in different iterations of loop
338+
339+
borrowck_cannot_use_when_mutably_borrowed =
340+
cannot use {$desc} because it was mutably borrowed
341+
.label = use of borrowed {$borrow_desc}
342+
.borrow_span_label = borrow of {$borrow_desc} occurs here
343+
344+
borrowck_cannot_move_when_borrowed =
345+
cannot move out of {$desc} because it is borrowed

0 commit comments

Comments
 (0)