]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/upvar.rs
Rollup merge of #58300 - taiki-e:librustc_typeck-2018, r=petrochenkov
[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::middle::expr_use_visitor as euv;
36 use crate::middle::mem_categorization as mc;
37 use crate::middle::mem_categorization::Categorization;
38 use rustc::hir;
39 use rustc::hir::def_id::DefId;
40 use rustc::hir::def_id::LocalDefId;
41 use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
42 use rustc::infer::UpvarRegion;
43 use rustc::ty::{self, Ty, TyCtxt, UpvarSubsts};
44 use syntax::ast;
45 use syntax_pos::Span;
46
47 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
48     pub fn closure_analyze(&self, body: &'gcx hir::Body) {
49         InferBorrowKindVisitor { fcx: self }.visit_body(body);
50
51         // it's our job to process these.
52         assert!(self.deferred_call_resolutions.borrow().is_empty());
53     }
54 }
55
56 struct InferBorrowKindVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
57     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
58 }
59
60 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for InferBorrowKindVisitor<'a, 'gcx, 'tcx> {
61     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
62         NestedVisitorMap::None
63     }
64
65     fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
66         if let hir::ExprKind::Closure(cc, _, body_id, _, _) = expr.node {
67             let body = self.fcx.tcx.hir().body(body_id);
68             self.visit_body(body);
69             self.fcx
70                 .analyze_closure(expr.id, expr.hir_id, expr.span, body, cc);
71         }
72
73         intravisit::walk_expr(self, expr);
74     }
75 }
76
77 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
78     fn analyze_closure(
79         &self,
80         closure_node_id: ast::NodeId,
81         closure_hir_id: hir::HirId,
82         span: Span,
83         body: &hir::Body,
84         capture_clause: hir::CaptureClause,
85     ) {
86         /*!
87          * Analysis starting point.
88          */
89
90         debug!(
91             "analyze_closure(id={:?}, body.id={:?})",
92             closure_node_id,
93             body.id()
94         );
95
96         // Extract the type of the closure.
97         let (closure_def_id, substs) = match self.node_ty(closure_hir_id).sty {
98             ty::Closure(def_id, substs) => (def_id, UpvarSubsts::Closure(substs)),
99             ty::Generator(def_id, substs, _) => (def_id, UpvarSubsts::Generator(substs)),
100             ty::Error => {
101                 // #51714: skip analysis when we have already encountered type errors
102                 return;
103             }
104             ref t => {
105                 span_bug!(
106                     span,
107                     "type of closure expr {:?} is not a closure {:?}",
108                     closure_node_id,
109                     t
110                 );
111             }
112         };
113
114         let infer_kind = if let UpvarSubsts::Closure(closure_substs) = substs {
115             if self.closure_kind(closure_def_id, closure_substs).is_none() {
116                 Some(closure_substs)
117             } else {
118                 None
119             }
120         } else {
121             None
122         };
123
124         self.tcx.with_freevars(closure_node_id, |freevars| {
125             let mut freevar_list: Vec<ty::UpvarId> = Vec::with_capacity(freevars.len());
126             for freevar in freevars {
127                 let upvar_id = ty::UpvarId {
128                     var_path: ty::UpvarPath {
129                         hir_id: self.tcx.hir().node_to_hir_id(freevar.var_id()),
130                     },
131                     closure_expr_id: LocalDefId::from_def_id(closure_def_id),
132                 };
133                 debug!("seed upvar_id {:?}", upvar_id);
134                 // Adding the upvar Id to the list of Upvars, which will be added
135                 // to the map for the closure at the end of the for loop.
136                 freevar_list.push(upvar_id);
137
138                 let capture_kind = match capture_clause {
139                     hir::CaptureByValue => 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 {
144                             kind: ty::ImmBorrow,
145                             region: freevar_region,
146                         };
147                         ty::UpvarCapture::ByRef(upvar_borrow)
148                     }
149                 };
150
151                 self.tables
152                     .borrow_mut()
153                     .upvar_capture_map
154                     .insert(upvar_id, capture_kind);
155             }
156             // Add the vector of freevars to the map keyed with the closure id.
157             // This gives us an easier access to them without having to call
158             // with_freevars again..
159             if !freevar_list.is_empty() {
160                 self.tables
161                     .borrow_mut()
162                     .upvar_list
163                     .insert(closure_def_id, freevar_list);
164             }
165         });
166
167         let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
168         let region_scope_tree = &self.tcx.region_scope_tree(body_owner_def_id);
169         let mut delegate = InferBorrowKind {
170             fcx: self,
171             closure_def_id: closure_def_id,
172             current_closure_kind: ty::ClosureKind::LATTICE_BOTTOM,
173             current_origin: None,
174             adjust_upvar_captures: ty::UpvarCaptureMap::default(),
175         };
176         euv::ExprUseVisitor::with_infer(
177             &mut delegate,
178             &self.infcx,
179             self.param_env,
180             region_scope_tree,
181             &self.tables.borrow(),
182         )
183         .consume_body(body);
184
185         if let Some(closure_substs) = infer_kind {
186             // Unify the (as yet unbound) type variable in the closure
187             // substs with the kind we inferred.
188             let inferred_kind = delegate.current_closure_kind;
189             let closure_kind_ty = closure_substs.closure_kind_ty(closure_def_id, self.tcx);
190             self.demand_eqtype(span, inferred_kind.to_ty(self.tcx), closure_kind_ty);
191
192             // If we have an origin, store it.
193             if let Some(origin) = delegate.current_origin {
194                 self.tables
195                     .borrow_mut()
196                     .closure_kind_origins_mut()
197                     .insert(closure_hir_id, origin);
198             }
199         }
200
201         self.tables
202             .borrow_mut()
203             .upvar_capture_map
204             .extend(delegate.adjust_upvar_captures);
205
206         // Now that we've analyzed the closure, we know how each
207         // variable is borrowed, and we know what traits the closure
208         // implements (Fn vs FnMut etc). We now have some updates to do
209         // with that information.
210         //
211         // Note that no closure type C may have an upvar of type C
212         // (though it may reference itself via a trait object). This
213         // results from the desugaring of closures to a struct like
214         // `Foo<..., UV0...UVn>`. If one of those upvars referenced
215         // C, then the type would have infinite size (and the
216         // inference algorithm will reject it).
217
218         // Equate the type variables for the upvars with the actual types.
219         let final_upvar_tys = self.final_upvar_tys(closure_node_id);
220         debug!(
221             "analyze_closure: id={:?} substs={:?} final_upvar_tys={:?}",
222             closure_node_id, substs, final_upvar_tys
223         );
224         for (upvar_ty, final_upvar_ty) in substs
225             .upvar_tys(closure_def_id, self.tcx)
226             .zip(final_upvar_tys)
227         {
228             self.demand_suptype(span, upvar_ty, final_upvar_ty);
229         }
230
231         // If we are also inferred the closure kind here,
232         // process any deferred resolutions.
233         let deferred_call_resolutions = self.remove_deferred_call_resolutions(closure_def_id);
234         for deferred_call_resolution in deferred_call_resolutions {
235             deferred_call_resolution.resolve(self);
236         }
237     }
238
239     // Returns a list of `ClosureUpvar`s for each upvar.
240     fn final_upvar_tys(&self, closure_id: ast::NodeId) -> Vec<Ty<'tcx>> {
241         // Presently an unboxed closure type cannot "escape" out of a
242         // function, so we will only encounter ones that originated in the
243         // local crate or were inlined into it along with some function.
244         // This may change if abstract return types of some sort are
245         // implemented.
246         let tcx = self.tcx;
247         let closure_def_index = tcx.hir().local_def_id(closure_id);
248
249         tcx.with_freevars(closure_id, |freevars| {
250             freevars
251                 .iter()
252                 .map(|freevar| {
253                     let var_node_id = freevar.var_id();
254                     let var_hir_id = tcx.hir().node_to_hir_id(var_node_id);
255                     let freevar_ty = self.node_ty(var_hir_id);
256                     let upvar_id = ty::UpvarId {
257                         var_path: ty::UpvarPath { hir_id: var_hir_id },
258                         closure_expr_id: LocalDefId::from_def_id(closure_def_index),
259                     };
260                     let capture = self.tables.borrow().upvar_capture(upvar_id);
261
262                     debug!(
263                         "var_id={:?} freevar_ty={:?} capture={:?}",
264                         var_node_id, freevar_ty, capture
265                     );
266
267                     match capture {
268                         ty::UpvarCapture::ByValue => freevar_ty,
269                         ty::UpvarCapture::ByRef(borrow) => tcx.mk_ref(
270                             borrow.region,
271                             ty::TypeAndMut {
272                                 ty: freevar_ty,
273                                 mutbl: borrow.kind.to_mutbl_lossy(),
274                             },
275                         ),
276                     }
277                 })
278                 .collect()
279         })
280     }
281 }
282
283 struct InferBorrowKind<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
284     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
285
286     // The def-id of the closure whose kind and upvar accesses are being inferred.
287     closure_def_id: DefId,
288
289     // The kind that we have inferred that the current closure
290     // requires. Note that we *always* infer a minimal kind, even if
291     // we don't always *use* that in the final result (i.e., sometimes
292     // we've taken the closure kind from the expectations instead, and
293     // for generators we don't even implement the closure traits
294     // really).
295     current_closure_kind: ty::ClosureKind,
296
297     // If we modified `current_closure_kind`, this field contains a `Some()` with the
298     // variable access that caused us to do so.
299     current_origin: Option<(Span, ast::Name)>,
300
301     // For each upvar that we access, we track the minimal kind of
302     // access we need (ref, ref mut, move, etc).
303     adjust_upvar_captures: ty::UpvarCaptureMap<'tcx>,
304 }
305
306 impl<'a, 'gcx, 'tcx> InferBorrowKind<'a, 'gcx, 'tcx> {
307     fn adjust_upvar_borrow_kind_for_consume(
308         &mut self,
309         cmt: &mc::cmt_<'tcx>,
310         mode: euv::ConsumeMode,
311     ) {
312         debug!(
313             "adjust_upvar_borrow_kind_for_consume(cmt={:?}, mode={:?})",
314             cmt, mode
315         );
316
317         // we only care about moves
318         match mode {
319             euv::Copy => {
320                 return;
321             }
322             euv::Move(_) => {}
323         }
324
325         let tcx = self.fcx.tcx;
326
327         // watch out for a move of the deref of a borrowed pointer;
328         // for that to be legal, the upvar would have to be borrowed
329         // by value instead
330         let guarantor = cmt.guarantor();
331         debug!(
332             "adjust_upvar_borrow_kind_for_consume: guarantor={:?}",
333             guarantor
334         );
335         debug!(
336             "adjust_upvar_borrow_kind_for_consume: guarantor.cat={:?}",
337             guarantor.cat
338         );
339         if let Categorization::Deref(_, mc::BorrowedPtr(..)) = guarantor.cat {
340             debug!(
341                 "adjust_upvar_borrow_kind_for_consume: found deref with note {:?}",
342                 cmt.note
343             );
344             match guarantor.note {
345                 mc::NoteUpvarRef(upvar_id) => {
346                     debug!(
347                         "adjust_upvar_borrow_kind_for_consume: \
348                          setting upvar_id={:?} to by value",
349                         upvar_id
350                     );
351
352                     // to move out of an upvar, this must be a FnOnce closure
353                     self.adjust_closure_kind(
354                         upvar_id.closure_expr_id,
355                         ty::ClosureKind::FnOnce,
356                         guarantor.span,
357                         var_name(tcx, upvar_id.var_path.hir_id),
358                     );
359
360                     self.adjust_upvar_captures
361                         .insert(upvar_id, ty::UpvarCapture::ByValue);
362                 }
363                 mc::NoteClosureEnv(upvar_id) => {
364                     // we get just a closureenv ref if this is a
365                     // `move` closure, or if the upvar has already
366                     // been inferred to by-value. In any case, we
367                     // must still adjust the kind of the closure
368                     // to be a FnOnce closure to permit moves out
369                     // of the environment.
370                     self.adjust_closure_kind(
371                         upvar_id.closure_expr_id,
372                         ty::ClosureKind::FnOnce,
373                         guarantor.span,
374                         var_name(tcx, upvar_id.var_path.hir_id),
375                     );
376                 }
377                 mc::NoteIndex | mc::NoteNone => {}
378             }
379         }
380     }
381
382     /// Indicates that `cmt` is being directly mutated (e.g., assigned
383     /// to). If cmt contains any by-ref upvars, this implies that
384     /// those upvars must be borrowed using an `&mut` borrow.
385     fn adjust_upvar_borrow_kind_for_mut(&mut self, cmt: &mc::cmt_<'tcx>) {
386         debug!("adjust_upvar_borrow_kind_for_mut(cmt={:?})", cmt);
387
388         match cmt.cat.clone() {
389             Categorization::Deref(base, mc::Unique)
390             | Categorization::Interior(base, _)
391             | Categorization::Downcast(base, _) => {
392                 // Interior or owned data is mutable if base is
393                 // mutable, so iterate to the base.
394                 self.adjust_upvar_borrow_kind_for_mut(&base);
395             }
396
397             Categorization::Deref(base, mc::BorrowedPtr(..)) => {
398                 if !self.try_adjust_upvar_deref(cmt, ty::MutBorrow) {
399                     // assignment to deref of an `&mut`
400                     // borrowed pointer implies that the
401                     // pointer itself must be unique, but not
402                     // necessarily *mutable*
403                     self.adjust_upvar_borrow_kind_for_unique(&base);
404                 }
405             }
406
407             Categorization::Deref(_, mc::UnsafePtr(..))
408             | Categorization::StaticItem
409             | Categorization::ThreadLocal(..)
410             | Categorization::Rvalue(..)
411             | Categorization::Local(_)
412             | Categorization::Upvar(..) => {
413                 return;
414             }
415         }
416     }
417
418     fn adjust_upvar_borrow_kind_for_unique(&mut self, cmt: &mc::cmt_<'tcx>) {
419         debug!("adjust_upvar_borrow_kind_for_unique(cmt={:?})", cmt);
420
421         match cmt.cat.clone() {
422             Categorization::Deref(base, mc::Unique)
423             | Categorization::Interior(base, _)
424             | Categorization::Downcast(base, _) => {
425                 // Interior or owned data is unique if base is
426                 // unique.
427                 self.adjust_upvar_borrow_kind_for_unique(&base);
428             }
429
430             Categorization::Deref(base, mc::BorrowedPtr(..)) => {
431                 if !self.try_adjust_upvar_deref(cmt, ty::UniqueImmBorrow) {
432                     // for a borrowed pointer to be unique, its
433                     // base must be unique
434                     self.adjust_upvar_borrow_kind_for_unique(&base);
435                 }
436             }
437
438             Categorization::Deref(_, mc::UnsafePtr(..))
439             | Categorization::StaticItem
440             | Categorization::ThreadLocal(..)
441             | Categorization::Rvalue(..)
442             | Categorization::Local(_)
443             | Categorization::Upvar(..) => {}
444         }
445     }
446
447     fn try_adjust_upvar_deref(
448         &mut self,
449         cmt: &mc::cmt_<'tcx>,
450         borrow_kind: ty::BorrowKind,
451     ) -> bool {
452         assert!(match borrow_kind {
453             ty::MutBorrow => true,
454             ty::UniqueImmBorrow => true,
455
456             // imm borrows never require adjusting any kinds, so we don't wind up here
457             ty::ImmBorrow => false,
458         });
459
460         let tcx = self.fcx.tcx;
461
462         match cmt.note {
463             mc::NoteUpvarRef(upvar_id) => {
464                 // if this is an implicit deref of an
465                 // upvar, then we need to modify the
466                 // borrow_kind of the upvar to make sure it
467                 // is inferred to mutable if necessary
468                 self.adjust_upvar_borrow_kind(upvar_id, borrow_kind);
469
470                 // also need to be in an FnMut closure since this is not an ImmBorrow
471                 self.adjust_closure_kind(
472                     upvar_id.closure_expr_id,
473                     ty::ClosureKind::FnMut,
474                     cmt.span,
475                     var_name(tcx, upvar_id.var_path.hir_id),
476                 );
477
478                 true
479             }
480             mc::NoteClosureEnv(upvar_id) => {
481                 // this kind of deref occurs in a `move` closure, or
482                 // for a by-value upvar; in either case, to mutate an
483                 // upvar, we need to be an FnMut closure
484                 self.adjust_closure_kind(
485                     upvar_id.closure_expr_id,
486                     ty::ClosureKind::FnMut,
487                     cmt.span,
488                     var_name(tcx, upvar_id.var_path.hir_id),
489                 );
490
491                 true
492             }
493             mc::NoteIndex | mc::NoteNone => false,
494         }
495     }
496
497     /// We infer the borrow_kind with which to borrow upvars in a stack closure.
498     /// The borrow_kind basically follows a lattice of `imm < unique-imm < mut`,
499     /// moving from left to right as needed (but never right to left).
500     /// Here the argument `mutbl` is the borrow_kind that is required by
501     /// some particular use.
502     fn adjust_upvar_borrow_kind(&mut self, upvar_id: ty::UpvarId, kind: ty::BorrowKind) {
503         let upvar_capture = self
504             .adjust_upvar_captures
505             .get(&upvar_id)
506             .cloned()
507             .unwrap_or_else(|| self.fcx.tables.borrow().upvar_capture(upvar_id));
508         debug!(
509             "adjust_upvar_borrow_kind(upvar_id={:?}, upvar_capture={:?}, kind={:?})",
510             upvar_id, upvar_capture, kind
511         );
512
513         match upvar_capture {
514             ty::UpvarCapture::ByValue => {
515                 // Upvar is already by-value, the strongest criteria.
516             }
517             ty::UpvarCapture::ByRef(mut upvar_borrow) => {
518                 match (upvar_borrow.kind, kind) {
519                     // Take RHS:
520                     (ty::ImmBorrow, ty::UniqueImmBorrow)
521                     | (ty::ImmBorrow, ty::MutBorrow)
522                     | (ty::UniqueImmBorrow, ty::MutBorrow) => {
523                         upvar_borrow.kind = kind;
524                         self.adjust_upvar_captures
525                             .insert(upvar_id, ty::UpvarCapture::ByRef(upvar_borrow));
526                     }
527                     // Take LHS:
528                     (ty::ImmBorrow, ty::ImmBorrow)
529                     | (ty::UniqueImmBorrow, ty::ImmBorrow)
530                     | (ty::UniqueImmBorrow, ty::UniqueImmBorrow)
531                     | (ty::MutBorrow, _) => {}
532                 }
533             }
534         }
535     }
536
537     fn adjust_closure_kind(
538         &mut self,
539         closure_id: LocalDefId,
540         new_kind: ty::ClosureKind,
541         upvar_span: Span,
542         var_name: ast::Name,
543     ) {
544         debug!(
545             "adjust_closure_kind(closure_id={:?}, new_kind={:?}, upvar_span={:?}, var_name={})",
546             closure_id, new_kind, upvar_span, var_name
547         );
548
549         // Is this the closure whose kind is currently being inferred?
550         if closure_id.to_def_id() != self.closure_def_id {
551             debug!("adjust_closure_kind: not current closure");
552             return;
553         }
554
555         // closures start out as `Fn`.
556         let existing_kind = self.current_closure_kind;
557
558         debug!(
559             "adjust_closure_kind: closure_id={:?}, existing_kind={:?}, new_kind={:?}",
560             closure_id, existing_kind, new_kind
561         );
562
563         match (existing_kind, new_kind) {
564             (ty::ClosureKind::Fn, ty::ClosureKind::Fn)
565             | (ty::ClosureKind::FnMut, ty::ClosureKind::Fn)
566             | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut)
567             | (ty::ClosureKind::FnOnce, _) => {
568                 // no change needed
569             }
570
571             (ty::ClosureKind::Fn, ty::ClosureKind::FnMut)
572             | (ty::ClosureKind::Fn, ty::ClosureKind::FnOnce)
573             | (ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
574                 // new kind is stronger than the old kind
575                 self.current_closure_kind = new_kind;
576                 self.current_origin = Some((upvar_span, var_name));
577             }
578         }
579     }
580 }
581
582 impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'gcx, 'tcx> {
583     fn consume(
584         &mut self,
585         _consume_id: ast::NodeId,
586         _consume_span: Span,
587         cmt: &mc::cmt_<'tcx>,
588         mode: euv::ConsumeMode,
589     ) {
590         debug!("consume(cmt={:?},mode={:?})", cmt, mode);
591         self.adjust_upvar_borrow_kind_for_consume(cmt, mode);
592     }
593
594     fn matched_pat(
595         &mut self,
596         _matched_pat: &hir::Pat,
597         _cmt: &mc::cmt_<'tcx>,
598         _mode: euv::MatchMode,
599     ) {
600     }
601
602     fn consume_pat(
603         &mut self,
604         _consume_pat: &hir::Pat,
605         cmt: &mc::cmt_<'tcx>,
606         mode: euv::ConsumeMode,
607     ) {
608         debug!("consume_pat(cmt={:?},mode={:?})", cmt, mode);
609         self.adjust_upvar_borrow_kind_for_consume(cmt, mode);
610     }
611
612     fn borrow(
613         &mut self,
614         borrow_id: ast::NodeId,
615         _borrow_span: Span,
616         cmt: &mc::cmt_<'tcx>,
617         _loan_region: ty::Region<'tcx>,
618         bk: ty::BorrowKind,
619         _loan_cause: euv::LoanCause,
620     ) {
621         debug!(
622             "borrow(borrow_id={}, cmt={:?}, bk={:?})",
623             borrow_id, cmt, bk
624         );
625
626         match bk {
627             ty::ImmBorrow => {}
628             ty::UniqueImmBorrow => {
629                 self.adjust_upvar_borrow_kind_for_unique(cmt);
630             }
631             ty::MutBorrow => {
632                 self.adjust_upvar_borrow_kind_for_mut(cmt);
633             }
634         }
635     }
636
637     fn decl_without_init(&mut self, _id: ast::NodeId, _span: Span) {}
638
639     fn mutate(
640         &mut self,
641         _assignment_id: ast::NodeId,
642         _assignment_span: Span,
643         assignee_cmt: &mc::cmt_<'tcx>,
644         _mode: euv::MutateMode,
645     ) {
646         debug!("mutate(assignee_cmt={:?})", assignee_cmt);
647
648         self.adjust_upvar_borrow_kind_for_mut(assignee_cmt);
649     }
650 }
651
652 fn var_name(tcx: TyCtxt, var_hir_id: hir::HirId) -> ast::Name {
653     tcx.hir().name_by_hir_id(var_hir_id)
654 }