1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! ### Inferring borrow kinds for upvars
13 //! Whenever there is a closure expression, we need to determine how each
14 //! upvar is used. We do this by initially assigning each upvar an
15 //! immutable "borrow kind" (see `ty::BorrowKind` for details) and then
16 //! "escalating" the kind as needed. The borrow kind proceeds according to
17 //! the following lattice:
19 //! ty::ImmBorrow -> ty::UniqueImmBorrow -> ty::MutBorrow
21 //! So, for example, if we see an assignment `x = 5` to an upvar `x`, we
22 //! will promote its borrow kind to mutable borrow. If we see an `&mut x`
23 //! we'll do the same. Naturally, this applies not just to the upvar, but
24 //! to everything owned by `x`, so the result is the same for something
25 //! like `x.f = 5` and so on (presuming `x` is not a borrowed pointer to a
26 //! struct). These adjustments are performed in
27 //! `adjust_upvar_borrow_kind()` (you can trace backwards through the code
30 //! The fact that we are inferring borrow kinds as we go results in a
31 //! semi-hacky interaction with mem-categorization. In particular,
32 //! mem-categorization will query the current borrow kind as it
33 //! categorizes, and we'll return the *current* value, but this may get
34 //! adjusted later. Therefore, in this module, we generally ignore the
35 //! borrow kind (and derived mutabilities) that are returned from
36 //! mem-categorization, since they may be inaccurate. (Another option
37 //! would be to use a unification scheme, where instead of returning a
38 //! concrete borrow kind like `ty::ImmBorrow`, we return a
39 //! `ty::InferBorrow(upvar_id)` or something like that, but this would
40 //! then mean that all later passes would have to check for these figments
41 //! and report an error, and it just seems like more mess in the end.)
45 use middle::expr_use_visitor as euv;
46 use middle::mem_categorization as mc;
47 use middle::mem_categorization::Categorization;
48 use rustc::ty::{self, Ty};
49 use rustc::infer::UpvarRegion;
53 use rustc::hir::def_id::DefIndex;
54 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
55 use rustc::util::nodemap::FxHashMap;
57 use std::collections::hash_map::Entry;
59 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
60 pub fn closure_analyze(&self, body: &'gcx hir::Body) {
61 InferBorrowKindVisitor { fcx: self }.visit_body(body);
63 // it's our job to process these.
64 assert!(self.deferred_call_resolutions.borrow().is_empty());
68 struct InferBorrowKindVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
69 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
72 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for InferBorrowKindVisitor<'a, 'gcx, 'tcx> {
73 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
74 NestedVisitorMap::None
77 fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
79 hir::ExprClosure(cc, _, body_id, _, is_generator) => {
80 let body = self.fcx.tcx.hir.body(body_id);
81 self.visit_body(body);
82 self.fcx.analyze_closure((expr.id, expr.hir_id),
92 intravisit::walk_expr(self, expr);
96 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
97 fn analyze_closure(&self,
98 (closure_node_id, closure_hir_id): (ast::NodeId, hir::HirId),
101 capture_clause: hir::CaptureClause,
104 * Analysis starting point.
107 debug!("analyze_closure(id={:?}, body.id={:?})", closure_node_id, body.id());
109 let infer_kind = if gen {
115 .entry(closure_hir_id) {
116 Entry::Occupied(_) => false,
117 Entry::Vacant(entry) => {
118 debug!("check_closure: adding closure {:?} as Fn", closure_node_id);
119 entry.insert((ty::ClosureKind::Fn, None));
125 let closure_def_id = self.tcx.hir.local_def_id(closure_node_id);
127 self.tcx.with_freevars(closure_node_id, |freevars| {
128 for freevar in freevars {
129 let var_def_id = freevar.def.def_id();
130 let upvar_id = ty::UpvarId {
131 var_id: var_def_id.index,
132 closure_expr_id: closure_def_id.index,
134 debug!("seed upvar_id {:?}", upvar_id);
136 let capture_kind = match capture_clause {
137 hir::CaptureByValue => {
138 ty::UpvarCapture::ByValue
140 hir::CaptureByRef => {
141 let origin = UpvarRegion(upvar_id, span);
142 let freevar_region = self.next_region_var(origin);
143 let upvar_borrow = ty::UpvarBorrow { kind: ty::ImmBorrow,
144 region: freevar_region };
145 ty::UpvarCapture::ByRef(upvar_borrow)
149 self.tables.borrow_mut().upvar_capture_map.insert(upvar_id, capture_kind);
154 let body_owner_def_id = self.tcx.hir.body_owner_def_id(body.id());
155 let region_scope_tree = &self.tcx.region_scope_tree(body_owner_def_id);
156 let mut delegate = InferBorrowKind {
158 adjust_closure_kinds: FxHashMap(),
159 adjust_upvar_captures: ty::UpvarCaptureMap::default(),
161 euv::ExprUseVisitor::with_infer(&mut delegate,
165 &self.tables.borrow())
168 // Write the adjusted values back into the main tables.
170 if let Some(kind) = delegate.adjust_closure_kinds
171 .remove(&closure_def_id.index) {
175 .insert(closure_hir_id, kind);
178 self.tables.borrow_mut().upvar_capture_map.extend(
179 delegate.adjust_upvar_captures);
182 // Now that we've analyzed the closure, we know how each
183 // variable is borrowed, and we know what traits the closure
184 // implements (Fn vs FnMut etc). We now have some updates to do
185 // with that information.
187 // Note that no closure type C may have an upvar of type C
188 // (though it may reference itself via a trait object). This
189 // results from the desugaring of closures to a struct like
190 // `Foo<..., UV0...UVn>`. If one of those upvars referenced
191 // C, then the type would have infinite size (and the
192 // inference algorithm will reject it).
194 // Extract the type variables UV0...UVn.
195 let (def_id, closure_substs) = match self.node_ty(closure_hir_id).sty {
196 ty::TyClosure(def_id, substs) |
197 ty::TyGenerator(def_id, substs, _) => (def_id, substs),
201 "type of closure expr {:?} is not a closure {:?}",
206 // Equate the type variables with the actual types.
207 let final_upvar_tys = self.final_upvar_tys(closure_node_id);
208 debug!("analyze_closure: id={:?} closure_substs={:?} final_upvar_tys={:?}",
209 closure_node_id, closure_substs, final_upvar_tys);
210 for (upvar_ty, final_upvar_ty) in
211 closure_substs.upvar_tys(def_id, self.tcx).zip(final_upvar_tys)
213 self.demand_eqtype(span, final_upvar_ty, upvar_ty);
216 // If we are also inferred the closure kind here,
217 // process any deferred resolutions.
219 let deferred_call_resolutions =
220 self.remove_deferred_call_resolutions(closure_def_id);
221 for deferred_call_resolution in deferred_call_resolutions {
222 deferred_call_resolution.resolve(self);
227 // Returns a list of `ClosureUpvar`s for each upvar.
228 fn final_upvar_tys(&self, closure_id: ast::NodeId) -> Vec<Ty<'tcx>> {
229 // Presently an unboxed closure type cannot "escape" out of a
230 // function, so we will only encounter ones that originated in the
231 // local crate or were inlined into it along with some function.
232 // This may change if abstract return types of some sort are
235 let closure_def_index = tcx.hir.local_def_id(closure_id).index;
237 tcx.with_freevars(closure_id, |freevars| {
238 freevars.iter().map(|freevar| {
239 let var_def_id = freevar.def.def_id();
240 let var_node_id = tcx.hir.as_local_node_id(var_def_id).unwrap();
241 let freevar_ty = self.node_ty(tcx.hir.node_to_hir_id(var_node_id));
242 let upvar_id = ty::UpvarId {
243 var_id: var_def_id.index,
244 closure_expr_id: closure_def_index,
246 let capture = self.tables.borrow().upvar_capture(upvar_id);
248 debug!("var_id={:?} freevar_ty={:?} capture={:?}",
249 var_node_id, freevar_ty, capture);
252 ty::UpvarCapture::ByValue => freevar_ty,
253 ty::UpvarCapture::ByRef(borrow) =>
254 tcx.mk_ref(borrow.region,
257 mutbl: borrow.kind.to_mutbl_lossy(),
265 struct InferBorrowKind<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
266 fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
267 adjust_closure_kinds: FxHashMap<DefIndex, (ty::ClosureKind, Option<(Span, ast::Name)>)>,
268 adjust_upvar_captures: ty::UpvarCaptureMap<'tcx>,
271 impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
272 fn adjust_upvar_borrow_kind_for_consume(&mut self,
274 mode: euv::ConsumeMode)
276 debug!("adjust_upvar_borrow_kind_for_consume(cmt={:?}, mode={:?})",
279 // we only care about moves
281 euv::Copy => { return; }
285 let tcx = self.fcx.tcx;
287 // watch out for a move of the deref of a borrowed pointer;
288 // for that to be legal, the upvar would have to be borrowed
290 let guarantor = cmt.guarantor();
291 debug!("adjust_upvar_borrow_kind_for_consume: guarantor={:?}",
293 match guarantor.cat {
294 Categorization::Deref(_, mc::BorrowedPtr(..)) |
295 Categorization::Deref(_, mc::Implicit(..)) => {
297 mc::NoteUpvarRef(upvar_id) => {
298 debug!("adjust_upvar_borrow_kind_for_consume: \
299 setting upvar_id={:?} to by value",
302 // to move out of an upvar, this must be a FnOnce closure
303 self.adjust_closure_kind(upvar_id.closure_expr_id,
304 ty::ClosureKind::FnOnce,
306 var_name(tcx, upvar_id.var_id));
308 self.adjust_upvar_captures.insert(upvar_id, ty::UpvarCapture::ByValue);
310 mc::NoteClosureEnv(upvar_id) => {
311 // we get just a closureenv ref if this is a
312 // `move` closure, or if the upvar has already
313 // been inferred to by-value. In any case, we
314 // must still adjust the kind of the closure
315 // to be a FnOnce closure to permit moves out
316 // of the environment.
317 self.adjust_closure_kind(upvar_id.closure_expr_id,
318 ty::ClosureKind::FnOnce,
320 var_name(tcx, upvar_id.var_id));
330 /// Indicates that `cmt` is being directly mutated (e.g., assigned
331 /// to). If cmt contains any by-ref upvars, this implies that
332 /// those upvars must be borrowed using an `&mut` borrow.
333 fn adjust_upvar_borrow_kind_for_mut(&mut self, cmt: mc::cmt<'tcx>) {
334 debug!("adjust_upvar_borrow_kind_for_mut(cmt={:?})",
337 match cmt.cat.clone() {
338 Categorization::Deref(base, mc::Unique) |
339 Categorization::Interior(base, _) |
340 Categorization::Downcast(base, _) => {
341 // Interior or owned data is mutable if base is
342 // mutable, so iterate to the base.
343 self.adjust_upvar_borrow_kind_for_mut(base);
346 Categorization::Deref(base, mc::BorrowedPtr(..)) |
347 Categorization::Deref(base, mc::Implicit(..)) => {
348 if !self.try_adjust_upvar_deref(cmt, ty::MutBorrow) {
349 // assignment to deref of an `&mut`
350 // borrowed pointer implies that the
351 // pointer itself must be unique, but not
352 // necessarily *mutable*
353 self.adjust_upvar_borrow_kind_for_unique(base);
357 Categorization::Deref(_, mc::UnsafePtr(..)) |
358 Categorization::StaticItem |
359 Categorization::Rvalue(..) |
360 Categorization::Local(_) |
361 Categorization::Upvar(..) => {
367 fn adjust_upvar_borrow_kind_for_unique(&mut self, cmt: mc::cmt<'tcx>) {
368 debug!("adjust_upvar_borrow_kind_for_unique(cmt={:?})",
371 match cmt.cat.clone() {
372 Categorization::Deref(base, mc::Unique) |
373 Categorization::Interior(base, _) |
374 Categorization::Downcast(base, _) => {
375 // Interior or owned data is unique if base is
377 self.adjust_upvar_borrow_kind_for_unique(base);
380 Categorization::Deref(base, mc::BorrowedPtr(..)) |
381 Categorization::Deref(base, mc::Implicit(..)) => {
382 if !self.try_adjust_upvar_deref(cmt, ty::UniqueImmBorrow) {
383 // for a borrowed pointer to be unique, its
384 // base must be unique
385 self.adjust_upvar_borrow_kind_for_unique(base);
389 Categorization::Deref(_, mc::UnsafePtr(..)) |
390 Categorization::StaticItem |
391 Categorization::Rvalue(..) |
392 Categorization::Local(_) |
393 Categorization::Upvar(..) => {
398 fn try_adjust_upvar_deref(&mut self,
400 borrow_kind: ty::BorrowKind)
403 assert!(match borrow_kind {
404 ty::MutBorrow => true,
405 ty::UniqueImmBorrow => true,
407 // imm borrows never require adjusting any kinds, so we don't wind up here
408 ty::ImmBorrow => false,
411 let tcx = self.fcx.tcx;
414 mc::NoteUpvarRef(upvar_id) => {
415 // if this is an implicit deref of an
416 // upvar, then we need to modify the
417 // borrow_kind of the upvar to make sure it
418 // is inferred to mutable if necessary
419 self.adjust_upvar_borrow_kind(upvar_id, borrow_kind);
421 // also need to be in an FnMut closure since this is not an ImmBorrow
422 self.adjust_closure_kind(upvar_id.closure_expr_id,
423 ty::ClosureKind::FnMut,
425 var_name(tcx, upvar_id.var_id));
429 mc::NoteClosureEnv(upvar_id) => {
430 // this kind of deref occurs in a `move` closure, or
431 // for a by-value upvar; in either case, to mutate an
432 // upvar, we need to be an FnMut closure
433 self.adjust_closure_kind(upvar_id.closure_expr_id,
434 ty::ClosureKind::FnMut,
436 var_name(tcx, upvar_id.var_id));
446 /// We infer the borrow_kind with which to borrow upvars in a stack closure.
447 /// The borrow_kind basically follows a lattice of `imm < unique-imm < mut`,
448 /// moving from left to right as needed (but never right to left).
449 /// Here the argument `mutbl` is the borrow_kind that is required by
450 /// some particular use.
451 fn adjust_upvar_borrow_kind(&mut self,
452 upvar_id: ty::UpvarId,
453 kind: ty::BorrowKind) {
454 let upvar_capture = self.adjust_upvar_captures.get(&upvar_id).cloned()
455 .unwrap_or_else(|| self.fcx.tables.borrow().upvar_capture(upvar_id));
456 debug!("adjust_upvar_borrow_kind(upvar_id={:?}, upvar_capture={:?}, kind={:?})",
457 upvar_id, upvar_capture, kind);
459 match upvar_capture {
460 ty::UpvarCapture::ByValue => {
461 // Upvar is already by-value, the strongest criteria.
463 ty::UpvarCapture::ByRef(mut upvar_borrow) => {
464 match (upvar_borrow.kind, kind) {
466 (ty::ImmBorrow, ty::UniqueImmBorrow) |
467 (ty::ImmBorrow, ty::MutBorrow) |
468 (ty::UniqueImmBorrow, ty::MutBorrow) => {
469 upvar_borrow.kind = kind;
470 self.adjust_upvar_captures.insert(upvar_id,
471 ty::UpvarCapture::ByRef(upvar_borrow));
474 (ty::ImmBorrow, ty::ImmBorrow) |
475 (ty::UniqueImmBorrow, ty::ImmBorrow) |
476 (ty::UniqueImmBorrow, ty::UniqueImmBorrow) |
477 (ty::MutBorrow, _) => {
484 fn adjust_closure_kind(&mut self,
485 closure_id: DefIndex,
486 new_kind: ty::ClosureKind,
488 var_name: ast::Name) {
489 debug!("adjust_closure_kind(closure_id={:?}, new_kind={:?}, upvar_span={:?}, var_name={})",
490 closure_id, new_kind, upvar_span, var_name);
492 let closure_kind = self.adjust_closure_kinds.get(&closure_id).cloned()
494 let closure_id = self.fcx.tcx.hir.def_index_to_hir_id(closure_id);
495 self.fcx.tables.borrow().closure_kinds().get(closure_id).cloned()
498 if let Some((existing_kind, _)) = closure_kind {
499 debug!("adjust_closure_kind: closure_id={:?}, existing_kind={:?}, new_kind={:?}",
500 closure_id, existing_kind, new_kind);
502 match (existing_kind, new_kind) {
503 (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
504 (ty::ClosureKind::FnMut, ty::ClosureKind::Fn) |
505 (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
506 (ty::ClosureKind::FnOnce, _) => {
510 (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) |
511 (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
512 (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
513 // new kind is stronger than the old kind
514 self.adjust_closure_kinds.insert(
516 (new_kind, Some((upvar_span, var_name)))
524 impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'gcx, 'tcx> {
525 fn consume(&mut self,
526 _consume_id: ast::NodeId,
529 mode: euv::ConsumeMode)
531 debug!("consume(cmt={:?},mode={:?})", cmt, mode);
532 self.adjust_upvar_borrow_kind_for_consume(cmt, mode);
535 fn matched_pat(&mut self,
536 _matched_pat: &hir::Pat,
538 _mode: euv::MatchMode)
541 fn consume_pat(&mut self,
542 _consume_pat: &hir::Pat,
544 mode: euv::ConsumeMode)
546 debug!("consume_pat(cmt={:?},mode={:?})", cmt, mode);
547 self.adjust_upvar_borrow_kind_for_consume(cmt, mode);
551 borrow_id: ast::NodeId,
554 _loan_region: ty::Region<'tcx>,
556 _loan_cause: euv::LoanCause)
558 debug!("borrow(borrow_id={}, cmt={:?}, bk={:?})",
563 ty::UniqueImmBorrow => {
564 self.adjust_upvar_borrow_kind_for_unique(cmt);
567 self.adjust_upvar_borrow_kind_for_mut(cmt);
572 fn decl_without_init(&mut self,
578 _assignment_id: ast::NodeId,
579 _assignment_span: Span,
580 assignee_cmt: mc::cmt<'tcx>,
581 _mode: euv::MutateMode)
583 debug!("mutate(assignee_cmt={:?})",
586 self.adjust_upvar_borrow_kind_for_mut(assignee_cmt);
590 fn var_name(tcx: ty::TyCtxt, var_def_index: DefIndex) -> ast::Name {
591 let var_node_id = tcx.hir.def_index_to_node_id(var_def_index);
592 tcx.hir.name(var_node_id)