1 //! ### Inferring borrow kinds for upvars
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:
9 //! ty::ImmBorrow -> ty::UniqueImmBorrow -> ty::MutBorrow
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
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.)
33 use super::writeback::Resolver;
36 use crate::expr_use_visitor as euv;
37 use rustc_data_structures::fx::FxIndexMap;
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;
48 use rustc_span::{MultiSpan, Span, Symbol};
50 /// Describe the relationship between the paths of two places
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 {
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
64 type InferredCaptureInformation<'tcx> = FxIndexMap<Place<'tcx>, ty::CaptureInfo<'tcx>>;
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);
70 // it's our job to process these.
71 assert!(self.deferred_call_resolutions.borrow().is_empty());
75 struct InferBorrowKindVisitor<'a, 'tcx> {
76 fcx: &'a FnCtxt<'a, 'tcx>,
79 impl<'a, 'tcx> Visitor<'tcx> for InferBorrowKindVisitor<'a, 'tcx> {
80 type Map = intravisit::ErasedMap<'tcx>;
82 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
83 NestedVisitorMap::None
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);
93 intravisit::walk_expr(self, expr);
97 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
98 /// Analysis starting point.
101 closure_hir_id: hir::HirId,
103 body: &'tcx hir::Body<'tcx>,
104 capture_clause: hir::CaptureBy,
106 debug!("analyze_closure(id={:?}, body.id={:?})", closure_hir_id, body.id());
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)),
114 // #51714: skip analysis when we have already encountered type errors
120 "type of closure expr {:?} is not a closure {:?}",
127 let infer_kind = if let UpvarSubsts::Closure(closure_substs) = substs {
128 self.closure_kind(closure_substs).is_none().then_some(closure_substs)
133 let local_def_id = closure_def_id.expect_local();
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 {
142 current_closure_kind: ty::ClosureKind::LATTICE_BOTTOM,
143 current_origin: None,
144 capture_information: Default::default(),
146 euv::ExprUseVisitor::new(
151 &self.typeck_results.borrow(),
156 "For closure={:?}, capture_information={:#?}",
157 closure_def_id, delegate.capture_information
159 self.log_capture_analysis_first_pass(closure_def_id, &delegate.capture_information, span);
161 self.compute_min_captures(closure_def_id, delegate.capture_information);
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(
170 self.typeck_results.borrow().closure_min_captures.get(&closure_def_id),
173 if !need_migrations.is_empty() {
174 let need_migrations_hir_id =
175 need_migrations.iter().map(|m| m.0).collect::<Vec<_>>();
177 let migrations_text =
178 migration_suggestion_for_2229(self.tcx, &need_migrations_hir_id);
180 self.tcx.struct_span_lint_hir(
181 lint::builtin::DISJOINT_CAPTURE_DROP_REORDER,
185 let mut diagnostics_builder = lint.build(
186 "drop order affected for closure because of `capture_disjoint_fields`",
188 diagnostics_builder.note(&migrations_text);
189 diagnostics_builder.emit();
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();
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);
204 debug!("seed place {:?}", place);
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,
214 capture_information.insert(place, fake_info);
218 // This will update the min captures based on this new fake information.
219 self.compute_min_captures(closure_def_id, capture_information);
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);
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 {
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 })
242 .closure_kind_origins_mut()
243 .insert(closure_hir_id, origin);
247 self.log_closure_min_capture_info(closure_def_id, span);
249 self.min_captures_to_closure_captures_bridge(closure_def_id);
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.
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).
263 // Equate the type variables for the upvars with the actual types.
264 let final_upvar_tys = self.final_upvar_tys(closure_def_id);
266 "analyze_closure: id={:?} substs={:?} final_upvar_tys={:?}",
267 closure_hir_id, substs, final_upvar_tys
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);
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);
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
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;
300 "final_upvar_tys: place={:?} upvar_ty={:?} capture={:?}, mutability={:?}",
301 captured_place.place, upvar_ty, capture, captured_place.mutability,
305 ty::UpvarCapture::ByValue(_) => upvar_ty,
306 ty::UpvarCapture::ByRef(borrow) => tcx.mk_ref(
308 ty::TypeAndMut { ty: upvar_ty, mutbl: borrow.kind.to_mutbl_lossy() },
315 /// Bridge for closure analysis
316 /// ----------------------------
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.
321 /// For example the following structure will be converted:
323 /// closure_min_captures
324 /// foo -> [ {foo.x, ImmBorrow}, {foo.y, MutBorrow} ]
325 /// bar -> [ {bar.z, ByValue}, {bar.q, MutBorrow} ]
329 /// 1. closure_captures
330 /// foo -> UpvarId(foo, c), bar -> UpvarId(bar, c)
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();
338 if let Some(min_captures) =
339 self.typeck_results.borrow().closure_min_captures.get(&closure_def_id)
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;
346 let upvar_id = match place.base {
347 PlaceBase::Upvar(upvar_id) => upvar_id,
348 base => bug!("Expected upvar, found={:?}", base),
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());
354 closure_captures.insert(*var_hir_id, upvar_id);
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,
363 capture_kind: *capture_kind,
365 determine_capture_info(fake_capture_info, capture_info).capture_kind
367 capture_info.capture_kind
369 upvar_capture_map.insert(upvar_id, new_capture_kind);
373 debug!("For closure_def_id={:?}, closure_captures={:#?}", closure_def_id, closure_captures);
375 "For closure_def_id={:?}, upvar_capture_map={:#?}",
376 closure_def_id, upvar_capture_map
379 if !closure_captures.is_empty() {
383 .insert(closure_def_id, closure_captures);
385 self.typeck_results.borrow_mut().upvar_capture_map.extend(upvar_capture_map);
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.
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.
399 /// struct Point { x: i32, y: i32 }
401 /// let s: String; // hir_id_s
402 /// let mut p: Point; // his_id_p
404 /// println!("{}", s); // L1
406 /// println!("{}" , p.y) // L3
407 /// println!("{}", p) // L4
411 /// and let hir_id_L1..5 be the expressions pointing to use of a captured variable on
412 /// the lines L1..5 respectively.
414 /// InferBorrowKind results in a structure like this:
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
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
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
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
440 /// After the min capture analysis, we get:
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
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
458 fn compute_min_captures(
460 closure_def_id: DefId,
461 capture_information: InferredCaptureInformation<'tcx>,
463 if capture_information.is_empty() {
467 let mut typeck_results = self.typeck_results.borrow_mut();
469 let mut root_var_min_capture_list =
470 typeck_results.closure_min_captures.remove(&closure_def_id).unwrap_or_default();
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),
478 let place = restrict_capture_precision(place, capture_info.capture_kind);
480 let min_cap_list = match root_var_min_capture_list.get_mut(&var_hir_id) {
482 let mutability = self.determine_capture_mutability(&typeck_results, &place);
484 vec![ty::CapturedPlace { place, info: capture_info, mutability }];
485 root_var_min_capture_list.insert(var_hir_id, min_cap_list);
488 Some(min_cap_list) => min_cap_list,
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.
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.
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
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;
511 updated_capture_info =
512 determine_capture_info(updated_capture_info, possible_descendant.info);
514 // we need to keep the ancestor's `path_expr_id`
515 updated_capture_info.path_expr_id = backup_path_expr_id;
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);
534 // we need to keep the ancestor's `path_expr_id`
535 possible_ancestor.info.path_expr_id = backup_path_expr_id;
537 // Only one ancestor of the current place will be in the list.
545 // Only need to insert when we don't have an ancestor in the existing min capture list
547 let mutability = self.determine_capture_mutability(&typeck_results, &place);
549 ty::CapturedPlace { place, info: updated_capture_info, mutability };
550 min_cap_list.push(captured_place);
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);
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.
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
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(
569 closure_def_id: DefId,
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>,
578 body: &'tcx hir::Body<'tcx>,
581 let mut resolver = Resolver::new(fcx, &span, body);
582 ty.fold_with(&mut resolver)
585 let upvars = if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
591 let mut need_migrations = Vec::new();
593 for (&var_hir_id, _) in upvars.iter() {
594 let ty = resolve_ty(self, closure_span, body, self.node_ty(var_hir_id));
596 if !ty.needs_drop(self.tcx, self.tcx.param_env(closure_def_id.expect_local())) {
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))
603 root_var_min_capture_list
605 // The upvar is mentioned within the closure but no path starting from it is
608 match closure_clause {
609 // Only migrate if closure is a move closure
610 hir::CaptureBy::Value => need_migrations.push((var_hir_id, ty)),
612 hir::CaptureBy::Ref => {}
618 let is_moved = root_var_min_capture_list
620 .find(|capture| matches!(capture.info.capture_kind, ty::UpvarCapture::ByValue(_)))
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.
628 && ((root_var_min_capture_list.len() > 1)
629 || (root_var_min_capture_list[0].place.projections.len() > 0))
631 need_migrations.push((var_hir_id, ty));
638 fn init_capture_kind(
640 capture_clause: hir::CaptureBy,
641 upvar_id: ty::UpvarId,
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)
655 fn place_for_root_variable(
657 closure_def_id: LocalDefId,
658 var_hir_id: hir::HirId,
660 let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id);
663 base_ty: self.node_ty(var_hir_id),
664 base: PlaceBase::Upvar(upvar_id),
665 projections: Default::default(),
669 fn should_log_capture_analysis(&self, closure_def_id: DefId) -> bool {
670 self.tcx.has_attr(closure_def_id, sym::rustc_capture_analysis)
673 fn log_capture_analysis_first_pass(
675 closure_def_id: rustc_hir::def_id::DefId,
676 capture_information: &FxIndexMap<Place<'tcx>, ty::CaptureInfo<'tcx>>,
679 if self.should_log_capture_analysis(closure_def_id) {
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);
687 capture_info.path_expr_id.map_or(closure_span, |e| self.tcx.hir().span(e));
688 diag.span_note(span, &output_str);
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)
700 self.tcx.sess.struct_span_err(closure_span, "Min Capture analysis includes:");
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;
708 construct_capture_info_string(self.tcx, place, capture_info);
709 let output_str = format!("Min Capture {}", capture_str);
711 if capture.info.path_expr_id != capture.info.capture_kind_expr_id {
712 let path_span = capture_info
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));
719 let mut multi_span: MultiSpan =
720 MultiSpan::from_spans(vec![path_span, capture_kind_span]);
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);
726 multi_span.push_span_label(path_span, path_label);
727 multi_span.push_span_label(capture_kind_span, capture_kind_label);
729 diag.span_note(multi_span, &output_str);
731 let span = capture_info
733 .map_or(closure_span, |e| self.tcx.hir().span(e));
735 diag.span_note(span, &output_str);
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(
749 typeck_results: &'a TypeckResults<'tcx>,
751 ) -> hir::Mutability {
752 let var_hir_id = match place.base {
753 PlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
757 let bm = *typeck_results.pat_binding_modes().get(var_hir_id).expect("missing binding mode");
759 let mut is_mutbl = match bm {
760 ty::BindByValue(mutability) => mutability,
761 ty::BindByReference(_) => hir::Mutability::Not,
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!(),
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,
773 // The place isn't mutable once we dereference a immutable reference.
774 ty::Ref(.., hir::Mutability::Not) => return hir::Mutability::Not,
776 // Dereferencing a box doesn't change mutability
777 ty::Adt(def, ..) if def.is_box() => {}
779 unexpected_ty => bug!("deref of unexpected pointer type {:?}", unexpected_ty),
787 struct InferBorrowKind<'a, 'tcx> {
788 fcx: &'a FnCtxt<'a, 'tcx>,
790 // The def-id of the closure whose kind and upvar accesses are being inferred.
791 closure_def_id: DefId,
795 capture_clause: hir::CaptureBy,
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
803 current_closure_kind: ty::ClosureKind,
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>)>,
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.
812 /// Consider closure where s.str1 is captured via an ImmutableBorrow and
813 /// s.str2 via a MutableBorrow
816 /// struct SomeStruct { str1: String, str2: String }
818 /// // Assume that the HirId for the variable definition is `V1`
819 /// let mut s = SomeStruct { str1: format!("s1"), str2: format!("s2") }
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`
829 /// For closure `fix_s`, (at a high level) the map contains
832 /// Place { V1, [ProjectionKind::Field(Index=0, Variant=0)] } : CaptureKind { E1, ImmutableBorrow }
833 /// Place { V1, [ProjectionKind::Field(Index=1, Variant=0)] } : CaptureKind { E2, MutableBorrow }
835 capture_information: InferredCaptureInformation<'tcx>,
838 impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
839 fn adjust_upvar_borrow_kind_for_consume(
841 place_with_id: &PlaceWithHirId<'tcx>,
842 diag_expr_id: hir::HirId,
843 mode: euv::ConsumeMode,
846 "adjust_upvar_borrow_kind_for_consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})",
847 place_with_id, diag_expr_id, mode
850 // we only care about moves
858 let tcx = self.fcx.tcx;
859 let upvar_id = if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
865 debug!("adjust_upvar_borrow_kind_for_consume: upvar={:?}", upvar_id);
867 let usage_span = tcx.hir().span(diag_expr_id);
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,
874 place_with_id.place.clone(),
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)),
883 let curr_info = self.capture_information[&place_with_id.place];
884 let updated_info = determine_capture_info(curr_info, capture_info);
886 self.capture_information[&place_with_id.place] = updated_info;
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(
894 place_with_id: &PlaceWithHirId<'tcx>,
895 diag_expr_id: hir::HirId,
898 "adjust_upvar_borrow_kind_for_mut(place_with_id={:?}, diag_expr_id={:?})",
899 place_with_id, diag_expr_id
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,
916 self.adjust_upvar_deref(place_with_id, diag_expr_id, borrow_kind);
920 fn adjust_upvar_borrow_kind_for_unique(
922 place_with_id: &PlaceWithHirId<'tcx>,
923 diag_expr_id: hir::HirId,
926 "adjust_upvar_borrow_kind_for_unique(place_with_id={:?}, diag_expr_id={:?})",
927 place_with_id, diag_expr_id
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.
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);
940 fn adjust_upvar_deref(
942 place_with_id: &PlaceWithHirId<'tcx>,
943 diag_expr_id: hir::HirId,
944 borrow_kind: ty::BorrowKind,
946 assert!(match borrow_kind {
947 ty::MutBorrow => true,
948 ty::UniqueImmBorrow => true,
950 // imm borrows never require adjusting any kinds, so we don't wind up here
951 ty::ImmBorrow => false,
954 let tcx = self.fcx.tcx;
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);
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(),
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(
979 place_with_id: &PlaceWithHirId<'tcx>,
980 diag_expr_id: hir::HirId,
981 kind: ty::BorrowKind,
983 let curr_capture_info = self.capture_information[&place_with_id.place];
986 "adjust_upvar_borrow_kind(place={:?}, diag_expr_id={:?}, capture_info={:?}, kind={:?})",
987 place_with_id, diag_expr_id, curr_capture_info, kind
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
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 };
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),
1003 let updated_info = determine_capture_info(curr_capture_info, capture_info);
1004 self.capture_information[&place_with_id.place] = updated_info;
1008 fn adjust_closure_kind(
1010 closure_id: LocalDefId,
1011 new_kind: ty::ClosureKind,
1016 "adjust_closure_kind(closure_id={:?}, new_kind={:?}, upvar_span={:?}, place={:?})",
1017 closure_id, new_kind, upvar_span, place
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");
1026 // closures start out as `Fn`.
1027 let existing_kind = self.current_closure_kind;
1030 "adjust_closure_kind: closure_id={:?}, existing_kind={:?}, new_kind={:?}",
1031 closure_id, existing_kind, new_kind
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, _) => {
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));
1050 fn init_capture_info_for_place(
1052 place_with_id: &PlaceWithHirId<'tcx>,
1053 diag_expr_id: hir::HirId,
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);
1059 self.fcx.init_capture_kind(self.capture_clause, upvar_id, self.closure_span);
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,
1068 debug!("Capturing new place {:?}, capture_info={:?}", place_with_id, capture_info);
1070 self.capture_information.insert(place_with_id.place.clone(), capture_info);
1072 debug!("Not upvar: {:?}", place_with_id);
1077 impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
1080 place_with_id: &PlaceWithHirId<'tcx>,
1081 diag_expr_id: hir::HirId,
1082 mode: euv::ConsumeMode,
1085 "consume(place_with_id={:?}, diag_expr_id={:?}, mode={:?})",
1086 place_with_id, diag_expr_id, mode
1088 if !self.capture_information.contains_key(&place_with_id.place) {
1089 self.init_capture_info_for_place(place_with_id, diag_expr_id);
1092 self.adjust_upvar_borrow_kind_for_consume(place_with_id, diag_expr_id, mode);
1097 place_with_id: &PlaceWithHirId<'tcx>,
1098 diag_expr_id: hir::HirId,
1102 "borrow(place_with_id={:?}, diag_expr_id={:?}, bk={:?})",
1103 place_with_id, diag_expr_id, bk
1106 if !self.capture_information.contains_key(&place_with_id.place) {
1107 self.init_capture_info_for_place(place_with_id, diag_expr_id);
1112 ty::UniqueImmBorrow => {
1113 self.adjust_upvar_borrow_kind_for_unique(&place_with_id, diag_expr_id);
1116 self.adjust_upvar_borrow_kind_for_mut(&place_with_id, diag_expr_id);
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);
1124 if !self.capture_information.contains_key(&assignee_place.place) {
1125 self.init_capture_info_for_place(assignee_place, diag_expr_id);
1128 self.adjust_upvar_borrow_kind_for_mut(assignee_place, diag_expr_id);
1132 /// Truncate projections so that following rules are obeyed by the captured `place`:
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>,
1142 if place.projections.is_empty() {
1143 // Nothing to do here
1147 if place.base_ty.is_unsafe_ptr() {
1148 place.projections.truncate(0);
1152 let mut truncated_length = usize::MAX;
1153 let mut first_deref_projection = usize::MAX;
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);
1162 ProjectionKind::Index => {
1163 // Arrays are completely captured, so we drop Index projections
1164 truncated_length = truncated_length.min(i);
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);
1172 ProjectionKind::Field(..) => {} // ignore
1173 ProjectionKind::Subslice => {} // We never capture this
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,
1187 place.projections.truncate(length);
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"),
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"),
1207 projections_str.push_str(",");
1209 projections_str.push_str(proj.as_str());
1212 format!("{}[{}]", variable_name, projections_str)
1215 fn construct_capture_kind_reason_string(
1217 place: &Place<'tcx>,
1218 capture_info: &ty::CaptureInfo<'tcx>,
1220 let place_str = construct_place_string(tcx, &place);
1222 let capture_kind_str = match capture_info.capture_kind {
1223 ty::UpvarCapture::ByValue(_) => "ByValue".into(),
1224 ty::UpvarCapture::ByRef(borrow) => format!("{:?}", borrow.kind),
1227 format!("{} captured as {} here", place_str, capture_kind_str)
1230 fn construct_path_string(tcx: TyCtxt<'_>, place: &Place<'tcx>) -> String {
1231 let place_str = construct_place_string(tcx, &place);
1233 format!("{} used here", place_str)
1236 fn construct_capture_info_string(
1238 place: &Place<'tcx>,
1239 capture_info: &ty::CaptureInfo<'tcx>,
1241 let place_str = construct_place_string(tcx, &place);
1243 let capture_kind_str = match capture_info.capture_kind {
1244 ty::UpvarCapture::ByValue(_) => "ByValue".into(),
1245 ty::UpvarCapture::ByRef(borrow) => format!("{:?}", borrow.kind),
1247 format!("{} -> {}", place_str, capture_kind_str)
1250 fn var_name(tcx: TyCtxt<'_>, var_hir_id: hir::HirId) -> Symbol {
1251 tcx.hir().name(var_hir_id)
1254 fn should_do_migration_analysis(tcx: TyCtxt<'_>, closure_id: hir::HirId) -> bool {
1256 tcx.lint_level_at_node(lint::builtin::DISJOINT_CAPTURE_DROP_REORDER, closure_id);
1258 !matches!(level, lint::Level::Allow)
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(", ");
1266 format!("let ({}) = ({});", migrations_list_concat, migrations_list_concat)
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)
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`.
1276 /// It is the caller's duty to figure out which path_expr_id to use.
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
1286 /// struct Point { x: i32, y: i32 }
1287 /// let mut p: Point { x: 10, y: 10 };
1295 /// p.x += 10; // E2
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
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.
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.
1320 (ty::UpvarCapture::ByRef(ref_a), ty::UpvarCapture::ByRef(ref_b)) => {
1321 ref_a.kind == ref_b.kind
1323 (ty::UpvarCapture::ByValue(_), _) | (ty::UpvarCapture::ByRef(_), _) => false,
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,
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) {
1340 (ty::UniqueImmBorrow | ty::MutBorrow, ty::ImmBorrow)
1341 | (ty::MutBorrow, ty::UniqueImmBorrow) => capture_info_a,
1344 (ty::ImmBorrow, ty::UniqueImmBorrow | ty::MutBorrow)
1345 | (ty::UniqueImmBorrow, ty::MutBorrow) => capture_info_b,
1347 (ty::ImmBorrow, ty::ImmBorrow)
1348 | (ty::UniqueImmBorrow, ty::UniqueImmBorrow)
1349 | (ty::MutBorrow, ty::MutBorrow) => {
1350 bug!("Expected unequal capture kinds");
1358 /// Determines the Ancestry relationship of Place A relative to Place B
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;
1372 // Assume of length of projections_a = n
1373 let projections_a = &place_a.projections;
1375 // Assume of length of projections_b = m
1376 let projections_b = &place_b.projections;
1378 let mut same_initial_projections = true;
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;
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
1393 PlaceAncestryRelation::Descendant
1396 PlaceAncestryRelation::Divergent