Enum rustc_middle::mir::syntax::MutBorrowKind
source · pub enum MutBorrowKind {
Default,
TwoPhaseBorrow,
ClosureCapture,
}
Variants§
Default
TwoPhaseBorrow
This borrow arose from method-call auto-ref. (i.e., adjustment::Adjust::Borrow
)
ClosureCapture
Data must be immutable but not aliasable. This kind of borrow cannot currently be expressed by the user and is used only in implicit closure bindings. It is needed when the closure is borrowing or mutating a mutable referent, e.g.:
let mut z = 3;
let x: &mut isize = &mut z;
let y = || *x += 5;
If we were to try to translate this closure into a more explicit form, we’d encounter an error with the code as written:
struct Env<'a> { x: &'a &'a mut isize }
let mut z = 3;
let x: &mut isize = &mut z;
let y = (&mut Env { x: &x }, fn_ptr); // Closure is pair of env and fn
fn fn_ptr(env: &mut Env) { **env.x += 5; }
This is then illegal because you cannot mutate an &mut
found
in an aliasable location. To solve, you’d have to translate with
an &mut
borrow:
struct Env<'a> { x: &'a mut &'a mut isize }
let mut z = 3;
let x: &mut isize = &mut z;
let y = (&mut Env { x: &mut x }, fn_ptr); // changed from &x to &mut x
fn fn_ptr(env: &mut Env) { **env.x += 5; }
Now the assignment to **env.x
is legal, but creating a
mutable pointer to x
is not because x
is not mutable. We
could fix this by declaring x
as let mut x
. This is ok in
user code, if awkward, but extra weird for closures, since the
borrow is hidden.
So we introduce a ClosureCapture
borrow – user will not have to mark the variable
containing the mutable reference as mut
, as they didn’t ever
intend to mutate the mutable reference itself. We still mutable capture it in order to
mutate the pointed value through it (but not mutating the reference itself).
This solves the problem. For simplicity, we don’t give users the way to express this borrow, it’s just used when translating closures.
Trait Implementations§
source§impl Clone for MutBorrowKind
impl Clone for MutBorrowKind
source§fn clone(&self) -> MutBorrowKind
fn clone(&self) -> MutBorrowKind
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for MutBorrowKind
impl Debug for MutBorrowKind
source§impl Hash for MutBorrowKind
impl Hash for MutBorrowKind
source§impl<'__ctx> HashStable<StableHashingContext<'__ctx>> for MutBorrowKind
impl<'__ctx> HashStable<StableHashingContext<'__ctx>> for MutBorrowKind
fn hash_stable( &self, __hcx: &mut StableHashingContext<'__ctx>, __hasher: &mut StableHasher )
source§impl Ord for MutBorrowKind
impl Ord for MutBorrowKind
source§fn cmp(&self, other: &MutBorrowKind) -> Ordering
fn cmp(&self, other: &MutBorrowKind) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl PartialEq<MutBorrowKind> for MutBorrowKind
impl PartialEq<MutBorrowKind> for MutBorrowKind
source§fn eq(&self, other: &MutBorrowKind) -> bool
fn eq(&self, other: &MutBorrowKind) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<MutBorrowKind> for MutBorrowKind
impl PartialOrd<MutBorrowKind> for MutBorrowKind
source§fn partial_cmp(&self, other: &MutBorrowKind) -> Option<Ordering>
fn partial_cmp(&self, other: &MutBorrowKind) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Copy for MutBorrowKind
impl Eq for MutBorrowKind
impl StructuralEq for MutBorrowKind
impl StructuralPartialEq for MutBorrowKind
Auto Trait Implementations§
impl RefUnwindSafe for MutBorrowKind
impl Send for MutBorrowKind
impl Sync for MutBorrowKind
impl Unpin for MutBorrowKind
impl UnwindSafe for MutBorrowKind
Blanket Implementations§
source§impl<'tcx, T> ArenaAllocatable<'tcx, IsCopy> for Twhere
T: Copy,
impl<'tcx, T> ArenaAllocatable<'tcx, IsCopy> for Twhere T: Copy,
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut T
fn allocate_from_iter<'a>( arena: &'a Arena<'tcx>, iter: impl IntoIterator<Item = T> ) -> &'a mut [T]
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T, R> CollectAndApply<T, R> for T
impl<T, R> CollectAndApply<T, R> for T
source§impl<Tcx, T> DepNodeParams<Tcx> for Twhere
Tcx: DepContext,
T: for<'a> HashStable<StableHashingContext<'a>> + Debug,
impl<Tcx, T> DepNodeParams<Tcx> for Twhere Tcx: DepContext, T: for<'a> HashStable<StableHashingContext<'a>> + Debug,
default fn fingerprint_style() -> FingerprintStyle
source§default fn to_fingerprint(&self, tcx: Tcx) -> Fingerprint
default fn to_fingerprint(&self, tcx: Tcx) -> Fingerprint
default fn to_debug_str(&self, _: Tcx) -> String
source§default fn recover(_: Tcx, _: &DepNode) -> Option<T>
default fn recover(_: Tcx, _: &DepNode) -> Option<T>
DepNode
,
something which is needed when forcing DepNode
s during red-green
evaluation. The query system will only call this method if
fingerprint_style()
is not FingerprintStyle::Opaque
.
It is always valid to return None
here, in which case incremental
compilation will treat the query as having changed instead of forcing it.source§impl<P> IntoQueryParam<P> for P
impl<P> IntoQueryParam<P> for P
fn into_query_param(self) -> P
source§impl<T> MaybeResult<T> for T
impl<T> MaybeResult<T> for T
source§impl<'tcx, T> ToPredicate<'tcx, T> for T
impl<'tcx, T> ToPredicate<'tcx, T> for T
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> T
source§impl<Tcx, T> Value<Tcx> for Twhere
Tcx: DepContext,
impl<Tcx, T> Value<Tcx> for Twhere Tcx: DepContext,
default fn from_cycle_error( tcx: Tcx, cycle: &[QueryInfo], _guar: ErrorGuaranteed ) -> T
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 1 byte
Size for each variant:
Default
: 0 bytesTwoPhaseBorrow
: 0 bytesClosureCapture
: 0 bytes