]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/mem_categorization.rs
change the format of the linked issue number
[rust.git] / src / librustc / middle / mem_categorization.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 //! # Categorization
12 //!
13 //! The job of the categorization module is to analyze an expression to
14 //! determine what kind of memory is used in evaluating it (for example,
15 //! where dereferences occur and what kind of pointer is dereferenced;
16 //! whether the memory is mutable; etc)
17 //!
18 //! Categorization effectively transforms all of our expressions into
19 //! expressions of the following forms (the actual enum has many more
20 //! possibilities, naturally, but they are all variants of these base
21 //! forms):
22 //!
23 //!     E = rvalue    // some computed rvalue
24 //!       | x         // address of a local variable or argument
25 //!       | *E        // deref of a ptr
26 //!       | E.comp    // access to an interior component
27 //!
28 //! Imagine a routine ToAddr(Expr) that evaluates an expression and returns an
29 //! address where the result is to be found.  If Expr is an lvalue, then this
30 //! is the address of the lvalue.  If Expr is an rvalue, this is the address of
31 //! some temporary spot in memory where the result is stored.
32 //!
33 //! Now, cat_expr() classifies the expression Expr and the address A=ToAddr(Expr)
34 //! as follows:
35 //!
36 //! - cat: what kind of expression was this?  This is a subset of the
37 //!   full expression forms which only includes those that we care about
38 //!   for the purpose of the analysis.
39 //! - mutbl: mutability of the address A
40 //! - ty: the type of data found at the address A
41 //!
42 //! The resulting categorization tree differs somewhat from the expressions
43 //! themselves.  For example, auto-derefs are explicit.  Also, an index a[b] is
44 //! decomposed into two operations: a dereference to reach the array data and
45 //! then an index to jump forward to the relevant item.
46 //!
47 //! ## By-reference upvars
48 //!
49 //! One part of the translation which may be non-obvious is that we translate
50 //! closure upvars into the dereference of a borrowed pointer; this more closely
51 //! resembles the runtime translation. So, for example, if we had:
52 //!
53 //!     let mut x = 3;
54 //!     let y = 5;
55 //!     let inc = || x += y;
56 //!
57 //! Then when we categorize `x` (*within* the closure) we would yield a
58 //! result of `*x'`, effectively, where `x'` is a `Categorization::Upvar` reference
59 //! tied to `x`. The type of `x'` will be a borrowed pointer.
60
61 #![allow(non_camel_case_types)]
62
63 pub use self::PointerKind::*;
64 pub use self::InteriorKind::*;
65 pub use self::FieldName::*;
66 pub use self::ElementKind::*;
67 pub use self::MutabilityCategory::*;
68 pub use self::AliasableReason::*;
69 pub use self::Note::*;
70
71 use self::Aliasability::*;
72
73 use hir::def_id::DefId;
74 use hir::map as hir_map;
75 use infer::InferCtxt;
76 use hir::def::{Def, CtorKind};
77 use ty::adjustment;
78 use ty::{self, Ty, TyCtxt};
79
80 use hir::{MutImmutable, MutMutable, PatKind};
81 use hir::pat_util::EnumerateAndAdjustIterator;
82 use hir;
83 use syntax::ast;
84 use syntax_pos::Span;
85
86 use std::fmt;
87 use std::rc::Rc;
88
89 #[derive(Clone, PartialEq)]
90 pub enum Categorization<'tcx> {
91     // temporary val, argument is its scope
92     Rvalue(&'tcx ty::Region, &'tcx ty::Region),
93     StaticItem,
94     Upvar(Upvar),                          // upvar referenced by closure env
95     Local(ast::NodeId),                    // local variable
96     Deref(cmt<'tcx>, usize, PointerKind<'tcx>),  // deref of a ptr
97     Interior(cmt<'tcx>, InteriorKind),     // something interior: field, tuple, etc
98     Downcast(cmt<'tcx>, DefId),            // selects a particular enum variant (*1)
99
100     // (*1) downcast is only required if the enum has more than one variant
101 }
102
103 // Represents any kind of upvar
104 #[derive(Clone, Copy, PartialEq)]
105 pub struct Upvar {
106     pub id: ty::UpvarId,
107     pub kind: ty::ClosureKind
108 }
109
110 // different kinds of pointers:
111 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
112 pub enum PointerKind<'tcx> {
113     /// `Box<T>`
114     Unique,
115
116     /// `&T`
117     BorrowedPtr(ty::BorrowKind, &'tcx ty::Region),
118
119     /// `*T`
120     UnsafePtr(hir::Mutability),
121
122     /// Implicit deref of the `&T` that results from an overloaded index `[]`.
123     Implicit(ty::BorrowKind, &'tcx ty::Region),
124 }
125
126 // We use the term "interior" to mean "something reachable from the
127 // base without a pointer dereference", e.g. a field
128 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
129 pub enum InteriorKind {
130     InteriorField(FieldName),
131     InteriorElement(InteriorOffsetKind, ElementKind),
132 }
133
134 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
135 pub enum FieldName {
136     NamedField(ast::Name),
137     PositionalField(usize)
138 }
139
140 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
141 pub enum InteriorOffsetKind {
142     Index,            // e.g. `array_expr[index_expr]`
143     Pattern,          // e.g. `fn foo([_, a, _, _]: [A; 4]) { ... }`
144 }
145
146 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
147 pub enum ElementKind {
148     VecElement,
149     OtherElement,
150 }
151
152 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
153 pub enum MutabilityCategory {
154     McImmutable, // Immutable.
155     McDeclared,  // Directly declared as mutable.
156     McInherited, // Inherited from the fact that owner is mutable.
157 }
158
159 // A note about the provenance of a `cmt`.  This is used for
160 // special-case handling of upvars such as mutability inference.
161 // Upvar categorization can generate a variable number of nested
162 // derefs.  The note allows detecting them without deep pattern
163 // matching on the categorization.
164 #[derive(Clone, Copy, PartialEq, Debug)]
165 pub enum Note {
166     NoteClosureEnv(ty::UpvarId), // Deref through closure env
167     NoteUpvarRef(ty::UpvarId),   // Deref through by-ref upvar
168     NoteNone                     // Nothing special
169 }
170
171 // `cmt`: "Category, Mutability, and Type".
172 //
173 // a complete categorization of a value indicating where it originated
174 // and how it is located, as well as the mutability of the memory in
175 // which the value is stored.
176 //
177 // *WARNING* The field `cmt.type` is NOT necessarily the same as the
178 // result of `node_id_to_type(cmt.id)`. This is because the `id` is
179 // always the `id` of the node producing the type; in an expression
180 // like `*x`, the type of this deref node is the deref'd type (`T`),
181 // but in a pattern like `@x`, the `@x` pattern is again a
182 // dereference, but its type is the type *before* the dereference
183 // (`@T`). So use `cmt.ty` to find the type of the value in a consistent
184 // fashion. For more details, see the method `cat_pattern`
185 #[derive(Clone, PartialEq)]
186 pub struct cmt_<'tcx> {
187     pub id: ast::NodeId,           // id of expr/pat producing this value
188     pub span: Span,                // span of same expr/pat
189     pub cat: Categorization<'tcx>, // categorization of expr
190     pub mutbl: MutabilityCategory, // mutability of expr as lvalue
191     pub ty: Ty<'tcx>,              // type of the expr (*see WARNING above*)
192     pub note: Note,                // Note about the provenance of this cmt
193 }
194
195 pub type cmt<'tcx> = Rc<cmt_<'tcx>>;
196
197 pub enum ImmutabilityBlame<'tcx> {
198     ImmLocal(ast::NodeId),
199     ClosureEnv(ast::NodeId),
200     LocalDeref(ast::NodeId),
201     AdtFieldDeref(&'tcx ty::AdtDef, &'tcx ty::FieldDef)
202 }
203
204 impl<'tcx> cmt_<'tcx> {
205     fn resolve_field(&self, field_name: FieldName) -> (&'tcx ty::AdtDef, &'tcx ty::FieldDef)
206     {
207         let adt_def = self.ty.ty_adt_def().unwrap_or_else(|| {
208             bug!("interior cmt {:?} is not an ADT", self)
209         });
210         let variant_def = match self.cat {
211             Categorization::Downcast(_, variant_did) => {
212                 adt_def.variant_with_id(variant_did)
213             }
214             _ => {
215                 assert!(adt_def.is_univariant());
216                 &adt_def.variants[0]
217             }
218         };
219         let field_def = match field_name {
220             NamedField(name) => variant_def.field_named(name),
221             PositionalField(idx) => &variant_def.fields[idx]
222         };
223         (adt_def, field_def)
224     }
225
226     pub fn immutability_blame(&self) -> Option<ImmutabilityBlame<'tcx>> {
227         match self.cat {
228             Categorization::Deref(ref base_cmt, _, BorrowedPtr(ty::ImmBorrow, _)) |
229             Categorization::Deref(ref base_cmt, _, Implicit(ty::ImmBorrow, _)) => {
230                 // try to figure out where the immutable reference came from
231                 match base_cmt.cat {
232                     Categorization::Local(node_id) =>
233                         Some(ImmutabilityBlame::LocalDeref(node_id)),
234                     Categorization::Interior(ref base_cmt, InteriorField(field_name)) => {
235                         let (adt_def, field_def) = base_cmt.resolve_field(field_name);
236                         Some(ImmutabilityBlame::AdtFieldDeref(adt_def, field_def))
237                     }
238                     Categorization::Upvar(Upvar { id, .. }) => {
239                         if let NoteClosureEnv(..) = self.note {
240                             Some(ImmutabilityBlame::ClosureEnv(id.closure_expr_id))
241                         } else {
242                             None
243                         }
244                     }
245                     _ => None
246                 }
247             }
248             Categorization::Local(node_id) => {
249                 Some(ImmutabilityBlame::ImmLocal(node_id))
250             }
251             Categorization::Rvalue(..) |
252             Categorization::Upvar(..) |
253             Categorization::Deref(.., UnsafePtr(..)) => {
254                 // This should not be reachable up to inference limitations.
255                 None
256             }
257             Categorization::Interior(ref base_cmt, _) |
258             Categorization::Downcast(ref base_cmt, _) |
259             Categorization::Deref(ref base_cmt, _, _) => {
260                 base_cmt.immutability_blame()
261             }
262             Categorization::StaticItem => {
263                 // Do we want to do something here?
264                 None
265             }
266         }
267     }
268 }
269
270 pub trait ast_node {
271     fn id(&self) -> ast::NodeId;
272     fn span(&self) -> Span;
273 }
274
275 impl ast_node for hir::Expr {
276     fn id(&self) -> ast::NodeId { self.id }
277     fn span(&self) -> Span { self.span }
278 }
279
280 impl ast_node for hir::Pat {
281     fn id(&self) -> ast::NodeId { self.id }
282     fn span(&self) -> Span { self.span }
283 }
284
285 #[derive(Copy, Clone)]
286 pub struct MemCategorizationContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
287     pub infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
288     options: MemCategorizationOptions,
289 }
290
291 #[derive(Copy, Clone, Default)]
292 pub struct MemCategorizationOptions {
293     // If true, then when analyzing a closure upvar, if the closure
294     // has a missing kind, we treat it like a Fn closure. When false,
295     // we ICE if the closure has a missing kind. Should be false
296     // except during closure kind inference. It is used by the
297     // mem-categorization code to be able to have stricter assertions
298     // (which are always true except during upvar inference).
299     pub during_closure_kind_inference: bool,
300 }
301
302 pub type McResult<T> = Result<T, ()>;
303
304 impl MutabilityCategory {
305     pub fn from_mutbl(m: hir::Mutability) -> MutabilityCategory {
306         let ret = match m {
307             MutImmutable => McImmutable,
308             MutMutable => McDeclared
309         };
310         debug!("MutabilityCategory::{}({:?}) => {:?}",
311                "from_mutbl", m, ret);
312         ret
313     }
314
315     pub fn from_borrow_kind(borrow_kind: ty::BorrowKind) -> MutabilityCategory {
316         let ret = match borrow_kind {
317             ty::ImmBorrow => McImmutable,
318             ty::UniqueImmBorrow => McImmutable,
319             ty::MutBorrow => McDeclared,
320         };
321         debug!("MutabilityCategory::{}({:?}) => {:?}",
322                "from_borrow_kind", borrow_kind, ret);
323         ret
324     }
325
326     fn from_pointer_kind(base_mutbl: MutabilityCategory,
327                          ptr: PointerKind) -> MutabilityCategory {
328         let ret = match ptr {
329             Unique => {
330                 base_mutbl.inherit()
331             }
332             BorrowedPtr(borrow_kind, _) | Implicit(borrow_kind, _) => {
333                 MutabilityCategory::from_borrow_kind(borrow_kind)
334             }
335             UnsafePtr(m) => {
336                 MutabilityCategory::from_mutbl(m)
337             }
338         };
339         debug!("MutabilityCategory::{}({:?}, {:?}) => {:?}",
340                "from_pointer_kind", base_mutbl, ptr, ret);
341         ret
342     }
343
344     fn from_local(tcx: TyCtxt, id: ast::NodeId) -> MutabilityCategory {
345         let ret = match tcx.hir.get(id) {
346             hir_map::NodeLocal(p) => match p.node {
347                 PatKind::Binding(bind_mode, ..) => {
348                     if bind_mode == hir::BindByValue(hir::MutMutable) {
349                         McDeclared
350                     } else {
351                         McImmutable
352                     }
353                 }
354                 _ => span_bug!(p.span, "expected identifier pattern")
355             },
356             _ => span_bug!(tcx.hir.span(id), "expected identifier pattern")
357         };
358         debug!("MutabilityCategory::{}(tcx, id={:?}) => {:?}",
359                "from_local", id, ret);
360         ret
361     }
362
363     pub fn inherit(&self) -> MutabilityCategory {
364         let ret = match *self {
365             McImmutable => McImmutable,
366             McDeclared => McInherited,
367             McInherited => McInherited,
368         };
369         debug!("{:?}.inherit() => {:?}", self, ret);
370         ret
371     }
372
373     pub fn is_mutable(&self) -> bool {
374         let ret = match *self {
375             McImmutable => false,
376             McInherited => true,
377             McDeclared => true,
378         };
379         debug!("{:?}.is_mutable() => {:?}", self, ret);
380         ret
381     }
382
383     pub fn is_immutable(&self) -> bool {
384         let ret = match *self {
385             McImmutable => true,
386             McDeclared | McInherited => false
387         };
388         debug!("{:?}.is_immutable() => {:?}", self, ret);
389         ret
390     }
391
392     pub fn to_user_str(&self) -> &'static str {
393         match *self {
394             McDeclared | McInherited => "mutable",
395             McImmutable => "immutable",
396         }
397     }
398 }
399
400 impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
401     pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>)
402                -> MemCategorizationContext<'a, 'gcx, 'tcx> {
403         MemCategorizationContext::with_options(infcx, MemCategorizationOptions::default())
404     }
405
406     pub fn with_options(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
407                         options: MemCategorizationOptions)
408                         -> MemCategorizationContext<'a, 'gcx, 'tcx> {
409         MemCategorizationContext {
410             infcx: infcx,
411             options: options,
412         }
413     }
414
415     fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
416         self.infcx.tcx
417     }
418
419     fn expr_ty(&self, expr: &hir::Expr) -> McResult<Ty<'tcx>> {
420         match self.infcx.node_ty(expr.id) {
421             Ok(t) => Ok(t),
422             Err(()) => {
423                 debug!("expr_ty({:?}) yielded Err", expr);
424                 Err(())
425             }
426         }
427     }
428
429     fn expr_ty_adjusted(&self, expr: &hir::Expr) -> McResult<Ty<'tcx>> {
430         self.infcx.expr_ty_adjusted(expr)
431     }
432
433     fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>> {
434         self.infcx.node_ty(id)
435     }
436
437     fn pat_ty(&self, pat: &hir::Pat) -> McResult<Ty<'tcx>> {
438         let base_ty = self.infcx.node_ty(pat.id)?;
439         // FIXME (Issue #18207): This code detects whether we are
440         // looking at a `ref x`, and if so, figures out what the type
441         // *being borrowed* is.  But ideally we would put in a more
442         // fundamental fix to this conflated use of the node id.
443         let ret_ty = match pat.node {
444             PatKind::Binding(hir::BindByRef(_), ..) => {
445                 // a bind-by-ref means that the base_ty will be the type of the ident itself,
446                 // but what we want here is the type of the underlying value being borrowed.
447                 // So peel off one-level, turning the &T into T.
448                 match base_ty.builtin_deref(false, ty::NoPreference) {
449                     Some(t) => t.ty,
450                     None => { return Err(()); }
451                 }
452             }
453             _ => base_ty,
454         };
455         debug!("pat_ty(pat={:?}) base_ty={:?} ret_ty={:?}",
456                pat, base_ty, ret_ty);
457         Ok(ret_ty)
458     }
459
460     pub fn cat_expr(&self, expr: &hir::Expr) -> McResult<cmt<'tcx>> {
461         match self.infcx.tables.borrow().adjustments.get(&expr.id) {
462             None => {
463                 // No adjustments.
464                 self.cat_expr_unadjusted(expr)
465             }
466
467             Some(adjustment) => {
468                 match adjustment.kind {
469                     adjustment::Adjust::DerefRef {
470                         autoderefs,
471                         autoref: None,
472                         unsize: false
473                     } => {
474                         // Equivalent to *expr or something similar.
475                         self.cat_expr_autoderefd(expr, autoderefs)
476                     }
477
478                     adjustment::Adjust::NeverToAny |
479                     adjustment::Adjust::ReifyFnPointer |
480                     adjustment::Adjust::UnsafeFnPointer |
481                     adjustment::Adjust::ClosureFnPointer |
482                     adjustment::Adjust::MutToConstPointer |
483                     adjustment::Adjust::DerefRef {..} => {
484                         debug!("cat_expr({:?}): {:?}",
485                                adjustment,
486                                expr);
487                         // Result is an rvalue.
488                         let expr_ty = self.expr_ty_adjusted(expr)?;
489                         Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
490                     }
491                 }
492             }
493         }
494     }
495
496     pub fn cat_expr_autoderefd(&self,
497                                expr: &hir::Expr,
498                                autoderefs: usize)
499                                -> McResult<cmt<'tcx>> {
500         let mut cmt = self.cat_expr_unadjusted(expr)?;
501         debug!("cat_expr_autoderefd: autoderefs={}, cmt={:?}",
502                autoderefs,
503                cmt);
504         for deref in 1..autoderefs + 1 {
505             cmt = self.cat_deref(expr, cmt, deref)?;
506         }
507         return Ok(cmt);
508     }
509
510     pub fn cat_expr_unadjusted(&self, expr: &hir::Expr) -> McResult<cmt<'tcx>> {
511         debug!("cat_expr: id={} expr={:?}", expr.id, expr);
512
513         let expr_ty = self.expr_ty(expr)?;
514         match expr.node {
515           hir::ExprUnary(hir::UnDeref, ref e_base) => {
516             let base_cmt = self.cat_expr(&e_base)?;
517             self.cat_deref(expr, base_cmt, 0)
518           }
519
520           hir::ExprField(ref base, f_name) => {
521             let base_cmt = self.cat_expr(&base)?;
522             debug!("cat_expr(cat_field): id={} expr={:?} base={:?}",
523                    expr.id,
524                    expr,
525                    base_cmt);
526             Ok(self.cat_field(expr, base_cmt, f_name.node, expr_ty))
527           }
528
529           hir::ExprTupField(ref base, idx) => {
530             let base_cmt = self.cat_expr(&base)?;
531             Ok(self.cat_tup_field(expr, base_cmt, idx.node, expr_ty))
532           }
533
534           hir::ExprIndex(ref base, _) => {
535             let method_call = ty::MethodCall::expr(expr.id());
536             match self.infcx.node_method_ty(method_call) {
537                 Some(method_ty) => {
538                     // If this is an index implemented by a method call, then it
539                     // will include an implicit deref of the result.
540                     let ret_ty = self.overloaded_method_return_ty(method_ty);
541
542                     // The index method always returns an `&T`, so
543                     // dereference it to find the result type.
544                     let elem_ty = match ret_ty.sty {
545                         ty::TyRef(_, mt) => mt.ty,
546                         _ => {
547                             debug!("cat_expr_unadjusted: return type of overloaded index is {:?}?",
548                                    ret_ty);
549                             return Err(());
550                         }
551                     };
552
553                     // The call to index() returns a `&T` value, which
554                     // is an rvalue. That is what we will be
555                     // dereferencing.
556                     let base_cmt = self.cat_rvalue_node(expr.id(), expr.span(), ret_ty);
557                     Ok(self.cat_deref_common(expr, base_cmt, 1, elem_ty, true))
558                 }
559                 None => {
560                     self.cat_index(expr, self.cat_expr(&base)?, InteriorOffsetKind::Index)
561                 }
562             }
563           }
564
565           hir::ExprPath(ref qpath) => {
566             let def = self.infcx.tables.borrow().qpath_def(qpath, expr.id);
567             self.cat_def(expr.id, expr.span, expr_ty, def)
568           }
569
570           hir::ExprType(ref e, _) => {
571             self.cat_expr(&e)
572           }
573
574           hir::ExprAddrOf(..) | hir::ExprCall(..) |
575           hir::ExprAssign(..) | hir::ExprAssignOp(..) |
576           hir::ExprClosure(..) | hir::ExprRet(..) |
577           hir::ExprUnary(..) |
578           hir::ExprMethodCall(..) | hir::ExprCast(..) |
579           hir::ExprArray(..) | hir::ExprTup(..) | hir::ExprIf(..) |
580           hir::ExprBinary(..) | hir::ExprWhile(..) |
581           hir::ExprBlock(..) | hir::ExprLoop(..) | hir::ExprMatch(..) |
582           hir::ExprLit(..) | hir::ExprBreak(..) |
583           hir::ExprAgain(..) | hir::ExprStruct(..) | hir::ExprRepeat(..) |
584           hir::ExprInlineAsm(..) | hir::ExprBox(..) => {
585             Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
586           }
587         }
588     }
589
590     pub fn cat_def(&self,
591                    id: ast::NodeId,
592                    span: Span,
593                    expr_ty: Ty<'tcx>,
594                    def: Def)
595                    -> McResult<cmt<'tcx>> {
596         debug!("cat_def: id={} expr={:?} def={:?}",
597                id, expr_ty, def);
598
599         match def {
600           Def::StructCtor(..) | Def::VariantCtor(..) | Def::Const(..) |
601           Def::AssociatedConst(..) | Def::Fn(..) | Def::Method(..) => {
602                 Ok(self.cat_rvalue_node(id, span, expr_ty))
603           }
604
605           Def::Static(_, mutbl) => {
606               Ok(Rc::new(cmt_ {
607                   id:id,
608                   span:span,
609                   cat:Categorization::StaticItem,
610                   mutbl: if mutbl { McDeclared } else { McImmutable},
611                   ty:expr_ty,
612                   note: NoteNone
613               }))
614           }
615
616           Def::Upvar(def_id, _, fn_node_id) => {
617               let var_id = self.tcx().hir.as_local_node_id(def_id).unwrap();
618               let ty = self.node_ty(fn_node_id)?;
619               match ty.sty {
620                   ty::TyClosure(closure_id, _) => {
621                       match self.infcx.closure_kind(closure_id) {
622                           Some(kind) => {
623                               self.cat_upvar(id, span, var_id, fn_node_id, kind)
624                           }
625                           None => {
626                               if !self.options.during_closure_kind_inference {
627                                   span_bug!(
628                                       span,
629                                       "No closure kind for {:?}",
630                                       closure_id);
631                               }
632
633                               // during closure kind inference, we
634                               // don't know the closure kind yet, but
635                               // it's ok because we detect that we are
636                               // accessing an upvar and handle that
637                               // case specially anyhow. Use Fn
638                               // arbitrarily.
639                               self.cat_upvar(id, span, var_id, fn_node_id, ty::ClosureKind::Fn)
640                           }
641                       }
642                   }
643                   _ => {
644                       span_bug!(
645                           span,
646                           "Upvar of non-closure {} - {:?}",
647                           fn_node_id,
648                           ty);
649                   }
650               }
651           }
652
653           Def::Local(def_id) => {
654             let vid = self.tcx().hir.as_local_node_id(def_id).unwrap();
655             Ok(Rc::new(cmt_ {
656                 id: id,
657                 span: span,
658                 cat: Categorization::Local(vid),
659                 mutbl: MutabilityCategory::from_local(self.tcx(), vid),
660                 ty: expr_ty,
661                 note: NoteNone
662             }))
663           }
664
665           def => span_bug!(span, "unexpected definition in memory categorization: {:?}", def)
666         }
667     }
668
669     // Categorize an upvar, complete with invisible derefs of closure
670     // environment and upvar reference as appropriate.
671     fn cat_upvar(&self,
672                  id: ast::NodeId,
673                  span: Span,
674                  var_id: ast::NodeId,
675                  fn_node_id: ast::NodeId,
676                  kind: ty::ClosureKind)
677                  -> McResult<cmt<'tcx>>
678     {
679         // An upvar can have up to 3 components. We translate first to a
680         // `Categorization::Upvar`, which is itself a fiction -- it represents the reference to the
681         // field from the environment.
682         //
683         // `Categorization::Upvar`.  Next, we add a deref through the implicit
684         // environment pointer with an anonymous free region 'env and
685         // appropriate borrow kind for closure kinds that take self by
686         // reference.  Finally, if the upvar was captured
687         // by-reference, we add a deref through that reference.  The
688         // region of this reference is an inference variable 'up that
689         // was previously generated and recorded in the upvar borrow
690         // map.  The borrow kind bk is inferred by based on how the
691         // upvar is used.
692         //
693         // This results in the following table for concrete closure
694         // types:
695         //
696         //                | move                 | ref
697         // ---------------+----------------------+-------------------------------
698         // Fn             | copied -> &'env      | upvar -> &'env -> &'up bk
699         // FnMut          | copied -> &'env mut  | upvar -> &'env mut -> &'up bk
700         // FnOnce         | copied               | upvar -> &'up bk
701
702         let upvar_id = ty::UpvarId { var_id: var_id,
703                                      closure_expr_id: fn_node_id };
704         let var_ty = self.node_ty(var_id)?;
705
706         // Mutability of original variable itself
707         let var_mutbl = MutabilityCategory::from_local(self.tcx(), var_id);
708
709         // Construct the upvar. This represents access to the field
710         // from the environment (perhaps we should eventually desugar
711         // this field further, but it will do for now).
712         let cmt_result = cmt_ {
713             id: id,
714             span: span,
715             cat: Categorization::Upvar(Upvar {id: upvar_id, kind: kind}),
716             mutbl: var_mutbl,
717             ty: var_ty,
718             note: NoteNone
719         };
720
721         // If this is a `FnMut` or `Fn` closure, then the above is
722         // conceptually a `&mut` or `&` reference, so we have to add a
723         // deref.
724         let cmt_result = match kind {
725             ty::ClosureKind::FnOnce => {
726                 cmt_result
727             }
728             ty::ClosureKind::FnMut => {
729                 self.env_deref(id, span, upvar_id, var_mutbl, ty::MutBorrow, cmt_result)
730             }
731             ty::ClosureKind::Fn => {
732                 self.env_deref(id, span, upvar_id, var_mutbl, ty::ImmBorrow, cmt_result)
733             }
734         };
735
736         // If this is a by-ref capture, then the upvar we loaded is
737         // actually a reference, so we have to add an implicit deref
738         // for that.
739         let upvar_id = ty::UpvarId { var_id: var_id,
740                                      closure_expr_id: fn_node_id };
741         let upvar_capture = self.infcx.upvar_capture(upvar_id).unwrap();
742         let cmt_result = match upvar_capture {
743             ty::UpvarCapture::ByValue => {
744                 cmt_result
745             }
746             ty::UpvarCapture::ByRef(upvar_borrow) => {
747                 let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
748                 cmt_ {
749                     id: id,
750                     span: span,
751                     cat: Categorization::Deref(Rc::new(cmt_result), 0, ptr),
752                     mutbl: MutabilityCategory::from_borrow_kind(upvar_borrow.kind),
753                     ty: var_ty,
754                     note: NoteUpvarRef(upvar_id)
755                 }
756             }
757         };
758
759         let ret = Rc::new(cmt_result);
760         debug!("cat_upvar ret={:?}", ret);
761         Ok(ret)
762     }
763
764     fn env_deref(&self,
765                  id: ast::NodeId,
766                  span: Span,
767                  upvar_id: ty::UpvarId,
768                  upvar_mutbl: MutabilityCategory,
769                  env_borrow_kind: ty::BorrowKind,
770                  cmt_result: cmt_<'tcx>)
771                  -> cmt_<'tcx>
772     {
773         // Look up the node ID of the closure body so we can construct
774         // a free region within it
775         let fn_body_id = {
776             let fn_expr = match self.tcx().hir.find(upvar_id.closure_expr_id) {
777                 Some(hir_map::NodeExpr(e)) => e,
778                 _ => bug!()
779             };
780
781             match fn_expr.node {
782                 hir::ExprClosure(.., body_id, _) => body_id.node_id,
783                 _ => bug!()
784             }
785         };
786
787         // Region of environment pointer
788         let env_region = self.tcx().mk_region(ty::ReFree(ty::FreeRegion {
789             // The environment of a closure is guaranteed to
790             // outlive any bindings introduced in the body of the
791             // closure itself.
792             scope: self.tcx().region_maps.item_extent(fn_body_id),
793             bound_region: ty::BrEnv
794         }));
795
796         let env_ptr = BorrowedPtr(env_borrow_kind, env_region);
797
798         let var_ty = cmt_result.ty;
799
800         // We need to add the env deref.  This means
801         // that the above is actually immutable and
802         // has a ref type.  However, nothing should
803         // actually look at the type, so we can get
804         // away with stuffing a `TyError` in there
805         // instead of bothering to construct a proper
806         // one.
807         let cmt_result = cmt_ {
808             mutbl: McImmutable,
809             ty: self.tcx().types.err,
810             ..cmt_result
811         };
812
813         let mut deref_mutbl = MutabilityCategory::from_borrow_kind(env_borrow_kind);
814
815         // Issue #18335. If variable is declared as immutable, override the
816         // mutability from the environment and substitute an `&T` anyway.
817         match upvar_mutbl {
818             McImmutable => { deref_mutbl = McImmutable; }
819             McDeclared | McInherited => { }
820         }
821
822         let ret = cmt_ {
823             id: id,
824             span: span,
825             cat: Categorization::Deref(Rc::new(cmt_result), 0, env_ptr),
826             mutbl: deref_mutbl,
827             ty: var_ty,
828             note: NoteClosureEnv(upvar_id)
829         };
830
831         debug!("env_deref ret {:?}", ret);
832
833         ret
834     }
835
836     /// Returns the lifetime of a temporary created by expr with id `id`.
837     /// This could be `'static` if `id` is part of a constant expression.
838     pub fn temporary_scope(&self, id: ast::NodeId) -> (&'tcx ty::Region, &'tcx ty::Region)
839     {
840         let (scope, old_scope) =
841             self.tcx().region_maps.old_and_new_temporary_scope(id);
842         (self.tcx().mk_region(match scope {
843             Some(scope) => ty::ReScope(scope),
844             None => ty::ReStatic
845         }),
846          self.tcx().mk_region(match old_scope {
847             Some(scope) => ty::ReScope(scope),
848             None => ty::ReStatic
849         }))
850     }
851
852     pub fn cat_rvalue_node(&self,
853                            id: ast::NodeId,
854                            span: Span,
855                            expr_ty: Ty<'tcx>)
856                            -> cmt<'tcx> {
857         let promotable = self.tcx().rvalue_promotable_to_static.borrow().get(&id).cloned()
858                                    .unwrap_or(false);
859
860         // When the corresponding feature isn't toggled, only promote `[T; 0]`.
861         let promotable = match expr_ty.sty {
862             ty::TyArray(_, 0) => true,
863             _ => promotable && self.tcx().sess.features.borrow().rvalue_static_promotion,
864         };
865
866         // Compute maximum lifetime of this rvalue. This is 'static if
867         // we can promote to a constant, otherwise equal to enclosing temp
868         // lifetime.
869         let (re, old_re) = if promotable {
870             (self.tcx().mk_region(ty::ReStatic),
871              self.tcx().mk_region(ty::ReStatic))
872         } else {
873             self.temporary_scope(id)
874         };
875         let ret = self.cat_rvalue(id, span, re, old_re, expr_ty);
876         debug!("cat_rvalue_node ret {:?}", ret);
877         ret
878     }
879
880     pub fn cat_rvalue(&self,
881                       cmt_id: ast::NodeId,
882                       span: Span,
883                       temp_scope: &'tcx ty::Region,
884                       old_temp_scope: &'tcx ty::Region,
885                       expr_ty: Ty<'tcx>) -> cmt<'tcx> {
886         let ret = Rc::new(cmt_ {
887             id:cmt_id,
888             span:span,
889             cat:Categorization::Rvalue(temp_scope, old_temp_scope),
890             mutbl:McDeclared,
891             ty:expr_ty,
892             note: NoteNone
893         });
894         debug!("cat_rvalue ret {:?}", ret);
895         ret
896     }
897
898     pub fn cat_field<N:ast_node>(&self,
899                                  node: &N,
900                                  base_cmt: cmt<'tcx>,
901                                  f_name: ast::Name,
902                                  f_ty: Ty<'tcx>)
903                                  -> cmt<'tcx> {
904         let ret = Rc::new(cmt_ {
905             id: node.id(),
906             span: node.span(),
907             mutbl: base_cmt.mutbl.inherit(),
908             cat: Categorization::Interior(base_cmt, InteriorField(NamedField(f_name))),
909             ty: f_ty,
910             note: NoteNone
911         });
912         debug!("cat_field ret {:?}", ret);
913         ret
914     }
915
916     pub fn cat_tup_field<N:ast_node>(&self,
917                                      node: &N,
918                                      base_cmt: cmt<'tcx>,
919                                      f_idx: usize,
920                                      f_ty: Ty<'tcx>)
921                                      -> cmt<'tcx> {
922         let ret = Rc::new(cmt_ {
923             id: node.id(),
924             span: node.span(),
925             mutbl: base_cmt.mutbl.inherit(),
926             cat: Categorization::Interior(base_cmt, InteriorField(PositionalField(f_idx))),
927             ty: f_ty,
928             note: NoteNone
929         });
930         debug!("cat_tup_field ret {:?}", ret);
931         ret
932     }
933
934     fn cat_deref<N:ast_node>(&self,
935                              node: &N,
936                              base_cmt: cmt<'tcx>,
937                              deref_cnt: usize)
938                              -> McResult<cmt<'tcx>> {
939         let method_call = ty::MethodCall {
940             expr_id: node.id(),
941             autoderef: deref_cnt as u32
942         };
943         let method_ty = self.infcx.node_method_ty(method_call);
944
945         debug!("cat_deref: method_call={:?} method_ty={:?}",
946                method_call, method_ty.map(|ty| ty));
947
948         let base_cmt = match method_ty {
949             Some(method_ty) => {
950                 let ref_ty =
951                     self.tcx().no_late_bound_regions(&method_ty.fn_ret()).unwrap();
952                 self.cat_rvalue_node(node.id(), node.span(), ref_ty)
953             }
954             None => base_cmt
955         };
956         let base_cmt_ty = base_cmt.ty;
957         match base_cmt_ty.builtin_deref(true, ty::NoPreference) {
958             Some(mt) => {
959                 let ret = self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty, false);
960                 debug!("cat_deref ret {:?}", ret);
961                 Ok(ret)
962             }
963             None => {
964                 debug!("Explicit deref of non-derefable type: {:?}",
965                        base_cmt_ty);
966                 return Err(());
967             }
968         }
969     }
970
971     fn cat_deref_common<N:ast_node>(&self,
972                                     node: &N,
973                                     base_cmt: cmt<'tcx>,
974                                     deref_cnt: usize,
975                                     deref_ty: Ty<'tcx>,
976                                     implicit: bool)
977                                     -> cmt<'tcx>
978     {
979         let ptr = match base_cmt.ty.sty {
980             ty::TyAdt(def, ..) if def.is_box() => Unique,
981             ty::TyRawPtr(ref mt) => UnsafePtr(mt.mutbl),
982             ty::TyRef(r, mt) => {
983                 let bk = ty::BorrowKind::from_mutbl(mt.mutbl);
984                 if implicit { Implicit(bk, r) } else { BorrowedPtr(bk, r) }
985             }
986             ref ty => bug!("unexpected type in cat_deref_common: {:?}", ty)
987         };
988         let ret = Rc::new(cmt_ {
989             id: node.id(),
990             span: node.span(),
991             // For unique ptrs, we inherit mutability from the owning reference.
992             mutbl: MutabilityCategory::from_pointer_kind(base_cmt.mutbl, ptr),
993             cat: Categorization::Deref(base_cmt, deref_cnt, ptr),
994             ty: deref_ty,
995             note: NoteNone
996         });
997         debug!("cat_deref_common ret {:?}", ret);
998         ret
999     }
1000
1001     pub fn cat_index<N:ast_node>(&self,
1002                                  elt: &N,
1003                                  mut base_cmt: cmt<'tcx>,
1004                                  context: InteriorOffsetKind)
1005                                  -> McResult<cmt<'tcx>> {
1006         //! Creates a cmt for an indexing operation (`[]`).
1007         //!
1008         //! One subtle aspect of indexing that may not be
1009         //! immediately obvious: for anything other than a fixed-length
1010         //! vector, an operation like `x[y]` actually consists of two
1011         //! disjoint (from the point of view of borrowck) operations.
1012         //! The first is a deref of `x` to create a pointer `p` that points
1013         //! at the first element in the array. The second operation is
1014         //! an index which adds `y*sizeof(T)` to `p` to obtain the
1015         //! pointer to `x[y]`. `cat_index` will produce a resulting
1016         //! cmt containing both this deref and the indexing,
1017         //! presuming that `base_cmt` is not of fixed-length type.
1018         //!
1019         //! # Parameters
1020         //! - `elt`: the AST node being indexed
1021         //! - `base_cmt`: the cmt of `elt`
1022
1023         let method_call = ty::MethodCall::expr(elt.id());
1024         let method_ty = self.infcx.node_method_ty(method_call);
1025
1026         let (element_ty, element_kind) = match method_ty {
1027             Some(method_ty) => {
1028                 let ref_ty = self.overloaded_method_return_ty(method_ty);
1029                 base_cmt = self.cat_rvalue_node(elt.id(), elt.span(), ref_ty);
1030
1031                 (ref_ty.builtin_deref(false, ty::NoPreference).unwrap().ty,
1032                  ElementKind::OtherElement)
1033             }
1034             None => {
1035                 match base_cmt.ty.builtin_index() {
1036                     Some(ty) => (ty, ElementKind::VecElement),
1037                     None => {
1038                         return Err(());
1039                     }
1040                 }
1041             }
1042         };
1043
1044         let interior_elem = InteriorElement(context, element_kind);
1045         let ret =
1046             self.cat_imm_interior(elt, base_cmt.clone(), element_ty, interior_elem);
1047         debug!("cat_index ret {:?}", ret);
1048         return Ok(ret);
1049     }
1050
1051     pub fn cat_imm_interior<N:ast_node>(&self,
1052                                         node: &N,
1053                                         base_cmt: cmt<'tcx>,
1054                                         interior_ty: Ty<'tcx>,
1055                                         interior: InteriorKind)
1056                                         -> cmt<'tcx> {
1057         let ret = Rc::new(cmt_ {
1058             id: node.id(),
1059             span: node.span(),
1060             mutbl: base_cmt.mutbl.inherit(),
1061             cat: Categorization::Interior(base_cmt, interior),
1062             ty: interior_ty,
1063             note: NoteNone
1064         });
1065         debug!("cat_imm_interior ret={:?}", ret);
1066         ret
1067     }
1068
1069     pub fn cat_downcast<N:ast_node>(&self,
1070                                     node: &N,
1071                                     base_cmt: cmt<'tcx>,
1072                                     downcast_ty: Ty<'tcx>,
1073                                     variant_did: DefId)
1074                                     -> cmt<'tcx> {
1075         let ret = Rc::new(cmt_ {
1076             id: node.id(),
1077             span: node.span(),
1078             mutbl: base_cmt.mutbl.inherit(),
1079             cat: Categorization::Downcast(base_cmt, variant_did),
1080             ty: downcast_ty,
1081             note: NoteNone
1082         });
1083         debug!("cat_downcast ret={:?}", ret);
1084         ret
1085     }
1086
1087     pub fn cat_pattern<F>(&self, cmt: cmt<'tcx>, pat: &hir::Pat, mut op: F) -> McResult<()>
1088         where F: FnMut(&MemCategorizationContext<'a, 'gcx, 'tcx>, cmt<'tcx>, &hir::Pat),
1089     {
1090         self.cat_pattern_(cmt, pat, &mut op)
1091     }
1092
1093     // FIXME(#19596) This is a workaround, but there should be a better way to do this
1094     fn cat_pattern_<F>(&self, cmt: cmt<'tcx>, pat: &hir::Pat, op: &mut F) -> McResult<()>
1095         where F : FnMut(&MemCategorizationContext<'a, 'gcx, 'tcx>, cmt<'tcx>, &hir::Pat)
1096     {
1097         // Here, `cmt` is the categorization for the value being
1098         // matched and pat is the pattern it is being matched against.
1099         //
1100         // In general, the way that this works is that we walk down
1101         // the pattern, constructing a cmt that represents the path
1102         // that will be taken to reach the value being matched.
1103         //
1104         // When we encounter named bindings, we take the cmt that has
1105         // been built up and pass it off to guarantee_valid() so that
1106         // we can be sure that the binding will remain valid for the
1107         // duration of the arm.
1108         //
1109         // (*2) There is subtlety concerning the correspondence between
1110         // pattern ids and types as compared to *expression* ids and
1111         // types. This is explained briefly. on the definition of the
1112         // type `cmt`, so go off and read what it says there, then
1113         // come back and I'll dive into a bit more detail here. :) OK,
1114         // back?
1115         //
1116         // In general, the id of the cmt should be the node that
1117         // "produces" the value---patterns aren't executable code
1118         // exactly, but I consider them to "execute" when they match a
1119         // value, and I consider them to produce the value that was
1120         // matched. So if you have something like:
1121         //
1122         //     let x = @@3;
1123         //     match x {
1124         //       @@y { ... }
1125         //     }
1126         //
1127         // In this case, the cmt and the relevant ids would be:
1128         //
1129         //     CMT             Id                  Type of Id Type of cmt
1130         //
1131         //     local(x)->@->@
1132         //     ^~~~~~~^        `x` from discr      @@int      @@int
1133         //     ^~~~~~~~~~^     `@@y` pattern node  @@int      @int
1134         //     ^~~~~~~~~~~~~^  `@y` pattern node   @int       int
1135         //
1136         // You can see that the types of the id and the cmt are in
1137         // sync in the first line, because that id is actually the id
1138         // of an expression. But once we get to pattern ids, the types
1139         // step out of sync again. So you'll see below that we always
1140         // get the type of the *subpattern* and use that.
1141
1142         debug!("cat_pattern: {:?} cmt={:?}", pat, cmt);
1143
1144         op(self, cmt.clone(), pat);
1145
1146         // Note: This goes up here (rather than within the PatKind::TupleStruct arm
1147         // alone) because PatKind::Struct can also refer to variants.
1148         let cmt = match pat.node {
1149             PatKind::Path(hir::QPath::Resolved(_, ref path)) |
1150             PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
1151             PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
1152                 match path.def {
1153                     Def::Err => return Err(()),
1154                     Def::Variant(variant_did) |
1155                     Def::VariantCtor(variant_did, ..) => {
1156                         // univariant enums do not need downcasts
1157                         let enum_did = self.tcx().parent_def_id(variant_did).unwrap();
1158                         if !self.tcx().lookup_adt_def(enum_did).is_univariant() {
1159                             self.cat_downcast(pat, cmt.clone(), cmt.ty, variant_did)
1160                         } else {
1161                             cmt
1162                         }
1163                     }
1164                     _ => cmt
1165                 }
1166             }
1167             _ => cmt
1168         };
1169
1170         match pat.node {
1171           PatKind::TupleStruct(ref qpath, ref subpats, ddpos) => {
1172             let def = self.infcx.tables.borrow().qpath_def(qpath, pat.id);
1173             let expected_len = match def {
1174                 Def::VariantCtor(def_id, CtorKind::Fn) => {
1175                     let enum_def = self.tcx().parent_def_id(def_id).unwrap();
1176                     self.tcx().lookup_adt_def(enum_def).variant_with_id(def_id).fields.len()
1177                 }
1178                 Def::StructCtor(_, CtorKind::Fn) => {
1179                     match self.pat_ty(&pat)?.sty {
1180                         ty::TyAdt(adt_def, _) => {
1181                             adt_def.struct_variant().fields.len()
1182                         }
1183                         ref ty => {
1184                             span_bug!(pat.span, "tuple struct pattern unexpected type {:?}", ty);
1185                         }
1186                     }
1187                 }
1188                 def => {
1189                     span_bug!(pat.span, "tuple struct pattern didn't resolve \
1190                                          to variant or struct {:?}", def);
1191                 }
1192             };
1193
1194             for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
1195                 let subpat_ty = self.pat_ty(&subpat)?; // see (*2)
1196                 let subcmt = self.cat_imm_interior(pat, cmt.clone(), subpat_ty,
1197                                                    InteriorField(PositionalField(i)));
1198                 self.cat_pattern_(subcmt, &subpat, op)?;
1199             }
1200           }
1201
1202           PatKind::Struct(_, ref field_pats, _) => {
1203             // {f1: p1, ..., fN: pN}
1204             for fp in field_pats {
1205                 let field_ty = self.pat_ty(&fp.node.pat)?; // see (*2)
1206                 let cmt_field = self.cat_field(pat, cmt.clone(), fp.node.name, field_ty);
1207                 self.cat_pattern_(cmt_field, &fp.node.pat, op)?;
1208             }
1209           }
1210
1211           PatKind::Binding(.., Some(ref subpat)) => {
1212               self.cat_pattern_(cmt, &subpat, op)?;
1213           }
1214
1215           PatKind::Tuple(ref subpats, ddpos) => {
1216             // (p1, ..., pN)
1217             let expected_len = match self.pat_ty(&pat)?.sty {
1218                 ty::TyTuple(ref tys, _) => tys.len(),
1219                 ref ty => span_bug!(pat.span, "tuple pattern unexpected type {:?}", ty),
1220             };
1221             for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
1222                 let subpat_ty = self.pat_ty(&subpat)?; // see (*2)
1223                 let subcmt = self.cat_imm_interior(pat, cmt.clone(), subpat_ty,
1224                                                    InteriorField(PositionalField(i)));
1225                 self.cat_pattern_(subcmt, &subpat, op)?;
1226             }
1227           }
1228
1229           PatKind::Box(ref subpat) | PatKind::Ref(ref subpat, _) => {
1230             // box p1, &p1, &mut p1.  we can ignore the mutability of
1231             // PatKind::Ref since that information is already contained
1232             // in the type.
1233             let subcmt = self.cat_deref(pat, cmt, 0)?;
1234             self.cat_pattern_(subcmt, &subpat, op)?;
1235           }
1236
1237           PatKind::Slice(ref before, ref slice, ref after) => {
1238             let context = InteriorOffsetKind::Pattern;
1239             let elt_cmt = self.cat_index(pat, cmt, context)?;
1240             for before_pat in before {
1241                 self.cat_pattern_(elt_cmt.clone(), &before_pat, op)?;
1242             }
1243             if let Some(ref slice_pat) = *slice {
1244                 self.cat_pattern_(elt_cmt.clone(), &slice_pat, op)?;
1245             }
1246             for after_pat in after {
1247                 self.cat_pattern_(elt_cmt.clone(), &after_pat, op)?;
1248             }
1249           }
1250
1251           PatKind::Path(_) | PatKind::Binding(.., None) |
1252           PatKind::Lit(..) | PatKind::Range(..) | PatKind::Wild => {
1253             // always ok
1254           }
1255         }
1256
1257         Ok(())
1258     }
1259
1260     fn overloaded_method_return_ty(&self,
1261                                    method_ty: Ty<'tcx>)
1262                                    -> Ty<'tcx>
1263     {
1264         // When we process an overloaded `*` or `[]` etc, we often
1265         // need to extract the return type of the method. These method
1266         // types are generated by method resolution and always have
1267         // all late-bound regions fully instantiated, so we just want
1268         // to skip past the binder.
1269         self.tcx().no_late_bound_regions(&method_ty.fn_ret())
1270            .unwrap()
1271     }
1272 }
1273
1274 #[derive(Clone, Debug)]
1275 pub enum Aliasability {
1276     FreelyAliasable(AliasableReason),
1277     NonAliasable,
1278     ImmutableUnique(Box<Aliasability>),
1279 }
1280
1281 #[derive(Copy, Clone, Debug)]
1282 pub enum AliasableReason {
1283     AliasableBorrowed,
1284     AliasableStatic,
1285     AliasableStaticMut,
1286 }
1287
1288 impl<'tcx> cmt_<'tcx> {
1289     pub fn guarantor(&self) -> cmt<'tcx> {
1290         //! Returns `self` after stripping away any derefs or
1291         //! interior content. The return value is basically the `cmt` which
1292         //! determines how long the value in `self` remains live.
1293
1294         match self.cat {
1295             Categorization::Rvalue(..) |
1296             Categorization::StaticItem |
1297             Categorization::Local(..) |
1298             Categorization::Deref(.., UnsafePtr(..)) |
1299             Categorization::Deref(.., BorrowedPtr(..)) |
1300             Categorization::Deref(.., Implicit(..)) |
1301             Categorization::Upvar(..) => {
1302                 Rc::new((*self).clone())
1303             }
1304             Categorization::Downcast(ref b, _) |
1305             Categorization::Interior(ref b, _) |
1306             Categorization::Deref(ref b, _, Unique) => {
1307                 b.guarantor()
1308             }
1309         }
1310     }
1311
1312     /// Returns `FreelyAliasable(_)` if this lvalue represents a freely aliasable pointer type.
1313     pub fn freely_aliasable(&self) -> Aliasability {
1314         // Maybe non-obvious: copied upvars can only be considered
1315         // non-aliasable in once closures, since any other kind can be
1316         // aliased and eventually recused.
1317
1318         match self.cat {
1319             Categorization::Deref(ref b, _, BorrowedPtr(ty::MutBorrow, _)) |
1320             Categorization::Deref(ref b, _, Implicit(ty::MutBorrow, _)) |
1321             Categorization::Deref(ref b, _, BorrowedPtr(ty::UniqueImmBorrow, _)) |
1322             Categorization::Deref(ref b, _, Implicit(ty::UniqueImmBorrow, _)) |
1323             Categorization::Deref(ref b, _, Unique) |
1324             Categorization::Downcast(ref b, _) |
1325             Categorization::Interior(ref b, _) => {
1326                 // Aliasability depends on base cmt
1327                 b.freely_aliasable()
1328             }
1329
1330             Categorization::Rvalue(..) |
1331             Categorization::Local(..) |
1332             Categorization::Upvar(..) |
1333             Categorization::Deref(.., UnsafePtr(..)) => { // yes, it's aliasable, but...
1334                 NonAliasable
1335             }
1336
1337             Categorization::StaticItem => {
1338                 if self.mutbl.is_mutable() {
1339                     FreelyAliasable(AliasableStaticMut)
1340                 } else {
1341                     FreelyAliasable(AliasableStatic)
1342                 }
1343             }
1344
1345             Categorization::Deref(_, _, BorrowedPtr(ty::ImmBorrow, _)) |
1346             Categorization::Deref(_, _, Implicit(ty::ImmBorrow, _)) => {
1347                 FreelyAliasable(AliasableBorrowed)
1348             }
1349         }
1350     }
1351
1352     // Digs down through one or two layers of deref and grabs the cmt
1353     // for the upvar if a note indicates there is one.
1354     pub fn upvar(&self) -> Option<cmt<'tcx>> {
1355         match self.note {
1356             NoteClosureEnv(..) | NoteUpvarRef(..) => {
1357                 Some(match self.cat {
1358                     Categorization::Deref(ref inner, ..) => {
1359                         match inner.cat {
1360                             Categorization::Deref(ref inner, ..) => inner.clone(),
1361                             Categorization::Upvar(..) => inner.clone(),
1362                             _ => bug!()
1363                         }
1364                     }
1365                     _ => bug!()
1366                 })
1367             }
1368             NoteNone => None
1369         }
1370     }
1371
1372
1373     pub fn descriptive_string(&self, tcx: TyCtxt) -> String {
1374         match self.cat {
1375             Categorization::StaticItem => {
1376                 "static item".to_string()
1377             }
1378             Categorization::Rvalue(..) => {
1379                 "non-lvalue".to_string()
1380             }
1381             Categorization::Local(vid) => {
1382                 if tcx.hir.is_argument(vid) {
1383                     "argument".to_string()
1384                 } else {
1385                     "local variable".to_string()
1386                 }
1387             }
1388             Categorization::Deref(.., pk) => {
1389                 let upvar = self.upvar();
1390                 match upvar.as_ref().map(|i| &i.cat) {
1391                     Some(&Categorization::Upvar(ref var)) => {
1392                         var.to_string()
1393                     }
1394                     Some(_) => bug!(),
1395                     None => {
1396                         match pk {
1397                             Implicit(..) => {
1398                                 format!("indexed content")
1399                             }
1400                             Unique => {
1401                                 format!("`Box` content")
1402                             }
1403                             UnsafePtr(..) => {
1404                                 format!("dereference of raw pointer")
1405                             }
1406                             BorrowedPtr(..) => {
1407                                 format!("borrowed content")
1408                             }
1409                         }
1410                     }
1411                 }
1412             }
1413             Categorization::Interior(_, InteriorField(NamedField(_))) => {
1414                 "field".to_string()
1415             }
1416             Categorization::Interior(_, InteriorField(PositionalField(_))) => {
1417                 "anonymous field".to_string()
1418             }
1419             Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Index,
1420                                                         VecElement)) |
1421             Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Index,
1422                                                         OtherElement)) => {
1423                 "indexed content".to_string()
1424             }
1425             Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Pattern,
1426                                                         VecElement)) |
1427             Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Pattern,
1428                                                         OtherElement)) => {
1429                 "pattern-bound indexed content".to_string()
1430             }
1431             Categorization::Upvar(ref var) => {
1432                 var.to_string()
1433             }
1434             Categorization::Downcast(ref cmt, _) => {
1435                 cmt.descriptive_string(tcx)
1436             }
1437         }
1438     }
1439 }
1440
1441 impl<'tcx> fmt::Debug for cmt_<'tcx> {
1442     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1443         write!(f, "{{{:?} id:{} m:{:?} ty:{:?}}}",
1444                self.cat,
1445                self.id,
1446                self.mutbl,
1447                self.ty)
1448     }
1449 }
1450
1451 impl<'tcx> fmt::Debug for Categorization<'tcx> {
1452     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1453         match *self {
1454             Categorization::StaticItem => write!(f, "static"),
1455             Categorization::Rvalue(r, or) => {
1456                 write!(f, "rvalue({:?}, {:?})", r, or)
1457             }
1458             Categorization::Local(id) => {
1459                let name = ty::tls::with(|tcx| tcx.local_var_name_str(id));
1460                write!(f, "local({})", name)
1461             }
1462             Categorization::Upvar(upvar) => {
1463                 write!(f, "upvar({:?})", upvar)
1464             }
1465             Categorization::Deref(ref cmt, derefs, ptr) => {
1466                 write!(f, "{:?}-{:?}{}->", cmt.cat, ptr, derefs)
1467             }
1468             Categorization::Interior(ref cmt, interior) => {
1469                 write!(f, "{:?}.{:?}", cmt.cat, interior)
1470             }
1471             Categorization::Downcast(ref cmt, _) => {
1472                 write!(f, "{:?}->(enum)", cmt.cat)
1473             }
1474         }
1475     }
1476 }
1477
1478 pub fn ptr_sigil(ptr: PointerKind) -> &'static str {
1479     match ptr {
1480         Unique => "Box",
1481         BorrowedPtr(ty::ImmBorrow, _) |
1482         Implicit(ty::ImmBorrow, _) => "&",
1483         BorrowedPtr(ty::MutBorrow, _) |
1484         Implicit(ty::MutBorrow, _) => "&mut",
1485         BorrowedPtr(ty::UniqueImmBorrow, _) |
1486         Implicit(ty::UniqueImmBorrow, _) => "&unique",
1487         UnsafePtr(_) => "*",
1488     }
1489 }
1490
1491 impl<'tcx> fmt::Debug for PointerKind<'tcx> {
1492     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1493         match *self {
1494             Unique => write!(f, "Box"),
1495             BorrowedPtr(ty::ImmBorrow, ref r) |
1496             Implicit(ty::ImmBorrow, ref r) => {
1497                 write!(f, "&{:?}", r)
1498             }
1499             BorrowedPtr(ty::MutBorrow, ref r) |
1500             Implicit(ty::MutBorrow, ref r) => {
1501                 write!(f, "&{:?} mut", r)
1502             }
1503             BorrowedPtr(ty::UniqueImmBorrow, ref r) |
1504             Implicit(ty::UniqueImmBorrow, ref r) => {
1505                 write!(f, "&{:?} uniq", r)
1506             }
1507             UnsafePtr(_) => write!(f, "*")
1508         }
1509     }
1510 }
1511
1512 impl fmt::Debug for InteriorKind {
1513     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1514         match *self {
1515             InteriorField(NamedField(fld)) => write!(f, "{}", fld),
1516             InteriorField(PositionalField(i)) => write!(f, "#{}", i),
1517             InteriorElement(..) => write!(f, "[]"),
1518         }
1519     }
1520 }
1521
1522 impl fmt::Debug for Upvar {
1523     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1524         write!(f, "{:?}/{:?}", self.id, self.kind)
1525     }
1526 }
1527
1528 impl fmt::Display for Upvar {
1529     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1530         let kind = match self.kind {
1531             ty::ClosureKind::Fn => "Fn",
1532             ty::ClosureKind::FnMut => "FnMut",
1533             ty::ClosureKind::FnOnce => "FnOnce",
1534         };
1535         write!(f, "captured outer variable in an `{}` closure", kind)
1536     }
1537 }