]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/upvar.rs
82d1210f42b2ecf5711c2e052ad39aee8a4a3d9e
[rust.git] / src / librustc_typeck / check / upvar.rs
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.
4 //
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.
10
11 //! ### Inferring borrow kinds for upvars
12 //!
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:
18 //!
19 //!     ty::ImmBorrow -> ty::UniqueImmBorrow -> ty::MutBorrow
20 //!
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
28 //! from there).
29 //!
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.)
42
43 use super::FnCtxt;
44
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;
50 use syntax::ast;
51 use syntax_pos::Span;
52 use rustc::hir;
53 use rustc::hir::def_id::DefIndex;
54 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
55 use rustc::util::nodemap::FxHashMap;
56
57 use std::collections::hash_map::Entry;
58
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);
62
63         // it's our job to process these.
64         assert!(self.deferred_call_resolutions.borrow().is_empty());
65     }
66 }
67
68 struct InferBorrowKindVisitor<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
69     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
70 }
71
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
75     }
76
77     fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
78         match expr.node {
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),
83                                          expr.span,
84                                          body,
85                                          cc,
86                                          is_generator);
87             }
88
89             _ => { }
90         }
91
92         intravisit::walk_expr(self, expr);
93     }
94 }
95
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),
99                        span: Span,
100                        body: &hir::Body,
101                        capture_clause: hir::CaptureClause,
102                        gen: bool) {
103         /*!
104          * Analysis starting point.
105          */
106
107         debug!("analyze_closure(id={:?}, body.id={:?})", closure_node_id, body.id());
108
109         let infer_kind = if gen {
110             false
111         } else {
112             match self.tables
113                       .borrow_mut()
114                       .closure_kinds_mut()
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));
120                     true
121                 }
122             }
123         };
124
125         let closure_def_id = self.tcx.hir.local_def_id(closure_node_id);
126
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,
133                 };
134                 debug!("seed upvar_id {:?}", upvar_id);
135
136                 let capture_kind = match capture_clause {
137                     hir::CaptureByValue => {
138                         ty::UpvarCapture::ByValue
139                     }
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)
146                     }
147                 };
148
149                 self.tables.borrow_mut().upvar_capture_map.insert(upvar_id, capture_kind);
150             }
151         });
152
153         {
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 {
157                 fcx: self,
158                 adjust_closure_kinds: FxHashMap(),
159                 adjust_upvar_captures: ty::UpvarCaptureMap::default(),
160             };
161             euv::ExprUseVisitor::with_infer(&mut delegate,
162                                             &self.infcx,
163                                             self.param_env,
164                                             region_scope_tree,
165                                             &self.tables.borrow())
166                 .consume_body(body);
167
168             // Write the adjusted values back into the main tables.
169             if infer_kind {
170                 if let Some(kind) = delegate.adjust_closure_kinds
171                                             .remove(&closure_def_id.index) {
172                     self.tables
173                         .borrow_mut()
174                         .closure_kinds_mut()
175                         .insert(closure_hir_id, kind);
176                 }
177             }
178             self.tables.borrow_mut().upvar_capture_map.extend(
179                 delegate.adjust_upvar_captures);
180         }
181
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.
186         //
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).
193
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),
198             ref t => {
199                 span_bug!(
200                     span,
201                     "type of closure expr {:?} is not a closure {:?}",
202                     closure_node_id, t);
203             }
204         };
205
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)
212         {
213             self.demand_eqtype(span, final_upvar_ty, upvar_ty);
214         }
215
216         // If we are also inferred the closure kind here,
217         // process any deferred resolutions.
218         if infer_kind {
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);
223             }
224         }
225     }
226
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
233         // implemented.
234         let tcx = self.tcx;
235         let closure_def_index = tcx.hir.local_def_id(closure_id).index;
236
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,
245                 };
246                 let capture = self.tables.borrow().upvar_capture(upvar_id);
247
248                 debug!("var_id={:?} freevar_ty={:?} capture={:?}",
249                        var_node_id, freevar_ty, capture);
250
251                 match capture {
252                     ty::UpvarCapture::ByValue => freevar_ty,
253                     ty::UpvarCapture::ByRef(borrow) =>
254                         tcx.mk_ref(borrow.region,
255                                     ty::TypeAndMut {
256                                         ty: freevar_ty,
257                                         mutbl: borrow.kind.to_mutbl_lossy(),
258                                     }),
259                 }
260             }).collect()
261         })
262     }
263 }
264
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>,
269 }
270
271 impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
272     fn adjust_upvar_borrow_kind_for_consume(&mut self,
273                                             cmt: mc::cmt<'tcx>,
274                                             mode: euv::ConsumeMode)
275     {
276         debug!("adjust_upvar_borrow_kind_for_consume(cmt={:?}, mode={:?})",
277                cmt, mode);
278
279         // we only care about moves
280         match mode {
281             euv::Copy => { return; }
282             euv::Move(_) => { }
283         }
284
285         let tcx = self.fcx.tcx;
286
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
289         // by value instead
290         let guarantor = cmt.guarantor();
291         debug!("adjust_upvar_borrow_kind_for_consume: guarantor={:?}",
292                guarantor);
293         match guarantor.cat {
294             Categorization::Deref(_, mc::BorrowedPtr(..)) |
295             Categorization::Deref(_, mc::Implicit(..)) => {
296                 match cmt.note {
297                     mc::NoteUpvarRef(upvar_id) => {
298                         debug!("adjust_upvar_borrow_kind_for_consume: \
299                                 setting upvar_id={:?} to by value",
300                                upvar_id);
301
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,
305                                                  guarantor.span,
306                                                  var_name(tcx, upvar_id.var_id));
307
308                         self.adjust_upvar_captures.insert(upvar_id, ty::UpvarCapture::ByValue);
309                     }
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,
319                                                  guarantor.span,
320                                                  var_name(tcx, upvar_id.var_id));
321                     }
322                     mc::NoteNone => {
323                     }
324                 }
325             }
326             _ => { }
327         }
328     }
329
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={:?})",
335                cmt);
336
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);
344             }
345
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);
354                 }
355             }
356
357             Categorization::Deref(_, mc::UnsafePtr(..)) |
358             Categorization::StaticItem |
359             Categorization::Rvalue(..) |
360             Categorization::Local(_) |
361             Categorization::Upvar(..) => {
362                 return;
363             }
364         }
365     }
366
367     fn adjust_upvar_borrow_kind_for_unique(&mut self, cmt: mc::cmt<'tcx>) {
368         debug!("adjust_upvar_borrow_kind_for_unique(cmt={:?})",
369                cmt);
370
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
376                 // unique.
377                 self.adjust_upvar_borrow_kind_for_unique(base);
378             }
379
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);
386                 }
387             }
388
389             Categorization::Deref(_, mc::UnsafePtr(..)) |
390             Categorization::StaticItem |
391             Categorization::Rvalue(..) |
392             Categorization::Local(_) |
393             Categorization::Upvar(..) => {
394             }
395         }
396     }
397
398     fn try_adjust_upvar_deref(&mut self,
399                               cmt: mc::cmt<'tcx>,
400                               borrow_kind: ty::BorrowKind)
401                               -> bool
402     {
403         assert!(match borrow_kind {
404             ty::MutBorrow => true,
405             ty::UniqueImmBorrow => true,
406
407             // imm borrows never require adjusting any kinds, so we don't wind up here
408             ty::ImmBorrow => false,
409         });
410
411         let tcx = self.fcx.tcx;
412
413         match cmt.note {
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);
420
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,
424                                          cmt.span,
425                                          var_name(tcx, upvar_id.var_id));
426
427                 true
428             }
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,
435                                          cmt.span,
436                                          var_name(tcx, upvar_id.var_id));
437
438                 true
439             }
440             mc::NoteNone => {
441                 false
442             }
443         }
444     }
445
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);
458
459         match upvar_capture {
460             ty::UpvarCapture::ByValue => {
461                 // Upvar is already by-value, the strongest criteria.
462             }
463             ty::UpvarCapture::ByRef(mut upvar_borrow) => {
464                 match (upvar_borrow.kind, kind) {
465                     // Take RHS:
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));
472                     }
473                     // Take LHS:
474                     (ty::ImmBorrow, ty::ImmBorrow) |
475                     (ty::UniqueImmBorrow, ty::ImmBorrow) |
476                     (ty::UniqueImmBorrow, ty::UniqueImmBorrow) |
477                     (ty::MutBorrow, _) => {
478                     }
479                 }
480             }
481         }
482     }
483
484     fn adjust_closure_kind(&mut self,
485                            closure_id: DefIndex,
486                            new_kind: ty::ClosureKind,
487                            upvar_span: Span,
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);
491
492         let closure_kind = self.adjust_closure_kinds.get(&closure_id).cloned()
493             .or_else(|| {
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()
496             });
497
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);
501
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, _) => {
507                     // no change needed
508                 }
509
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(
515                         closure_id,
516                         (new_kind, Some((upvar_span, var_name)))
517                     );
518                 }
519             }
520         }
521     }
522 }
523
524 impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'gcx, 'tcx> {
525     fn consume(&mut self,
526                _consume_id: ast::NodeId,
527                _consume_span: Span,
528                cmt: mc::cmt<'tcx>,
529                mode: euv::ConsumeMode)
530     {
531         debug!("consume(cmt={:?},mode={:?})", cmt, mode);
532         self.adjust_upvar_borrow_kind_for_consume(cmt, mode);
533     }
534
535     fn matched_pat(&mut self,
536                    _matched_pat: &hir::Pat,
537                    _cmt: mc::cmt<'tcx>,
538                    _mode: euv::MatchMode)
539     {}
540
541     fn consume_pat(&mut self,
542                    _consume_pat: &hir::Pat,
543                    cmt: mc::cmt<'tcx>,
544                    mode: euv::ConsumeMode)
545     {
546         debug!("consume_pat(cmt={:?},mode={:?})", cmt, mode);
547         self.adjust_upvar_borrow_kind_for_consume(cmt, mode);
548     }
549
550     fn borrow(&mut self,
551               borrow_id: ast::NodeId,
552               _borrow_span: Span,
553               cmt: mc::cmt<'tcx>,
554               _loan_region: ty::Region<'tcx>,
555               bk: ty::BorrowKind,
556               _loan_cause: euv::LoanCause)
557     {
558         debug!("borrow(borrow_id={}, cmt={:?}, bk={:?})",
559                borrow_id, cmt, bk);
560
561         match bk {
562             ty::ImmBorrow => { }
563             ty::UniqueImmBorrow => {
564                 self.adjust_upvar_borrow_kind_for_unique(cmt);
565             }
566             ty::MutBorrow => {
567                 self.adjust_upvar_borrow_kind_for_mut(cmt);
568             }
569         }
570     }
571
572     fn decl_without_init(&mut self,
573                          _id: ast::NodeId,
574                          _span: Span)
575     {}
576
577     fn mutate(&mut self,
578               _assignment_id: ast::NodeId,
579               _assignment_span: Span,
580               assignee_cmt: mc::cmt<'tcx>,
581               _mode: euv::MutateMode)
582     {
583         debug!("mutate(assignee_cmt={:?})",
584                assignee_cmt);
585
586         self.adjust_upvar_borrow_kind_for_mut(assignee_cmt);
587     }
588 }
589
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)
593 }