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::WriteKind;
12 use rustc::middle::region::ScopeTree;
13 use rustc::mir::VarBindingForm;
14 use rustc::mir::{BindingForm, BorrowKind, ClearCrossCrate, Field, Local};
15 use rustc::mir::{LocalDecl, LocalKind, Location, Operand, Place};
16 use rustc::mir::{ProjectionElem, Rvalue, Statement, StatementKind};
18 use rustc_data_structures::fx::FxHashSet;
19 use rustc_data_structures::indexed_vec::Idx;
20 use rustc_data_structures::sync::Lrc;
21 use rustc_errors::DiagnosticBuilder;
24 use super::borrow_set::BorrowData;
25 use super::{Context, MirBorrowckCtxt};
26 use super::{InitializationRequiringAction, PrefixSet};
28 use dataflow::drop_flag_effects;
29 use dataflow::move_paths::indexes::MoveOutIndex;
30 use dataflow::move_paths::MovePathIndex;
31 use util::borrowck_errors::{BorrowckErrors, Origin};
33 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
34 pub(super) fn report_use_of_moved_or_uninitialized(
37 desired_action: InitializationRequiringAction,
38 (place, span): (&Place<'tcx>, Span),
42 .move_spans(place, context.loc)
43 .or_else(|| self.borrow_spans(span, context.loc));
44 let span = use_spans.args_or_use();
46 let mois = self.get_moved_indexes(context, mpi);
47 debug!("report_use_of_moved_or_uninitialized: mois={:?}", mois);
50 let root_place = self.prefixes(&place, PrefixSet::All).last().unwrap();
52 if self.moved_error_reported.contains(&root_place.clone()) {
54 "report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
60 self.moved_error_reported.insert(root_place.clone());
62 let item_msg = match self.describe_place_with_options(place, IncludingDowncast(true)) {
63 Some(name) => format!("`{}`", name),
64 None => "value".to_owned(),
66 let mut err = self.tcx.cannot_act_on_uninitialized_variable(
68 desired_action.as_noun(),
70 .describe_place_with_options(place, IncludingDowncast(true))
71 .unwrap_or("_".to_owned()),
74 err.span_label(span, format!("use of possibly uninitialized {}", item_msg));
76 use_spans.var_span_label(
78 format!("{} occurs due to use in closure", desired_action.as_noun()),
81 err.buffer(&mut self.errors_buffer);
83 let msg = ""; //FIXME: add "partially " or "collaterally "
85 let mut err = self.tcx.cannot_act_on_moved_value(
87 desired_action.as_noun(),
89 self.describe_place_with_options(&place, IncludingDowncast(true)),
93 let mut is_loop_move = false;
95 let move_out = self.move_data.moves[*moi];
96 let moved_place = &self.move_data.move_paths[move_out.path].place;
98 let move_spans = self.move_spans(moved_place, move_out.source);
99 let move_span = move_spans.args_or_use();
101 let move_msg = if move_spans.for_closure() {
107 if span == move_span {
110 format!("value moved{} here in previous iteration of loop", move_msg),
114 err.span_label(move_span, format!("value moved{} here", move_msg));
115 move_spans.var_span_label(&mut err, "variable moved due to use in closure");
119 use_spans.var_span_label(
121 format!("{} occurs due to use in closure", desired_action.as_noun()),
128 "value {} here after move",
129 desired_action.as_verb_in_past_tense()
134 if let Some(ty) = self.retrieve_type_for_place(place) {
135 let needs_note = match ty.sty {
136 ty::Closure(id, _) => {
137 let tables = self.tcx.typeck_tables_of(id);
138 let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
139 let hir_id = self.tcx.hir.node_to_hir_id(node_id);
140 if tables.closure_kind_origins().get(hir_id).is_some() {
150 let mpi = self.move_data.moves[mois[0]].path;
151 let place = &self.move_data.move_paths[mpi].place;
153 if let Some(ty) = self.retrieve_type_for_place(place) {
154 let note_msg = match self
155 .describe_place_with_options(place, IncludingDowncast(true))
157 Some(name) => format!("`{}`", name),
158 None => "value".to_owned(),
162 "move occurs because {} has type `{}`, \
163 which does not implement the `Copy` trait",
170 err.buffer(&mut self.errors_buffer);
174 pub(super) fn report_move_out_while_borrowed(
177 (place, _span): (&Place<'tcx>, Span),
178 borrow: &BorrowData<'tcx>,
181 let value_msg = match self.describe_place(place) {
182 Some(name) => format!("`{}`", name),
183 None => "value".to_owned(),
185 let borrow_msg = match self.describe_place(&borrow.borrowed_place) {
186 Some(name) => format!("`{}`", name),
187 None => "value".to_owned(),
190 let borrow_spans = self.retrieve_borrow_spans(borrow);
191 let borrow_span = borrow_spans.args_or_use();
193 let move_spans = self.move_spans(place, context.loc);
194 let span = move_spans.args_or_use();
196 let mut err = tcx.cannot_move_when_borrowed(
198 &self.describe_place(place).unwrap_or("_".to_owned()),
201 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
202 err.span_label(span, format!("move out of {} occurs here", value_msg));
204 borrow_spans.var_span_label(&mut err, "borrow occurs due to use in closure");
206 move_spans.var_span_label(&mut err, "move occurs due to use in closure");
208 self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
209 err.buffer(&mut self.errors_buffer);
212 pub(super) fn report_use_while_mutably_borrowed(
215 (place, _span): (&Place<'tcx>, Span),
216 borrow: &BorrowData<'tcx>,
220 let borrow_spans = self.retrieve_borrow_spans(borrow);
221 let borrow_span = borrow_spans.args_or_use();
223 // Conflicting borrows are reported separately, so only check for move
225 let use_spans = self.move_spans(place, context.loc);
226 let span = use_spans.var_or_use();
228 let mut err = tcx.cannot_use_when_mutably_borrowed(
230 &self.describe_place(place).unwrap_or("_".to_owned()),
233 .describe_place(&borrow.borrowed_place)
234 .unwrap_or("_".to_owned()),
238 borrow_spans.var_span_label(&mut err, {
239 let place = &borrow.borrowed_place;
240 let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
242 format!("borrow occurs due to use of `{}` in closure", desc_place)
245 self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
246 err.buffer(&mut self.errors_buffer);
249 pub(super) fn report_conflicting_borrow(
252 (place, span): (&Place<'tcx>, Span),
253 gen_borrow_kind: BorrowKind,
254 issued_borrow: &BorrowData<'tcx>,
256 let issued_spans = self.retrieve_borrow_spans(issued_borrow);
257 let issued_span = issued_spans.args_or_use();
259 let borrow_spans = self.borrow_spans(span, context.loc);
260 let span = borrow_spans.args_or_use();
262 let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
265 // FIXME: supply non-"" `opt_via` when appropriate
266 let mut err = match (
274 (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt)
275 | (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => tcx
276 .cannot_reborrow_already_borrowed(
289 (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => tcx
290 .cannot_mutably_borrow_multiply(
300 (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => tcx
301 .cannot_uniquely_borrow_by_two_closures(
309 (BorrowKind::Unique, _, _, _, _, _) => tcx.cannot_uniquely_borrow_by_one_closure(
320 (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => tcx
321 .cannot_reborrow_already_uniquely_borrowed(
332 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => tcx
333 .cannot_reborrow_already_uniquely_borrowed(
344 (BorrowKind::Shared, _, _, BorrowKind::Shared, _, _) => unreachable!(),
347 if issued_spans == borrow_spans {
348 borrow_spans.var_span_label(
350 format!("borrows occur due to use of `{}` in closure", desc_place),
353 let borrow_place = &issued_borrow.borrowed_place;
354 let borrow_place_desc = self.describe_place(borrow_place).unwrap_or("_".to_owned());
355 issued_spans.var_span_label(
358 "first borrow occurs due to use of `{}` in closure",
363 borrow_spans.var_span_label(
366 "second borrow occurs due to use of `{}` in closure",
372 self.explain_why_borrow_contains_point(context, issued_borrow, None, &mut err);
374 err.buffer(&mut self.errors_buffer);
377 pub(super) fn report_borrowed_value_does_not_live_long_enough(
380 borrow: &BorrowData<'tcx>,
381 place_span: (&Place<'tcx>, Span),
382 kind: Option<WriteKind>,
384 let drop_span = place_span.1;
385 let scope_tree = self.tcx.region_scope_tree(self.mir_def_id);
386 let root_place = self
387 .prefixes(&borrow.borrowed_place, PrefixSet::All)
391 let borrow_spans = self.retrieve_borrow_spans(borrow);
392 let borrow_span = borrow_spans.var_or_use();
394 let proper_span = match *root_place {
395 Place::Local(local) => self.mir.local_decls[local].source_info.span,
400 .access_place_error_reported
401 .contains(&(root_place.clone(), borrow_span))
404 "suppressing access_place error when borrow doesn't live long enough for {:?}",
410 self.access_place_error_reported
411 .insert((root_place.clone(), borrow_span));
413 let mut err = match &self.describe_place(&borrow.borrowed_place) {
414 Some(_) if self.is_place_thread_local(root_place) => {
415 self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
417 Some(name) => self.report_local_value_does_not_live_long_enough(
425 kind.map(|k| (k, place_span.0)),
427 None => self.report_temporary_value_does_not_live_long_enough(
437 borrow_spans.args_span_label(&mut err, "value captured here");
439 err.buffer(&mut self.errors_buffer);
442 fn report_local_value_does_not_live_long_enough(
446 scope_tree: &Lrc<ScopeTree>,
447 borrow: &BorrowData<'tcx>,
451 kind_place: Option<(WriteKind, &Place<'tcx>)>,
452 ) -> DiagnosticBuilder<'cx> {
454 "report_local_value_does_not_live_long_enough(\
455 {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
457 context, name, scope_tree, borrow, drop_span, borrow_span
460 let mut err = self.tcx.path_does_not_live_long_enough(
462 &format!("`{}`", name),
466 err.span_label(borrow_span, "borrowed value does not live long enough");
469 format!("`{}` dropped here while still borrowed", name),
472 self.explain_why_borrow_contains_point(context, borrow, kind_place, &mut err);
476 fn report_thread_local_value_does_not_live_long_enough(
480 ) -> DiagnosticBuilder<'cx> {
482 "report_thread_local_value_does_not_live_long_enough(\
485 drop_span, borrow_span
490 .thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
494 "thread-local variables cannot be borrowed beyond the end of the function",
496 err.span_label(drop_span, "end of enclosing function is here");
500 fn report_temporary_value_does_not_live_long_enough(
503 scope_tree: &Lrc<ScopeTree>,
504 borrow: &BorrowData<'tcx>,
508 ) -> DiagnosticBuilder<'cx> {
510 "report_temporary_value_does_not_live_long_enough(\
511 {:?}, {:?}, {:?}, {:?}, {:?}\
513 context, scope_tree, borrow, drop_span, proper_span
518 tcx.path_does_not_live_long_enough(proper_span, "borrowed value", Origin::Mir);
519 err.span_label(proper_span, "temporary value does not live long enough");
520 err.span_label(drop_span, "temporary value only lives until here");
522 self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
526 fn get_moved_indexes(&mut self, context: Context, mpi: MovePathIndex) -> Vec<MoveOutIndex> {
529 let mut stack = Vec::new();
530 stack.extend(mir.predecessor_locations(context.loc));
532 let mut visited = FxHashSet();
533 let mut result = vec![];
535 'dfs: while let Some(l) = stack.pop() {
537 "report_use_of_moved_or_uninitialized: current_location={:?}",
541 if !visited.insert(l) {
546 let stmt_kind = mir[l.block]
548 .get(l.statement_index)
550 if let Some(StatementKind::StorageDead(..)) = stmt_kind {
551 // this analysis only tries to find moves explicitly
552 // written by the user, so we ignore the move-outs
553 // created by `StorageDead` and at the beginning
556 for moi in &self.move_data.loc_map[l] {
557 debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
558 if self.move_data.moves[*moi].path == mpi {
559 debug!("report_use_of_moved_or_uninitialized: found");
562 // Strictly speaking, we could continue our DFS here. There may be
563 // other moves that can reach the point of error. But it is kind of
564 // confusing to highlight them.
572 // drop(a); // <-- current point of error
575 // Because we stop the DFS here, we only highlight `let c = a`,
576 // and not `let b = a`. We will of course also report an error at
577 // `let c = a` which highlights `let b = a` as the move.
584 let mut any_match = false;
585 drop_flag_effects::for_location_inits(self.tcx, self.mir, self.move_data, l, |m| {
594 stack.extend(mir.predecessor_locations(l));
600 pub(super) fn report_illegal_mutation_of_borrowed(
603 (place, span): (&Place<'tcx>, Span),
604 loan: &BorrowData<'tcx>,
606 let loan_spans = self.retrieve_borrow_spans(loan);
607 let loan_span = loan_spans.args_or_use();
610 let mut err = tcx.cannot_assign_to_borrowed(
613 &self.describe_place(place).unwrap_or("_".to_owned()),
617 loan_spans.var_span_label(&mut err, "borrow occurs due to use in closure");
619 self.explain_why_borrow_contains_point(context, loan, None, &mut err);
621 err.buffer(&mut self.errors_buffer);
624 /// Reports an illegal reassignment; for example, an assignment to
625 /// (part of) a non-`mut` local that occurs potentially after that
626 /// local has already been initialized. `place` is the path being
627 /// assigned; `err_place` is a place providing a reason why
628 /// `place` is not mutable (e.g. the non-`mut` local `x` in an
629 /// assignment to `x.f`).
630 pub(super) fn report_illegal_reassignment(
633 (place, span): (&Place<'tcx>, Span),
635 err_place: &Place<'tcx>,
637 let (from_arg, local_decl) = if let Place::Local(local) = *err_place {
638 if let LocalKind::Arg = self.mir.local_kind(local) {
639 (true, Some(&self.mir.local_decls[local]))
641 (false, Some(&self.mir.local_decls[local]))
647 // If root local is initialized immediately (everything apart from let
648 // PATTERN;) then make the error refer to that local, rather than the
649 // place being assigned later.
650 let (place_description, assigned_span) = match local_decl {
652 is_user_variable: Some(ClearCrossCrate::Clear),
657 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
658 opt_match_place: None,
664 is_user_variable: None,
667 | None => (self.describe_place(place), assigned_span),
668 Some(decl) => (self.describe_place(err_place), decl.source_info.span),
671 let mut err = self.tcx.cannot_reassign_immutable(
673 place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
677 let msg = if from_arg {
678 "cannot assign to immutable argument"
680 "cannot assign twice to immutable variable"
682 if span != assigned_span {
684 let value_msg = match place_description {
685 Some(name) => format!("`{}`", name),
686 None => "value".to_owned(),
688 err.span_label(assigned_span, format!("first assignment to {}", value_msg));
691 if let Some(decl) = local_decl {
692 if let Some(name) = decl.name {
693 if decl.can_be_made_mutable() {
695 decl.source_info.span,
696 format!("consider changing this to `mut {}`", name),
701 err.span_label(span, msg);
702 err.buffer(&mut self.errors_buffer);
706 pub(super) struct IncludingDowncast(bool);
708 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
709 // End-user visible description of `place` if one can be found. If the
710 // place is a temporary for instance, None will be returned.
711 pub(super) fn describe_place(&self, place: &Place<'tcx>) -> Option<String> {
712 self.describe_place_with_options(place, IncludingDowncast(false))
715 // End-user visible description of `place` if one can be found. If the
716 // place is a temporary for instance, None will be returned.
717 // `IncludingDowncast` parameter makes the function return `Err` if `ProjectionElem` is
718 // `Downcast` and `IncludingDowncast` is true
719 pub(super) fn describe_place_with_options(
722 including_downcast: IncludingDowncast,
723 ) -> Option<String> {
724 let mut buf = String::new();
725 match self.append_place_to_string(place, &mut buf, false, &including_downcast) {
731 // Appends end-user visible description of `place` to `buf`.
732 fn append_place_to_string(
737 including_downcast: &IncludingDowncast,
738 ) -> Result<(), ()> {
740 Place::Promoted(_) => {
741 buf.push_str("promoted");
743 Place::Local(local) => {
744 self.append_local_to_string(local, buf)?;
746 Place::Static(ref static_) => {
747 buf.push_str(&self.tcx.item_name(static_.def_id).to_string());
749 Place::Projection(ref proj) => {
751 ProjectionElem::Deref => {
752 let upvar_field_projection =
753 place.is_upvar_field_projection(self.mir, &self.tcx);
754 if let Some(field) = upvar_field_projection {
755 let var_index = field.index();
756 let name = self.mir.upvar_decls[var_index].debug_name.to_string();
757 if self.mir.upvar_decls[var_index].by_ref {
760 buf.push_str(&format!("*{}", &name));
764 self.append_place_to_string(
770 } else if let Place::Local(local) = proj.base {
771 if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
772 self.mir.local_decls[local].is_user_variable
774 self.append_place_to_string(
782 self.append_place_to_string(
791 self.append_place_to_string(
800 ProjectionElem::Downcast(..) => {
801 self.append_place_to_string(
807 if including_downcast.0 {
811 ProjectionElem::Field(field, _ty) => {
814 let upvar_field_projection =
815 place.is_upvar_field_projection(self.mir, &self.tcx);
816 if let Some(field) = upvar_field_projection {
817 let var_index = field.index();
818 let name = self.mir.upvar_decls[var_index].debug_name.to_string();
821 let field_name = self.describe_field(&proj.base, field);
822 self.append_place_to_string(
828 buf.push_str(&format!(".{}", field_name));
831 ProjectionElem::Index(index) => {
834 self.append_place_to_string(
841 if self.append_local_to_string(index, buf).is_err() {
846 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
848 // Since it isn't possible to borrow an element on a particular index and
849 // then use another while the borrow is held, don't output indices details
850 // to avoid confusing the end-user
851 self.append_place_to_string(
857 buf.push_str(&"[..]");
866 // Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
867 // a name, then `Err` is returned
868 fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
869 let local = &self.mir.local_decls[local_index];
872 buf.push_str(&name.to_string());
879 // End-user visible description of the `field`nth field of `base`
880 fn describe_field(&self, base: &Place, field: Field) -> String {
882 Place::Local(local) => {
883 let local = &self.mir.local_decls[local];
884 self.describe_field_from_ty(&local.ty, field)
886 Place::Promoted(ref prom) => self.describe_field_from_ty(&prom.1, field),
887 Place::Static(ref static_) => self.describe_field_from_ty(&static_.ty, field),
888 Place::Projection(ref proj) => match proj.elem {
889 ProjectionElem::Deref => self.describe_field(&proj.base, field),
890 ProjectionElem::Downcast(def, variant_index) => format!(
892 def.variants[variant_index].fields[field.index()].ident
894 ProjectionElem::Field(_, field_type) => {
895 self.describe_field_from_ty(&field_type, field)
897 ProjectionElem::Index(..)
898 | ProjectionElem::ConstantIndex { .. }
899 | ProjectionElem::Subslice { .. } => {
900 self.describe_field(&proj.base, field).to_string()
906 // End-user visible description of the `field_index`nth field of `ty`
907 fn describe_field_from_ty(&self, ty: &ty::Ty, field: Field) -> String {
909 // If the type is a box, the field is described from the boxed type
910 self.describe_field_from_ty(&ty.boxed_ty(), field)
913 ty::Adt(def, _) => if def.is_enum() {
914 field.index().to_string()
916 def.non_enum_variant().fields[field.index()]
920 ty::Tuple(_) => field.index().to_string(),
921 ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
922 self.describe_field_from_ty(&ty, field)
924 ty::Array(ty, _) | ty::Slice(ty) => self.describe_field_from_ty(&ty, field),
925 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
926 // Convert the def-id into a node-id. node-ids are only valid for
927 // the local code in the current crate, so this returns an `Option` in case
928 // the closure comes from another crate. But in that case we wouldn't
929 // be borrowck'ing it, so we can just unwrap:
930 let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
931 let freevar = self.tcx.with_freevars(node_id, |fv| fv[field.index()]);
933 self.tcx.hir.name(freevar.var_id()).to_string()
936 // Might need a revision when the fields in trait RFC is implemented
937 // (https://github.com/rust-lang/rfcs/pull/1546)
939 "End-user description not implemented for field access on `{:?}`",
947 // Retrieve type of a place for the current MIR representation
948 fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
950 Place::Local(local) => {
951 let local = &self.mir.local_decls[*local];
954 Place::Promoted(ref prom) => Some(prom.1),
955 Place::Static(ref st) => Some(st.ty),
956 Place::Projection(ref proj) => match proj.elem {
957 ProjectionElem::Field(_, ty) => Some(ty),
963 /// Check if a place is a thread-local static.
964 pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
965 if let Place::Static(statik) = place {
966 let attrs = self.tcx.get_attrs(statik.def_id);
967 let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
970 "is_place_thread_local: attrs={:?} is_thread_local={:?}",
971 attrs, is_thread_local
975 debug!("is_place_thread_local: no");
981 // The span(s) associated to a use of a place.
982 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
983 pub(super) enum UseSpans {
984 // The access is caused by capturing a variable for a closure.
986 // The span of the args of the closure, including the `move` keyword if
989 // The span of the first use of the captured variable inside the closure.
992 // This access has a single span associated to it: common case.
997 pub(super) fn args_or_use(self) -> Span {
999 UseSpans::ClosureUse {
1002 | UseSpans::OtherUse(span) => span,
1006 pub(super) fn var_or_use(self) -> Span {
1008 UseSpans::ClosureUse { var_span: span, .. } | UseSpans::OtherUse(span) => span,
1012 // Add a span label to the arguments of the closure, if it exists.
1013 pub(super) fn args_span_label(self, err: &mut DiagnosticBuilder, message: impl Into<String>) {
1014 if let UseSpans::ClosureUse { args_span, .. } = self {
1015 err.span_label(args_span, message);
1019 // Add a span label to the use of the captured variable, if it exists.
1020 pub(super) fn var_span_label(self, err: &mut DiagnosticBuilder, message: impl Into<String>) {
1021 if let UseSpans::ClosureUse { var_span, .. } = self {
1022 err.span_label(var_span, message);
1026 pub(super) fn for_closure(self) -> bool {
1028 UseSpans::ClosureUse { .. } => true,
1029 UseSpans::OtherUse(_) => false,
1033 pub(super) fn or_else<F>(self, if_other: F) -> Self
1035 F: FnOnce() -> Self,
1038 closure @ UseSpans::ClosureUse { .. } => closure,
1039 UseSpans::OtherUse(_) => if_other(),
1044 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1045 /// Finds the spans associated to a move or copy of move_place at location.
1046 pub(super) fn move_spans(
1048 moved_place: &Place<'tcx>, // Could also be an upvar.
1051 use self::UseSpans::*;
1052 use rustc::hir::ExprKind::Closure;
1053 use rustc::mir::AggregateKind;
1055 let stmt = match self.mir[location.block]
1057 .get(location.statement_index)
1060 None => return OtherUse(self.mir.source_info(location).span),
1063 if let StatementKind::Assign(_, Rvalue::Aggregate(ref kind, ref places)) = stmt.kind {
1064 if let AggregateKind::Closure(def_id, _) = **kind {
1065 debug!("find_closure_move_span: found closure {:?}", places);
1067 if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1068 if let Closure(_, _, _, args_span, _) = self.tcx.hir.expect_expr(node_id).node {
1069 if let Some(var_span) = self.tcx.with_freevars(node_id, |freevars| {
1070 for (v, place) in freevars.iter().zip(places) {
1072 Operand::Copy(place) | Operand::Move(place)
1073 if moved_place == place =>
1076 "find_closure_move_span: found captured local {:?}",
1079 return Some(v.span);
1096 return OtherUse(stmt.source_info.span);
1099 /// Finds the span of arguments of a closure (within `maybe_closure_span`)
1100 /// and its usage of the local assigned at `location`.
1101 /// This is done by searching in statements succeeding `location`
1102 /// and originating from `maybe_closure_span`.
1103 pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans {
1104 use self::UseSpans::*;
1105 use rustc::hir::ExprKind::Closure;
1106 use rustc::mir::AggregateKind;
1108 let local = match self.mir[location.block]
1110 .get(location.statement_index)
1113 kind: StatementKind::Assign(Place::Local(local), _),
1116 _ => return OtherUse(use_span),
1119 if self.mir.local_kind(local) != LocalKind::Temp {
1120 // operands are always temporaries.
1121 return OtherUse(use_span);
1124 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
1125 if let StatementKind::Assign(_, Rvalue::Aggregate(ref kind, ref places)) = stmt.kind {
1126 if let AggregateKind::Closure(def_id, _) = **kind {
1127 debug!("find_closure_borrow_span: found closure {:?}", places);
1129 return if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1130 let args_span = if let Closure(_, _, _, span, _) =
1131 self.tcx.hir.expect_expr(node_id).node
1135 return OtherUse(use_span);
1139 .with_freevars(node_id, |freevars| {
1140 for (v, place) in freevars.iter().zip(places) {
1142 Operand::Copy(Place::Local(l))
1143 | Operand::Move(Place::Local(l))
1147 "find_closure_borrow_span: found captured local \
1151 return Some(v.span);
1157 }).map(|var_span| ClosureUse {
1160 }).unwrap_or(OtherUse(use_span))
1167 if use_span != stmt.source_info.span {
1175 /// Helper to retrieve span(s) of given borrow from the current MIR
1177 pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData) -> UseSpans {
1178 let span = self.mir.source_info(borrow.reserve_location).span;
1179 self.borrow_spans(span, borrow.reserve_location)