1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 * The job of the categorization module is to analyze an expression to
15 * determine what kind of memory is used in evaluating it (for example,
16 * where dereferences occur and what kind of pointer is dereferenced;
17 * whether the memory is mutable; etc)
19 * Categorization effectively transforms all of our expressions into
20 * expressions of the following forms (the actual enum has many more
21 * possibilities, naturally, but they are all variants of these base
24 * E = rvalue // some computed rvalue
25 * | x // address of a local variable or argument
26 * | *E // deref of a ptr
27 * | E.comp // access to an interior component
29 * Imagine a routine ToAddr(Expr) that evaluates an expression and returns an
30 * address where the result is to be found. If Expr is an lvalue, then this
31 * is the address of the lvalue. If Expr is an rvalue, this is the address of
32 * some temporary spot in memory where the result is stored.
34 * Now, cat_expr() classies the expression Expr and the address A=ToAddr(Expr)
37 * - cat: what kind of expression was this? This is a subset of the
38 * full expression forms which only includes those that we care about
39 * for the purpose of the analysis.
40 * - mutbl: mutability of the address A
41 * - ty: the type of data found at the address A
43 * The resulting categorization tree differs somewhat from the expressions
44 * themselves. For example, auto-derefs are explicit. Also, an index a[b] is
45 * decomposed into two operations: a derefence to reach the array data and
46 * then an index to jump forward to the relevant item.
48 * ## By-reference upvars
50 * One part of the translation which may be non-obvious is that we translate
51 * closure upvars into the dereference of a borrowed pointer; this more closely
52 * resembles the runtime translation. So, for example, if we had:
56 * let inc = || x += y;
58 * Then when we categorize `x` (*within* the closure) we would yield a
59 * result of `*x'`, effectively, where `x'` is a `cat_upvar` reference
60 * tied to `x`. The type of `x'` will be a borrowed pointer.
65 use util::ppaux::{ty_to_str, region_ptr_to_str, Repr};
67 use syntax::ast::{MutImmutable, MutMutable};
69 use syntax::codemap::Span;
70 use syntax::print::pprust;
71 use syntax::parse::token;
74 pub enum categorization {
75 cat_rvalue(ty::Region), // temporary val, argument is its scope
77 cat_copied_upvar(CopiedUpvar), // upvar copied into @fn or ~fn env
78 cat_upvar(ty::UpvarId, ty::UpvarBorrow), // by ref upvar from stack closure
79 cat_local(ast::NodeId), // local variable
80 cat_arg(ast::NodeId), // formal argument
81 cat_deref(cmt, uint, PointerKind), // deref of a ptr
82 cat_interior(cmt, InteriorKind), // something interior: field, tuple, etc
83 cat_downcast(cmt), // selects a particular enum variant (*1)
84 cat_discr(cmt, ast::NodeId), // match discriminant (see preserve())
86 // (*1) downcast is only required if the enum has more than one variant
90 pub struct CopiedUpvar {
91 upvar_id: ast::NodeId,
92 onceness: ast::Onceness,
95 // different kinds of pointers:
96 #[deriving(Eq, IterBytes)]
97 pub enum PointerKind {
100 BorrowedPtr(ty::BorrowKind, ty::Region),
101 UnsafePtr(ast::Mutability),
104 // We use the term "interior" to mean "something reachable from the
105 // base without a pointer dereference", e.g. a field
106 #[deriving(Eq, IterBytes)]
107 pub enum InteriorKind {
108 InteriorField(FieldName),
109 InteriorElement(ElementKind),
112 #[deriving(Eq, IterBytes)]
114 NamedField(ast::Name),
115 PositionalField(uint)
118 #[deriving(Eq, IterBytes)]
119 pub enum ElementKind {
125 #[deriving(Eq, IterBytes)]
126 pub enum MutabilityCategory {
127 McImmutable, // Immutable.
128 McDeclared, // Directly declared as mutable.
129 McInherited, // Inherited from the fact that owner is mutable.
132 // `cmt`: "Category, Mutability, and Type".
134 // a complete categorization of a value indicating where it originated
135 // and how it is located, as well as the mutability of the memory in
136 // which the value is stored.
138 // *WARNING* The field `cmt.type` is NOT necessarily the same as the
139 // result of `node_id_to_type(cmt.id)`. This is because the `id` is
140 // always the `id` of the node producing the type; in an expression
141 // like `*x`, the type of this deref node is the deref'd type (`T`),
142 // but in a pattern like `@x`, the `@x` pattern is again a
143 // dereference, but its type is the type *before* the dereference
144 // (`@T`). So use `cmt.type` to find the type of the value in a consistent
145 // fashion. For more details, see the method `cat_pattern`
148 id: ast::NodeId, // id of expr/pat producing this value
149 span: Span, // span of same expr/pat
150 cat: categorization, // categorization of expr
151 mutbl: MutabilityCategory, // mutability of expr as lvalue
152 ty: ty::t // type of the expr (*see WARNING above*)
155 pub type cmt = @cmt_;
157 // We pun on *T to mean both actual deref of a ptr as well
158 // as accessing of components:
159 pub enum deref_kind {
160 deref_ptr(PointerKind),
161 deref_interior(InteriorKind),
164 // Categorizes a derefable type. Note that we include vectors and strings as
165 // derefable (we model an index as the combination of a deref and then a
166 // pointer adjustment).
167 pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
168 match ty::get(t).sty {
170 ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
171 ty::ty_vec(_, ty::vstore_uniq) |
172 ty::ty_str(ty::vstore_uniq) |
173 ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
174 Some(deref_ptr(OwnedPtr))
178 ty::ty_vec(mt, ty::vstore_slice(r)) => {
179 let kind = ty::BorrowKind::from_mutbl(mt.mutbl);
180 Some(deref_ptr(BorrowedPtr(kind, r)))
183 ty::ty_trait(_, _, ty::RegionTraitStore(r), m, _) => {
184 let kind = ty::BorrowKind::from_mutbl(m);
185 Some(deref_ptr(BorrowedPtr(kind, r)))
188 ty::ty_str(ty::vstore_slice(r)) |
189 ty::ty_closure(ty::ClosureTy {sigil: ast::BorrowedSigil,
191 Some(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
195 Some(deref_ptr(GcPtr))
198 ty::ty_ptr(ref mt) => {
199 Some(deref_ptr(UnsafePtr(mt.mutbl)))
203 ty::ty_struct(..) => { // newtype
204 Some(deref_interior(InteriorField(PositionalField(0))))
207 ty::ty_vec(_, ty::vstore_fixed(_)) |
208 ty::ty_str(ty::vstore_fixed(_)) => {
209 Some(deref_interior(InteriorElement(element_kind(t))))
216 pub fn deref_kind(tcx: ty::ctxt, t: ty::t) -> deref_kind {
217 match opt_deref_kind(t) {
221 format!("deref_cat() invoked on non-derefable type {}",
228 fn id(&self) -> ast::NodeId;
229 fn span(&self) -> Span;
232 impl ast_node for ast::Expr {
233 fn id(&self) -> ast::NodeId { self.id }
234 fn span(&self) -> Span { self.span }
237 impl ast_node for ast::Pat {
238 fn id(&self) -> ast::NodeId { self.id }
239 fn span(&self) -> Span { self.span }
242 pub struct MemCategorizationContext<TYPER> {
246 pub type McResult<T> = Result<T, ()>;
249 * The `Typer` trait provides the interface for the mem-categorization
250 * module to the results of the type check. It can be used to query
251 * the type assigned to an expression node, to inquire after adjustments,
254 * This interface is needed because mem-categorization is used from
255 * two places: `regionck` and `borrowck`. `regionck` executes before
256 * type inference is complete, and hence derives types and so on from
257 * intermediate tables. This also implies that type errors can occur,
258 * and hence `node_ty()` and friends return a `Result` type -- any
259 * error will propagate back up through the mem-categorization
262 * In the borrow checker, in contrast, type checking is complete and we
263 * know that no errors have occurred, so we simply consult the tcx and we
264 * can be sure that only `Ok` results will occur.
267 fn tcx(&self) -> ty::ctxt;
268 fn node_ty(&mut self, id: ast::NodeId) -> McResult<ty::t>;
269 fn adjustment(&mut self, node_id: ast::NodeId) -> Option<@ty::AutoAdjustment>;
270 fn is_method_call(&mut self, id: ast::NodeId) -> bool;
271 fn temporary_scope(&mut self, rvalue_id: ast::NodeId) -> Option<ast::NodeId>;
272 fn upvar_borrow(&mut self, upvar_id: ty::UpvarId) -> ty::UpvarBorrow;
275 impl ToStr for MutabilityCategory {
276 fn to_str(&self) -> ~str {
277 format!("{:?}", *self)
281 impl MutabilityCategory {
282 pub fn from_mutbl(m: ast::Mutability) -> MutabilityCategory {
284 MutImmutable => McImmutable,
285 MutMutable => McDeclared
289 pub fn from_borrow_kind(borrow_kind: ty::BorrowKind) -> MutabilityCategory {
291 ty::ImmBorrow => McImmutable,
292 ty::UniqueImmBorrow => McImmutable,
293 ty::MutBorrow => McDeclared,
297 pub fn from_pointer_kind(base_mutbl: MutabilityCategory,
298 ptr: PointerKind) -> MutabilityCategory {
303 BorrowedPtr(borrow_kind, _) => {
304 MutabilityCategory::from_borrow_kind(borrow_kind)
310 MutabilityCategory::from_mutbl(m)
315 pub fn inherit(&self) -> MutabilityCategory {
317 McImmutable => McImmutable,
318 McDeclared => McInherited,
319 McInherited => McInherited,
323 pub fn is_mutable(&self) -> bool {
325 McImmutable => false,
331 pub fn is_immutable(&self) -> bool {
334 McDeclared | McInherited => false
338 pub fn to_user_str(&self) -> &'static str {
340 McDeclared | McInherited => "mutable",
341 McImmutable => "immutable",
350 Err(e) => { return Err(e); }
355 impl<TYPER:Typer> MemCategorizationContext<TYPER> {
356 fn tcx(&self) -> ty::ctxt {
360 fn adjustment(&mut self, id: ast::NodeId) -> Option<@ty::AutoAdjustment> {
361 self.typer.adjustment(id)
364 fn expr_ty(&mut self, expr: &ast::Expr) -> McResult<ty::t> {
365 self.typer.node_ty(expr.id)
368 fn expr_ty_adjusted(&mut self, expr: &ast::Expr) -> McResult<ty::t> {
369 let unadjusted_ty = if_ok!(self.expr_ty(expr));
370 let adjustment = self.adjustment(expr.id);
371 Ok(ty::adjust_ty(self.tcx(), expr.span, unadjusted_ty, adjustment))
374 fn node_ty(&mut self, id: ast::NodeId) -> McResult<ty::t> {
375 self.typer.node_ty(id)
378 fn pat_ty(&mut self, pat: @ast::Pat) -> McResult<ty::t> {
379 self.typer.node_ty(pat.id)
382 pub fn cat_expr(&mut self, expr: &ast::Expr) -> McResult<cmt> {
383 match self.adjustment(expr.id) {
386 self.cat_expr_unadjusted(expr)
389 Some(adjustment) => {
391 ty::AutoObject(..) => {
392 // Implicity casts a concrete object to trait object
393 // so just patch up the type
394 let expr_ty = if_ok!(self.expr_ty_adjusted(expr));
395 let expr_cmt = if_ok!(self.cat_expr_unadjusted(expr));
396 Ok(@cmt_ {ty: expr_ty, ..*expr_cmt})
399 ty::AutoAddEnv(..) => {
400 // Convert a bare fn to a closure by adding NULL env.
401 // Result is an rvalue.
402 let expr_ty = if_ok!(self.expr_ty_adjusted(expr));
403 Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
408 autoref: Some(_), ..}) => {
409 // Equivalent to &*expr or something similar.
410 // Result is an rvalue.
411 let expr_ty = if_ok!(self.expr_ty_adjusted(expr));
412 Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
417 autoref: None, autoderefs: autoderefs}) => {
418 // Equivalent to *expr or something similar.
419 self.cat_expr_autoderefd(expr, autoderefs)
426 pub fn cat_expr_autoderefd(&mut self, expr: &ast::Expr, autoderefs: uint)
428 let mut cmt = if_ok!(self.cat_expr_unadjusted(expr));
429 for deref in range(1u, autoderefs + 1) {
430 cmt = self.cat_deref(expr, cmt, deref);
435 pub fn cat_expr_unadjusted(&mut self, expr: &ast::Expr) -> McResult<cmt> {
436 debug!("cat_expr: id={} expr={}", expr.id, expr.repr(self.tcx()));
438 let expr_ty = if_ok!(self.expr_ty(expr));
440 ast::ExprUnary(_, ast::UnDeref, e_base) => {
441 if self.typer.is_method_call(expr.id) {
442 return Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty));
445 let base_cmt = if_ok!(self.cat_expr(e_base));
446 Ok(self.cat_deref(expr, base_cmt, 0))
449 ast::ExprField(base, f_name, _) => {
450 // Method calls are now a special syntactic form,
451 // so `a.b` should always be a field.
452 assert!(!self.typer.is_method_call(expr.id));
454 let base_cmt = if_ok!(self.cat_expr(base));
455 Ok(self.cat_field(expr, base_cmt, f_name, expr_ty))
458 ast::ExprIndex(_, base, _) => {
459 if self.typer.is_method_call(expr.id) {
460 return Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty));
463 let base_cmt = if_ok!(self.cat_expr(base));
464 Ok(self.cat_index(expr, base_cmt, 0))
467 ast::ExprPath(_) => {
468 let def_map = self.tcx().def_map.borrow();
469 let def = def_map.get().get_copy(&expr.id);
470 self.cat_def(expr.id, expr.span, expr_ty, def)
473 ast::ExprParen(e) => self.cat_expr_unadjusted(e),
475 ast::ExprAddrOf(..) | ast::ExprCall(..) |
476 ast::ExprAssign(..) | ast::ExprAssignOp(..) |
477 ast::ExprFnBlock(..) | ast::ExprProc(..) | ast::ExprRet(..) |
479 ast::ExprMethodCall(..) | ast::ExprCast(..) | ast::ExprVstore(..) |
480 ast::ExprVec(..) | ast::ExprTup(..) | ast::ExprIf(..) |
481 ast::ExprLogLevel | ast::ExprBinary(..) | ast::ExprWhile(..) |
482 ast::ExprBlock(..) | ast::ExprLoop(..) | ast::ExprMatch(..) |
483 ast::ExprLit(..) | ast::ExprBreak(..) | ast::ExprMac(..) |
484 ast::ExprAgain(..) | ast::ExprStruct(..) | ast::ExprRepeat(..) |
485 ast::ExprInlineAsm(..) | ast::ExprBox(..) => {
486 Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty))
489 ast::ExprForLoop(..) => fail!("non-desugared expr_for_loop")
493 pub fn cat_def(&mut self,
499 debug!("cat_def: id={} expr={}",
500 id, expr_ty.repr(self.tcx()));
503 ast::DefStruct(..) | ast::DefVariant(..) => {
504 Ok(self.cat_rvalue_node(id, span, expr_ty))
506 ast::DefFn(..) | ast::DefStaticMethod(..) | ast::DefMod(_) |
507 ast::DefForeignMod(_) | ast::DefStatic(_, false) |
508 ast::DefUse(_) | ast::DefTrait(_) | ast::DefTy(_) | ast::DefPrimTy(_) |
509 ast::DefTyParam(..) | ast::DefTyParamBinder(..) | ast::DefRegion(_) |
510 ast::DefLabel(_) | ast::DefSelfTy(..) | ast::DefMethod(..) => {
520 ast::DefStatic(_, true) => {
530 ast::DefArg(vid, binding_mode) => {
531 // Idea: make this could be rewritten to model by-ref
532 // stuff as `&const` and `&mut`?
534 // m: mutability of the argument
535 let m = match binding_mode {
536 ast::BindByValue(ast::MutMutable) => McDeclared,
548 ast::DefUpvar(var_id, _, fn_node_id, _) => {
549 let ty = if_ok!(self.node_ty(fn_node_id));
550 match ty::get(ty).sty {
551 ty::ty_closure(ref closure_ty) => {
552 // Decide whether to use implicit reference or by copy/move
553 // capture for the upvar. This, combined with the onceness,
554 // determines whether the closure can move out of it.
555 let var_is_refd = match (closure_ty.sigil, closure_ty.onceness) {
556 // Many-shot stack closures can never move out.
557 (ast::BorrowedSigil, ast::Many) => true,
558 // 1-shot stack closures can move out.
559 (ast::BorrowedSigil, ast::Once) => false,
560 // Heap closures always capture by copy/move, and can
561 // move out if they are once.
562 (ast::OwnedSigil, _) |
563 (ast::ManagedSigil, _) => false,
567 self.cat_upvar(id, span, var_id, fn_node_id)
569 // FIXME #2152 allow mutation of moved upvars
573 cat:cat_copied_upvar(CopiedUpvar {
575 onceness: closure_ty.onceness}),
582 self.tcx().sess.span_bug(
584 format!("Upvar of non-closure {} - {}",
585 fn_node_id, ty.repr(self.tcx())));
590 ast::DefLocal(vid, binding_mode) |
591 ast::DefBinding(vid, binding_mode) => {
592 // by-value/by-ref bindings are local variables
593 let m = match binding_mode {
594 ast::BindByValue(ast::MutMutable) => McDeclared,
609 fn cat_upvar(&mut self,
613 fn_node_id: ast::NodeId)
616 * Upvars through a closure are in fact indirect
617 * references. That is, when a closure refers to a
618 * variable from a parent stack frame like `x = 10`,
619 * that is equivalent to `*x_ = 10` where `x_` is a
620 * borrowed pointer (`&mut x`) created when the closure
621 * was created and store in the environment. This
622 * equivalence is expose in the mem-categorization.
625 let upvar_id = ty::UpvarId { var_id: var_id,
626 closure_expr_id: fn_node_id };
628 let upvar_borrow = self.typer.upvar_borrow(upvar_id);
630 let var_ty = if_ok!(self.node_ty(var_id));
632 // We can't actually represent the types of all upvars
633 // as user-describable types, since upvars support const
634 // and unique-imm borrows! Therefore, we cheat, and just
635 // give err type. Nobody should be inspecting this type anyhow.
636 let upvar_ty = ty::mk_err();
638 let base_cmt = @cmt_ {
641 cat:cat_upvar(upvar_id, upvar_borrow),
646 let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
648 let deref_cmt = @cmt_ {
651 cat:cat_deref(base_cmt, 0, ptr),
652 mutbl:MutabilityCategory::from_borrow_kind(upvar_borrow.kind),
659 pub fn cat_rvalue_node(&mut self,
664 match self.typer.temporary_scope(id) {
666 self.cat_rvalue(id, span, ty::ReScope(scope), expr_ty)
669 self.cat_rvalue(id, span, ty::ReStatic, expr_ty)
674 pub fn cat_rvalue(&mut self,
677 temp_scope: ty::Region,
678 expr_ty: ty::t) -> cmt {
682 cat:cat_rvalue(temp_scope),
688 /// inherited mutability: used in cases where the mutability of a
689 /// component is inherited from the base it is a part of. For
690 /// example, a record field is mutable if it is declared mutable
691 /// or if the container is mutable.
692 pub fn inherited_mutability(&mut self,
693 base_m: MutabilityCategory,
694 interior_m: ast::Mutability)
695 -> MutabilityCategory {
697 MutImmutable => base_m.inherit(),
698 MutMutable => McDeclared
702 pub fn cat_field<N:ast_node>(&mut self,
711 cat: cat_interior(base_cmt, InteriorField(NamedField(f_name.name))),
712 mutbl: base_cmt.mutbl.inherit(),
717 pub fn cat_deref_fn_or_obj<N:ast_node>(&mut self,
722 // Bit of a hack: the "dereference" of a function pointer like
723 // `@fn()` is a mere logical concept. We interpret it as
724 // dereferencing the environment pointer; of course, we don't
725 // know what type lies at the other end, so we just call it
726 // `()` (the empty tuple).
728 let opaque_ty = ty::mk_tup(self.tcx(), ~[]);
729 return self.cat_deref_common(node, base_cmt, deref_cnt, opaque_ty);
732 pub fn cat_deref<N:ast_node>(&mut self,
737 let mt = match ty::deref(base_cmt.ty, true) {
740 self.tcx().sess.span_bug(
742 format!("Explicit deref of non-derefable type: {}",
743 base_cmt.ty.repr(self.tcx())));
747 return self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty);
750 pub fn cat_deref_common<N:ast_node>(&mut self,
756 match deref_kind(self.tcx(), base_cmt.ty) {
758 // for unique ptrs, we inherit mutability from the
760 let m = MutabilityCategory::from_pointer_kind(base_cmt.mutbl,
766 cat:cat_deref(base_cmt, deref_cnt, ptr),
772 deref_interior(interior) => {
773 let m = base_cmt.mutbl.inherit();
777 cat:cat_interior(base_cmt, interior),
785 pub fn cat_index<N:ast_node>(&mut self,
790 //! Creates a cmt for an indexing operation (`[]`); this
791 //! indexing operation may occurs as part of an
792 //! AutoBorrowVec, which when converting a `~[]` to an `&[]`
793 //! effectively takes the address of the 0th element.
795 //! One subtle aspect of indexing that may not be
796 //! immediately obvious: for anything other than a fixed-length
797 //! vector, an operation like `x[y]` actually consists of two
798 //! disjoint (from the point of view of borrowck) operations.
799 //! The first is a deref of `x` to create a pointer `p` that points
800 //! at the first element in the array. The second operation is
801 //! an index which adds `y*sizeof(T)` to `p` to obtain the
802 //! pointer to `x[y]`. `cat_index` will produce a resulting
803 //! cmt containing both this deref and the indexing,
804 //! presuming that `base_cmt` is not of fixed-length type.
806 //! In the event that a deref is needed, the "deref count"
807 //! is taken from the parameter `derefs`. See the comment
808 //! on the def'n of `root_map_key` in borrowck/mod.rs
809 //! for more details about deref counts; the summary is
810 //! that `derefs` should be 0 for an explicit indexing
811 //! operation and N+1 for an indexing that is part of
812 //! an auto-adjustment, where N is the number of autoderefs
813 //! in that adjustment.
816 //! - `elt`: the AST node being indexed
817 //! - `base_cmt`: the cmt of `elt`
818 //! - `derefs`: the deref number to be used for
819 //! the implicit index deref, if any (see above)
821 let element_ty = match ty::index(base_cmt.ty) {
822 Some(ref mt) => mt.ty,
824 self.tcx().sess.span_bug(
826 format!("Explicit index of non-index type `{}`",
827 base_cmt.ty.repr(self.tcx())));
831 return match deref_kind(self.tcx(), base_cmt.ty) {
833 // for unique ptrs, we inherit mutability from the
835 let m = MutabilityCategory::from_pointer_kind(base_cmt.mutbl, ptr);
837 // the deref is explicit in the resulting cmt
838 let deref_cmt = @cmt_ {
841 cat:cat_deref(base_cmt, derefs, ptr),
846 interior(elt, deref_cmt, base_cmt.ty, m.inherit(), element_ty)
849 deref_interior(_) => {
850 // fixed-length vectors have no deref
851 let m = base_cmt.mutbl.inherit();
852 interior(elt, base_cmt, base_cmt.ty, m, element_ty)
856 fn interior<N: ast_node>(elt: &N,
859 mutbl: MutabilityCategory,
860 element_ty: ty::t) -> cmt
865 cat:cat_interior(of_cmt, InteriorElement(element_kind(vec_ty))),
872 pub fn cat_slice_pattern(&mut self,
874 slice_pat: @ast::Pat)
875 -> McResult<(cmt, ast::Mutability, ty::Region)> {
877 * Given a pattern P like: `[_, ..Q, _]`, where `vec_cmt` is
878 * the cmt for `P`, `slice_pat` is the pattern `Q`, returns:
880 * - the mutability and region of the slice `Q`
882 * These last two bits of info happen to be things that
886 let slice_ty = if_ok!(self.node_ty(slice_pat.id));
887 let (slice_mutbl, slice_r) = vec_slice_info(self.tcx(),
890 let cmt_slice = self.cat_index(slice_pat, vec_cmt, 0);
891 return Ok((cmt_slice, slice_mutbl, slice_r));
893 fn vec_slice_info(tcx: ty::ctxt,
896 -> (ast::Mutability, ty::Region) {
898 * In a pattern like [a, b, ..c], normally `c` has slice type,
899 * but if you have [a, b, ..ref c], then the type of `ref c`
900 * will be `&&[]`, so to extract the slice details we have
901 * to recurse through rptrs.
904 match ty::get(slice_ty).sty {
905 ty::ty_vec(slice_mt, ty::vstore_slice(slice_r)) => {
906 (slice_mt.mutbl, slice_r)
909 ty::ty_rptr(_, ref mt) => {
910 vec_slice_info(tcx, pat, mt.ty)
916 format!("Type of slice pattern is not a slice"));
922 pub fn cat_imm_interior<N:ast_node>(&mut self,
926 interior: InteriorKind)
931 cat: cat_interior(base_cmt, interior),
932 mutbl: base_cmt.mutbl.inherit(),
937 pub fn cat_downcast<N:ast_node>(&mut self,
945 cat: cat_downcast(base_cmt),
946 mutbl: base_cmt.mutbl.inherit(),
951 pub fn cat_pattern(&mut self,
954 op: |&mut MemCategorizationContext<TYPER>,
958 // Here, `cmt` is the categorization for the value being
959 // matched and pat is the pattern it is being matched against.
961 // In general, the way that this works is that we walk down
962 // the pattern, constructing a cmt that represents the path
963 // that will be taken to reach the value being matched.
965 // When we encounter named bindings, we take the cmt that has
966 // been built up and pass it off to guarantee_valid() so that
967 // we can be sure that the binding will remain valid for the
968 // duration of the arm.
970 // (*2) There is subtlety concerning the correspondence between
971 // pattern ids and types as compared to *expression* ids and
972 // types. This is explained briefly. on the definition of the
973 // type `cmt`, so go off and read what it says there, then
974 // come back and I'll dive into a bit more detail here. :) OK,
977 // In general, the id of the cmt should be the node that
978 // "produces" the value---patterns aren't executable code
979 // exactly, but I consider them to "execute" when they match a
980 // value, and I consider them to produce the value that was
981 // matched. So if you have something like:
988 // In this case, the cmt and the relevant ids would be:
990 // CMT Id Type of Id Type of cmt
993 // ^~~~~~~^ `x` from discr @@int @@int
994 // ^~~~~~~~~~^ `@@y` pattern node @@int @int
995 // ^~~~~~~~~~~~~^ `@y` pattern node @int int
997 // You can see that the types of the id and the cmt are in
998 // sync in the first line, because that id is actually the id
999 // of an expression. But once we get to pattern ids, the types
1000 // step out of sync again. So you'll see below that we always
1001 // get the type of the *subpattern* and use that.
1003 let tcx = self.tcx();
1004 debug!("cat_pattern: id={} pat={} cmt={}",
1005 pat.id, pprust::pat_to_str(pat),
1011 ast::PatWild | ast::PatWildMulti => {
1015 ast::PatEnum(_, None) => {
1018 ast::PatEnum(_, Some(ref subpats)) => {
1019 let def_map = self.tcx().def_map.borrow();
1020 match def_map.get().find(&pat.id) {
1021 Some(&ast::DefVariant(enum_did, _, _)) => {
1024 let downcast_cmt = {
1025 if ty::enum_is_univariant(self.tcx(), enum_did) {
1026 cmt // univariant, no downcast needed
1028 self.cat_downcast(pat, cmt, cmt.ty)
1032 for (i, &subpat) in subpats.iter().enumerate() {
1033 let subpat_ty = if_ok!(self.pat_ty(subpat)); // see (*2)
1036 self.cat_imm_interior(
1037 pat, downcast_cmt, subpat_ty,
1038 InteriorField(PositionalField(i)));
1040 if_ok!(self.cat_pattern(subcmt, subpat, |x,y,z| op(x,y,z)));
1043 Some(&ast::DefFn(..)) |
1044 Some(&ast::DefStruct(..)) => {
1045 for (i, &subpat) in subpats.iter().enumerate() {
1046 let subpat_ty = if_ok!(self.pat_ty(subpat)); // see (*2)
1048 self.cat_imm_interior(
1049 pat, cmt, subpat_ty,
1050 InteriorField(PositionalField(i)));
1051 if_ok!(self.cat_pattern(cmt_field, subpat, |x,y,z| op(x,y,z)));
1054 Some(&ast::DefStatic(..)) => {
1055 for &subpat in subpats.iter() {
1056 if_ok!(self.cat_pattern(cmt, subpat, |x,y,z| op(x,y,z)));
1060 self.tcx().sess.span_bug(
1062 "enum pattern didn't resolve to enum or struct");
1067 ast::PatIdent(_, _, Some(subpat)) => {
1068 if_ok!(self.cat_pattern(cmt, subpat, op));
1071 ast::PatIdent(_, _, None) => {
1072 // nullary variant or identifier: ignore
1075 ast::PatStruct(_, ref field_pats, _) => {
1076 // {f1: p1, ..., fN: pN}
1077 for fp in field_pats.iter() {
1078 let field_ty = if_ok!(self.pat_ty(fp.pat)); // see (*2)
1079 let cmt_field = self.cat_field(pat, cmt, fp.ident, field_ty);
1080 if_ok!(self.cat_pattern(cmt_field, fp.pat, |x,y,z| op(x,y,z)));
1084 ast::PatTup(ref subpats) => {
1086 for (i, &subpat) in subpats.iter().enumerate() {
1087 let subpat_ty = if_ok!(self.pat_ty(subpat)); // see (*2)
1089 self.cat_imm_interior(
1090 pat, cmt, subpat_ty,
1091 InteriorField(PositionalField(i)));
1092 if_ok!(self.cat_pattern(subcmt, subpat, |x,y,z| op(x,y,z)));
1096 ast::PatUniq(subpat) | ast::PatRegion(subpat) => {
1098 let subcmt = self.cat_deref(pat, cmt, 0);
1099 if_ok!(self.cat_pattern(subcmt, subpat, op));
1102 ast::PatVec(ref before, slice, ref after) => {
1103 let elt_cmt = self.cat_index(pat, cmt, 0);
1104 for &before_pat in before.iter() {
1105 if_ok!(self.cat_pattern(elt_cmt, before_pat, |x,y,z| op(x,y,z)));
1107 for &slice_pat in slice.iter() {
1108 let slice_ty = if_ok!(self.pat_ty(slice_pat));
1109 let slice_cmt = self.cat_rvalue_node(pat.id(), pat.span(), slice_ty);
1110 if_ok!(self.cat_pattern(slice_cmt, slice_pat, |x,y,z| op(x,y,z)));
1112 for &after_pat in after.iter() {
1113 if_ok!(self.cat_pattern(elt_cmt, after_pat, |x,y,z| op(x,y,z)));
1117 ast::PatLit(_) | ast::PatRange(_, _) => {
1125 pub fn mut_to_str(&mut self, mutbl: ast::Mutability) -> ~str {
1127 MutMutable => ~"mutable",
1128 MutImmutable => ~"immutable"
1132 pub fn cmt_to_str(&self, cmt: cmt) -> ~str {
1134 cat_static_item => {
1137 cat_copied_upvar(_) => {
1138 ~"captured outer variable in a heap closure"
1149 cat_deref(base, _, pk) => {
1152 format!("captured outer variable")
1155 format!("dereference of `{}`-pointer", ptr_sigil(pk))
1159 cat_interior(_, InteriorField(NamedField(_))) => {
1162 cat_interior(_, InteriorField(PositionalField(_))) => {
1165 cat_interior(_, InteriorElement(VecElement)) => {
1168 cat_interior(_, InteriorElement(StrElement)) => {
1171 cat_interior(_, InteriorElement(OtherElement)) => {
1175 ~"captured outer variable"
1177 cat_discr(cmt, _) => {
1178 self.cmt_to_str(cmt)
1180 cat_downcast(cmt) => {
1181 self.cmt_to_str(cmt)
1186 pub fn region_to_str(&self, r: ty::Region) -> ~str {
1187 region_ptr_to_str(self.tcx(), r)
1191 /// The node_id here is the node of the expression that references the field.
1192 /// This function looks it up in the def map in case the type happens to be
1193 /// an enum to determine which variant is in use.
1194 pub fn field_mutbl(tcx: ty::ctxt,
1196 // FIXME #6993: change type to Name
1198 node_id: ast::NodeId)
1199 -> Option<ast::Mutability> {
1200 // Need to refactor so that struct/enum fields can be treated uniformly.
1201 match ty::get(base_ty).sty {
1202 ty::ty_struct(did, _) => {
1203 let r = ty::lookup_struct_fields(tcx, did);
1204 for fld in r.iter() {
1205 if fld.name == f_name.name {
1206 return Some(ast::MutImmutable);
1210 ty::ty_enum(..) => {
1211 let def_map = tcx.def_map.borrow();
1212 match def_map.get().get_copy(&node_id) {
1213 ast::DefVariant(_, variant_id, _) => {
1214 let r = ty::lookup_struct_fields(tcx, variant_id);
1215 for fld in r.iter() {
1216 if fld.name == f_name.name {
1217 return Some(ast::MutImmutable);
1230 pub enum AliasableReason {
1239 pub fn guarantor(self) -> cmt {
1240 //! Returns `self` after stripping away any owned pointer derefs or
1241 //! interior content. The return value is basically the `cmt` which
1242 //! determines how long the value in `self` remains live.
1247 cat_copied_upvar(..) |
1250 cat_deref(_, _, UnsafePtr(..)) |
1251 cat_deref(_, _, GcPtr(..)) |
1252 cat_deref(_, _, BorrowedPtr(..)) |
1258 cat_interior(b, _) |
1259 cat_deref(b, _, OwnedPtr) => {
1265 pub fn freely_aliasable(&self) -> Option<AliasableReason> {
1267 * Returns `Some(_)` if this lvalue represents a freely aliasable
1271 // Maybe non-obvious: copied upvars can only be considered
1272 // non-aliasable in once closures, since any other kind can be
1273 // aliased and eventually recused.
1276 cat_deref(b, _, BorrowedPtr(ty::MutBorrow, _)) |
1277 cat_deref(b, _, BorrowedPtr(ty::UniqueImmBorrow, _)) |
1279 cat_deref(b, _, OwnedPtr) |
1280 cat_interior(b, _) |
1281 cat_discr(b, _) => {
1282 // Aliasability depends on base cmt
1283 b.freely_aliasable()
1286 cat_copied_upvar(CopiedUpvar {onceness: ast::Once, ..}) |
1291 cat_deref(_, _, UnsafePtr(..)) => { // yes, it's aliasable, but...
1295 cat_copied_upvar(CopiedUpvar {onceness: ast::Many, ..}) => {
1296 Some(AliasableOther)
1299 cat_static_item(..) => {
1300 if self.mutbl.is_mutable() {
1301 Some(AliasableStaticMut)
1303 Some(AliasableStatic)
1307 cat_deref(_, _, GcPtr) => {
1308 Some(AliasableManaged)
1311 cat_deref(_, _, BorrowedPtr(ty::ImmBorrow, _)) => {
1312 Some(AliasableBorrowed)
1318 impl Repr for cmt_ {
1319 fn repr(&self, tcx: ty::ctxt) -> ~str {
1320 format!("\\{{} id:{} m:{:?} ty:{}\\}",
1328 impl Repr for categorization {
1329 fn repr(&self, tcx: ty::ctxt) -> ~str {
1333 cat_copied_upvar(..) |
1337 format!("{:?}", *self)
1339 cat_deref(cmt, derefs, ptr) => {
1340 format!("{}-{}{}->",
1345 cat_interior(cmt, interior) => {
1350 cat_downcast(cmt) => {
1351 format!("{}->(enum)", cmt.cat.repr(tcx))
1353 cat_discr(cmt, _) => {
1360 pub fn ptr_sigil(ptr: PointerKind) -> &'static str {
1364 BorrowedPtr(ty::ImmBorrow, _) => "&",
1365 BorrowedPtr(ty::MutBorrow, _) => "&mut",
1366 BorrowedPtr(ty::UniqueImmBorrow, _) => "&unique",
1371 impl Repr for InteriorKind {
1372 fn repr(&self, _tcx: ty::ctxt) -> ~str {
1374 InteriorField(NamedField(fld)) => {
1375 token::get_name(fld).get().to_str()
1377 InteriorField(PositionalField(i)) => format!("\\#{:?}", i),
1378 InteriorElement(_) => ~"[]",
1383 fn element_kind(t: ty::t) -> ElementKind {
1384 match ty::get(t).sty {
1385 ty::ty_vec(..) => VecElement,
1386 ty::ty_str(..) => StrElement,