Expand description
§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 BorrowKind for details) and then
“escalating” the kind as needed. The borrow kind proceeds according to
the following lattice:
ty::ImmBorrow -> ty::UniqueImmBorrow -> ty::MutBorrowSo, 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_for_non_move_closure (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 the way ExprUseVisitor is computing
Places. In particular, it will query the current borrow kind as it
goes, 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 ExprUseVisitor returns
within Places, 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.)
Modules§
- expr_
use_ 🔒visitor - A different sort of visitor for walking fn bodies. Unlike the
normal visitor, which just walks the entire body in one shot, the
ExprUseVisitordetermines how expressions are being used.
Structs§
Enums§
- Borrow
Kind - Place
Ancestry 🔒Relation - Describe the relationship between the paths of two places eg:
- Upvar
Args 🔒
Functions§
- adjust_
for_ 🔒move_ closure - Truncate deref of any reference.
- adjust_
for_ 🔒non_ move_ closure - Adjust closure capture just that if taking ownership of data, only move data from enclosing stack frame.
- analyze_
coroutine_ 🔒closure_ captures - apply_
capture_ 🔒kind_ on_ capture_ ty - Returns a Ty that applies the specified capture kind on the provided capture Ty
- child_
prefix_ 🔒matches_ parent_ projections - determine_
capture_ 🔒info - At the end,
capture_info_awill contain the selected info. - determine_
capture_ 🔒sources - determine_
place_ 🔒ancestry_ relation - Determines the Ancestry relationship of Place A relative to Place B
- enable_
precise_ 🔒capture - Precise capture is enabled if user is using Rust Edition 2021 or higher.
spanis the span of the closure. - restrict_
capture_ 🔒precision - Truncate projections so that the following rules are obeyed by the captured
place: - restrict_
precision_ 🔒for_ drop_ types - Rust doesn’t permit moving fields out of a type that implements drop
- restrict_
precision_ 🔒for_ unsafe - Truncate
placeso that anunsafeblock isn’t required to capture it. - restrict_
repr_ 🔒packed_ field_ ref_ capture - Truncate the capture so that the place being borrowed is in accordance with RFC 1240,
which states that it’s unsafe to take a reference into a struct marked
repr(packed). - should_
reborrow_ 🔒from_ env_ of_ parent_ coroutine_ closure - Determines whether a child capture that is derived from a parent capture should be borrowed with the lifetime of the parent coroutine-closure’s env.
- truncate_
capture_ 🔒for_ optimization - Reduces the precision of the captured place when the precision doesn’t yield any benefit from borrow checking perspective, allowing us to save us on the size of the capture.
- truncate_
place_ 🔒to_ len_ and_ update_ capture_ kind - Truncates
placeto have up tolenprojections.curr_modeis the current required capture kind for the place. Returns the truncatedplaceand the updated required capture kind.
Type Aliases§
- Inferred
Capture 🔒Information - Intermediate format to store a captured
Placeand associatedCaptureInfoduring capture analysis. Information in this map feeds into the minimum capture analysis pass.