2 use rustc::hir::def_id::DefId;
4 self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, ConstraintCategory, Local,
5 LocalDecl, LocalKind, Location, Operand, Place, PlaceBase, Projection, PlaceRef,
6 ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind, VarBindingForm,
8 use rustc::ty::{self, Ty};
9 use rustc_data_structures::fx::FxHashSet;
10 use rustc_data_structures::indexed_vec::Idx;
11 use rustc_errors::{Applicability, DiagnosticBuilder};
13 use syntax::source_map::DesugaringKind;
15 use super::nll::explain_borrow::BorrowExplanation;
16 use super::nll::region_infer::{RegionName, RegionNameSource};
17 use super::prefixes::IsPrefixOf;
19 use super::borrow_set::BorrowData;
20 use super::MirBorrowckCtxt;
21 use super::{InitializationRequiringAction, PrefixSet};
22 use super::error_reporting::{IncludingDowncast, UseSpans};
23 use crate::dataflow::drop_flag_effects;
24 use crate::dataflow::indexes::{MovePathIndex, MoveOutIndex};
25 use crate::util::borrowck_errors;
29 /// Index of the "move out" that we found. The `MoveData` can
30 /// then tell us where the move occurred.
33 /// `true` if we traversed a back edge while walking from the point
34 /// of error to the move site.
35 traversed_back_edge: bool
38 /// Which case a StorageDeadOrDrop is for.
39 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
40 enum StorageDeadOrDrop<'tcx> {
46 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
47 pub(super) fn report_use_of_moved_or_uninitialized(
50 desired_action: InitializationRequiringAction,
51 (moved_place, used_place, span): (PlaceRef<'cx, 'tcx>, PlaceRef<'cx, 'tcx>, Span),
55 "report_use_of_moved_or_uninitialized: location={:?} desired_action={:?} \
56 moved_place={:?} used_place={:?} span={:?} mpi={:?}",
57 location, desired_action, moved_place, used_place, span, mpi
60 let use_spans = self.move_spans(moved_place, location)
61 .or_else(|| self.borrow_spans(span, location));
62 let span = use_spans.args_or_use();
64 let move_site_vec = self.get_moved_indexes(location, mpi);
66 "report_use_of_moved_or_uninitialized: move_site_vec={:?}",
69 let move_out_indices: Vec<_> = move_site_vec
71 .map(|move_site| move_site.moi)
74 if move_out_indices.is_empty() {
76 .prefixes(used_place, PrefixSet::All)
80 if self.uninitialized_error_reported.contains(&root_place) {
82 "report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
88 self.uninitialized_error_reported.insert(root_place);
90 let item_msg = match self.describe_place_with_options(used_place,
91 IncludingDowncast(true)) {
92 Some(name) => format!("`{}`", name),
93 None => "value".to_owned(),
95 let mut err = self.cannot_act_on_uninitialized_variable(
97 desired_action.as_noun(),
98 &self.describe_place_with_options(moved_place, IncludingDowncast(true))
99 .unwrap_or_else(|| "_".to_owned()),
101 err.span_label(span, format!("use of possibly-uninitialized {}", item_msg));
103 use_spans.var_span_label(
105 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
108 // This error should not be downgraded to a warning,
109 // even in migrate mode.
110 self.disable_error_downgrading();
111 err.buffer(&mut self.errors_buffer);
113 if let Some((reported_place, _)) = self.move_error_reported.get(&move_out_indices) {
114 if self.prefixes(*reported_place, PrefixSet::All)
115 .any(|p| p == used_place)
118 "report_use_of_moved_or_uninitialized place: error suppressed \
126 let msg = ""; //FIXME: add "partially " or "collaterally "
128 let mut err = self.cannot_act_on_moved_value(
130 desired_action.as_noun(),
132 self.describe_place_with_options(moved_place, IncludingDowncast(true)),
135 self.add_moved_or_invoked_closure_note(
141 let mut is_loop_move = false;
142 let is_partial_move = move_site_vec.iter().any(|move_site| {
143 let move_out = self.move_data.moves[(*move_site).moi];
144 let moved_place = &self.move_data.move_paths[move_out.path].place;
145 used_place != moved_place.as_ref()
146 && used_place.is_prefix_of(moved_place.as_ref())
148 for move_site in &move_site_vec {
149 let move_out = self.move_data.moves[(*move_site).moi];
150 let moved_place = &self.move_data.move_paths[move_out.path].place;
152 let move_spans = self.move_spans(moved_place.as_ref(), move_out.source);
153 let move_span = move_spans.args_or_use();
155 let move_msg = if move_spans.for_closure() {
161 if span == move_span {
164 format!("value moved{} here, in previous iteration of loop", move_msg),
167 } else if move_site.traversed_back_edge {
171 "value moved{} here, in previous iteration of loop",
176 err.span_label(move_span, format!("value moved{} here", move_msg));
177 move_spans.var_span_label(
179 format!("variable moved due to use{}", move_spans.describe()),
182 if Some(DesugaringKind::ForLoop) == move_span.desugaring_kind() {
183 if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
186 "consider borrowing to avoid moving into the for loop",
187 format!("&{}", snippet),
188 Applicability::MaybeIncorrect,
194 use_spans.var_span_label(
196 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
204 desired_action.as_verb_in_past_tense(),
205 if is_partial_move { "after partial move" } else { "after move" },
211 Place::ty_from(used_place.base, used_place.projection, self.body, self.infcx.tcx)
213 let needs_note = match ty.sty {
214 ty::Closure(id, _) => {
215 let tables = self.infcx.tcx.typeck_tables_of(id);
216 let hir_id = self.infcx.tcx.hir().as_local_hir_id(id).unwrap();
218 tables.closure_kind_origins().get(hir_id).is_none()
224 let mpi = self.move_data.moves[move_out_indices[0]].path;
225 let place = &self.move_data.move_paths[mpi].place;
227 let ty = place.ty(self.body, self.infcx.tcx).ty;
229 self.describe_place_with_options(place.as_ref(), IncludingDowncast(true));
230 let note_msg = match opt_name {
231 Some(ref name) => format!("`{}`", name),
232 None => "value".to_owned(),
234 if let ty::Param(param_ty) = ty.sty {
235 let tcx = self.infcx.tcx;
236 let generics = tcx.generics_of(self.mir_def_id);
237 let def_id = generics.type_param(¶m_ty, tcx).def_id;
238 if let Some(sp) = tcx.hir().span_if_local(def_id) {
241 "consider adding a `Copy` constraint to this type argument",
245 let span = if let Place {
246 base: PlaceBase::Local(local),
249 let decl = &self.body.local_decls[*local];
250 Some(decl.source_info.span)
254 self.note_type_does_not_implement_copy(
262 if let Some((_, mut old_err)) = self.move_error_reported
263 .insert(move_out_indices, (used_place, err))
265 // Cancel the old error so it doesn't ICE.
271 pub(super) fn report_move_out_while_borrowed(
274 (place, span): (&Place<'tcx>, Span),
275 borrow: &BorrowData<'tcx>,
278 "report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}",
279 location, place, span, borrow
281 let value_msg = match self.describe_place(place.as_ref()) {
282 Some(name) => format!("`{}`", name),
283 None => "value".to_owned(),
285 let borrow_msg = match self.describe_place(borrow.borrowed_place.as_ref()) {
286 Some(name) => format!("`{}`", name),
287 None => "value".to_owned(),
290 let borrow_spans = self.retrieve_borrow_spans(borrow);
291 let borrow_span = borrow_spans.args_or_use();
293 let move_spans = self.move_spans(place.as_ref(), location);
294 let span = move_spans.args_or_use();
296 let mut err = self.cannot_move_when_borrowed(
298 &self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
300 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
301 err.span_label(span, format!("move out of {} occurs here", value_msg));
303 borrow_spans.var_span_label(
305 format!("borrow occurs due to use{}", borrow_spans.describe())
308 move_spans.var_span_label(
310 format!("move occurs due to use{}", move_spans.describe())
313 self.explain_why_borrow_contains_point(
317 ).add_explanation_to_diagnostic(self.infcx.tcx, self.body, &mut err, "", Some(borrow_span));
318 err.buffer(&mut self.errors_buffer);
321 pub(super) fn report_use_while_mutably_borrowed(
324 (place, _span): (&Place<'tcx>, Span),
325 borrow: &BorrowData<'tcx>,
326 ) -> DiagnosticBuilder<'cx> {
327 let borrow_spans = self.retrieve_borrow_spans(borrow);
328 let borrow_span = borrow_spans.args_or_use();
330 // Conflicting borrows are reported separately, so only check for move
332 let use_spans = self.move_spans(place.as_ref(), location);
333 let span = use_spans.var_or_use();
335 let mut err = self.cannot_use_when_mutably_borrowed(
337 &self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
339 &self.describe_place(borrow.borrowed_place.as_ref())
340 .unwrap_or_else(|| "_".to_owned()),
343 borrow_spans.var_span_label(&mut err, {
344 let place = &borrow.borrowed_place;
346 self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned());
348 format!("borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe())
351 self.explain_why_borrow_contains_point(location, borrow, None)
352 .add_explanation_to_diagnostic(self.infcx.tcx, self.body, &mut err, "", None);
356 pub(super) fn report_conflicting_borrow(
359 (place, span): (&Place<'tcx>, Span),
360 gen_borrow_kind: BorrowKind,
361 issued_borrow: &BorrowData<'tcx>,
362 ) -> DiagnosticBuilder<'cx> {
363 let issued_spans = self.retrieve_borrow_spans(issued_borrow);
364 let issued_span = issued_spans.args_or_use();
366 let borrow_spans = self.borrow_spans(span, location);
367 let span = borrow_spans.args_or_use();
369 let container_name = if issued_spans.for_generator() || borrow_spans.for_generator() {
375 let (desc_place, msg_place, msg_borrow, union_type_name) =
376 self.describe_place_for_conflicting_borrow(place, &issued_borrow.borrowed_place);
378 let explanation = self.explain_why_borrow_contains_point(location, issued_borrow, None);
379 let second_borrow_desc = if explanation.is_explained() {
385 // FIXME: supply non-"" `opt_via` when appropriate
386 let first_borrow_desc;
387 let mut err = match (
395 (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => {
396 first_borrow_desc = "mutable ";
397 self.cannot_reborrow_already_borrowed(
409 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
410 first_borrow_desc = "immutable ";
411 self.cannot_reborrow_already_borrowed(
424 (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
425 first_borrow_desc = "first ";
426 self.cannot_mutably_borrow_multiply(
436 (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
437 first_borrow_desc = "first ";
438 self.cannot_uniquely_borrow_by_two_closures(
446 (BorrowKind::Mut { .. }, _, _, BorrowKind::Shallow, _, _)
447 | (BorrowKind::Unique, _, _, BorrowKind::Shallow, _, _) => {
448 let mut err = self.cannot_mutate_in_match_guard(
454 borrow_spans.var_span_label(
457 "borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe()
464 (BorrowKind::Unique, _, _, _, _, _) => {
465 first_borrow_desc = "first ";
466 self.cannot_uniquely_borrow_by_one_closure(
478 (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
479 first_borrow_desc = "first ";
480 self.cannot_reborrow_already_uniquely_borrowed(
493 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
494 first_borrow_desc = "first ";
495 self.cannot_reborrow_already_uniquely_borrowed(
508 (BorrowKind::Shared, _, _, BorrowKind::Shared, _, _)
509 | (BorrowKind::Shared, _, _, BorrowKind::Shallow, _, _)
510 | (BorrowKind::Shallow, _, _, BorrowKind::Mut { .. }, _, _)
511 | (BorrowKind::Shallow, _, _, BorrowKind::Unique, _, _)
512 | (BorrowKind::Shallow, _, _, BorrowKind::Shared, _, _)
513 | (BorrowKind::Shallow, _, _, BorrowKind::Shallow, _, _) => unreachable!(),
516 if issued_spans == borrow_spans {
517 borrow_spans.var_span_label(
519 format!("borrows occur due to use of `{}`{}", desc_place, borrow_spans.describe()),
522 let borrow_place = &issued_borrow.borrowed_place;
523 let borrow_place_desc = self.describe_place(borrow_place.as_ref())
524 .unwrap_or_else(|| "_".to_owned());
525 issued_spans.var_span_label(
528 "first borrow occurs due to use of `{}`{}",
530 issued_spans.describe(),
534 borrow_spans.var_span_label(
537 "second borrow occurs due to use of `{}`{}",
539 borrow_spans.describe(),
544 if union_type_name != "" {
546 "`{}` is a field of the union `{}`, so it overlaps the field `{}`",
547 msg_place, union_type_name, msg_borrow,
551 explanation.add_explanation_to_diagnostic(
562 /// Returns the description of the root place for a conflicting borrow and the full
563 /// descriptions of the places that caused the conflict.
565 /// In the simplest case, where there are no unions involved, if a mutable borrow of `x` is
566 /// attempted while a shared borrow is live, then this function will return:
570 /// In the simple union case, if a mutable borrow of a union field `x.z` is attempted while
571 /// a shared borrow of another field `x.y`, then this function will return:
573 /// ("x", "x.z", "x.y")
575 /// In the more complex union case, where the union is a field of a struct, then if a mutable
576 /// borrow of a union field in a struct `x.u.z` is attempted while a shared borrow of
577 /// another field `x.u.y`, then this function will return:
579 /// ("x.u", "x.u.z", "x.u.y")
581 /// This is used when creating error messages like below:
583 /// > cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
584 /// > mutable (via `a.u.s.b`) [E0502]
585 pub(super) fn describe_place_for_conflicting_borrow(
587 first_borrowed_place: &Place<'tcx>,
588 second_borrowed_place: &Place<'tcx>,
589 ) -> (String, String, String, String) {
590 // Define a small closure that we can use to check if the type of a place
592 let union_ty = |place_base, place_projection| {
593 let ty = Place::ty_from(place_base, place_projection, self.body, self.infcx.tcx).ty;
594 ty.ty_adt_def().filter(|adt| adt.is_union()).map(|_| ty)
596 let describe_place = |place| self.describe_place(place).unwrap_or_else(|| "_".to_owned());
598 // Start with an empty tuple, so we can use the functions on `Option` to reduce some
599 // code duplication (particularly around returning an empty description in the failure
603 // If we have a conflicting borrow of the same place, then we don't want to add
604 // an extraneous "via x.y" to our diagnostics, so filter out this case.
605 first_borrowed_place != second_borrowed_place
608 // We're going to want to traverse the first borrowed place to see if we can find
609 // field access to a union. If we find that, then we will keep the place of the
610 // union being accessed and the field that was being accessed so we can check the
611 // second borrowed place for the same union and a access to a different field.
615 } = first_borrowed_place;
617 let mut current = projection;
619 while let Some(box Projection { base: base_proj, elem }) = current {
621 ProjectionElem::Field(field, _) if union_ty(base, base_proj).is_some() => {
622 return Some((PlaceRef {
624 projection: base_proj,
627 _ => current = base_proj,
632 .and_then(|(target_base, target_field)| {
633 // With the place of a union and a field access into it, we traverse the second
634 // borrowed place and look for a access to a different field of the same union.
638 } = second_borrowed_place;
640 let mut current = projection;
642 while let Some(box Projection { base: proj_base, elem }) = current {
643 if let ProjectionElem::Field(field, _) = elem {
644 if let Some(union_ty) = union_ty(base, proj_base) {
645 if field != target_field
646 && base == target_base.base
647 && proj_base == target_base.projection {
648 // FIXME when we avoid clone reuse describe_place closure
649 let describe_base_place = self.describe_place(PlaceRef {
651 projection: proj_base,
652 }).unwrap_or_else(|| "_".to_owned());
656 describe_place(first_borrowed_place.as_ref()),
657 describe_place(second_borrowed_place.as_ref()),
658 union_ty.to_string(),
669 // If we didn't find a field access into a union, or both places match, then
670 // only return the description of the first place.
672 describe_place(first_borrowed_place.as_ref()),
680 /// Reports StorageDeadOrDrop of `place` conflicts with `borrow`.
682 /// This means that some data referenced by `borrow` needs to live
683 /// past the point where the StorageDeadOrDrop of `place` occurs.
684 /// This is usually interpreted as meaning that `place` has too
685 /// short a lifetime. (But sometimes it is more useful to report
686 /// it as a more direct conflict between the execution of a
687 /// `Drop::drop` with an aliasing borrow.)
688 pub(super) fn report_borrowed_value_does_not_live_long_enough(
691 borrow: &BorrowData<'tcx>,
692 place_span: (&Place<'tcx>, Span),
693 kind: Option<WriteKind>,
696 "report_borrowed_value_does_not_live_long_enough(\
697 {:?}, {:?}, {:?}, {:?}\
699 location, borrow, place_span, kind
702 let drop_span = place_span.1;
703 let root_place = self.prefixes(borrow.borrowed_place.as_ref(), PrefixSet::All)
707 let borrow_spans = self.retrieve_borrow_spans(borrow);
708 let borrow_span = borrow_spans.var_or_use();
710 assert!(root_place.projection.is_none());
711 let proper_span = match root_place.base {
712 PlaceBase::Local(local) => self.body.local_decls[*local].source_info.span,
716 if self.access_place_error_reported
718 base: root_place.base.clone(),
719 projection: root_place.projection.clone(),
723 "suppressing access_place error when borrow doesn't live long enough for {:?}",
729 self.access_place_error_reported
731 base: root_place.base.clone(),
732 projection: root_place.projection.clone(),
735 if let StorageDeadOrDrop::Destructor(dropped_ty) =
736 self.classify_drop_access_kind(borrow.borrowed_place.as_ref())
738 // If a borrow of path `B` conflicts with drop of `D` (and
739 // we're not in the uninteresting case where `B` is a
740 // prefix of `D`), then report this as a more interesting
741 // destructor conflict.
742 if !borrow.borrowed_place.as_ref().is_prefix_of(place_span.0.as_ref()) {
743 self.report_borrow_conflicts_with_destructor(
744 location, borrow, place_span, kind, dropped_ty,
750 let place_desc = self.describe_place(borrow.borrowed_place.as_ref());
752 let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
753 let explanation = self.explain_why_borrow_contains_point(location, &borrow, kind_place);
755 let err = match (place_desc, explanation) {
756 (Some(_), _) if self.is_place_thread_local(root_place) => {
757 self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
759 // If the outlives constraint comes from inside the closure,
764 // Box::new(|| y) as Box<Fn() -> &'static i32>
766 // then just use the normal error. The closure isn't escaping
767 // and `move` will not help here.
770 BorrowExplanation::MustBeValidFor {
771 category: category @ ConstraintCategory::Return,
780 BorrowExplanation::MustBeValidFor {
781 category: category @ ConstraintCategory::CallArgument,
787 ) if borrow_spans.for_closure() => self.report_escaping_closure_capture(
788 borrow_spans.args_or_use(),
793 &format!("`{}`", name),
797 BorrowExplanation::MustBeValidFor {
798 category: ConstraintCategory::Assignment,
800 region_name: RegionName {
801 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
807 ) => self.report_escaping_data(borrow_span, name, upvar_span, upvar_name, span),
808 (Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
816 (None, explanation) => self.report_temporary_value_does_not_live_long_enough(
826 err.buffer(&mut self.errors_buffer);
829 fn report_local_value_does_not_live_long_enough(
833 borrow: &BorrowData<'tcx>,
835 borrow_spans: UseSpans,
836 explanation: BorrowExplanation,
837 ) -> DiagnosticBuilder<'cx> {
839 "report_local_value_does_not_live_long_enough(\
840 {:?}, {:?}, {:?}, {:?}, {:?}\
842 location, name, borrow, drop_span, borrow_spans
845 let borrow_span = borrow_spans.var_or_use();
846 if let BorrowExplanation::MustBeValidFor {
853 if let Some(diag) = self.try_report_cannot_return_reference_to_local(
858 opt_place_desc.as_ref(),
864 let mut err = self.path_does_not_live_long_enough(
866 &format!("`{}`", name),
869 if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
870 let region_name = annotation.emit(self, &mut err);
874 format!("`{}` would have to be valid for `{}`...", name, region_name),
877 if let Some(fn_hir_id) = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id) {
881 "...but `{}` will be dropped here, when the function `{}` returns",
883 self.infcx.tcx.hir().name(fn_hir_id),
888 "functions cannot return a borrow to data owned within the function's scope, \
889 functions can only return borrows to data passed as arguments",
892 "to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
893 references-and-borrowing.html#dangling-references>",
898 format!("...but `{}` dropped here while still borrowed", name),
902 if let BorrowExplanation::MustBeValidFor { .. } = explanation {
904 explanation.add_explanation_to_diagnostic(
913 err.span_label(borrow_span, "borrowed value does not live long enough");
916 format!("`{}` dropped here while still borrowed", name),
919 let within = if borrow_spans.for_generator() {
925 borrow_spans.args_span_label(
927 format!("value captured here{}", within),
930 explanation.add_explanation_to_diagnostic(
931 self.infcx.tcx, self.body, &mut err, "", None);
937 fn report_borrow_conflicts_with_destructor(
940 borrow: &BorrowData<'tcx>,
941 (place, drop_span): (&Place<'tcx>, Span),
942 kind: Option<WriteKind>,
943 dropped_ty: Ty<'tcx>,
946 "report_borrow_conflicts_with_destructor(\
947 {:?}, {:?}, ({:?}, {:?}), {:?}\
949 location, borrow, place, drop_span, kind,
952 let borrow_spans = self.retrieve_borrow_spans(borrow);
953 let borrow_span = borrow_spans.var_or_use();
955 let mut err = self.cannot_borrow_across_destructor(borrow_span);
957 let what_was_dropped = match self.describe_place(place.as_ref()) {
958 Some(name) => format!("`{}`", name.as_str()),
959 None => String::from("temporary value"),
962 let label = match self.describe_place(borrow.borrowed_place.as_ref()) {
963 Some(borrowed) => format!(
964 "here, drop of {D} needs exclusive access to `{B}`, \
965 because the type `{T}` implements the `Drop` trait",
966 D = what_was_dropped,
971 "here is drop of {D}; whose type `{T}` implements the `Drop` trait",
972 D = what_was_dropped,
976 err.span_label(drop_span, label);
978 // Only give this note and suggestion if they could be relevant.
980 self.explain_why_borrow_contains_point(location, borrow, kind.map(|k| (k, place)));
982 BorrowExplanation::UsedLater { .. }
983 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
984 err.note("consider using a `let` binding to create a longer lived value");
989 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.body, &mut err, "", None);
991 err.buffer(&mut self.errors_buffer);
994 fn report_thread_local_value_does_not_live_long_enough(
998 ) -> DiagnosticBuilder<'cx> {
1000 "report_thread_local_value_does_not_live_long_enough(\
1003 drop_span, borrow_span
1006 let mut err = self.thread_local_value_does_not_live_long_enough(borrow_span);
1010 "thread-local variables cannot be borrowed beyond the end of the function",
1012 err.span_label(drop_span, "end of enclosing function is here");
1017 fn report_temporary_value_does_not_live_long_enough(
1020 borrow: &BorrowData<'tcx>,
1022 borrow_spans: UseSpans,
1024 explanation: BorrowExplanation,
1025 ) -> DiagnosticBuilder<'cx> {
1027 "report_temporary_value_does_not_live_long_enough(\
1028 {:?}, {:?}, {:?}, {:?}\
1030 location, borrow, drop_span, proper_span
1033 if let BorrowExplanation::MustBeValidFor {
1036 from_closure: false,
1039 if let Some(diag) = self.try_report_cannot_return_reference_to_local(
1050 let mut err = self.temporary_value_borrowed_for_too_long(proper_span);
1053 "creates a temporary which is freed while still in use",
1057 "temporary value is freed at the end of this statement",
1061 BorrowExplanation::UsedLater(..)
1062 | BorrowExplanation::UsedLaterInLoop(..)
1063 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
1064 // Only give this note and suggestion if it could be relevant.
1065 err.note("consider using a `let` binding to create a longer lived value");
1069 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.body, &mut err, "", None);
1071 let within = if borrow_spans.for_generator() {
1077 borrow_spans.args_span_label(
1079 format!("value captured here{}", within),
1085 fn try_report_cannot_return_reference_to_local(
1087 borrow: &BorrowData<'tcx>,
1090 category: ConstraintCategory,
1091 opt_place_desc: Option<&String>,
1092 ) -> Option<DiagnosticBuilder<'cx>> {
1093 let return_kind = match category {
1094 ConstraintCategory::Return => "return",
1095 ConstraintCategory::Yield => "yield",
1099 // FIXME use a better heuristic than Spans
1100 let reference_desc = if return_span == self.body.source_info(borrow.reserve_location).span {
1106 let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
1107 let local_kind = match borrow.borrowed_place {
1109 base: PlaceBase::Local(local),
1112 match self.body.local_kind(local) {
1113 LocalKind::ReturnPointer
1114 | LocalKind::Temp => bug!("temporary or return pointer with a name"),
1115 LocalKind::Var => "local variable ",
1117 if !self.upvars.is_empty()
1118 && local == Local::new(1) => {
1119 "variable captured by `move` "
1122 "function parameter "
1129 format!("{}`{}`", local_kind, place_desc),
1130 format!("`{}` is borrowed here", place_desc),
1133 let root_place = self.prefixes(borrow.borrowed_place.as_ref(),
1137 let local = if let PlaceRef {
1138 base: PlaceBase::Local(local),
1143 bug!("try_report_cannot_return_reference_to_local: not a local")
1145 match self.body.local_kind(*local) {
1146 LocalKind::ReturnPointer | LocalKind::Temp => (
1147 "temporary value".to_string(),
1148 "temporary value created here".to_string(),
1151 "function parameter".to_string(),
1152 "function parameter borrowed here".to_string(),
1155 "local binding".to_string(),
1156 "local binding introduced here".to_string(),
1161 let mut err = self.cannot_return_reference_to_local(
1168 if return_span != borrow_span {
1169 err.span_label(borrow_span, note);
1175 fn report_escaping_closure_capture(
1179 fr_name: &RegionName,
1180 category: ConstraintCategory,
1181 constraint_span: Span,
1183 ) -> DiagnosticBuilder<'cx> {
1184 let tcx = self.infcx.tcx;
1186 let mut err = self.cannot_capture_in_long_lived_closure(
1192 let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
1194 if string.starts_with("async ") {
1195 string.insert_str(6, "move ");
1196 } else if string.starts_with("async|") {
1197 string.insert_str(5, " move");
1199 string.insert_str(0, "move ");
1203 Err(_) => "move |<args>| <body>".to_string()
1206 err.span_suggestion(
1208 &format!("to force the closure to take ownership of {} (and any \
1209 other referenced variables), use the `move` keyword",
1212 Applicability::MachineApplicable,
1216 ConstraintCategory::Return => {
1217 err.span_note(constraint_span, "closure is returned here");
1219 ConstraintCategory::CallArgument => {
1220 fr_name.highlight_region_name(&mut err);
1223 &format!("function requires argument type to outlive `{}`", fr_name),
1226 _ => bug!("report_escaping_closure_capture called with unexpected constraint \
1227 category: `{:?}`", category),
1232 fn report_escaping_data(
1235 name: &Option<String>,
1239 ) -> DiagnosticBuilder<'cx> {
1240 let tcx = self.infcx.tcx;
1242 let escapes_from = if tcx.is_closure(self.mir_def_id) {
1243 let tables = tcx.typeck_tables_of(self.mir_def_id);
1244 let mir_hir_id = tcx.hir().def_index_to_hir_id(self.mir_def_id.index);
1245 match tables.node_type(mir_hir_id).sty {
1246 ty::Closure(..) => "closure",
1247 ty::Generator(..) => "generator",
1248 _ => bug!("Closure body doesn't have a closure or generator type"),
1254 let mut err = borrowck_errors::borrowed_data_escapes_closure(
1263 "`{}` is declared here, outside of the {} body",
1264 upvar_name, escapes_from
1271 "borrow is only valid in the {} body",
1276 if let Some(name) = name {
1279 format!("reference to `{}` escapes the {} body here", name, escapes_from),
1284 format!("reference escapes the {} body here", escapes_from),
1291 fn get_moved_indexes(&mut self, location: Location, mpi: MovePathIndex) -> Vec<MoveSite> {
1292 let body = self.body;
1294 let mut stack = Vec::new();
1295 stack.extend(body.predecessor_locations(location).map(|predecessor| {
1296 let is_back_edge = location.dominates(predecessor, &self.dominators);
1297 (predecessor, is_back_edge)
1300 let mut visited = FxHashSet::default();
1301 let mut result = vec![];
1303 'dfs: while let Some((location, is_back_edge)) = stack.pop() {
1305 "report_use_of_moved_or_uninitialized: (current_location={:?}, back_edge={})",
1306 location, is_back_edge
1309 if !visited.insert(location) {
1314 let stmt_kind = body[location.block]
1316 .get(location.statement_index)
1318 if let Some(StatementKind::StorageDead(..)) = stmt_kind {
1319 // this analysis only tries to find moves explicitly
1320 // written by the user, so we ignore the move-outs
1321 // created by `StorageDead` and at the beginning
1324 // If we are found a use of a.b.c which was in error, then we want to look for
1325 // moves not only of a.b.c but also a.b and a.
1327 // Note that the moves data already includes "parent" paths, so we don't have to
1328 // worry about the other case: that is, if there is a move of a.b.c, it is already
1329 // marked as a move of a.b and a as well, so we will generate the correct errors
1331 let mut mpis = vec![mpi];
1332 let move_paths = &self.move_data.move_paths;
1333 mpis.extend(move_paths[mpi].parents(move_paths));
1335 for moi in &self.move_data.loc_map[location] {
1336 debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
1337 if mpis.contains(&self.move_data.moves[*moi].path) {
1338 debug!("report_use_of_moved_or_uninitialized: found");
1339 result.push(MoveSite {
1341 traversed_back_edge: is_back_edge,
1344 // Strictly speaking, we could continue our DFS here. There may be
1345 // other moves that can reach the point of error. But it is kind of
1346 // confusing to highlight them.
1354 // drop(a); // <-- current point of error
1357 // Because we stop the DFS here, we only highlight `let c = a`,
1358 // and not `let b = a`. We will of course also report an error at
1359 // `let c = a` which highlights `let b = a` as the move.
1366 let mut any_match = false;
1367 drop_flag_effects::for_location_inits(
1382 stack.extend(body.predecessor_locations(location).map(|predecessor| {
1383 let back_edge = location.dominates(predecessor, &self.dominators);
1384 (predecessor, is_back_edge || back_edge)
1391 pub(super) fn report_illegal_mutation_of_borrowed(
1394 (place, span): (&Place<'tcx>, Span),
1395 loan: &BorrowData<'tcx>,
1397 let loan_spans = self.retrieve_borrow_spans(loan);
1398 let loan_span = loan_spans.args_or_use();
1400 if loan.kind == BorrowKind::Shallow {
1401 let mut err = self.cannot_mutate_in_match_guard(
1404 &self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
1407 loan_spans.var_span_label(
1409 format!("borrow occurs due to use{}", loan_spans.describe()),
1412 err.buffer(&mut self.errors_buffer);
1417 let mut err = self.cannot_assign_to_borrowed(
1420 &self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
1423 loan_spans.var_span_label(
1425 format!("borrow occurs due to use{}", loan_spans.describe()),
1428 self.explain_why_borrow_contains_point(location, loan, None)
1429 .add_explanation_to_diagnostic(self.infcx.tcx, self.body, &mut err, "", None);
1431 err.buffer(&mut self.errors_buffer);
1434 /// Reports an illegal reassignment; for example, an assignment to
1435 /// (part of) a non-`mut` local that occurs potentially after that
1436 /// local has already been initialized. `place` is the path being
1437 /// assigned; `err_place` is a place providing a reason why
1438 /// `place` is not mutable (e.g., the non-`mut` local `x` in an
1439 /// assignment to `x.f`).
1440 pub(super) fn report_illegal_reassignment(
1442 _location: Location,
1443 (place, span): (&Place<'tcx>, Span),
1444 assigned_span: Span,
1445 err_place: &Place<'tcx>,
1447 let (from_arg, local_decl) = if let Place {
1448 base: PlaceBase::Local(local),
1451 if let LocalKind::Arg = self.body.local_kind(local) {
1452 (true, Some(&self.body.local_decls[local]))
1454 (false, Some(&self.body.local_decls[local]))
1460 // If root local is initialized immediately (everything apart from let
1461 // PATTERN;) then make the error refer to that local, rather than the
1462 // place being assigned later.
1463 let (place_description, assigned_span) = match local_decl {
1465 is_user_variable: Some(ClearCrossCrate::Clear),
1470 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
1471 opt_match_place: None,
1477 is_user_variable: None,
1480 | None => (self.describe_place(place.as_ref()), assigned_span),
1481 Some(decl) => (self.describe_place(err_place.as_ref()), decl.source_info.span),
1484 let mut err = self.cannot_reassign_immutable(
1486 place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
1489 let msg = if from_arg {
1490 "cannot assign to immutable argument"
1492 "cannot assign twice to immutable variable"
1494 if span != assigned_span {
1496 let value_msg = match place_description {
1497 Some(name) => format!("`{}`", name),
1498 None => "value".to_owned(),
1500 err.span_label(assigned_span, format!("first assignment to {}", value_msg));
1503 if let Some(decl) = local_decl {
1504 if let Some(name) = decl.name {
1505 if decl.can_be_made_mutable() {
1506 err.span_suggestion(
1507 decl.source_info.span,
1508 "make this binding mutable",
1509 format!("mut {}", name),
1510 Applicability::MachineApplicable,
1515 err.span_label(span, msg);
1516 err.buffer(&mut self.errors_buffer);
1519 fn classify_drop_access_kind(&self, place: PlaceRef<'cx, 'tcx>) -> StorageDeadOrDrop<'tcx> {
1520 let tcx = self.infcx.tcx;
1521 match place.projection {
1523 StorageDeadOrDrop::LocalStorageDead
1525 Some(box Projection { ref base, ref elem }) => {
1526 let base_access = self.classify_drop_access_kind(PlaceRef {
1531 ProjectionElem::Deref => match base_access {
1532 StorageDeadOrDrop::LocalStorageDead
1533 | StorageDeadOrDrop::BoxedStorageDead => {
1535 Place::ty_from(&place.base, base, self.body, tcx).ty.is_box(),
1536 "Drop of value behind a reference or raw pointer"
1538 StorageDeadOrDrop::BoxedStorageDead
1540 StorageDeadOrDrop::Destructor(_) => base_access,
1542 ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1543 let base_ty = Place::ty_from(&place.base, base, self.body, tcx).ty;
1545 ty::Adt(def, _) if def.has_dtor(tcx) => {
1546 // Report the outermost adt with a destructor
1548 StorageDeadOrDrop::Destructor(_) => base_access,
1549 StorageDeadOrDrop::LocalStorageDead
1550 | StorageDeadOrDrop::BoxedStorageDead => {
1551 StorageDeadOrDrop::Destructor(base_ty)
1559 ProjectionElem::ConstantIndex { .. }
1560 | ProjectionElem::Subslice { .. }
1561 | ProjectionElem::Index(_) => base_access,
1567 /// Annotate argument and return type of function and closure with (synthesized) lifetime for
1568 /// borrow of local value that does not live long enough.
1569 fn annotate_argument_and_return_for_borrow(
1571 borrow: &BorrowData<'tcx>,
1572 ) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
1573 // Define a fallback for when we can't match a closure.
1575 let is_closure = self.infcx.tcx.is_closure(self.mir_def_id);
1579 let ty = self.infcx.tcx.type_of(self.mir_def_id);
1581 ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
1583 self.infcx.tcx.fn_sig(self.mir_def_id),
1590 // In order to determine whether we need to annotate, we need to check whether the reserve
1591 // place was an assignment into a temporary.
1593 // If it was, we check whether or not that temporary is eventually assigned into the return
1594 // place. If it was, we can add annotations about the function's return type and arguments
1595 // and it'll make sense.
1596 let location = borrow.reserve_location;
1598 "annotate_argument_and_return_for_borrow: location={:?}",
1601 if let Some(&Statement { kind: StatementKind::Assign(ref reservation, _), ..})
1602 = &self.body[location.block].statements.get(location.statement_index)
1605 "annotate_argument_and_return_for_borrow: reservation={:?}",
1608 // Check that the initial assignment of the reserve location is into a temporary.
1609 let mut target = *match reservation {
1611 base: PlaceBase::Local(local),
1613 } if self.body.local_kind(*local) == LocalKind::Temp => local,
1617 // Next, look through the rest of the block, checking if we are assigning the
1618 // `target` (that is, the place that contains our borrow) to anything.
1619 let mut annotated_closure = None;
1620 for stmt in &self.body[location.block].statements[location.statement_index + 1..] {
1622 "annotate_argument_and_return_for_borrow: target={:?} stmt={:?}",
1625 if let StatementKind::Assign(
1627 base: PlaceBase::Local(assigned_to),
1633 "annotate_argument_and_return_for_borrow: assigned_to={:?} \
1637 // Check if our `target` was captured by a closure.
1638 if let Rvalue::Aggregate(
1639 box AggregateKind::Closure(def_id, substs),
1643 for operand in operands {
1644 let assigned_from = match operand {
1645 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1651 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1655 // Find the local from the operand.
1656 let assigned_from_local = match assigned_from.local_or_deref_local() {
1657 Some(local) => local,
1661 if assigned_from_local != target {
1665 // If a closure captured our `target` and then assigned
1666 // into a place then we should annotate the closure in
1667 // case it ends up being assigned into the return place.
1668 annotated_closure = self.annotate_fn_sig(
1670 self.infcx.closure_sig(*def_id, *substs),
1673 "annotate_argument_and_return_for_borrow: \
1674 annotated_closure={:?} assigned_from_local={:?} \
1676 annotated_closure, assigned_from_local, assigned_to
1679 if *assigned_to == mir::RETURN_PLACE {
1680 // If it was assigned directly into the return place, then
1682 return annotated_closure;
1684 // Otherwise, update the target.
1685 target = *assigned_to;
1689 // If none of our closure's operands matched, then skip to the next
1694 // Otherwise, look at other types of assignment.
1695 let assigned_from = match rvalue {
1696 Rvalue::Ref(_, _, assigned_from) => assigned_from,
1697 Rvalue::Use(operand) => match operand {
1698 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1706 "annotate_argument_and_return_for_borrow: \
1707 assigned_from={:?}",
1711 // Find the local from the rvalue.
1712 let assigned_from_local = match assigned_from.local_or_deref_local() {
1713 Some(local) => local,
1717 "annotate_argument_and_return_for_borrow: \
1718 assigned_from_local={:?}",
1719 assigned_from_local,
1722 // Check if our local matches the target - if so, we've assigned our
1723 // borrow to a new place.
1724 if assigned_from_local != target {
1728 // If we assigned our `target` into a new place, then we should
1729 // check if it was the return place.
1731 "annotate_argument_and_return_for_borrow: \
1732 assigned_from_local={:?} assigned_to={:?}",
1733 assigned_from_local, assigned_to
1735 if *assigned_to == mir::RETURN_PLACE {
1736 // If it was then return the annotated closure if there was one,
1737 // else, annotate this function.
1738 return annotated_closure.or_else(fallback);
1741 // If we didn't assign into the return place, then we just update
1743 target = *assigned_to;
1747 // Check the terminator if we didn't find anything in the statements.
1748 let terminator = &self.body[location.block].terminator();
1750 "annotate_argument_and_return_for_borrow: target={:?} terminator={:?}",
1753 if let TerminatorKind::Call {
1754 destination: Some((Place {
1755 base: PlaceBase::Local(assigned_to),
1760 } = &terminator.kind
1763 "annotate_argument_and_return_for_borrow: assigned_to={:?} args={:?}",
1766 for operand in args {
1767 let assigned_from = match operand {
1768 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1774 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1778 if let Some(assigned_from_local) = assigned_from.local_or_deref_local() {
1780 "annotate_argument_and_return_for_borrow: assigned_from_local={:?}",
1781 assigned_from_local,
1784 if *assigned_to == mir::RETURN_PLACE && assigned_from_local == target {
1785 return annotated_closure.or_else(fallback);
1792 // If we haven't found an assignment into the return place, then we need not add
1794 debug!("annotate_argument_and_return_for_borrow: none found");
1798 /// Annotate the first argument and return type of a function signature if they are
1803 sig: ty::PolyFnSig<'tcx>,
1804 ) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
1805 debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
1806 let is_closure = self.infcx.tcx.is_closure(did);
1807 let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did)?;
1808 let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
1810 // We need to work out which arguments to highlight. We do this by looking
1811 // at the return type, where there are three cases:
1813 // 1. If there are named arguments, then we should highlight the return type and
1814 // highlight any of the arguments that are also references with that lifetime.
1815 // If there are no arguments that have the same lifetime as the return type,
1816 // then don't highlight anything.
1817 // 2. The return type is a reference with an anonymous lifetime. If this is
1818 // the case, then we can take advantage of (and teach) the lifetime elision
1821 // We know that an error is being reported. So the arguments and return type
1822 // must satisfy the elision rules. Therefore, if there is a single argument
1823 // then that means the return type and first (and only) argument have the same
1824 // lifetime and the borrow isn't meeting that, we can highlight the argument
1827 // If there are multiple arguments then the first argument must be self (else
1828 // it would not satisfy the elision rules), so we can highlight self and the
1830 // 3. The return type is not a reference. In this case, we don't highlight
1832 let return_ty = sig.output();
1833 match return_ty.skip_binder().sty {
1834 ty::Ref(return_region, _, _) if return_region.has_name() && !is_closure => {
1835 // This is case 1 from above, return type is a named reference so we need to
1836 // search for relevant arguments.
1837 let mut arguments = Vec::new();
1838 for (index, argument) in sig.inputs().skip_binder().iter().enumerate() {
1839 if let ty::Ref(argument_region, _, _) = argument.sty {
1840 if argument_region == return_region {
1841 // Need to use the `rustc::ty` types to compare against the
1842 // `return_region`. Then use the `rustc::hir` type to get only
1843 // the lifetime span.
1844 if let hir::TyKind::Rptr(lifetime, _) = &fn_decl.inputs[index].node {
1845 // With access to the lifetime, we can get
1847 arguments.push((*argument, lifetime.span));
1849 bug!("ty type is a ref but hir type is not");
1855 // We need to have arguments. This shouldn't happen, but it's worth checking.
1856 if arguments.is_empty() {
1860 // We use a mix of the HIR and the Ty types to get information
1861 // as the HIR doesn't have full types for closure arguments.
1862 let return_ty = *sig.output().skip_binder();
1863 let mut return_span = fn_decl.output.span();
1864 if let hir::FunctionRetTy::Return(ty) = &fn_decl.output {
1865 if let hir::TyKind::Rptr(lifetime, _) = ty.node {
1866 return_span = lifetime.span;
1870 Some(AnnotatedBorrowFnSignature::NamedFunction {
1876 ty::Ref(_, _, _) if is_closure => {
1877 // This is case 2 from above but only for closures, return type is anonymous
1878 // reference so we select
1879 // the first argument.
1880 let argument_span = fn_decl.inputs.first()?.span;
1881 let argument_ty = sig.inputs().skip_binder().first()?;
1883 // Closure arguments are wrapped in a tuple, so we need to get the first
1885 if let ty::Tuple(elems) = argument_ty.sty {
1886 let argument_ty = elems.first()?.expect_ty();
1887 if let ty::Ref(_, _, _) = argument_ty.sty {
1888 return Some(AnnotatedBorrowFnSignature::Closure {
1897 ty::Ref(_, _, _) => {
1898 // This is also case 2 from above but for functions, return type is still an
1899 // anonymous reference so we select the first argument.
1900 let argument_span = fn_decl.inputs.first()?.span;
1901 let argument_ty = sig.inputs().skip_binder().first()?;
1903 let return_span = fn_decl.output.span();
1904 let return_ty = *sig.output().skip_binder();
1906 // We expect the first argument to be a reference.
1907 match argument_ty.sty {
1908 ty::Ref(_, _, _) => {}
1912 Some(AnnotatedBorrowFnSignature::AnonymousFunction {
1920 // This is case 3 from above, return type is not a reference so don't highlight
1929 enum AnnotatedBorrowFnSignature<'tcx> {
1931 arguments: Vec<(Ty<'tcx>, Span)>,
1932 return_ty: Ty<'tcx>,
1936 argument_ty: Ty<'tcx>,
1937 argument_span: Span,
1938 return_ty: Ty<'tcx>,
1942 argument_ty: Ty<'tcx>,
1943 argument_span: Span,
1947 impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
1948 /// Annotate the provided diagnostic with information about borrow from the fn signature that
1952 cx: &mut MirBorrowckCtxt<'_, 'tcx>,
1953 diag: &mut DiagnosticBuilder<'_>,
1956 AnnotatedBorrowFnSignature::Closure {
1962 format!("has type `{}`", cx.get_name_for_ty(argument_ty, 0)),
1965 cx.get_region_name_for_ty(argument_ty, 0)
1967 AnnotatedBorrowFnSignature::AnonymousFunction {
1973 let argument_ty_name = cx.get_name_for_ty(argument_ty, 0);
1974 diag.span_label(*argument_span, format!("has type `{}`", argument_ty_name));
1976 let return_ty_name = cx.get_name_for_ty(return_ty, 0);
1977 let types_equal = return_ty_name == argument_ty_name;
1982 if types_equal { "also " } else { "" },
1988 "argument and return type have the same lifetime due to lifetime elision rules",
1991 "to learn more, visit <https://doc.rust-lang.org/book/ch10-03-\
1992 lifetime-syntax.html#lifetime-elision>",
1995 cx.get_region_name_for_ty(return_ty, 0)
1997 AnnotatedBorrowFnSignature::NamedFunction {
2002 // Region of return type and arguments checked to be the same earlier.
2003 let region_name = cx.get_region_name_for_ty(return_ty, 0);
2004 for (_, argument_span) in arguments {
2005 diag.span_label(*argument_span, format!("has lifetime `{}`", region_name));
2010 format!("also has lifetime `{}`", region_name,),
2014 "use data from the highlighted arguments which match the `{}` lifetime of \