2 use rustc::hir::def_id::DefId;
4 self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, ConstraintCategory, Local,
5 LocalDecl, LocalKind, Location, Operand, Place, PlaceBase, PlaceProjection,
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::CompilerDesugaringKind;
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::{BorrowckErrors, Origin};
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, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
47 pub(super) fn report_use_of_moved_or_uninitialized(
50 desired_action: InitializationRequiringAction,
51 (moved_place, used_place, span): (&Place<'tcx>, &Place<'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() {
75 let root_place = self.prefixes(&used_place, PrefixSet::All).last().unwrap();
77 if self.uninitialized_error_reported.contains(root_place) {
79 "report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
85 self.uninitialized_error_reported.insert(root_place.clone());
87 let item_msg = match self.describe_place_with_options(used_place,
88 IncludingDowncast(true)) {
89 Some(name) => format!("`{}`", name),
90 None => "value".to_owned(),
92 let mut err = self.infcx.tcx.cannot_act_on_uninitialized_variable(
94 desired_action.as_noun(),
95 &self.describe_place_with_options(moved_place, IncludingDowncast(true))
96 .unwrap_or_else(|| "_".to_owned()),
99 err.span_label(span, format!("use of possibly uninitialized {}", item_msg));
101 use_spans.var_span_label(
103 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
106 err.buffer(&mut self.errors_buffer);
108 if let Some((reported_place, _)) = self.move_error_reported.get(&move_out_indices) {
109 if self.prefixes(&reported_place, PrefixSet::All)
110 .any(|p| p == used_place)
113 "report_use_of_moved_or_uninitialized place: error suppressed \
121 let msg = ""; //FIXME: add "partially " or "collaterally "
123 let mut err = self.infcx.tcx.cannot_act_on_moved_value(
125 desired_action.as_noun(),
127 self.describe_place_with_options(&moved_place, IncludingDowncast(true)),
131 self.add_moved_or_invoked_closure_note(
137 let mut is_loop_move = false;
138 let is_partial_move = move_site_vec.iter().any(|move_site| {
139 let move_out = self.move_data.moves[(*move_site).moi];
140 let moved_place = &self.move_data.move_paths[move_out.path].place;
141 used_place != moved_place && used_place.is_prefix_of(moved_place)
143 for move_site in &move_site_vec {
144 let move_out = self.move_data.moves[(*move_site).moi];
145 let moved_place = &self.move_data.move_paths[move_out.path].place;
147 let move_spans = self.move_spans(moved_place, move_out.source);
148 let move_span = move_spans.args_or_use();
150 let move_msg = if move_spans.for_closure() {
156 if span == move_span {
159 format!("value moved{} here, in previous iteration of loop", move_msg),
161 if Some(CompilerDesugaringKind::ForLoop) == span.compiler_desugaring_kind() {
162 if let Ok(snippet) = self.infcx.tcx.sess.source_map()
163 .span_to_snippet(span)
167 "consider borrowing this to avoid moving it into the for loop",
168 format!("&{}", snippet),
169 Applicability::MaybeIncorrect,
174 } else if move_site.traversed_back_edge {
178 "value moved{} here, in previous iteration of loop",
183 err.span_label(move_span, format!("value moved{} here", move_msg));
184 move_spans.var_span_label(
186 format!("variable moved due to use{}", move_spans.describe()),
191 use_spans.var_span_label(
193 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
201 desired_action.as_verb_in_past_tense(),
202 if is_partial_move { "after partial move" } else { "after move" },
207 let ty = used_place.ty(self.mir, self.infcx.tcx).ty;
208 let needs_note = match ty.sty {
209 ty::Closure(id, _) => {
210 let tables = self.infcx.tcx.typeck_tables_of(id);
211 let hir_id = self.infcx.tcx.hir().as_local_hir_id(id).unwrap();
213 tables.closure_kind_origins().get(hir_id).is_none()
219 let mpi = self.move_data.moves[move_out_indices[0]].path;
220 let place = &self.move_data.move_paths[mpi].place;
222 let ty = place.ty(self.mir, self.infcx.tcx).ty;
223 let opt_name = self.describe_place_with_options(place, IncludingDowncast(true));
224 let note_msg = match opt_name {
225 Some(ref name) => format!("`{}`", name),
226 None => "value".to_owned(),
228 if let ty::Param(param_ty) = ty.sty {
229 let tcx = self.infcx.tcx;
230 let generics = tcx.generics_of(self.mir_def_id);
231 let def_id = generics.type_param(¶m_ty, tcx).def_id;
232 if let Some(sp) = tcx.hir().span_if_local(def_id) {
235 "consider adding a `Copy` constraint to this type argument",
239 if let Place::Base(PlaceBase::Local(local)) = place {
240 let decl = &self.mir.local_decls[*local];
242 decl.source_info.span,
244 "move occurs because {} has type `{}`, \
245 which does not implement the `Copy` trait",
250 "move occurs because {} has type `{}`, \
251 which does not implement the `Copy` trait",
257 if let Some((_, mut old_err)) = self.move_error_reported
258 .insert(move_out_indices, (used_place.clone(), err))
260 // Cancel the old error so it doesn't ICE.
266 pub(super) fn report_move_out_while_borrowed(
269 (place, span): (&Place<'tcx>, Span),
270 borrow: &BorrowData<'tcx>,
273 "report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}",
274 location, place, span, borrow
276 let tcx = self.infcx.tcx;
277 let value_msg = match self.describe_place(place) {
278 Some(name) => format!("`{}`", name),
279 None => "value".to_owned(),
281 let borrow_msg = match self.describe_place(&borrow.borrowed_place) {
282 Some(name) => format!("`{}`", name),
283 None => "value".to_owned(),
286 let borrow_spans = self.retrieve_borrow_spans(borrow);
287 let borrow_span = borrow_spans.args_or_use();
289 let move_spans = self.move_spans(place, location);
290 let span = move_spans.args_or_use();
292 let mut err = tcx.cannot_move_when_borrowed(
294 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
297 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
298 err.span_label(span, format!("move out of {} occurs here", value_msg));
300 borrow_spans.var_span_label(
302 format!("borrow occurs due to use{}", borrow_spans.describe())
305 move_spans.var_span_label(
307 format!("move occurs due to use{}", move_spans.describe())
310 self.explain_why_borrow_contains_point(
314 ).add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", Some(borrow_span));
315 err.buffer(&mut self.errors_buffer);
318 pub(super) fn report_use_while_mutably_borrowed(
321 (place, _span): (&Place<'tcx>, Span),
322 borrow: &BorrowData<'tcx>,
323 ) -> DiagnosticBuilder<'cx> {
324 let tcx = self.infcx.tcx;
326 let borrow_spans = self.retrieve_borrow_spans(borrow);
327 let borrow_span = borrow_spans.args_or_use();
329 // Conflicting borrows are reported separately, so only check for move
331 let use_spans = self.move_spans(place, location);
332 let span = use_spans.var_or_use();
334 let mut err = tcx.cannot_use_when_mutably_borrowed(
336 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
338 &self.describe_place(&borrow.borrowed_place)
339 .unwrap_or_else(|| "_".to_owned()),
343 borrow_spans.var_span_label(&mut err, {
344 let place = &borrow.borrowed_place;
345 let desc_place = self.describe_place(place).unwrap_or_else(|| "_".to_owned());
347 format!("borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe())
350 self.explain_why_borrow_contains_point(location, borrow, None)
351 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
355 pub(super) fn report_conflicting_borrow(
358 (place, span): (&Place<'tcx>, Span),
359 gen_borrow_kind: BorrowKind,
360 issued_borrow: &BorrowData<'tcx>,
361 ) -> DiagnosticBuilder<'cx> {
362 let issued_spans = self.retrieve_borrow_spans(issued_borrow);
363 let issued_span = issued_spans.args_or_use();
365 let borrow_spans = self.borrow_spans(span, location);
366 let span = borrow_spans.args_or_use();
368 let container_name = if issued_spans.for_generator() || borrow_spans.for_generator() {
374 let (desc_place, msg_place, msg_borrow, union_type_name) =
375 self.describe_place_for_conflicting_borrow(place, &issued_borrow.borrowed_place);
377 let explanation = self.explain_why_borrow_contains_point(location, issued_borrow, None);
378 let second_borrow_desc = if explanation.is_explained() {
384 // FIXME: supply non-"" `opt_via` when appropriate
385 let tcx = self.infcx.tcx;
386 let first_borrow_desc;
387 let mut err = match (
395 (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => {
396 first_borrow_desc = "mutable ";
397 tcx.cannot_reborrow_already_borrowed(
410 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
411 first_borrow_desc = "immutable ";
412 tcx.cannot_reborrow_already_borrowed(
426 (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
427 first_borrow_desc = "first ";
428 tcx.cannot_mutably_borrow_multiply(
439 (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
440 first_borrow_desc = "first ";
441 tcx.cannot_uniquely_borrow_by_two_closures(
450 (BorrowKind::Mut { .. }, _, _, BorrowKind::Shallow, _, _)
451 | (BorrowKind::Unique, _, _, BorrowKind::Shallow, _, _) => {
452 let mut err = tcx.cannot_mutate_in_match_guard(
459 borrow_spans.var_span_label(
462 "borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe()
469 (BorrowKind::Unique, _, _, _, _, _) => {
470 first_borrow_desc = "first ";
471 tcx.cannot_uniquely_borrow_by_one_closure(
484 (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
485 first_borrow_desc = "first ";
486 tcx.cannot_reborrow_already_uniquely_borrowed(
500 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
501 first_borrow_desc = "first ";
502 tcx.cannot_reborrow_already_uniquely_borrowed(
516 (BorrowKind::Shared, _, _, BorrowKind::Shared, _, _)
517 | (BorrowKind::Shared, _, _, BorrowKind::Shallow, _, _)
518 | (BorrowKind::Shallow, _, _, BorrowKind::Mut { .. }, _, _)
519 | (BorrowKind::Shallow, _, _, BorrowKind::Unique, _, _)
520 | (BorrowKind::Shallow, _, _, BorrowKind::Shared, _, _)
521 | (BorrowKind::Shallow, _, _, BorrowKind::Shallow, _, _) => unreachable!(),
524 if issued_spans == borrow_spans {
525 borrow_spans.var_span_label(
527 format!("borrows occur due to use of `{}`{}", desc_place, borrow_spans.describe()),
530 let borrow_place = &issued_borrow.borrowed_place;
531 let borrow_place_desc = self.describe_place(borrow_place)
532 .unwrap_or_else(|| "_".to_owned());
533 issued_spans.var_span_label(
536 "first borrow occurs due to use of `{}`{}",
538 issued_spans.describe(),
542 borrow_spans.var_span_label(
545 "second borrow occurs due to use of `{}`{}",
547 borrow_spans.describe(),
552 if union_type_name != "" {
554 "`{}` is a field of the union `{}`, so it overlaps the field `{}`",
555 msg_place, union_type_name, msg_borrow,
559 explanation.add_explanation_to_diagnostic(
570 /// Returns the description of the root place for a conflicting borrow and the full
571 /// descriptions of the places that caused the conflict.
573 /// In the simplest case, where there are no unions involved, if a mutable borrow of `x` is
574 /// attempted while a shared borrow is live, then this function will return:
578 /// In the simple union case, if a mutable borrow of a union field `x.z` is attempted while
579 /// a shared borrow of another field `x.y`, then this function will return:
581 /// ("x", "x.z", "x.y")
583 /// In the more complex union case, where the union is a field of a struct, then if a mutable
584 /// borrow of a union field in a struct `x.u.z` is attempted while a shared borrow of
585 /// another field `x.u.y`, then this function will return:
587 /// ("x.u", "x.u.z", "x.u.y")
589 /// This is used when creating error messages like below:
591 /// > cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
592 /// > mutable (via `a.u.s.b`) [E0502]
593 pub(super) fn describe_place_for_conflicting_borrow(
595 first_borrowed_place: &Place<'tcx>,
596 second_borrowed_place: &Place<'tcx>,
597 ) -> (String, String, String, String) {
598 // Define a small closure that we can use to check if the type of a place
600 let is_union = |place: &Place<'tcx>| -> bool {
601 place.ty(self.mir, self.infcx.tcx).ty
603 .map(|adt| adt.is_union())
607 // Start with an empty tuple, so we can use the functions on `Option` to reduce some
608 // code duplication (particularly around returning an empty description in the failure
612 // If we have a conflicting borrow of the same place, then we don't want to add
613 // an extraneous "via x.y" to our diagnostics, so filter out this case.
614 first_borrowed_place != second_borrowed_place
617 // We're going to want to traverse the first borrowed place to see if we can find
618 // field access to a union. If we find that, then we will keep the place of the
619 // union being accessed and the field that was being accessed so we can check the
620 // second borrowed place for the same union and a access to a different field.
621 let mut current = first_borrowed_place;
622 while let Place::Projection(box PlaceProjection { base, elem }) = current {
624 ProjectionElem::Field(field, _) if is_union(base) => {
625 return Some((base, field));
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.
635 let mut current = second_borrowed_place;
636 while let Place::Projection(box PlaceProjection { base, elem }) = current {
638 ProjectionElem::Field(field, _) if {
639 is_union(base) && field != target_field && base == target_base
641 let desc_base = self.describe_place(base)
642 .unwrap_or_else(|| "_".to_owned());
643 let desc_first = self.describe_place(first_borrowed_place)
644 .unwrap_or_else(|| "_".to_owned());
645 let desc_second = self.describe_place(second_borrowed_place)
646 .unwrap_or_else(|| "_".to_owned());
648 // Also compute the name of the union type, eg. `Foo` so we
649 // can add a helpful note with it.
650 let ty = base.ty(self.mir, self.infcx.tcx).ty;
652 return Some((desc_base, desc_first, desc_second, ty.to_string()));
660 // If we didn't find a field access into a union, or both places match, then
661 // only return the description of the first place.
662 let desc_place = self.describe_place(first_borrowed_place)
663 .unwrap_or_else(|| "_".to_owned());
664 (desc_place, "".to_string(), "".to_string(), "".to_string())
668 /// Reports StorageDeadOrDrop of `place` conflicts with `borrow`.
670 /// This means that some data referenced by `borrow` needs to live
671 /// past the point where the StorageDeadOrDrop of `place` occurs.
672 /// This is usually interpreted as meaning that `place` has too
673 /// short a lifetime. (But sometimes it is more useful to report
674 /// it as a more direct conflict between the execution of a
675 /// `Drop::drop` with an aliasing borrow.)
676 pub(super) fn report_borrowed_value_does_not_live_long_enough(
679 borrow: &BorrowData<'tcx>,
680 place_span: (&Place<'tcx>, Span),
681 kind: Option<WriteKind>,
684 "report_borrowed_value_does_not_live_long_enough(\
685 {:?}, {:?}, {:?}, {:?}\
687 location, borrow, place_span, kind
690 let drop_span = place_span.1;
691 let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
695 let borrow_spans = self.retrieve_borrow_spans(borrow);
696 let borrow_span = borrow_spans.var_or_use();
698 let proper_span = match *root_place {
699 Place::Base(PlaceBase::Local(local)) => self.mir.local_decls[local].source_info.span,
703 if self.access_place_error_reported
704 .contains(&(root_place.clone(), borrow_span))
707 "suppressing access_place error when borrow doesn't live long enough for {:?}",
713 self.access_place_error_reported
714 .insert((root_place.clone(), borrow_span));
716 if let StorageDeadOrDrop::Destructor(dropped_ty) =
717 self.classify_drop_access_kind(&borrow.borrowed_place)
719 // If a borrow of path `B` conflicts with drop of `D` (and
720 // we're not in the uninteresting case where `B` is a
721 // prefix of `D`), then report this as a more interesting
722 // destructor conflict.
723 if !borrow.borrowed_place.is_prefix_of(place_span.0) {
724 self.report_borrow_conflicts_with_destructor(
725 location, borrow, place_span, kind, dropped_ty,
731 let place_desc = self.describe_place(&borrow.borrowed_place);
733 let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
734 let explanation = self.explain_why_borrow_contains_point(location, &borrow, kind_place);
736 let err = match (place_desc, explanation) {
737 (Some(_), _) if self.is_place_thread_local(root_place) => {
738 self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
740 // If the outlives constraint comes from inside the closure,
745 // Box::new(|| y) as Box<Fn() -> &'static i32>
747 // then just use the normal error. The closure isn't escaping
748 // and `move` will not help here.
751 BorrowExplanation::MustBeValidFor {
752 category: category @ ConstraintCategory::Return,
761 BorrowExplanation::MustBeValidFor {
762 category: category @ ConstraintCategory::CallArgument,
768 ) if borrow_spans.for_closure() => self.report_escaping_closure_capture(
769 borrow_spans.args_or_use(),
774 &format!("`{}`", name),
778 BorrowExplanation::MustBeValidFor {
779 category: ConstraintCategory::Assignment,
781 region_name: RegionName {
782 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
788 ) => self.report_escaping_data(borrow_span, name, upvar_span, upvar_name, span),
789 (Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
797 (None, explanation) => self.report_temporary_value_does_not_live_long_enough(
807 err.buffer(&mut self.errors_buffer);
810 fn report_local_value_does_not_live_long_enough(
814 borrow: &BorrowData<'tcx>,
816 borrow_spans: UseSpans,
817 explanation: BorrowExplanation,
818 ) -> DiagnosticBuilder<'cx> {
820 "report_local_value_does_not_live_long_enough(\
821 {:?}, {:?}, {:?}, {:?}, {:?}\
823 location, name, borrow, drop_span, borrow_spans
826 let borrow_span = borrow_spans.var_or_use();
827 if let BorrowExplanation::MustBeValidFor {
834 if let Some(diag) = self.try_report_cannot_return_reference_to_local(
839 opt_place_desc.as_ref(),
845 let mut err = self.infcx.tcx.path_does_not_live_long_enough(
847 &format!("`{}`", name),
851 if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
852 let region_name = annotation.emit(self, &mut err);
856 format!("`{}` would have to be valid for `{}`...", name, region_name),
859 if let Some(fn_hir_id) = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id) {
863 "...but `{}` will be dropped here, when the function `{}` returns",
865 self.infcx.tcx.hir().name_by_hir_id(fn_hir_id),
870 "functions cannot return a borrow to data owned within the function's scope, \
871 functions can only return borrows to data passed as arguments",
874 "to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
875 references-and-borrowing.html#dangling-references>",
880 format!("...but `{}` dropped here while still borrowed", name),
884 if let BorrowExplanation::MustBeValidFor { .. } = explanation {
886 explanation.add_explanation_to_diagnostic(
895 err.span_label(borrow_span, "borrowed value does not live long enough");
898 format!("`{}` dropped here while still borrowed", name),
901 let within = if borrow_spans.for_generator() {
907 borrow_spans.args_span_label(
909 format!("value captured here{}", within),
912 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
918 fn report_borrow_conflicts_with_destructor(
921 borrow: &BorrowData<'tcx>,
922 (place, drop_span): (&Place<'tcx>, Span),
923 kind: Option<WriteKind>,
924 dropped_ty: Ty<'tcx>,
927 "report_borrow_conflicts_with_destructor(\
928 {:?}, {:?}, ({:?}, {:?}), {:?}\
930 location, borrow, place, drop_span, kind,
933 let borrow_spans = self.retrieve_borrow_spans(borrow);
934 let borrow_span = borrow_spans.var_or_use();
936 let mut err = self.infcx
938 .cannot_borrow_across_destructor(borrow_span, Origin::Mir);
940 let what_was_dropped = match self.describe_place(place) {
941 Some(name) => format!("`{}`", name.as_str()),
942 None => String::from("temporary value"),
945 let label = match self.describe_place(&borrow.borrowed_place) {
946 Some(borrowed) => format!(
947 "here, drop of {D} needs exclusive access to `{B}`, \
948 because the type `{T}` implements the `Drop` trait",
949 D = what_was_dropped,
954 "here is drop of {D}; whose type `{T}` implements the `Drop` trait",
955 D = what_was_dropped,
959 err.span_label(drop_span, label);
961 // Only give this note and suggestion if they could be relevant.
963 self.explain_why_borrow_contains_point(location, borrow, kind.map(|k| (k, place)));
965 BorrowExplanation::UsedLater { .. }
966 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
967 err.note("consider using a `let` binding to create a longer lived value");
972 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
974 err.buffer(&mut self.errors_buffer);
977 fn report_thread_local_value_does_not_live_long_enough(
981 ) -> DiagnosticBuilder<'cx> {
983 "report_thread_local_value_does_not_live_long_enough(\
986 drop_span, borrow_span
989 let mut err = self.infcx
991 .thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
995 "thread-local variables cannot be borrowed beyond the end of the function",
997 err.span_label(drop_span, "end of enclosing function is here");
1002 fn report_temporary_value_does_not_live_long_enough(
1005 borrow: &BorrowData<'tcx>,
1007 borrow_spans: UseSpans,
1009 explanation: BorrowExplanation,
1010 ) -> DiagnosticBuilder<'cx> {
1012 "report_temporary_value_does_not_live_long_enough(\
1013 {:?}, {:?}, {:?}, {:?}\
1015 location, borrow, drop_span, proper_span
1018 if let BorrowExplanation::MustBeValidFor {
1021 from_closure: false,
1024 if let Some(diag) = self.try_report_cannot_return_reference_to_local(
1035 let tcx = self.infcx.tcx;
1036 let mut err = tcx.temporary_value_borrowed_for_too_long(proper_span, Origin::Mir);
1039 "creates a temporary which is freed while still in use",
1043 "temporary value is freed at the end of this statement",
1047 BorrowExplanation::UsedLater(..)
1048 | BorrowExplanation::UsedLaterInLoop(..)
1049 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
1050 // Only give this note and suggestion if it could be relevant.
1051 err.note("consider using a `let` binding to create a longer lived value");
1055 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
1057 let within = if borrow_spans.for_generator() {
1063 borrow_spans.args_span_label(
1065 format!("value captured here{}", within),
1071 fn try_report_cannot_return_reference_to_local(
1073 borrow: &BorrowData<'tcx>,
1076 category: ConstraintCategory,
1077 opt_place_desc: Option<&String>,
1078 ) -> Option<DiagnosticBuilder<'cx>> {
1079 let tcx = self.infcx.tcx;
1081 let return_kind = match category {
1082 ConstraintCategory::Return => "return",
1083 ConstraintCategory::Yield => "yield",
1087 // FIXME use a better heuristic than Spans
1088 let reference_desc = if return_span == self.mir.source_info(borrow.reserve_location).span {
1094 let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
1095 let local_kind = match borrow.borrowed_place {
1096 Place::Base(PlaceBase::Local(local)) => {
1097 match self.mir.local_kind(local) {
1098 LocalKind::ReturnPointer
1099 | LocalKind::Temp => bug!("temporary or return pointer with a name"),
1100 LocalKind::Var => "local variable ",
1102 if !self.upvars.is_empty()
1103 && local == Local::new(1) => {
1104 "variable captured by `move` "
1107 "function parameter "
1114 format!("{}`{}`", local_kind, place_desc),
1115 format!("`{}` is borrowed here", place_desc),
1118 let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
1121 let local = if let Place::Base(PlaceBase::Local(local)) = *root_place {
1124 bug!("try_report_cannot_return_reference_to_local: not a local")
1126 match self.mir.local_kind(local) {
1127 LocalKind::ReturnPointer | LocalKind::Temp => {
1129 "temporary value".to_string(),
1130 "temporary value created here".to_string(),
1135 "function parameter".to_string(),
1136 "function parameter borrowed here".to_string(),
1139 LocalKind::Var => bug!("local variable without a name"),
1143 let mut err = tcx.cannot_return_reference_to_local(
1151 if return_span != borrow_span {
1152 err.span_label(borrow_span, note);
1158 fn report_escaping_closure_capture(
1162 fr_name: &RegionName,
1163 category: ConstraintCategory,
1164 constraint_span: Span,
1166 ) -> DiagnosticBuilder<'cx> {
1167 let tcx = self.infcx.tcx;
1169 let mut err = tcx.cannot_capture_in_long_lived_closure(
1176 let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
1177 Ok(string) => format!("move {}", string),
1178 Err(_) => "move |<args>| <body>".to_string()
1181 err.span_suggestion(
1183 &format!("to force the closure to take ownership of {} (and any \
1184 other referenced variables), use the `move` keyword",
1187 Applicability::MachineApplicable,
1191 ConstraintCategory::Return => {
1192 err.span_note(constraint_span, "closure is returned here");
1194 ConstraintCategory::CallArgument => {
1195 fr_name.highlight_region_name(&mut err);
1198 &format!("function requires argument type to outlive `{}`", fr_name),
1201 _ => bug!("report_escaping_closure_capture called with unexpected constraint \
1202 category: `{:?}`", category),
1207 fn report_escaping_data(
1210 name: &Option<String>,
1214 ) -> DiagnosticBuilder<'cx> {
1215 let tcx = self.infcx.tcx;
1217 let escapes_from = if tcx.is_closure(self.mir_def_id) {
1218 let tables = tcx.typeck_tables_of(self.mir_def_id);
1219 let mir_hir_id = tcx.hir().def_index_to_hir_id(self.mir_def_id.index);
1220 match tables.node_type(mir_hir_id).sty {
1221 ty::Closure(..) => "closure",
1222 ty::Generator(..) => "generator",
1223 _ => bug!("Closure body doesn't have a closure or generator type"),
1229 let mut err = tcx.borrowed_data_escapes_closure(escape_span, escapes_from, Origin::Mir);
1234 "`{}` is declared here, outside of the {} body",
1235 upvar_name, escapes_from
1242 "borrow is only valid in the {} body",
1247 if let Some(name) = name {
1250 format!("reference to `{}` escapes the {} body here", name, escapes_from),
1255 format!("reference escapes the {} body here", escapes_from),
1262 fn get_moved_indexes(&mut self, location: Location, mpi: MovePathIndex) -> Vec<MoveSite> {
1265 let mut stack = Vec::new();
1266 stack.extend(mir.predecessor_locations(location).map(|predecessor| {
1267 let is_back_edge = location.dominates(predecessor, &self.dominators);
1268 (predecessor, is_back_edge)
1271 let mut visited = FxHashSet::default();
1272 let mut result = vec![];
1274 'dfs: while let Some((location, is_back_edge)) = stack.pop() {
1276 "report_use_of_moved_or_uninitialized: (current_location={:?}, back_edge={})",
1277 location, is_back_edge
1280 if !visited.insert(location) {
1285 let stmt_kind = mir[location.block]
1287 .get(location.statement_index)
1289 if let Some(StatementKind::StorageDead(..)) = stmt_kind {
1290 // this analysis only tries to find moves explicitly
1291 // written by the user, so we ignore the move-outs
1292 // created by `StorageDead` and at the beginning
1295 // If we are found a use of a.b.c which was in error, then we want to look for
1296 // moves not only of a.b.c but also a.b and a.
1298 // Note that the moves data already includes "parent" paths, so we don't have to
1299 // worry about the other case: that is, if there is a move of a.b.c, it is already
1300 // marked as a move of a.b and a as well, so we will generate the correct errors
1302 let mut mpis = vec![mpi];
1303 let move_paths = &self.move_data.move_paths;
1304 mpis.extend(move_paths[mpi].parents(move_paths));
1306 for moi in &self.move_data.loc_map[location] {
1307 debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
1308 if mpis.contains(&self.move_data.moves[*moi].path) {
1309 debug!("report_use_of_moved_or_uninitialized: found");
1310 result.push(MoveSite {
1312 traversed_back_edge: is_back_edge,
1315 // Strictly speaking, we could continue our DFS here. There may be
1316 // other moves that can reach the point of error. But it is kind of
1317 // confusing to highlight them.
1325 // drop(a); // <-- current point of error
1328 // Because we stop the DFS here, we only highlight `let c = a`,
1329 // and not `let b = a`. We will of course also report an error at
1330 // `let c = a` which highlights `let b = a` as the move.
1337 let mut any_match = false;
1338 drop_flag_effects::for_location_inits(
1353 stack.extend(mir.predecessor_locations(location).map(|predecessor| {
1354 let back_edge = location.dominates(predecessor, &self.dominators);
1355 (predecessor, is_back_edge || back_edge)
1362 pub(super) fn report_illegal_mutation_of_borrowed(
1365 (place, span): (&Place<'tcx>, Span),
1366 loan: &BorrowData<'tcx>,
1368 let loan_spans = self.retrieve_borrow_spans(loan);
1369 let loan_span = loan_spans.args_or_use();
1371 let tcx = self.infcx.tcx;
1372 if loan.kind == BorrowKind::Shallow {
1373 let mut err = tcx.cannot_mutate_in_match_guard(
1376 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1380 loan_spans.var_span_label(
1382 format!("borrow occurs due to use{}", loan_spans.describe()),
1385 err.buffer(&mut self.errors_buffer);
1390 let mut err = tcx.cannot_assign_to_borrowed(
1393 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1397 loan_spans.var_span_label(
1399 format!("borrow occurs due to use{}", loan_spans.describe()),
1402 self.explain_why_borrow_contains_point(location, loan, None)
1403 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
1405 err.buffer(&mut self.errors_buffer);
1408 /// Reports an illegal reassignment; for example, an assignment to
1409 /// (part of) a non-`mut` local that occurs potentially after that
1410 /// local has already been initialized. `place` is the path being
1411 /// assigned; `err_place` is a place providing a reason why
1412 /// `place` is not mutable (e.g., the non-`mut` local `x` in an
1413 /// assignment to `x.f`).
1414 pub(super) fn report_illegal_reassignment(
1416 _location: Location,
1417 (place, span): (&Place<'tcx>, Span),
1418 assigned_span: Span,
1419 err_place: &Place<'tcx>,
1421 let (from_arg, local_decl) = if let Place::Base(PlaceBase::Local(local)) = *err_place {
1422 if let LocalKind::Arg = self.mir.local_kind(local) {
1423 (true, Some(&self.mir.local_decls[local]))
1425 (false, Some(&self.mir.local_decls[local]))
1431 // If root local is initialized immediately (everything apart from let
1432 // PATTERN;) then make the error refer to that local, rather than the
1433 // place being assigned later.
1434 let (place_description, assigned_span) = match local_decl {
1436 is_user_variable: Some(ClearCrossCrate::Clear),
1441 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
1442 opt_match_place: None,
1448 is_user_variable: None,
1451 | None => (self.describe_place(place), assigned_span),
1452 Some(decl) => (self.describe_place(err_place), decl.source_info.span),
1455 let mut err = self.infcx.tcx.cannot_reassign_immutable(
1457 place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
1461 let msg = if from_arg {
1462 "cannot assign to immutable argument"
1464 "cannot assign twice to immutable variable"
1466 if span != assigned_span {
1468 let value_msg = match place_description {
1469 Some(name) => format!("`{}`", name),
1470 None => "value".to_owned(),
1472 err.span_label(assigned_span, format!("first assignment to {}", value_msg));
1475 if let Some(decl) = local_decl {
1476 if let Some(name) = decl.name {
1477 if decl.can_be_made_mutable() {
1478 err.span_suggestion(
1479 decl.source_info.span,
1480 "make this binding mutable",
1481 format!("mut {}", name),
1482 Applicability::MachineApplicable,
1487 err.span_label(span, msg);
1488 err.buffer(&mut self.errors_buffer);
1491 fn classify_drop_access_kind(&self, place: &Place<'tcx>) -> StorageDeadOrDrop<'tcx> {
1492 let tcx = self.infcx.tcx;
1494 Place::Base(PlaceBase::Local(_)) |
1495 Place::Base(PlaceBase::Static(_)) => {
1496 StorageDeadOrDrop::LocalStorageDead
1498 Place::Projection(box PlaceProjection { base, elem }) => {
1499 let base_access = self.classify_drop_access_kind(base);
1501 ProjectionElem::Deref => match base_access {
1502 StorageDeadOrDrop::LocalStorageDead
1503 | StorageDeadOrDrop::BoxedStorageDead => {
1505 base.ty(self.mir, tcx).ty.is_box(),
1506 "Drop of value behind a reference or raw pointer"
1508 StorageDeadOrDrop::BoxedStorageDead
1510 StorageDeadOrDrop::Destructor(_) => base_access,
1512 ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1513 let base_ty = base.ty(self.mir, tcx).ty;
1515 ty::Adt(def, _) if def.has_dtor(tcx) => {
1516 // Report the outermost adt with a destructor
1518 StorageDeadOrDrop::Destructor(_) => base_access,
1519 StorageDeadOrDrop::LocalStorageDead
1520 | StorageDeadOrDrop::BoxedStorageDead => {
1521 StorageDeadOrDrop::Destructor(base_ty)
1529 ProjectionElem::ConstantIndex { .. }
1530 | ProjectionElem::Subslice { .. }
1531 | ProjectionElem::Index(_) => base_access,
1537 /// Annotate argument and return type of function and closure with (synthesized) lifetime for
1538 /// borrow of local value that does not live long enough.
1539 fn annotate_argument_and_return_for_borrow(
1541 borrow: &BorrowData<'tcx>,
1542 ) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
1543 // Define a fallback for when we can't match a closure.
1545 let is_closure = self.infcx.tcx.is_closure(self.mir_def_id);
1549 let ty = self.infcx.tcx.type_of(self.mir_def_id);
1551 ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
1553 self.infcx.tcx.fn_sig(self.mir_def_id),
1560 // In order to determine whether we need to annotate, we need to check whether the reserve
1561 // place was an assignment into a temporary.
1563 // If it was, we check whether or not that temporary is eventually assigned into the return
1564 // place. If it was, we can add annotations about the function's return type and arguments
1565 // and it'll make sense.
1566 let location = borrow.reserve_location;
1568 "annotate_argument_and_return_for_borrow: location={:?}",
1571 if let Some(&Statement { kind: StatementKind::Assign(ref reservation, _), ..})
1572 = &self.mir[location.block].statements.get(location.statement_index)
1575 "annotate_argument_and_return_for_borrow: reservation={:?}",
1578 // Check that the initial assignment of the reserve location is into a temporary.
1579 let mut target = *match reservation {
1580 Place::Base(PlaceBase::Local(local))
1581 if self.mir.local_kind(*local) == LocalKind::Temp => local,
1585 // Next, look through the rest of the block, checking if we are assigning the
1586 // `target` (that is, the place that contains our borrow) to anything.
1587 let mut annotated_closure = None;
1588 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
1590 "annotate_argument_and_return_for_borrow: target={:?} stmt={:?}",
1593 if let StatementKind::Assign(
1594 Place::Base(PlaceBase::Local(assigned_to)),
1598 "annotate_argument_and_return_for_borrow: assigned_to={:?} \
1602 // Check if our `target` was captured by a closure.
1603 if let Rvalue::Aggregate(
1604 box AggregateKind::Closure(def_id, substs),
1608 for operand in operands {
1609 let assigned_from = match operand {
1610 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1616 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1620 // Find the local from the operand.
1621 let assigned_from_local = match assigned_from.local() {
1622 Some(local) => local,
1626 if assigned_from_local != target {
1630 // If a closure captured our `target` and then assigned
1631 // into a place then we should annotate the closure in
1632 // case it ends up being assigned into the return place.
1633 annotated_closure = self.annotate_fn_sig(
1635 self.infcx.closure_sig(*def_id, *substs),
1638 "annotate_argument_and_return_for_borrow: \
1639 annotated_closure={:?} assigned_from_local={:?} \
1641 annotated_closure, assigned_from_local, assigned_to
1644 if *assigned_to == mir::RETURN_PLACE {
1645 // If it was assigned directly into the return place, then
1647 return annotated_closure;
1649 // Otherwise, update the target.
1650 target = *assigned_to;
1654 // If none of our closure's operands matched, then skip to the next
1659 // Otherwise, look at other types of assignment.
1660 let assigned_from = match rvalue {
1661 Rvalue::Ref(_, _, assigned_from) => assigned_from,
1662 Rvalue::Use(operand) => match operand {
1663 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1671 "annotate_argument_and_return_for_borrow: \
1672 assigned_from={:?}",
1676 // Find the local from the rvalue.
1677 let assigned_from_local = match assigned_from.local() {
1678 Some(local) => local,
1682 "annotate_argument_and_return_for_borrow: \
1683 assigned_from_local={:?}",
1684 assigned_from_local,
1687 // Check if our local matches the target - if so, we've assigned our
1688 // borrow to a new place.
1689 if assigned_from_local != target {
1693 // If we assigned our `target` into a new place, then we should
1694 // check if it was the return place.
1696 "annotate_argument_and_return_for_borrow: \
1697 assigned_from_local={:?} assigned_to={:?}",
1698 assigned_from_local, assigned_to
1700 if *assigned_to == mir::RETURN_PLACE {
1701 // If it was then return the annotated closure if there was one,
1702 // else, annotate this function.
1703 return annotated_closure.or_else(fallback);
1706 // If we didn't assign into the return place, then we just update
1708 target = *assigned_to;
1712 // Check the terminator if we didn't find anything in the statements.
1713 let terminator = &self.mir[location.block].terminator();
1715 "annotate_argument_and_return_for_borrow: target={:?} terminator={:?}",
1718 if let TerminatorKind::Call {
1719 destination: Some((Place::Base(PlaceBase::Local(assigned_to)), _)),
1722 } = &terminator.kind
1725 "annotate_argument_and_return_for_borrow: assigned_to={:?} args={:?}",
1728 for operand in args {
1729 let assigned_from = match operand {
1730 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1736 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1740 if let Some(assigned_from_local) = assigned_from.local() {
1742 "annotate_argument_and_return_for_borrow: assigned_from_local={:?}",
1743 assigned_from_local,
1746 if *assigned_to == mir::RETURN_PLACE && assigned_from_local == target {
1747 return annotated_closure.or_else(fallback);
1754 // If we haven't found an assignment into the return place, then we need not add
1756 debug!("annotate_argument_and_return_for_borrow: none found");
1760 /// Annotate the first argument and return type of a function signature if they are
1765 sig: ty::PolyFnSig<'tcx>,
1766 ) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
1767 debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
1768 let is_closure = self.infcx.tcx.is_closure(did);
1769 let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did)?;
1770 let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
1772 // We need to work out which arguments to highlight. We do this by looking
1773 // at the return type, where there are three cases:
1775 // 1. If there are named arguments, then we should highlight the return type and
1776 // highlight any of the arguments that are also references with that lifetime.
1777 // If there are no arguments that have the same lifetime as the return type,
1778 // then don't highlight anything.
1779 // 2. The return type is a reference with an anonymous lifetime. If this is
1780 // the case, then we can take advantage of (and teach) the lifetime elision
1783 // We know that an error is being reported. So the arguments and return type
1784 // must satisfy the elision rules. Therefore, if there is a single argument
1785 // then that means the return type and first (and only) argument have the same
1786 // lifetime and the borrow isn't meeting that, we can highlight the argument
1789 // If there are multiple arguments then the first argument must be self (else
1790 // it would not satisfy the elision rules), so we can highlight self and the
1792 // 3. The return type is not a reference. In this case, we don't highlight
1794 let return_ty = sig.output();
1795 match return_ty.skip_binder().sty {
1796 ty::Ref(return_region, _, _) if return_region.has_name() && !is_closure => {
1797 // This is case 1 from above, return type is a named reference so we need to
1798 // search for relevant arguments.
1799 let mut arguments = Vec::new();
1800 for (index, argument) in sig.inputs().skip_binder().iter().enumerate() {
1801 if let ty::Ref(argument_region, _, _) = argument.sty {
1802 if argument_region == return_region {
1803 // Need to use the `rustc::ty` types to compare against the
1804 // `return_region`. Then use the `rustc::hir` type to get only
1805 // the lifetime span.
1806 if let hir::TyKind::Rptr(lifetime, _) = &fn_decl.inputs[index].node {
1807 // With access to the lifetime, we can get
1809 arguments.push((*argument, lifetime.span));
1811 bug!("ty type is a ref but hir type is not");
1817 // We need to have arguments. This shouldn't happen, but it's worth checking.
1818 if arguments.is_empty() {
1822 // We use a mix of the HIR and the Ty types to get information
1823 // as the HIR doesn't have full types for closure arguments.
1824 let return_ty = *sig.output().skip_binder();
1825 let mut return_span = fn_decl.output.span();
1826 if let hir::FunctionRetTy::Return(ty) = fn_decl.output {
1827 if let hir::TyKind::Rptr(lifetime, _) = ty.into_inner().node {
1828 return_span = lifetime.span;
1832 Some(AnnotatedBorrowFnSignature::NamedFunction {
1838 ty::Ref(_, _, _) if is_closure => {
1839 // This is case 2 from above but only for closures, return type is anonymous
1840 // reference so we select
1841 // the first argument.
1842 let argument_span = fn_decl.inputs.first()?.span;
1843 let argument_ty = sig.inputs().skip_binder().first()?;
1845 // Closure arguments are wrapped in a tuple, so we need to get the first
1847 if let ty::Tuple(elems) = argument_ty.sty {
1848 let argument_ty = elems.first()?.expect_ty();
1849 if let ty::Ref(_, _, _) = argument_ty.sty {
1850 return Some(AnnotatedBorrowFnSignature::Closure {
1859 ty::Ref(_, _, _) => {
1860 // This is also case 2 from above but for functions, return type is still an
1861 // anonymous reference so we select the first argument.
1862 let argument_span = fn_decl.inputs.first()?.span;
1863 let argument_ty = sig.inputs().skip_binder().first()?;
1865 let return_span = fn_decl.output.span();
1866 let return_ty = *sig.output().skip_binder();
1868 // We expect the first argument to be a reference.
1869 match argument_ty.sty {
1870 ty::Ref(_, _, _) => {}
1874 Some(AnnotatedBorrowFnSignature::AnonymousFunction {
1882 // This is case 3 from above, return type is not a reference so don't highlight
1891 enum AnnotatedBorrowFnSignature<'tcx> {
1893 arguments: Vec<(Ty<'tcx>, Span)>,
1894 return_ty: Ty<'tcx>,
1898 argument_ty: Ty<'tcx>,
1899 argument_span: Span,
1900 return_ty: Ty<'tcx>,
1904 argument_ty: Ty<'tcx>,
1905 argument_span: Span,
1909 impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
1910 /// Annotate the provided diagnostic with information about borrow from the fn signature that
1914 cx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
1915 diag: &mut DiagnosticBuilder<'_>,
1918 AnnotatedBorrowFnSignature::Closure {
1924 format!("has type `{}`", cx.get_name_for_ty(argument_ty, 0)),
1927 cx.get_region_name_for_ty(argument_ty, 0)
1929 AnnotatedBorrowFnSignature::AnonymousFunction {
1935 let argument_ty_name = cx.get_name_for_ty(argument_ty, 0);
1936 diag.span_label(*argument_span, format!("has type `{}`", argument_ty_name));
1938 let return_ty_name = cx.get_name_for_ty(return_ty, 0);
1939 let types_equal = return_ty_name == argument_ty_name;
1944 if types_equal { "also " } else { "" },
1950 "argument and return type have the same lifetime due to lifetime elision rules",
1953 "to learn more, visit <https://doc.rust-lang.org/book/ch10-03-\
1954 lifetime-syntax.html#lifetime-elision>",
1957 cx.get_region_name_for_ty(return_ty, 0)
1959 AnnotatedBorrowFnSignature::NamedFunction {
1964 // Region of return type and arguments checked to be the same earlier.
1965 let region_name = cx.get_region_name_for_ty(return_ty, 0);
1966 for (_, argument_span) in arguments {
1967 diag.span_label(*argument_span, format!("has lifetime `{}`", region_name));
1972 format!("also has lifetime `{}`", region_name,),
1976 "use data from the highlighted arguments which match the `{}` lifetime of \