Enum rustc_middle::ty::RegionKind
source · pub enum RegionKind<I>where
I: Interner,{
ReEarlyBound(<I as Interner>::EarlyBoundRegion),
ReLateBound(DebruijnIndex, <I as Interner>::BoundRegion),
ReFree(<I as Interner>::FreeRegion),
ReStatic,
ReVar(<I as Interner>::RegionVid),
RePlaceholder(<I as Interner>::PlaceholderRegion),
ReErased,
}
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 InternalSubsts
, 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 Param
s, 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 as Interner>::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 as Interner>::BoundRegion)
Region bound in a function scope, which will be substituted when the function is called.
ReFree(<I as Interner>::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 as Interner>::RegionVid)
A region variable. Should not exist outside of type inference.
RePlaceholder(<I as Interner>::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.
Trait Implementations§
source§impl<'tcx> Borrow<RegionKind<TyCtxt<'tcx>>> for InternedInSet<'tcx, RegionKind<'tcx>>
impl<'tcx> Borrow<RegionKind<TyCtxt<'tcx>>> for InternedInSet<'tcx, RegionKind<'tcx>>
source§fn borrow<'a>(&'a self) -> &'a RegionKind<'tcx>
fn borrow<'a>(&'a self) -> &'a RegionKind<'tcx>
Auto Trait Implementations§
impl<I> RefUnwindSafe for RegionKind<I>where
<I as Interner>::BoundRegion: RefUnwindSafe,
<I as Interner>::EarlyBoundRegion: RefUnwindSafe,
<I as Interner>::FreeRegion: RefUnwindSafe,
<I as Interner>::PlaceholderRegion: RefUnwindSafe,
<I as Interner>::RegionVid: RefUnwindSafe,
impl<I> Send for RegionKind<I>where
<I as Interner>::BoundRegion: Send,
<I as Interner>::EarlyBoundRegion: Send,
<I as Interner>::FreeRegion: Send,
<I as Interner>::PlaceholderRegion: Send,
<I as Interner>::RegionVid: Send,
impl<I> Sync for RegionKind<I>where
<I as Interner>::BoundRegion: Sync,
<I as Interner>::EarlyBoundRegion: Sync,
<I as Interner>::FreeRegion: Sync,
<I as Interner>::PlaceholderRegion: Sync,
<I as Interner>::RegionVid: Sync,
impl<I> Unpin for RegionKind<I>where
<I as Interner>::BoundRegion: Unpin,
<I as Interner>::EarlyBoundRegion: Unpin,
<I as Interner>::FreeRegion: Unpin,
<I as Interner>::PlaceholderRegion: Unpin,
<I as Interner>::RegionVid: Unpin,
impl<I> UnwindSafe for RegionKind<I>where
<I as Interner>::BoundRegion: UnwindSafe,
<I as Interner>::EarlyBoundRegion: UnwindSafe,
<I as Interner>::FreeRegion: UnwindSafe,
<I as Interner>::PlaceholderRegion: UnwindSafe,
<I as Interner>::RegionVid: UnwindSafe,
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<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<<Tcx as DepContext>::DepKind>
) -> Option<T>
default fn recover(
_: Tcx,
_: &DepNode<<Tcx as DepContext>::DepKind>
) -> 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. Read moresource§impl<T, R> InternIteratorElement<T, R> for T
impl<T, R> InternIteratorElement<T, R> for T
type Output = R
fn intern_with<I, F>(iter: I, f: F) -> <T as InternIteratorElement<T, R>>::Outputwhere
I: Iterator<Item = T>,
F: FnOnce(&[T]) -> R,
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, _: &[QueryInfo]) -> T
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.