]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/upvar.rs
Migrations first pass
[rust.git] / compiler / rustc_typeck / src / check / upvar.rs
1 //! ### Inferring borrow kinds for upvars
2 //!
3 //! Whenever there is a closure expression, we need to determine how each
4 //! upvar is used. We do this by initially assigning each upvar an
5 //! immutable "borrow kind" (see `ty::BorrowKind` for details) and then
6 //! "escalating" the kind as needed. The borrow kind proceeds according to
7 //! the following lattice:
8 //!
9 //!     ty::ImmBorrow -> ty::UniqueImmBorrow -> ty::MutBorrow
10 //!
11 //! So, for example, if we see an assignment `x = 5` to an upvar `x`, we
12 //! will promote its borrow kind to mutable borrow. If we see an `&mut x`
13 //! we'll do the same. Naturally, this applies not just to the upvar, but
14 //! to everything owned by `x`, so the result is the same for something
15 //! like `x.f = 5` and so on (presuming `x` is not a borrowed pointer to a
16 //! struct). These adjustments are performed in
17 //! `adjust_upvar_borrow_kind()` (you can trace backwards through the code
18 //! from there).
19 //!
20 //! The fact that we are inferring borrow kinds as we go results in a
21 //! semi-hacky interaction with mem-categorization. In particular,
22 //! mem-categorization will query the current borrow kind as it
23 //! categorizes, and we'll return the *current* value, but this may get
24 //! adjusted later. Therefore, in this module, we generally ignore the
25 //! borrow kind (and derived mutabilities) that are returned from
26 //! mem-categorization, since they may be inaccurate. (Another option
27 //! would be to use a unification scheme, where instead of returning a
28 //! concrete borrow kind like `ty::ImmBorrow`, we return a
29 //! `ty::InferBorrow(upvar_id)` or something like that, but this would
30 //! then mean that all later passes would have to check for these figments
31 //! and report an error, and it just seems like more mess in the end.)
32
33 use super::writeback::Resolver;
34 use super::FnCtxt;
35
36 use crate::expr_use_visitor as euv;
37 use rustc_data_structures::fx::FxIndexMap;
38 use rustc_hir as hir;
39 use rustc_hir::def_id::DefId;
40 use rustc_hir::def_id::LocalDefId;
41 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
42 use rustc_infer::infer::UpvarRegion;
43 use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, ProjectionKind};
44 use rustc_middle::ty::fold::TypeFoldable;
45 use rustc_middle::ty::{self, Ty, TyCtxt, TypeckResults, UpvarSubsts};
46 use rustc_session::lint;
47 use rustc_span::sym;
48 use rustc_span::{MultiSpan, Span, Symbol};
49
50 /// Describe the relationship between the paths of two places
51 /// eg:
52 /// - `foo` is ancestor of `foo.bar.baz`
53 /// - `foo.bar.baz` is an descendant of `foo.bar`
54 /// - `foo.bar` and `foo.baz` are divergent
55 enum PlaceAncestryRelation {
56     Ancestor,
57     Descendant,
58     Divergent,
59 }
60
61 /// Intermediate format to store a captured `Place` and associated `ty::CaptureInfo`
62 /// during capture analysis. Information in this map feeds into the minimum capture
63 /// analysis pass.
64 type InferredCaptureInformation<'tcx> = FxIndexMap<Place<'tcx>, ty::CaptureInfo<'tcx>>;
65
66 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
67     pub fn closure_analyze(&self, body: &'tcx hir::Body<'tcx>) {
68         InferBorrowKindVisitor { fcx: self }.visit_body(body);
69
70         // it's our job to process these.
71         assert!(self.deferred_call_resolutions.borrow().is_empty());
72     }
73 }
74
75 struct InferBorrowKindVisitor<'a, 'tcx> {
76     fcx: &'a FnCtxt<'a, 'tcx>,
77 }
78
79 impl<'a, 'tcx> Visitor<'tcx> for InferBorrowKindVisitor<'a, 'tcx> {
80     type Map = intravisit::ErasedMap<'tcx>;
81
82     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
83         NestedVisitorMap::None
84     }
85
86     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
87         if let hir::ExprKind::Closure(cc, _, body_id, _, _) = expr.kind {
88             let body = self.fcx.tcx.hir().body(body_id);
89             self.visit_body(body);
90             self.fcx.analyze_closure(expr.hir_id, expr.span, body, cc);
91         }
92
93         intravisit::walk_expr(self, expr);
94     }
95 }
96
97 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
98     /// Analysis starting point.
99     fn analyze_closure(
100         &self,
101         closure_hir_id: hir::HirId,
102         span: Span,
103         body: &'tcx hir::Body<'tcx>,
104         capture_clause: hir::CaptureBy,
105     ) {
106         debug!("analyze_closure(id={:?}, body.id={:?})", closure_hir_id, body.id());
107
108         // Extract the type of the closure.
109         let ty = self.node_ty(closure_hir_id);
110         let (closure_def_id, substs) = match *ty.kind() {
111             ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs)),
112             ty::Generator(def_id, substs, _) => (def_id, UpvarSubsts::Generator(substs)),
113             ty::Error(_) => {
114                 // #51714: skip analysis when we have already encountered type errors
115                 return;
116             }
117             _ => {
118                 span_bug!(
119                     span,
120                     "type of closure expr {:?} is not a closure {:?}",
121                     closure_hir_id,
122                     ty
123                 );
124             }
125         };
126
127         let infer_kind = if let UpvarSubsts::Closure(closure_substs) = substs {
128             self.closure_kind(closure_substs).is_none().then_some(closure_substs)
129         } else {
130             None
131         };
132
133         let local_def_id = closure_def_id.expect_local();
134
135         let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
136         assert_eq!(body_owner_def_id.to_def_id(), closure_def_id);
137         let mut delegate = InferBorrowKind {
138             fcx: self,
139             closure_def_id,
140             closure_span: span,
141             capture_clause,
142             current_closure_kind: ty::ClosureKind::LATTICE_BOTTOM,
143             current_origin: None,
144             capture_information: Default::default(),
145         };
146         euv::ExprUseVisitor::new(
147             &mut delegate,
148             &self.infcx,
149             body_owner_def_id,
150             self.param_env,
151             &self.typeck_results.borrow(),
152         )
153         .consume_body(body);
154
155         debug!(
156             "For closure={:?}, capture_information={:#?}",
157             closure_def_id, delegate.capture_information
158         );
159         self.log_capture_analysis_first_pass(closure_def_id, &delegate.capture_information, span);
160
161         self.compute_min_captures(closure_def_id, delegate.capture_information);
162
163         let closure_hir_id = self.tcx.hir().local_def_id_to_hir_id(local_def_id);
164         if should_do_migration_analysis(self.tcx, closure_hir_id) {
165             let need_migrations = self.compute_2229_migrations_first_pass(
166                 closure_def_id,
167                 span,
168                 capture_clause,
169                 body,
170                 self.typeck_results.borrow().closure_min_captures.get(&closure_def_id),
171             );
172
173             if !need_migrations.is_empty() {
174                 let need_migrations_hir_id =
175                     need_migrations.iter().map(|m| m.0).collect::<Vec<_>>();
176
177                 let migrations_text =
178                     migration_suggestion_for_2229(self.tcx, &need_migrations_hir_id);
179
180                 self.tcx.struct_span_lint_hir(
181                     lint::builtin::DISJOINT_CAPTURE_DROP_REORDER,
182                     closure_hir_id,
183                     span,
184                     |lint| {
185                         let mut diagnostics_builder = lint.build(
186                             "drop order affected for closure because of `capture_disjoint_fields`",
187                         );
188                         diagnostics_builder.note(&migrations_text);
189                         diagnostics_builder.emit();
190                     },
191                 );
192             }
193         }
194
195         // We now fake capture information for all variables that are mentioned within the closure
196         // We do this after handling migrations so that min_captures computes before
197         if !self.tcx.features().capture_disjoint_fields {
198             let mut capture_information: InferredCaptureInformation<'tcx> = Default::default();
199
200             if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
201                 for var_hir_id in upvars.keys() {
202                     let place = self.place_for_root_variable(local_def_id, *var_hir_id);
203
204                     debug!("seed place {:?}", place);
205
206                     let upvar_id = ty::UpvarId::new(*var_hir_id, local_def_id);
207                     let capture_kind = self.init_capture_kind(capture_clause, upvar_id, span);
208                     let fake_info = ty::CaptureInfo {
209                         capture_kind_expr_id: None,
210                         path_expr_id: None,
211                         capture_kind,
212                     };
213
214                     capture_information.insert(place, fake_info);
215                 }
216             }
217
218             // This will update the min captures based on this new fake information.
219             self.compute_min_captures(closure_def_id, capture_information);
220         }
221
222         if let Some(closure_substs) = infer_kind {
223             // Unify the (as yet unbound) type variable in the closure
224             // substs with the kind we inferred.
225             let inferred_kind = delegate.current_closure_kind;
226             let closure_kind_ty = closure_substs.as_closure().kind_ty();
227             self.demand_eqtype(span, inferred_kind.to_ty(self.tcx), closure_kind_ty);
228
229             // If we have an origin, store it.
230             if let Some(origin) = delegate.current_origin.clone() {
231                 let origin = if self.tcx.features().capture_disjoint_fields {
232                     origin
233                 } else {
234                     // FIXME(project-rfc-2229#31): Once the changes to support reborrowing are
235                     //                             made, make sure we are selecting and restricting
236                     //                             the origin correctly.
237                     (origin.0, Place { projections: vec![], ..origin.1 })
238                 };
239
240                 self.typeck_results
241                     .borrow_mut()
242                     .closure_kind_origins_mut()
243                     .insert(closure_hir_id, origin);
244             }
245         }
246
247         self.log_closure_min_capture_info(closure_def_id, span);
248
249         self.min_captures_to_closure_captures_bridge(closure_def_id);
250
251         // Now that we've analyzed the closure, we know how each
252         // variable is borrowed, and we know what traits the closure
253         // implements (Fn vs FnMut etc). We now have some updates to do
254         // with that information.
255         //
256         // Note that no closure type C may have an upvar of type C
257         // (though it may reference itself via a trait object). This
258         // results from the desugaring of closures to a struct like
259         // `Foo<..., UV0...UVn>`. If one of those upvars referenced
260         // C, then the type would have infinite size (and the
261         // inference algorithm will reject it).
262
263         // Equate the type variables for the upvars with the actual types.
264         let final_upvar_tys = self.final_upvar_tys(closure_def_id);
265         debug!(
266             "analyze_closure: id={:?} substs={:?} final_upvar_tys={:?}",
267             closure_hir_id, substs, final_upvar_tys
268         );
269
270         // Build a tuple (U0..Un) of the final upvar types U0..Un
271         // and unify the upvar tupe type in the closure with it:
272         let final_tupled_upvars_type = self.tcx.mk_tup(final_upvar_tys.iter());
273         self.demand_suptype(span, substs.tupled_upvars_ty(), final_tupled_upvars_type);
274
275         // If we are also inferred the closure kind here,
276         // process any deferred resolutions.
277         let deferred_call_resolutions = self.remove_deferred_call_resolutions(closure_def_id);
278         for deferred_call_resolution in deferred_call_resolutions {
279             deferred_call_resolution.resolve(self);
280         }
281     }
282
283     // Returns a list of `Ty`s for each upvar.
284     fn final_upvar_tys(&self, closure_id: DefId) -> Vec<Ty<'tcx>> {
285         // Presently an unboxed closure type cannot "escape" out of a
286         // function, so we will only encounter ones that originated in the
287         // local crate or were inlined into it along with some function.
288         // This may change if abstract return types of some sort are
289         // implemented.
290         let tcx = self.tcx;
291
292         self.typeck_results
293             .borrow()
294             .closure_min_captures_flattened(closure_id)
295             .map(|captured_place| {
296                 let upvar_ty = captured_place.place.ty();
297                 let capture = captured_place.info.capture_kind;
298
299                 debug!(
300                     "final_upvar_tys: place={:?} upvar_ty={:?} capture={:?}, mutability={:?}",
301                     captured_place.place, upvar_ty, capture, captured_place.mutability,
302                 );
303
304                 match capture {
305                     ty::UpvarCapture::ByValue(_) => upvar_ty,
306                     ty::UpvarCapture::ByRef(borrow) => tcx.mk_ref(
307                         borrow.region,
308                         ty::TypeAndMut { ty: upvar_ty, mutbl: borrow.kind.to_mutbl_lossy() },
309                     ),
310                 }
311             })
312             .collect()
313     }
314
315     /// Bridge for closure analysis
316     /// ----------------------------
317     ///
318     /// For closure with DefId `c`, the bridge converts structures required for supporting RFC 2229,
319     /// to structures currently used in the compiler for handling closure captures.
320     ///
321     /// For example the following structure will be converted:
322     ///
323     /// closure_min_captures
324     /// foo -> [ {foo.x, ImmBorrow}, {foo.y, MutBorrow} ]
325     /// bar -> [ {bar.z, ByValue}, {bar.q, MutBorrow} ]
326     ///
327     /// to
328     ///
329     /// 1. closure_captures
330     /// foo -> UpvarId(foo, c), bar -> UpvarId(bar, c)
331     ///
332     /// 2. upvar_capture_map
333     /// UpvarId(foo,c) -> MutBorrow, UpvarId(bar, c) -> ByValue
334     fn min_captures_to_closure_captures_bridge(&self, closure_def_id: DefId) {
335         let mut closure_captures: FxIndexMap<hir::HirId, ty::UpvarId> = Default::default();
336         let mut upvar_capture_map = ty::UpvarCaptureMap::default();
337
338         if let Some(min_captures) =
339             self.typeck_results.borrow().closure_min_captures.get(&closure_def_id)
340         {
341             for (var_hir_id, min_list) in min_captures.iter() {
342                 for captured_place in min_list {
343                     let place = &captured_place.place;
344                     let capture_info = captured_place.info;
345
346                     let upvar_id = match place.base {
347                         PlaceBase::Upvar(upvar_id) => upvar_id,
348                         base => bug!("Expected upvar, found={:?}", base),
349                     };
350
351                     assert_eq!(upvar_id.var_path.hir_id, *var_hir_id);
352                     assert_eq!(upvar_id.closure_expr_id, closure_def_id.expect_local());
353
354                     closure_captures.insert(*var_hir_id, upvar_id);
355
356                     let new_capture_kind =
357                         if let Some(capture_kind) = upvar_capture_map.get(&upvar_id) {
358                             // upvar_capture_map only stores the UpvarCapture (CaptureKind),
359                             // so we create a fake capture info with no expression.
360                             let fake_capture_info = ty::CaptureInfo {
361                                 capture_kind_expr_id: None,
362                                 path_expr_id: None,
363                                 capture_kind: *capture_kind,
364                             };
365                             determine_capture_info(fake_capture_info, capture_info).capture_kind
366                         } else {
367                             capture_info.capture_kind
368                         };
369                     upvar_capture_map.insert(upvar_id, new_capture_kind);
370                 }
371             }
372         }
373         debug!("For closure_def_id={:?}, closure_captures={:#?}", closure_def_id, closure_captures);
374         debug!(
375             "For closure_def_id={:?}, upvar_capture_map={:#?}",
376             closure_def_id, upvar_capture_map
377         );
378
379         if !closure_captures.is_empty() {
380             self.typeck_results
381                 .borrow_mut()
382                 .closure_captures
383                 .insert(closure_def_id, closure_captures);
384
385             self.typeck_results.borrow_mut().upvar_capture_map.extend(upvar_capture_map);
386         }
387     }
388
389     /// Analyzes the information collected by `InferBorrowKind` to compute the min number of
390     /// Places (and corresponding capture kind) that we need to keep track of to support all
391     /// the required captured paths.
392     ///
393     ///
394     /// Note: If this function is called multiple times for the same closure, it will update
395     ///       the existing min_capture map that is stored in TypeckResults.
396     ///
397     /// Eg:
398     /// ```rust,no_run
399     /// struct Point { x: i32, y: i32 }
400     ///
401     /// let s: String;  // hir_id_s
402     /// let mut p: Point; // his_id_p
403     /// let c = || {
404     ///        println!("{}", s);  // L1
405     ///        p.x += 10;  // L2
406     ///        println!("{}" , p.y) // L3
407     ///        println!("{}", p) // L4
408     ///        drop(s);   // L5
409     /// };
410     /// ```
411     /// and let hir_id_L1..5 be the expressions pointing to use of a captured variable on
412     /// the lines L1..5 respectively.
413     ///
414     /// InferBorrowKind results in a structure like this:
415     ///
416     /// ```
417     /// {
418     ///       Place(base: hir_id_s, projections: [], ....) -> {
419     ///                                                            capture_kind_expr: hir_id_L5,
420     ///                                                            path_expr_id: hir_id_L5,
421     ///                                                            capture_kind: ByValue
422     ///                                                       },
423     ///       Place(base: hir_id_p, projections: [Field(0, 0)], ...) -> {
424     ///                                                                     capture_kind_expr: hir_id_L2,
425     ///                                                                     path_expr_id: hir_id_L2,
426     ///                                                                     capture_kind: ByValue
427     ///                                                                 },
428     ///       Place(base: hir_id_p, projections: [Field(1, 0)], ...) -> {
429     ///                                                                     capture_kind_expr: hir_id_L3,
430     ///                                                                     path_expr_id: hir_id_L3,
431     ///                                                                     capture_kind: ByValue
432     ///                                                                 },
433     ///       Place(base: hir_id_p, projections: [], ...) -> {
434     ///                                                          capture_kind_expr: hir_id_L4,
435     ///                                                          path_expr_id: hir_id_L4,
436     ///                                                          capture_kind: ByValue
437     ///                                                      },
438     /// ```
439     ///
440     /// After the min capture analysis, we get:
441     /// ```
442     /// {
443     ///       hir_id_s -> [
444     ///            Place(base: hir_id_s, projections: [], ....) -> {
445     ///                                                                capture_kind_expr: hir_id_L5,
446     ///                                                                path_expr_id: hir_id_L5,
447     ///                                                                capture_kind: ByValue
448     ///                                                            },
449     ///       ],
450     ///       hir_id_p -> [
451     ///            Place(base: hir_id_p, projections: [], ...) -> {
452     ///                                                               capture_kind_expr: hir_id_L2,
453     ///                                                               path_expr_id: hir_id_L4,
454     ///                                                               capture_kind: ByValue
455     ///                                                           },
456     ///       ],
457     /// ```
458     fn compute_min_captures(
459         &self,
460         closure_def_id: DefId,
461         capture_information: InferredCaptureInformation<'tcx>,
462     ) {
463         if capture_information.is_empty() {
464             return;
465         }
466
467         let mut typeck_results = self.typeck_results.borrow_mut();
468
469         let mut root_var_min_capture_list =
470             typeck_results.closure_min_captures.remove(&closure_def_id).unwrap_or_default();
471
472         for (place, capture_info) in capture_information.into_iter() {
473             let var_hir_id = match place.base {
474                 PlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
475                 base => bug!("Expected upvar, found={:?}", base),
476             };
477
478             let place = restrict_capture_precision(place, capture_info.capture_kind);
479
480             let min_cap_list = match root_var_min_capture_list.get_mut(&var_hir_id) {
481                 None => {
482                     let mutability = self.determine_capture_mutability(&typeck_results, &place);
483                     let min_cap_list =
484                         vec![ty::CapturedPlace { place, info: capture_info, mutability }];
485                     root_var_min_capture_list.insert(var_hir_id, min_cap_list);
486                     continue;
487                 }
488                 Some(min_cap_list) => min_cap_list,
489             };
490
491             // Go through each entry in the current list of min_captures
492             // - if ancestor is found, update it's capture kind to account for current place's
493             // capture information.
494             //
495             // - if descendant is found, remove it from the list, and update the current place's
496             // capture information to account for the descendants's capture kind.
497             //
498             // We can never be in a case where the list contains both an ancestor and a descendant
499             // Also there can only be ancestor but in case of descendants there might be
500             // multiple.
501
502             let mut descendant_found = false;
503             let mut updated_capture_info = capture_info;
504             min_cap_list.retain(|possible_descendant| {
505                 match determine_place_ancestry_relation(&place, &possible_descendant.place) {
506                     // current place is ancestor of possible_descendant
507                     PlaceAncestryRelation::Ancestor => {
508                         descendant_found = true;
509                         let backup_path_expr_id = updated_capture_info.path_expr_id;
510
511                         updated_capture_info =
512                             determine_capture_info(updated_capture_info, possible_descendant.info);
513
514                         // we need to keep the ancestor's `path_expr_id`
515                         updated_capture_info.path_expr_id = backup_path_expr_id;
516                         false
517                     }
518
519                     _ => true,
520                 }
521             });
522
523             let mut ancestor_found = false;
524             if !descendant_found {
525                 for possible_ancestor in min_cap_list.iter_mut() {
526                     match determine_place_ancestry_relation(&place, &possible_ancestor.place) {
527                         // current place is descendant of possible_ancestor
528                         PlaceAncestryRelation::Descendant => {
529                             ancestor_found = true;
530                             let backup_path_expr_id = possible_ancestor.info.path_expr_id;
531                             possible_ancestor.info =
532                                 determine_capture_info(possible_ancestor.info, capture_info);
533
534                             // we need to keep the ancestor's `path_expr_id`
535                             possible_ancestor.info.path_expr_id = backup_path_expr_id;
536
537                             // Only one ancestor of the current place will be in the list.
538                             break;
539                         }
540                         _ => {}
541                     }
542                 }
543             }
544
545             // Only need to insert when we don't have an ancestor in the existing min capture list
546             if !ancestor_found {
547                 let mutability = self.determine_capture_mutability(&typeck_results, &place);
548                 let captured_place =
549                     ty::CapturedPlace { place, info: updated_capture_info, mutability };
550                 min_cap_list.push(captured_place);
551             }
552         }
553
554         debug!("For closure={:?}, min_captures={:#?}", closure_def_id, root_var_min_capture_list);
555         typeck_results.closure_min_captures.insert(closure_def_id, root_var_min_capture_list);
556     }
557
558     /// Figures out the list of root variables (and their types) that aren't completely
559     /// captured by the closure when `capture_disjoint_fields` is enabled and drop order of
560     /// some path starting at that root variable **might** be affected.
561     ///
562     /// The output list would include a root variable if:
563     /// - It would have been moved into the closure when `capture_disjoint_fields` wasn't
564     ///   enabled, **and**
565     /// - It wasn't completely captured by the closure, **and**
566     /// - The type of the root variable needs Drop.
567     fn compute_2229_migrations_first_pass(
568         &self,
569         closure_def_id: DefId,
570         closure_span: Span,
571         closure_clause: hir::CaptureBy,
572         body: &'tcx hir::Body<'tcx>,
573         min_captures: Option<&ty::RootVariableMinCaptureList<'tcx>>,
574     ) -> Vec<(hir::HirId, Ty<'tcx>)> {
575         fn resolve_ty<T: TypeFoldable<'tcx>>(
576             fcx: &FnCtxt<'_, 'tcx>,
577             span: Span,
578             body: &'tcx hir::Body<'tcx>,
579             ty: T,
580         ) -> T {
581             let mut resolver = Resolver::new(fcx, &span, body);
582             ty.fold_with(&mut resolver)
583         }
584
585         let upvars = if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
586             upvars
587         } else {
588             return vec![];
589         };
590
591         let mut need_migrations = Vec::new();
592
593         for (&var_hir_id, _) in upvars.iter() {
594             let ty = resolve_ty(self, closure_span, body, self.node_ty(var_hir_id));
595
596             if !ty.needs_drop(self.tcx, self.tcx.param_env(closure_def_id.expect_local())) {
597                 continue;
598             }
599
600             let root_var_min_capture_list = if let Some(root_var_min_capture_list) =
601                 min_captures.and_then(|m| m.get(&var_hir_id))
602             {
603                 root_var_min_capture_list
604             } else {
605                 // The upvar is mentioned within the closure but no path starting from it is
606                 // used.
607
608                 match closure_clause {
609                     // Only migrate if closure is a move closure
610                     hir::CaptureBy::Value => need_migrations.push((var_hir_id, ty)),
611
612                     hir::CaptureBy::Ref => {}
613                 }
614
615                 continue;
616             };
617
618             let is_moved = root_var_min_capture_list
619                 .iter()
620                 .find(|capture| matches!(capture.info.capture_kind, ty::UpvarCapture::ByValue(_)))
621                 .is_some();
622
623             // 1. If we capture more than one path starting at the root variabe then the root variable
624             //    isn't being captured in its entirety
625             // 2. If we only capture one path starting at the root variable, it's still possible
626             //    that it isn't the root variable completely.
627             if is_moved
628                 && ((root_var_min_capture_list.len() > 1)
629                     || (root_var_min_capture_list[0].place.projections.len() > 0))
630             {
631                 need_migrations.push((var_hir_id, ty));
632             }
633         }
634
635         need_migrations
636     }
637
638     fn init_capture_kind(
639         &self,
640         capture_clause: hir::CaptureBy,
641         upvar_id: ty::UpvarId,
642         closure_span: Span,
643     ) -> ty::UpvarCapture<'tcx> {
644         match capture_clause {
645             hir::CaptureBy::Value => ty::UpvarCapture::ByValue(None),
646             hir::CaptureBy::Ref => {
647                 let origin = UpvarRegion(upvar_id, closure_span);
648                 let upvar_region = self.next_region_var(origin);
649                 let upvar_borrow = ty::UpvarBorrow { kind: ty::ImmBorrow, region: upvar_region };
650                 ty::UpvarCapture::ByRef(upvar_borrow)
651             }
652         }
653     }
654
655     fn place_for_root_variable(
656         &self,
657         closure_def_id: LocalDefId,
658         var_hir_id: hir::HirId,
659     ) -> Place<'tcx> {
660         let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id);
661
662         Place {
663             base_ty: self.node_ty(var_hir_id),
664             base: PlaceBase::Upvar(upvar_id),
665             projections: Default::default(),
666         }
667     }
668
669     fn should_log_capture_analysis(&self, closure_def_id: DefId) -> bool {
670         self.tcx.has_attr(closure_def_id, sym::rustc_capture_analysis)
671     }
672
673     fn log_capture_analysis_first_pass(
674         &self,
675         closure_def_id: rustc_hir::def_id::DefId,
676         capture_information: &FxIndexMap<Place<'tcx>, ty::CaptureInfo<'tcx>>,
677         closure_span: Span,
678     ) {
679         if self.should_log_capture_analysis(closure_def_id) {
680             let mut diag =
681                 self.tcx.sess.struct_span_err(closure_span, "First Pass analysis includes:");
682             for (place, capture_info) in capture_information {
683                 let capture_str = construct_capture_info_string(self.tcx, place, capture_info);
684                 let output_str = format!("Capturing {}", capture_str);
685
686                 let span =
687                     capture_info.path_expr_id.map_or(closure_span, |e| self.tcx.hir().span(e));
688                 diag.span_note(span, &output_str);
689             }
690             diag.emit();
691         }
692     }
693
694     fn log_closure_min_capture_info(&self, closure_def_id: DefId, closure_span: Span) {
695         if self.should_log_capture_analysis(closure_def_id) {
696             if let Some(min_captures) =
697                 self.typeck_results.borrow().closure_min_captures.get(&closure_def_id)
698             {
699                 let mut diag =
700                     self.tcx.sess.struct_span_err(closure_span, "Min Capture analysis includes:");
701
702                 for (_, min_captures_for_var) in min_captures {
703                     for capture in min_captures_for_var {
704                         let place = &capture.place;
705                         let capture_info = &capture.info;
706
707                         let capture_str =
708                             construct_capture_info_string(self.tcx, place, capture_info);
709                         let output_str = format!("Min Capture {}", capture_str);
710
711                         if capture.info.path_expr_id != capture.info.capture_kind_expr_id {
712                             let path_span = capture_info
713                                 .path_expr_id
714                                 .map_or(closure_span, |e| self.tcx.hir().span(e));
715                             let capture_kind_span = capture_info
716                                 .capture_kind_expr_id
717                                 .map_or(closure_span, |e| self.tcx.hir().span(e));
718
719                             let mut multi_span: MultiSpan =
720                                 MultiSpan::from_spans(vec![path_span, capture_kind_span]);
721
722                             let capture_kind_label =
723                                 construct_capture_kind_reason_string(self.tcx, place, capture_info);
724                             let path_label = construct_path_string(self.tcx, place);
725
726                             multi_span.push_span_label(path_span, path_label);
727                             multi_span.push_span_label(capture_kind_span, capture_kind_label);
728
729                             diag.span_note(multi_span, &output_str);
730                         } else {
731                             let span = capture_info
732                                 .path_expr_id
733                                 .map_or(closure_span, |e| self.tcx.hir().span(e));
734
735                             diag.span_note(span, &output_str);
736                         };
737                     }
738                 }
739                 diag.emit();
740             }
741         }
742     }
743
744     /// A captured place is mutable if
745     /// 1. Projections don't include a Deref of an immut-borrow, **and**
746     /// 2. PlaceBase is mut or projections include a Deref of a mut-borrow.
747     fn determine_capture_mutability(
748         &self,
749         typeck_results: &'a TypeckResults<'tcx>,
750         place: &Place<'tcx>,
751     ) -> hir::Mutability {
752         let var_hir_id = match place.base {
753             PlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
754             _ => unreachable!(),
755         };
756
757         let bm = *typeck_results.pat_binding_modes().get(var_hir_id).expect("missing binding mode");
758
759         let mut is_mutbl = match bm {
760             ty::BindByValue(mutability) => mutability,
761             ty::BindByReference(_) => hir::Mutability::Not,
762         };
763
764         for pointer_ty in place.deref_tys() {
765             match pointer_ty.kind() {
766                 // We don't capture derefs of raw ptrs
767                 ty::RawPtr(_) => unreachable!(),
768
769                 // Derefencing a mut-ref allows us to mut the Place if we don't deref
770                 // an immut-ref after on top of this.
771                 ty::Ref(.., hir::Mutability::Mut) => is_mutbl = hir::Mutability::Mut,
772
773                 // The place isn't mutable once we dereference a immutable reference.
774                 ty::Ref(.., hir::Mutability::Not) => return hir::Mutability::Not,
775
776                 // Dereferencing a box doesn't change mutability
777                 ty::Adt(def, ..) if def.is_box() => {}
778
779                 unexpected_ty => bug!("deref of unexpected pointer type {:?}", unexpected_ty),
780             }
781         }
782
783         is_mutbl
784     }
785 }
786
787 struct InferBorrowKind<'a, 'tcx> {
788     fcx: &'a FnCtxt<'a, 'tcx>,
789
790     // The def-id of the closure whose kind and upvar accesses are being inferred.
791     closure_def_id: DefId,
792
793     closure_span: Span,
794
795     capture_clause: hir::CaptureBy,
796
797     // The kind that we have inferred that the current closure
798     // requires. Note that we *always* infer a minimal kind, even if
799     // we don't always *use* that in the final result (i.e., sometimes
800     // we've taken the closure kind from the expectations instead, and
801     // for generators we don't even implement the closure traits
802     // really).
803     current_closure_kind: ty::ClosureKind,
804
805     // If we modified `current_closure_kind`, this field contains a `Some()` with the
806     // variable access that caused us to do so.
807     current_origin: Option<(Span, Place<'tcx>)>,
808
809     /// For each Place that is captured by the closure, we track the minimal kind of
810     /// access we need (ref, ref mut, move, etc) and the expression that resulted in such access.
811     ///
812     /// Consider closure where s.str1 is captured via an ImmutableBorrow and
813     /// s.str2 via a MutableBorrow
814     ///
815     /// ```rust,no_run
816     /// struct SomeStruct { str1: String, str2: String }
817     ///
818     /// // Assume that the HirId for the variable definition is `V1`
819     /// let mut s = SomeStruct { str1: format!("s1"), str2: format!("s2") }
820     ///
821     /// let fix_s = |new_s2| {
822     ///     // Assume that the HirId for the expression `s.str1` is `E1`
823     ///     println!("Updating SomeStruct with str1=", s.str1);
824     ///     // Assume that the HirId for the expression `*s.str2` is `E2`
825     ///     s.str2 = new_s2;
826     /// };
827     /// ```
828     ///
829     /// For closure `fix_s`, (at a high level) the map contains
830     ///
831     /// ```
832     /// Place { V1, [ProjectionKind::Field(Index=0, Variant=0)] } : CaptureKind { E1, ImmutableBorrow }
833     /// Place { V1, [ProjectionKind::Field(Index=1, Variant=0)] } : CaptureKind { E2, MutableBorrow }
834     /// ```
835     capture_information: InferredCaptureInformation<'tcx>,
836 }
837
838 impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
839     fn adjust_upvar_borrow_kind_for_consume(
840         &mut self,
841         place_with_id: &PlaceWithHirId<'tcx>,
842         diag_expr_id: hir::HirId,
843         mode: euv::ConsumeMode,
844     ) {
845         debug!(
846             "adjust_upvar_borrow_kind_for_consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})",
847             place_with_id, diag_expr_id, mode
848         );
849
850         // we only care about moves
851         match mode {
852             euv::Copy => {
853                 return;
854             }
855             euv::Move => {}
856         }
857
858         let tcx = self.fcx.tcx;
859         let upvar_id = if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
860             upvar_id
861         } else {
862             return;
863         };
864
865         debug!("adjust_upvar_borrow_kind_for_consume: upvar={:?}", upvar_id);
866
867         let usage_span = tcx.hir().span(diag_expr_id);
868
869         // To move out of an upvar, this must be a FnOnce closure
870         self.adjust_closure_kind(
871             upvar_id.closure_expr_id,
872             ty::ClosureKind::FnOnce,
873             usage_span,
874             place_with_id.place.clone(),
875         );
876
877         let capture_info = ty::CaptureInfo {
878             capture_kind_expr_id: Some(diag_expr_id),
879             path_expr_id: Some(diag_expr_id),
880             capture_kind: ty::UpvarCapture::ByValue(Some(usage_span)),
881         };
882
883         let curr_info = self.capture_information[&place_with_id.place];
884         let updated_info = determine_capture_info(curr_info, capture_info);
885
886         self.capture_information[&place_with_id.place] = updated_info;
887     }
888
889     /// Indicates that `place_with_id` is being directly mutated (e.g., assigned
890     /// to). If the place is based on a by-ref upvar, this implies that
891     /// the upvar must be borrowed using an `&mut` borrow.
892     fn adjust_upvar_borrow_kind_for_mut(
893         &mut self,
894         place_with_id: &PlaceWithHirId<'tcx>,
895         diag_expr_id: hir::HirId,
896     ) {
897         debug!(
898             "adjust_upvar_borrow_kind_for_mut(place_with_id={:?}, diag_expr_id={:?})",
899             place_with_id, diag_expr_id
900         );
901
902         if let PlaceBase::Upvar(_) = place_with_id.place.base {
903             let mut borrow_kind = ty::MutBorrow;
904             for pointer_ty in place_with_id.place.deref_tys() {
905                 match pointer_ty.kind() {
906                     // Raw pointers don't inherit mutability.
907                     ty::RawPtr(_) => return,
908                     // assignment to deref of an `&mut`
909                     // borrowed pointer implies that the
910                     // pointer itself must be unique, but not
911                     // necessarily *mutable*
912                     ty::Ref(.., hir::Mutability::Mut) => borrow_kind = ty::UniqueImmBorrow,
913                     _ => (),
914                 }
915             }
916             self.adjust_upvar_deref(place_with_id, diag_expr_id, borrow_kind);
917         }
918     }
919
920     fn adjust_upvar_borrow_kind_for_unique(
921         &mut self,
922         place_with_id: &PlaceWithHirId<'tcx>,
923         diag_expr_id: hir::HirId,
924     ) {
925         debug!(
926             "adjust_upvar_borrow_kind_for_unique(place_with_id={:?}, diag_expr_id={:?})",
927             place_with_id, diag_expr_id
928         );
929
930         if let PlaceBase::Upvar(_) = place_with_id.place.base {
931             if place_with_id.place.deref_tys().any(ty::TyS::is_unsafe_ptr) {
932                 // Raw pointers don't inherit mutability.
933                 return;
934             }
935             // for a borrowed pointer to be unique, its base must be unique
936             self.adjust_upvar_deref(place_with_id, diag_expr_id, ty::UniqueImmBorrow);
937         }
938     }
939
940     fn adjust_upvar_deref(
941         &mut self,
942         place_with_id: &PlaceWithHirId<'tcx>,
943         diag_expr_id: hir::HirId,
944         borrow_kind: ty::BorrowKind,
945     ) {
946         assert!(match borrow_kind {
947             ty::MutBorrow => true,
948             ty::UniqueImmBorrow => true,
949
950             // imm borrows never require adjusting any kinds, so we don't wind up here
951             ty::ImmBorrow => false,
952         });
953
954         let tcx = self.fcx.tcx;
955
956         // if this is an implicit deref of an
957         // upvar, then we need to modify the
958         // borrow_kind of the upvar to make sure it
959         // is inferred to mutable if necessary
960         self.adjust_upvar_borrow_kind(place_with_id, diag_expr_id, borrow_kind);
961
962         if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
963             self.adjust_closure_kind(
964                 upvar_id.closure_expr_id,
965                 ty::ClosureKind::FnMut,
966                 tcx.hir().span(diag_expr_id),
967                 place_with_id.place.clone(),
968             );
969         }
970     }
971
972     /// We infer the borrow_kind with which to borrow upvars in a stack closure.
973     /// The borrow_kind basically follows a lattice of `imm < unique-imm < mut`,
974     /// moving from left to right as needed (but never right to left).
975     /// Here the argument `mutbl` is the borrow_kind that is required by
976     /// some particular use.
977     fn adjust_upvar_borrow_kind(
978         &mut self,
979         place_with_id: &PlaceWithHirId<'tcx>,
980         diag_expr_id: hir::HirId,
981         kind: ty::BorrowKind,
982     ) {
983         let curr_capture_info = self.capture_information[&place_with_id.place];
984
985         debug!(
986             "adjust_upvar_borrow_kind(place={:?}, diag_expr_id={:?}, capture_info={:?}, kind={:?})",
987             place_with_id, diag_expr_id, curr_capture_info, kind
988         );
989
990         if let ty::UpvarCapture::ByValue(_) = curr_capture_info.capture_kind {
991             // It's already captured by value, we don't need to do anything here
992             return;
993         } else if let ty::UpvarCapture::ByRef(curr_upvar_borrow) = curr_capture_info.capture_kind {
994             // Use the same region as the current capture information
995             // Doesn't matter since only one of the UpvarBorrow will be used.
996             let new_upvar_borrow = ty::UpvarBorrow { kind, region: curr_upvar_borrow.region };
997
998             let capture_info = ty::CaptureInfo {
999                 capture_kind_expr_id: Some(diag_expr_id),
1000                 path_expr_id: Some(diag_expr_id),
1001                 capture_kind: ty::UpvarCapture::ByRef(new_upvar_borrow),
1002             };
1003             let updated_info = determine_capture_info(curr_capture_info, capture_info);
1004             self.capture_information[&place_with_id.place] = updated_info;
1005         };
1006     }
1007
1008     fn adjust_closure_kind(
1009         &mut self,
1010         closure_id: LocalDefId,
1011         new_kind: ty::ClosureKind,
1012         upvar_span: Span,
1013         place: Place<'tcx>,
1014     ) {
1015         debug!(
1016             "adjust_closure_kind(closure_id={:?}, new_kind={:?}, upvar_span={:?}, place={:?})",
1017             closure_id, new_kind, upvar_span, place
1018         );
1019
1020         // Is this the closure whose kind is currently being inferred?
1021         if closure_id.to_def_id() != self.closure_def_id {
1022             debug!("adjust_closure_kind: not current closure");
1023             return;
1024         }
1025
1026         // closures start out as `Fn`.
1027         let existing_kind = self.current_closure_kind;
1028
1029         debug!(
1030             "adjust_closure_kind: closure_id={:?}, existing_kind={:?}, new_kind={:?}",
1031             closure_id, existing_kind, new_kind
1032         );
1033
1034         match (existing_kind, new_kind) {
1035             (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
1036             | (ty::ClosureKind::FnMut, ty::ClosureKind::Fn | ty::ClosureKind::FnMut)
1037             | (ty::ClosureKind::FnOnce, _) => {
1038                 // no change needed
1039             }
1040
1041             (ty::ClosureKind::Fn, ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce)
1042             | (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
1043                 // new kind is stronger than the old kind
1044                 self.current_closure_kind = new_kind;
1045                 self.current_origin = Some((upvar_span, place));
1046             }
1047         }
1048     }
1049
1050     fn init_capture_info_for_place(
1051         &mut self,
1052         place_with_id: &PlaceWithHirId<'tcx>,
1053         diag_expr_id: hir::HirId,
1054     ) {
1055         if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
1056             assert_eq!(self.closure_def_id.expect_local(), upvar_id.closure_expr_id);
1057
1058             let capture_kind =
1059                 self.fcx.init_capture_kind(self.capture_clause, upvar_id, self.closure_span);
1060
1061             let expr_id = Some(diag_expr_id);
1062             let capture_info = ty::CaptureInfo {
1063                 capture_kind_expr_id: expr_id,
1064                 path_expr_id: expr_id,
1065                 capture_kind,
1066             };
1067
1068             debug!("Capturing new place {:?}, capture_info={:?}", place_with_id, capture_info);
1069
1070             self.capture_information.insert(place_with_id.place.clone(), capture_info);
1071         } else {
1072             debug!("Not upvar: {:?}", place_with_id);
1073         }
1074     }
1075 }
1076
1077 impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
1078     fn consume(
1079         &mut self,
1080         place_with_id: &PlaceWithHirId<'tcx>,
1081         diag_expr_id: hir::HirId,
1082         mode: euv::ConsumeMode,
1083     ) {
1084         debug!(
1085             "consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})",
1086             place_with_id, diag_expr_id, mode
1087         );
1088         if !self.capture_information.contains_key(&place_with_id.place) {
1089             self.init_capture_info_for_place(place_with_id, diag_expr_id);
1090         }
1091
1092         self.adjust_upvar_borrow_kind_for_consume(place_with_id, diag_expr_id, mode);
1093     }
1094
1095     fn borrow(
1096         &mut self,
1097         place_with_id: &PlaceWithHirId<'tcx>,
1098         diag_expr_id: hir::HirId,
1099         bk: ty::BorrowKind,
1100     ) {
1101         debug!(
1102             "borrow(place_with_id={:?}, diag_expr_id={:?}, bk={:?})",
1103             place_with_id, diag_expr_id, bk
1104         );
1105
1106         if !self.capture_information.contains_key(&place_with_id.place) {
1107             self.init_capture_info_for_place(place_with_id, diag_expr_id);
1108         }
1109
1110         match bk {
1111             ty::ImmBorrow => {}
1112             ty::UniqueImmBorrow => {
1113                 self.adjust_upvar_borrow_kind_for_unique(&place_with_id, diag_expr_id);
1114             }
1115             ty::MutBorrow => {
1116                 self.adjust_upvar_borrow_kind_for_mut(&place_with_id, diag_expr_id);
1117             }
1118         }
1119     }
1120
1121     fn mutate(&mut self, assignee_place: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId) {
1122         debug!("mutate(assignee_place={:?}, diag_expr_id={:?})", assignee_place, diag_expr_id);
1123
1124         if !self.capture_information.contains_key(&assignee_place.place) {
1125             self.init_capture_info_for_place(assignee_place, diag_expr_id);
1126         }
1127
1128         self.adjust_upvar_borrow_kind_for_mut(assignee_place, diag_expr_id);
1129     }
1130 }
1131
1132 /// Truncate projections so that following rules are obeyed by the captured `place`:
1133 ///
1134 /// - No Derefs in move closure, this will result in value behind a reference getting moved.
1135 /// - No projections are applied to raw pointers, since these require unsafe blocks. We capture
1136 ///   them completely.
1137 /// - No Index projections are captured, since arrays are captured completely.
1138 fn restrict_capture_precision<'tcx>(
1139     mut place: Place<'tcx>,
1140     capture_kind: ty::UpvarCapture<'tcx>,
1141 ) -> Place<'tcx> {
1142     if place.projections.is_empty() {
1143         // Nothing to do here
1144         return place;
1145     }
1146
1147     if place.base_ty.is_unsafe_ptr() {
1148         place.projections.truncate(0);
1149         return place;
1150     }
1151
1152     let mut truncated_length = usize::MAX;
1153     let mut first_deref_projection = usize::MAX;
1154
1155     for (i, proj) in place.projections.iter().enumerate() {
1156         if proj.ty.is_unsafe_ptr() {
1157             // Don't apply any projections on top of an unsafe ptr
1158             truncated_length = truncated_length.min(i + 1);
1159             break;
1160         }
1161         match proj.kind {
1162             ProjectionKind::Index => {
1163                 // Arrays are completely captured, so we drop Index projections
1164                 truncated_length = truncated_length.min(i);
1165                 break;
1166             }
1167             ProjectionKind::Deref => {
1168                 // We only drop Derefs in case of move closures
1169                 // There might be an index projection or raw ptr ahead, so we don't stop here.
1170                 first_deref_projection = first_deref_projection.min(i);
1171             }
1172             ProjectionKind::Field(..) => {} // ignore
1173             ProjectionKind::Subslice => {}  // We never capture this
1174         }
1175     }
1176
1177     let length = place
1178         .projections
1179         .len()
1180         .min(truncated_length)
1181         // In case of capture `ByValue` we want to not capture derefs
1182         .min(match capture_kind {
1183             ty::UpvarCapture::ByValue(..) => first_deref_projection,
1184             ty::UpvarCapture::ByRef(..) => usize::MAX,
1185         });
1186
1187     place.projections.truncate(length);
1188
1189     place
1190 }
1191
1192 fn construct_place_string(tcx: TyCtxt<'_>, place: &Place<'tcx>) -> String {
1193     let variable_name = match place.base {
1194         PlaceBase::Upvar(upvar_id) => var_name(tcx, upvar_id.var_path.hir_id).to_string(),
1195         _ => bug!("Capture_information should only contain upvars"),
1196     };
1197
1198     let mut projections_str = String::new();
1199     for (i, item) in place.projections.iter().enumerate() {
1200         let proj = match item.kind {
1201             ProjectionKind::Field(a, b) => format!("({:?}, {:?})", a, b),
1202             ProjectionKind::Deref => String::from("Deref"),
1203             ProjectionKind::Index => String::from("Index"),
1204             ProjectionKind::Subslice => String::from("Subslice"),
1205         };
1206         if i != 0 {
1207             projections_str.push_str(",");
1208         }
1209         projections_str.push_str(proj.as_str());
1210     }
1211
1212     format!("{}[{}]", variable_name, projections_str)
1213 }
1214
1215 fn construct_capture_kind_reason_string(
1216     tcx: TyCtxt<'_>,
1217     place: &Place<'tcx>,
1218     capture_info: &ty::CaptureInfo<'tcx>,
1219 ) -> String {
1220     let place_str = construct_place_string(tcx, &place);
1221
1222     let capture_kind_str = match capture_info.capture_kind {
1223         ty::UpvarCapture::ByValue(_) => "ByValue".into(),
1224         ty::UpvarCapture::ByRef(borrow) => format!("{:?}", borrow.kind),
1225     };
1226
1227     format!("{} captured as {} here", place_str, capture_kind_str)
1228 }
1229
1230 fn construct_path_string(tcx: TyCtxt<'_>, place: &Place<'tcx>) -> String {
1231     let place_str = construct_place_string(tcx, &place);
1232
1233     format!("{} used here", place_str)
1234 }
1235
1236 fn construct_capture_info_string(
1237     tcx: TyCtxt<'_>,
1238     place: &Place<'tcx>,
1239     capture_info: &ty::CaptureInfo<'tcx>,
1240 ) -> String {
1241     let place_str = construct_place_string(tcx, &place);
1242
1243     let capture_kind_str = match capture_info.capture_kind {
1244         ty::UpvarCapture::ByValue(_) => "ByValue".into(),
1245         ty::UpvarCapture::ByRef(borrow) => format!("{:?}", borrow.kind),
1246     };
1247     format!("{} -> {}", place_str, capture_kind_str)
1248 }
1249
1250 fn var_name(tcx: TyCtxt<'_>, var_hir_id: hir::HirId) -> Symbol {
1251     tcx.hir().name(var_hir_id)
1252 }
1253
1254 fn should_do_migration_analysis(tcx: TyCtxt<'_>, closure_id: hir::HirId) -> bool {
1255     let (level, _) =
1256         tcx.lint_level_at_node(lint::builtin::DISJOINT_CAPTURE_DROP_REORDER, closure_id);
1257
1258     !matches!(level, lint::Level::Allow)
1259 }
1260
1261 fn migration_suggestion_for_2229(tcx: TyCtxt<'_>, need_migrations: &Vec<hir::HirId>) -> String {
1262     let need_migrations_strings =
1263         need_migrations.iter().map(|v| format!("{}", var_name(tcx, *v))).collect::<Vec<_>>();
1264     let migrations_list_concat = need_migrations_strings.join(", ");
1265
1266     format!("let ({}) = ({});", migrations_list_concat, migrations_list_concat)
1267 }
1268
1269 /// Helper function to determine if we need to escalate CaptureKind from
1270 /// CaptureInfo A to B and returns the escalated CaptureInfo.
1271 /// (Note: CaptureInfo contains CaptureKind and an expression that led to capture it in that way)
1272 ///
1273 /// If both `CaptureKind`s are considered equivalent, then the CaptureInfo is selected based
1274 /// on the `CaptureInfo` containing an associated `capture_kind_expr_id`.
1275 ///
1276 /// It is the caller's duty to figure out which path_expr_id to use.
1277 ///
1278 /// If both the CaptureKind and Expression are considered to be equivalent,
1279 /// then `CaptureInfo` A is preferred. This can be useful in cases where we want to priortize
1280 /// expressions reported back to the user as part of diagnostics based on which appears earlier
1281 /// in the closure. This can be acheived simply by calling
1282 /// `determine_capture_info(existing_info, current_info)`. This works out because the
1283 /// expressions that occur earlier in the closure body than the current expression are processed before.
1284 /// Consider the following example
1285 /// ```rust,no_run
1286 /// struct Point { x: i32, y: i32 }
1287 /// let mut p: Point { x: 10, y: 10 };
1288 ///
1289 /// let c = || {
1290 ///     p.x     += 10;
1291 /// // ^ E1 ^
1292 ///     // ...
1293 ///     // More code
1294 ///     // ...
1295 ///     p.x += 10; // E2
1296 /// // ^ E2 ^
1297 /// };
1298 /// ```
1299 /// `CaptureKind` associated with both `E1` and `E2` will be ByRef(MutBorrow),
1300 /// and both have an expression associated, however for diagnostics we prefer reporting
1301 /// `E1` since it appears earlier in the closure body. When `E2` is being processed we
1302 /// would've already handled `E1`, and have an existing capture_information for it.
1303 /// Calling `determine_capture_info(existing_info_e1, current_info_e2)` will return
1304 /// `existing_info_e1` in this case, allowing us to point to `E1` in case of diagnostics.
1305 fn determine_capture_info(
1306     capture_info_a: ty::CaptureInfo<'tcx>,
1307     capture_info_b: ty::CaptureInfo<'tcx>,
1308 ) -> ty::CaptureInfo<'tcx> {
1309     // If the capture kind is equivalent then, we don't need to escalate and can compare the
1310     // expressions.
1311     let eq_capture_kind = match (capture_info_a.capture_kind, capture_info_b.capture_kind) {
1312         (ty::UpvarCapture::ByValue(_), ty::UpvarCapture::ByValue(_)) => {
1313             // We don't need to worry about the spans being ignored here.
1314             //
1315             // The expr_id in capture_info corresponds to the span that is stored within
1316             // ByValue(span) and therefore it gets handled with priortizing based on
1317             // expressions below.
1318             true
1319         }
1320         (ty::UpvarCapture::ByRef(ref_a), ty::UpvarCapture::ByRef(ref_b)) => {
1321             ref_a.kind == ref_b.kind
1322         }
1323         (ty::UpvarCapture::ByValue(_), _) | (ty::UpvarCapture::ByRef(_), _) => false,
1324     };
1325
1326     if eq_capture_kind {
1327         match (capture_info_a.capture_kind_expr_id, capture_info_b.capture_kind_expr_id) {
1328             (Some(_), _) | (None, None) => capture_info_a,
1329             (None, Some(_)) => capture_info_b,
1330         }
1331     } else {
1332         // We select the CaptureKind which ranks higher based the following priority order:
1333         // ByValue > MutBorrow > UniqueImmBorrow > ImmBorrow
1334         match (capture_info_a.capture_kind, capture_info_b.capture_kind) {
1335             (ty::UpvarCapture::ByValue(_), _) => capture_info_a,
1336             (_, ty::UpvarCapture::ByValue(_)) => capture_info_b,
1337             (ty::UpvarCapture::ByRef(ref_a), ty::UpvarCapture::ByRef(ref_b)) => {
1338                 match (ref_a.kind, ref_b.kind) {
1339                     // Take LHS:
1340                     (ty::UniqueImmBorrow | ty::MutBorrow, ty::ImmBorrow)
1341                     | (ty::MutBorrow, ty::UniqueImmBorrow) => capture_info_a,
1342
1343                     // Take RHS:
1344                     (ty::ImmBorrow, ty::UniqueImmBorrow | ty::MutBorrow)
1345                     | (ty::UniqueImmBorrow, ty::MutBorrow) => capture_info_b,
1346
1347                     (ty::ImmBorrow, ty::ImmBorrow)
1348                     | (ty::UniqueImmBorrow, ty::UniqueImmBorrow)
1349                     | (ty::MutBorrow, ty::MutBorrow) => {
1350                         bug!("Expected unequal capture kinds");
1351                     }
1352                 }
1353             }
1354         }
1355     }
1356 }
1357
1358 /// Determines the Ancestry relationship of Place A relative to Place B
1359 ///
1360 /// `PlaceAncestryRelation::Ancestor` implies Place A is ancestor of Place B
1361 /// `PlaceAncestryRelation::Descendant` implies Place A is descendant of Place B
1362 /// `PlaceAncestryRelation::Divergent` implies neither of them is the ancestor of the other.
1363 fn determine_place_ancestry_relation(
1364     place_a: &Place<'tcx>,
1365     place_b: &Place<'tcx>,
1366 ) -> PlaceAncestryRelation {
1367     // If Place A and Place B, don't start off from the same root variable, they are divergent.
1368     if place_a.base != place_b.base {
1369         return PlaceAncestryRelation::Divergent;
1370     }
1371
1372     // Assume of length of projections_a = n
1373     let projections_a = &place_a.projections;
1374
1375     // Assume of length of projections_b = m
1376     let projections_b = &place_b.projections;
1377
1378     let mut same_initial_projections = true;
1379
1380     for (proj_a, proj_b) in projections_a.iter().zip(projections_b.iter()) {
1381         if proj_a != proj_b {
1382             same_initial_projections = false;
1383             break;
1384         }
1385     }
1386
1387     if same_initial_projections {
1388         // First min(n, m) projections are the same
1389         // Select Ancestor/Descendant
1390         if projections_b.len() >= projections_a.len() {
1391             PlaceAncestryRelation::Ancestor
1392         } else {
1393             PlaceAncestryRelation::Descendant
1394         }
1395     } else {
1396         PlaceAncestryRelation::Divergent
1397     }
1398 }