pub enum RegionKind<I: Interner> {
    ReEarlyBound(I::EarlyBoundRegion),
    ReLateBound(DebruijnIndex, I::BoundRegion),
    ReFree(I::FreeRegion),
    ReStatic,
    ReVar(I::RegionVid),
    RePlaceholder(I::PlaceholderRegion),
    ReErased,
    ReError(I::ErrorGuaranteed),
}
Expand description

Representation of regions. Note that the NLL checker uses a distinct representation of regions. For this reason, it internally replaces all the regions with inference variables – the index of the variable is then used to index into internal NLL data structures. See rustc_const_eval::borrow_check module for more information.

Note: operations are on the wrapper Region type, which is interned, rather than this type.

The Region lattice within a given function

In general, the region lattice looks like

static ----------+-----...------+       (greatest)
|                |              |
early-bound and  |              |
free regions     |              |
|                |              |
|                |              |
empty(root)   placeholder(U1)   |
|            /                  |
|           /         placeholder(Un)
empty(U1) --         /
|                   /
...                /
|                 /
empty(Un) --------                      (smallest)

Early-bound/free regions are the named lifetimes in scope from the function declaration. They have relationships to one another determined based on the declared relationships from the function.

Note that inference variables and bound regions are not included in this diagram. In the case of inference variables, they should be inferred to some other region from the diagram. In the case of bound regions, they are excluded because they don’t make sense to include – the diagram indicates the relationship between free regions.

Inference variables

During region inference, we sometimes create inference variables, represented as ReVar. These will be inferred by the code in infer::lexical_region_resolve to some free region from the lattice above (the minimal region that meets the constraints).

During NLL checking, where regions are defined differently, we also use ReVar – in that case, the index is used to index into the NLL region checker’s data structures. The variable may in fact represent either a free region or an inference variable, in that case.

Bound Regions

These are regions that are stored behind a binder and must be substituted with some concrete region before being used. There are two kind of bound regions: early-bound, which are bound in an item’s Generics, and are substituted by an GenericArgs, and late-bound, which are part of higher-ranked types (e.g., for<'a> fn(&'a ())), and are substituted by the likes of liberate_late_bound_regions. The distinction exists because higher-ranked lifetimes aren’t supported in all places. See 1.

Unlike Params, bound regions are not supposed to exist “in the wild” outside their binder, e.g., in types passed to type inference, and should first be substituted (by placeholder regions, free regions, or region variables).

Placeholder and Free Regions

One often wants to work with bound regions without knowing their precise identity. For example, when checking a function, the lifetime of a borrow can end up being assigned to some region parameter. In these cases, it must be ensured that bounds on the region can’t be accidentally assumed without being checked.

To do this, we replace the bound regions with placeholder markers, which don’t satisfy any relation not explicitly provided.

There are two kinds of placeholder regions in rustc: ReFree and RePlaceholder. When checking an item’s body, ReFree is supposed to be used. These also support explicit bounds: both the internally-stored scope, which the region is assumed to outlive, as well as other relations stored in the FreeRegionMap. Note that these relations aren’t checked when you make_subregion (or eq_types), only by resolve_regions_and_report_errors.

When working with higher-ranked types, some region relations aren’t yet known, so you can’t just call resolve_regions_and_report_errors. RePlaceholder is designed for this purpose. In these contexts, there’s also the risk that some inference variable laying around will get unified with your placeholder region: if you want to check whether for<'a> Foo<'_>: 'a, and you substitute your bound region 'a with a placeholder region '%a, the variable '_ would just be instantiated to the placeholder region '%a, which is wrong because the inference variable is supposed to satisfy the relation for every value of the placeholder region. To ensure that doesn’t happen, you can use leak_check. This is more clearly explained by the rustc dev guide.

Variants§

§

ReEarlyBound(I::EarlyBoundRegion)

Region bound in a type or fn declaration which will be substituted ‘early’ – that is, at the same time when type parameters are substituted.

§

ReLateBound(DebruijnIndex, I::BoundRegion)

Region bound in a function scope, which will be substituted when the function is called.

§

ReFree(I::FreeRegion)

When checking a function body, the types of all arguments and so forth that refer to bound region parameters are modified to refer to free region parameters.

§

ReStatic

Static data that has an “infinite” lifetime. Top in the region lattice.

§

ReVar(I::RegionVid)

A region variable. Should not exist outside of type inference.

§

RePlaceholder(I::PlaceholderRegion)

A placeholder region – basically, the higher-ranked version of ReFree. Should not exist outside of type inference.

§

ReErased

Erased region, used by trait selection, in MIR and during codegen.

§

ReError(I::ErrorGuaranteed)

A region that resulted from some other error. Used exclusively for diagnostics.

Trait Implementations§

source§

impl<I: Interner> Clone for RegionKind<I>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<I: Interner> Debug for RegionKind<I>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<I: Interner> DebugWithInfcx<I> for RegionKind<I>

source§

fn fmt<InfCtx: InferCtxtLike<I>>( this: OptWithInfcx<'_, I, InfCtx, &Self>, f: &mut Formatter<'_> ) -> Result

source§

impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for RegionKind<I>where I::EarlyBoundRegion: Decodable<D>, I::BoundRegion: Decodable<D>, I::FreeRegion: Decodable<D>, I::RegionVid: Decodable<D>, I::PlaceholderRegion: Decodable<D>, I::ErrorGuaranteed: Decodable<D>,

source§

fn decode(d: &mut D) -> Self

source§

impl<I: Interner, E: TyEncoder> Encodable<E> for RegionKind<I>where I::EarlyBoundRegion: Encodable<E>, I::BoundRegion: Encodable<E>, I::FreeRegion: Encodable<E>, I::RegionVid: Encodable<E>, I::PlaceholderRegion: Encodable<E>,

source§

fn encode(&self, e: &mut E)

source§

impl<I: Interner> Hash for RegionKind<I>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>where I::EarlyBoundRegion: HashStable<CTX>, I::BoundRegion: HashStable<CTX>, I::FreeRegion: HashStable<CTX>, I::RegionVid: HashStable<CTX>, I::PlaceholderRegion: HashStable<CTX>,

source§

fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher)

source§

impl<I: Interner> Ord for RegionKind<I>

source§

fn cmp(&self, other: &RegionKind<I>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<I: Interner> PartialEq<RegionKind<I>> for RegionKind<I>

source§

fn eq(&self, other: &RegionKind<I>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<I: Interner> PartialOrd<RegionKind<I>> for RegionKind<I>

source§

fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<I: Interner> Copy for RegionKind<I>where I::EarlyBoundRegion: Copy, I::BoundRegion: Copy, I::FreeRegion: Copy, I::RegionVid: Copy, I::PlaceholderRegion: Copy, I::ErrorGuaranteed: Copy,

source§

impl<I: Interner> Eq for RegionKind<I>

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T, R> CollectAndApply<T, R> for T

source§

fn collect_and_apply<I, F>(iter: I, f: F) -> Rwhere I: Iterator<Item = T>, F: FnOnce(&[T]) -> R,

Equivalent to f(&iter.collect::<Vec<_>>()).

§

type Output = R

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.