1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use borrow_check::nll::explain_borrow::BorrowExplanation;
12 use borrow_check::nll::region_infer::{RegionName, RegionNameSource};
13 use borrow_check::prefixes::IsPrefixOf;
14 use borrow_check::WriteKind;
16 use rustc::hir::def_id::DefId;
17 use rustc::middle::region::ScopeTree;
19 self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, Constant,
20 ConstraintCategory, Field, Local, LocalDecl, LocalKind, Location, Operand,
21 Place, PlaceProjection, ProjectionElem, Rvalue, Statement, StatementKind,
22 TerminatorKind, VarBindingForm,
24 use rustc::ty::{self, DefIdTree};
25 use rustc::util::ppaux::with_highlight_region_for_bound_region;
26 use rustc_data_structures::fx::FxHashSet;
27 use rustc_data_structures::indexed_vec::Idx;
28 use rustc_data_structures::sync::Lrc;
29 use rustc_errors::{Applicability, DiagnosticBuilder};
32 use super::borrow_set::BorrowData;
33 use super::{Context, MirBorrowckCtxt};
34 use super::{InitializationRequiringAction, PrefixSet};
35 use dataflow::drop_flag_effects;
36 use dataflow::move_paths::indexes::MoveOutIndex;
37 use dataflow::move_paths::MovePathIndex;
38 use util::borrowck_errors::{BorrowckErrors, Origin};
42 /// Index of the "move out" that we found. The `MoveData` can
43 /// then tell us where the move occurred.
46 /// True if we traversed a back edge while walking from the point
47 /// of error to the move site.
48 traversed_back_edge: bool
51 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
52 pub(super) fn report_use_of_moved_or_uninitialized(
55 desired_action: InitializationRequiringAction,
56 (moved_place, used_place, span): (&Place<'tcx>, &Place<'tcx>, Span),
60 "report_use_of_moved_or_uninitialized: context={:?} desired_action={:?} \
61 moved_place={:?} used_place={:?} span={:?} mpi={:?}",
62 context, desired_action, moved_place, used_place, span, mpi
65 let use_spans = self.move_spans(moved_place, context.loc)
66 .or_else(|| self.borrow_spans(span, context.loc));
67 let span = use_spans.args_or_use();
69 let move_site_vec = self.get_moved_indexes(context, mpi);
71 "report_use_of_moved_or_uninitialized: move_site_vec={:?}",
74 let move_out_indices: Vec<_> = move_site_vec
76 .map(|move_site| move_site.moi)
79 if move_out_indices.is_empty() {
80 let root_place = self.prefixes(&used_place, PrefixSet::All).last().unwrap();
82 if self.uninitialized_error_reported.contains(root_place) {
84 "report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
90 self.uninitialized_error_reported.insert(root_place.clone());
92 let item_msg = match self.describe_place_with_options(used_place,
93 IncludingDowncast(true)) {
94 Some(name) => format!("`{}`", name),
95 None => "value".to_owned(),
97 let mut err = self.infcx.tcx.cannot_act_on_uninitialized_variable(
99 desired_action.as_noun(),
100 &self.describe_place_with_options(moved_place, IncludingDowncast(true))
101 .unwrap_or_else(|| "_".to_owned()),
104 err.span_label(span, format!("use of possibly uninitialized {}", item_msg));
106 use_spans.var_span_label(
108 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
111 err.buffer(&mut self.errors_buffer);
113 if let Some((reported_place, _)) = self.move_error_reported.get(&move_out_indices) {
114 if self.prefixes(&reported_place, PrefixSet::All)
115 .any(|p| p == used_place)
118 "report_use_of_moved_or_uninitialized place: error suppressed \
126 let msg = ""; //FIXME: add "partially " or "collaterally "
128 let mut err = self.infcx.tcx.cannot_act_on_moved_value(
130 desired_action.as_noun(),
132 self.describe_place_with_options(&moved_place, IncludingDowncast(true)),
136 self.add_closure_invoked_twice_with_moved_variable_suggestion(
142 let mut is_loop_move = false;
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),
162 } else if move_site.traversed_back_edge {
166 "value moved{} here, in previous iteration of loop",
171 err.span_label(move_span, format!("value moved{} here", move_msg));
172 move_spans.var_span_label(
174 format!("variable moved due to use{}", move_spans.describe()),
179 use_spans.var_span_label(
181 format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
188 "value {} here after move",
189 desired_action.as_verb_in_past_tense()
194 if let Some(ty) = self.retrieve_type_for_place(used_place) {
195 let needs_note = match ty.sty {
196 ty::Closure(id, _) => {
197 let tables = self.infcx.tcx.typeck_tables_of(id);
198 let node_id = self.infcx.tcx.hir().as_local_node_id(id).unwrap();
199 let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);
201 tables.closure_kind_origins().get(hir_id).is_none()
207 let mpi = self.move_data.moves[move_out_indices[0]].path;
208 let place = &self.move_data.move_paths[mpi].place;
210 if let Some(ty) = self.retrieve_type_for_place(place) {
211 let note_msg = match self.describe_place_with_options(
213 IncludingDowncast(true),
215 Some(name) => format!("`{}`", name),
216 None => "value".to_owned(),
220 "move occurs because {} has type `{}`, \
221 which does not implement the `Copy` trait",
228 if let Some((_, mut old_err)) = self.move_error_reported
229 .insert(move_out_indices, (used_place.clone(), err))
231 // Cancel the old error so it doesn't ICE.
237 pub(super) fn report_move_out_while_borrowed(
240 (place, span): (&Place<'tcx>, Span),
241 borrow: &BorrowData<'tcx>,
244 "report_move_out_while_borrowed: context={:?} place={:?} span={:?} borrow={:?}",
245 context, place, span, borrow
247 let tcx = self.infcx.tcx;
248 let value_msg = match self.describe_place(place) {
249 Some(name) => format!("`{}`", name),
250 None => "value".to_owned(),
252 let borrow_msg = match self.describe_place(&borrow.borrowed_place) {
253 Some(name) => format!("`{}`", name),
254 None => "value".to_owned(),
257 let borrow_spans = self.retrieve_borrow_spans(borrow);
258 let borrow_span = borrow_spans.args_or_use();
260 let move_spans = self.move_spans(place, context.loc);
261 let span = move_spans.args_or_use();
263 let mut err = tcx.cannot_move_when_borrowed(
265 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
268 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
269 err.span_label(span, format!("move out of {} occurs here", value_msg));
271 borrow_spans.var_span_label(
273 format!("borrow occurs due to use{}", borrow_spans.describe())
276 move_spans.var_span_label(
278 format!("move occurs due to use{}", move_spans.describe())
281 self.explain_why_borrow_contains_point(context, borrow, None)
282 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
283 err.buffer(&mut self.errors_buffer);
286 pub(super) fn report_use_while_mutably_borrowed(
289 (place, _span): (&Place<'tcx>, Span),
290 borrow: &BorrowData<'tcx>,
292 let tcx = self.infcx.tcx;
294 let borrow_spans = self.retrieve_borrow_spans(borrow);
295 let borrow_span = borrow_spans.args_or_use();
297 // Conflicting borrows are reported separately, so only check for move
299 let use_spans = self.move_spans(place, context.loc);
300 let span = use_spans.var_or_use();
302 let mut err = tcx.cannot_use_when_mutably_borrowed(
304 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
306 &self.describe_place(&borrow.borrowed_place)
307 .unwrap_or_else(|| "_".to_owned()),
311 borrow_spans.var_span_label(&mut err, {
312 let place = &borrow.borrowed_place;
313 let desc_place = self.describe_place(place).unwrap_or_else(|| "_".to_owned());
315 format!("borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe())
318 self.explain_why_borrow_contains_point(context, borrow, None)
319 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
320 err.buffer(&mut self.errors_buffer);
323 pub(super) fn report_conflicting_borrow(
326 (place, span): (&Place<'tcx>, Span),
327 gen_borrow_kind: BorrowKind,
328 issued_borrow: &BorrowData<'tcx>,
330 let issued_spans = self.retrieve_borrow_spans(issued_borrow);
331 let issued_span = issued_spans.args_or_use();
333 let borrow_spans = self.borrow_spans(span, context.loc);
334 let span = borrow_spans.args_or_use();
336 let container_name = if issued_spans.for_generator() || borrow_spans.for_generator() {
342 let desc_place = self.describe_place(place).unwrap_or_else(|| "_".to_owned());
343 let tcx = self.infcx.tcx;
345 let first_borrow_desc;
347 let explanation = self.explain_why_borrow_contains_point(context, issued_borrow, None);
348 let second_borrow_desc = if explanation.is_explained() {
354 // FIXME: supply non-"" `opt_via` when appropriate
355 let mut err = match (
363 (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => {
364 first_borrow_desc = "mutable ";
365 tcx.cannot_reborrow_already_borrowed(
378 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
379 first_borrow_desc = "immutable ";
380 tcx.cannot_reborrow_already_borrowed(
394 (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
395 first_borrow_desc = "first ";
396 tcx.cannot_mutably_borrow_multiply(
407 (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
408 first_borrow_desc = "first ";
409 tcx.cannot_uniquely_borrow_by_two_closures(
418 (BorrowKind::Mut { .. }, _, _, BorrowKind::Shallow, _, _)
419 | (BorrowKind::Unique, _, _, BorrowKind::Shallow, _, _) => {
420 let mut err = tcx.cannot_mutate_in_match_guard(
427 borrow_spans.var_span_label(
430 "borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe()
433 err.buffer(&mut self.errors_buffer);
438 (BorrowKind::Unique, _, _, _, _, _) => {
439 first_borrow_desc = "first ";
440 tcx.cannot_uniquely_borrow_by_one_closure(
453 (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
454 first_borrow_desc = "first ";
455 tcx.cannot_reborrow_already_uniquely_borrowed(
469 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
470 first_borrow_desc = "first ";
471 tcx.cannot_reborrow_already_uniquely_borrowed(
485 (BorrowKind::Shallow, _, _, BorrowKind::Unique, _, _)
486 | (BorrowKind::Shallow, _, _, BorrowKind::Mut { .. }, _, _) => {
487 // Shallow borrows are uses from the user's point of view.
488 self.report_use_while_mutably_borrowed(context, (place, span), issued_borrow);
491 (BorrowKind::Shared, _, _, BorrowKind::Shared, _, _)
492 | (BorrowKind::Shared, _, _, BorrowKind::Shallow, _, _)
493 | (BorrowKind::Shallow, _, _, BorrowKind::Shared, _, _)
494 | (BorrowKind::Shallow, _, _, BorrowKind::Shallow, _, _) => unreachable!(),
497 if issued_spans == borrow_spans {
498 borrow_spans.var_span_label(
500 format!("borrows occur due to use of `{}`{}", desc_place, borrow_spans.describe()),
503 let borrow_place = &issued_borrow.borrowed_place;
504 let borrow_place_desc = self.describe_place(borrow_place)
505 .unwrap_or_else(|| "_".to_owned());
506 issued_spans.var_span_label(
509 "first borrow occurs due to use of `{}`{}",
511 issued_spans.describe(),
515 borrow_spans.var_span_label(
518 "second borrow occurs due to use of `{}`{}",
520 borrow_spans.describe(),
526 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, first_borrow_desc);
528 err.buffer(&mut self.errors_buffer);
531 /// Reports StorageDeadOrDrop of `place` conflicts with `borrow`.
533 /// This means that some data referenced by `borrow` needs to live
534 /// past the point where the StorageDeadOrDrop of `place` occurs.
535 /// This is usually interpreted as meaning that `place` has too
536 /// short a lifetime. (But sometimes it is more useful to report
537 /// it as a more direct conflict between the execution of a
538 /// `Drop::drop` with an aliasing borrow.)
539 pub(super) fn report_borrowed_value_does_not_live_long_enough(
542 borrow: &BorrowData<'tcx>,
543 place_span: (&Place<'tcx>, Span),
544 kind: Option<WriteKind>,
547 "report_borrowed_value_does_not_live_long_enough(\
548 {:?}, {:?}, {:?}, {:?}\
550 context, borrow, place_span, kind
553 let drop_span = place_span.1;
554 let scope_tree = self.infcx.tcx.region_scope_tree(self.mir_def_id);
555 let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
559 let borrow_spans = self.retrieve_borrow_spans(borrow);
560 let borrow_span = borrow_spans.var_or_use();
562 let proper_span = match *root_place {
563 Place::Local(local) => self.mir.local_decls[local].source_info.span,
567 if self.access_place_error_reported
568 .contains(&(root_place.clone(), borrow_span))
571 "suppressing access_place error when borrow doesn't live long enough for {:?}",
577 self.access_place_error_reported
578 .insert((root_place.clone(), borrow_span));
580 if let StorageDeadOrDrop::Destructor(dropped_ty) =
581 self.classify_drop_access_kind(&borrow.borrowed_place)
583 // If a borrow of path `B` conflicts with drop of `D` (and
584 // we're not in the uninteresting case where `B` is a
585 // prefix of `D`), then report this as a more interesting
586 // destructor conflict.
587 if !borrow.borrowed_place.is_prefix_of(place_span.0) {
588 self.report_borrow_conflicts_with_destructor(
589 context, borrow, place_span, kind, dropped_ty,
595 let place_desc = self.describe_place(&borrow.borrowed_place);
597 let kind_place = kind.filter(|_| place_desc.is_some()).map(|k| (k, place_span.0));
598 let explanation = self.explain_why_borrow_contains_point(context, &borrow, kind_place);
600 let err = match (place_desc, explanation) {
601 (Some(_), _) if self.is_place_thread_local(root_place) => {
602 self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
604 // If the outlives constraint comes from inside the closure,
609 // Box::new(|| y) as Box<Fn() -> &'static i32>
611 // then just use the normal error. The closure isn't escaping
612 // and `move` will not help here.
615 BorrowExplanation::MustBeValidFor {
616 category: category @ ConstraintCategory::Return,
625 BorrowExplanation::MustBeValidFor {
626 category: category @ ConstraintCategory::CallArgument,
632 ) if borrow_spans.for_closure() => self.report_escaping_closure_capture(
633 borrow_spans.args_or_use(),
638 &format!("`{}`", name),
642 BorrowExplanation::MustBeValidFor {
643 category: ConstraintCategory::Assignment,
645 region_name: RegionName {
646 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
652 ) => self.report_escaping_data(borrow_span, name, upvar_span, upvar_name, span),
653 (Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
662 (None, explanation) => self.report_temporary_value_does_not_live_long_enough(
673 err.buffer(&mut self.errors_buffer);
676 fn report_local_value_does_not_live_long_enough(
680 scope_tree: &Lrc<ScopeTree>,
681 borrow: &BorrowData<'tcx>,
683 borrow_spans: UseSpans,
684 explanation: BorrowExplanation,
685 ) -> DiagnosticBuilder<'cx> {
687 "report_local_value_does_not_live_long_enough(\
688 {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
690 context, name, scope_tree, borrow, drop_span, borrow_spans
693 let borrow_span = borrow_spans.var_or_use();
694 if let BorrowExplanation::MustBeValidFor {
695 category: ConstraintCategory::Return,
701 return self.report_cannot_return_reference_to_local(
705 opt_place_desc.as_ref(),
709 let mut err = self.infcx.tcx.path_does_not_live_long_enough(
711 &format!("`{}`", name),
715 if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) {
716 let region_name = annotation.emit(&mut err);
720 format!("`{}` would have to be valid for `{}`...", name, region_name),
723 if let Some(fn_node_id) = self.infcx.tcx.hir().as_local_node_id(self.mir_def_id) {
727 "...but `{}` will be dropped here, when the function `{}` returns",
729 self.infcx.tcx.hir().name(fn_node_id),
734 "functions cannot return a borrow to data owned within the function's scope, \
735 functions can only return borrows to data passed as arguments",
738 "to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch04-02-\
739 references-and-borrowing.html#dangling-references>",
744 format!("...but `{}` dropped here while still borrowed", name),
748 if let BorrowExplanation::MustBeValidFor { .. } = explanation {
750 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
753 err.span_label(borrow_span, "borrowed value does not live long enough");
756 format!("`{}` dropped here while still borrowed", name),
759 let within = if borrow_spans.for_generator() {
765 borrow_spans.args_span_label(
767 format!("value captured here{}", within),
770 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
776 fn report_borrow_conflicts_with_destructor(
779 borrow: &BorrowData<'tcx>,
780 (place, drop_span): (&Place<'tcx>, Span),
781 kind: Option<WriteKind>,
782 dropped_ty: ty::Ty<'tcx>,
785 "report_borrow_conflicts_with_destructor(\
786 {:?}, {:?}, ({:?}, {:?}), {:?}\
788 context, borrow, place, drop_span, kind,
791 let borrow_spans = self.retrieve_borrow_spans(borrow);
792 let borrow_span = borrow_spans.var_or_use();
794 let mut err = self.infcx
796 .cannot_borrow_across_destructor(borrow_span, Origin::Mir);
798 let what_was_dropped = match self.describe_place(place) {
799 Some(name) => format!("`{}`", name.as_str()),
800 None => String::from("temporary value"),
803 let label = match self.describe_place(&borrow.borrowed_place) {
804 Some(borrowed) => format!(
805 "here, drop of {D} needs exclusive access to `{B}`, \
806 because the type `{T}` implements the `Drop` trait",
807 D = what_was_dropped,
812 "here is drop of {D}; whose type `{T}` implements the `Drop` trait",
813 D = what_was_dropped,
817 err.span_label(drop_span, label);
819 // Only give this note and suggestion if they could be relevant.
821 self.explain_why_borrow_contains_point(context, borrow, kind.map(|k| (k, place)));
823 BorrowExplanation::UsedLater { .. }
824 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
825 err.note("consider using a `let` binding to create a longer lived value");
830 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
832 err.buffer(&mut self.errors_buffer);
835 fn report_thread_local_value_does_not_live_long_enough(
839 ) -> DiagnosticBuilder<'cx> {
841 "report_thread_local_value_does_not_live_long_enough(\
844 drop_span, borrow_span
847 let mut err = self.infcx
849 .thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
853 "thread-local variables cannot be borrowed beyond the end of the function",
855 err.span_label(drop_span, "end of enclosing function is here");
860 fn report_temporary_value_does_not_live_long_enough(
863 scope_tree: &Lrc<ScopeTree>,
864 borrow: &BorrowData<'tcx>,
866 borrow_spans: UseSpans,
868 explanation: BorrowExplanation,
869 ) -> DiagnosticBuilder<'cx> {
871 "report_temporary_value_does_not_live_long_enough(\
872 {:?}, {:?}, {:?}, {:?}, {:?}\
874 context, scope_tree, borrow, drop_span, proper_span
877 if let BorrowExplanation::MustBeValidFor {
878 category: ConstraintCategory::Return,
883 return self.report_cannot_return_reference_to_local(
891 let tcx = self.infcx.tcx;
892 let mut err = tcx.temporary_value_borrowed_for_too_long(proper_span, Origin::Mir);
895 "creates a temporary which is freed while still in use",
899 "temporary value is freed at the end of this statement",
903 BorrowExplanation::UsedLater(..)
904 | BorrowExplanation::UsedLaterInLoop(..)
905 | BorrowExplanation::UsedLaterWhenDropped { .. } => {
906 // Only give this note and suggestion if it could be relevant.
907 err.note("consider using a `let` binding to create a longer lived value");
911 explanation.add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
913 let within = if borrow_spans.for_generator() {
919 borrow_spans.args_span_label(
921 format!("value captured here{}", within),
927 fn report_cannot_return_reference_to_local(
929 borrow: &BorrowData<'tcx>,
932 opt_place_desc: Option<&String>,
933 ) -> DiagnosticBuilder<'cx> {
934 let tcx = self.infcx.tcx;
936 // FIXME use a better heuristic than Spans
937 let reference_desc = if return_span == self.mir.source_info(borrow.reserve_location).span {
943 let (place_desc, note) = if let Some(place_desc) = opt_place_desc {
944 let local_kind = match borrow.borrowed_place {
945 Place::Local(local) => {
946 match self.mir.local_kind(local) {
947 LocalKind::ReturnPointer
948 | LocalKind::Temp => bug!("temporary or return pointer with a name"),
949 LocalKind::Var => "local variable ",
951 if !self.mir.upvar_decls.is_empty()
952 && local == Local::new(1) => {
953 "variable captured by `move` "
956 "function parameter "
963 format!("{}`{}`", local_kind, place_desc),
964 format!("`{}` is borrowed here", place_desc),
967 let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
970 let local = if let Place::Local(local) = *root_place {
973 bug!("report_cannot_return_reference_to_local: not a local")
975 match self.mir.local_kind(local) {
976 LocalKind::ReturnPointer | LocalKind::Temp => {
978 "temporary value".to_string(),
979 "temporary value created here".to_string(),
984 "function parameter".to_string(),
985 "function parameter borrowed here".to_string(),
988 LocalKind::Var => bug!("local variable without a name"),
992 let mut err = tcx.cannot_return_reference_to_local(
999 if return_span != borrow_span {
1000 err.span_label(borrow_span, note);
1006 fn report_escaping_closure_capture(
1010 fr_name: &RegionName,
1011 category: ConstraintCategory,
1012 constraint_span: Span,
1014 ) -> DiagnosticBuilder<'cx> {
1015 let tcx = self.infcx.tcx;
1017 let mut err = tcx.cannot_capture_in_long_lived_closure(
1024 let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) {
1025 Ok(string) => format!("move {}", string),
1026 Err(_) => "move |<args>| <body>".to_string()
1029 err.span_suggestion_with_applicability(
1031 &format!("to force the closure to take ownership of {} (and any \
1032 other referenced variables), use the `move` keyword",
1035 Applicability::MachineApplicable,
1039 ConstraintCategory::Return => {
1040 err.span_note(constraint_span, "closure is returned here");
1042 ConstraintCategory::CallArgument => {
1043 fr_name.highlight_region_name(&mut err);
1046 &format!("function requires argument type to outlive `{}`", fr_name),
1049 _ => bug!("report_escaping_closure_capture called with unexpected constraint \
1050 category: `{:?}`", category),
1055 fn report_escaping_data(
1058 name: &Option<String>,
1062 ) -> DiagnosticBuilder<'cx> {
1063 let tcx = self.infcx.tcx;
1065 let escapes_from = if tcx.is_closure(self.mir_def_id) {
1066 let tables = tcx.typeck_tables_of(self.mir_def_id);
1067 let mir_hir_id = tcx.hir().def_index_to_hir_id(self.mir_def_id.index);
1068 match tables.node_id_to_type(mir_hir_id).sty {
1069 ty::Closure(..) => "closure",
1070 ty::Generator(..) => "generator",
1071 _ => bug!("Closure body doesn't have a closure or generator type"),
1077 let mut err = tcx.borrowed_data_escapes_closure(escape_span, escapes_from, Origin::Mir);
1082 "`{}` is declared here, outside of the {} body",
1083 upvar_name, escapes_from
1090 "borrow is only valid in the {} body",
1095 if let Some(name) = name {
1098 format!("reference to `{}` escapes the {} body here", name, escapes_from),
1103 format!("reference escapes the {} body here", escapes_from),
1110 fn get_moved_indexes(&mut self, context: Context, mpi: MovePathIndex) -> Vec<MoveSite> {
1113 let mut stack = Vec::new();
1114 stack.extend(mir.predecessor_locations(context.loc).map(|predecessor| {
1115 let is_back_edge = context.loc.dominates(predecessor, &self.dominators);
1116 (predecessor, is_back_edge)
1119 let mut visited = FxHashSet::default();
1120 let mut result = vec![];
1122 'dfs: while let Some((location, is_back_edge)) = stack.pop() {
1124 "report_use_of_moved_or_uninitialized: (current_location={:?}, back_edge={})",
1125 location, is_back_edge
1128 if !visited.insert(location) {
1133 let stmt_kind = mir[location.block]
1135 .get(location.statement_index)
1137 if let Some(StatementKind::StorageDead(..)) = stmt_kind {
1138 // this analysis only tries to find moves explicitly
1139 // written by the user, so we ignore the move-outs
1140 // created by `StorageDead` and at the beginning
1143 // If we are found a use of a.b.c which was in error, then we want to look for
1144 // moves not only of a.b.c but also a.b and a.
1146 // Note that the moves data already includes "parent" paths, so we don't have to
1147 // worry about the other case: that is, if there is a move of a.b.c, it is already
1148 // marked as a move of a.b and a as well, so we will generate the correct errors
1150 let mut mpis = vec![mpi];
1151 let move_paths = &self.move_data.move_paths;
1152 mpis.extend(move_paths[mpi].parents(move_paths));
1154 for moi in &self.move_data.loc_map[location] {
1155 debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
1156 if mpis.contains(&self.move_data.moves[*moi].path) {
1157 debug!("report_use_of_moved_or_uninitialized: found");
1158 result.push(MoveSite {
1160 traversed_back_edge: is_back_edge,
1163 // Strictly speaking, we could continue our DFS here. There may be
1164 // other moves that can reach the point of error. But it is kind of
1165 // confusing to highlight them.
1173 // drop(a); // <-- current point of error
1176 // Because we stop the DFS here, we only highlight `let c = a`,
1177 // and not `let b = a`. We will of course also report an error at
1178 // `let c = a` which highlights `let b = a` as the move.
1185 let mut any_match = false;
1186 drop_flag_effects::for_location_inits(
1201 stack.extend(mir.predecessor_locations(location).map(|predecessor| {
1202 let back_edge = location.dominates(predecessor, &self.dominators);
1203 (predecessor, is_back_edge || back_edge)
1210 pub(super) fn report_illegal_mutation_of_borrowed(
1213 (place, span): (&Place<'tcx>, Span),
1214 loan: &BorrowData<'tcx>,
1216 let loan_spans = self.retrieve_borrow_spans(loan);
1217 let loan_span = loan_spans.args_or_use();
1219 let tcx = self.infcx.tcx;
1220 let mut err = if loan.kind == BorrowKind::Shallow {
1221 tcx.cannot_mutate_in_match_guard(
1224 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1229 tcx.cannot_assign_to_borrowed(
1232 &self.describe_place(place).unwrap_or_else(|| "_".to_owned()),
1237 loan_spans.var_span_label(
1239 format!("borrow occurs due to use{}", loan_spans.describe()),
1242 self.explain_why_borrow_contains_point(context, loan, None)
1243 .add_explanation_to_diagnostic(self.infcx.tcx, self.mir, &mut err, "");
1245 err.buffer(&mut self.errors_buffer);
1248 /// Reports an illegal reassignment; for example, an assignment to
1249 /// (part of) a non-`mut` local that occurs potentially after that
1250 /// local has already been initialized. `place` is the path being
1251 /// assigned; `err_place` is a place providing a reason why
1252 /// `place` is not mutable (e.g., the non-`mut` local `x` in an
1253 /// assignment to `x.f`).
1254 pub(super) fn report_illegal_reassignment(
1257 (place, span): (&Place<'tcx>, Span),
1258 assigned_span: Span,
1259 err_place: &Place<'tcx>,
1261 let (from_arg, local_decl) = if let Place::Local(local) = *err_place {
1262 if let LocalKind::Arg = self.mir.local_kind(local) {
1263 (true, Some(&self.mir.local_decls[local]))
1265 (false, Some(&self.mir.local_decls[local]))
1271 // If root local is initialized immediately (everything apart from let
1272 // PATTERN;) then make the error refer to that local, rather than the
1273 // place being assigned later.
1274 let (place_description, assigned_span) = match local_decl {
1276 is_user_variable: Some(ClearCrossCrate::Clear),
1281 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
1282 opt_match_place: None,
1288 is_user_variable: None,
1291 | None => (self.describe_place(place), assigned_span),
1292 Some(decl) => (self.describe_place(err_place), decl.source_info.span),
1295 let mut err = self.infcx.tcx.cannot_reassign_immutable(
1297 place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
1301 let msg = if from_arg {
1302 "cannot assign to immutable argument"
1304 "cannot assign twice to immutable variable"
1306 if span != assigned_span {
1308 let value_msg = match place_description {
1309 Some(name) => format!("`{}`", name),
1310 None => "value".to_owned(),
1312 err.span_label(assigned_span, format!("first assignment to {}", value_msg));
1315 if let Some(decl) = local_decl {
1316 if let Some(name) = decl.name {
1317 if decl.can_be_made_mutable() {
1318 err.span_suggestion_with_applicability(
1319 decl.source_info.span,
1320 "make this binding mutable",
1321 format!("mut {}", name),
1322 Applicability::MachineApplicable,
1327 err.span_label(span, msg);
1328 err.buffer(&mut self.errors_buffer);
1332 pub(super) struct IncludingDowncast(bool);
1334 /// Which case a StorageDeadOrDrop is for.
1335 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
1336 enum StorageDeadOrDrop<'tcx> {
1339 Destructor(ty::Ty<'tcx>),
1342 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1344 /// Adds a suggestion when a closure is invoked twice with a moved variable.
1347 /// note: closure cannot be invoked more than once because it moves the variable `dict` out of
1349 /// --> $DIR/issue-42065.rs:16:29
1351 /// LL | for (key, value) in dict {
1354 pub(super) fn add_closure_invoked_twice_with_moved_variable_suggestion(
1357 place: &Place<'tcx>,
1358 diag: &mut DiagnosticBuilder<'_>,
1360 let mut target = place.local();
1362 "add_closure_invoked_twice_with_moved_variable_suggestion: location={:?} place={:?} \
1364 location, place, target,
1366 for stmt in &self.mir[location.block].statements[location.statement_index..] {
1368 "add_closure_invoked_twice_with_moved_variable_suggestion: stmt={:?} \
1372 if let StatementKind::Assign(into, box Rvalue::Use(from)) = &stmt.kind {
1374 "add_closure_invoked_twice_with_moved_variable_suggestion: into={:?} \
1379 Operand::Copy(ref place) |
1380 Operand::Move(ref place) if target == place.local() =>
1381 target = into.local(),
1388 let terminator = self.mir[location.block].terminator();
1390 "add_closure_invoked_twice_with_moved_variable_suggestion: terminator={:?}",
1393 if let TerminatorKind::Call {
1394 func: Operand::Constant(box Constant {
1395 literal: ty::Const { ty: &ty::TyS { sty: ty::TyKind::FnDef(id, _), .. }, .. },
1400 } = &terminator.kind {
1401 debug!("add_closure_invoked_twice_with_moved_variable_suggestion: id={:?}", id);
1402 if self.infcx.tcx.parent(id) == self.infcx.tcx.lang_items().fn_once_trait() {
1403 let closure = match args.first() {
1404 Some(Operand::Copy(ref place)) |
1405 Some(Operand::Move(ref place)) if target == place.local() =>
1406 place.local().unwrap(),
1410 "add_closure_invoked_twice_with_moved_variable_suggestion: closure={:?}",
1414 if let ty::TyKind::Closure(did, _substs) = self.mir.local_decls[closure].ty.sty {
1415 let node_id = match self.infcx.tcx.hir().as_local_node_id(did) {
1416 Some(node_id) => node_id,
1419 let hir_id = self.infcx.tcx.hir().node_to_hir_id(node_id);
1423 )) = self.infcx.tcx.typeck_tables_of(did).closure_kind_origins().get(hir_id) {
1427 "closure cannot be invoked more than once because it \
1428 moves the variable `{}` out of its environment",
1438 /// End-user visible description of `place` if one can be found. If the
1439 /// place is a temporary for instance, None will be returned.
1440 pub(super) fn describe_place(&self, place: &Place<'tcx>) -> Option<String> {
1441 self.describe_place_with_options(place, IncludingDowncast(false))
1444 /// End-user visible description of `place` if one can be found. If the
1445 /// place is a temporary for instance, None will be returned.
1446 /// `IncludingDowncast` parameter makes the function return `Err` if `ProjectionElem` is
1447 /// `Downcast` and `IncludingDowncast` is true
1448 pub(super) fn describe_place_with_options(
1450 place: &Place<'tcx>,
1451 including_downcast: IncludingDowncast,
1452 ) -> Option<String> {
1453 let mut buf = String::new();
1454 match self.append_place_to_string(place, &mut buf, false, &including_downcast) {
1455 Ok(()) => Some(buf),
1460 /// Appends end-user visible description of `place` to `buf`.
1461 fn append_place_to_string(
1463 place: &Place<'tcx>,
1465 mut autoderef: bool,
1466 including_downcast: &IncludingDowncast,
1467 ) -> Result<(), ()> {
1469 Place::Promoted(_) => {
1470 buf.push_str("promoted");
1472 Place::Local(local) => {
1473 self.append_local_to_string(local, buf)?;
1475 Place::Static(ref static_) => {
1476 buf.push_str(&self.infcx.tcx.item_name(static_.def_id).to_string());
1478 Place::Projection(ref proj) => {
1480 ProjectionElem::Deref => {
1481 let upvar_field_projection =
1482 place.is_upvar_field_projection(self.mir, &self.infcx.tcx);
1483 if let Some(field) = upvar_field_projection {
1484 let var_index = field.index();
1485 let name = self.mir.upvar_decls[var_index].debug_name.to_string();
1486 if self.mir.upvar_decls[var_index].by_ref {
1487 buf.push_str(&name);
1489 buf.push_str(&format!("*{}", &name));
1493 self.append_place_to_string(
1497 &including_downcast,
1499 } else if let Place::Local(local) = proj.base {
1500 if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
1501 self.mir.local_decls[local].is_user_variable
1503 self.append_place_to_string(
1507 &including_downcast,
1511 self.append_place_to_string(
1515 &including_downcast,
1520 self.append_place_to_string(
1524 &including_downcast,
1529 ProjectionElem::Downcast(..) => {
1530 self.append_place_to_string(
1534 &including_downcast,
1536 if including_downcast.0 {
1540 ProjectionElem::Field(field, _ty) => {
1543 let upvar_field_projection =
1544 place.is_upvar_field_projection(self.mir, &self.infcx.tcx);
1545 if let Some(field) = upvar_field_projection {
1546 let var_index = field.index();
1547 let name = self.mir.upvar_decls[var_index].debug_name.to_string();
1548 buf.push_str(&name);
1550 let field_name = self.describe_field(&proj.base, field);
1551 self.append_place_to_string(
1555 &including_downcast,
1557 buf.push_str(&format!(".{}", field_name));
1560 ProjectionElem::Index(index) => {
1563 self.append_place_to_string(
1567 &including_downcast,
1570 if self.append_local_to_string(index, buf).is_err() {
1575 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
1577 // Since it isn't possible to borrow an element on a particular index and
1578 // then use another while the borrow is held, don't output indices details
1579 // to avoid confusing the end-user
1580 self.append_place_to_string(
1584 &including_downcast,
1586 buf.push_str(&"[..]");
1595 /// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
1596 /// a name, then `Err` is returned
1597 fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
1598 let local = &self.mir.local_decls[local_index];
1601 buf.push_str(&name.to_string());
1608 /// End-user visible description of the `field`nth field of `base`
1609 fn describe_field(&self, base: &Place, field: Field) -> String {
1611 Place::Local(local) => {
1612 let local = &self.mir.local_decls[local];
1613 self.describe_field_from_ty(&local.ty, field)
1615 Place::Promoted(ref prom) => self.describe_field_from_ty(&prom.1, field),
1616 Place::Static(ref static_) => self.describe_field_from_ty(&static_.ty, field),
1617 Place::Projection(ref proj) => match proj.elem {
1618 ProjectionElem::Deref => self.describe_field(&proj.base, field),
1619 ProjectionElem::Downcast(def, variant_index) =>
1620 def.variants[variant_index].fields[field.index()].ident.to_string(),
1621 ProjectionElem::Field(_, field_type) => {
1622 self.describe_field_from_ty(&field_type, field)
1624 ProjectionElem::Index(..)
1625 | ProjectionElem::ConstantIndex { .. }
1626 | ProjectionElem::Subslice { .. } => {
1627 self.describe_field(&proj.base, field)
1633 /// End-user visible description of the `field_index`nth field of `ty`
1634 fn describe_field_from_ty(&self, ty: &ty::Ty, field: Field) -> String {
1636 // If the type is a box, the field is described from the boxed type
1637 self.describe_field_from_ty(&ty.boxed_ty(), field)
1640 ty::Adt(def, _) => if def.is_enum() {
1641 field.index().to_string()
1643 def.non_enum_variant().fields[field.index()]
1647 ty::Tuple(_) => field.index().to_string(),
1648 ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
1649 self.describe_field_from_ty(&ty, field)
1651 ty::Array(ty, _) | ty::Slice(ty) => self.describe_field_from_ty(&ty, field),
1652 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
1653 // Convert the def-id into a node-id. node-ids are only valid for
1654 // the local code in the current crate, so this returns an `Option` in case
1655 // the closure comes from another crate. But in that case we wouldn't
1656 // be borrowck'ing it, so we can just unwrap:
1657 let node_id = self.infcx.tcx.hir().as_local_node_id(def_id).unwrap();
1658 let freevar = self.infcx
1660 .with_freevars(node_id, |fv| fv[field.index()]);
1662 self.infcx.tcx.hir().name(freevar.var_id()).to_string()
1665 // Might need a revision when the fields in trait RFC is implemented
1666 // (https://github.com/rust-lang/rfcs/pull/1546)
1668 "End-user description not implemented for field access on `{:?}`",
1676 /// Retrieve type of a place for the current MIR representation
1677 fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
1679 Place::Local(local) => {
1680 let local = &self.mir.local_decls[*local];
1683 Place::Promoted(ref prom) => Some(prom.1),
1684 Place::Static(ref st) => Some(st.ty),
1685 Place::Projection(ref proj) => match proj.elem {
1686 ProjectionElem::Field(_, ty) => Some(ty),
1692 /// Check if a place is a thread-local static.
1693 pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
1694 if let Place::Static(statik) = place {
1695 let attrs = self.infcx.tcx.get_attrs(statik.def_id);
1696 let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
1699 "is_place_thread_local: attrs={:?} is_thread_local={:?}",
1700 attrs, is_thread_local
1704 debug!("is_place_thread_local: no");
1709 fn classify_drop_access_kind(&self, place: &Place<'tcx>) -> StorageDeadOrDrop<'tcx> {
1710 let tcx = self.infcx.tcx;
1712 Place::Local(_) | Place::Static(_) | Place::Promoted(_) => {
1713 StorageDeadOrDrop::LocalStorageDead
1715 Place::Projection(box PlaceProjection { base, elem }) => {
1716 let base_access = self.classify_drop_access_kind(base);
1718 ProjectionElem::Deref => match base_access {
1719 StorageDeadOrDrop::LocalStorageDead
1720 | StorageDeadOrDrop::BoxedStorageDead => {
1722 base.ty(self.mir, tcx).to_ty(tcx).is_box(),
1723 "Drop of value behind a reference or raw pointer"
1725 StorageDeadOrDrop::BoxedStorageDead
1727 StorageDeadOrDrop::Destructor(_) => base_access,
1729 ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
1730 let base_ty = base.ty(self.mir, tcx).to_ty(tcx);
1732 ty::Adt(def, _) if def.has_dtor(tcx) => {
1733 // Report the outermost adt with a destructor
1735 StorageDeadOrDrop::Destructor(_) => base_access,
1736 StorageDeadOrDrop::LocalStorageDead
1737 | StorageDeadOrDrop::BoxedStorageDead => {
1738 StorageDeadOrDrop::Destructor(base_ty)
1746 ProjectionElem::ConstantIndex { .. }
1747 | ProjectionElem::Subslice { .. }
1748 | ProjectionElem::Index(_) => base_access,
1754 /// Annotate argument and return type of function and closure with (synthesized) lifetime for
1755 /// borrow of local value that does not live long enough.
1756 fn annotate_argument_and_return_for_borrow(
1758 borrow: &BorrowData<'tcx>,
1759 ) -> Option<AnnotatedBorrowFnSignature> {
1760 // Define a fallback for when we can't match a closure.
1762 let is_closure = self.infcx.tcx.is_closure(self.mir_def_id);
1766 let ty = self.infcx.tcx.type_of(self.mir_def_id);
1768 ty::TyKind::FnDef(_, _) | ty::TyKind::FnPtr(_) => self.annotate_fn_sig(
1770 self.infcx.tcx.fn_sig(self.mir_def_id),
1777 // In order to determine whether we need to annotate, we need to check whether the reserve
1778 // place was an assignment into a temporary.
1780 // If it was, we check whether or not that temporary is eventually assigned into the return
1781 // place. If it was, we can add annotations about the function's return type and arguments
1782 // and it'll make sense.
1783 let location = borrow.reserve_location;
1785 "annotate_argument_and_return_for_borrow: location={:?}",
1788 if let Some(&Statement { kind: StatementKind::Assign(ref reservation, _), ..})
1789 = &self.mir[location.block].statements.get(location.statement_index)
1792 "annotate_argument_and_return_for_borrow: reservation={:?}",
1795 // Check that the initial assignment of the reserve location is into a temporary.
1796 let mut target = *match reservation {
1797 Place::Local(local) if self.mir.local_kind(*local) == LocalKind::Temp => local,
1801 // Next, look through the rest of the block, checking if we are assigning the
1802 // `target` (that is, the place that contains our borrow) to anything.
1803 let mut annotated_closure = None;
1804 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
1806 "annotate_argument_and_return_for_borrow: target={:?} stmt={:?}",
1809 if let StatementKind::Assign(Place::Local(assigned_to), box rvalue) = &stmt.kind
1812 "annotate_argument_and_return_for_borrow: assigned_to={:?} \
1816 // Check if our `target` was captured by a closure.
1817 if let Rvalue::Aggregate(
1818 box AggregateKind::Closure(def_id, substs),
1822 for operand in operands {
1823 let assigned_from = match operand {
1824 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1830 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1834 // Find the local from the operand.
1835 let assigned_from_local = match assigned_from.local() {
1836 Some(local) => local,
1840 if assigned_from_local != target {
1844 // If a closure captured our `target` and then assigned
1845 // into a place then we should annotate the closure in
1846 // case it ends up being assigned into the return place.
1847 annotated_closure = self.annotate_fn_sig(
1849 self.infcx.closure_sig(*def_id, *substs),
1852 "annotate_argument_and_return_for_borrow: \
1853 annotated_closure={:?} assigned_from_local={:?} \
1855 annotated_closure, assigned_from_local, assigned_to
1858 if *assigned_to == mir::RETURN_PLACE {
1859 // If it was assigned directly into the return place, then
1861 return annotated_closure;
1863 // Otherwise, update the target.
1864 target = *assigned_to;
1868 // If none of our closure's operands matched, then skip to the next
1873 // Otherwise, look at other types of assignment.
1874 let assigned_from = match rvalue {
1875 Rvalue::Ref(_, _, assigned_from) => assigned_from,
1876 Rvalue::Use(operand) => match operand {
1877 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1885 "annotate_argument_and_return_for_borrow: \
1886 assigned_from={:?}",
1890 // Find the local from the rvalue.
1891 let assigned_from_local = match assigned_from.local() {
1892 Some(local) => local,
1896 "annotate_argument_and_return_for_borrow: \
1897 assigned_from_local={:?}",
1898 assigned_from_local,
1901 // Check if our local matches the target - if so, we've assigned our
1902 // borrow to a new place.
1903 if assigned_from_local != target {
1907 // If we assigned our `target` into a new place, then we should
1908 // check if it was the return place.
1910 "annotate_argument_and_return_for_borrow: \
1911 assigned_from_local={:?} assigned_to={:?}",
1912 assigned_from_local, assigned_to
1914 if *assigned_to == mir::RETURN_PLACE {
1915 // If it was then return the annotated closure if there was one,
1916 // else, annotate this function.
1917 return annotated_closure.or_else(fallback);
1920 // If we didn't assign into the return place, then we just update
1922 target = *assigned_to;
1926 // Check the terminator if we didn't find anything in the statements.
1927 let terminator = &self.mir[location.block].terminator();
1929 "annotate_argument_and_return_for_borrow: target={:?} terminator={:?}",
1932 if let TerminatorKind::Call {
1933 destination: Some((Place::Local(assigned_to), _)),
1936 } = &terminator.kind
1939 "annotate_argument_and_return_for_borrow: assigned_to={:?} args={:?}",
1942 for operand in args {
1943 let assigned_from = match operand {
1944 Operand::Copy(assigned_from) | Operand::Move(assigned_from) => {
1950 "annotate_argument_and_return_for_borrow: assigned_from={:?}",
1954 if let Some(assigned_from_local) = assigned_from.local() {
1956 "annotate_argument_and_return_for_borrow: assigned_from_local={:?}",
1957 assigned_from_local,
1960 if *assigned_to == mir::RETURN_PLACE && assigned_from_local == target {
1961 return annotated_closure.or_else(fallback);
1968 // If we haven't found an assignment into the return place, then we need not add
1970 debug!("annotate_argument_and_return_for_borrow: none found");
1974 /// Annotate the first argument and return type of a function signature if they are
1979 sig: ty::PolyFnSig<'tcx>,
1980 ) -> Option<AnnotatedBorrowFnSignature> {
1981 debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
1982 let is_closure = self.infcx.tcx.is_closure(did);
1983 let fn_node_id = self.infcx.tcx.hir().as_local_node_id(did)?;
1984 let fn_decl = self.infcx.tcx.hir().fn_decl(fn_node_id)?;
1986 // We need to work out which arguments to highlight. We do this by looking
1987 // at the return type, where there are three cases:
1989 // 1. If there are named arguments, then we should highlight the return type and
1990 // highlight any of the arguments that are also references with that lifetime.
1991 // If there are no arguments that have the same lifetime as the return type,
1992 // then don't highlight anything.
1993 // 2. The return type is a reference with an anonymous lifetime. If this is
1994 // the case, then we can take advantage of (and teach) the lifetime elision
1997 // We know that an error is being reported. So the arguments and return type
1998 // must satisfy the elision rules. Therefore, if there is a single argument
1999 // then that means the return type and first (and only) argument have the same
2000 // lifetime and the borrow isn't meeting that, we can highlight the argument
2003 // If there are multiple arguments then the first argument must be self (else
2004 // it would not satisfy the elision rules), so we can highlight self and the
2006 // 3. The return type is not a reference. In this case, we don't highlight
2008 let return_ty = sig.output();
2009 match return_ty.skip_binder().sty {
2010 ty::TyKind::Ref(return_region, _, _) if return_region.has_name() && !is_closure => {
2011 // This is case 1 from above, return type is a named reference so we need to
2012 // search for relevant arguments.
2013 let mut arguments = Vec::new();
2014 for (index, argument) in sig.inputs().skip_binder().iter().enumerate() {
2015 if let ty::TyKind::Ref(argument_region, _, _) = argument.sty {
2016 if argument_region == return_region {
2017 // Need to use the `rustc::ty` types to compare against the
2018 // `return_region`. Then use the `rustc::hir` type to get only
2019 // the lifetime span.
2020 if let hir::TyKind::Rptr(lifetime, _) = &fn_decl.inputs[index].node {
2021 // With access to the lifetime, we can get
2023 arguments.push((*argument, lifetime.span));
2025 bug!("ty type is a ref but hir type is not");
2031 // We need to have arguments. This shouldn't happen, but it's worth checking.
2032 if arguments.is_empty() {
2036 // We use a mix of the HIR and the Ty types to get information
2037 // as the HIR doesn't have full types for closure arguments.
2038 let return_ty = *sig.output().skip_binder();
2039 let mut return_span = fn_decl.output.span();
2040 if let hir::FunctionRetTy::Return(ty) = fn_decl.output {
2041 if let hir::TyKind::Rptr(lifetime, _) = ty.into_inner().node {
2042 return_span = lifetime.span;
2046 Some(AnnotatedBorrowFnSignature::NamedFunction {
2052 ty::TyKind::Ref(_, _, _) if is_closure => {
2053 // This is case 2 from above but only for closures, return type is anonymous
2054 // reference so we select
2055 // the first argument.
2056 let argument_span = fn_decl.inputs.first()?.span;
2057 let argument_ty = sig.inputs().skip_binder().first()?;
2059 // Closure arguments are wrapped in a tuple, so we need to get the first
2061 if let ty::TyKind::Tuple(elems) = argument_ty.sty {
2062 let argument_ty = elems.first()?;
2063 if let ty::TyKind::Ref(_, _, _) = argument_ty.sty {
2064 return Some(AnnotatedBorrowFnSignature::Closure {
2073 ty::TyKind::Ref(_, _, _) => {
2074 // This is also case 2 from above but for functions, return type is still an
2075 // anonymous reference so we select the first argument.
2076 let argument_span = fn_decl.inputs.first()?.span;
2077 let argument_ty = sig.inputs().skip_binder().first()?;
2079 let return_span = fn_decl.output.span();
2080 let return_ty = *sig.output().skip_binder();
2082 // We expect the first argument to be a reference.
2083 match argument_ty.sty {
2084 ty::TyKind::Ref(_, _, _) => {}
2088 Some(AnnotatedBorrowFnSignature::AnonymousFunction {
2096 // This is case 3 from above, return type is not a reference so don't highlight
2105 enum AnnotatedBorrowFnSignature<'tcx> {
2107 arguments: Vec<(ty::Ty<'tcx>, Span)>,
2108 return_ty: ty::Ty<'tcx>,
2112 argument_ty: ty::Ty<'tcx>,
2113 argument_span: Span,
2114 return_ty: ty::Ty<'tcx>,
2118 argument_ty: ty::Ty<'tcx>,
2119 argument_span: Span,
2123 impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
2124 /// Annotate the provided diagnostic with information about borrow from the fn signature that
2126 fn emit(&self, diag: &mut DiagnosticBuilder<'_>) -> String {
2128 AnnotatedBorrowFnSignature::Closure {
2134 format!("has type `{}`", self.get_name_for_ty(argument_ty, 0)),
2137 self.get_region_name_for_ty(argument_ty, 0)
2139 AnnotatedBorrowFnSignature::AnonymousFunction {
2145 let argument_ty_name = self.get_name_for_ty(argument_ty, 0);
2146 diag.span_label(*argument_span, format!("has type `{}`", argument_ty_name));
2148 let return_ty_name = self.get_name_for_ty(return_ty, 0);
2149 let types_equal = return_ty_name == argument_ty_name;
2154 if types_equal { "also " } else { "" },
2160 "argument and return type have the same lifetime due to lifetime elision rules",
2163 "to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch10-03-\
2164 lifetime-syntax.html#lifetime-elision>",
2167 self.get_region_name_for_ty(return_ty, 0)
2169 AnnotatedBorrowFnSignature::NamedFunction {
2174 // Region of return type and arguments checked to be the same earlier.
2175 let region_name = self.get_region_name_for_ty(return_ty, 0);
2176 for (_, argument_span) in arguments {
2177 diag.span_label(*argument_span, format!("has lifetime `{}`", region_name));
2182 format!("also has lifetime `{}`", region_name,),
2186 "use data from the highlighted arguments which match the `{}` lifetime of \
2196 /// Return the name of the provided `Ty` (that must be a reference) with a synthesized lifetime
2197 /// name where required.
2198 fn get_name_for_ty(&self, ty: ty::Ty<'tcx>, counter: usize) -> String {
2199 // We need to add synthesized lifetimes where appropriate. We do
2200 // this by hooking into the pretty printer and telling it to label the
2201 // lifetimes without names with the value `'0`.
2203 ty::TyKind::Ref(ty::RegionKind::ReLateBound(_, br), _, _)
2205 ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }),
2208 ) => with_highlight_region_for_bound_region(*br, counter, || ty.to_string()),
2209 _ => ty.to_string(),
2213 /// Return the name of the provided `Ty` (that must be a reference)'s region with a
2214 /// synthesized lifetime name where required.
2215 fn get_region_name_for_ty(&self, ty: ty::Ty<'tcx>, counter: usize) -> String {
2217 ty::TyKind::Ref(region, _, _) => match region {
2218 ty::RegionKind::ReLateBound(_, br)
2219 | ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
2220 with_highlight_region_for_bound_region(*br, counter, || region.to_string())
2222 _ => region.to_string(),
2224 _ => bug!("ty for annotation of borrow region is not a reference"),
2229 // The span(s) associated to a use of a place.
2230 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
2231 pub(super) enum UseSpans {
2232 // The access is caused by capturing a variable for a closure.
2234 // This is true if the captured variable was from a generator.
2236 // The span of the args of the closure, including the `move` keyword if
2239 // The span of the first use of the captured variable inside the closure.
2242 // This access has a single span associated to it: common case.
2247 pub(super) fn args_or_use(self) -> Span {
2249 UseSpans::ClosureUse {
2252 | UseSpans::OtherUse(span) => span,
2256 pub(super) fn var_or_use(self) -> Span {
2258 UseSpans::ClosureUse { var_span: span, .. } | UseSpans::OtherUse(span) => span,
2262 // Add a span label to the arguments of the closure, if it exists.
2263 pub(super) fn args_span_label(self, err: &mut DiagnosticBuilder, message: impl Into<String>) {
2264 if let UseSpans::ClosureUse { args_span, .. } = self {
2265 err.span_label(args_span, message);
2269 // Add a span label to the use of the captured variable, if it exists.
2270 pub(super) fn var_span_label(self, err: &mut DiagnosticBuilder, message: impl Into<String>) {
2271 if let UseSpans::ClosureUse { var_span, .. } = self {
2272 err.span_label(var_span, message);
2276 /// Return `false` if this place is not used in a closure.
2277 fn for_closure(&self) -> bool {
2279 UseSpans::ClosureUse { is_generator, .. } => !is_generator,
2284 /// Return `false` if this place is not used in a generator.
2285 fn for_generator(&self) -> bool {
2287 UseSpans::ClosureUse { is_generator, .. } => is_generator,
2292 /// Describe the span associated with a use of a place.
2293 fn describe(&self) -> String {
2295 UseSpans::ClosureUse { is_generator, .. } => if is_generator {
2296 " in generator".to_string()
2298 " in closure".to_string()
2300 _ => "".to_string(),
2304 pub(super) fn or_else<F>(self, if_other: F) -> Self
2306 F: FnOnce() -> Self,
2309 closure @ UseSpans::ClosureUse { .. } => closure,
2310 UseSpans::OtherUse(_) => if_other(),
2315 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
2316 /// Finds the spans associated to a move or copy of move_place at location.
2317 pub(super) fn move_spans(
2319 moved_place: &Place<'tcx>, // Could also be an upvar.
2322 use self::UseSpans::*;
2324 let stmt = match self.mir[location.block].statements.get(location.statement_index) {
2326 None => return OtherUse(self.mir.source_info(location).span),
2329 debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
2330 if let StatementKind::Assign(
2332 box Rvalue::Aggregate(ref kind, ref places)
2334 let (def_id, is_generator) = match kind {
2335 box AggregateKind::Closure(def_id, _) => (def_id, false),
2336 box AggregateKind::Generator(def_id, _, _) => (def_id, true),
2337 _ => return OtherUse(stmt.source_info.span),
2341 "move_spans: def_id={:?} is_generator={:?} places={:?}",
2342 def_id, is_generator, places
2344 if let Some((args_span, var_span)) = self.closure_span(*def_id, moved_place, places) {
2353 OtherUse(stmt.source_info.span)
2356 /// Finds the span of arguments of a closure (within `maybe_closure_span`)
2357 /// and its usage of the local assigned at `location`.
2358 /// This is done by searching in statements succeeding `location`
2359 /// and originating from `maybe_closure_span`.
2360 pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans {
2361 use self::UseSpans::*;
2362 debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
2364 let target = match self.mir[location.block]
2366 .get(location.statement_index)
2369 kind: StatementKind::Assign(Place::Local(local), _),
2372 _ => return OtherUse(use_span),
2375 if self.mir.local_kind(target) != LocalKind::Temp {
2376 // operands are always temporaries.
2377 return OtherUse(use_span);
2380 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
2381 if let StatementKind::Assign(
2382 _, box Rvalue::Aggregate(ref kind, ref places)
2384 let (def_id, is_generator) = match kind {
2385 box AggregateKind::Closure(def_id, _) => (def_id, false),
2386 box AggregateKind::Generator(def_id, _, _) => (def_id, true),
2391 "borrow_spans: def_id={:?} is_generator={:?} places={:?}",
2392 def_id, is_generator, places
2394 if let Some((args_span, var_span)) = self.closure_span(
2395 *def_id, &Place::Local(target), places
2403 return OtherUse(use_span);
2407 if use_span != stmt.source_info.span {
2415 /// Finds the span of a captured variable within a closure or generator.
2419 target_place: &Place<'tcx>,
2420 places: &Vec<Operand<'tcx>>,
2421 ) -> Option<(Span, Span)> {
2423 "closure_span: def_id={:?} target_place={:?} places={:?}",
2424 def_id, target_place, places
2426 let node_id = self.infcx.tcx.hir().as_local_node_id(def_id)?;
2427 let expr = &self.infcx.tcx.hir().expect_expr(node_id).node;
2428 debug!("closure_span: node_id={:?} expr={:?}", node_id, expr);
2429 if let hir::ExprKind::Closure(
2432 let var_span = self.infcx.tcx.with_freevars(
2435 for (v, place) in freevars.iter().zip(places) {
2437 Operand::Copy(place) |
2438 Operand::Move(place) if target_place == place => {
2439 debug!("closure_span: found captured local {:?}", place);
2440 return Some(v.span);
2450 Some((*args_span, var_span))
2456 /// Helper to retrieve span(s) of given borrow from the current MIR
2458 pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData) -> UseSpans {
2459 let span = self.mir.source_info(borrow.reserve_location).span;
2460 self.borrow_spans(span, borrow.reserve_location)