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.).
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
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
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.
23 //! Now, `cat_expr()` classifies the expression `Expr` and the address `A = ToAddr(Expr)`
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`.
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.
37 //! ## By-reference upvars
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:
45 //! let inc = || x += y;
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.
51 #![allow(non_camel_case_types)]
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::*;
59 use self::Aliasability::*;
61 use crate::middle::region;
62 use crate::hir::def_id::{DefId, LocalDefId};
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;
70 use crate::hir::{MutImmutable, MutMutable, PatKind};
71 use crate::hir::pat_util::EnumerateAndAdjustIterator;
73 use syntax::ast::{self, Name};
74 use syntax::symbol::sym;
79 use std::hash::{Hash, Hasher};
80 use rustc_data_structures::fx::FxIndexMap;
82 use crate::util::nodemap::ItemLocalSet;
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
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)
95 // (*1) downcast is only required if the enum has more than one variant
98 // Represents any kind of upvar
99 #[derive(Clone, Copy, PartialEq)]
102 pub kind: ty::ClosureKind
105 // different kinds of pointers:
106 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
107 pub enum PointerKind<'tcx> {
112 BorrowedPtr(ty::BorrowKind, ty::Region<'tcx>),
115 UnsafePtr(hir::Mutability),
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),
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);
131 impl PartialEq for FieldIndex {
132 fn eq(&self, rhs: &Self) -> bool {
137 impl Hash for FieldIndex {
138 fn hash<H: Hasher>(&self, h: &mut H) {
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]) { ... }`
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.
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)]
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
169 // `cmt`: "Category, Mutability, and Type".
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.
175 // *WARNING* The field `cmt.type` is NOT necessarily the same as the
176 // result of `node_type(cmt.id)`.
178 // (FIXME: rewrite the following comment given that `@x` managed
179 // pointers have been obsolete for quite some time.)
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
197 pub type cmt<'tcx> = Rc<cmt_<'tcx>>;
200 fn hir_id(&self) -> hir::HirId;
201 fn span(&self) -> Span;
204 impl HirNode for hir::Expr {
205 fn hir_id(&self) -> hir::HirId { self.hir_id }
206 fn span(&self) -> Span { self.span }
209 impl HirNode for hir::Pat {
210 fn hir_id(&self) -> hir::HirId { self.hir_id }
211 fn span(&self) -> Span { self.span }
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>>,
225 pub type McResult<T> = Result<T, ()>;
227 impl MutabilityCategory {
228 pub fn from_mutbl(m: hir::Mutability) -> MutabilityCategory {
230 MutImmutable => McImmutable,
231 MutMutable => McDeclared
233 debug!("MutabilityCategory::{}({:?}) => {:?}",
234 "from_mutbl", m, ret);
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,
244 debug!("MutabilityCategory::{}({:?}) => {:?}",
245 "from_borrow_kind", borrow_kind, ret);
249 fn from_pointer_kind(base_mutbl: MutabilityCategory,
250 ptr: PointerKind<'_>) -> MutabilityCategory {
251 let ret = match ptr {
255 BorrowedPtr(borrow_kind, _) => {
256 MutabilityCategory::from_borrow_kind(borrow_kind)
259 MutabilityCategory::from_mutbl(m)
262 debug!("MutabilityCategory::{}({:?}, {:?}) => {:?}",
263 "from_pointer_kind", base_mutbl, ptr, ret);
269 tables: &ty::TypeckTables<'_>,
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()
277 .expect("missing binding mode");
278 if bm == ty::BindByValue(hir::MutMutable) {
284 _ => span_bug!(p.span, "expected identifier pattern")
286 _ => span_bug!(tcx.hir().span(id), "expected identifier pattern")
288 debug!("MutabilityCategory::{}(tcx, id={:?}) => {:?}",
289 "from_local", id, ret);
293 pub fn inherit(&self) -> MutabilityCategory {
294 let ret = match *self {
295 McImmutable => McImmutable,
296 McDeclared => McInherited,
297 McInherited => McInherited,
299 debug!("{:?}.inherit() => {:?}", self, ret);
303 pub fn is_mutable(&self) -> bool {
304 let ret = match *self {
305 McImmutable => false,
309 debug!("{:?}.is_mutable() => {:?}", self, ret);
313 pub fn is_immutable(&self) -> bool {
314 let ret = match *self {
316 McDeclared | McInherited => false
318 debug!("{:?}.is_immutable() => {:?}", self, ret);
322 pub fn to_user_str(&self) -> &'static str {
324 McDeclared | McInherited => "mutable",
325 McImmutable => "immutable",
330 impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
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 {
341 upvars: tcx.upvars(body_owner),
344 rvalue_promotable_map,
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:
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.
361 infcx: &'a InferCtxt<'a, 'tcx>,
363 region_scope_tree: &'a region::ScopeTree,
364 tables: &'a ty::TypeckTables<'tcx>,
365 ) -> MemCategorizationContext<'a, 'tcx> {
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;
374 MemCategorizationContext {
377 upvars: tcx.upvars(body_owner),
380 rvalue_promotable_map,
385 pub fn type_is_copy_modulo_regions(
387 param_env: ty::ParamEnv<'tcx>,
391 self.infcx.map(|infcx| infcx.type_is_copy_modulo_regions(param_env, ty, span))
393 if (param_env, ty).has_local_value() {
396 Some(ty.is_copy_modulo_regions(self.tcx, param_env, span))
402 fn resolve_vars_if_possible<T>(&self, value: &T) -> T
403 where T: TypeFoldable<'tcx>
405 self.infcx.map(|infcx| infcx.resolve_vars_if_possible(value))
406 .unwrap_or_else(|| value.clone())
409 fn is_tainted_by_errors(&self) -> bool {
410 self.infcx.map_or(false, |infcx| infcx.is_tainted_by_errors())
413 fn resolve_type_vars_or_error(&self,
415 ty: Option<Ty<'tcx>>)
416 -> McResult<Ty<'tcx>> {
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);
428 None if self.is_tainted_by_errors() => Err(()),
430 bug!("no type for node {}: {} in mem_categorization",
431 id, self.tcx.hir().node_to_string(id));
436 pub fn node_ty(&self,
438 -> McResult<Ty<'tcx>> {
439 self.resolve_type_vars_or_error(hir_id,
440 self.tables.node_type_opt(hir_id))
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))
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))
451 /// Returns the type of value that this pattern matches against.
452 /// Some non-obvious cases:
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);
473 self.pat_ty_unadjusted(pat)
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);
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
489 .expect("missing binding mode");
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) {
498 debug!("By-ref binding of non-derefable type {:?}", base_ty);
508 debug!("pat_ty(pat={:?}) ret_ty={:?}", pat, ret_ty);
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.
517 mc: &MemCategorizationContext<'a, 'tcx>,
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)
529 helper(self, expr, self.tables.expr_adjustments(expr))
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)
539 fn cat_expr_adjusted_with<F>(&self, expr: &hir::Expr,
541 adjustment: &adjustment::Adjustment<'tcx>)
542 -> McResult<cmt_<'tcx>>
543 where F: FnOnce() -> McResult<cmt_<'tcx>>
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 {
555 self.cat_rvalue_node(expr.hir_id, expr.span, ref_ty)
559 self.cat_deref(expr, base, NoteNone)
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))
571 pub fn cat_expr_unadjusted(&self, expr: &hir::Expr) -> McResult<cmt_<'tcx>> {
572 debug!("cat_expr: id={} expr={:?}", expr.hir_id, expr);
574 let expr_ty = self.expr_ty(expr)?;
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)
580 let base_cmt = Rc::new(self.cat_expr(&e_base)?);
581 self.cat_deref(expr, base_cmt, NoteNone)
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={:?}",
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))
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
602 self.cat_overloaded_place(expr, base, NoteIndex)
604 let base_cmt = Rc::new(self.cat_expr(&base)?);
605 self.cat_index(expr, base_cmt, expr_ty, InteriorOffsetKind::Index)
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)
614 hir::ExprKind::Type(ref e, _) => {
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))
634 pub fn cat_res(&self,
639 -> McResult<cmt_<'tcx>> {
640 debug!("cat_res: id={:?} expr={:?} def={:?}",
641 hir_id, expr_ty, 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))
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)[..]
659 .any(|attr| attr.check_name(sym::thread_local));
661 let cat = if is_thread_local {
662 let re = self.temporary_scope(hir_id.local_id);
663 Categorization::ThreadLocal(re)
665 Categorization::StaticItem
672 mutbl: match self.tcx.static_mutability(def_id).unwrap() {
673 hir::MutImmutable => McImmutable,
674 hir::MutMutable => McDeclared,
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)
688 cat: Categorization::Local(var_id),
689 mutbl: MutabilityCategory::from_local(self.tcx, self.tables, var_id),
696 def => span_bug!(span, "unexpected definition in memory categorization: {:?}", def)
700 // Categorize an upvar, complete with invisible derefs of closure
701 // environment and upvar reference as appropriate.
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.
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
722 // This results in the following table for concrete closure
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
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),
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) => {
740 // During upvar inference we may not know the
741 // closure kind, just use the LATTICE_BOTTOM value.
743 infcx.closure_kind(closure_def_id, closure_substs)
744 .unwrap_or(ty::ClosureKind::LATTICE_BOTTOM),
747 closure_substs.closure_kind(closure_def_id, self.tcx.global_tcx()),
750 _ => span_bug!(span, "unexpected type for fn in mem_categorization: {:?}", ty),
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(),
758 let var_ty = self.node_ty(var_id)?;
760 // Mutability of original variable itself
761 let var_mutbl = MutabilityCategory::from_local(self.tcx, self.tables, var_id);
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_ {
769 cat: Categorization::Upvar(Upvar {id: upvar_id, kind: kind}),
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
778 let cmt_result = match kind {
779 ty::ClosureKind::FnOnce => {
782 ty::ClosureKind::FnMut => {
783 self.env_deref(hir_id, span, upvar_id, var_mutbl, ty::MutBorrow, cmt_result)
785 ty::ClosureKind::Fn => {
786 self.env_deref(hir_id, span, upvar_id, var_mutbl, ty::ImmBorrow, cmt_result)
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
793 let upvar_capture = self.tables.upvar_capture(upvar_id);
794 let cmt_result = match upvar_capture {
795 ty::UpvarCapture::ByValue => {
798 ty::UpvarCapture::ByRef(upvar_borrow) => {
799 let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
803 cat: Categorization::Deref(Rc::new(cmt_result), ptr),
804 mutbl: MutabilityCategory::from_borrow_kind(upvar_borrow.kind),
806 note: NoteUpvarRef(upvar_id)
811 let ret = cmt_result;
812 debug!("cat_upvar ret={:?}", ret);
819 upvar_id: ty::UpvarId,
820 upvar_mutbl: MutabilityCategory,
821 env_borrow_kind: ty::BorrowKind,
822 cmt_result: cmt_<'tcx>)
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
830 scope: upvar_id.closure_expr_id.to_def_id(),
831 bound_region: ty::BrEnv
834 let env_ptr = BorrowedPtr(env_borrow_kind, env_region);
836 let var_ty = cmt_result.ty;
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
845 let cmt_result = cmt_ {
847 ty: self.tcx.types.err,
851 let mut deref_mutbl = MutabilityCategory::from_borrow_kind(env_borrow_kind);
853 // Issue #18335. If variable is declared as immutable, override the
854 // mutability from the environment and substitute an `&T` anyway.
856 McImmutable => { deref_mutbl = McImmutable; }
857 McDeclared | McInherited => { }
863 cat: Categorization::Deref(Rc::new(cmt_result), env_ptr),
866 note: NoteClosureEnv(upvar_id)
869 debug!("env_deref ret {:?}", ret);
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),
884 pub fn cat_rvalue_node(&self,
889 debug!("cat_rvalue_node(id={:?}, span={:?}, expr_ty={:?})",
890 hir_id, span, expr_ty);
892 let promotable = self.rvalue_promotable_map.as_ref().map(|m| m.contains(&hir_id.local_id))
895 debug!("cat_rvalue_node: promotable = {:?}", promotable);
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,
903 debug!("cat_rvalue_node: promotable = {:?} (2)", promotable);
905 // Compute maximum lifetime of this rvalue. This is 'static if
906 // we can promote to a constant, otherwise equal to enclosing temp
908 let re = if promotable {
909 self.tcx.lifetimes.re_static
911 self.temporary_scope(hir_id.local_id)
913 let ret = self.cat_rvalue(hir_id, span, re, expr_ty);
914 debug!("cat_rvalue_node ret {:?}", ret);
918 pub fn cat_rvalue(&self,
919 cmt_hir_id: hir::HirId,
921 temp_scope: ty::Region<'tcx>,
922 expr_ty: Ty<'tcx>) -> cmt_<'tcx> {
926 cat:Categorization::Rvalue(temp_scope),
931 debug!("cat_rvalue ret {:?}", ret);
935 pub fn cat_field<N: HirNode>(&self,
943 hir_id: node.hir_id(),
945 mutbl: base_cmt.mutbl.inherit(),
946 cat: Categorization::Interior(base_cmt,
947 InteriorField(FieldIndex(f_index, f_ident.name))),
951 debug!("cat_field ret {:?}", ret);
955 fn cat_overloaded_place(
960 ) -> McResult<cmt_<'tcx>> {
961 debug!("cat_overloaded_place(expr={:?}, base={:?}, note={:?})",
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)?;
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")
976 let ref_ty = self.tcx.mk_ref(region, ty::TypeAndMut {
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)
990 ) -> McResult<cmt_<'tcx>> {
991 debug!("cat_deref: base_cmt={:?}", base_cmt);
993 let base_cmt_ty = base_cmt.ty;
994 let deref_ty = match base_cmt_ty.builtin_deref(true) {
997 debug!("explicit deref of non-derefable type: {:?}", base_cmt_ty);
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);
1009 _ => bug!("unexpected type in cat_deref: {:?}", base_cmt.ty)
1012 hir_id: node.hir_id(),
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),
1020 debug!("cat_deref ret {:?}", ret);
1024 fn cat_index<N: HirNode>(&self,
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 (`[]`).
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.
1044 //! - `elt`: the HIR node being indexed
1045 //! - `base_cmt`: the cmt of `elt`
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);
1053 pub fn cat_imm_interior<N:HirNode>(&self,
1055 base_cmt: cmt<'tcx>,
1056 interior_ty: Ty<'tcx>,
1057 interior: InteriorKind)
1060 hir_id: node.hir_id(),
1062 mutbl: base_cmt.mutbl.inherit(),
1063 cat: Categorization::Interior(base_cmt, interior),
1067 debug!("cat_imm_interior ret={:?}", ret);
1071 pub fn cat_downcast_if_needed<N:HirNode>(&self,
1073 base_cmt: 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(),
1083 mutbl: base_cmt.mutbl.inherit(),
1084 cat: Categorization::Downcast(base_cmt, variant_did),
1088 debug!("cat_downcast ret={:?}", ret);
1091 debug!("cat_downcast univariant={:?}", base_cmt);
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),
1099 self.cat_pattern_(cmt, pat, &mut op)
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)
1106 // Here, `cmt` is the categorization for the value being
1107 // matched and pat is the pattern it is being matched against.
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.
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.
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,
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:
1131 // (FIXME: `@@3` is not legal code anymore!)
1138 // In this case, the cmt and the relevant ids would be:
1140 // CMT Id Type of Id Type of cmt
1143 // ^~~~~~~^ `x` from discr @@int @@int
1144 // ^~~~~~~~~~^ `@@y` pattern node @@int @int
1145 // ^~~~~~~~~~~~~^ `@y` pattern node @int int
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.
1153 debug!("cat_pattern(pat={:?}, cmt={:?})", pat, cmt);
1155 // If (pattern) adjustments are active for this pattern, adjust the `cmt` correspondingly.
1156 // `cmt`s are constructed differently from patterns. For example, in
1160 // &&Some(x, ) => { ... },
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?
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
1174 // The above example has no adjustments. If the code were instead the (after adjustments,
1175 // equivalent) version
1179 // Some(x, ) => { ... },
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
1193 debug!("cat_pattern: applying adjustment to cmt={:?}", cmt);
1194 cmt = Rc::new(self.cat_deref(pat, cmt, NoteNone)?);
1196 let cmt = cmt; // lose mutability
1197 debug!("cat_pattern: applied adjustment derefs to get cmt={:?}", cmt);
1199 // Invoke the callback, but only now, after the `cmt` has adjusted.
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);
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 {
1215 debug!("access to unresolvable pattern {:?}", pat);
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())
1225 Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _)
1226 | Res::SelfCtor(..) => {
1227 let ty = self.pat_ty_unadjusted(&pat)?;
1229 ty::Adt(adt_def, _) => {
1230 (cmt, adt_def.non_enum_variant().fields.len())
1234 "tuple struct pattern unexpected type {:?}", ty);
1240 "tuple struct pattern didn't resolve to variant or struct {:?} at {:?}",
1244 self.tcx.sess.delay_span_bug(pat.span, &format!(
1245 "tuple struct pattern didn't resolve to variant or struct {:?}",
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)?;
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 {
1266 debug!("access to unresolvable pattern {:?}", pat);
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)
1273 Res::Def(DefKind::Variant, variant_did) => {
1274 self.cat_downcast_if_needed(pat, cmt, variant_did)
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)?;
1288 PatKind::Binding(.., Some(ref subpat)) => {
1289 self.cat_pattern_(cmt, &subpat, op)?;
1292 PatKind::Tuple(ref subpats, ddpos) => {
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),
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)?;
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
1312 let subcmt = Rc::new(self.cat_deref(pat, cmt, NoteNone)?);
1313 self.cat_pattern_(subcmt, &subpat, op)?;
1316 PatKind::Slice(ref before, ref slice, ref after) => {
1317 let element_ty = match cmt.ty.builtin_index() {
1320 debug!("explicit index of non-indexable type {:?}", cmt);
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)?;
1329 if let Some(ref slice_pat) = *slice {
1330 self.cat_pattern_(elt_cmt.clone(), &slice_pat, op)?;
1332 for after_pat in after {
1333 self.cat_pattern_(elt_cmt.clone(), &after_pat, op)?;
1337 PatKind::Path(_) | PatKind::Binding(.., None) |
1338 PatKind::Lit(..) | PatKind::Range(..) | PatKind::Wild => {
1347 #[derive(Clone, Debug)]
1348 pub enum Aliasability {
1349 FreelyAliasable(AliasableReason),
1351 ImmutableUnique(Box<Aliasability>),
1354 #[derive(Copy, Clone, Debug)]
1355 pub enum AliasableReason {
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.
1368 Categorization::Rvalue(..) |
1369 Categorization::StaticItem |
1370 Categorization::ThreadLocal(..) |
1371 Categorization::Local(..) |
1372 Categorization::Deref(_, UnsafePtr(..)) |
1373 Categorization::Deref(_, BorrowedPtr(..)) |
1374 Categorization::Upvar(..) => {
1377 Categorization::Downcast(ref b, _) |
1378 Categorization::Interior(ref b, _) |
1379 Categorization::Deref(ref b, Unique) => {
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.
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()
1401 Categorization::Rvalue(..) |
1402 Categorization::ThreadLocal(..) |
1403 Categorization::Local(..) |
1404 Categorization::Upvar(..) |
1405 Categorization::Deref(_, UnsafePtr(..)) => { // yes, it's aliasable, but...
1409 Categorization::StaticItem => {
1410 if self.mutbl.is_mutable() {
1411 FreelyAliasable(AliasableStaticMut)
1413 FreelyAliasable(AliasableStatic)
1417 Categorization::Deref(_, BorrowedPtr(ty::ImmBorrow, _)) => {
1418 FreelyAliasable(AliasableBorrowed)
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
1426 pub fn upvar_cat(&self) -> Option<&Categorization<'tcx>> {
1428 NoteClosureEnv(..) | NoteUpvarRef(..) => {
1429 Some(match self.cat {
1430 Categorization::Deref(ref inner, _) => {
1432 Categorization::Deref(ref inner, _) => &inner.cat,
1433 Categorization::Upvar(..) => &inner.cat,
1440 NoteIndex | NoteNone => None
1444 pub fn descriptive_string(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
1446 Categorization::StaticItem => {
1447 "static item".into()
1449 Categorization::ThreadLocal(..) => {
1450 "thread-local static item".into()
1452 Categorization::Rvalue(..) => {
1455 Categorization::Local(vid) => {
1456 if tcx.hir().is_argument(vid) {
1462 Categorization::Deref(_, pk) => {
1463 match self.upvar_cat() {
1464 Some(&Categorization::Upvar(ref var)) => {
1465 var.to_string().into()
1474 "dereference of raw pointer"
1476 BorrowedPtr(..) => {
1478 NoteIndex => "indexed content",
1479 _ => "borrowed content"
1486 Categorization::Interior(_, InteriorField(..)) => {
1489 Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Index)) => {
1490 "indexed content".into()
1492 Categorization::Interior(_, InteriorElement(InteriorOffsetKind::Pattern)) => {
1493 "pattern-bound indexed content".into()
1495 Categorization::Upvar(ref var) => {
1496 var.to_string().into()
1498 Categorization::Downcast(ref cmt, _) => {
1499 cmt.descriptive_string(tcx).into()
1505 pub fn ptr_sigil(ptr: PointerKind<'_>) -> &'static str {
1508 BorrowedPtr(ty::ImmBorrow, _) => "&",
1509 BorrowedPtr(ty::MutBorrow, _) => "&mut",
1510 BorrowedPtr(ty::UniqueImmBorrow, _) => "&unique",
1511 UnsafePtr(_) => "*",
1515 impl fmt::Debug for InteriorKind {
1516 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1518 InteriorField(FieldIndex(_, info)) => write!(f, "{}", info),
1519 InteriorElement(..) => write!(f, "[]"),
1524 impl fmt::Debug for Upvar {
1525 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1526 write!(f, "{:?}/{:?}", self.id, self.kind)
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",
1537 write!(f, "captured outer variable in an `{}` closure", kind)