Enum FakeReadCause

Source
pub enum FakeReadCause {
    ForMatchGuard,
    ForMatchedPlace(Option<LocalDefId>),
    ForGuardBinding,
    ForLet(Option<LocalDefId>),
    ForIndex,
}
Expand description

The FakeReadCause describes the type of pattern why a FakeRead statement exists.

Variants§

§

ForMatchGuard

A fake read injected into a match guard to ensure that the discriminants that are being matched on aren’t modified while the match guard is being evaluated.

At the beginning of each match guard, a fake borrow is inserted for each discriminant accessed in the entire match statement.

Then, at the end of the match guard, a FakeRead(ForMatchGuard) is inserted to keep the fake borrows alive until that point.

This should ensure that you cannot change the variant for an enum while you are in the midst of matching on it.

§

ForMatchedPlace(Option<LocalDefId>)

Fake read of the scrutinee of a match or destructuring let (i.e. let with non-trivial pattern).

In match x { ... }, we generate a FakeRead(ForMatchedPlace, x) and insert it into the otherwise_block (which is supposed to be unreachable for irrefutable pattern-matches like match or let).

This is necessary because let x: !; match x {} doesn’t generate any actual read of x, so we need to generate a FakeRead to check that it is initialized.

If the FakeRead(ForMatchedPlace) is being performed with a closure that doesn’t capture the required upvars, the FakeRead within the closure is omitted entirely.

To make sure that this is still sound, if a closure matches against a Place starting with an Upvar, we hoist the FakeRead to the definition point of the closure.

If the FakeRead comes from being hoisted out of a closure like this, we record the LocalDefId of the closure. Otherwise, the Option will be None.

§

ForGuardBinding

A fake read injected into a match guard to ensure that the places bound by the pattern are immutable for the duration of the match guard.

Within a match guard, references are created for each place that the pattern creates a binding for — this is known as the RefWithinGuard version of the variables. To make sure that the references stay alive until the end of the match guard, and properly prevent the places in question from being modified, a FakeRead(ForGuardBinding) is inserted at the end of the match guard.

For details on how these references are created, see the extensive documentation on bind_matched_candidate_for_guard in rustc_mir_build.

§

ForLet(Option<LocalDefId>)

Officially, the semantics of

let pattern = <expr>;

is that <expr> is evaluated into a temporary and then this temporary is into the pattern.

However, if we see the simple pattern let var = <expr>, we optimize this to evaluate <expr> directly into the variable var. This is mostly unobservable, but in some cases it can affect the borrow checker, as in #53695.

Therefore, we insert a FakeRead(ForLet) immediately after each let with a trivial pattern.

FIXME: ExprUseVisitor has an entirely different opinion on what FakeRead(ForLet) is supposed to mean. If it was accurate to what MIR lowering does, would it even make sense to hoist these out of closures like ForMatchedPlace?

§

ForIndex

Currently, index expressions overloaded through the Index trait get lowered differently than index expressions with builtin semantics for arrays and slices — the latter will emit code to perform bound checks, and then return a MIR place that will only perform the indexing “for real” when it gets incorporated into an instruction.

This is observable in the fact that the following compiles:

fn f(x: &mut [&mut [u32]], i: usize) {
    x[i][x[i].len() - 1] += 1;
}

However, we need to be careful to not let the user invalidate the bound check with an expression like

(*x)[1][{ x = y; 4}]

Here, the first bounds check would be invalidated when we evaluate the second index expression. To make sure that this doesn’t happen, we create a fake borrow of x and hold it while we evaluate the second index.

This borrow is kept alive by a FakeRead(ForIndex) at the end of its scope.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.