pub struct RegionInferenceContext<'tcx> {
Show 17 fields pub var_infos: VarInfos, definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>, liveness_constraints: LivenessValues<RegionVid>, constraints: Frozen<OutlivesConstraintSet<'tcx>>, constraint_graph: Frozen<ConstraintGraph<Normal>>, constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>, rev_scc_graph: Option<Rc<ReverseSccGraph>>, member_constraints: Rc<MemberConstraintSet<'tcx, ConstraintSccIndex>>, member_constraints_applied: Vec<AppliedMemberConstraint>, closure_bounds_mapping: FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>, universe_causes: FxHashMap<UniverseIndex, UniverseInfo<'tcx>>, scc_universes: IndexVec<ConstraintSccIndex, UniverseIndex>, scc_representatives: IndexVec<ConstraintSccIndex, RegionVid>, scc_values: RegionValues<ConstraintSccIndex>, type_tests: Vec<TypeTest<'tcx>>, universal_regions: Rc<UniversalRegions<'tcx>>, universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,


var_infos: VarInfosdefinitions: IndexVec<RegionVid, RegionDefinition<'tcx>>

Contains the definition for every region variable. Region variables are identified by their index (RegionVid). The definition contains information about where the region came from as well as its final inferred value.

liveness_constraints: LivenessValues<RegionVid>

The liveness constraints added to each region. For most regions, these start out empty and steadily grow, though for each universally quantified region R they start out containing the entire CFG and end(R).

constraints: Frozen<OutlivesConstraintSet<'tcx>>

The outlives constraints computed by the type-check.

constraint_graph: Frozen<ConstraintGraph<Normal>>

The constraint-set, but in graph form, making it easy to traverse the constraints adjacent to a particular region. Used to construct the SCC (see constraint_sccs) and for error reporting.

constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>

The SCC computed from constraints and the constraint graph. We have an edge from SCC A to SCC B if A: B. Used to compute the values of each region.

rev_scc_graph: Option<Rc<ReverseSccGraph>>

Reverse of the SCC constraint graph – i.e., an edge A -> B exists if B: A. This is used to compute the universal regions that are required to outlive a given SCC. Computed lazily.

member_constraints: Rc<MemberConstraintSet<'tcx, ConstraintSccIndex>>

The “R0 member of [R1..Rn]” constraints, indexed by SCC.

member_constraints_applied: Vec<AppliedMemberConstraint>

Records the member constraints that we applied to each scc. This is useful for error reporting. Once constraint propagation is done, this vector is sorted according to member_region_scc.

closure_bounds_mapping: FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>

Map closure bounds to a Span that should be used for error reporting.

universe_causes: FxHashMap<UniverseIndex, UniverseInfo<'tcx>>

Map universe indexes to information on why we created it.

scc_universes: IndexVec<ConstraintSccIndex, UniverseIndex>

Contains the minimum universe of any variable within the same SCC. We will ensure that no SCC contains values that are not visible from this index.

scc_representatives: IndexVec<ConstraintSccIndex, RegionVid>

Contains a “representative” from each SCC. This will be the minimal RegionVid belonging to that universe. It is used as a kind of hacky way to manage checking outlives relationships, since we can ‘canonicalize’ each region to the representative of its SCC and be sure that – if they have the same repr – they must be equal (though not having the same repr does not mean they are unequal).

scc_values: RegionValues<ConstraintSccIndex>

The final inferred values of the region variables; we compute one value per SCC. To get the value for any given region, you first find which scc it is a part of.

type_tests: Vec<TypeTest<'tcx>>

Type constraints that we check after solving.

universal_regions: Rc<UniversalRegions<'tcx>>

Information about the universally quantified regions in scope on this function.

universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>

Information about how the universally quantified regions in scope on this function relate to one another.


Search the upvars (if any) to find one that references fr. Return its index.

Given the index of an upvar, finds its name and the span from where it was declared.

Search the argument types for one that references fr (which should be a free region). Returns Some(_) with the index of the input if one is found.

N.B., in the case of a closure, the index is indexing into the signature as seen by the user - in particular, index 0 is not the implicit self parameter.

Given the index of an argument, finds its name (if any) and the span from where it was declared.

Write out our state into the .mir files.

Debugging aid: Invokes the with_msg callback repeatedly with our internal region constraints. These are dumped into the -Zdump-mir file so that we can figure out why the region inference resulted in the values that it did when debugging.

Write out the region constraint graph.

Write out the region constraint graph.

Resolve any opaque types that were encountered while borrow checking this item. This is then used to get the type in the type_of query.

For example consider fn f<'a>(x: &'a i32) -> impl Sized + 'a { x }. This is lowered to give HIR something like

type f<‘a>::_Return<’_a> = impl Sized + ’_a; fn f<’a>(x: &’a i32) -> f<’static>::_Return<’a> { x }

When checking the return type record the type from the return and the type used in the return value. In this case they might be _Return<'1> and &'2 i32 respectively.

Once we to this method, we have completed region inference and want to call infer_opaque_definition_from_instantiation to get the inferred type of _Return<'_a>. infer_opaque_definition_from_instantiation compares lifetimes directly, so we need to map the inference variables back to concrete lifetimes: 'static, ReEarlyBound or ReFree.

First we map all the lifetimes in the concrete type to an equal universal region that occurs in the concrete type’s substs, in this case this would result in &'1 i32. We only consider regions in the substs in case there is an equal region that does not. For example, this should be allowed: fn f<'a: 'b, 'b: 'a>(x: *mut &'b i32) -> impl Sized + 'a { x }

Then we map the regions in both the type and the subst to their external_name giving concrete_type = &'a i32, substs = ['static, 'a]. This will then allow infer_opaque_definition_from_instantiation to determine that _Return<'_a> = &'_a i32.

There’s a slight complication around closures. Given fn f<'a: 'a>() { || {} } the closure’s type is something like f::<'a>::{{closure}}. The region parameter from f is essentially ignored by type checking so ends up being inferred to an empty region. Calling universal_upper_bound for such a region gives fr_fn_body, which has no external_name in which case we use 'empty as the region to pass to infer_opaque_definition_from_instantiation.

Map the regions in the type to named regions. This is similar to what infer_opaque_types does, but can infer any universal region, not only ones from the substs for the opaque type. It also doesn’t double check that the regions produced are in fact equal to the named region they are replaced with. This is fine because this function is only to improve the region names in error messages.

Compute and return the reverse SCC-based constraint graph (lazily).

Creates a new region inference context with a total of num_region_variables valid inference variables; the first N of those will be constant regions representing the free regions defined in universal_regions.

The outlives_constraints and type_tests are an initial set of constraints produced by the MIR type check.

Each SCC is the combination of many region variables which have been equated. Therefore, we can associate a universe with each SCC which is minimum of all the universes of its constituent regions – this is because whatever value the SCC takes on must be a value that each of the regions within the SCC could have as well. This implies that the SCC must have the minimum, or narrowest, universe.

For each SCC, we compute a unique RegionVid (in fact, the minimal one that belongs to the SCC). See scc_representatives field of RegionInferenceContext for more details.

Initializes the region variables for each universally quantified region (lifetime parameter). The first N variables always correspond to the regions appearing in the function signature (both named and anonymous) and where-clauses. This function iterates over those regions and initializes them with minimum values.

For example:

fn foo<'a, 'b>( /* ... */ ) where 'a: 'b { /* ... */ }

would initialize two variables like so:

R0 = { CFG, R0 } // 'a
R1 = { CFG, R0, R1 } // 'b

Here, R0 represents 'a, and it contains (a) the entire CFG and (b) any universally quantified regions that it outlives, which in this case is just itself. R1 ('b) in contrast also outlives 'a and hence contains R0 and R1.

Returns an iterator over all the region indices.

Given a universal region in scope on the MIR, returns the corresponding index.

(Panics if r is not a registered universal region.)

Adds annotations for #[rustc_regions]; see UniversalRegions::annotate.

Returns true if the region r contains the point p.

Panics if called before solve() executes,

Returns access to the value of r for debugging purposes.

Returns access to the value of r for debugging purposes.

Once region solving has completed, this function will return the member constraints that were applied to the value of a given region r. See AppliedMemberConstraint.

Performs region inference and report errors if we see any unsatisfiable constraints. If this is a closure, returns the region requirements to propagate to our creator, if any.

Propagate the region constraints: this will grow the values for each region variable until all the constraints are satisfied. Note that some values may grow too large to be feasible, but we check this later.

Computes the value of the SCC scc_a, which has not yet been computed, by unioning the values of its successors. Assumes that all successors have been computed already (which is assured by iterating over SCCs in dependency order).

Invoked for each R0 member of [R1..Rn] constraint.

scc is the SCC containing R0, and choice_regions are the R1..Rn regions – they are always known to be universal regions (and if that’s not true, we just don’t attempt to enforce the constraint).

The current value of scc at the time the method is invoked is considered a lower bound. If possible, we will modify the constraint to set it equal to one of the option regions. If we make any changes, returns true, else false.

Returns true if all the elements in the value of scc_b are nameable in scc_a. Used during constraint propagation, and only once the value of scc_b has been computed.

Extend scc so that it can outlive some placeholder region from a universe it can’t name; at present, the only way for this to be true is if scc outlives 'static. This is actually stricter than necessary: ideally, we’d support bounds like for<'a: 'b>that might then allow us to approximate’awith’band not’static`. But it will have to do for now.

Once regions have been propagated, this method is used to see whether the “type tests” produced by typeck were satisfied; type tests encode type-outlives relationships like T: 'a. See TypeTest for more details.

Invoked when we have some type-test (e.g., T: 'X) that we cannot prove to be satisfied. If this is a closure, we will attempt to “promote” this type-test into our ClosureRegionRequirements and hence pass it up the creator. To do this, we have to phrase the type-test in terms of external free regions, as local free regions are not nameable by the closure’s creator.

Promotion works as follows: we first check that the type T contains only regions that the creator knows about. If this is true, then – as a consequence – we know that all regions in the type T are free regions that outlive the closure body. If false, then promotion fails.

Once we’ve promoted T, we have to “promote” 'X to some region that is “external” to the closure. Generally speaking, a region may be the union of some points in the closure body as well as various free lifetimes. We can ignore the points in the closure body: if the type T can be expressed in terms of external regions, we know it outlives the points in the closure body. That just leaves the free regions.

The idea then is to lower the T: 'X constraint into multiple bounds – e.g., if 'X is the union of two free lifetimes, '1 and '2, then we would create T: '1 and T: '2.

When we promote a type test T: 'r, we have to convert the type T into something we can store in a query result (so something allocated for 'tcx). This is problematic if ty contains regions. During the course of NLL region checking, we will have replaced all of those regions with fresh inference variables. To create a test subject, we want to replace those inference variables with some region from the closure signature – this is not always possible, so this is a fallible process. Presuming we do find a suitable region, we will use it’s external name, which will be a RegionKind variant that can be used in query responses such as ReEarlyBound.

Given some universal or existential region r, finds a non-local, universal region r+ that outlives r at entry to (and exit from) the closure. In the worst case, this will be 'static.

This is used for two purposes. First, if we are propagated some requirement T: r, we can use this method to enlarge r to something we can encode for our creator (which only knows about non-local, universal regions). It is also used when encoding T as part of try_promote_type_test_subject (see that fn for details).

This is based on the result 'y of universal_upper_bound, except that it converts further takes the non-local upper bound of 'y, so that the final result is non-local.

Returns a universally quantified region that outlives the value of r (r may be existentially or universally quantified).

Since r is (potentially) an existential region, it has some value which may include (a) any number of points in the CFG and (b) any number of end('x) elements of universally quantified regions. To convert this into a single universal region we do as follows:

  • Ignore the CFG points in 'r. All universally quantified regions include the CFG anyhow.
  • For each end('x) element in 'r, compute the mutual LUB, yielding a result 'y.

Like universal_upper_bound, but returns an approximation more suitable for diagnostics. If r contains multiple disjoint universal regions (e.g. ’a and ’b in fn foo<'a, 'b> { ... }, we pick the lower-numbered region. This corresponds to picking named regions over unnamed regions (e.g. picking early-bound regions over a closure late-bound region).

This means that the returned value may not be a true upper bound, since only ’static is known to outlive disjoint universal regions. Therefore, this method should only be used in diagnostic code, where displaying some named universal region is better than falling back to ’static.

Tests if test is true when applied to lower_bound at point.

This is a conservative normalization procedure. It takes every free region in value and replaces it with the “representative” of its SCC (see scc_representatives field). We are guaranteed that if two values normalize to the same thing, then they are equal; this is a conservative check in that they could still be equal even if they normalize to different results. (For example, there might be two regions with the same value that are not in the same SCC).

N.B., this is not an ideal approach and I would like to revisit it. However, it works pretty well in practice. In particular, this is needed to deal with projection outlives bounds like

<T as Foo<'0>>::Item: '1

In particular, this routine winds up being important when there are bounds like where <T as Foo<'a>>::Item: 'b in the environment. In this case, if we can show that '0 == 'a, and that 'b: '1, then we know that the clause is satisfied. In such cases, particularly due to limitations of the trait solver =), we usually wind up with a where-clause like T: Foo<'a> in scope, which thus forces '0 == 'a to be added as a constraint, and thus ensures that they are in the same SCC.

So why can’t we do a more correct routine? Well, we could almost use the relate_tys code, but the way it is currently setup it creates inference variables to deal with higher-ranked things and so forth, and right now the inference context is not permitted to make more inference variables. So we use this kind of hacky solution.

Once regions have been propagated, this method is used to see whether any of the constraints were too strong. In particular, we want to check for a case where a universally quantified region exceeded its bounds. Consider:

fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }

In this case, returning x requires &'a u32 <: &'b u32 and hence we establish (transitively) a constraint that 'a: 'b. The propagate_constraints code above will therefore add end('a) into the region for 'b – but we have no evidence that 'b outlives 'a, so we want to report an error.

If propagated_outlives_requirements is Some, then we will push unsatisfied obligations into there. Otherwise, we’ll report them as errors.

Checks if Polonius has found any unexpected free region relations.

In Polonius terms, a “subset error” (or “illegal subset relation error”) is the equivalent of NLL’s “checking if any region constraints were too strong”: a placeholder origin 'a was unexpectedly found to be a subset of another placeholder origin 'b, and means in NLL terms that the “longer free region” 'a outlived the “shorter free region” 'b.

More details can be found in this blog post by Niko:

In the canonical example

fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }

returning x requires &'a u32 <: &'b u32 and hence we establish (transitively) a constraint that 'a: 'b. It is an error that we have no evidence that this constraint holds.

If propagated_outlives_requirements is Some, then we will push unsatisfied obligations into there. Otherwise, we’ll report them as errors.

Checks the final value for the free region fr to see if it grew too large. In particular, examine what end(X) points wound up in fr’s final value; for each end(X) where X != fr, we want to check that fr: X. If not, that’s either an error, or something we have to propagate to our creator.

Things that are to be propagated are accumulated into the outlives_requirements vector.

Checks that we can prove that longer_fr: shorter_fr. If we can’t we attempt to propagate the constraint outward (e.g. to a closure environment), but if that fails, there is an error.

Attempt to propagate a region error (e.g. 'a: 'b) that is not met to a closure’s creator. If we cannot, then the caller should report an error to the user.

We have a constraint fr1: fr2 that is not satisfied, where fr2 represents some universal region. Here, r is some region where we know that fr1: r and this function has the job of determining whether r is “to blame” for the fact that fr1: fr2 is required.

This is true under two conditions:

  • r == fr2
  • fr2 is 'static and r is some placeholder in a universe that cannot be named by fr1; in that case, we will require that fr1: 'static because it is the only way to fr1: r to be satisfied. (See add_incompatible_universe.)

If r2 represents a placeholder region, then this returns true if r1 cannot name that placeholder in its value; otherwise, returns false.

Finds a good ObligationCause to blame for the fact that fr1 outlives fr2.

Walks the graph of constraints (where 'a: 'b is considered an edge 'a -> 'b) to find all paths from from_region to to_region. The paths are accumulated into the vector results. The paths are stored as a series of ConstraintIndex values – in other words, a list of edges.

Returns: a series of constraints as well as the region R that passed the target test.

Finds some region R such that fr1: R and R is live at elem.

Get the region outlived by longer_fr and live at element.

Get the region definition of r.

Check if the SCC of r contains upper.

Tries to find the best constraint to blame for the fact that R: from_region, where R is some region that meets target_test. This works by following the constraint graph, creating a constraint path that forces R to outlive from_region, and then finding the best choices within that path to blame.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.


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: 728 bytes