1 use rustc_errors
::{struct_span_err, DiagnosticBuilder, DiagnosticId, ErrorGuaranteed}
;
2 use rustc_middle
::ty
::{self, Ty, TyCtxt}
;
3 use rustc_span
::{MultiSpan, Span}
;
5 impl<'cx
, 'tcx
> crate::MirBorrowckCtxt
<'cx
, 'tcx
> {
6 crate fn cannot_move_when_borrowed(
10 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
11 struct_span_err
!(self, span
, E0505
, "cannot move out of {} because it is borrowed", desc
,)
14 crate fn cannot_use_when_mutably_borrowed(
20 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
21 let mut err
= struct_span_err
!(
25 "cannot use {} because it was mutably borrowed",
29 err
.span_label(borrow_span
, format
!("borrow of {} occurs here", borrow_desc
));
30 err
.span_label(span
, format
!("use of borrowed {}", borrow_desc
));
34 crate fn cannot_act_on_uninitialized_variable(
39 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
44 "{} of possibly-uninitialized variable: `{}`",
50 crate fn cannot_mutably_borrow_multiply(
57 old_load_end_span
: Option
<Span
>,
58 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
60 |msg
: &str| if msg
.is_empty() { "".to_string() }
else { format!(" (via {}
)", msg) };
61 let mut err = struct_span_err!(
65 "cannot borrow {}{}
as mutable more than once at a time
",
69 if old_loan_span == new_loan_span {
70 // Both borrows are happening in the same place
71 // Meaning the borrow is occurring in a loop
75 "{}{} was mutably borrowed here
in the previous iteration of the
loop{}
",
81 if let Some(old_load_end_span) = old_load_end_span {
82 err.span_label(old_load_end_span, "mutable borrow ends here
");
87 format!("first mutable borrow occurs here{}
", via(old_opt_via)),
91 format!("second mutable borrow occurs here{}
", via(opt_via)),
93 if let Some(old_load_end_span) = old_load_end_span {
94 err.span_label(old_load_end_span, "first borrow ends here
");
100 crate fn cannot_uniquely_borrow_by_two_closures(
105 old_load_end_span: Option<Span>,
106 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
107 let mut err = struct_span_err!(
111 "two closures require unique access to {} at the same time
",
114 if old_loan_span == new_loan_span {
117 "closures are constructed here
in different iterations of
loop",
120 err.span_label(old_loan_span, "first closure is constructed here
");
121 err.span_label(new_loan_span, "second closure is constructed here
");
123 if let Some(old_load_end_span) = old_load_end_span {
124 err.span_label(old_load_end_span, "borrow from first closure ends here
");
129 crate fn cannot_uniquely_borrow_by_one_closure(
132 container_name: &str,
138 previous_end_span: Option<Span>,
139 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
140 let mut err = struct_span_err!(
144 "closure requires unique access to {} but {} is already borrowed{}
",
151 format!("{} construction occurs here{}
", container_name, opt_via),
153 err.span_label(old_loan_span, format!("borrow occurs here{}
", old_opt_via));
154 if let Some(previous_end_span) = previous_end_span {
155 err.span_label(previous_end_span, "borrow ends here
");
160 crate fn cannot_reborrow_already_uniquely_borrowed(
163 container_name: &str,
169 previous_end_span: Option<Span>,
170 second_borrow_desc: &str,
171 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
172 let mut err = struct_span_err!(
176 "cannot borrow {}{}
as {} because previous closure
\
177 requires unique access
",
184 format!("{}borrow occurs here{}
", second_borrow_desc, opt_via),
188 format!("{} construction occurs here{}
", container_name, old_opt_via),
190 if let Some(previous_end_span) = previous_end_span {
191 err.span_label(previous_end_span, "borrow from closure ends here
");
196 crate fn cannot_reborrow_already_borrowed(
206 old_load_end_span: Option<Span>,
207 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
209 |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg
) };
210 let mut err
= struct_span_err
!(
214 "cannot borrow {}{} as {} because {} is also borrowed as {}{}",
224 // If `msg_new` is empty, then this isn't a borrow of a union field.
225 err
.span_label(span
, format
!("{} borrow occurs here", kind_new
));
226 err
.span_label(old_span
, format
!("{} borrow occurs here", kind_old
));
228 // If `msg_new` isn't empty, then this a borrow of a union field.
232 "{} borrow of {} -- which overlaps with {} -- occurs here",
233 kind_new
, msg_new
, msg_old
,
236 err
.span_label(old_span
, format
!("{} borrow occurs here{}", kind_old
, via(msg_old
)));
239 if let Some(old_load_end_span
) = old_load_end_span
{
240 err
.span_label(old_load_end_span
, format
!("{} borrow ends here", kind_old
));
245 crate fn cannot_assign_to_borrowed(
250 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
251 let mut err
= struct_span_err
!(
255 "cannot assign to {} because it is borrowed",
259 err
.span_label(borrow_span
, format
!("borrow of {} occurs here", desc
));
260 err
.span_label(span
, format
!("assignment to borrowed {} occurs here", desc
));
264 crate fn cannot_reassign_immutable(
269 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
270 let msg
= if is_arg { "to immutable argument" }
else { "twice to immutable variable" }
;
271 struct_span_err
!(self, span
, E0384
, "cannot assign {} {}", msg
, desc
)
274 crate fn cannot_assign(
278 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
279 struct_span_err
!(self, span
, E0594
, "cannot assign to {}", desc
)
282 crate fn cannot_move_out_of(
284 move_from_span
: Span
,
285 move_from_desc
: &str,
286 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
287 struct_span_err
!(self, move_from_span
, E0507
, "cannot move out of {}", move_from_desc
,)
290 /// Signal an error due to an attempt to move out of the interior
291 /// of an array or slice. `is_index` is None when error origin
292 /// didn't capture whether there was an indexing operation or not.
293 crate fn cannot_move_out_of_interior_noncopy(
295 move_from_span
: Span
,
297 is_index
: Option
<bool
>,
298 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
299 let type_name
= match (&ty
.kind(), is_index
) {
300 (&ty
::Array(_
, _
), Some(true)) | (&ty
::Array(_
, _
), None
) => "array",
301 (&ty
::Slice(_
), _
) => "slice",
302 _
=> span_bug
!(move_from_span
, "this path should not cause illegal move"),
304 let mut err
= struct_span_err
!(
308 "cannot move out of type `{}`, a non-copy {}",
312 err
.span_label(move_from_span
, "cannot move out of here");
316 crate fn cannot_move_out_of_interior_of_drop(
318 move_from_span
: Span
,
319 container_ty
: Ty
<'_
>,
320 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
321 let mut err
= struct_span_err
!(
325 "cannot move out of type `{}`, which implements the `Drop` trait",
328 err
.span_label(move_from_span
, "cannot move out of here");
332 crate fn cannot_act_on_moved_value(
336 optional_adverb_for_moved
: &str,
337 moved_path
: Option
<String
>,
338 ) -> DiagnosticBuilder
<'tcx
, ErrorGuaranteed
> {
339 let moved_path
= moved_path
.map(|mp
| format
!(": `{}`", mp
)).unwrap_or_default();
345 "{} of {}moved value{}",
347 optional_adverb_for_moved
,
352 crate fn cannot_borrow_path_as_mutable_because(
357 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
358 struct_span_err
!(self, span
, E0596
, "cannot borrow {} as mutable{}", path
, reason
,)
361 crate fn cannot_mutate_in_immutable_section(
364 immutable_span
: Span
,
365 immutable_place
: &str,
366 immutable_section
: &str,
368 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
369 let mut err
= struct_span_err
!(
373 "cannot {} {} in {}",
378 err
.span_label(mutate_span
, format
!("cannot {}", action
));
379 err
.span_label(immutable_span
, format
!("value is immutable in {}", immutable_section
));
383 crate fn cannot_borrow_across_generator_yield(
387 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
388 let mut err
= struct_span_err
!(
392 "borrow may still be in use when generator yields",
394 err
.span_label(yield_span
, "possible yield occurs here");
398 crate fn cannot_borrow_across_destructor(
401 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
406 "borrow may still be in use when destructor runs",
410 crate fn path_does_not_live_long_enough(
414 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
415 struct_span_err
!(self, span
, E0597
, "{} does not live long enough", path
,)
418 crate fn cannot_return_reference_to_local(
422 reference_desc
: &str,
424 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
425 let mut err
= struct_span_err
!(
429 "cannot {RETURN} {REFERENCE} {LOCAL}",
430 RETURN
= return_kind
,
431 REFERENCE
= reference_desc
,
437 format
!("{}s a {} data owned by the current function", return_kind
, reference_desc
),
443 crate fn cannot_capture_in_long_lived_closure(
449 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
450 let mut err
= struct_span_err
!(
454 "{} may outlive the current function, \
456 which is owned by the current function",
460 err
.span_label(capture_span
, format
!("{} is borrowed here", borrowed_path
))
461 .span_label(closure_span
, format
!("may outlive borrowed value {}", borrowed_path
));
465 crate fn thread_local_value_does_not_live_long_enough(
468 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
469 struct_span_err
!(self, span
, E0712
, "thread-local variable borrowed past end of function",)
472 crate fn temporary_value_borrowed_for_too_long(
475 ) -> DiagnosticBuilder
<'cx
, ErrorGuaranteed
> {
476 struct_span_err
!(self, span
, E0716
, "temporary value dropped while borrowed",)
479 fn struct_span_err_with_code
<S
: Into
<MultiSpan
>>(
484 ) -> DiagnosticBuilder
<'tcx
, ErrorGuaranteed
> {
485 self.infcx
.tcx
.sess
.struct_span_err_with_code(sp
, msg
, code
)
489 crate fn borrowed_data_escapes_closure
<'tcx
>(
493 ) -> DiagnosticBuilder
<'tcx
, ErrorGuaranteed
> {
498 "borrowed data escapes outside of {}",