Module rustc_typeck::check::upvar[][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?

Inferring borrow kinds for upvars

Whenever there is a closure expression, we need to determine how each upvar is used. We do this by initially assigning each upvar an immutable "borrow kind" (see ty::BorrowKind for details) and then "escalating" the kind as needed. The borrow kind proceeds according to the following lattice:

ty::ImmBorrow -> ty::UniqueImmBorrow -> ty::MutBorrow

So, for example, if we see an assignment x = 5 to an upvar x, we will promote its borrow kind to mutable borrow. If we see an &mut x we'll do the same. Naturally, this applies not just to the upvar, but to everything owned by x, so the result is the same for something like x.f = 5 and so on (presuming x is not a borrowed pointer to a struct). These adjustments are performed in adjust_upvar_borrow_kind() (you can trace backwards through the code from there).

The fact that we are inferring borrow kinds as we go results in a semi-hacky interaction with mem-categorization. In particular, mem-categorization will query the current borrow kind as it categorizes, and we'll return the current value, but this may get adjusted later. Therefore, in this module, we generally ignore the borrow kind (and derived mutabilities) that are returned from mem-categorization, since they may be inaccurate. (Another option would be to use a unification scheme, where instead of returning a concrete borrow kind like ty::ImmBorrow, we return a ty::InferBorrow(upvar_id) or something like that, but this would then mean that all later passes would have to check for these figments and report an error, and it just seems like more mess in the end.)

Re-exports

use super::FnCtxt;
use middle::expr_use_visitor as euv;
use middle::mem_categorization as mc;
use middle::mem_categorization::Categorization;
use rustc::hir::def_id::DefId;
use rustc::ty;
use rustc::ty::Ty;
use rustc::ty::TyCtxt;
use rustc::ty::UpvarSubsts;
use rustc::infer::UpvarRegion;
use syntax::ast;
use syntax_pos::Span;
use rustc::hir;
use rustc::hir::def_id::LocalDefId;
use rustc::hir::intravisit;
use rustc::hir::intravisit::NestedVisitorMap;
use rustc::hir::intravisit::Visitor;

Structs

InferBorrowKind [
Experimental
]
InferBorrowKindVisitor [
Experimental
]

Functions

var_name [
Experimental
]