1 use rustc::ty::{self, Ty, TyCtxt};
2 use rustc_errors::{struct_span_err, DiagnosticBuilder, DiagnosticId};
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() { msg.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() { msg.to_string() } else { format!(" (via `{}`)", msg) };
205 let mut err = struct_span_err!(
209 "cannot borrow `{}`{} as {} because {} is also borrowed \
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<'cx> {
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(
437 ) -> DiagnosticBuilder<'cx> {
438 let mut err = struct_span_err!(
442 "closure may outlive the current function, \
444 which is owned by the current function",
447 err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
448 .span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
452 crate fn thread_local_value_does_not_live_long_enough(
455 ) -> DiagnosticBuilder<'cx> {
456 struct_span_err!(self, span, E0712, "thread-local variable borrowed past end of function",)
459 crate fn temporary_value_borrowed_for_too_long(&self, span: Span) -> DiagnosticBuilder<'cx> {
460 struct_span_err!(self, span, E0716, "temporary value dropped while borrowed",)
463 fn struct_span_err_with_code<S: Into<MultiSpan>>(
468 ) -> DiagnosticBuilder<'tcx> {
469 self.infcx.tcx.sess.struct_span_err_with_code(sp, msg, code)
473 crate fn borrowed_data_escapes_closure<'tcx>(
477 ) -> DiagnosticBuilder<'tcx> {
482 "borrowed data escapes outside of {}",