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