2 struct_span_err, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, ErrorGuaranteed, MultiSpan,
4 use rustc_middle::ty::{self, Ty, TyCtxt};
7 impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
8 pub(crate) fn cannot_move_when_borrowed(
12 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
13 struct_span_err!(self, span, E0505, "cannot move out of {} because it is borrowed", desc,)
16 pub(crate) fn cannot_use_when_mutably_borrowed(
22 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
23 let mut err = struct_span_err!(
27 "cannot use {} because it was mutably borrowed",
31 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
32 err.span_label(span, format!("use of borrowed {}", borrow_desc));
36 pub(crate) fn cannot_mutably_borrow_multiply(
43 old_load_end_span: Option<Span>,
44 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
46 |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
47 let mut err = struct_span_err!(
51 "cannot borrow {}{} as mutable more than once at a time",
55 if old_loan_span == new_loan_span {
56 // Both borrows are happening in the same place
57 // Meaning the borrow is occurring in a loop
61 "{}{} was mutably borrowed here in the previous iteration of the loop{}",
67 if let Some(old_load_end_span) = old_load_end_span {
68 err.span_label(old_load_end_span, "mutable borrow ends here");
73 format!("first mutable borrow occurs here{}", via(old_opt_via)),
77 format!("second mutable borrow occurs here{}", via(opt_via)),
79 if let Some(old_load_end_span) = old_load_end_span {
80 err.span_label(old_load_end_span, "first borrow ends here");
86 pub(crate) fn cannot_uniquely_borrow_by_two_closures(
91 old_load_end_span: Option<Span>,
92 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
93 let mut err = struct_span_err!(
97 "two closures require unique access to {} at the same time",
100 if old_loan_span == new_loan_span {
103 "closures are constructed here in different iterations of loop",
106 err.span_label(old_loan_span, "first closure is constructed here");
107 err.span_label(new_loan_span, "second closure is constructed here");
109 if let Some(old_load_end_span) = old_load_end_span {
110 err.span_label(old_load_end_span, "borrow from first closure ends here");
115 pub(crate) fn cannot_uniquely_borrow_by_one_closure(
118 container_name: &str,
124 previous_end_span: Option<Span>,
125 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
126 let mut err = struct_span_err!(
130 "closure requires unique access to {} but {} is already borrowed{}",
137 format!("{} construction occurs here{}", container_name, opt_via),
139 err.span_label(old_loan_span, format!("borrow occurs here{}", old_opt_via));
140 if let Some(previous_end_span) = previous_end_span {
141 err.span_label(previous_end_span, "borrow ends here");
146 pub(crate) fn cannot_reborrow_already_uniquely_borrowed(
149 container_name: &str,
155 previous_end_span: Option<Span>,
156 second_borrow_desc: &str,
157 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
158 let mut err = struct_span_err!(
162 "cannot borrow {}{} as {} because previous closure requires unique access",
169 format!("{}borrow occurs here{}", second_borrow_desc, opt_via),
173 format!("{} construction occurs here{}", container_name, old_opt_via),
175 if let Some(previous_end_span) = previous_end_span {
176 err.span_label(previous_end_span, "borrow from closure ends here");
181 pub(crate) fn cannot_reborrow_already_borrowed(
191 old_load_end_span: Option<Span>,
192 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
194 |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
195 let mut err = struct_span_err!(
199 "cannot borrow {}{} as {} because {} is also borrowed as {}{}",
209 // If `msg_new` is empty, then this isn't a borrow of a union field.
210 err.span_label(span, format!("{} borrow occurs here", kind_new));
211 err.span_label(old_span, format!("{} borrow occurs here", kind_old));
213 // If `msg_new` isn't empty, then this a borrow of a union field.
217 "{} borrow of {} -- which overlaps with {} -- occurs here",
218 kind_new, msg_new, msg_old,
221 err.span_label(old_span, format!("{} borrow occurs here{}", kind_old, via(msg_old)));
224 if let Some(old_load_end_span) = old_load_end_span {
225 err.span_label(old_load_end_span, format!("{} borrow ends here", kind_old));
230 pub(crate) fn cannot_assign_to_borrowed(
235 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
236 let mut err = struct_span_err!(
240 "cannot assign to {} because it is borrowed",
244 err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
245 err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
249 pub(crate) fn cannot_reassign_immutable(
254 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
255 let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
256 struct_span_err!(self, span, E0384, "cannot assign {} {}", msg, desc)
259 pub(crate) fn cannot_assign(
263 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
264 struct_span_err!(self, span, E0594, "cannot assign to {}", desc)
267 pub(crate) fn cannot_move_out_of(
269 move_from_span: Span,
270 move_from_desc: &str,
271 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
272 struct_span_err!(self, move_from_span, E0507, "cannot move out of {}", move_from_desc,)
275 /// Signal an error due to an attempt to move out of the interior
276 /// of an array or slice. `is_index` is None when error origin
277 /// didn't capture whether there was an indexing operation or not.
278 pub(crate) fn cannot_move_out_of_interior_noncopy(
280 move_from_span: Span,
282 is_index: Option<bool>,
283 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
284 let type_name = match (&ty.kind(), is_index) {
285 (&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array",
286 (&ty::Slice(_), _) => "slice",
287 _ => span_bug!(move_from_span, "this path should not cause illegal move"),
289 let mut err = struct_span_err!(
293 "cannot move out of type `{}`, a non-copy {}",
297 err.span_label(move_from_span, "cannot move out of here");
301 pub(crate) fn cannot_move_out_of_interior_of_drop(
303 move_from_span: Span,
304 container_ty: Ty<'_>,
305 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
306 let mut err = struct_span_err!(
310 "cannot move out of type `{}`, which implements the `Drop` trait",
313 err.span_label(move_from_span, "cannot move out of here");
317 pub(crate) fn cannot_act_on_moved_value(
321 optional_adverb_for_moved: &str,
322 moved_path: Option<String>,
323 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
324 let moved_path = moved_path.map(|mp| format!(": `{}`", mp)).unwrap_or_default();
330 "{} of {}moved value{}",
332 optional_adverb_for_moved,
337 pub(crate) fn cannot_borrow_path_as_mutable_because(
342 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
343 struct_span_err!(self, span, E0596, "cannot borrow {} as mutable{}", path, reason,)
346 pub(crate) fn cannot_mutate_in_immutable_section(
349 immutable_span: Span,
350 immutable_place: &str,
351 immutable_section: &str,
353 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
354 let mut err = struct_span_err!(
358 "cannot {} {} in {}",
363 err.span_label(mutate_span, format!("cannot {}", action));
364 err.span_label(immutable_span, format!("value is immutable in {}", immutable_section));
368 pub(crate) fn cannot_borrow_across_generator_yield(
372 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
373 let mut err = struct_span_err!(
377 "borrow may still be in use when generator yields",
379 err.span_label(yield_span, "possible yield occurs here");
383 pub(crate) fn cannot_borrow_across_destructor(
386 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
391 "borrow may still be in use when destructor runs",
395 pub(crate) fn path_does_not_live_long_enough(
399 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
400 struct_span_err!(self, span, E0597, "{} does not live long enough", path,)
403 pub(crate) fn cannot_return_reference_to_local(
407 reference_desc: &str,
409 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
410 let mut err = struct_span_err!(
414 "cannot {RETURN} {REFERENCE} {LOCAL}",
415 RETURN = return_kind,
416 REFERENCE = reference_desc,
422 format!("{}s a {} data owned by the current function", return_kind, reference_desc),
428 pub(crate) fn cannot_capture_in_long_lived_closure(
434 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
435 let mut err = struct_span_err!(
439 "{} may outlive the current function, but it borrows {}, which is owned by the current \
444 err.span_label(capture_span, format!("{} is borrowed here", borrowed_path))
445 .span_label(closure_span, format!("may outlive borrowed value {}", borrowed_path));
449 pub(crate) fn thread_local_value_does_not_live_long_enough(
452 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
453 struct_span_err!(self, span, E0712, "thread-local variable borrowed past end of function",)
456 pub(crate) fn temporary_value_borrowed_for_too_long(
459 ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> {
460 struct_span_err!(self, span, E0716, "temporary value dropped while borrowed",)
463 #[rustc_lint_diagnostics]
464 pub(crate) fn struct_span_err_with_code<S: Into<MultiSpan>>(
467 msg: impl Into<DiagnosticMessage>,
469 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
470 self.infcx.tcx.sess.struct_span_err_with_code(sp, msg, code)
474 pub(crate) fn borrowed_data_escapes_closure<'tcx>(
478 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
483 "borrowed data escapes outside of {}",