Module rustc::middle::mem_categorization[][src]

🔬 This is a nightly-only experimental API. (rustc_private)

this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via Cargo.toml instead?

Categorization

The job of the categorization module is to analyze an expression to determine what kind of memory is used in evaluating it (for example, where dereferences occur and what kind of pointer is dereferenced; whether the memory is mutable; etc)

Categorization effectively transforms all of our expressions into expressions of the following forms (the actual enum has many more possibilities, naturally, but they are all variants of these base forms):

E = rvalue    // some computed rvalue
  | x         // address of a local variable or argument
  | *E        // deref of a ptr
  | E.comp    // access to an interior component

Imagine a routine ToAddr(Expr) that evaluates an expression and returns an address where the result is to be found. If Expr is a place, then this is the address of the place. If Expr is an rvalue, this is the address of some temporary spot in memory where the result is stored.

Now, cat_expr() classifies the expression Expr and the address A=ToAddr(Expr) as follows:

The resulting categorization tree differs somewhat from the expressions themselves. For example, auto-derefs are explicit. Also, an index a[b] is decomposed into two operations: a dereference to reach the array data and then an index to jump forward to the relevant item.

By-reference upvars

One part of the translation which may be non-obvious is that we translate closure upvars into the dereference of a borrowed pointer; this more closely resembles the runtime translation. So, for example, if we had:

let mut x = 3;
let y = 5;
let inc = || x += y;

Then when we categorize x (within the closure) we would yield a result of *x', effectively, where x' is a Categorization::Upvar reference tied to x. The type of x' will be a borrowed pointer.

Re-exports

pub use self::PointerKind::*;
pub use self::InteriorKind::*;
pub use self::MutabilityCategory::*;
pub use self::AliasableReason::*;
pub use self::Note::*;
use self::Aliasability::*;
use middle::region;
use hir::def_id::DefId;
use hir::def_id::LocalDefId;
use hir::map as hir_map;
use infer::InferCtxt;
use hir::def::Def;
use hir::def::CtorKind;
use ty::adjustment;
use ty;
use ty::Ty;
use ty::TyCtxt;
use ty::fold::TypeFoldable;
use hir::MutImmutable;
use hir::MutMutable;
use hir::PatKind;
use hir::pat_util::EnumerateAndAdjustIterator;
use hir;
use syntax::ast;
use syntax::ast::Name;
use syntax_pos::Span;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use rustc_data_structures::sync::Lrc;
use std::rc::Rc;
use util::nodemap::ItemLocalSet;

Structs

FieldIndex [
Experimental
]
MemCategorizationContext [
Experimental
]
Upvar [
Experimental
]
cmt_ [
Experimental
]

Enums

Aliasability [
Experimental
]
AliasableReason [
Experimental
]
Categorization [
Experimental
]
ImmutabilityBlame [
Experimental
]
InteriorKind [
Experimental
]
InteriorOffsetKind [
Experimental
]
MutabilityCategory [
Experimental
]
Note [
Experimental
]
PointerKind [
Experimental
]

Traits

ast_node [
Experimental
]

Functions

ptr_sigil [
Experimental
]

Type Definitions

McResult [
Experimental
]
cmt [
Experimental
]