pub struct InferCtxt<'tcx> {Show 14 fields
pub tcx: TyCtxt<'tcx>,
typing_mode: TypingMode<'tcx>,
pub considering_regions: bool,
skip_leak_check: bool,
pub inner: RefCell<InferCtxtInner<'tcx>>,
lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
pub selection_cache: SelectionCache<'tcx>,
pub evaluation_cache: EvaluationCache<'tcx>,
pub reported_trait_errors: RefCell<FxIndexMap<Span, (Vec<Predicate<'tcx>>, ErrorGuaranteed)>>,
pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
universe: Cell<UniverseIndex>,
next_trait_solver: bool,
pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
}
Fields§
§tcx: TyCtxt<'tcx>
§typing_mode: TypingMode<'tcx>
The mode of this inference context, see the struct documentation for more details.
considering_regions: bool
Whether this inference context should care about region obligations in the root universe. Most notably, this is used during hir typeck as region solving is left to borrowck instead.
skip_leak_check: bool
If set, this flag causes us to skip the ‘leak check’ during higher-ranked subtyping operations. This flag is a temporary one used to manage the removal of the leak-check: for the time being, we still run the leak-check, but we issue warnings.
inner: RefCell<InferCtxtInner<'tcx>>
§lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>
Once region inference is done, the values for each variable.
selection_cache: SelectionCache<'tcx>
Caches the results of trait selection. This cache is used for things that have to do with the parameters in scope.
evaluation_cache: EvaluationCache<'tcx>
Caches the results of trait evaluation.
reported_trait_errors: RefCell<FxIndexMap<Span, (Vec<Predicate<'tcx>>, ErrorGuaranteed)>>
The set of predicates on which errors have been reported, to avoid reporting the same error twice.
reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>
§tainted_by_errors: Cell<Option<ErrorGuaranteed>>
When an error occurs, we want to avoid reporting “derived” errors that are due to this original failure. We have this flag that one can set whenever one creates a type-error that is due to an error in a prior pass.
Don’t read this flag directly, call is_tainted_by_errors()
and set_tainted_by_errors()
.
universe: Cell<UniverseIndex>
What is the innermost universe we have created? Starts out as
UniverseIndex::root()
but grows from there as we enter
universal quantifiers.
N.B., at present, we exclude the universal quantifiers on the
item we are type-checking, and just consider those names as
part of the root universe. So this would only get incremented
when we enter into a higher-ranked (for<..>
) type or trait
bound.
next_trait_solver: bool
§obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>
Implementations§
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
pub fn at<'a>( &'a self, cause: &'a ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, ) -> At<'a, 'tcx>
Sourcepub fn fork(&self) -> Self
pub fn fork(&self) -> Self
Forks the inference context, creating a new inference context with the same inference variables in the same state. This can be used to “branch off” many tests from the same common state.
Sourcepub fn fork_with_typing_mode(&self, typing_mode: TypingMode<'tcx>) -> Self
pub fn fork_with_typing_mode(&self, typing_mode: TypingMode<'tcx>) -> Self
Forks the inference context, creating a new inference context with the same inference variables in the same state, except possibly changing the intercrate mode. This can be used to “branch off” many tests from the same common state. Used in negative coherence.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn canonicalize_query<V>(
&self,
value: ParamEnvAnd<'tcx, V>,
query_state: &mut OriginalQueryValues<'tcx>,
) -> CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, V>>where
V: TypeFoldable<TyCtxt<'tcx>>,
pub fn canonicalize_query<V>(
&self,
value: ParamEnvAnd<'tcx, V>,
query_state: &mut OriginalQueryValues<'tcx>,
) -> CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, V>>where
V: TypeFoldable<TyCtxt<'tcx>>,
Canonicalizes a query value V
. When we canonicalize a query,
we not only canonicalize unbound inference variables, but we
also replace all free regions whatsoever. So for example a
query like T: Trait<'static>
would be canonicalized to
T: Trait<'?0>
with a mapping M that maps '?0
to 'static
.
To get a good understanding of what is happening here, check out the chapter in the rustc dev guide.
Sourcepub fn canonicalize_response<V>(&self, value: V) -> Canonical<'tcx, V>where
V: TypeFoldable<TyCtxt<'tcx>>,
pub fn canonicalize_response<V>(&self, value: V) -> Canonical<'tcx, V>where
V: TypeFoldable<TyCtxt<'tcx>>,
Canonicalizes a query response V
. When we canonicalize a
query response, we only canonicalize unbound inference
variables, and we leave other free regions alone. So,
continuing with the example from canonicalize_query
, if
there was an input query T: Trait<'static>
, it would have
been canonicalized to
T: Trait<'?0>
with a mapping M that maps '?0
to 'static
. But if we found that there
exists only one possible impl of Trait
, and it looks like
impl<T> Trait<'static> for T { .. }
then we would prepare a query result R that (among other
things) includes a mapping to '?0 := 'static
. When
canonicalizing this query result R, we would leave this
reference to 'static
alone.
To get a good understanding of what is happening here, check out the chapter in the rustc dev guide.
pub fn canonicalize_user_type_annotation<V>(
&self,
value: V,
) -> Canonical<'tcx, V>where
V: TypeFoldable<TyCtxt<'tcx>>,
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn make_canonicalized_query_response<T>(
&self,
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
) -> Result<CanonicalQueryResponse<'tcx, T>, NoSolution>where
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
Canonical<'tcx, QueryResponse<'tcx, T>>: ArenaAllocatable<'tcx>,
pub fn make_canonicalized_query_response<T>(
&self,
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
) -> Result<CanonicalQueryResponse<'tcx, T>, NoSolution>where
T: Debug + TypeFoldable<TyCtxt<'tcx>>,
Canonical<'tcx, QueryResponse<'tcx, T>>: ArenaAllocatable<'tcx>,
This method is meant to be invoked as the final step of a canonical query implementation. It is given:
- the instantiated variables
inference_vars
created from the query key - the result
answer
of the query - a fulfillment context
fulfill_cx
that may contain various obligations which have yet to be proven.
Given this, the function will process the obligations pending
in fulfill_cx
:
- If all the obligations can be proven successfully, it will
package up any resulting region obligations (extracted from
infcx
) along with the fully resolved valueanswer
into a query result (which is then itself canonicalized). - If some obligations can be neither proven nor disproven, then the same thing happens, but the resulting query is marked as ambiguous.
- Finally, if any of the obligations result in a hard error,
then
Err(NoSolution)
is returned.
Sourcepub fn make_query_response_ignoring_pending_obligations<T>(
&self,
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
) -> Canonical<'tcx, QueryResponse<'tcx, T>>
pub fn make_query_response_ignoring_pending_obligations<T>( &self, inference_vars: CanonicalVarValues<'tcx>, answer: T, ) -> Canonical<'tcx, QueryResponse<'tcx, T>>
A version of make_canonicalized_query_response
that does
not pack in obligations, for contexts that want to drop
pending obligations instead of treating them as an ambiguity (e.g.
typeck “probing” contexts).
If you DO want to keep track of pending obligations (which include all region obligations, so this includes all cases that care about regions) with this function, you have to do it yourself, by e.g., having them be a part of the answer.
Sourcefn make_query_response<T>(
&self,
inference_vars: CanonicalVarValues<'tcx>,
answer: T,
fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
) -> Result<QueryResponse<'tcx, T>, NoSolution>
fn make_query_response<T>( &self, inference_vars: CanonicalVarValues<'tcx>, answer: T, fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>, ) -> Result<QueryResponse<'tcx, T>, NoSolution>
Helper for make_canonicalized_query_response
that does
everything up until the final canonicalization.
Sourcepub fn clone_opaque_types_for_query_response(
&self,
) -> Vec<(OpaqueTypeKey<'tcx>, Ty<'tcx>)>
pub fn clone_opaque_types_for_query_response( &self, ) -> Vec<(OpaqueTypeKey<'tcx>, Ty<'tcx>)>
Used by the new solver as that one takes the opaque types at the end of a probe to deal with multiple candidates without having to recompute them.
fn take_opaque_types_for_query_response( &self, ) -> Vec<(OpaqueTypeKey<'tcx>, Ty<'tcx>)>
Sourcepub fn instantiate_query_response_and_region_obligations<R>(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
original_values: &OriginalQueryValues<'tcx>,
query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
) -> InferResult<'tcx, R>
pub fn instantiate_query_response_and_region_obligations<R>( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, original_values: &OriginalQueryValues<'tcx>, query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>, ) -> InferResult<'tcx, R>
Given the (canonicalized) result to a canonical query, instantiates the result so it can be used, plugging in the values from the canonical query. (Note that the result may have been ambiguous; you should check the certainty level of the query before applying this function.)
To get a good understanding of what is happening here, check out the chapter in the rustc dev guide.
Sourcepub fn instantiate_nll_query_response_and_region_obligations<R>(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
original_values: &OriginalQueryValues<'tcx>,
query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
output_query_region_constraints: &mut QueryRegionConstraints<'tcx>,
) -> InferResult<'tcx, R>
pub fn instantiate_nll_query_response_and_region_obligations<R>( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, original_values: &OriginalQueryValues<'tcx>, query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>, output_query_region_constraints: &mut QueryRegionConstraints<'tcx>, ) -> InferResult<'tcx, R>
An alternative to
instantiate_query_response_and_region_obligations
that is more
efficient for NLL. NLL is a bit more advanced in the
“transition to chalk” than the rest of the compiler. During
the NLL type check, all of the “processing” of types and
things happens in queries – the NLL checker itself is only
interested in the region obligations ('a: 'b
or T: 'b
)
that come out of these queries, which it wants to convert into
MIR-based constraints and solve. Therefore, it is most
convenient for the NLL Type Checker to directly consume
the QueryOutlivesConstraint
values that arise from doing a
query. This is contrast to other parts of the compiler, which
would prefer for those QueryOutlivesConstraint
to be converted
into the older infcx-style constraints (e.g., calls to
sub_regions
or register_region_obligation
).
Therefore, instantiate_nll_query_response_and_region_obligations
performs the same
basic operations as instantiate_query_response_and_region_obligations
but
it returns its result differently:
- It creates an instantiation
S
that maps from the original query variables to the values computed in the query result. If any errors arise, they are propagated back as anErr
result. - In the case of a successful instantiation, we will append
QueryOutlivesConstraint
values onto theoutput_query_region_constraints
vector for the solver to use (if an error arises, some values may also be pushed, but they should be ignored). - It can happen (though it rarely does currently) that equating types and things will give rise to subobligations that must be processed. In this case, those subobligations are propagated back in the return value.
- Finally, the query result (of type
R
) is propagated back, after applying the instantiationS
.
Sourcefn query_response_instantiation<R>(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
original_values: &OriginalQueryValues<'tcx>,
query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
fn query_response_instantiation<R>( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, original_values: &OriginalQueryValues<'tcx>, query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>, ) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
Given the original values and the (canonicalized) result from computing a query, returns an instantiation that can be applied to the query result to convert the result back into the original namespace.
The instantiation also comes accompanied with subobligations that arose from unification; these might occur if (for example) we are doing lazy normalization and the value assigned to a type variable is unified with an unnormalized projection.
Sourcefn query_response_instantiation_guess<R>(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
original_values: &OriginalQueryValues<'tcx>,
query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
fn query_response_instantiation_guess<R>( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, original_values: &OriginalQueryValues<'tcx>, query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>, ) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
Given the original values and the (canonicalized) result from computing a query, returns a guess at an instantiation that can be applied to the query result to convert the result back into the original namespace. This is called a guess because it uses a quick heuristic to find the values for each canonical variable; if that quick heuristic fails, then we will instantiate fresh inference variables for each canonical variable instead. Therefore, the result of this method must be properly unified
Sourcefn unify_query_response_instantiation_guess<R>(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
original_values: &OriginalQueryValues<'tcx>,
result_args: &CanonicalVarValues<'tcx>,
query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
) -> InferResult<'tcx, ()>
fn unify_query_response_instantiation_guess<R>( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, original_values: &OriginalQueryValues<'tcx>, result_args: &CanonicalVarValues<'tcx>, query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>, ) -> InferResult<'tcx, ()>
Given a “guess” at the values for the canonical variables in the input, try to unify with the actual values found in the query result. Often, but not always, this is a no-op, because we already found the mapping in the “guessing” step.
See also: Self::query_response_instantiation_guess
Sourcefn query_outlives_constraints_into_obligations<'a>(
&'a self,
cause: &'a ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
uninstantiated_region_constraints: &'a [QueryOutlivesConstraint<'tcx>],
result_args: &'a CanonicalVarValues<'tcx>,
) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx>
fn query_outlives_constraints_into_obligations<'a>( &'a self, cause: &'a ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, uninstantiated_region_constraints: &'a [QueryOutlivesConstraint<'tcx>], result_args: &'a CanonicalVarValues<'tcx>, ) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx>
Converts the region constraints resulting from a query into an iterator of obligations.
pub fn query_outlives_constraint_to_obligation( &self, (predicate, _): QueryOutlivesConstraint<'tcx>, cause: ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, ) -> Obligation<'tcx, Predicate<'tcx>>
Sourcefn unify_canonical_vars(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
variables1: &OriginalQueryValues<'tcx>,
variables2: impl Fn(BoundVar) -> GenericArg<'tcx>,
) -> InferResult<'tcx, ()>
fn unify_canonical_vars( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, variables1: &OriginalQueryValues<'tcx>, variables2: impl Fn(BoundVar) -> GenericArg<'tcx>, ) -> InferResult<'tcx, ()>
Given two sets of values for the same set of canonical variables, unify them. The second set is produced lazily by supplying indices from the first set.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn instantiate_canonical<T>(
&self,
span: Span,
canonical: &Canonical<'tcx, T>,
) -> (T, CanonicalVarValues<'tcx>)where
T: TypeFoldable<TyCtxt<'tcx>>,
pub fn instantiate_canonical<T>(
&self,
span: Span,
canonical: &Canonical<'tcx, T>,
) -> (T, CanonicalVarValues<'tcx>)where
T: TypeFoldable<TyCtxt<'tcx>>,
Creates an instantiation S for the canonical value with fresh inference variables and placeholders then applies it to the canonical value. Returns both the instantiated result and the instantiation S.
This can be invoked as part of constructing an
inference context at the start of a query (see
InferCtxtBuilder::build_with_canonical
). It basically
brings the canonical value “into scope” within your new infcx.
At the end of processing, the instantiation S (once canonicalized) then represents the values that you computed for each of the canonical inputs to your query.
Sourcefn instantiate_canonical_vars(
&self,
span: Span,
variables: &List<CanonicalVarInfo<'tcx>>,
universe_map: impl Fn(UniverseIndex) -> UniverseIndex,
) -> CanonicalVarValues<'tcx>
fn instantiate_canonical_vars( &self, span: Span, variables: &List<CanonicalVarInfo<'tcx>>, universe_map: impl Fn(UniverseIndex) -> UniverseIndex, ) -> CanonicalVarValues<'tcx>
Given the “infos” about the canonical variables from some
canonical, creates fresh variables with the same
characteristics (see instantiate_canonical_var
for
details). You can then use instantiate
to instantiate the
canonical variable with these inference variables.
Sourcepub fn instantiate_canonical_var(
&self,
span: Span,
cv_info: CanonicalVarInfo<'tcx>,
universe_map: impl Fn(UniverseIndex) -> UniverseIndex,
) -> GenericArg<'tcx>
pub fn instantiate_canonical_var( &self, span: Span, cv_info: CanonicalVarInfo<'tcx>, universe_map: impl Fn(UniverseIndex) -> UniverseIndex, ) -> GenericArg<'tcx>
Given the “info” about a canonical variable, creates a fresh variable for it. If this is an existentially quantified variable, then you’ll get a new inference variable; if it is a universally quantified variable, you get a placeholder.
FIXME(-Znext-solver): This is public because it’s used by the new trait solver which has a different canonicalization routine. We should somehow deduplicate all of this.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn replace_opaque_types_with_inference_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
&self,
value: T,
body_id: LocalDefId,
span: Span,
param_env: ParamEnv<'tcx>,
) -> InferOk<'tcx, T>
pub fn replace_opaque_types_with_inference_vars<T: TypeFoldable<TyCtxt<'tcx>>>( &self, value: T, body_id: LocalDefId, span: Span, param_env: ParamEnv<'tcx>, ) -> InferOk<'tcx, T>
This is a backwards compatibility hack to prevent breaking changes from lazy TAIT around RPIT handling.
pub fn handle_opaque_type( &self, a: Ty<'tcx>, b: Ty<'tcx>, span: Span, param_env: ParamEnv<'tcx>, ) -> Result<Vec<Goal<'tcx, Predicate<'tcx>>>, TypeError<'tcx>>
Sourcepub fn register_member_constraints(
&self,
opaque_type_key: OpaqueTypeKey<'tcx>,
concrete_ty: Ty<'tcx>,
span: Span,
)
pub fn register_member_constraints( &self, opaque_type_key: OpaqueTypeKey<'tcx>, concrete_ty: Ty<'tcx>, span: Span, )
Given the map opaque_types
containing the opaque
impl Trait
types whose underlying, hidden types are being
inferred, this method adds constraints to the regions
appearing in those underlying hidden types to ensure that they
at least do not refer to random scopes within the current
function. These constraints are not (quite) sufficient to
guarantee that the regions are actually legal values; that
final condition is imposed after region inference is done.
§The Problem
Let’s work through an example to explain how it works. Assume the current function is as follows:
fn foo<'a, 'b>(..) -> (impl Bar<'a>, impl Bar<'b>)
Here, we have two impl Trait
types whose values are being
inferred (the impl Bar<'a>
and the impl Bar<'b>
). Conceptually, this is sugar for a setup where we
define underlying opaque types (Foo1
, Foo2
) and then, in
the return type of foo
, we reference those definitions:
type Foo1<'x> = impl Bar<'x>;
type Foo2<'x> = impl Bar<'x>;
fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
// ^^^^ ^^
// | |
// | args
// def_id
As indicating in the comments above, each of those references
is (in the compiler) basically generic parameters (args
)
applied to the type of a suitable def_id
(which identifies
Foo1
or Foo2
).
Now, at this point in compilation, what we have done is to
replace each of the references (Foo1<'a>
, Foo2<'b>
) with
fresh inference variables C1 and C2. We wish to use the values
of these variables to infer the underlying types of Foo1
and
Foo2
. That is, this gives rise to higher-order (pattern) unification
constraints like:
for<'a> (Foo1<'a> = C1)
for<'b> (Foo1<'b> = C2)
For these equation to be satisfiable, the types C1
and C2
can only refer to a limited set of regions. For example, C1
can only refer to 'static
and 'a
, and C2
can only refer
to 'static
and 'b
. The job of this function is to impose that
constraint.
Up to this point, C1 and C2 are basically just random type
inference variables, and hence they may contain arbitrary
regions. In fact, it is fairly likely that they do! Consider
this possible definition of foo
:
fn foo<'a, 'b>(x: &'a i32, y: &'b i32) -> (impl Bar<'a>, impl Bar<'b>) {
(&*x, &*y)
}
Here, the values for the concrete types of the two impl traits will include inference variables:
&'0 i32
&'1 i32
Ordinarily, the subtyping rules would ensure that these are
sufficiently large. But since impl Bar<'a>
isn’t a specific
type per se, we don’t get such constraints by default. This
is where this function comes into play. It adds extra
constraints to ensure that all the regions which appear in the
inferred type are regions that could validly appear.
This is actually a bit of a tricky constraint in general. We
want to say that each variable (e.g., '0
) can only take on
values that were supplied as arguments to the opaque type
(e.g., 'a
for Foo1<'a>
) or 'static
, which is always in
scope. We don’t have a constraint quite of this kind in the current
region checker.
§The Solution
We generally prefer to make <=
constraints, since they
integrate best into the region solver. To do that, we find the
“minimum” of all the arguments that appear in the args: that
is, some region which is less than all the others. In the case
of Foo1<'a>
, that would be 'a
(it’s the only choice, after
all). Then we apply that as a least bound to the variables
(e.g., 'a <= '0
).
In some cases, there is no minimum. Consider this example:
fn baz<'a, 'b>() -> impl Trait<'a, 'b> { ... }
Here we would report a more complex “in constraint”, like 'r in ['a, 'b, 'static]
(where 'r
is some region appearing in
the hidden type).
§Constrain regions, not the hidden concrete type
Note that generating constraints on each region Rc
is not
the same as generating an outlives constraint on Tc
itself.
For example, if we had a function like this:
fn foo<'a, T>(x: &'a u32, y: T) -> impl Foo<'a> {
(x, y)
}
// Equivalent to:
type FooReturn<'a, T> = impl Foo<'a>;
fn foo<'a, T>(x: &'a u32, y: T) -> FooReturn<'a, T> {
(x, y)
}
then the hidden type Tc
would be (&'0 u32, T)
(where '0
is an inference variable). If we generated a constraint that
Tc: 'a
, then this would incorrectly require that T: 'a
–
but this is not necessary, because the opaque type we
create will be allowed to reference T
. So we only generate a
constraint that '0: 'a
.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Insert a hidden type into the opaque type storage, making sure it hasn’t previously been defined. This does not emit any constraints and it’s the responsibility of the caller to make sure that the item bounds of the opaque are checked.
Insert a hidden type into the opaque type storage, equating it with any previous entries if necessary.
This does not add the item bounds of the opaque as nested obligations. That is only necessary when normalizing the opaque itself, not when getting the opaque type constraints from somewhere else.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn register_region_obligation(&self, obligation: RegionObligation<'tcx>)
pub fn register_region_obligation(&self, obligation: RegionObligation<'tcx>)
Registers that the given region obligation must be resolved
from within the scope of body_id
. These regions are enqueued
and later processed by regionck, when full type information is
available (see region_obligations
field for more
information).
pub fn register_region_obligation_with_cause( &self, sup_type: Ty<'tcx>, sub_region: Region<'tcx>, cause: &ObligationCause<'tcx>, )
Sourcepub fn take_registered_region_obligations(&self) -> Vec<RegionObligation<'tcx>>
pub fn take_registered_region_obligations(&self) -> Vec<RegionObligation<'tcx>>
Trait queries just want to pass back type obligations “as is”
Sourcepub fn process_registered_region_obligations(
&self,
outlives_env: &OutlivesEnvironment<'tcx>,
deeply_normalize_ty: impl FnMut(PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>) -> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>,
) -> Result<(), (PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>)>
pub fn process_registered_region_obligations( &self, outlives_env: &OutlivesEnvironment<'tcx>, deeply_normalize_ty: impl FnMut(PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>) -> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>, ) -> Result<(), (PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>)>
Process the region obligations that must be proven (during
regionck
) for the given body_id
, given information about
the region bounds in scope and so forth.
See the region_obligations
field of InferCtxt
for some
comments about how this function fits into the overall expected
flow of the inferencer. The key point is that it is
invoked after all type-inference variables have been bound –
right before lexical region resolution.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn resolve_regions_with_normalize(
&self,
outlives_env: &OutlivesEnvironment<'tcx>,
deeply_normalize_ty: impl Fn(PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>) -> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>,
) -> Vec<RegionResolutionError<'tcx>>
pub fn resolve_regions_with_normalize( &self, outlives_env: &OutlivesEnvironment<'tcx>, deeply_normalize_ty: impl Fn(PolyTypeOutlivesPredicate<'tcx>, SubregionOrigin<'tcx>) -> Result<PolyTypeOutlivesPredicate<'tcx>, NoSolution>, ) -> Vec<RegionResolutionError<'tcx>>
Process the region constraints and return any errors that
result. After this, no more unification operations should be
done – or the compiler will panic – but it is legal to use
resolve_vars_if_possible
as well as fully_resolve
.
If you are in a crate that has access to rustc_trait_selection
,
then it’s probably better to use resolve_regions
,
which knows how to normalize registered region obligations.
Sourcepub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx>
pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx>
Obtains (and clears) the current set of region constraints. The inference context is still usable: further unifications will simply add new constraints.
This method is not meant to be used with normal lexical region resolution. Rather, it is used in the NLL mode as a kind of interim hack: basically we run normal type-check and generate region constraints as normal, but then we take them and translate them into the form that the NLL solver understands. See the NLL module for mode details.
Sourcepub fn with_region_constraints<R>(
&self,
op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
) -> R
pub fn with_region_constraints<R>( &self, op: impl FnOnce(&RegionConstraintData<'tcx>) -> R, ) -> R
Gives temporary access to the region constraint data.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn projection_ty_to_infer(
&self,
param_env: ParamEnv<'tcx>,
projection_ty: AliasTy<'tcx>,
cause: ObligationCause<'tcx>,
recursion_depth: usize,
obligations: &mut PredicateObligations<'tcx>,
) -> Ty<'tcx>
pub fn projection_ty_to_infer( &self, param_env: ParamEnv<'tcx>, projection_ty: AliasTy<'tcx>, cause: ObligationCause<'tcx>, recursion_depth: usize, obligations: &mut PredicateObligations<'tcx>, ) -> Ty<'tcx>
Instead of normalizing an associated type projection, this function generates an inference variable and registers an obligation that this inference variable must be the result of the given projection. This allows us to proceed with projections while they cannot be resolved yet due to missing information or simply due to the lack of access to the trait resolution machinery.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn instantiate_ty_var<R: PredicateEmittingRelation<InferCtxt<'tcx>>>(
&self,
relation: &mut R,
target_is_expected: bool,
target_vid: TyVid,
instantiation_variance: Variance,
source_ty: Ty<'tcx>,
) -> RelateResult<'tcx, ()>
pub fn instantiate_ty_var<R: PredicateEmittingRelation<InferCtxt<'tcx>>>( &self, relation: &mut R, target_is_expected: bool, target_vid: TyVid, instantiation_variance: Variance, source_ty: Ty<'tcx>, ) -> RelateResult<'tcx, ()>
The idea is that we should ensure that the type variable target_vid
is equal to, a subtype of, or a supertype of source_ty
.
For this, we will instantiate target_vid
with a generalized version
of source_ty
. Generalization introduces other inference variables wherever
subtyping could occur. This also does the occurs checks, detecting whether
instantiating target_vid
would result in a cyclic type. We eagerly error
in this case.
This is not expected to be used anywhere except for an implementation of
TypeRelation
. Do not use this, and instead please use At::eq
, for all
other usecases (i.e. setting the value of a type var).
Sourcepub(crate) fn instantiate_const_var<R: PredicateEmittingRelation<InferCtxt<'tcx>>>(
&self,
relation: &mut R,
target_is_expected: bool,
target_vid: ConstVid,
source_ct: Const<'tcx>,
) -> RelateResult<'tcx, ()>
pub(crate) fn instantiate_const_var<R: PredicateEmittingRelation<InferCtxt<'tcx>>>( &self, relation: &mut R, target_is_expected: bool, target_vid: ConstVid, source_ct: Const<'tcx>, ) -> RelateResult<'tcx, ()>
Instantiates the const variable target_vid
with the given constant.
This also tests if the given const ct
contains an inference variable which was previously
unioned with target_vid
. If this is the case, inferring target_vid
to ct
would result in an infinite type as we continuously replace an inference variable
in ct
with ct
itself.
This is especially important as unevaluated consts use their parents generics. They therefore often contain unused args, making these errors far more likely.
A good example of this is the following:
#![feature(generic_const_exprs)]
fn bind<const N: usize>(value: [u8; N]) -> [u8; 3 + 4] {
todo!()
}
fn main() {
let mut arr = Default::default();
arr = bind(arr);
}
Here 3 + 4
ends up as ConstKind::Unevaluated
which uses the generics
of fn bind
(meaning that its args contain N
).
bind(arr)
now infers that the type of arr
must be [u8; N]
.
The assignment arr = bind(arr)
now tries to equate N
with 3 + 4
.
As 3 + 4
contains N
in its args, this must not succeed.
See tests/ui/const-generics/occurs-check/
for more examples where this is relevant.
Sourcefn generalize<T: Into<Term<'tcx>> + Relate<TyCtxt<'tcx>>>(
&self,
span: Span,
structurally_relate_aliases: StructurallyRelateAliases,
target_vid: impl Into<TermVid>,
ambient_variance: Variance,
source_term: T,
) -> RelateResult<'tcx, Generalization<T>>
fn generalize<T: Into<Term<'tcx>> + Relate<TyCtxt<'tcx>>>( &self, span: Span, structurally_relate_aliases: StructurallyRelateAliases, target_vid: impl Into<TermVid>, ambient_variance: Variance, source_term: T, ) -> RelateResult<'tcx, Generalization<T>>
Attempts to generalize source_term
for the type variable target_vid
.
This checks for cycles – that is, whether source_term
references target_vid
.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn enter_forall_and_leak_universe<T>(&self, binder: Binder<'tcx, T>) -> T
pub fn enter_forall_and_leak_universe<T>(&self, binder: Binder<'tcx, T>) -> T
Replaces all bound variables (lifetimes, types, and constants) bound by
binder
with placeholder variables in a new universe. This means that the
new placeholders can only be named by inference variables created after
this method has been called.
This is the first step of checking subtyping when higher-ranked things are involved. For more details visit the relevant sections of the rustc dev guide.
fn enter_forall
should be preferred over this method.
Sourcepub fn enter_forall<T, U>(
&self,
forall: Binder<'tcx, T>,
f: impl FnOnce(T) -> U,
) -> U
pub fn enter_forall<T, U>( &self, forall: Binder<'tcx, T>, f: impl FnOnce(T) -> U, ) -> U
Replaces all bound variables (lifetimes, types, and constants) bound by
binder
with placeholder variables in a new universe and then calls the
closure f
with the instantiated value. The new placeholders can only be
named by inference variables created inside of the closure f
or afterwards.
This is the first step of checking subtyping when higher-ranked things are involved. For more details visit the relevant sections of the rustc dev guide.
This method should be preferred over fn enter_forall_and_leak_universe
.
Sourcepub fn leak_check(
&self,
outer_universe: UniverseIndex,
only_consider_snapshot: Option<&CombinedSnapshot<'tcx>>,
) -> RelateResult<'tcx, ()>
pub fn leak_check( &self, outer_universe: UniverseIndex, only_consider_snapshot: Option<&CombinedSnapshot<'tcx>>, ) -> RelateResult<'tcx, ()>
See RegionConstraintCollector::leak_check. We only check placeholder
leaking into outer_universe
, i.e. placeholders which cannot be named by that
universe.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn fudge_inference_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
pub fn fudge_inference_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
This rather funky routine is used while processing expected
types. What happens here is that we want to propagate a
coercion through the return type of a fn to its
argument. Consider the type of Option::Some
, which is
basically for<T> fn(T) -> Option<T>
. So if we have an
expression Some(&[1, 2, 3])
, and that has the expected type
Option<&[u32]>
, we would like to type check &[1, 2, 3]
with the expectation of &[u32]
. This will cause us to coerce
from &[u32; 3]
to &[u32]
and make the users life more
pleasant.
The way we do this is using fudge_inference_if_ok
. What the
routine actually does is to start a snapshot and execute the
closure f
. In our example above, what this closure will do
is to unify the expectation (Option<&[u32]>
) with the actual
return type (Option<?T>
, where ?T
represents the variable
instantiated for T
). This will cause ?T
to be unified
with &?a [u32]
, where ?a
is a fresh lifetime variable. The
input type (?T
) is then returned by f()
.
At this point, fudge_inference_if_ok
will normalize all type
variables, converting ?T
to &?a [u32]
and end the
snapshot. The problem is that we can’t just return this type
out, because it references the region variable ?a
, and that
region variable was popped when we popped the snapshot.
So what we do is to keep a list (region_vars
, in the code below)
of region variables created during the snapshot (here, ?a
). We
fold the return value and replace any such regions with a new
region variable (e.g., ?b
) and return the result (&?b [u32]
).
This can then be used as the expectation for the fn argument.
The important point here is that, for soundness purposes, the
regions in question are not particularly important. We will
use the expected types to guide coercions, but we will still
type-check the resulting types from those coercions against
the actual types (?T
, Option<?T>
) – and remember that
after the snapshot is popped, the variable ?T
is no longer
unified.
fn fudge_inference<T: TypeFoldable<TyCtxt<'tcx>>>( &self, snapshot_vars: SnapshotVarData, value: T, ) -> T
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
fn variable_lengths(&self) -> VariableLengths
pub fn in_snapshot(&self) -> bool
pub fn num_open_snapshots(&self) -> usize
fn start_snapshot(&self) -> CombinedSnapshot<'tcx>
fn rollback_to(&self, snapshot: CombinedSnapshot<'tcx>)
fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>)
Sourcepub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
Execute f
and commit the bindings if closure f
returns Ok(_)
.
Sourcepub fn probe<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
pub fn probe<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&CombinedSnapshot<'tcx>) -> R,
Execute f
then unroll any bindings it creates.
Sourcepub fn region_constraints_added_in_snapshot(
&self,
snapshot: &CombinedSnapshot<'tcx>,
) -> bool
pub fn region_constraints_added_in_snapshot( &self, snapshot: &CombinedSnapshot<'tcx>, ) -> bool
Scan the constraints produced since snapshot
and check whether
we added any region constraints.
pub fn opaque_types_added_in_snapshot( &self, snapshot: &CombinedSnapshot<'tcx>, ) -> bool
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
pub fn dcx(&self) -> DiagCtxtHandle<'_>
pub fn next_trait_solver(&self) -> bool
pub fn typing_mode( &self, param_env_for_debug_assertion: ParamEnv<'tcx>, ) -> TypingMode<'tcx>
pub fn typing_mode_unchecked(&self) -> TypingMode<'tcx>
pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T
Sourcepub fn type_var_origin(&self, vid: TyVid) -> TypeVariableOrigin
pub fn type_var_origin(&self, vid: TyVid) -> TypeVariableOrigin
Returns the origin of the type variable identified by vid
.
No attempt is made to resolve vid
to its root variable.
Sourcepub fn const_var_origin(&self, vid: ConstVid) -> Option<ConstVariableOrigin>
pub fn const_var_origin(&self, vid: ConstVid) -> Option<ConstVariableOrigin>
Returns the origin of the const variable identified by vid
pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx>
pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>>
pub fn sub_regions( &self, origin: SubregionOrigin<'tcx>, a: Region<'tcx>, b: Region<'tcx>, )
Sourcepub fn member_constraint(
&self,
key: OpaqueTypeKey<'tcx>,
definition_span: Span,
hidden_ty: Ty<'tcx>,
region: Region<'tcx>,
in_regions: Lrc<Vec<Region<'tcx>>>,
)
pub fn member_constraint( &self, key: OpaqueTypeKey<'tcx>, definition_span: Span, hidden_ty: Ty<'tcx>, region: Region<'tcx>, in_regions: Lrc<Vec<Region<'tcx>>>, )
Require that the region r
be equal to one of the regions in
the set regions
.
Sourcepub fn coerce_predicate(
&self,
cause: &ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
predicate: PolyCoercePredicate<'tcx>,
) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)>
pub fn coerce_predicate( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, predicate: PolyCoercePredicate<'tcx>, ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)>
Processes a Coerce
predicate from the fulfillment context.
This is NOT the preferred way to handle coercion, which is to
invoke FnCtxt::coerce
or a similar method (see coercion.rs
).
This method here is actually a fallback that winds up being
invoked when FnCtxt::coerce
encounters unresolved type variables
and records a coercion predicate. Presently, this method is equivalent
to subtype_predicate
– that is, “coercing” a
to b
winds up
actually requiring a <: b
. This is of course a valid coercion,
but it’s not as flexible as FnCtxt::coerce
would be.
(We may refactor this in the future, but there are a number of
practical obstacles. Among other things, FnCtxt::coerce
presently
records adjustments that are required on the HIR in order to perform
the coercion, and we don’t currently have a way to manage that.)
pub fn subtype_predicate( &self, cause: &ObligationCause<'tcx>, param_env: ParamEnv<'tcx>, predicate: PolySubtypePredicate<'tcx>, ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)>
pub fn region_outlives_predicate( &self, cause: &ObligationCause<'tcx>, predicate: PolyRegionOutlivesPredicate<'tcx>, )
Sourcepub fn num_ty_vars(&self) -> usize
pub fn num_ty_vars(&self) -> usize
Number of type variables created so far.
pub fn next_ty_var(&self, span: Span) -> Ty<'tcx>
pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx>
pub fn next_ty_var_id_in_universe( &self, span: Span, universe: UniverseIndex, ) -> TyVid
pub fn next_ty_var_in_universe( &self, span: Span, universe: UniverseIndex, ) -> Ty<'tcx>
pub fn next_const_var(&self, span: Span) -> Const<'tcx>
pub fn next_const_var_with_origin( &self, origin: ConstVariableOrigin, ) -> Const<'tcx>
pub fn next_const_var_in_universe( &self, span: Span, universe: UniverseIndex, ) -> Const<'tcx>
pub fn next_int_var(&self) -> Ty<'tcx>
pub fn next_float_var(&self) -> Ty<'tcx>
Sourcepub fn next_region_var(&self, origin: RegionVariableOrigin) -> Region<'tcx>
pub fn next_region_var(&self, origin: RegionVariableOrigin) -> Region<'tcx>
Creates a fresh region variable with the next available index. The variable will be created in the maximum universe created thus far, allowing it to name any region created thus far.
Sourcepub fn next_region_var_in_universe(
&self,
origin: RegionVariableOrigin,
universe: UniverseIndex,
) -> Region<'tcx>
pub fn next_region_var_in_universe( &self, origin: RegionVariableOrigin, universe: UniverseIndex, ) -> Region<'tcx>
Creates a fresh region variable with the next available index
in the given universe; typically, you can use
next_region_var
and just use the maximal universe.
Sourcepub fn universe_of_region(&self, r: Region<'tcx>) -> UniverseIndex
pub fn universe_of_region(&self, r: Region<'tcx>) -> UniverseIndex
Return the universe that the region r
was created in. For
most regions (e.g., 'static
, named regions from the user,
etc) this is the root universe U0. For inference variables or
placeholders, however, it will return the universe which they
are associated.
Sourcepub fn num_region_vars(&self) -> usize
pub fn num_region_vars(&self) -> usize
Number of region variables created so far.
Sourcepub fn next_nll_region_var(
&self,
origin: NllRegionVariableOrigin,
) -> Region<'tcx>
pub fn next_nll_region_var( &self, origin: NllRegionVariableOrigin, ) -> Region<'tcx>
Just a convenient wrapper of next_region_var
for using during NLL.
Sourcepub fn next_nll_region_var_in_universe(
&self,
origin: NllRegionVariableOrigin,
universe: UniverseIndex,
) -> Region<'tcx>
pub fn next_nll_region_var_in_universe( &self, origin: NllRegionVariableOrigin, universe: UniverseIndex, ) -> Region<'tcx>
Just a convenient wrapper of next_region_var
for using during NLL.
pub fn var_for_def( &self, span: Span, param: &GenericParamDef, ) -> GenericArg<'tcx>
Sourcepub fn fresh_args_for_item(
&self,
span: Span,
def_id: DefId,
) -> GenericArgsRef<'tcx>
pub fn fresh_args_for_item( &self, span: Span, def_id: DefId, ) -> GenericArgsRef<'tcx>
Given a set of generics defined on a type or impl, returns the generic parameters mapping each type/region parameter to a fresh inference variable.
Sourcepub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed>
pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed>
Returns true
if errors have been reported since this infcx was
created. This is sometimes used as a heuristic to skip
reporting errors that often occur as a result of earlier
errors, but where it’s hard to be 100% sure (e.g., unresolved
inference variables, regionck errors).
Sourcepub fn set_tainted_by_errors(&self, e: ErrorGuaranteed)
pub fn set_tainted_by_errors(&self, e: ErrorGuaranteed)
Set the “tainted by errors” flag to true. We call this when we observe an error from a prior pass.
pub fn region_var_origin(&self, vid: RegionVid) -> RegionVariableOrigin
Sourcepub fn get_region_var_origins(&self) -> VarInfos
pub fn get_region_var_origins(&self) -> VarInfos
Clone the list of variable regions. This is used only during NLL processing to put the set of region variables into the NLL region context.
pub fn take_opaque_types( &self, ) -> FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>
pub fn clone_opaque_types( &self, ) -> FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>
pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String
Sourcepub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, UniverseIndex>
pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, UniverseIndex>
If TyVar(vid)
resolves to a type, return that type. Else, return the
universe index of TyVar(vid)
.
pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx>
pub fn shallow_resolve_const(&self, ct: Const<'tcx>) -> Const<'tcx>
pub fn root_var(&self, var: TyVid) -> TyVid
pub fn root_const_var(&self, var: ConstVid) -> ConstVid
Sourcepub fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'tcx>
pub fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'tcx>
Resolves an int var to a rigid int type, if it was constrained to one, or else the root int var in the unification table.
Sourcepub fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'tcx>
pub fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'tcx>
Resolves a float var to a rigid int type, if it was constrained to one, or else the root float var in the unification table.
Sourcepub fn resolve_vars_if_possible<T>(&self, value: T) -> Twhere
T: TypeFoldable<TyCtxt<'tcx>>,
pub fn resolve_vars_if_possible<T>(&self, value: T) -> Twhere
T: TypeFoldable<TyCtxt<'tcx>>,
Where possible, replaces type/const variables in
value
with their final value. Note that region variables
are unaffected. If a type/const variable has not been unified, it
is left as is. This is an idempotent operation that does
not affect inference state in any way and so you can do it
at will.
pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> Twhere
T: TypeFoldable<TyCtxt<'tcx>>,
pub fn probe_const_var( &self, vid: ConstVid, ) -> Result<Const<'tcx>, UniverseIndex>
Sourcepub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(
&self,
value: T,
) -> Result<T, FixupError>
pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>( &self, value: T, ) -> Result<T, FixupError>
Attempts to resolve all type/region/const variables in
value
. Region inference must have been run already (e.g.,
by calling resolve_regions_and_report_errors
). If some
variable was never unified, an Err
results.
This method is idempotent, but it not typically not invoked except during the writeback phase.
pub fn instantiate_binder_with_fresh_vars<T>( &self, span: Span, lbrct: BoundRegionConversionTime, value: Binder<'tcx, T>, ) -> T
Sourcepub(crate) fn verify_generic_bound(
&self,
origin: SubregionOrigin<'tcx>,
kind: GenericKind<'tcx>,
a: Region<'tcx>,
bound: VerifyBound<'tcx>,
)
pub(crate) fn verify_generic_bound( &self, origin: SubregionOrigin<'tcx>, kind: GenericKind<'tcx>, a: Region<'tcx>, bound: VerifyBound<'tcx>, )
Sourcepub fn closure_kind(&self, closure_ty: Ty<'tcx>) -> Option<ClosureKind>
pub fn closure_kind(&self, closure_ty: Ty<'tcx>) -> Option<ClosureKind>
Obtains the latest type of the given closure; this may be a
closure in the current function, in which case its
ClosureKind
may not yet be known.
pub fn universe(&self) -> UniverseIndex
Sourcepub fn create_next_universe(&self) -> UniverseIndex
pub fn create_next_universe(&self) -> UniverseIndex
Creates and return a fresh universe that extends all previous
universes. Updates self.universe
to that new universe.
pub fn try_const_eval_resolve( &self, param_env: ParamEnv<'tcx>, unevaluated: UnevaluatedConst<'tcx>, span: Span, ) -> Result<Const<'tcx>, ErrorHandled>
Sourcepub fn const_eval_resolve(
&self,
param_env: ParamEnv<'tcx>,
unevaluated: UnevaluatedConst<'tcx>,
span: Span,
) -> EvalToValTreeResult<'tcx>
pub fn const_eval_resolve( &self, param_env: ParamEnv<'tcx>, unevaluated: UnevaluatedConst<'tcx>, span: Span, ) -> EvalToValTreeResult<'tcx>
Resolves and evaluates a constant.
The constant can be located on a trait like <A as B>::C
, in which case the given
generic parameters and environment are used to resolve the constant. Alternatively if the
constant has generic parameters in scope the instantiations are used to evaluate the value
of the constant. For example in fn foo<T>() { let _ = [0; bar::<T>()]; }
the repeat count
constant bar::<T>()
requires a instantiation for T
, if the instantiation for T
is
still too generic for the constant to be evaluated then Err(ErrorHandled::TooGeneric)
is
returned.
This handles inferences variables within both param_env
and args
by
performing the operation on their respective canonical forms.
Sourcepub fn is_ty_infer_var_definitely_unchanged<'a>(
&'a self,
) -> impl Fn(TyOrConstInferVar) -> bool + Captures<'tcx> + 'a
pub fn is_ty_infer_var_definitely_unchanged<'a>( &'a self, ) -> impl Fn(TyOrConstInferVar) -> bool + Captures<'tcx> + 'a
The returned function is used in a fast path. If it returns true
the variable is
unchanged, false
indicates that the status is unknown.
Sourcepub fn ty_or_const_infer_var_changed(
&self,
infer_var: TyOrConstInferVar,
) -> bool
pub fn ty_or_const_infer_var_changed( &self, infer_var: TyOrConstInferVar, ) -> bool
ty_or_const_infer_var_changed
is equivalent to one of these two:
shallow_resolve(ty) != ty
(wherety.kind = ty::Infer(_)
)shallow_resolve(ct) != ct
(wherect.kind = ty::ConstKind::Infer(_)
)
However, ty_or_const_infer_var_changed
is more efficient. It’s always
inlined, despite being large, because it has only two call sites that
are extremely hot (both in traits::fulfill
’s checking of stalled_on
inference variables), and it handles both Ty
and ty::Const
without
having to resort to storing full GenericArg
s in stalled_on
.
Sourcepub fn attach_obligation_inspector(&self, inspector: ObligationInspector<'tcx>)
pub fn attach_obligation_inspector(&self, inspector: ObligationInspector<'tcx>)
Attach a callback to be invoked on each root obligation evaluated in the new trait solver.
Source§impl<'tcx> InferCtxt<'tcx>
impl<'tcx> InferCtxt<'tcx>
Sourcepub fn find_block_span(&self, block: &'tcx Block<'tcx>) -> Span
pub fn find_block_span(&self, block: &'tcx Block<'tcx>) -> Span
Given a [hir::Block
], get the span of its last expression or
statement, peeling off any inner blocks.
Sourcepub fn find_block_span_from_hir_id(&self, hir_id: HirId) -> Span
pub fn find_block_span_from_hir_id(&self, hir_id: HirId) -> Span
Given a [hir::HirId
] for a block, get the span of its last expression
or statement, peeling off any inner blocks.
Trait Implementations§
Source§impl<'tcx> InferCtxtLike for InferCtxt<'tcx>
impl<'tcx> InferCtxtLike for InferCtxt<'tcx>
type Interner = TyCtxt<'tcx>
fn cx(&self) -> TyCtxt<'tcx>
Source§fn next_trait_solver(&self) -> bool
fn next_trait_solver(&self) -> bool
fn typing_mode( &self, param_env_for_debug_assertion: ParamEnv<'tcx>, ) -> TypingMode<'tcx>
fn universe(&self) -> UniverseIndex
fn create_next_universe(&self) -> UniverseIndex
fn universe_of_ty(&self, vid: TyVid) -> Option<UniverseIndex>
fn universe_of_lt(&self, lt: RegionVid) -> Option<UniverseIndex>
fn universe_of_ct(&self, ct: ConstVid) -> Option<UniverseIndex>
fn root_ty_var(&self, var: TyVid) -> TyVid
fn root_const_var(&self, var: ConstVid) -> ConstVid
fn opportunistic_resolve_ty_var(&self, vid: TyVid) -> Ty<'tcx>
fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'tcx>
fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'tcx>
fn opportunistic_resolve_ct_var(&self, vid: ConstVid) -> Const<'tcx>
fn opportunistic_resolve_lt_var(&self, vid: RegionVid) -> Region<'tcx>
fn next_ty_infer(&self) -> Ty<'tcx>
fn next_const_infer(&self) -> Const<'tcx>
fn fresh_args_for_item(&self, def_id: DefId) -> GenericArgsRef<'tcx>
fn instantiate_binder_with_infer<T: TypeFoldable<TyCtxt<'tcx>> + Copy>( &self, value: Binder<'tcx, T>, ) -> T
fn enter_forall<T: TypeFoldable<TyCtxt<'tcx>> + Copy, U>( &self, value: Binder<'tcx, T>, f: impl FnOnce(T) -> U, ) -> U
fn equate_ty_vids_raw(&self, a: TyVid, b: TyVid)
fn equate_int_vids_raw(&self, a: IntVid, b: IntVid)
fn equate_float_vids_raw(&self, a: FloatVid, b: FloatVid)
fn equate_const_vids_raw(&self, a: ConstVid, b: ConstVid)
fn instantiate_ty_var_raw<R: PredicateEmittingRelation<Self>>( &self, relation: &mut R, target_is_expected: bool, target_vid: TyVid, instantiation_variance: Variance, source_ty: Ty<'tcx>, ) -> RelateResult<'tcx, ()>
fn instantiate_int_var_raw(&self, vid: IntVid, value: IntVarValue)
fn instantiate_float_var_raw(&self, vid: FloatVid, value: FloatVarValue)
fn instantiate_const_var_raw<R: PredicateEmittingRelation<Self>>( &self, relation: &mut R, target_is_expected: bool, target_vid: ConstVid, source_ct: Const<'tcx>, ) -> RelateResult<'tcx, ()>
fn set_tainted_by_errors(&self, e: ErrorGuaranteed)
fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx>
fn shallow_resolve_const(&self, ct: Const<'tcx>) -> Const<'tcx>
fn resolve_vars_if_possible<T>(&self, value: T) -> Twhere
T: TypeFoldable<TyCtxt<'tcx>>,
fn probe<T>(&self, probe: impl FnOnce() -> T) -> T
fn sub_regions(&self, sub: Region<'tcx>, sup: Region<'tcx>)
fn equate_regions(&self, a: Region<'tcx>, b: Region<'tcx>)
fn register_ty_outlives(&self, ty: Ty<'tcx>, r: Region<'tcx>)
Source§impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for LatticeOp<'_, 'tcx>
impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for LatticeOp<'_, 'tcx>
fn span(&self) -> Span
Source§fn structurally_relate_aliases(&self) -> StructurallyRelateAliases
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases
No
unless you’re equating in some specific locations of the
new solver. See the comments in these use-cases for more details.fn param_env(&self) -> ParamEnv<'tcx>
Source§fn register_predicates(
&mut self,
preds: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, Predicate<'tcx>>>,
)
fn register_predicates( &mut self, preds: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, Predicate<'tcx>>>, )
param_env
of the obligation.Source§fn register_goals(
&mut self,
goals: impl IntoIterator<Item = Goal<'tcx, Predicate<'tcx>>>,
)
fn register_goals( &mut self, goals: impl IntoIterator<Item = Goal<'tcx, Predicate<'tcx>>>, )
Source§fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
AliasRelate
obligation(s) that both types must be related to each other.Source§impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for TypeRelating<'_, 'tcx>
impl<'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for TypeRelating<'_, 'tcx>
fn span(&self) -> Span
fn param_env(&self) -> ParamEnv<'tcx>
Source§fn structurally_relate_aliases(&self) -> StructurallyRelateAliases
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases
No
unless you’re equating in some specific locations of the
new solver. See the comments in these use-cases for more details.Source§fn register_predicates(
&mut self,
preds: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, Predicate<'tcx>>>,
)
fn register_predicates( &mut self, preds: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, Predicate<'tcx>>>, )
param_env
of the obligation.Source§fn register_goals(
&mut self,
goals: impl IntoIterator<Item = Goal<'tcx, Predicate<'tcx>>>,
)
fn register_goals( &mut self, goals: impl IntoIterator<Item = Goal<'tcx, Predicate<'tcx>>>, )
Source§fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
AliasRelate
obligation(s) that both types must be related to each other.Source§impl<'cx, 'tcx> TypeOutlivesDelegate<'tcx> for &'cx InferCtxt<'tcx>
impl<'cx, 'tcx> TypeOutlivesDelegate<'tcx> for &'cx InferCtxt<'tcx>
fn push_sub_region_constraint( &mut self, origin: SubregionOrigin<'tcx>, a: Region<'tcx>, b: Region<'tcx>, _constraint_category: ConstraintCategory<'tcx>, )
fn push_verify( &mut self, origin: SubregionOrigin<'tcx>, kind: GenericKind<'tcx>, a: Region<'tcx>, bound: VerifyBound<'tcx>, )
Auto Trait Implementations§
impl<'tcx> DynSend for InferCtxt<'tcx>
impl<'tcx> !DynSync for InferCtxt<'tcx>
impl<'tcx> !Freeze for InferCtxt<'tcx>
impl<'tcx> !RefUnwindSafe for InferCtxt<'tcx>
impl<'tcx> !Send for InferCtxt<'tcx>
impl<'tcx> !Sync for InferCtxt<'tcx>
impl<'tcx> Unpin for InferCtxt<'tcx>
impl<'tcx> !UnwindSafe for InferCtxt<'tcx>
Blanket Implementations§
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
§impl<T, R> CollectAndApply<T, R> for T
impl<T, R> CollectAndApply<T, R> for T
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§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<T> Pointable for T
impl<T> Pointable for T
§impl<Infcx> RelateExt for Infcxwhere
Infcx: InferCtxtLike,
impl<Infcx> RelateExt for Infcxwhere
Infcx: InferCtxtLike,
fn relate<T>( &self, param_env: <<Infcx as InferCtxtLike>::Interner as Interner>::ParamEnv, lhs: T, variance: Variance, rhs: T, ) -> Result<Vec<Goal<<Infcx as InferCtxtLike>::Interner, <<Infcx as InferCtxtLike>::Interner as Interner>::Predicate>>, TypeError<<Infcx as InferCtxtLike>::Interner>>
fn eq_structurally_relating_aliases<T>( &self, param_env: <<Infcx as InferCtxtLike>::Interner as Interner>::ParamEnv, lhs: T, rhs: T, ) -> Result<Vec<Goal<<Infcx as InferCtxtLike>::Interner, <<Infcx as InferCtxtLike>::Interner as Interner>::Predicate>>, TypeError<<Infcx as InferCtxtLike>::Interner>>
§impl<I, T, U> Upcast<I, U> for Twhere
U: UpcastFrom<I, T>,
impl<I, T, U> Upcast<I, U> for Twhere
U: UpcastFrom<I, T>,
§impl<I, T> UpcastFrom<I, T> for T
impl<I, T> UpcastFrom<I, T> for T
fn upcast_from(from: T, _tcx: I) -> 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_error: &CycleError, _guar: ErrorGuaranteed, ) -> T
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
impl<'a, T> Captures<'a> for Twhere
T: ?Sized,
impl<T> ErasedDestructor for Twhere
T: 'static,
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: 720 bytes