]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/borrow_check/error_reporting.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / src / librustc_mir / borrow_check / error_reporting.rs
1 use rustc::hir;
2 use rustc::hir::def::Namespace;
3 use rustc::hir::def_id::DefId;
4 use rustc::hir::GeneratorKind;
5 use rustc::mir::{
6 AggregateKind, Constant, Field, Local, LocalKind, Location, Operand,
7 Place, PlaceBase, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind,
8 Static, StaticKind, Terminator, TerminatorKind,
9 };
10 use rustc::ty::{self, DefIdTree, Ty, TyCtxt};
11 use rustc::ty::layout::VariantIdx;
12 use rustc::ty::print::Print;
13 use rustc_errors::DiagnosticBuilder;
14 use syntax_pos::Span;
15 use syntax::symbol::sym;
16
17 use super::borrow_set::BorrowData;
18 use super::MirBorrowckCtxt;
19 use crate::dataflow::move_paths::{InitLocation, LookupResult};
20
21 pub(super) struct IncludingDowncast(pub(super) bool);
22
23 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24 /// Adds a suggestion when a closure is invoked twice with a moved variable or when a closure
25 /// is moved after being invoked.
26 ///
27 /// ```text
28 /// note: closure cannot be invoked more than once because it moves the variable `dict` out of
29 /// its environment
30 /// --> $DIR/issue-42065.rs:16:29
31 /// |
32 /// LL | for (key, value) in dict {
33 /// | ^^^^
34 /// ```
35 pub(super) fn add_moved_or_invoked_closure_note(
36 &self,
37 location: Location,
38 place: PlaceRef<'cx, 'tcx>,
39 diag: &mut DiagnosticBuilder<'_>,
40 ) {
41 debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
42 let mut target = place.local_or_deref_local();
43 for stmt in &self.body[location.block].statements[location.statement_index..] {
44 debug!("add_moved_or_invoked_closure_note: stmt={:?} target={:?}", stmt, target);
45 if let StatementKind::Assign(box(into, Rvalue::Use(from))) = &stmt.kind {
46 debug!("add_fnonce_closure_note: into={:?} from={:?}", into, from);
47 match from {
48 Operand::Copy(ref place) |
49 Operand::Move(ref place) if target == place.local_or_deref_local() =>
50 target = into.local_or_deref_local(),
51 _ => {},
52 }
53 }
54 }
55
56 // Check if we are attempting to call a closure after it has been invoked.
57 let terminator = self.body[location.block].terminator();
58 debug!("add_moved_or_invoked_closure_note: terminator={:?}", terminator);
59 if let TerminatorKind::Call {
60 func: Operand::Constant(box Constant {
61 literal: ty::Const {
62 ty: &ty::TyS { kind: ty::FnDef(id, _), .. },
63 ..
64 },
65 ..
66 }),
67 args,
68 ..
69 } = &terminator.kind {
70 debug!("add_moved_or_invoked_closure_note: id={:?}", id);
71 if self.infcx.tcx.parent(id) == self.infcx.tcx.lang_items().fn_once_trait() {
72 let closure = match args.first() {
73 Some(Operand::Copy(ref place)) |
74 Some(Operand::Move(ref place)) if target == place.local_or_deref_local() =>
75 place.local_or_deref_local().unwrap(),
76 _ => return,
77 };
78
79 debug!("add_moved_or_invoked_closure_note: closure={:?}", closure);
80 if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind {
81 let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap();
82
83 if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did)
84 .closure_kind_origins()
85 .get(hir_id)
86 {
87 diag.span_note(
88 *span,
89 &format!(
90 "closure cannot be invoked more than once because it moves the \
91 variable `{}` out of its environment",
92 name,
93 ),
94 );
95 return;
96 }
97 }
98 }
99 }
100
101 // Check if we are just moving a closure after it has been invoked.
102 if let Some(target) = target {
103 if let ty::Closure(did, _) = self.body.local_decls[target].ty.kind {
104 let hir_id = self.infcx.tcx.hir().as_local_hir_id(did).unwrap();
105
106 if let Some((span, name)) = self.infcx.tcx.typeck_tables_of(did)
107 .closure_kind_origins()
108 .get(hir_id)
109 {
110 diag.span_note(
111 *span,
112 &format!(
113 "closure cannot be moved more than once as it is not `Copy` due to \
114 moving the variable `{}` out of its environment",
115 name
116 ),
117 );
118 }
119 }
120 }
121 }
122
123 /// End-user visible description of `place` if one can be found. If the
124 /// place is a temporary for instance, None will be returned.
125 pub(super) fn describe_place(&self, place_ref: PlaceRef<'cx, 'tcx>) -> Option<String> {
126 self.describe_place_with_options(place_ref, IncludingDowncast(false))
127 }
128
129 /// End-user visible description of `place` if one can be found. If the
130 /// place is a temporary for instance, None will be returned.
131 /// `IncludingDowncast` parameter makes the function return `Err` if `ProjectionElem` is
132 /// `Downcast` and `IncludingDowncast` is true
133 pub(super) fn describe_place_with_options(
134 &self,
135 place: PlaceRef<'cx, 'tcx>,
136 including_downcast: IncludingDowncast,
137 ) -> Option<String> {
138 let mut buf = String::new();
139 match self.append_place_to_string(place, &mut buf, false, &including_downcast) {
140 Ok(()) => Some(buf),
141 Err(()) => None,
142 }
143 }
144
145 /// Appends end-user visible description of `place` to `buf`.
146 fn append_place_to_string(
147 &self,
148 place: PlaceRef<'cx, 'tcx>,
149 buf: &mut String,
150 mut autoderef: bool,
151 including_downcast: &IncludingDowncast,
152 ) -> Result<(), ()> {
153 match place {
154 PlaceRef {
155 base: PlaceBase::Local(local),
156 projection: [],
157 } => {
158 self.append_local_to_string(*local, buf)?;
159 }
160 PlaceRef {
161 base:
162 PlaceBase::Static(box Static {
163 kind: StaticKind::Promoted(..),
164 ..
165 }),
166 projection: [],
167 } => {
168 buf.push_str("promoted");
169 }
170 PlaceRef {
171 base:
172 PlaceBase::Static(box Static {
173 kind: StaticKind::Static,
174 def_id,
175 ..
176 }),
177 projection: [],
178 } => {
179 buf.push_str(&self.infcx.tcx.item_name(*def_id).to_string());
180 }
181 PlaceRef {
182 base,
183 projection: [proj_base @ .., elem],
184 } => {
185 match elem {
186 ProjectionElem::Deref => {
187 let upvar_field_projection =
188 self.is_upvar_field_projection(place);
189 if let Some(field) = upvar_field_projection {
190 let var_index = field.index();
191 let name = self.upvars[var_index].name.to_string();
192 if self.upvars[var_index].by_ref {
193 buf.push_str(&name);
194 } else {
195 buf.push_str(&format!("*{}", &name));
196 }
197 } else {
198 if autoderef {
199 // FIXME turn this recursion into iteration
200 self.append_place_to_string(
201 PlaceRef {
202 base,
203 projection: proj_base,
204 },
205 buf,
206 autoderef,
207 &including_downcast,
208 )?;
209 } else {
210 match (proj_base, base) {
211 ([], PlaceBase::Local(local)) => {
212 if self.body.local_decls[*local].is_ref_for_guard() {
213 self.append_place_to_string(
214 PlaceRef {
215 base,
216 projection: proj_base,
217 },
218 buf,
219 autoderef,
220 &including_downcast,
221 )?;
222 } else {
223 // FIXME deduplicate this and the _ => body below
224 buf.push_str(&"*");
225 self.append_place_to_string(
226 PlaceRef {
227 base,
228 projection: proj_base,
229 },
230 buf,
231 autoderef,
232 &including_downcast,
233 )?;
234 }
235 }
236
237 _ => {
238 buf.push_str(&"*");
239 self.append_place_to_string(
240 PlaceRef {
241 base,
242 projection: proj_base,
243 },
244 buf,
245 autoderef,
246 &including_downcast,
247 )?;
248 }
249 }
250 }
251 }
252 }
253 ProjectionElem::Downcast(..) => {
254 self.append_place_to_string(
255 PlaceRef {
256 base,
257 projection: proj_base,
258 },
259 buf,
260 autoderef,
261 &including_downcast,
262 )?;
263 if including_downcast.0 {
264 return Err(());
265 }
266 }
267 ProjectionElem::Field(field, _ty) => {
268 autoderef = true;
269
270 let upvar_field_projection =
271 self.is_upvar_field_projection(place);
272 if let Some(field) = upvar_field_projection {
273 let var_index = field.index();
274 let name = self.upvars[var_index].name.to_string();
275 buf.push_str(&name);
276 } else {
277 let field_name = self.describe_field(PlaceRef {
278 base,
279 projection: proj_base,
280 }, *field);
281 self.append_place_to_string(
282 PlaceRef {
283 base,
284 projection: proj_base,
285 },
286 buf,
287 autoderef,
288 &including_downcast,
289 )?;
290 buf.push_str(&format!(".{}", field_name));
291 }
292 }
293 ProjectionElem::Index(index) => {
294 autoderef = true;
295
296 self.append_place_to_string(
297 PlaceRef {
298 base,
299 projection: proj_base,
300 },
301 buf,
302 autoderef,
303 &including_downcast,
304 )?;
305 buf.push_str("[");
306 if self.append_local_to_string(*index, buf).is_err() {
307 buf.push_str("_");
308 }
309 buf.push_str("]");
310 }
311 ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
312 autoderef = true;
313 // Since it isn't possible to borrow an element on a particular index and
314 // then use another while the borrow is held, don't output indices details
315 // to avoid confusing the end-user
316 self.append_place_to_string(
317 PlaceRef {
318 base,
319 projection: proj_base,
320 },
321 buf,
322 autoderef,
323 &including_downcast,
324 )?;
325 buf.push_str(&"[..]");
326 }
327 };
328 }
329 }
330
331 Ok(())
332 }
333
334 /// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
335 /// a name, or its name was generated by the compiler, then `Err` is returned
336 fn append_local_to_string(&self, local_index: Local, buf: &mut String) -> Result<(), ()> {
337 let local = &self.body.local_decls[local_index];
338 match local.name {
339 Some(name) if !local.from_compiler_desugaring() => {
340 buf.push_str(&name.as_str());
341 Ok(())
342 }
343 _ => Err(()),
344 }
345 }
346
347 /// End-user visible description of the `field`nth field of `base`
348 fn describe_field(&self, place: PlaceRef<'cx, 'tcx>, field: Field) -> String {
349 // FIXME Place2 Make this work iteratively
350 match place {
351 PlaceRef {
352 base: PlaceBase::Local(local),
353 projection: [],
354 } => {
355 let local = &self.body.local_decls[*local];
356 self.describe_field_from_ty(&local.ty, field, None)
357 }
358 PlaceRef {
359 base: PlaceBase::Static(static_),
360 projection: [],
361 } =>
362 self.describe_field_from_ty(&static_.ty, field, None),
363 PlaceRef {
364 base,
365 projection: [proj_base @ .., elem],
366 } => match elem {
367 ProjectionElem::Deref => {
368 self.describe_field(PlaceRef {
369 base,
370 projection: proj_base,
371 }, field)
372 }
373 ProjectionElem::Downcast(_, variant_index) => {
374 let base_ty =
375 Place::ty_from(place.base, place.projection, self.body, self.infcx.tcx).ty;
376 self.describe_field_from_ty(&base_ty, field, Some(*variant_index))
377 }
378 ProjectionElem::Field(_, field_type) => {
379 self.describe_field_from_ty(&field_type, field, None)
380 }
381 ProjectionElem::Index(..)
382 | ProjectionElem::ConstantIndex { .. }
383 | ProjectionElem::Subslice { .. } => {
384 self.describe_field(PlaceRef {
385 base,
386 projection: proj_base,
387 }, field)
388 }
389 },
390 }
391 }
392
393 /// End-user visible description of the `field_index`nth field of `ty`
394 fn describe_field_from_ty(
395 &self,
396 ty: Ty<'_>,
397 field: Field,
398 variant_index: Option<VariantIdx>
399 ) -> String {
400 if ty.is_box() {
401 // If the type is a box, the field is described from the boxed type
402 self.describe_field_from_ty(&ty.boxed_ty(), field, variant_index)
403 } else {
404 match ty.kind {
405 ty::Adt(def, _) => {
406 let variant = if let Some(idx) = variant_index {
407 assert!(def.is_enum());
408 &def.variants[idx]
409 } else {
410 def.non_enum_variant()
411 };
412 variant.fields[field.index()]
413 .ident
414 .to_string()
415 },
416 ty::Tuple(_) => field.index().to_string(),
417 ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
418 self.describe_field_from_ty(&ty, field, variant_index)
419 }
420 ty::Array(ty, _) | ty::Slice(ty) =>
421 self.describe_field_from_ty(&ty, field, variant_index),
422 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
423 // `tcx.upvars(def_id)` returns an `Option`, which is `None` in case
424 // the closure comes from another crate. But in that case we wouldn't
425 // be borrowck'ing it, so we can just unwrap:
426 let (&var_id, _) = self.infcx.tcx.upvars(def_id).unwrap()
427 .get_index(field.index()).unwrap();
428
429 self.infcx.tcx.hir().name(var_id).to_string()
430 }
431 _ => {
432 // Might need a revision when the fields in trait RFC is implemented
433 // (https://github.com/rust-lang/rfcs/pull/1546)
434 bug!(
435 "End-user description not implemented for field access on `{:?}`",
436 ty
437 );
438 }
439 }
440 }
441 }
442
443 /// Checks if a place is a thread-local static.
444 pub fn is_place_thread_local(&self, place_ref: PlaceRef<'cx, 'tcx>) -> bool {
445 if let PlaceRef {
446 base: PlaceBase::Static(box Static {
447 kind: StaticKind::Static,
448 def_id,
449 ..
450 }),
451 projection: [],
452 } = place_ref {
453 let attrs = self.infcx.tcx.get_attrs(*def_id);
454 let is_thread_local = attrs.iter().any(|attr| attr.check_name(sym::thread_local));
455
456 debug!(
457 "is_place_thread_local: attrs={:?} is_thread_local={:?}",
458 attrs, is_thread_local
459 );
460 is_thread_local
461 } else {
462 debug!("is_place_thread_local: no");
463 false
464 }
465 }
466
467 /// Add a note that a type does not implement `Copy`
468 pub(super) fn note_type_does_not_implement_copy(
469 &self,
470 err: &mut DiagnosticBuilder<'a>,
471 place_desc: &str,
472 ty: Ty<'tcx>,
473 span: Option<Span>,
474 ) {
475 let message = format!(
476 "move occurs because {} has type `{}`, which does not implement the `Copy` trait",
477 place_desc,
478 ty,
479 );
480 if let Some(span) = span {
481 err.span_label(span, message);
482 } else {
483 err.note(&message);
484 }
485 }
486
487 pub(super) fn borrowed_content_source(
488 &self,
489 deref_base: PlaceRef<'cx, 'tcx>,
490 ) -> BorrowedContentSource<'tcx> {
491 let tcx = self.infcx.tcx;
492
493 // Look up the provided place and work out the move path index for it,
494 // we'll use this to check whether it was originally from an overloaded
495 // operator.
496 match self.move_data.rev_lookup.find(deref_base) {
497 LookupResult::Exact(mpi) | LookupResult::Parent(Some(mpi)) => {
498 debug!("borrowed_content_source: mpi={:?}", mpi);
499
500 for i in &self.move_data.init_path_map[mpi] {
501 let init = &self.move_data.inits[*i];
502 debug!("borrowed_content_source: init={:?}", init);
503 // We're only interested in statements that initialized a value, not the
504 // initializations from arguments.
505 let loc = match init.location {
506 InitLocation::Statement(stmt) => stmt,
507 _ => continue,
508 };
509
510 let bbd = &self.body[loc.block];
511 let is_terminator = bbd.statements.len() == loc.statement_index;
512 debug!(
513 "borrowed_content_source: loc={:?} is_terminator={:?}",
514 loc,
515 is_terminator,
516 );
517 if !is_terminator {
518 continue;
519 } else if let Some(Terminator {
520 kind: TerminatorKind::Call {
521 ref func,
522 from_hir_call: false,
523 ..
524 },
525 ..
526 }) = bbd.terminator {
527 if let Some(source)
528 = BorrowedContentSource::from_call(func.ty(self.body, tcx), tcx)
529 {
530 return source;
531 }
532 }
533 }
534 }
535 // Base is a `static` so won't be from an overloaded operator
536 _ => (),
537 };
538
539 // If we didn't find an overloaded deref or index, then assume it's a
540 // built in deref and check the type of the base.
541 let base_ty = Place::ty_from(deref_base.base, deref_base.projection, self.body, tcx).ty;
542 if base_ty.is_unsafe_ptr() {
543 BorrowedContentSource::DerefRawPointer
544 } else if base_ty.is_mutable_ptr() {
545 BorrowedContentSource::DerefMutableRef
546 } else {
547 BorrowedContentSource::DerefSharedRef
548 }
549 }
550 }
551
552 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
553 /// Return the name of the provided `Ty` (that must be a reference) with a synthesized lifetime
554 /// name where required.
555 pub(super) fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
556 let mut s = String::new();
557 let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
558
559 // We need to add synthesized lifetimes where appropriate. We do
560 // this by hooking into the pretty printer and telling it to label the
561 // lifetimes without names with the value `'0`.
562 match ty.kind {
563 ty::Ref(ty::RegionKind::ReLateBound(_, br), _, _)
564 | ty::Ref(
565 ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }),
566 _,
567 _,
568 ) => printer.region_highlight_mode.highlighting_bound_region(*br, counter),
569 _ => {}
570 }
571
572 let _ = ty.print(printer);
573 s
574 }
575
576 /// Returns the name of the provided `Ty` (that must be a reference)'s region with a
577 /// synthesized lifetime name where required.
578 pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
579 let mut s = String::new();
580 let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, &mut s, Namespace::TypeNS);
581
582 let region = match ty.kind {
583 ty::Ref(region, _, _) => {
584 match region {
585 ty::RegionKind::ReLateBound(_, br)
586 | ty::RegionKind::RePlaceholder(ty::PlaceholderRegion { name: br, .. }) => {
587 printer.region_highlight_mode.highlighting_bound_region(*br, counter)
588 }
589 _ => {}
590 }
591
592 region
593 }
594 _ => bug!("ty for annotation of borrow region is not a reference"),
595 };
596
597 let _ = region.print(printer);
598 s
599 }
600 }
601
602 // The span(s) associated to a use of a place.
603 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
604 pub(super) enum UseSpans {
605 // The access is caused by capturing a variable for a closure.
606 ClosureUse {
607 // This is true if the captured variable was from a generator.
608 generator_kind: Option<GeneratorKind>,
609 // The span of the args of the closure, including the `move` keyword if
610 // it's present.
611 args_span: Span,
612 // The span of the first use of the captured variable inside the closure.
613 var_span: Span,
614 },
615 // This access has a single span associated to it: common case.
616 OtherUse(Span),
617 }
618
619 impl UseSpans {
620 pub(super) fn args_or_use(self) -> Span {
621 match self {
622 UseSpans::ClosureUse {
623 args_span: span, ..
624 }
625 | UseSpans::OtherUse(span) => span,
626 }
627 }
628
629 pub(super) fn var_or_use(self) -> Span {
630 match self {
631 UseSpans::ClosureUse { var_span: span, .. } | UseSpans::OtherUse(span) => span,
632 }
633 }
634
635 pub(super) fn generator_kind(self) -> Option<GeneratorKind> {
636 match self {
637 UseSpans::ClosureUse { generator_kind, .. } => generator_kind,
638 _ => None,
639 }
640 }
641
642 // Add a span label to the arguments of the closure, if it exists.
643 pub(super) fn args_span_label(
644 self,
645 err: &mut DiagnosticBuilder<'_>,
646 message: impl Into<String>,
647 ) {
648 if let UseSpans::ClosureUse { args_span, .. } = self {
649 err.span_label(args_span, message);
650 }
651 }
652
653 // Add a span label to the use of the captured variable, if it exists.
654 pub(super) fn var_span_label(
655 self,
656 err: &mut DiagnosticBuilder<'_>,
657 message: impl Into<String>,
658 ) {
659 if let UseSpans::ClosureUse { var_span, .. } = self {
660 err.span_label(var_span, message);
661 }
662 }
663
664 /// Returns `false` if this place is not used in a closure.
665 pub(super) fn for_closure(&self) -> bool {
666 match *self {
667 UseSpans::ClosureUse { generator_kind, .. } => generator_kind.is_none(),
668 _ => false,
669 }
670 }
671
672 /// Returns `false` if this place is not used in a generator.
673 pub(super) fn for_generator(&self) -> bool {
674 match *self {
675 UseSpans::ClosureUse { generator_kind, .. } => generator_kind.is_some(),
676 _ => false,
677 }
678 }
679
680 /// Describe the span associated with a use of a place.
681 pub(super) fn describe(&self) -> String {
682 match *self {
683 UseSpans::ClosureUse { generator_kind, .. } => if generator_kind.is_some() {
684 " in generator".to_string()
685 } else {
686 " in closure".to_string()
687 },
688 _ => "".to_string(),
689 }
690 }
691
692 pub(super) fn or_else<F>(self, if_other: F) -> Self
693 where
694 F: FnOnce() -> Self,
695 {
696 match self {
697 closure @ UseSpans::ClosureUse { .. } => closure,
698 UseSpans::OtherUse(_) => if_other(),
699 }
700 }
701 }
702
703 pub(super) enum BorrowedContentSource<'tcx> {
704 DerefRawPointer,
705 DerefMutableRef,
706 DerefSharedRef,
707 OverloadedDeref(Ty<'tcx>),
708 OverloadedIndex(Ty<'tcx>),
709 }
710
711 impl BorrowedContentSource<'tcx> {
712 pub(super) fn describe_for_unnamed_place(&self) -> String {
713 match *self {
714 BorrowedContentSource::DerefRawPointer => format!("a raw pointer"),
715 BorrowedContentSource::DerefSharedRef => format!("a shared reference"),
716 BorrowedContentSource::DerefMutableRef => {
717 format!("a mutable reference")
718 }
719 BorrowedContentSource::OverloadedDeref(ty) => {
720 if ty.is_rc() {
721 format!("an `Rc`")
722 } else if ty.is_arc() {
723 format!("an `Arc`")
724 } else {
725 format!("dereference of `{}`", ty)
726 }
727 }
728 BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
729 }
730 }
731
732 pub(super) fn describe_for_named_place(&self) -> Option<&'static str> {
733 match *self {
734 BorrowedContentSource::DerefRawPointer => Some("raw pointer"),
735 BorrowedContentSource::DerefSharedRef => Some("shared reference"),
736 BorrowedContentSource::DerefMutableRef => Some("mutable reference"),
737 // Overloaded deref and index operators should be evaluated into a
738 // temporary. So we don't need a description here.
739 BorrowedContentSource::OverloadedDeref(_)
740 | BorrowedContentSource::OverloadedIndex(_) => None
741 }
742 }
743
744 pub(super) fn describe_for_immutable_place(&self) -> String {
745 match *self {
746 BorrowedContentSource::DerefRawPointer => format!("a `*const` pointer"),
747 BorrowedContentSource::DerefSharedRef => format!("a `&` reference"),
748 BorrowedContentSource::DerefMutableRef => {
749 bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
750 },
751 BorrowedContentSource::OverloadedDeref(ty) => {
752 if ty.is_rc() {
753 format!("an `Rc`")
754 } else if ty.is_arc() {
755 format!("an `Arc`")
756 } else {
757 format!("a dereference of `{}`", ty)
758 }
759 }
760 BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
761 }
762 }
763
764 fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
765 match func.kind {
766 ty::FnDef(def_id, substs) => {
767 let trait_id = tcx.trait_of_item(def_id)?;
768
769 let lang_items = tcx.lang_items();
770 if Some(trait_id) == lang_items.deref_trait()
771 || Some(trait_id) == lang_items.deref_mut_trait()
772 {
773 Some(BorrowedContentSource::OverloadedDeref(substs.type_at(0)))
774 } else if Some(trait_id) == lang_items.index_trait()
775 || Some(trait_id) == lang_items.index_mut_trait()
776 {
777 Some(BorrowedContentSource::OverloadedIndex(substs.type_at(0)))
778 } else {
779 None
780 }
781 }
782 _ => None,
783 }
784 }
785 }
786
787 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
788 /// Finds the spans associated to a move or copy of move_place at location.
789 pub(super) fn move_spans(
790 &self,
791 moved_place: PlaceRef<'cx, 'tcx>, // Could also be an upvar.
792 location: Location,
793 ) -> UseSpans {
794 use self::UseSpans::*;
795
796 let stmt = match self.body[location.block].statements.get(location.statement_index) {
797 Some(stmt) => stmt,
798 None => return OtherUse(self.body.source_info(location).span),
799 };
800
801 debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
802 if let StatementKind::Assign(
803 box(_, Rvalue::Aggregate(ref kind, ref places))
804 ) = stmt.kind {
805 let def_id = match kind {
806 box AggregateKind::Closure(def_id, _)
807 | box AggregateKind::Generator(def_id, _, _) => def_id,
808 _ => return OtherUse(stmt.source_info.span),
809 };
810
811 debug!(
812 "move_spans: def_id={:?} places={:?}",
813 def_id, places
814 );
815 if let Some((args_span, generator_kind, var_span))
816 = self.closure_span(*def_id, moved_place, places) {
817 return ClosureUse {
818 generator_kind,
819 args_span,
820 var_span,
821 };
822 }
823 }
824
825 OtherUse(stmt.source_info.span)
826 }
827
828 /// Finds the span of arguments of a closure (within `maybe_closure_span`)
829 /// and its usage of the local assigned at `location`.
830 /// This is done by searching in statements succeeding `location`
831 /// and originating from `maybe_closure_span`.
832 pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans {
833 use self::UseSpans::*;
834 debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
835
836 let target = match self.body[location.block]
837 .statements
838 .get(location.statement_index)
839 {
840 Some(&Statement {
841 kind: StatementKind::Assign(box(ref place, _)),
842 ..
843 }) => {
844 if let Some(local) = place.as_local() {
845 local
846 } else {
847 return OtherUse(use_span);
848 }
849 }
850 _ => return OtherUse(use_span),
851 };
852
853 if self.body.local_kind(target) != LocalKind::Temp {
854 // operands are always temporaries.
855 return OtherUse(use_span);
856 }
857
858 for stmt in &self.body[location.block].statements[location.statement_index + 1..] {
859 if let StatementKind::Assign(
860 box(_, Rvalue::Aggregate(ref kind, ref places))
861 ) = stmt.kind {
862 let (def_id, is_generator) = match kind {
863 box AggregateKind::Closure(def_id, _) => (def_id, false),
864 box AggregateKind::Generator(def_id, _, _) => (def_id, true),
865 _ => continue,
866 };
867
868 debug!(
869 "borrow_spans: def_id={:?} is_generator={:?} places={:?}",
870 def_id, is_generator, places
871 );
872 if let Some((args_span, generator_kind, var_span)) = self.closure_span(
873 *def_id, Place::from(target).as_ref(), places
874 ) {
875 return ClosureUse {
876 generator_kind,
877 args_span,
878 var_span,
879 };
880 } else {
881 return OtherUse(use_span);
882 }
883 }
884
885 if use_span != stmt.source_info.span {
886 break;
887 }
888 }
889
890 OtherUse(use_span)
891 }
892
893 /// Finds the span of a captured variable within a closure or generator.
894 fn closure_span(
895 &self,
896 def_id: DefId,
897 target_place: PlaceRef<'cx, 'tcx>,
898 places: &Vec<Operand<'tcx>>,
899 ) -> Option<(Span, Option<GeneratorKind>, Span)> {
900 debug!(
901 "closure_span: def_id={:?} target_place={:?} places={:?}",
902 def_id, target_place, places
903 );
904 let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id)?;
905 let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind;
906 debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
907 if let hir::ExprKind::Closure(
908 .., body_id, args_span, _
909 ) = expr {
910 for (upvar, place) in self.infcx.tcx.upvars(def_id)?.values().zip(places) {
911 match place {
912 Operand::Copy(place) |
913 Operand::Move(place) if target_place == place.as_ref() => {
914 debug!("closure_span: found captured local {:?}", place);
915 let body = self.infcx.tcx.hir().body(*body_id);
916 let generator_kind = body.generator_kind();
917 return Some((*args_span, generator_kind, upvar.span));
918 },
919 _ => {}
920 }
921 }
922
923 }
924 None
925 }
926
927 /// Helper to retrieve span(s) of given borrow from the current MIR
928 /// representation
929 pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData<'_>) -> UseSpans {
930 let span = self.body.source_info(borrow.reserve_location).span;
931 self.borrow_spans(span, borrow.reserve_location)
932 }
933 }