]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/upvar.rs
Move hir::Place to librustc_middle/hir
[rust.git] / src / librustc_typeck / 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::FnCtxt;
34
35 use crate::expr_use_visitor as euv;
36 use rustc_data_structures::fx::FxIndexMap;
37 use rustc_hir as hir;
38 use rustc_hir::def_id::DefId;
39 use rustc_hir::def_id::LocalDefId;
40 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
41 use rustc_infer::infer::UpvarRegion;
42 use rustc_middle::hir::place::{PlaceBase, PlaceWithHirId};
43 use rustc_middle::ty::{self, Ty, TyCtxt, UpvarSubsts};
44 use rustc_span::{Span, Symbol};
45
46 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
47     pub fn closure_analyze(&self, body: &'tcx hir::Body<'tcx>) {
48         InferBorrowKindVisitor { fcx: self }.visit_body(body);
49
50         // it's our job to process these.
51         assert!(self.deferred_call_resolutions.borrow().is_empty());
52     }
53 }
54
55 struct InferBorrowKindVisitor<'a, 'tcx> {
56     fcx: &'a FnCtxt<'a, 'tcx>,
57 }
58
59 impl<'a, 'tcx> Visitor<'tcx> for InferBorrowKindVisitor<'a, 'tcx> {
60     type Map = intravisit::ErasedMap<'tcx>;
61
62     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
63         NestedVisitorMap::None
64     }
65
66     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
67         if let hir::ExprKind::Closure(cc, _, body_id, _, _) = expr.kind {
68             let body = self.fcx.tcx.hir().body(body_id);
69             self.visit_body(body);
70             self.fcx.analyze_closure(expr.hir_id, expr.span, body, cc);
71         }
72
73         intravisit::walk_expr(self, expr);
74     }
75 }
76
77 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
78     /// Analysis starting point.
79     fn analyze_closure(
80         &self,
81         closure_hir_id: hir::HirId,
82         span: Span,
83         body: &hir::Body<'_>,
84         capture_clause: hir::CaptureBy,
85     ) {
86         debug!("analyze_closure(id={:?}, body.id={:?})", closure_hir_id, body.id());
87
88         // Extract the type of the closure.
89         let ty = self.node_ty(closure_hir_id);
90         let (closure_def_id, substs) = match ty.kind {
91             ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs)),
92             ty::Generator(def_id, substs, _) => (def_id, UpvarSubsts::Generator(substs)),
93             ty::Error(_) => {
94                 // #51714: skip analysis when we have already encountered type errors
95                 return;
96             }
97             _ => {
98                 span_bug!(
99                     span,
100                     "type of closure expr {:?} is not a closure {:?}",
101                     closure_hir_id,
102                     ty
103                 );
104             }
105         };
106
107         let infer_kind = if let UpvarSubsts::Closure(closure_substs) = substs {
108             self.closure_kind(closure_substs).is_none().then_some(closure_substs)
109         } else {
110             None
111         };
112
113         if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
114             let mut closure_captures: FxIndexMap<hir::HirId, ty::UpvarId> =
115                 FxIndexMap::with_capacity_and_hasher(upvars.len(), Default::default());
116             for (&var_hir_id, _) in upvars.iter() {
117                 let upvar_id = ty::UpvarId {
118                     var_path: ty::UpvarPath { hir_id: var_hir_id },
119                     closure_expr_id: closure_def_id.expect_local(),
120                 };
121                 debug!("seed upvar_id {:?}", upvar_id);
122                 // Adding the upvar Id to the list of Upvars, which will be added
123                 // to the map for the closure at the end of the for loop.
124                 closure_captures.insert(var_hir_id, upvar_id);
125
126                 let capture_kind = match capture_clause {
127                     hir::CaptureBy::Value => ty::UpvarCapture::ByValue,
128                     hir::CaptureBy::Ref => {
129                         let origin = UpvarRegion(upvar_id, span);
130                         let upvar_region = self.next_region_var(origin);
131                         let upvar_borrow =
132                             ty::UpvarBorrow { kind: ty::ImmBorrow, region: upvar_region };
133                         ty::UpvarCapture::ByRef(upvar_borrow)
134                     }
135                 };
136
137                 self.tables.borrow_mut().upvar_capture_map.insert(upvar_id, capture_kind);
138             }
139             // Add the vector of upvars to the map keyed with the closure id.
140             // This gives us an easier access to them without having to call
141             // tcx.upvars again..
142             if !closure_captures.is_empty() {
143                 self.tables.borrow_mut().closure_captures.insert(closure_def_id, closure_captures);
144             }
145         }
146
147         let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
148         assert_eq!(body_owner_def_id.to_def_id(), closure_def_id);
149         let mut delegate = InferBorrowKind {
150             fcx: self,
151             closure_def_id,
152             current_closure_kind: ty::ClosureKind::LATTICE_BOTTOM,
153             current_origin: None,
154             adjust_upvar_captures: ty::UpvarCaptureMap::default(),
155         };
156         euv::ExprUseVisitor::new(
157             &mut delegate,
158             &self.infcx,
159             body_owner_def_id,
160             self.param_env,
161             &self.tables.borrow(),
162         )
163         .consume_body(body);
164
165         if let Some(closure_substs) = infer_kind {
166             // Unify the (as yet unbound) type variable in the closure
167             // substs with the kind we inferred.
168             let inferred_kind = delegate.current_closure_kind;
169             let closure_kind_ty = closure_substs.as_closure().kind_ty();
170             self.demand_eqtype(span, inferred_kind.to_ty(self.tcx), closure_kind_ty);
171
172             // If we have an origin, store it.
173             if let Some(origin) = delegate.current_origin {
174                 self.tables.borrow_mut().closure_kind_origins_mut().insert(closure_hir_id, origin);
175             }
176         }
177
178         self.tables.borrow_mut().upvar_capture_map.extend(delegate.adjust_upvar_captures);
179
180         // Now that we've analyzed the closure, we know how each
181         // variable is borrowed, and we know what traits the closure
182         // implements (Fn vs FnMut etc). We now have some updates to do
183         // with that information.
184         //
185         // Note that no closure type C may have an upvar of type C
186         // (though it may reference itself via a trait object). This
187         // results from the desugaring of closures to a struct like
188         // `Foo<..., UV0...UVn>`. If one of those upvars referenced
189         // C, then the type would have infinite size (and the
190         // inference algorithm will reject it).
191
192         // Equate the type variables for the upvars with the actual types.
193         let final_upvar_tys = self.final_upvar_tys(closure_hir_id);
194         debug!(
195             "analyze_closure: id={:?} substs={:?} final_upvar_tys={:?}",
196             closure_hir_id, substs, final_upvar_tys
197         );
198         for (upvar_ty, final_upvar_ty) in substs.upvar_tys().zip(final_upvar_tys) {
199             self.demand_suptype(span, upvar_ty, final_upvar_ty);
200         }
201
202         // If we are also inferred the closure kind here,
203         // process any deferred resolutions.
204         let deferred_call_resolutions = self.remove_deferred_call_resolutions(closure_def_id);
205         for deferred_call_resolution in deferred_call_resolutions {
206             deferred_call_resolution.resolve(self);
207         }
208     }
209
210     // Returns a list of `Ty`s for each upvar.
211     fn final_upvar_tys(&self, closure_id: hir::HirId) -> Vec<Ty<'tcx>> {
212         // Presently an unboxed closure type cannot "escape" out of a
213         // function, so we will only encounter ones that originated in the
214         // local crate or were inlined into it along with some function.
215         // This may change if abstract return types of some sort are
216         // implemented.
217         let tcx = self.tcx;
218         let closure_def_id = tcx.hir().local_def_id(closure_id);
219
220         tcx.upvars_mentioned(closure_def_id)
221             .iter()
222             .flat_map(|upvars| {
223                 upvars.iter().map(|(&var_hir_id, _)| {
224                     let upvar_ty = self.node_ty(var_hir_id);
225                     let upvar_id = ty::UpvarId {
226                         var_path: ty::UpvarPath { hir_id: var_hir_id },
227                         closure_expr_id: closure_def_id,
228                     };
229                     let capture = self.tables.borrow().upvar_capture(upvar_id);
230
231                     debug!("var_id={:?} upvar_ty={:?} capture={:?}", var_hir_id, upvar_ty, capture);
232
233                     match capture {
234                         ty::UpvarCapture::ByValue => upvar_ty,
235                         ty::UpvarCapture::ByRef(borrow) => tcx.mk_ref(
236                             borrow.region,
237                             ty::TypeAndMut { ty: upvar_ty, mutbl: borrow.kind.to_mutbl_lossy() },
238                         ),
239                     }
240                 })
241             })
242             .collect()
243     }
244 }
245
246 struct InferBorrowKind<'a, 'tcx> {
247     fcx: &'a FnCtxt<'a, 'tcx>,
248
249     // The def-id of the closure whose kind and upvar accesses are being inferred.
250     closure_def_id: DefId,
251
252     // The kind that we have inferred that the current closure
253     // requires. Note that we *always* infer a minimal kind, even if
254     // we don't always *use* that in the final result (i.e., sometimes
255     // we've taken the closure kind from the expectations instead, and
256     // for generators we don't even implement the closure traits
257     // really).
258     current_closure_kind: ty::ClosureKind,
259
260     // If we modified `current_closure_kind`, this field contains a `Some()` with the
261     // variable access that caused us to do so.
262     current_origin: Option<(Span, Symbol)>,
263
264     // For each upvar that we access, we track the minimal kind of
265     // access we need (ref, ref mut, move, etc).
266     adjust_upvar_captures: ty::UpvarCaptureMap<'tcx>,
267 }
268
269 impl<'a, 'tcx> InferBorrowKind<'a, 'tcx> {
270     fn adjust_upvar_borrow_kind_for_consume(
271         &mut self,
272         place_with_id: &PlaceWithHirId<'tcx>,
273         mode: euv::ConsumeMode,
274     ) {
275         debug!(
276             "adjust_upvar_borrow_kind_for_consume(place_with_id={:?}, mode={:?})",
277             place_with_id, mode
278         );
279
280         // we only care about moves
281         match mode {
282             euv::Copy => {
283                 return;
284             }
285             euv::Move => {}
286         }
287
288         let tcx = self.fcx.tcx;
289         let upvar_id = if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
290             upvar_id
291         } else {
292             return;
293         };
294
295         debug!("adjust_upvar_borrow_kind_for_consume: upvar={:?}", upvar_id);
296
297         // To move out of an upvar, this must be a FnOnce closure
298         self.adjust_closure_kind(
299             upvar_id.closure_expr_id,
300             ty::ClosureKind::FnOnce,
301             tcx.hir().span(place_with_id.hir_id),
302             var_name(tcx, upvar_id.var_path.hir_id),
303         );
304
305         self.adjust_upvar_captures.insert(upvar_id, ty::UpvarCapture::ByValue);
306     }
307
308     /// Indicates that `place_with_id` is being directly mutated (e.g., assigned
309     /// to). If the place is based on a by-ref upvar, this implies that
310     /// the upvar must be borrowed using an `&mut` borrow.
311     fn adjust_upvar_borrow_kind_for_mut(&mut self, place_with_id: &PlaceWithHirId<'tcx>) {
312         debug!("adjust_upvar_borrow_kind_for_mut(place_with_id={:?})", place_with_id);
313
314         if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
315             let mut borrow_kind = ty::MutBorrow;
316             for pointer_ty in place_with_id.place.deref_tys() {
317                 match pointer_ty.kind {
318                     // Raw pointers don't inherit mutability.
319                     ty::RawPtr(_) => return,
320                     // assignment to deref of an `&mut`
321                     // borrowed pointer implies that the
322                     // pointer itself must be unique, but not
323                     // necessarily *mutable*
324                     ty::Ref(.., hir::Mutability::Mut) => borrow_kind = ty::UniqueImmBorrow,
325                     _ => (),
326                 }
327             }
328             self.adjust_upvar_deref(
329                 upvar_id,
330                 self.fcx.tcx.hir().span(place_with_id.hir_id),
331                 borrow_kind,
332             );
333         }
334     }
335
336     fn adjust_upvar_borrow_kind_for_unique(&mut self, place_with_id: &PlaceWithHirId<'tcx>) {
337         debug!("adjust_upvar_borrow_kind_for_unique(place_with_id={:?})", place_with_id);
338
339         if let PlaceBase::Upvar(upvar_id) = place_with_id.place.base {
340             if place_with_id.place.deref_tys().any(ty::TyS::is_unsafe_ptr) {
341                 // Raw pointers don't inherit mutability.
342                 return;
343             }
344             // for a borrowed pointer to be unique, its base must be unique
345             self.adjust_upvar_deref(
346                 upvar_id,
347                 self.fcx.tcx.hir().span(place_with_id.hir_id),
348                 ty::UniqueImmBorrow,
349             );
350         }
351     }
352
353     fn adjust_upvar_deref(
354         &mut self,
355         upvar_id: ty::UpvarId,
356         place_span: Span,
357         borrow_kind: ty::BorrowKind,
358     ) {
359         assert!(match borrow_kind {
360             ty::MutBorrow => true,
361             ty::UniqueImmBorrow => true,
362
363             // imm borrows never require adjusting any kinds, so we don't wind up here
364             ty::ImmBorrow => false,
365         });
366
367         let tcx = self.fcx.tcx;
368
369         // if this is an implicit deref of an
370         // upvar, then we need to modify the
371         // borrow_kind of the upvar to make sure it
372         // is inferred to mutable if necessary
373         self.adjust_upvar_borrow_kind(upvar_id, borrow_kind);
374
375         // also need to be in an FnMut closure since this is not an ImmBorrow
376         self.adjust_closure_kind(
377             upvar_id.closure_expr_id,
378             ty::ClosureKind::FnMut,
379             place_span,
380             var_name(tcx, upvar_id.var_path.hir_id),
381         );
382     }
383
384     /// We infer the borrow_kind with which to borrow upvars in a stack closure.
385     /// The borrow_kind basically follows a lattice of `imm < unique-imm < mut`,
386     /// moving from left to right as needed (but never right to left).
387     /// Here the argument `mutbl` is the borrow_kind that is required by
388     /// some particular use.
389     fn adjust_upvar_borrow_kind(&mut self, upvar_id: ty::UpvarId, kind: ty::BorrowKind) {
390         let upvar_capture = self
391             .adjust_upvar_captures
392             .get(&upvar_id)
393             .copied()
394             .unwrap_or_else(|| self.fcx.tables.borrow().upvar_capture(upvar_id));
395         debug!(
396             "adjust_upvar_borrow_kind(upvar_id={:?}, upvar_capture={:?}, kind={:?})",
397             upvar_id, upvar_capture, kind
398         );
399
400         match upvar_capture {
401             ty::UpvarCapture::ByValue => {
402                 // Upvar is already by-value, the strongest criteria.
403             }
404             ty::UpvarCapture::ByRef(mut upvar_borrow) => {
405                 match (upvar_borrow.kind, kind) {
406                     // Take RHS:
407                     (ty::ImmBorrow, ty::UniqueImmBorrow | ty::MutBorrow)
408                     | (ty::UniqueImmBorrow, ty::MutBorrow) => {
409                         upvar_borrow.kind = kind;
410                         self.adjust_upvar_captures
411                             .insert(upvar_id, ty::UpvarCapture::ByRef(upvar_borrow));
412                     }
413                     // Take LHS:
414                     (ty::ImmBorrow, ty::ImmBorrow)
415                     | (ty::UniqueImmBorrow, ty::ImmBorrow | ty::UniqueImmBorrow)
416                     | (ty::MutBorrow, _) => {}
417                 }
418             }
419         }
420     }
421
422     fn adjust_closure_kind(
423         &mut self,
424         closure_id: LocalDefId,
425         new_kind: ty::ClosureKind,
426         upvar_span: Span,
427         var_name: Symbol,
428     ) {
429         debug!(
430             "adjust_closure_kind(closure_id={:?}, new_kind={:?}, upvar_span={:?}, var_name={})",
431             closure_id, new_kind, upvar_span, var_name
432         );
433
434         // Is this the closure whose kind is currently being inferred?
435         if closure_id.to_def_id() != self.closure_def_id {
436             debug!("adjust_closure_kind: not current closure");
437             return;
438         }
439
440         // closures start out as `Fn`.
441         let existing_kind = self.current_closure_kind;
442
443         debug!(
444             "adjust_closure_kind: closure_id={:?}, existing_kind={:?}, new_kind={:?}",
445             closure_id, existing_kind, new_kind
446         );
447
448         match (existing_kind, new_kind) {
449             (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
450             | (ty::ClosureKind::FnMut, ty::ClosureKind::Fn | ty::ClosureKind::FnMut)
451             | (ty::ClosureKind::FnOnce, _) => {
452                 // no change needed
453             }
454
455             (ty::ClosureKind::Fn, ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce)
456             | (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
457                 // new kind is stronger than the old kind
458                 self.current_closure_kind = new_kind;
459                 self.current_origin = Some((upvar_span, var_name));
460             }
461         }
462     }
463 }
464
465 impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
466     fn consume(&mut self, place_with_id: &PlaceWithHirId<'tcx>, mode: euv::ConsumeMode) {
467         debug!("consume(place_with_id={:?},mode={:?})", place_with_id, mode);
468         self.adjust_upvar_borrow_kind_for_consume(place_with_id, mode);
469     }
470
471     fn borrow(&mut self, place_with_id: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
472         debug!("borrow(place_with_id={:?}, bk={:?})", place_with_id, bk);
473
474         match bk {
475             ty::ImmBorrow => {}
476             ty::UniqueImmBorrow => {
477                 self.adjust_upvar_borrow_kind_for_unique(place_with_id);
478             }
479             ty::MutBorrow => {
480                 self.adjust_upvar_borrow_kind_for_mut(place_with_id);
481             }
482         }
483     }
484
485     fn mutate(&mut self, assignee_place: &PlaceWithHirId<'tcx>) {
486         debug!("mutate(assignee_place={:?})", assignee_place);
487
488         self.adjust_upvar_borrow_kind_for_mut(assignee_place);
489     }
490 }
491
492 fn var_name(tcx: TyCtxt<'_>, var_hir_id: hir::HirId) -> Symbol {
493     tcx.hir().name(var_hir_id)
494 }