1 use rustc_errors::{struct_span_err, DiagnosticBuilder, DiagnosticId};
2 use rustc_middle::ty::{self, Ty, TyCtxt};
3 use rustc_span::{MultiSpan, Span};
5 impl<'cx, 'tcx> crate::borrow_check::MirBorrowckCtxt<'cx, 'tcx> {
6 crate fn cannot_move_when_borrowed(&self, span: Span, desc: &str) -> DiagnosticBuilder<'cx> {
7 struct_span_err!(self, span, E0505, "cannot move out of {} because it is borrowed", desc,)
10 crate fn cannot_use_when_mutably_borrowed(
16 ) -> DiagnosticBuilder<'cx> {
17 let mut err = struct_span_err!(
21 "cannot use {} because it was mutably borrowed",
25 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
26 err.span_label(span, format!("use of borrowed {}", borrow_desc));
30 crate fn cannot_act_on_uninitialized_variable(
35 ) -> DiagnosticBuilder<'cx> {
40 "{} of possibly-uninitialized variable: `{}`",
46 crate fn cannot_mutably_borrow_multiply(
53 old_load_end_span: Option<Span>,
54 ) -> DiagnosticBuilder<'cx> {
56 |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
57 let mut err = struct_span_err!(
61 "cannot borrow {}{} as mutable more than once at a time",
65 if old_loan_span == new_loan_span {
66 // Both borrows are happening in the same place
67 // Meaning the borrow is occurring in a loop
71 "mutable borrow starts here in previous \
76 if let Some(old_load_end_span) = old_load_end_span {
77 err.span_label(old_load_end_span, "mutable borrow ends here");
82 format!("first mutable borrow occurs here{}", via(old_opt_via)),
86 format!("second mutable borrow occurs here{}", via(opt_via)),
88 if let Some(old_load_end_span) = old_load_end_span {
89 err.span_label(old_load_end_span, "first borrow ends here");
95 crate fn cannot_uniquely_borrow_by_two_closures(
100 old_load_end_span: Option<Span>,
101 ) -> DiagnosticBuilder<'cx> {
102 let mut err = struct_span_err!(
106 "two closures require unique access to {} at the same time",
109 if old_loan_span == new_loan_span {
112 "closures are constructed here in different iterations of loop",
115 err.span_label(old_loan_span, "first closure is constructed here");
116 err.span_label(new_loan_span, "second closure is constructed here");
118 if let Some(old_load_end_span) = old_load_end_span {
119 err.span_label(old_load_end_span, "borrow from first closure ends here");
124 crate fn cannot_uniquely_borrow_by_one_closure(
127 container_name: &str,
133 previous_end_span: Option<Span>,
134 ) -> DiagnosticBuilder<'cx> {
135 let mut err = struct_span_err!(
139 "closure requires unique access to {} but {} is already borrowed{}",
146 format!("{} construction occurs here{}", container_name, opt_via),
148 err.span_label(old_loan_span, format!("borrow occurs here{}", old_opt_via));
149 if let Some(previous_end_span) = previous_end_span {
150 err.span_label(previous_end_span, "borrow ends here");
155 crate fn cannot_reborrow_already_uniquely_borrowed(
158 container_name: &str,
164 previous_end_span: Option<Span>,
165 second_borrow_desc: &str,
166 ) -> DiagnosticBuilder<'cx> {
167 let mut err = struct_span_err!(
171 "cannot borrow {}{} as {} because previous closure \
172 requires unique access",
179 format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
183 format!("{} construction occurs here{}", container_name, old_opt_via),
185 if let Some(previous_end_span) = previous_end_span {
186 err.span_label(previous_end_span, "borrow from closure ends here");
191 crate fn cannot_reborrow_already_borrowed(
201 old_load_end_span: Option<Span>,
202 ) -> DiagnosticBuilder<'cx> {
204 |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
205 let mut err = struct_span_err!(
209 "cannot borrow {}{} as {} because {} is also borrowed as {}{}",
219 // If `msg_new` is empty, then this isn't a borrow of a union field.
220 err.span_label(span, format!("{} borrow occurs here", kind_new));
221 err.span_label(old_span, format!("{} borrow occurs here", kind_old));
223 // If `msg_new` isn't empty, then this a borrow of a union field.
227 "{} borrow of {} -- which overlaps with {} -- occurs here",
228 kind_new, msg_new, msg_old,
231 err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, via(msg_old)));
234 if let Some(old_load_end_span) = old_load_end_span {
235 err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
240 crate fn cannot_assign_to_borrowed(
245 ) -> DiagnosticBuilder<'cx> {
246 let mut err = struct_span_err!(
250 "cannot assign to {} because it is borrowed",
254 err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
255 err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
259 crate fn cannot_reassign_immutable(
264 ) -> DiagnosticBuilder<'cx> {
265 let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
266 struct_span_err!(self, span, E0384, "cannot assign {} {}", msg, desc)
269 crate fn cannot_assign(&self, span: Span, desc: &str) -> DiagnosticBuilder<'cx> {
270 struct_span_err!(self, span, E0594, "cannot assign to {}", desc)
273 crate fn cannot_move_out_of(
275 move_from_span: Span,
276 move_from_desc: &str,
277 ) -> DiagnosticBuilder<'cx> {
278 struct_span_err!(self, move_from_span, E0507, "cannot move out of {}", move_from_desc,)
281 /// Signal an error due to an attempt to move out of the interior
282 /// of an array or slice. `is_index` is None when error origin
283 /// didn't capture whether there was an indexing operation or not.
284 crate fn cannot_move_out_of_interior_noncopy(
286 move_from_span: Span,
288 is_index: Option<bool>,
289 ) -> DiagnosticBuilder<'cx> {
290 let type_name = match (&ty.kind(), is_index) {
291 (&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
292 (&ty::Slice(_), _) => "slice",
293 _ => span_bug!(move_from_span, "this path should not cause illegal move"),
295 let mut err = struct_span_err!(
299 "cannot move out of type `{}`, a non-copy {}",
303 err.span_label(move_from_span, "cannot move out of here");
307 crate fn cannot_move_out_of_interior_of_drop(
309 move_from_span: Span,
310 container_ty: Ty<'_>,
311 ) -> DiagnosticBuilder<'cx> {
312 let mut err = struct_span_err!(
316 "cannot move out of type `{}`, which implements the `Drop` trait",
319 err.span_label(move_from_span, "cannot move out of here");
323 crate fn cannot_act_on_moved_value(
327 optional_adverb_for_moved: &str,
328 moved_path: Option<String>,
329 ) -> DiagnosticBuilder<'cx> {
330 let moved_path = moved_path.map(|mp| format!(": `{}`", mp)).unwrap_or_default();
336 "{} of {}moved value{}",
338 optional_adverb_for_moved,
343 crate fn cannot_borrow_path_as_mutable_because(
348 ) -> DiagnosticBuilder<'cx> {
349 struct_span_err!(self, span, E0596, "cannot borrow {} as mutable{}", path, reason,)
352 crate fn cannot_mutate_in_immutable_section(
355 immutable_span: Span,
356 immutable_place: &str,
357 immutable_section: &str,
359 ) -> DiagnosticBuilder<'cx> {
360 let mut err = struct_span_err!(
364 "cannot {} {} in {}",
369 err.span_label(mutate_span, format!("cannot {}", action));
370 err.span_label(immutable_span, format!("value is immutable in {}", immutable_section));
374 crate fn cannot_borrow_across_generator_yield(
378 ) -> DiagnosticBuilder<'cx> {
379 let mut err = struct_span_err!(
383 "borrow may still be in use when generator yields",
385 err.span_label(yield_span, "possible yield occurs here");
389 crate fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> DiagnosticBuilder<'cx> {
394 "borrow may still be in use when destructor runs",
398 crate fn path_does_not_live_long_enough(
402 ) -> DiagnosticBuilder<'cx> {
403 struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
406 crate fn cannot_return_reference_to_local(
410 reference_desc: &str,
412 ) -> DiagnosticBuilder<'cx> {
413 let mut err = struct_span_err!(
417 "cannot {RETURN} {REFERENCE} {LOCAL}",
418 RETURN = return_kind,
419 REFERENCE = reference_desc,
425 format!("{}s a {} data owned by the current function", return_kind, reference_desc),
431 crate fn cannot_capture_in_long_lived_closure(
437 ) -> DiagnosticBuilder<'cx> {
438 let mut err = struct_span_err!(
442 "{} may outlive the current function, \
444 which is owned by the current function",
448 err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
449 .span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
453 crate fn thread_local_value_does_not_live_long_enough(
456 ) -> DiagnosticBuilder<'cx> {
457 struct_span_err!(self, span, E0712, "thread-local variable borrowed past end of function",)
460 crate fn temporary_value_borrowed_for_too_long(&self, span: Span) -> DiagnosticBuilder<'cx> {
461 struct_span_err!(self, span, E0716, "temporary value dropped while borrowed",)
464 fn struct_span_err_with_code<S: Into<MultiSpan>>(
469 ) -> DiagnosticBuilder<'tcx> {
470 self.infcx.tcx.sess.struct_span_err_with_code(sp, msg, code)
474 crate fn borrowed_data_escapes_closure<'tcx>(
478 ) -> DiagnosticBuilder<'tcx> {
483 "borrowed data escapes outside of {}",