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::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 "{}{} was mutably borrowed here in the previous iteration of the loop{}",
77 if let Some(old_load_end_span) = old_load_end_span {
78 err.span_label(old_load_end_span, "mutable borrow ends here");
83 format!("first mutable borrow occurs here{}", via(old_opt_via)),
87 format!("second mutable borrow occurs here{}", via(opt_via)),
89 if let Some(old_load_end_span) = old_load_end_span {
90 err.span_label(old_load_end_span, "first borrow ends here");
96 crate fn cannot_uniquely_borrow_by_two_closures(
101 old_load_end_span: Option<Span>,
102 ) -> DiagnosticBuilder<'cx> {
103 let mut err = struct_span_err!(
107 "two closures require unique access to {} at the same time",
110 if old_loan_span == new_loan_span {
113 "closures are constructed here in different iterations of loop",
116 err.span_label(old_loan_span, "first closure is constructed here");
117 err.span_label(new_loan_span, "second closure is constructed here");
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");
125 crate fn cannot_uniquely_borrow_by_one_closure(
128 container_name: &str,
134 previous_end_span: Option<Span>,
135 ) -> DiagnosticBuilder<'cx> {
136 let mut err = struct_span_err!(
140 "closure requires unique access to {} but {} is already borrowed{}",
147 format!("{} construction occurs here{}", container_name, opt_via),
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");
156 crate fn cannot_reborrow_already_uniquely_borrowed(
159 container_name: &str,
165 previous_end_span: Option<Span>,
166 second_borrow_desc: &str,
167 ) -> DiagnosticBuilder<'cx> {
168 let mut err = struct_span_err!(
172 "cannot borrow {}{} as {} because previous closure \
173 requires unique access",
180 format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
184 format!("{} construction occurs here{}", container_name, old_opt_via),
186 if let Some(previous_end_span) = previous_end_span {
187 err.span_label(previous_end_span, "borrow from closure ends here");
192 crate fn cannot_reborrow_already_borrowed(
202 old_load_end_span: Option<Span>,
203 ) -> DiagnosticBuilder<'cx> {
205 |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
206 let mut err = struct_span_err!(
210 "cannot borrow {}{} as {} because {} is also borrowed as {}{}",
220 // If `msg_new` is empty, then this isn't a borrow of a union field.
221 err.span_label(span, format!("{} borrow occurs here", kind_new));
222 err.span_label(old_span, format!("{} borrow occurs here", kind_old));
224 // If `msg_new` isn't empty, then this a borrow of a union field.
228 "{} borrow of {} -- which overlaps with {} -- occurs here",
229 kind_new, msg_new, msg_old,
232 err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, via(msg_old)));
235 if let Some(old_load_end_span) = old_load_end_span {
236 err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
241 crate fn cannot_assign_to_borrowed(
246 ) -> DiagnosticBuilder<'cx> {
247 let mut err = struct_span_err!(
251 "cannot assign to {} because it is borrowed",
255 err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
256 err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
260 crate fn cannot_reassign_immutable(
265 ) -> DiagnosticBuilder<'cx> {
266 let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
267 struct_span_err!(self, span, E0384, "cannot assign {} {}", msg, desc)
270 crate fn cannot_assign(&self, span: Span, desc: &str) -> DiagnosticBuilder<'cx> {
271 struct_span_err!(self, span, E0594, "cannot assign to {}", desc)
274 crate fn cannot_move_out_of(
276 move_from_span: Span,
277 move_from_desc: &str,
278 ) -> DiagnosticBuilder<'cx> {
279 struct_span_err!(self, move_from_span, E0507, "cannot move out of {}", move_from_desc,)
282 /// Signal an error due to an attempt to move out of the interior
283 /// of an array or slice. `is_index` is None when error origin
284 /// didn't capture whether there was an indexing operation or not.
285 crate fn cannot_move_out_of_interior_noncopy(
287 move_from_span: Span,
289 is_index: Option<bool>,
290 ) -> DiagnosticBuilder<'cx> {
291 let type_name = match (&ty.kind(), is_index) {
292 (&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
293 (&ty::Slice(_), _) => "slice",
294 _ => span_bug!(move_from_span, "this path should not cause illegal move"),
296 let mut err = struct_span_err!(
300 "cannot move out of type `{}`, a non-copy {}",
304 err.span_label(move_from_span, "cannot move out of here");
308 crate fn cannot_move_out_of_interior_of_drop(
310 move_from_span: Span,
311 container_ty: Ty<'_>,
312 ) -> DiagnosticBuilder<'cx> {
313 let mut err = struct_span_err!(
317 "cannot move out of type `{}`, which implements the `Drop` trait",
320 err.span_label(move_from_span, "cannot move out of here");
324 crate fn cannot_act_on_moved_value(
328 optional_adverb_for_moved: &str,
329 moved_path: Option<String>,
330 ) -> DiagnosticBuilder<'tcx> {
331 let moved_path = moved_path.map(|mp| format!(": `{}`", mp)).unwrap_or_default();
337 "{} of {}moved value{}",
339 optional_adverb_for_moved,
344 crate fn cannot_borrow_path_as_mutable_because(
349 ) -> DiagnosticBuilder<'cx> {
350 struct_span_err!(self, span, E0596, "cannot borrow {} as mutable{}", path, reason,)
353 crate fn cannot_mutate_in_immutable_section(
356 immutable_span: Span,
357 immutable_place: &str,
358 immutable_section: &str,
360 ) -> DiagnosticBuilder<'cx> {
361 let mut err = struct_span_err!(
365 "cannot {} {} in {}",
370 err.span_label(mutate_span, format!("cannot {}", action));
371 err.span_label(immutable_span, format!("value is immutable in {}", immutable_section));
375 crate fn cannot_borrow_across_generator_yield(
379 ) -> DiagnosticBuilder<'cx> {
380 let mut err = struct_span_err!(
384 "borrow may still be in use when generator yields",
386 err.span_label(yield_span, "possible yield occurs here");
390 crate fn cannot_borrow_across_destructor(&self, borrow_span: Span) -> DiagnosticBuilder<'cx> {
395 "borrow may still be in use when destructor runs",
399 crate fn path_does_not_live_long_enough(
403 ) -> DiagnosticBuilder<'cx> {
404 struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
407 crate fn cannot_return_reference_to_local(
411 reference_desc: &str,
413 ) -> DiagnosticBuilder<'cx> {
414 let mut err = struct_span_err!(
418 "cannot {RETURN} {REFERENCE} {LOCAL}",
419 RETURN = return_kind,
420 REFERENCE = reference_desc,
426 format!("{}s a {} data owned by the current function", return_kind, reference_desc),
432 crate fn cannot_capture_in_long_lived_closure(
438 ) -> DiagnosticBuilder<'cx> {
439 let mut err = struct_span_err!(
443 "{} may outlive the current function, \
445 which is owned by the current function",
449 err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
450 .span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
454 crate fn thread_local_value_does_not_live_long_enough(
457 ) -> DiagnosticBuilder<'cx> {
458 struct_span_err!(self, span, E0712, "thread-local variable borrowed past end of function",)
461 crate fn temporary_value_borrowed_for_too_long(&self, span: Span) -> DiagnosticBuilder<'cx> {
462 struct_span_err!(self, span, E0716, "temporary value dropped while borrowed",)
465 fn struct_span_err_with_code<S: Into<MultiSpan>>(
470 ) -> DiagnosticBuilder<'tcx> {
471 self.infcx.tcx.sess.struct_span_err_with_code(sp, msg, code)
475 crate fn borrowed_data_escapes_closure<'tcx>(
479 ) -> DiagnosticBuilder<'tcx> {
484 "borrowed data escapes outside of {}",