1 use crate::borrow_check::nll::explain_borrow::BorrowExplanation;
2 use crate::borrow_check::nll::region_infer::{RegionName, RegionNameSource};
3 use crate::borrow_check::prefixes::IsPrefixOf;
4 use crate::borrow_check::WriteKind;
6 use rustc::hir::def::Namespace;
7 use rustc::hir::def_id::DefId;
8 use rustc::middle::region::ScopeTree;
10 self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, Constant,
11 ConstraintCategory, Field, Local, LocalDecl, LocalKind, Location, Operand,
12 Place, PlaceBase, PlaceProjection, ProjectionElem, Rvalue, Statement, StatementKind,
13 Static, StaticKind, TerminatorKind, VarBindingForm,
15 use rustc::ty::{self, DefIdTree, Ty};
16 use rustc::ty::layout::VariantIdx;
17 use rustc::ty::print::Print;
18 use rustc_data_structures::fx::FxHashSet;
19 use rustc_data_structures::indexed_vec::Idx;
20 use rustc_errors::{Applicability, DiagnosticBuilder};
22 use syntax::source_map::CompilerDesugaringKind;
24 use super::borrow_set::BorrowData;
25 use super::{Context, MirBorrowckCtxt};
26 use super::{InitializationRequiringAction, PrefixSet};
27 use crate::dataflow::drop_flag_effects;
28 use crate::dataflow::indexes::{MovePathIndex, MoveOutIndex};
29 use crate::util::borrowck_errors::{BorrowckErrors, Origin};
33 /// Index of the "move out" that we found. The `MoveData` can
34 /// then tell us where the move occurred.
37 /// `true` if we traversed a back edge while walking from the point
38 /// of error to the move site.
39 traversed_back_edge: bool
42 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
43 pub(super) fn report_use_of_moved_or_uninitialized(
46 desired_action: InitializationRequiringAction,
47 (moved_place, used_place, span): (&Place<'tcx>, &Place<'tcx>, Span),
51 "report_use_of_moved_or_uninitialized: context={:?} desired_action={:?} \
52 moved_place={:?} used_place={:?} span={:?} mpi={:?}",
53 context, desired_action, moved_place, used_place, span, mpi
56 let use_spans = self.move_spans(moved_place, context.loc)
57 .or_else(|| self.borrow_spans(span, context.loc));
58 let span = use_spans.args_or_use();
60 let move_site_vec = self.get_moved_indexes(context, mpi);
62 "report_use_of_moved_or_uninitialized: move_site_vec={:?}",
65 let move_out_indices: Vec<_> = move_site_vec
67 .map(|move_site| move_site.moi)
70 if move_out_indices.is_empty() {
71 let root_place = self.prefixes(&used_place, PrefixSet::All).last().unwrap();
73 if self.uninitialized_error_reported.contains(root_place) {
75 "report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
81 self.uninitialized_error_reported.insert(root_place.clone());
83 let item_msg = match self.describe_place_with_options(used_place,
84 IncludingDowncast(true)) {
85 Some(name) => format!("`{}`", name),
86 None => "value".to_owned(),
88 let mut err = self.infcx.tcx.cannot_act_on_uninitialized_variable(
90 desired_action.as_noun(),
91 &self.describe_place_with_options(moved_place, IncludingDowncast(true))
92 .unwrap_or_else(|| "_".to_owned()),
95 err.span_label(span, format!("use of possibly uninitialized {}", item_msg));
97 use_spans.var_span_label(
99 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
102 err.buffer(&mut self.errors_buffer);
104 if let Some((reported_place, _)) = self.move_error_reported.get(&move_out_indices) {
105 if self.prefixes(&reported_place, PrefixSet::All)
106 .any(|p| p == used_place)
109 "report_use_of_moved_or_uninitialized place: error suppressed \
117 let msg = ""; //FIXME: add "partially " or "collaterally "
119 let mut err = self.infcx.tcx.cannot_act_on_moved_value(
121 desired_action.as_noun(),
123 self.describe_place_with_options(&moved_place, IncludingDowncast(true)),
127 self.add_moved_or_invoked_closure_note(
133 let mut is_loop_move = false;
134 let is_partial_move = move_site_vec.iter().any(|move_site| {
135 let move_out = self.move_data.moves[(*move_site).moi];
136 let moved_place = &self.move_data.move_paths[move_out.path].place;
137 used_place != moved_place && used_place.is_prefix_of(moved_place)
139 for move_site in &move_site_vec {
140 let move_out = self.move_data.moves[(*move_site).moi];
141 let moved_place = &self.move_data.move_paths[move_out.path].place;
143 let move_spans = self.move_spans(moved_place, move_out.source);
144 let move_span = move_spans.args_or_use();
146 let move_msg = if move_spans.for_closure() {
152 if span == move_span {
155 format!("value moved{} here, in previous iteration of loop", move_msg),
157 if Some(CompilerDesugaringKind::ForLoop) == span.compiler_desugaring_kind() {
158 if let Ok(snippet) = self.infcx.tcx.sess.source_map()
159 .span_to_snippet(span)
163 "consider borrowing this to avoid moving it into the for loop",
164 format!("&{}", snippet),
165 Applicability::MaybeIncorrect,
170 } else if move_site.traversed_back_edge {
174 "value moved{} here, in previous iteration of loop",
179 err.span_label(move_span, format!("value moved{} here", move_msg));
180 move_spans.var_span_label(
182 format!("variable moved due to use{}", move_spans.describe()),
187 use_spans.var_span_label(
189 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
197 desired_action.as_verb_in_past_tense(),
198 if is_partial_move { "after partial move" } else { "after move" },
203 let ty = used_place.ty(self.mir, self.infcx.tcx).ty;
204 let needs_note = match ty.sty {
205 ty::Closure(id, _) => {
206 let tables = self.infcx.tcx.typeck_tables_of(id);
207 let hir_id = self.infcx.tcx.hir().as_local_hir_id(id).unwrap();
209 tables.closure_kind_origins().get(hir_id).is_none()
215 let mpi = self.move_data.moves[move_out_indices[0]].path;
216 let place = &self.move_data.move_paths[mpi].place;
218 let ty = place.ty(self.mir, self.infcx.tcx).ty;
219 let opt_name = self.describe_place_with_options(place, IncludingDowncast(true));
220 let note_msg = match opt_name {
221 Some(ref name) => format!("`{}`", name),
222 None => "value".to_owned(),
224 if let ty::Param(param_ty) = ty.sty {
225 let tcx = self.infcx.tcx;
226 let generics = tcx.generics_of(self.mir_def_id);
227 let def_id = generics.type_param(¶m_ty, tcx).def_id;
228 if let Some(sp) = tcx.hir().span_if_local(def_id) {
231 "consider adding a `Copy` constraint to this type argument",
235 if let Place::Base(PlaceBase::Local(local)) = place {
236 let decl = &self.mir.local_decls[*local];
238 decl.source_info.span,
240 "move occurs because {} has type `{}`, \
241 which does not implement the `Copy` trait",
246 "move occurs because {} has type `{}`, \
247 which does not implement the `Copy` trait",
253 if let Some((_, mut old_err)) = self.move_error_reported
254 .insert(move_out_indices, (used_place.clone(), err))
256 // Cancel the old error so it doesn't ICE.
262 pub(super) fn report_move_out_while_borrowed(
265 (place, span): (&Place<'tcx>, Span),
266 borrow: &BorrowData<'tcx>,
269 "report_move_out_while_borrowed: context={:?} place={:?} span={:?} borrow={:?}",
270 context, place, span, borrow
272 let tcx = self.infcx.tcx;
273 let value_msg = match self.describe_place(place) {
274 Some(name) => format!("`{}`", name),
275 None => "value".to_owned(),
277 let borrow_msg = match self.describe_place(&borrow.borrowed_place) {
278 Some(name) => format!("`{}`", name),
279 None => "value".to_owned(),
282 let borrow_spans = self.retrieve_borrow_spans(borrow);
283 let borrow_span = borrow_spans.args_or_use();
285 let move_spans = self.move_spans(place, context.loc);
286 let span = move_spans.args_or_use();
288 let mut err = tcx.cannot_move_when_borrowed(
290 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
293 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
294 err.span_label(span, format!("move out of {} occurs here", value_msg));
296 borrow_spans.var_span_label(
298 format!("borrow occurs due to use{}", borrow_spans.describe())
301 move_spans.var_span_label(
303 format!("move occurs due to use{}", move_spans.describe())
306 self.explain_why_borrow_contains_point(
310 ).add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", Some(borrow_span));
311 err.buffer(&mut self.errors_buffer);
314 pub(super) fn report_use_while_mutably_borrowed(
317 (place, _span): (&Place<'tcx>, Span),
318 borrow: &BorrowData<'tcx>,
319 ) -> DiagnosticBuilder<'cx> {
320 let tcx = self.infcx.tcx;
322 let borrow_spans = self.retrieve_borrow_spans(borrow);
323 let borrow_span = borrow_spans.args_or_use();
325 // Conflicting borrows are reported separately, so only check for move
327 let use_spans = self.move_spans(place, context.loc);
328 let span = use_spans.var_or_use();
330 let mut err = tcx.cannot_use_when_mutably_borrowed(
332 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
334 &self.describe_place(&borrow.borrowed_place)
335 .unwrap_or_else(|| "_".to_owned()),
339 borrow_spans.var_span_label(&mut err, {
340 let place = &borrow.borrowed_place;
341 let desc_place = self.describe_place(place).unwrap_or_else(|| "_".to_owned());
343 format!("borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe())
346 self.explain_why_borrow_contains_point(context, borrow, None)
347 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
351 pub(super) fn report_conflicting_borrow(
354 (place, span): (&Place<'tcx>, Span),
355 gen_borrow_kind: BorrowKind,
356 issued_borrow: &BorrowData<'tcx>,
357 ) -> DiagnosticBuilder<'cx> {
358 let issued_spans = self.retrieve_borrow_spans(issued_borrow);
359 let issued_span = issued_spans.args_or_use();
361 let borrow_spans = self.borrow_spans(span, context.loc);
362 let span = borrow_spans.args_or_use();
364 let container_name = if issued_spans.for_generator() || borrow_spans.for_generator() {
370 let (desc_place, msg_place, msg_borrow, union_type_name) =
371 self.describe_place_for_conflicting_borrow(place, &issued_borrow.borrowed_place);
373 let explanation = self.explain_why_borrow_contains_point(context, issued_borrow, None);
374 let second_borrow_desc = if explanation.is_explained() {
380 // FIXME: supply non-"" `opt_via` when appropriate
381 let tcx = self.infcx.tcx;
382 let first_borrow_desc;
383 let mut err = match (
391 (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => {
392 first_borrow_desc = "mutable ";
393 tcx.cannot_reborrow_already_borrowed(
406 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
407 first_borrow_desc = "immutable ";
408 tcx.cannot_reborrow_already_borrowed(
422 (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
423 first_borrow_desc = "first ";
424 tcx.cannot_mutably_borrow_multiply(
435 (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
436 first_borrow_desc = "first ";
437 tcx.cannot_uniquely_borrow_by_two_closures(
446 (BorrowKind::Mut { .. }, _, _, BorrowKind::Shallow, _, _)
447 | (BorrowKind::Unique, _, _, BorrowKind::Shallow, _, _) => {
448 let mut err = tcx.cannot_mutate_in_match_guard(
455 borrow_spans.var_span_label(
458 "borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe()
465 (BorrowKind::Unique, _, _, _, _, _) => {
466 first_borrow_desc = "first ";
467 tcx.cannot_uniquely_borrow_by_one_closure(
480 (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
481 first_borrow_desc = "first ";
482 tcx.cannot_reborrow_already_uniquely_borrowed(
496 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
497 first_borrow_desc = "first ";
498 tcx.cannot_reborrow_already_uniquely_borrowed(
512 (BorrowKind::Shared, _, _, BorrowKind::Shared, _, _)
513 | (BorrowKind::Shared, _, _, BorrowKind::Shallow, _, _)
514 | (BorrowKind::Shallow, _, _, BorrowKind::Mut { .. }, _, _)
515 | (BorrowKind::Shallow, _, _, BorrowKind::Unique, _, _)
516 | (BorrowKind::Shallow, _, _, BorrowKind::Shared, _, _)
517 | (BorrowKind::Shallow, _, _, BorrowKind::Shallow, _, _) => unreachable!(),
520 if issued_spans == borrow_spans {
521 borrow_spans.var_span_label(
523 format!("borrows occur due to use of `{}`{}", desc_place, borrow_spans.describe()),
526 let borrow_place = &issued_borrow.borrowed_place;
527 let borrow_place_desc = self.describe_place(borrow_place)
528 .unwrap_or_else(|| "_".to_owned());
529 issued_spans.var_span_label(
532 "first borrow occurs due to use of `{}`{}",
534 issued_spans.describe(),
538 borrow_spans.var_span_label(
541 "second borrow occurs due to use of `{}`{}",
543 borrow_spans.describe(),
548 if union_type_name != "" {
550 "`{}` is a field of the union `{}`, so it overlaps the field `{}`",
551 msg_place, union_type_name, msg_borrow,
555 explanation.add_explanation_to_diagnostic(
566 /// Returns the description of the root place for a conflicting borrow and the full
567 /// descriptions of the places that caused the conflict.
569 /// In the simplest case, where there are no unions involved, if a mutable borrow of `x` is
570 /// attempted while a shared borrow is live, then this function will return:
574 /// In the simple union case, if a mutable borrow of a union field `x.z` is attempted while
575 /// a shared borrow of another field `x.y`, then this function will return:
577 /// ("x", "x.z", "x.y")
579 /// In the more complex union case, where the union is a field of a struct, then if a mutable
580 /// borrow of a union field in a struct `x.u.z` is attempted while a shared borrow of
581 /// another field `x.u.y`, then this function will return:
583 /// ("x.u", "x.u.z", "x.u.y")
585 /// This is used when creating error messages like below:
587 /// > cannot borrow `a.u` (via `a.u.z.c`) as immutable because it is also borrowed as
588 /// > mutable (via `a.u.s.b`) [E0502]
589 pub(super) fn describe_place_for_conflicting_borrow(
591 first_borrowed_place: &Place<'tcx>,
592 second_borrowed_place: &Place<'tcx>,
593 ) -> (String, String, String, String) {
594 // Define a small closure that we can use to check if the type of a place
596 let is_union = |place: &Place<'tcx>| -> bool {
597 place.ty(self.mir, self.infcx.tcx).ty
599 .map(|adt| adt.is_union())
603 // Start with an empty tuple, so we can use the functions on `Option` to reduce some
604 // code duplication (particularly around returning an empty description in the failure
608 // If we have a conflicting borrow of the same place, then we don't want to add
609 // an extraneous "via x.y" to our diagnostics, so filter out this case.
610 first_borrowed_place != second_borrowed_place
613 // We're going to want to traverse the first borrowed place to see if we can find
614 // field access to a union. If we find that, then we will keep the place of the
615 // union being accessed and the field that was being accessed so we can check the
616 // second borrowed place for the same union and a access to a different field.
617 let mut current = first_borrowed_place;
618 while let Place::Projection(box PlaceProjection { base, elem }) = current {
620 ProjectionElem::Field(field, _) if is_union(base) => {
621 return Some((base, field));
628 .and_then(|(target_base, target_field)| {
629 // With the place of a union and a field access into it, we traverse the second
630 // borrowed place and look for a access to a different field of the same union.
631 let mut current = second_borrowed_place;
632 while let Place::Projection(box PlaceProjection { base, elem }) = current {
634 ProjectionElem::Field(field, _) if {
635 is_union(base) && field != target_field && base == target_base
637 let desc_base = self.describe_place(base)
638 .unwrap_or_else(|| "_".to_owned());
639 let desc_first = self.describe_place(first_borrowed_place)
640 .unwrap_or_else(|| "_".to_owned());
641 let desc_second = self.describe_place(second_borrowed_place)
642 .unwrap_or_else(|| "_".to_owned());
644 // Also compute the name of the union type, eg. `Foo` so we
645 // can add a helpful note with it.
646 let ty = base.ty(self.mir, self.infcx.tcx).ty;
648 return Some((desc_base, desc_first, desc_second, ty.to_string()));
656 // If we didn't find a field access into a union, or both places match, then
657 // only return the description of the first place.
658 let desc_place = self.describe_place(first_borrowed_place)
659 .unwrap_or_else(|| "_".to_owned());
660 (desc_place, "".to_string(), "".to_string(), "".to_string())
664 /// Reports StorageDeadOrDrop of `place` conflicts with `borrow`.
666 /// This means that some data referenced by `borrow` needs to live
667 /// past the point where the StorageDeadOrDrop of `place` occurs.
668 /// This is usually interpreted as meaning that `place` has too
669 /// short a lifetime. (But sometimes it is more useful to report
670 /// it as a more direct conflict between the execution of a
671 /// `Drop::drop` with an aliasing borrow.)
672 pub(super) fn report_borrowed_value_does_not_live_long_enough(
675 borrow: &BorrowData<'tcx>,
676 place_span: (&Place<'tcx>, Span),
677 kind: Option<WriteKind>,
680 "report_borrowed_value_does_not_live_long_enough(\
681 {:?}, {:?}, {:?}, {:?}\
683 context, borrow, place_span, kind
686 let drop_span = place_span.1;
687 let scope_tree = self.infcx.tcx.region_scope_tree(self.mir_def_id);
688 let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
692 let borrow_spans = self.retrieve_borrow_spans(borrow);
693 let borrow_span = borrow_spans.var_or_use();
695 let proper_span = match *root_place {
696 Place::Base(PlaceBase::Local(local)) => self.mir.local_decls[local].source_info.span,
700 if self.access_place_error_reported
701 .contains(&(root_place.clone(), borrow_span))
704 "suppressing access_place error when borrow doesn't live long enough for {:?}",
710 self.access_place_error_reported
711 .insert((root_place.clone(), borrow_span));
713 if let StorageDeadOrDrop::Destructor(dropped_ty) =
714 self.classify_drop_access_kind(&borrow.borrowed_place)
716 // If a borrow of path `B` conflicts with drop of `D` (and
717 // we're not in the uninteresting case where `B` is a
718 // prefix of `D`), then report this as a more interesting
719 // destructor conflict.
720 if !borrow.borrowed_place.is_prefix_of(place_span.0) {
721 self.report_borrow_conflicts_with_destructor(
722 context, borrow, place_span, kind, dropped_ty,
728 let place_desc = self.describe_place(&borrow.borrowed_place);
730 let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
731 let explanation = self.explain_why_borrow_contains_point(context, &borrow, kind_place);
733 let err = match (place_desc, explanation) {
734 (Some(_), _) if self.is_place_thread_local(root_place) => {
735 self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
737 // If the outlives constraint comes from inside the closure,
742 // Box::new(|| y) as Box<Fn() -> &'static i32>
744 // then just use the normal error. The closure isn't escaping
745 // and `move` will not help here.
748 BorrowExplanation::MustBeValidFor {
749 category: category @ ConstraintCategory::Return,
758 BorrowExplanation::MustBeValidFor {
759 category: category @ ConstraintCategory::CallArgument,
765 ) if borrow_spans.for_closure() => self.report_escaping_closure_capture(
766 borrow_spans.args_or_use(),
771 &format!("`{}`", name),
775 BorrowExplanation::MustBeValidFor {
776 category: ConstraintCategory::Assignment,
778 region_name: RegionName {
779 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
785 ) => self.report_escaping_data(borrow_span, name, upvar_span, upvar_name, span),
786 (Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
795 (None, explanation) => self.report_temporary_value_does_not_live_long_enough(
806 err.buffer(&mut self.errors_buffer);
809 fn report_local_value_does_not_live_long_enough(
813 scope_tree: &'tcx ScopeTree,
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 context, name, scope_tree, borrow, drop_span, borrow_spans
826 let borrow_span = borrow_spans.var_or_use();
827 if let BorrowExplanation::MustBeValidFor {
828 category: ConstraintCategory::Return,
834 return self.report_cannot_return_reference_to_local(
838 opt_place_desc.as_ref(),
842 let mut err = self.infcx.tcx.path_does_not_live_long_enough(
844 &format!("`{}`", name),
848 if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
849 let region_name = annotation.emit(self, &mut err);
853 format!("`{}` would have to be valid for `{}`...", name, region_name),
856 if let Some(fn_hir_id) = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id) {
860 "...but `{}` will be dropped here, when the function `{}` returns",
862 self.infcx.tcx.hir().name_by_hir_id(fn_hir_id),
867 "functions cannot return a borrow to data owned within the function's scope, \
868 functions can only return borrows to data passed as arguments",
871 "to learn more, visit <https://doc.rust-lang.org/book/ch04-02-\
872 references-and-borrowing.html#dangling-references>",
877 format!("...but `{}` dropped here while still borrowed", name),
881 if let BorrowExplanation::MustBeValidFor { .. } = explanation {
883 explanation.add_explanation_to_diagnostic(
892 err.span_label(borrow_span, "borrowed value does not live long enough");
895 format!("`{}` dropped here while still borrowed", name),
898 let within = if borrow_spans.for_generator() {
904 borrow_spans.args_span_label(
906 format!("value captured here{}", within),
909 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
915 fn report_borrow_conflicts_with_destructor(
918 borrow: &BorrowData<'tcx>,
919 (place, drop_span): (&Place<'tcx>, Span),
920 kind: Option<WriteKind>,
921 dropped_ty: Ty<'tcx>,
924 "report_borrow_conflicts_with_destructor(\
925 {:?}, {:?}, ({:?}, {:?}), {:?}\
927 context, borrow, place, drop_span, kind,
930 let borrow_spans = self.retrieve_borrow_spans(borrow);
931 let borrow_span = borrow_spans.var_or_use();
933 let mut err = self.infcx
935 .cannot_borrow_across_destructor(borrow_span, Origin::Mir);
937 let what_was_dropped = match self.describe_place(place) {
938 Some(name) => format!("`{}`", name.as_str()),
939 None => String::from("temporary value"),
942 let label = match self.describe_place(&borrow.borrowed_place) {
943 Some(borrowed) => format!(
944 "here, drop of {D} needs exclusive access to `{B}`, \
945 because the type `{T}` implements the `Drop` trait",
946 D = what_was_dropped,
951 "here is drop of {D}; whose type `{T}` implements the `Drop` trait",
952 D = what_was_dropped,
956 err.span_label(drop_span, label);
958 // Only give this note and suggestion if they could be relevant.
960 self.explain_why_borrow_contains_point(context, borrow, kind.map(|k| (k, place)));
962 BorrowExplanation::UsedLater { .. }
963 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
964 err.note("consider using a `let` binding to create a longer lived value");
969 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
971 err.buffer(&mut self.errors_buffer);
974 fn report_thread_local_value_does_not_live_long_enough(
978 ) -> DiagnosticBuilder<'cx> {
980 "report_thread_local_value_does_not_live_long_enough(\
983 drop_span, borrow_span
986 let mut err = self.infcx
988 .thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
992 "thread-local variables cannot be borrowed beyond the end of the function",
994 err.span_label(drop_span, "end of enclosing function is here");
999 fn report_temporary_value_does_not_live_long_enough(
1002 scope_tree: &'tcx ScopeTree,
1003 borrow: &BorrowData<'tcx>,
1005 borrow_spans: UseSpans,
1007 explanation: BorrowExplanation,
1008 ) -> DiagnosticBuilder<'cx> {
1010 "report_temporary_value_does_not_live_long_enough(\
1011 {:?}, {:?}, {:?}, {:?}, {:?}\
1013 context, scope_tree, borrow, drop_span, proper_span
1016 if let BorrowExplanation::MustBeValidFor {
1017 category: ConstraintCategory::Return,
1019 from_closure: false,
1022 return self.report_cannot_return_reference_to_local(
1030 let tcx = self.infcx.tcx;
1031 let mut err = tcx.temporary_value_borrowed_for_too_long(proper_span, Origin::Mir);
1034 "creates a temporary which is freed while still in use",
1038 "temporary value is freed at the end of this statement",
1042 BorrowExplanation::UsedLater(..)
1043 | BorrowExplanation::UsedLaterInLoop(..)
1044 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
1045 // Only give this note and suggestion if it could be relevant.
1046 err.note("consider using a `let` binding to create a longer lived value");
1050 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
1052 let within = if borrow_spans.for_generator() {
1058 borrow_spans.args_span_label(
1060 format!("value captured here{}", within),
1066 fn report_cannot_return_reference_to_local(
1068 borrow: &BorrowData<'tcx>,
1071 opt_place_desc: Option<&String>,
1072 ) -> DiagnosticBuilder<'cx> {
1073 let tcx = self.infcx.tcx;
1075 // FIXME use a better heuristic than Spans
1076 let reference_desc = if return_span == self.mir.source_info(borrow.reserve_location).span {
1082 let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
1083 let local_kind = match borrow.borrowed_place {
1084 Place::Base(PlaceBase::Local(local)) => {
1085 match self.mir.local_kind(local) {
1086 LocalKind::ReturnPointer
1087 | LocalKind::Temp => bug!("temporary or return pointer with a name"),
1088 LocalKind::Var => "local variable ",
1090 if !self.upvars.is_empty()
1091 && local == Local::new(1) => {
1092 "variable captured by `move` "
1095 "function parameter "
1102 format!("{}`{}`", local_kind, place_desc),
1103 format!("`{}` is borrowed here", place_desc),
1106 let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
1109 let local = if let Place::Base(PlaceBase::Local(local)) = *root_place {
1112 bug!("report_cannot_return_reference_to_local: not a local")
1114 match self.mir.local_kind(local) {
1115 LocalKind::ReturnPointer | LocalKind::Temp => {
1117 "temporary value".to_string(),
1118 "temporary value created here".to_string(),
1123 "function parameter".to_string(),
1124 "function parameter borrowed here".to_string(),
1127 LocalKind::Var => bug!("local variable without a name"),
1131 let mut err = tcx.cannot_return_reference_to_local(
1138 if return_span != borrow_span {
1139 err.span_label(borrow_span, note);
1145 fn report_escaping_closure_capture(
1149 fr_name: &RegionName,
1150 category: ConstraintCategory,
1151 constraint_span: Span,
1153 ) -> DiagnosticBuilder<'cx> {
1154 let tcx = self.infcx.tcx;
1156 let mut err = tcx.cannot_capture_in_long_lived_closure(
1163 let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
1164 Ok(string) => format!("move {}", string),
1165 Err(_) => "move |<args>| <body>".to_string()
1168 err.span_suggestion(
1170 &format!("to force the closure to take ownership of {} (and any \
1171 other referenced variables), use the `move` keyword",
1174 Applicability::MachineApplicable,
1178 ConstraintCategory::Return => {
1179 err.span_note(constraint_span, "closure is returned here");
1181 ConstraintCategory::CallArgument => {
1182 fr_name.highlight_region_name(&mut err);
1185 &format!("function requires argument type to outlive `{}`", fr_name),
1188 _ => bug!("report_escaping_closure_capture called with unexpected constraint \
1189 category: `{:?}`", category),
1194 fn report_escaping_data(
1197 name: &Option<String>,
1201 ) -> DiagnosticBuilder<'cx> {
1202 let tcx = self.infcx.tcx;
1204 let escapes_from = if tcx.is_closure(self.mir_def_id) {
1205 let tables = tcx.typeck_tables_of(self.mir_def_id);
1206 let mir_hir_id = tcx.hir().def_index_to_hir_id(self.mir_def_id.index);
1207 match tables.node_type(mir_hir_id).sty {
1208 ty::Closure(..) => "closure",
1209 ty::Generator(..) => "generator",
1210 _ => bug!("Closure body doesn't have a closure or generator type"),
1216 let mut err = tcx.borrowed_data_escapes_closure(escape_span, escapes_from, Origin::Mir);
1221 "`{}` is declared here, outside of the {} body",
1222 upvar_name, escapes_from
1229 "borrow is only valid in the {} body",
1234 if let Some(name) = name {
1237 format!("reference to `{}` escapes the {} body here", name, escapes_from),
1242 format!("reference escapes the {} body here", escapes_from),
1249 fn get_moved_indexes(&mut self, context: Context, mpi: MovePathIndex) -> Vec<MoveSite> {
1252 let mut stack = Vec::new();
1253 stack.extend(mir.predecessor_locations(context.loc).map(|predecessor| {
1254 let is_back_edge = context.loc.dominates(predecessor, &self.dominators);
1255 (predecessor, is_back_edge)
1258 let mut visited = FxHashSet::default();
1259 let mut result = vec![];
1261 'dfs: while let Some((location, is_back_edge)) = stack.pop() {
1263 "report_use_of_moved_or_uninitialized: (current_location={:?}, back_edge={})",
1264 location, is_back_edge
1267 if !visited.insert(location) {
1272 let stmt_kind = mir[location.block]
1274 .get(location.statement_index)
1276 if let Some(StatementKind::StorageDead(..)) = stmt_kind {
1277 // this analysis only tries to find moves explicitly
1278 // written by the user, so we ignore the move-outs
1279 // created by `StorageDead` and at the beginning
1282 // If we are found a use of a.b.c which was in error, then we want to look for
1283 // moves not only of a.b.c but also a.b and a.
1285 // Note that the moves data already includes "parent" paths, so we don't have to
1286 // worry about the other case: that is, if there is a move of a.b.c, it is already
1287 // marked as a move of a.b and a as well, so we will generate the correct errors
1289 let mut mpis = vec![mpi];
1290 let move_paths = &self.move_data.move_paths;
1291 mpis.extend(move_paths[mpi].parents(move_paths));
1293 for moi in &self.move_data.loc_map[location] {
1294 debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
1295 if mpis.contains(&self.move_data.moves[*moi].path) {
1296 debug!("report_use_of_moved_or_uninitialized: found");
1297 result.push(MoveSite {
1299 traversed_back_edge: is_back_edge,
1302 // Strictly speaking, we could continue our DFS here. There may be
1303 // other moves that can reach the point of error. But it is kind of
1304 // confusing to highlight them.
1312 // drop(a); // <-- current point of error
1315 // Because we stop the DFS here, we only highlight `let c = a`,
1316 // and not `let b = a`. We will of course also report an error at
1317 // `let c = a` which highlights `let b = a` as the move.
1324 let mut any_match = false;
1325 drop_flag_effects::for_location_inits(
1340 stack.extend(mir.predecessor_locations(location).map(|predecessor| {
1341 let back_edge = location.dominates(predecessor, &self.dominators);
1342 (predecessor, is_back_edge || back_edge)
1349 pub(super) fn report_illegal_mutation_of_borrowed(
1352 (place, span): (&Place<'tcx>, Span),
1353 loan: &BorrowData<'tcx>,
1355 let loan_spans = self.retrieve_borrow_spans(loan);
1356 let loan_span = loan_spans.args_or_use();
1358 let tcx = self.infcx.tcx;
1359 if loan.kind == BorrowKind::Shallow {
1360 let mut err = tcx.cannot_mutate_in_match_guard(
1363 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1367 loan_spans.var_span_label(
1369 format!("borrow occurs due to use{}", loan_spans.describe()),
1372 err.buffer(&mut self.errors_buffer);
1377 let mut err = tcx.cannot_assign_to_borrowed(
1380 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1384 loan_spans.var_span_label(
1386 format!("borrow occurs due to use{}", loan_spans.describe()),
1389 self.explain_why_borrow_contains_point(context, loan, None)
1390 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "", None);
1392 err.buffer(&mut self.errors_buffer);
1395 /// Reports an illegal reassignment; for example, an assignment to
1396 /// (part of) a non-`mut` local that occurs potentially after that
1397 /// local has already been initialized. `place` is the path being
1398 /// assigned; `err_place` is a place providing a reason why
1399 /// `place` is not mutable (e.g., the non-`mut` local `x` in an
1400 /// assignment to `x.f`).
1401 pub(super) fn report_illegal_reassignment(
1404 (place, span): (&Place<'tcx>, Span),
1405 assigned_span: Span,
1406 err_place: &Place<'tcx>,
1408 let (from_arg, local_decl) = if let Place::Base(PlaceBase::Local(local)) = *err_place {
1409 if let LocalKind::Arg = self.mir.local_kind(local) {
1410 (true, Some(&self.mir.local_decls[local]))
1412 (false, Some(&self.mir.local_decls[local]))
1418 // If root local is initialized immediately (everything apart from let
1419 // PATTERN;) then make the error refer to that local, rather than the
1420 // place being assigned later.
1421 let (place_description, assigned_span) = match local_decl {
1423 is_user_variable: Some(ClearCrossCrate::Clear),
1428 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
1429 opt_match_place: None,
1435 is_user_variable: None,
1438 | None => (self.describe_place(place), assigned_span),
1439 Some(decl) => (self.describe_place(err_place), decl.source_info.span),
1442 let mut err = self.infcx.tcx.cannot_reassign_immutable(
1444 place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
1448 let msg = if from_arg {
1449 "cannot assign to immutable argument"
1451 "cannot assign twice to immutable variable"
1453 if span != assigned_span {
1455 let value_msg = match place_description {
1456 Some(name) => format!("`{}`", name),
1457 None => "value".to_owned(),
1459 err.span_label(assigned_span, format!("first assignment to {}", value_msg));
1462 if let Some(decl) = local_decl {
1463 if let Some(name) = decl.name {
1464 if decl.can_be_made_mutable() {
1465 err.span_suggestion(
1466 decl.source_info.span,
1467 "make this binding mutable",
1468 format!("mut {}", name),
1469 Applicability::MachineApplicable,
1474 err.span_label(span, msg);
1475 err.buffer(&mut self.errors_buffer);
1479 pub(super) struct IncludingDowncast(bool);
1481 /// Which case a StorageDeadOrDrop is for.
1482 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
1483 enum StorageDeadOrDrop<'tcx> {
1486 Destructor(Ty<'tcx>),
1489 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1491 /// Adds a suggestion when a closure is invoked twice with a moved variable or when a closure
1492 /// is moved after being invoked.
1495 /// note: closure cannot be invoked more than once because it moves the variable `dict` out of
1497 /// --> $DIR/issue-42065.rs:16:29
1499 /// LL | for (key, value) in dict {
1502 pub(super) fn add_moved_or_invoked_closure_note(
1505 place: &Place<'tcx>,
1506 diag: &mut DiagnosticBuilder<'_>,
1508 debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
1509 let mut target = place.local();
1510 for stmt in &self.mir[location.block].statements[location.statement_index..] {
1511 debug!("add_moved_or_invoked_closure_note: stmt={:?} target={:?}", stmt, target);
1512 if let StatementKind::Assign(into, box Rvalue::Use(from)) = &stmt.kind {
1513 debug!("add_fnonce_closure_note: into={:?} from={:?}", into, from);
1515 Operand::Copy(ref place) |
1516 Operand::Move(ref place) if target == place.local() =>
1517 target = into.local(),
1523 // Check if we are attempting to call a closure after it has been invoked.
1524 let terminator = self.mir[location.block].terminator();
1525 debug!("add_moved_or_invoked_closure_note: terminator={:?}", terminator);
1526 if let TerminatorKind::Call {
1527 func: Operand::Constant(box Constant {
1528 literal: ty::Const {
1529 ty: &ty::TyS { sty: ty::FnDef(id, _), .. },
1536 } = &terminator.kind {
1537 debug!("add_moved_or_invoked_closure_note: id={:?}", id);
1538 if self.infcx.tcx.parent(id) == self.infcx.tcx.lang_items().fn_once_trait() {
1539 let closure = match args.first() {
1540 Some(Operand::Copy(ref place)) |
1541 Some(Operand::Move(ref place)) if target == place.local() =>
1542 place.local().unwrap(),
1546 debug!("add_moved_or_invoked_closure_note: closure={:?}", closure);
1547 if let ty::Closure(did, _) = self.mir.local_decls[closure].ty.sty {
1548 let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap();
1550 if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did)
1551 .closure_kind_origins()
1557 "closure cannot be invoked more than once because it moves the \
1558 variable `{}` out of its environment",
1568 // Check if we are just moving a closure after it has been invoked.
1569 if let Some(target) = target {
1570 if let ty::Closure(did, _) = self.mir.local_decls[target].ty.sty {
1571 let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap();
1573 if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did)
1574 .closure_kind_origins()
1580 "closure cannot be moved more than once as it is not `Copy` due to \
1581 moving the variable `{}` out of its environment",
1590 /// End-user visible description of `place` if one can be found. If the
1591 /// place is a temporary for instance, None will be returned.
1592 pub(super) fn describe_place(&self, place: &Place<'tcx>) -> Option<String> {
1593 self.describe_place_with_options(place, IncludingDowncast(false))
1596 /// End-user visible description of `place` if one can be found. If the
1597 /// place is a temporary for instance, None will be returned.
1598 /// `IncludingDowncast` parameter makes the function return `Err` if `ProjectionElem` is
1599 /// `Downcast` and `IncludingDowncast` is true
1600 pub(super) fn describe_place_with_options(
1602 place: &Place<'tcx>,
1603 including_downcast: IncludingDowncast,
1604 ) -> Option<String> {
1605 let mut buf = String::new();
1606 match self.append_place_to_string(place, &mut buf, false, &including_downcast) {
1607 Ok(()) => Some(buf),
1612 /// Appends end-user visible description of `place` to `buf`.
1613 fn append_place_to_string(
1615 place: &Place<'tcx>,
1617 mut autoderef: bool,
1618 including_downcast: &IncludingDowncast,
1619 ) -> Result<(), ()> {
1621 Place::Base(PlaceBase::Local(local)) => {
1622 self.append_local_to_string(local, buf)?;
1624 Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {
1625 buf.push_str("promoted");
1627 Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
1628 buf.push_str(&self.infcx.tcx.item_name(def_id).to_string());
1630 Place::Projection(ref proj) => {
1632 ProjectionElem::Deref => {
1633 let upvar_field_projection =
1634 self.is_upvar_field_projection(place);
1635 if let Some(field) = upvar_field_projection {
1636 let var_index = field.index();
1637 let name = self.upvars[var_index].name.to_string();
1638 if self.upvars[var_index].by_ref {
1639 buf.push_str(&name);
1641 buf.push_str(&format!("*{}", &name));
1645 self.append_place_to_string(
1649 &including_downcast,
1651 } else if let Place::Base(PlaceBase::Local(local)) = proj.base {
1652 if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
1653 self.mir.local_decls[local].is_user_variable
1655 self.append_place_to_string(
1659 &including_downcast,
1663 self.append_place_to_string(
1667 &including_downcast,
1672 self.append_place_to_string(
1676 &including_downcast,
1681 ProjectionElem::Downcast(..) => {
1682 self.append_place_to_string(
1686 &including_downcast,
1688 if including_downcast.0 {
1692 ProjectionElem::Field(field, _ty) => {
1695 let upvar_field_projection =
1696 self.is_upvar_field_projection(place);
1697 if let Some(field) = upvar_field_projection {
1698 let var_index = field.index();
1699 let name = self.upvars[var_index].name.to_string();
1700 buf.push_str(&name);
1702 let field_name = self.describe_field(&proj.base, field);
1703 self.append_place_to_string(
1707 &including_downcast,
1709 buf.push_str(&format!(".{}", field_name));
1712 ProjectionElem::Index(index) => {
1715 self.append_place_to_string(
1719 &including_downcast,
1722 if self.append_local_to_string(index, buf).is_err() {
1727 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
1729 // Since it isn't possible to borrow an element on a particular index and
1730 // then use another while the borrow is held, don't output indices details
1731 // to avoid confusing the end-user
1732 self.append_place_to_string(
1736 &including_downcast,
1738 buf.push_str(&"[..]");
1747 /// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
1748 /// a name, then `Err` is returned
1749 fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
1750 let local = &self.mir.local_decls[local_index];
1753 buf.push_str(&name.to_string());
1760 /// End-user visible description of the `field`nth field of `base`
1761 fn describe_field(&self, base: &Place<'tcx>, field: Field) -> String {
1763 Place::Base(PlaceBase::Local(local)) => {
1764 let local = &self.mir.local_decls[local];
1765 self.describe_field_from_ty(&local.ty, field, None)
1767 Place::Base(PlaceBase::Static(ref static_)) =>
1768 self.describe_field_from_ty(&static_.ty, field, None),
1769 Place::Projection(ref proj) => match proj.elem {
1770 ProjectionElem::Deref => self.describe_field(&proj.base, field),
1771 ProjectionElem::Downcast(_, variant_index) => {
1772 let base_ty = base.ty(self.mir, self.infcx.tcx).ty;
1773 self.describe_field_from_ty(&base_ty, field, Some(variant_index))
1775 ProjectionElem::Field(_, field_type) => {
1776 self.describe_field_from_ty(&field_type, field, None)
1778 ProjectionElem::Index(..)
1779 | ProjectionElem::ConstantIndex { .. }
1780 | ProjectionElem::Subslice { .. } => {
1781 self.describe_field(&proj.base, field)
1787 /// End-user visible description of the `field_index`nth field of `ty`
1788 fn describe_field_from_ty(
1792 variant_index: Option<VariantIdx>
1795 // If the type is a box, the field is described from the boxed type
1796 self.describe_field_from_ty(&ty.boxed_ty(), field, variant_index)
1799 ty::Adt(def, _) => {
1800 let variant = if let Some(idx) = variant_index {
1801 assert!(def.is_enum());
1804 def.non_enum_variant()
1806 variant.fields[field.index()]
1810 ty::Tuple(_) => field.index().to_string(),
1811 ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
1812 self.describe_field_from_ty(&ty, field, variant_index)
1814 ty::Array(ty, _) | ty::Slice(ty) =>
1815 self.describe_field_from_ty(&ty, field, variant_index),
1816 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
1817 // Convert the def-id into a node-id. node-ids are only valid for
1818 // the local code in the current crate, so this returns an `Option` in case
1819 // the closure comes from another crate. But in that case we wouldn't
1820 // be borrowck'ing it, so we can just unwrap:
1821 let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id).unwrap();
1822 let freevar = self.infcx
1824 .with_freevars(hir_id, |fv| fv[field.index()]);
1826 self.infcx.tcx.hir().name_by_hir_id(freevar.var_id()).to_string()
1829 // Might need a revision when the fields in trait RFC is implemented
1830 // (https://github.com/rust-lang/rfcs/pull/1546)
1832 "End-user description not implemented for field access on `{:?}`",
1840 /// Checks if a place is a thread-local static.
1841 pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
1843 PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })
1845 let attrs = self.infcx.tcx.get_attrs(*def_id);
1846 let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
1849 "is_place_thread_local: attrs={:?} is_thread_local={:?}",
1850 attrs, is_thread_local
1854 debug!("is_place_thread_local: no");
1859 fn classify_drop_access_kind(&self, place: &Place<'tcx>) -> StorageDeadOrDrop<'tcx> {
1860 let tcx = self.infcx.tcx;
1862 Place::Base(PlaceBase::Local(_)) |
1863 Place::Base(PlaceBase::Static(_)) => {
1864 StorageDeadOrDrop::LocalStorageDead
1866 Place::Projection(box PlaceProjection { base, elem }) => {
1867 let base_access = self.classify_drop_access_kind(base);
1869 ProjectionElem::Deref => match base_access {
1870 StorageDeadOrDrop::LocalStorageDead
1871 | StorageDeadOrDrop::BoxedStorageDead => {
1873 base.ty(self.mir, tcx).ty.is_box(),
1874 "Drop of value behind a reference or raw pointer"
1876 StorageDeadOrDrop::BoxedStorageDead
1878 StorageDeadOrDrop::Destructor(_) => base_access,
1880 ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1881 let base_ty = base.ty(self.mir, tcx).ty;
1883 ty::Adt(def, _) if def.has_dtor(tcx) => {
1884 // Report the outermost adt with a destructor
1886 StorageDeadOrDrop::Destructor(_) => base_access,
1887 StorageDeadOrDrop::LocalStorageDead
1888 | StorageDeadOrDrop::BoxedStorageDead => {
1889 StorageDeadOrDrop::Destructor(base_ty)
1897 ProjectionElem::ConstantIndex { .. }
1898 | ProjectionElem::Subslice { .. }
1899 | ProjectionElem::Index(_) => base_access,
1905 /// Annotate argument and return type of function and closure with (synthesized) lifetime for
1906 /// borrow of local value that does not live long enough.
1907 fn annotate_argument_and_return_for_borrow(
1909 borrow: &BorrowData<'tcx>,
1910 ) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
1911 // Define a fallback for when we can't match a closure.
1913 let is_closure = self.infcx.tcx.is_closure(self.mir_def_id);
1917 let ty = self.infcx.tcx.type_of(self.mir_def_id);
1919 ty::FnDef(_, _) | ty::FnPtr(_) => self.annotate_fn_sig(
1921 self.infcx.tcx.fn_sig(self.mir_def_id),
1928 // In order to determine whether we need to annotate, we need to check whether the reserve
1929 // place was an assignment into a temporary.
1931 // If it was, we check whether or not that temporary is eventually assigned into the return
1932 // place. If it was, we can add annotations about the function's return type and arguments
1933 // and it'll make sense.
1934 let location = borrow.reserve_location;
1936 "annotate_argument_and_return_for_borrow: location={:?}",
1939 if let Some(&Statement { kind: StatementKind::Assign(ref reservation, _), ..})
1940 = &self.mir[location.block].statements.get(location.statement_index)
1943 "annotate_argument_and_return_for_borrow: reservation={:?}",
1946 // Check that the initial assignment of the reserve location is into a temporary.
1947 let mut target = *match reservation {
1948 Place::Base(PlaceBase::Local(local))
1949 if self.mir.local_kind(*local) == LocalKind::Temp => local,
1953 // Next, look through the rest of the block, checking if we are assigning the
1954 // `target` (that is, the place that contains our borrow) to anything.
1955 let mut annotated_closure = None;
1956 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
1958 "annotate_argument_and_return_for_borrow: target={:?} stmt={:?}",
1961 if let StatementKind::Assign(
1962 Place::Base(PlaceBase::Local(assigned_to)),
1966 "annotate_argument_and_return_for_borrow: assigned_to={:?} \
1970 // Check if our `target` was captured by a closure.
1971 if let Rvalue::Aggregate(
1972 box AggregateKind::Closure(def_id, substs),
1976 for operand in operands {
1977 let assigned_from = match operand {
1978 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1984 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1988 // Find the local from the operand.
1989 let assigned_from_local = match assigned_from.local() {
1990 Some(local) => local,
1994 if assigned_from_local != target {
1998 // If a closure captured our `target` and then assigned
1999 // into a place then we should annotate the closure in
2000 // case it ends up being assigned into the return place.
2001 annotated_closure = self.annotate_fn_sig(
2003 self.infcx.closure_sig(*def_id, *substs),
2006 "annotate_argument_and_return_for_borrow: \
2007 annotated_closure={:?} assigned_from_local={:?} \
2009 annotated_closure, assigned_from_local, assigned_to
2012 if *assigned_to == mir::RETURN_PLACE {
2013 // If it was assigned directly into the return place, then
2015 return annotated_closure;
2017 // Otherwise, update the target.
2018 target = *assigned_to;
2022 // If none of our closure's operands matched, then skip to the next
2027 // Otherwise, look at other types of assignment.
2028 let assigned_from = match rvalue {
2029 Rvalue::Ref(_, _, assigned_from) => assigned_from,
2030 Rvalue::Use(operand) => match operand {
2031 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
2039 "annotate_argument_and_return_for_borrow: \
2040 assigned_from={:?}",
2044 // Find the local from the rvalue.
2045 let assigned_from_local = match assigned_from.local() {
2046 Some(local) => local,
2050 "annotate_argument_and_return_for_borrow: \
2051 assigned_from_local={:?}",
2052 assigned_from_local,
2055 // Check if our local matches the target - if so, we've assigned our
2056 // borrow to a new place.
2057 if assigned_from_local != target {
2061 // If we assigned our `target` into a new place, then we should
2062 // check if it was the return place.
2064 "annotate_argument_and_return_for_borrow: \
2065 assigned_from_local={:?} assigned_to={:?}",
2066 assigned_from_local, assigned_to
2068 if *assigned_to == mir::RETURN_PLACE {
2069 // If it was then return the annotated closure if there was one,
2070 // else, annotate this function.
2071 return annotated_closure.or_else(fallback);
2074 // If we didn't assign into the return place, then we just update
2076 target = *assigned_to;
2080 // Check the terminator if we didn't find anything in the statements.
2081 let terminator = &self.mir[location.block].terminator();
2083 "annotate_argument_and_return_for_borrow: target={:?} terminator={:?}",
2086 if let TerminatorKind::Call {
2087 destination: Some((Place::Base(PlaceBase::Local(assigned_to)), _)),
2090 } = &terminator.kind
2093 "annotate_argument_and_return_for_borrow: assigned_to={:?} args={:?}",
2096 for operand in args {
2097 let assigned_from = match operand {
2098 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
2104 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
2108 if let Some(assigned_from_local) = assigned_from.local() {
2110 "annotate_argument_and_return_for_borrow: assigned_from_local={:?}",
2111 assigned_from_local,
2114 if *assigned_to == mir::RETURN_PLACE && assigned_from_local == target {
2115 return annotated_closure.or_else(fallback);
2122 // If we haven't found an assignment into the return place, then we need not add
2124 debug!("annotate_argument_and_return_for_borrow: none found");
2128 /// Annotate the first argument and return type of a function signature if they are
2133 sig: ty::PolyFnSig<'tcx>,
2134 ) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
2135 debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
2136 let is_closure = self.infcx.tcx.is_closure(did);
2137 let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did)?;
2138 let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
2140 // We need to work out which arguments to highlight. We do this by looking
2141 // at the return type, where there are three cases:
2143 // 1. If there are named arguments, then we should highlight the return type and
2144 // highlight any of the arguments that are also references with that lifetime.
2145 // If there are no arguments that have the same lifetime as the return type,
2146 // then don't highlight anything.
2147 // 2. The return type is a reference with an anonymous lifetime. If this is
2148 // the case, then we can take advantage of (and teach) the lifetime elision
2151 // We know that an error is being reported. So the arguments and return type
2152 // must satisfy the elision rules. Therefore, if there is a single argument
2153 // then that means the return type and first (and only) argument have the same
2154 // lifetime and the borrow isn't meeting that, we can highlight the argument
2157 // If there are multiple arguments then the first argument must be self (else
2158 // it would not satisfy the elision rules), so we can highlight self and the
2160 // 3. The return type is not a reference. In this case, we don't highlight
2162 let return_ty = sig.output();
2163 match return_ty.skip_binder().sty {
2164 ty::Ref(return_region, _, _) if return_region.has_name() && !is_closure => {
2165 // This is case 1 from above, return type is a named reference so we need to
2166 // search for relevant arguments.
2167 let mut arguments = Vec::new();
2168 for (index, argument) in sig.inputs().skip_binder().iter().enumerate() {
2169 if let ty::Ref(argument_region, _, _) = argument.sty {
2170 if argument_region == return_region {
2171 // Need to use the `rustc::ty` types to compare against the
2172 // `return_region`. Then use the `rustc::hir` type to get only
2173 // the lifetime span.
2174 if let hir::TyKind::Rptr(lifetime, _) = &fn_decl.inputs[index].node {
2175 // With access to the lifetime, we can get
2177 arguments.push((*argument, lifetime.span));
2179 bug!("ty type is a ref but hir type is not");
2185 // We need to have arguments. This shouldn't happen, but it's worth checking.
2186 if arguments.is_empty() {
2190 // We use a mix of the HIR and the Ty types to get information
2191 // as the HIR doesn't have full types for closure arguments.
2192 let return_ty = *sig.output().skip_binder();
2193 let mut return_span = fn_decl.output.span();
2194 if let hir::FunctionRetTy::Return(ty) = fn_decl.output {
2195 if let hir::TyKind::Rptr(lifetime, _) = ty.into_inner().node {
2196 return_span = lifetime.span;
2200 Some(AnnotatedBorrowFnSignature::NamedFunction {
2206 ty::Ref(_, _, _) if is_closure => {
2207 // This is case 2 from above but only for closures, return type is anonymous
2208 // reference so we select
2209 // the first argument.
2210 let argument_span = fn_decl.inputs.first()?.span;
2211 let argument_ty = sig.inputs().skip_binder().first()?;
2213 // Closure arguments are wrapped in a tuple, so we need to get the first
2215 if let ty::Tuple(elems) = argument_ty.sty {
2216 let argument_ty = elems.first()?.expect_ty();
2217 if let ty::Ref(_, _, _) = argument_ty.sty {
2218 return Some(AnnotatedBorrowFnSignature::Closure {
2227 ty::Ref(_, _, _) => {
2228 // This is also case 2 from above but for functions, return type is still an
2229 // anonymous reference so we select the first argument.
2230 let argument_span = fn_decl.inputs.first()?.span;
2231 let argument_ty = sig.inputs().skip_binder().first()?;
2233 let return_span = fn_decl.output.span();
2234 let return_ty = *sig.output().skip_binder();
2236 // We expect the first argument to be a reference.
2237 match argument_ty.sty {
2238 ty::Ref(_, _, _) => {}
2242 Some(AnnotatedBorrowFnSignature::AnonymousFunction {
2250 // This is case 3 from above, return type is not a reference so don't highlight
2259 enum AnnotatedBorrowFnSignature<'tcx> {
2261 arguments: Vec<(Ty<'tcx>, Span)>,
2262 return_ty: Ty<'tcx>,
2266 argument_ty: Ty<'tcx>,
2267 argument_span: Span,
2268 return_ty: Ty<'tcx>,
2272 argument_ty: Ty<'tcx>,
2273 argument_span: Span,
2277 impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
2278 /// Annotate the provided diagnostic with information about borrow from the fn signature that
2282 cx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
2283 diag: &mut DiagnosticBuilder<'_>,
2286 AnnotatedBorrowFnSignature::Closure {
2292 format!("has type `{}`", cx.get_name_for_ty(argument_ty, 0)),
2295 cx.get_region_name_for_ty(argument_ty, 0)
2297 AnnotatedBorrowFnSignature::AnonymousFunction {
2303 let argument_ty_name = cx.get_name_for_ty(argument_ty, 0);
2304 diag.span_label(*argument_span, format!("has type `{}`", argument_ty_name));
2306 let return_ty_name = cx.get_name_for_ty(return_ty, 0);
2307 let types_equal = return_ty_name == argument_ty_name;
2312 if types_equal { "also " } else { "" },
2318 "argument and return type have the same lifetime due to lifetime elision rules",
2321 "to learn more, visit <https://doc.rust-lang.org/book/ch10-03-\
2322 lifetime-syntax.html#lifetime-elision>",
2325 cx.get_region_name_for_ty(return_ty, 0)
2327 AnnotatedBorrowFnSignature::NamedFunction {
2332 // Region of return type and arguments checked to be the same earlier.
2333 let region_name = cx.get_region_name_for_ty(return_ty, 0);
2334 for (_, argument_span) in arguments {
2335 diag.span_label(*argument_span, format!("has lifetime `{}`", region_name));
2340 format!("also has lifetime `{}`", region_name,),
2344 "use data from the highlighted arguments which match the `{}` lifetime of \
2355 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
2356 /// Return the name of the provided `Ty` (that must be a reference) with a synthesized lifetime
2357 /// name where required.
2358 fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
2359 let mut s = String::new();
2360 let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
2362 // We need to add synthesized lifetimes where appropriate. We do
2363 // this by hooking into the pretty printer and telling it to label the
2364 // lifetimes without names with the value `'0`.
2366 ty::Ref(ty::RegionKind::ReLateBound(_, br), _, _)
2368 ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }),
2371 ) => printer.region_highlight_mode.highlighting_bound_region(*br, counter),
2375 let _ = ty.print(printer);
2379 /// Returns the name of the provided `Ty` (that must be a reference)'s region with a
2380 /// synthesized lifetime name where required.
2381 fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
2382 let mut s = String::new();
2383 let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
2385 let region = match ty.sty {
2386 ty::Ref(region, _, _) => {
2388 ty::RegionKind::ReLateBound(_, br)
2389 | ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
2390 printer.region_highlight_mode.highlighting_bound_region(*br, counter)
2397 _ => bug!("ty for annotation of borrow region is not a reference"),
2400 let _ = region.print(printer);
2405 // The span(s) associated to a use of a place.
2406 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
2407 pub(super) enum UseSpans {
2408 // The access is caused by capturing a variable for a closure.
2410 // This is true if the captured variable was from a generator.
2412 // The span of the args of the closure, including the `move` keyword if
2415 // The span of the first use of the captured variable inside the closure.
2418 // This access has a single span associated to it: common case.
2423 pub(super) fn args_or_use(self) -> Span {
2425 UseSpans::ClosureUse {
2428 | UseSpans::OtherUse(span) => span,
2432 pub(super) fn var_or_use(self) -> Span {
2434 UseSpans::ClosureUse { var_span: span, .. } | UseSpans::OtherUse(span) => span,
2438 // Add a span label to the arguments of the closure, if it exists.
2439 pub(super) fn args_span_label(
2441 err: &mut DiagnosticBuilder<'_>,
2442 message: impl Into<String>,
2444 if let UseSpans::ClosureUse { args_span, .. } = self {
2445 err.span_label(args_span, message);
2449 // Add a span label to the use of the captured variable, if it exists.
2450 pub(super) fn var_span_label(
2452 err: &mut DiagnosticBuilder<'_>,
2453 message: impl Into<String>,
2455 if let UseSpans::ClosureUse { var_span, .. } = self {
2456 err.span_label(var_span, message);
2460 /// Returns `false` if this place is not used in a closure.
2461 fn for_closure(&self) -> bool {
2463 UseSpans::ClosureUse { is_generator, .. } => !is_generator,
2468 /// Returns `false` if this place is not used in a generator.
2469 fn for_generator(&self) -> bool {
2471 UseSpans::ClosureUse { is_generator, .. } => is_generator,
2476 /// Describe the span associated with a use of a place.
2477 fn describe(&self) -> String {
2479 UseSpans::ClosureUse { is_generator, .. } => if is_generator {
2480 " in generator".to_string()
2482 " in closure".to_string()
2484 _ => "".to_string(),
2488 pub(super) fn or_else<F>(self, if_other: F) -> Self
2490 F: FnOnce() -> Self,
2493 closure @ UseSpans::ClosureUse { .. } => closure,
2494 UseSpans::OtherUse(_) => if_other(),
2499 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
2500 /// Finds the spans associated to a move or copy of move_place at location.
2501 pub(super) fn move_spans(
2503 moved_place: &Place<'tcx>, // Could also be an upvar.
2506 use self::UseSpans::*;
2508 let stmt = match self.mir[location.block].statements.get(location.statement_index) {
2510 None => return OtherUse(self.mir.source_info(location).span),
2513 debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
2514 if let StatementKind::Assign(
2516 box Rvalue::Aggregate(ref kind, ref places)
2518 let (def_id, is_generator) = match kind {
2519 box AggregateKind::Closure(def_id, _) => (def_id, false),
2520 box AggregateKind::Generator(def_id, _, _) => (def_id, true),
2521 _ => return OtherUse(stmt.source_info.span),
2525 "move_spans: def_id={:?} is_generator={:?} places={:?}",
2526 def_id, is_generator, places
2528 if let Some((args_span, var_span)) = self.closure_span(*def_id, moved_place, places) {
2537 OtherUse(stmt.source_info.span)
2540 /// Finds the span of arguments of a closure (within `maybe_closure_span`)
2541 /// and its usage of the local assigned at `location`.
2542 /// This is done by searching in statements succeeding `location`
2543 /// and originating from `maybe_closure_span`.
2544 pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans {
2545 use self::UseSpans::*;
2546 debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
2548 let target = match self.mir[location.block]
2550 .get(location.statement_index)
2553 kind: StatementKind::Assign(Place::Base(PlaceBase::Local(local)), _),
2556 _ => return OtherUse(use_span),
2559 if self.mir.local_kind(target) != LocalKind::Temp {
2560 // operands are always temporaries.
2561 return OtherUse(use_span);
2564 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
2565 if let StatementKind::Assign(
2566 _, box Rvalue::Aggregate(ref kind, ref places)
2568 let (def_id, is_generator) = match kind {
2569 box AggregateKind::Closure(def_id, _) => (def_id, false),
2570 box AggregateKind::Generator(def_id, _, _) => (def_id, true),
2575 "borrow_spans: def_id={:?} is_generator={:?} places={:?}",
2576 def_id, is_generator, places
2578 if let Some((args_span, var_span)) = self.closure_span(
2579 *def_id, &Place::Base(PlaceBase::Local(target)), places
2587 return OtherUse(use_span);
2591 if use_span != stmt.source_info.span {
2599 /// Finds the span of a captured variable within a closure or generator.
2603 target_place: &Place<'tcx>,
2604 places: &Vec<Operand<'tcx>>,
2605 ) -> Option<(Span, Span)> {
2607 "closure_span: def_id={:?} target_place={:?} places={:?}",
2608 def_id, target_place, places
2610 let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id)?;
2611 let expr = &self.infcx.tcx.hir().expect_expr_by_hir_id(hir_id).node;
2612 debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
2613 if let hir::ExprKind::Closure(
2616 let var_span = self.infcx.tcx.with_freevars(
2619 for (v, place) in freevars.iter().zip(places) {
2621 Operand::Copy(place) |
2622 Operand::Move(place) if target_place == place => {
2623 debug!("closure_span: found captured local {:?}", place);
2624 return Some(v.span);
2634 Some((*args_span, var_span))
2640 /// Helper to retrieve span(s) of given borrow from the current MIR
2642 pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData<'_>) -> UseSpans {
2643 let span = self.mir.source_info(borrow.reserve_location).span;
2644 self.borrow_spans(span, borrow.reserve_location)