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::sync::Lrc;
20 use rustc_errors::DiagnosticBuilder;
23 use super::borrow_set::BorrowData;
24 use super::{Context, MirBorrowckCtxt};
25 use super::{InitializationRequiringAction, PrefixSet};
27 use dataflow::drop_flag_effects;
28 use dataflow::move_paths::indexes::MoveOutIndex;
29 use dataflow::move_paths::MovePathIndex;
30 use util::borrowck_errors::{BorrowckErrors, Origin};
32 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
33 pub(super) fn report_use_of_moved_or_uninitialized(
36 desired_action: InitializationRequiringAction,
37 (place, span): (&Place<'tcx>, Span),
41 .move_spans(place, context.loc)
42 .or_else(|| self.borrow_spans(span, context.loc));
43 let span = use_spans.args_or_use();
45 let mois = self.get_moved_indexes(context, mpi);
46 debug!("report_use_of_moved_or_uninitialized: mois={:?}", mois);
49 let root_place = self.prefixes(&place, PrefixSet::All).last().unwrap();
51 if self.moved_error_reported.contains(&root_place.clone()) {
53 "report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
59 self.moved_error_reported.insert(root_place.clone());
61 let item_msg = match self.describe_place_with_options(place, IncludingDowncast(true)) {
62 Some(name) => format!("`{}`", name),
63 None => "value".to_owned(),
65 let mut err = self.tcx.cannot_act_on_uninitialized_variable(
67 desired_action.as_noun(),
69 .describe_place_with_options(place, IncludingDowncast(true))
70 .unwrap_or("_".to_owned()),
73 err.span_label(span, format!("use of possibly uninitialized {}", item_msg));
75 use_spans.var_span_label(
77 format!("{} occurs due to use in closure", desired_action.as_noun()),
80 err.buffer(&mut self.errors_buffer);
82 let msg = ""; //FIXME: add "partially " or "collaterally "
84 let mut err = self.tcx.cannot_act_on_moved_value(
86 desired_action.as_noun(),
88 self.describe_place_with_options(&place, IncludingDowncast(true)),
92 let mut is_loop_move = false;
94 let move_out = self.move_data.moves[*moi];
95 let moved_place = &self.move_data.move_paths[move_out.path].place;
97 let move_spans = self.move_spans(moved_place, move_out.source);
98 let move_span = move_spans.args_or_use();
100 let move_msg = if move_spans.for_closure() {
106 if span == move_span {
109 format!("value moved{} here in previous iteration of loop", move_msg),
113 err.span_label(move_span, format!("value moved{} here", move_msg));
114 move_spans.var_span_label(&mut err, "variable moved due to use in closure");
118 use_spans.var_span_label(
120 format!("{} occurs due to use in closure", desired_action.as_noun()),
127 "value {} here after move",
128 desired_action.as_verb_in_past_tense()
133 if let Some(ty) = self.retrieve_type_for_place(place) {
134 let needs_note = match ty.sty {
135 ty::Closure(id, _) => {
136 let tables = self.tcx.typeck_tables_of(id);
137 let node_id = self.tcx.hir.as_local_node_id(id).unwrap();
138 let hir_id = self.tcx.hir.node_to_hir_id(node_id);
139 if tables.closure_kind_origins().get(hir_id).is_some() {
149 let mpi = self.move_data.moves[mois[0]].path;
150 let place = &self.move_data.move_paths[mpi].place;
152 if let Some(ty) = self.retrieve_type_for_place(place) {
153 let note_msg = match self
154 .describe_place_with_options(place, IncludingDowncast(true))
156 Some(name) => format!("`{}`", name),
157 None => "value".to_owned(),
161 "move occurs because {} has type `{}`, \
162 which does not implement the `Copy` trait",
169 err.buffer(&mut self.errors_buffer);
173 pub(super) fn report_move_out_while_borrowed(
176 (place, _span): (&Place<'tcx>, Span),
177 borrow: &BorrowData<'tcx>,
180 let value_msg = match self.describe_place(place) {
181 Some(name) => format!("`{}`", name),
182 None => "value".to_owned(),
184 let borrow_msg = match self.describe_place(&borrow.borrowed_place) {
185 Some(name) => format!("`{}`", name),
186 None => "value".to_owned(),
189 let borrow_spans = self.retrieve_borrow_spans(borrow);
190 let borrow_span = borrow_spans.args_or_use();
192 let move_spans = self.move_spans(place, context.loc);
193 let span = move_spans.args_or_use();
195 let mut err = tcx.cannot_move_when_borrowed(
197 &self.describe_place(place).unwrap_or("_".to_owned()),
200 err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
201 err.span_label(span, format!("move out of {} occurs here", value_msg));
203 borrow_spans.var_span_label(&mut err, "borrow occurs due to use in closure");
205 move_spans.var_span_label(&mut err, "move occurs due to use in closure");
207 self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
208 err.buffer(&mut self.errors_buffer);
211 pub(super) fn report_use_while_mutably_borrowed(
214 (place, _span): (&Place<'tcx>, Span),
215 borrow: &BorrowData<'tcx>,
219 let borrow_spans = self.retrieve_borrow_spans(borrow);
220 let borrow_span = borrow_spans.args_or_use();
222 // Conflicting borrows are reported separately, so only check for move
224 let use_spans = self.move_spans(place, context.loc);
225 let span = use_spans.var_or_use();
227 let mut err = tcx.cannot_use_when_mutably_borrowed(
229 &self.describe_place(place).unwrap_or("_".to_owned()),
232 .describe_place(&borrow.borrowed_place)
233 .unwrap_or("_".to_owned()),
237 borrow_spans.var_span_label(&mut err, {
238 let place = &borrow.borrowed_place;
239 let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
241 format!("borrow occurs due to use of `{}` in closure", desc_place)
244 self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
245 err.buffer(&mut self.errors_buffer);
248 pub(super) fn report_conflicting_borrow(
251 (place, span): (&Place<'tcx>, Span),
252 gen_borrow_kind: BorrowKind,
253 issued_borrow: &BorrowData<'tcx>,
255 let issued_spans = self.retrieve_borrow_spans(issued_borrow);
256 let issued_span = issued_spans.args_or_use();
258 let borrow_spans = self.borrow_spans(span, context.loc);
259 let span = borrow_spans.args_or_use();
261 let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
264 // FIXME: supply non-"" `opt_via` when appropriate
265 let mut err = match (
273 (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt)
274 | (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => tcx
275 .cannot_reborrow_already_borrowed(
288 (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => tcx
289 .cannot_mutably_borrow_multiply(
299 (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => tcx
300 .cannot_uniquely_borrow_by_two_closures(
308 (BorrowKind::Unique, _, _, _, _, _) => tcx.cannot_uniquely_borrow_by_one_closure(
319 (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => tcx
320 .cannot_reborrow_already_uniquely_borrowed(
331 (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => tcx
332 .cannot_reborrow_already_uniquely_borrowed(
343 (BorrowKind::Shared, _, _, BorrowKind::Shared, _, _) => unreachable!(),
346 if issued_spans == borrow_spans {
347 borrow_spans.var_span_label(
349 format!("borrows occur due to use of `{}` in closure", desc_place),
352 let borrow_place = &issued_borrow.borrowed_place;
353 let borrow_place_desc = self.describe_place(borrow_place).unwrap_or("_".to_owned());
354 issued_spans.var_span_label(
357 "first borrow occurs due to use of `{}` in closure",
362 borrow_spans.var_span_label(
365 "second borrow occurs due to use of `{}` in closure",
371 self.explain_why_borrow_contains_point(context, issued_borrow, None, &mut err);
373 err.buffer(&mut self.errors_buffer);
376 pub(super) fn report_borrowed_value_does_not_live_long_enough(
379 borrow: &BorrowData<'tcx>,
380 place_span: (&Place<'tcx>, Span),
381 kind: Option<WriteKind>,
383 let drop_span = place_span.1;
384 let scope_tree = self.tcx.region_scope_tree(self.mir_def_id);
385 let root_place = self
386 .prefixes(&borrow.borrowed_place, PrefixSet::All)
390 let borrow_spans = self.retrieve_borrow_spans(borrow);
391 let borrow_span = borrow_spans.var_or_use();
393 let proper_span = match *root_place {
394 Place::Local(local) => self.mir.local_decls[local].source_info.span,
399 .access_place_error_reported
400 .contains(&(root_place.clone(), borrow_span))
403 "suppressing access_place error when borrow doesn't live long enough for {:?}",
409 self.access_place_error_reported
410 .insert((root_place.clone(), borrow_span));
412 let mut err = match &self.describe_place(&borrow.borrowed_place) {
413 Some(_) if self.is_place_thread_local(root_place) => {
414 self.report_thread_local_value_does_not_live_long_enough(drop_span, borrow_span)
416 Some(name) => self.report_local_value_does_not_live_long_enough(
424 kind.map(|k| (k, place_span.0)),
426 None => self.report_temporary_value_does_not_live_long_enough(
436 borrow_spans.args_span_label(&mut err, "value captured here");
438 err.buffer(&mut self.errors_buffer);
441 fn report_local_value_does_not_live_long_enough(
445 scope_tree: &Lrc<ScopeTree>,
446 borrow: &BorrowData<'tcx>,
450 kind_place: Option<(WriteKind, &Place<'tcx>)>,
451 ) -> DiagnosticBuilder<'cx> {
453 "report_local_value_does_not_live_long_enough(\
454 {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
456 context, name, scope_tree, borrow, drop_span, borrow_span
459 let mut err = self.tcx.path_does_not_live_long_enough(
461 &format!("`{}`", name),
465 err.span_label(borrow_span, "borrowed value does not live long enough");
468 format!("`{}` dropped here while still borrowed", name),
471 self.explain_why_borrow_contains_point(context, borrow, kind_place, &mut err);
475 fn report_thread_local_value_does_not_live_long_enough(
479 ) -> DiagnosticBuilder<'cx> {
481 "report_thread_local_value_does_not_live_long_enough(\
484 drop_span, borrow_span
489 .thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir);
493 "thread-local variables cannot be borrowed beyond the end of the function",
495 err.span_label(drop_span, "end of enclosing function is here");
499 fn report_temporary_value_does_not_live_long_enough(
502 scope_tree: &Lrc<ScopeTree>,
503 borrow: &BorrowData<'tcx>,
507 ) -> DiagnosticBuilder<'cx> {
509 "report_temporary_value_does_not_live_long_enough(\
510 {:?}, {:?}, {:?}, {:?}, {:?}\
512 context, scope_tree, borrow, drop_span, proper_span
517 tcx.path_does_not_live_long_enough(proper_span, "borrowed value", Origin::Mir);
518 err.span_label(proper_span, "temporary value does not live long enough");
519 err.span_label(drop_span, "temporary value only lives until here");
521 self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
525 fn get_moved_indexes(&mut self, context: Context, mpi: MovePathIndex) -> Vec<MoveOutIndex> {
528 let mut stack = Vec::new();
529 stack.extend(mir.predecessor_locations(context.loc));
531 let mut visited = FxHashSet();
532 let mut result = vec![];
534 'dfs: while let Some(l) = stack.pop() {
536 "report_use_of_moved_or_uninitialized: current_location={:?}",
540 if !visited.insert(l) {
545 let stmt_kind = mir[l.block]
547 .get(l.statement_index)
549 if let Some(StatementKind::StorageDead(..)) = stmt_kind {
550 // this analysis only tries to find moves explicitly
551 // written by the user, so we ignore the move-outs
552 // created by `StorageDead` and at the beginning
555 for moi in &self.move_data.loc_map[l] {
556 debug!("report_use_of_moved_or_uninitialized: moi={:?}", moi);
557 if self.move_data.moves[*moi].path == mpi {
558 debug!("report_use_of_moved_or_uninitialized: found");
561 // Strictly speaking, we could continue our DFS here. There may be
562 // other moves that can reach the point of error. But it is kind of
563 // confusing to highlight them.
571 // drop(a); // <-- current point of error
574 // Because we stop the DFS here, we only highlight `let c = a`,
575 // and not `let b = a`. We will of course also report an error at
576 // `let c = a` which highlights `let b = a` as the move.
583 let mut any_match = false;
584 drop_flag_effects::for_location_inits(self.tcx, self.mir, self.move_data, l, |m| {
593 stack.extend(mir.predecessor_locations(l));
599 pub(super) fn report_illegal_mutation_of_borrowed(
602 (place, span): (&Place<'tcx>, Span),
603 loan: &BorrowData<'tcx>,
605 let loan_spans = self.retrieve_borrow_spans(loan);
606 let loan_span = loan_spans.args_or_use();
609 let mut err = tcx.cannot_assign_to_borrowed(
612 &self.describe_place(place).unwrap_or("_".to_owned()),
616 loan_spans.var_span_label(&mut err, "borrow occurs due to use in closure");
618 self.explain_why_borrow_contains_point(context, loan, None, &mut err);
620 err.buffer(&mut self.errors_buffer);
623 /// Reports an illegal reassignment; for example, an assignment to
624 /// (part of) a non-`mut` local that occurs potentially after that
625 /// local has already been initialized. `place` is the path being
626 /// assigned; `err_place` is a place providing a reason why
627 /// `place` is not mutable (e.g. the non-`mut` local `x` in an
628 /// assignment to `x.f`).
629 pub(super) fn report_illegal_reassignment(
632 (place, span): (&Place<'tcx>, Span),
634 err_place: &Place<'tcx>,
636 let (from_arg, local_decl) = if let Place::Local(local) = *err_place {
637 if let LocalKind::Arg = self.mir.local_kind(local) {
638 (true, Some(&self.mir.local_decls[local]))
640 (false, Some(&self.mir.local_decls[local]))
646 // If root local is initialized immediately (everything apart from let
647 // PATTERN;) then make the error refer to that local, rather than the
648 // place being assigned later.
649 let (place_description, assigned_span) = match local_decl {
651 is_user_variable: Some(ClearCrossCrate::Clear),
656 Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
657 opt_match_place: None,
663 is_user_variable: None,
666 | None => (self.describe_place(place), assigned_span),
667 Some(decl) => (self.describe_place(err_place), decl.source_info.span),
670 let mut err = self.tcx.cannot_reassign_immutable(
672 place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
676 let msg = if from_arg {
677 "cannot assign to immutable argument"
679 "cannot assign twice to immutable variable"
681 if span != assigned_span {
683 let value_msg = match place_description {
684 Some(name) => format!("`{}`", name),
685 None => "value".to_owned(),
687 err.span_label(assigned_span, format!("first assignment to {}", value_msg));
690 if let Some(decl) = local_decl {
691 if let Some(name) = decl.name {
692 if decl.can_be_made_mutable() {
694 decl.source_info.span,
695 format!("consider changing this to `mut {}`", name),
700 err.span_label(span, msg);
701 err.buffer(&mut self.errors_buffer);
705 pub(super) struct IncludingDowncast(bool);
707 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
708 // End-user visible description of `place` if one can be found. If the
709 // place is a temporary for instance, None will be returned.
710 pub(super) fn describe_place(&self, place: &Place<'tcx>) -> Option<String> {
711 self.describe_place_with_options(place, IncludingDowncast(false))
714 // End-user visible description of `place` if one can be found. If the
715 // place is a temporary for instance, None will be returned.
716 // `IncludingDowncast` parameter makes the function return `Err` if `ProjectionElem` is
717 // `Downcast` and `IncludingDowncast` is true
718 pub(super) fn describe_place_with_options(
721 including_downcast: IncludingDowncast,
722 ) -> Option<String> {
723 let mut buf = String::new();
724 match self.append_place_to_string(place, &mut buf, false, &including_downcast) {
730 // Appends end-user visible description of `place` to `buf`.
731 fn append_place_to_string(
736 including_downcast: &IncludingDowncast,
737 ) -> Result<(), ()> {
739 Place::Promoted(_) => {
740 buf.push_str("promoted");
742 Place::Local(local) => {
743 self.append_local_to_string(local, buf)?;
745 Place::Static(ref static_) => {
746 buf.push_str(&self.tcx.item_name(static_.def_id).to_string());
748 Place::Projection(ref proj) => {
750 ProjectionElem::Deref => {
751 let upvar_field_projection =
752 place.is_upvar_field_projection(self.mir, &self.tcx);
753 if let Some(field) = upvar_field_projection {
754 let var_index = field.index();
755 let name = self.mir.upvar_decls[var_index].debug_name.to_string();
756 if self.mir.upvar_decls[var_index].by_ref {
759 buf.push_str(&format!("*{}", &name));
763 self.append_place_to_string(
769 } else if let Place::Local(local) = proj.base {
770 if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
771 self.mir.local_decls[local].is_user_variable
773 self.append_place_to_string(
781 self.append_place_to_string(
790 self.append_place_to_string(
799 ProjectionElem::Downcast(..) => {
800 self.append_place_to_string(
806 if including_downcast.0 {
810 ProjectionElem::Field(field, _ty) => {
813 let upvar_field_projection =
814 place.is_upvar_field_projection(self.mir, &self.tcx);
815 if let Some(field) = upvar_field_projection {
816 let var_index = field.index();
817 let name = self.mir.upvar_decls[var_index].debug_name.to_string();
820 let field_name = self.describe_field(&proj.base, field);
821 self.append_place_to_string(
827 buf.push_str(&format!(".{}", field_name));
830 ProjectionElem::Index(index) => {
833 self.append_place_to_string(
840 if self.append_local_to_string(index, buf).is_err() {
845 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
847 // Since it isn't possible to borrow an element on a particular index and
848 // then use another while the borrow is held, don't output indices details
849 // to avoid confusing the end-user
850 self.append_place_to_string(
856 buf.push_str(&"[..]");
865 // Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
866 // a name, then `Err` is returned
867 fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
868 let local = &self.mir.local_decls[local_index];
871 buf.push_str(&name.to_string());
878 // End-user visible description of the `field`nth field of `base`
879 fn describe_field(&self, base: &Place, field: Field) -> String {
881 Place::Local(local) => {
882 let local = &self.mir.local_decls[local];
883 self.describe_field_from_ty(&local.ty, field)
885 Place::Promoted(ref prom) => self.describe_field_from_ty(&prom.1, field),
886 Place::Static(ref static_) => self.describe_field_from_ty(&static_.ty, field),
887 Place::Projection(ref proj) => match proj.elem {
888 ProjectionElem::Deref => self.describe_field(&proj.base, field),
889 ProjectionElem::Downcast(def, variant_index) => format!(
891 def.variants[variant_index].fields[field.index()].ident
893 ProjectionElem::Field(_, field_type) => {
894 self.describe_field_from_ty(&field_type, field)
896 ProjectionElem::Index(..)
897 | ProjectionElem::ConstantIndex { .. }
898 | ProjectionElem::Subslice { .. } => {
899 self.describe_field(&proj.base, field).to_string()
905 // End-user visible description of the `field_index`nth field of `ty`
906 fn describe_field_from_ty(&self, ty: &ty::Ty, field: Field) -> String {
908 // If the type is a box, the field is described from the boxed type
909 self.describe_field_from_ty(&ty.boxed_ty(), field)
912 ty::Adt(def, _) => if def.is_enum() {
913 field.index().to_string()
915 def.non_enum_variant().fields[field.index()]
919 ty::Tuple(_) => field.index().to_string(),
920 ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
921 self.describe_field_from_ty(&ty, field)
923 ty::Array(ty, _) | ty::Slice(ty) => self.describe_field_from_ty(&ty, field),
924 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
925 // Convert the def-id into a node-id. node-ids are only valid for
926 // the local code in the current crate, so this returns an `Option` in case
927 // the closure comes from another crate. But in that case we wouldn't
928 // be borrowck'ing it, so we can just unwrap:
929 let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
930 let freevar = self.tcx.with_freevars(node_id, |fv| fv[field.index()]);
932 self.tcx.hir.name(freevar.var_id()).to_string()
935 // Might need a revision when the fields in trait RFC is implemented
936 // (https://github.com/rust-lang/rfcs/pull/1546)
938 "End-user description not implemented for field access on `{:?}`",
946 // Retrieve type of a place for the current MIR representation
947 fn retrieve_type_for_place(&self, place: &Place<'tcx>) -> Option<ty::Ty> {
949 Place::Local(local) => {
950 let local = &self.mir.local_decls[*local];
953 Place::Promoted(ref prom) => Some(prom.1),
954 Place::Static(ref st) => Some(st.ty),
955 Place::Projection(ref proj) => match proj.elem {
956 ProjectionElem::Field(_, ty) => Some(ty),
962 /// Check if a place is a thread-local static.
963 pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
964 if let Place::Static(statik) = place {
965 let attrs = self.tcx.get_attrs(statik.def_id);
966 let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
969 "is_place_thread_local: attrs={:?} is_thread_local={:?}",
970 attrs, is_thread_local
974 debug!("is_place_thread_local: no");
980 // The span(s) associated to a use of a place.
981 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
982 pub(super) enum UseSpans {
983 // The access is caused by capturing a variable for a closure.
985 // The span of the args of the closure, including the `move` keyword if
988 // The span of the first use of the captured variable inside the closure.
991 // This access has a single span associated to it: common case.
996 pub(super) fn args_or_use(self) -> Span {
998 UseSpans::ClosureUse {
1001 | UseSpans::OtherUse(span) => span,
1005 pub(super) fn var_or_use(self) -> Span {
1007 UseSpans::ClosureUse { var_span: span, .. } | UseSpans::OtherUse(span) => span,
1011 // Add a span label to the arguments of the closure, if it exists.
1012 pub(super) fn args_span_label(self, err: &mut DiagnosticBuilder, message: impl Into<String>) {
1013 if let UseSpans::ClosureUse { args_span, .. } = self {
1014 err.span_label(args_span, message);
1018 // Add a span label to the use of the captured variable, if it exists.
1019 pub(super) fn var_span_label(self, err: &mut DiagnosticBuilder, message: impl Into<String>) {
1020 if let UseSpans::ClosureUse { var_span, .. } = self {
1021 err.span_label(var_span, message);
1025 pub(super) fn for_closure(self) -> bool {
1027 UseSpans::ClosureUse { .. } => true,
1028 UseSpans::OtherUse(_) => false,
1032 pub(super) fn or_else<F>(self, if_other: F) -> Self
1034 F: FnOnce() -> Self,
1037 closure @ UseSpans::ClosureUse { .. } => closure,
1038 UseSpans::OtherUse(_) => if_other(),
1043 impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
1044 /// Finds the spans associated to a move or copy of move_place at location.
1045 pub(super) fn move_spans(
1047 moved_place: &Place<'tcx>, // Could also be an upvar.
1050 use self::UseSpans::*;
1051 use rustc::hir::ExprKind::Closure;
1052 use rustc::mir::AggregateKind;
1054 let stmt = match self.mir[location.block]
1056 .get(location.statement_index)
1059 None => return OtherUse(self.mir.source_info(location).span),
1062 if let StatementKind::Assign(_, Rvalue::Aggregate(ref kind, ref places)) = stmt.kind {
1063 if let AggregateKind::Closure(def_id, _) = **kind {
1064 debug!("find_closure_move_span: found closure {:?}", places);
1066 if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1067 if let Closure(_, _, _, args_span, _) = self.tcx.hir.expect_expr(node_id).node {
1068 if let Some(var_span) = self.tcx.with_freevars(node_id, |freevars| {
1069 for (v, place) in freevars.iter().zip(places) {
1071 Operand::Copy(place) | Operand::Move(place)
1072 if moved_place == place =>
1075 "find_closure_move_span: found captured local {:?}",
1078 return Some(v.span);
1095 return OtherUse(stmt.source_info.span);
1098 /// Finds the span of arguments of a closure (within `maybe_closure_span`)
1099 /// and its usage of the local assigned at `location`.
1100 /// This is done by searching in statements succeeding `location`
1101 /// and originating from `maybe_closure_span`.
1102 pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans {
1103 use self::UseSpans::*;
1104 use rustc::hir::ExprKind::Closure;
1105 use rustc::mir::AggregateKind;
1107 let local = match self.mir[location.block]
1109 .get(location.statement_index)
1112 kind: StatementKind::Assign(Place::Local(local), _),
1115 _ => return OtherUse(use_span),
1118 if self.mir.local_kind(local) != LocalKind::Temp {
1119 // operands are always temporaries.
1120 return OtherUse(use_span);
1123 for stmt in &self.mir[location.block].statements[location.statement_index + 1..] {
1124 if let StatementKind::Assign(_, Rvalue::Aggregate(ref kind, ref places)) = stmt.kind {
1125 if let AggregateKind::Closure(def_id, _) = **kind {
1126 debug!("find_closure_borrow_span: found closure {:?}", places);
1128 return if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
1129 let args_span = if let Closure(_, _, _, span, _) =
1130 self.tcx.hir.expect_expr(node_id).node
1134 return OtherUse(use_span);
1138 .with_freevars(node_id, |freevars| {
1139 for (v, place) in freevars.iter().zip(places) {
1141 Operand::Copy(Place::Local(l))
1142 | Operand::Move(Place::Local(l))
1146 "find_closure_borrow_span: found captured local \
1150 return Some(v.span);
1156 }).map(|var_span| ClosureUse {
1159 }).unwrap_or(OtherUse(use_span))
1166 if use_span != stmt.source_info.span {
1174 /// Helper to retrieve span(s) of given borrow from the current MIR
1176 pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData) -> UseSpans {
1177 let span = self.mir.source_info(borrow.reserve_location).span;
1178 self.borrow_spans(span, borrow.reserve_location)