Struct InferCtxt

Source
pub struct InferCtxt<'db> {
    pub interner: DbInterner<'db>,
    pub inner: RefCell<InferCtxtInner<'db>>,
    /* private fields */
}

Fields§

§interner: DbInterner<'db>§inner: RefCell<InferCtxtInner<'db>>

Implementations§

Source§

impl<'db> InferCtxt<'db>

Source

pub fn at<'a>( &'a self, cause: &'a ObligationCause, param_env: ParamEnv<'db>, ) -> At<'a, 'db>

Source

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.

Source

pub fn fork_with_typing_mode( &self, typing_mode: TypingMode<DbInterner<'db>>, ) -> 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<'db> InferCtxt<'db>

Source

pub fn instantiate_canonical<T>( &self, canonical: &Canonical<'db, T>, ) -> (T, CanonicalVarValues<'db>)
where T: TypeFoldable<DbInterner<'db>>,

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.

Source

pub fn instantiate_canonical_var( &self, cv_info: CanonicalVarKind<DbInterner<'db>>, universe_map: impl Fn(UniverseIndex) -> UniverseIndex, ) -> GenericArg<'db>

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<'db> InferCtxt<'db>

Source

pub fn register_hidden_type_in_storage( &self, opaque_type_key: OpaqueTypeKey<'db>, hidden_ty: OpaqueHiddenType<'db>, ) -> Option<Ty<'db>>

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.

Source§

impl<'db> InferCtxt<'db>

Source

pub fn instantiate_ty_var<R: PredicateEmittingRelation<InferCtxt<'db>>>( &self, relation: &mut R, target_is_expected: bool, target_vid: TyVid, instantiation_variance: Variance, source_ty: Ty<'db>, ) -> RelateResult<'db, ()>

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).

Source§

impl<'db> InferCtxt<'db>

Source

pub fn enter_forall_and_leak_universe<T>(&self, binder: Binder<'db, T>) -> T
where T: TypeFoldable<DbInterner<'db>> + Clone,

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.

Source

pub fn enter_forall<T, U>( &self, forall: Binder<'db, T>, f: impl FnOnce(T) -> U, ) -> U
where T: TypeFoldable<DbInterner<'db>> + Clone,

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.

Source§

impl<'db> InferCtxt<'db>

Source

pub fn in_snapshot(&self) -> bool

Source

pub fn num_open_snapshots(&self) -> usize

Source

pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
where F: FnOnce(&CombinedSnapshot) -> Result<T, E>,

Execute f and commit the bindings if closure f returns Ok(_).

Source

pub fn probe<R, F>(&self, f: F) -> R
where F: FnOnce(&CombinedSnapshot) -> R,

Execute f then unroll any bindings it creates.

Source§

impl<'db> InferCtxt<'db>

Source

pub fn typing_mode(&self) -> TypingMode<'db>

Source

pub fn typing_mode_unchecked(&self) -> TypingMode<'db>

Source

pub fn unresolved_variables(&self) -> Vec<Ty<'db>>

Source

pub fn sub_regions(&self, a: Region<'db>, b: Region<'db>)

Source

pub fn coerce_predicate( &self, cause: &ObligationCause, param_env: ParamEnv<'db>, predicate: PolyCoercePredicate<'db>, ) -> Result<InferResult<'db, ()>, (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.)

Source

pub fn subtype_predicate( &self, cause: &ObligationCause, param_env: ParamEnv<'db>, predicate: PolySubtypePredicate<'db>, ) -> Result<InferResult<'db, ()>, (TyVid, TyVid)>

Source

pub fn region_outlives_predicate( &self, cause: &ObligationCause, predicate: PolyRegionOutlivesPredicate<'db>, )

Source

pub fn num_ty_vars(&self) -> usize

Number of type variables created so far.

Source

pub fn next_ty_var(&self) -> Ty<'db>

Source

pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'db>

Source

pub fn next_ty_var_id_in_universe(&self, universe: UniverseIndex) -> TyVid

Source

pub fn next_ty_var_in_universe(&self, universe: UniverseIndex) -> Ty<'db>

Source

pub fn next_const_var(&self) -> Const<'db>

Source

pub fn next_const_var_with_origin( &self, origin: ConstVariableOrigin, ) -> Const<'db>

Source

pub fn next_const_var_in_universe(&self, universe: UniverseIndex) -> Const<'db>

Source

pub fn next_int_var(&self) -> Ty<'db>

Source

pub fn next_float_var(&self) -> Ty<'db>

Source

pub fn next_region_var(&self) -> Region<'db>

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.

Source

pub fn next_region_var_in_universe( &self, universe: UniverseIndex, ) -> Region<'db>

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.

Source

pub fn next_term_var_of_kind(&self, term: Term<'db>) -> Term<'db>

Source

pub fn universe_of_region(&self, r: Region<'db>) -> 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.

Source

pub fn num_region_vars(&self) -> usize

Number of region variables created so far.

Source

pub fn next_nll_region_var(&self) -> Region<'db>

Just a convenient wrapper of next_region_var for using during NLL.

Source

pub fn next_nll_region_var_in_universe( &self, universe: UniverseIndex, ) -> Region<'db>

Just a convenient wrapper of next_region_var for using during NLL.

Source

pub fn fresh_args_for_item(&self, def_id: SolverDefId) -> GenericArgs<'db>

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.

Source

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).

Source

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.

Source

pub fn take_opaque_types( &self, ) -> Vec<(OpaqueTypeKey<'db>, OpaqueHiddenType<'db>)>

Source

pub fn clone_opaque_types( &self, ) -> Vec<(OpaqueTypeKey<'db>, OpaqueHiddenType<'db>)>

Source

pub fn can_define_opaque_ty(&self, id: impl Into<SolverDefId>) -> bool

Source

pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'db>, UniverseIndex>

If TyVar(vid) resolves to a type, return that type. Else, return the universe index of TyVar(vid).

Source

pub fn shallow_resolve(&self, ty: Ty<'db>) -> Ty<'db>

Source

pub fn shallow_resolve_const(&self, ct: Const<'db>) -> Const<'db>

Source

pub fn root_var(&self, var: TyVid) -> TyVid

Source

pub fn root_const_var(&self, var: ConstVid) -> ConstVid

Source

pub fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'db>

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.

Source

pub fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'db>

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.

Source

pub fn resolve_vars_if_possible<T>(&self, value: T) -> T
where T: TypeFoldable<DbInterner<'db>>,

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.

Source

pub fn probe_const_var( &self, vid: ConstVid, ) -> Result<Const<'db>, UniverseIndex>

Source

pub fn instantiate_binder_with_fresh_vars<T>( &self, lbrct: BoundRegionConversionTime, value: Binder<'db, T>, ) -> T
where T: TypeFoldable<DbInterner<'db>> + Clone,

Source

pub fn closure_kind(&self, closure_ty: Ty<'db>) -> 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.

Source

pub fn universe(&self) -> UniverseIndex

Source

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.

Source

pub fn is_ty_infer_var_definitely_unchanged<'a>( &'a self, ) -> impl Fn(TyOrConstInferVar) -> bool + Captures<'db> + '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.

Source

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 (where ty.kind = Infer(_))
  • shallow_resolve(ct) != ct (where ct.kind = 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 Const without having to resort to storing full GenericArgs in stalled_on.

Trait Implementations§

Source§

impl<'db> InferCtxtLike for InferCtxt<'db>

Source§

type Interner = DbInterner<'db>

Source§

type OpaqueTypeStorageEntries = OpaqueTypeStorageEntries

Source§

fn cx(&self) -> DbInterner<'db>

Source§

fn next_trait_solver(&self) -> bool

Whether the new trait solver is enabled. This only exists because rustc shares code between the new and old trait solvers; for all other users, this should always be true. If this is unknowingly false and you try to use the new trait solver, things will break badly.
Source§

fn typing_mode(&self) -> TypingMode<DbInterner<'db>>

Source§

fn universe(&self) -> UniverseIndex

Source§

fn create_next_universe(&self) -> UniverseIndex

Source§

fn universe_of_ty(&self, vid: TyVid) -> Option<UniverseIndex>

Source§

fn universe_of_lt(&self, lt: RegionVid) -> Option<UniverseIndex>

Source§

fn universe_of_ct(&self, ct: ConstVid) -> Option<UniverseIndex>

Source§

fn root_ty_var(&self, var: TyVid) -> TyVid

Source§

fn root_const_var(&self, var: ConstVid) -> ConstVid

Source§

fn opportunistic_resolve_ty_var(&self, vid: TyVid) -> Ty<'db>

Source§

fn opportunistic_resolve_int_var(&self, vid: IntVid) -> Ty<'db>

Source§

fn opportunistic_resolve_float_var(&self, vid: FloatVid) -> Ty<'db>

Source§

fn opportunistic_resolve_ct_var(&self, vid: ConstVid) -> Const<'db>

Source§

fn opportunistic_resolve_lt_var(&self, vid: RegionVid) -> Region<'db>

Source§

fn is_changed_arg(&self, arg: <Self::Interner as Interner>::GenericArg) -> bool

Source§

fn next_ty_infer(&self) -> Ty<'db>

Source§

fn next_region_infer(&self) -> <Self::Interner as Interner>::Region

Source§

fn next_const_infer(&self) -> Const<'db>

Source§

fn fresh_args_for_item(&self, def_id: SolverDefId) -> GenericArgs<'db>

Source§

fn instantiate_binder_with_infer<T: TypeFoldable<DbInterner<'db>> + Clone>( &self, value: Binder<'db, T>, ) -> T

Source§

fn enter_forall<T: TypeFoldable<DbInterner<'db>> + Clone, U>( &self, value: Binder<'db, T>, f: impl FnOnce(T) -> U, ) -> U

Source§

fn equate_ty_vids_raw(&self, a: TyVid, b: TyVid)

Source§

fn equate_int_vids_raw(&self, a: IntVid, b: IntVid)

Source§

fn equate_float_vids_raw(&self, a: FloatVid, b: FloatVid)

Source§

fn equate_const_vids_raw(&self, a: ConstVid, b: ConstVid)

Source§

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<'db>, ) -> RelateResult<'db, ()>

Source§

fn instantiate_int_var_raw(&self, vid: IntVid, value: IntVarValue)

Source§

fn instantiate_float_var_raw(&self, vid: FloatVid, value: FloatVarValue)

Source§

fn instantiate_const_var_raw<R: PredicateEmittingRelation<Self>>( &self, relation: &mut R, target_is_expected: bool, target_vid: ConstVid, source_ct: Const<'db>, ) -> RelateResult<'db, ()>

Source§

fn set_tainted_by_errors(&self, e: ErrorGuaranteed)

Source§

fn shallow_resolve(&self, ty: Ty<'db>) -> Ty<'db>

Source§

fn shallow_resolve_const(&self, ct: Const<'db>) -> Const<'db>

Source§

fn resolve_vars_if_possible<T>(&self, value: T) -> T
where T: TypeFoldable<DbInterner<'db>>,

Source§

fn probe<T>(&self, probe: impl FnOnce() -> T) -> T

Source§

fn sub_regions(&self, sub: Region<'db>, sup: Region<'db>, span: Span)

Source§

fn equate_regions(&self, a: Region<'db>, b: Region<'db>, span: Span)

Source§

fn register_ty_outlives(&self, ty: Ty<'db>, r: Region<'db>, span: Span)

Source§

fn opaque_types_storage_num_entries(&self) -> OpaqueTypeStorageEntries

Source§

fn clone_opaque_types_lookup_table(&self) -> Vec<(OpaqueTypeKey<'db>, Ty<'db>)>

Source§

fn clone_duplicate_opaque_types(&self) -> Vec<(OpaqueTypeKey<'db>, Ty<'db>)>

Source§

fn clone_opaque_types_added_since( &self, prev_entries: OpaqueTypeStorageEntries, ) -> Vec<(OpaqueTypeKey<'db>, Ty<'db>)>

Source§

fn register_hidden_type_in_storage( &self, opaque_type_key: OpaqueTypeKey<'db>, hidden_ty: Ty<'db>, _span: Span, ) -> Option<Ty<'db>>

Source§

fn add_duplicate_opaque_type( &self, opaque_type_key: OpaqueTypeKey<'db>, hidden_ty: Ty<'db>, _span: Span, )

Source§

fn reset_opaque_types(&self)

Auto Trait Implementations§

§

impl<'db> !Freeze for InferCtxt<'db>

§

impl<'db> !RefUnwindSafe for InferCtxt<'db>

§

impl<'db> Send for InferCtxt<'db>

§

impl<'db> !Sync for InferCtxt<'db>

§

impl<'db> Unpin for InferCtxt<'db>

§

impl<'db> !UnwindSafe for InferCtxt<'db>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
§

impl<T> Cast for T

§

fn cast<U>(self, interner: <U as HasInterner>::Interner) -> U
where Self: CastTo<U>, U: HasInterner,

Cast a value to type U using CastTo.
§

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

§

fn collect_and_apply<I, F>(iter: I, f: F) -> R
where 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

§

impl<T> IntoBox<dyn Any> for T
where T: Any,

§

fn into_box(self) -> Box<dyn Any>

Convert self into the appropriate boxed form.
§

impl<T> IntoBox<dyn Any + Send> for T
where T: Any + Send,

§

fn into_box(self) -> Box<dyn Any + Send>

Convert self into the appropriate boxed form.
Source§

impl<T> IntoEither for T

Source§

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

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

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

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

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<Infcx> RelateExt for Infcx
where Infcx: InferCtxtLike,

§

fn relate<T>( &self, param_env: <<Infcx as InferCtxtLike>::Interner as Interner>::ParamEnv, lhs: T, variance: Variance, rhs: T, span: <<Infcx as InferCtxtLike>::Interner as Interner>::Span, ) -> Result<Vec<Goal<<Infcx as InferCtxtLike>::Interner, <<Infcx as InferCtxtLike>::Interner as Interner>::Predicate>>, TypeError<<Infcx as InferCtxtLike>::Interner>>
where T: Relate<<Infcx as InferCtxtLike>::Interner>,

§

fn eq_structurally_relating_aliases<T>( &self, param_env: <<Infcx as InferCtxtLike>::Interner as Interner>::ParamEnv, lhs: T, rhs: T, span: <<Infcx as InferCtxtLike>::Interner as Interner>::Span, ) -> Result<Vec<Goal<<Infcx as InferCtxtLike>::Interner, <<Infcx as InferCtxtLike>::Interner as Interner>::Predicate>>, TypeError<<Infcx as InferCtxtLike>::Interner>>
where T: Relate<<Infcx as InferCtxtLike>::Interner>,

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
§

impl<I, T, U> Upcast<I, U> for T
where U: UpcastFrom<I, T>,

§

fn upcast(self, interner: I) -> U

§

impl<I, T> UpcastFrom<I, T> for T

§

fn upcast_from(from: T, _tcx: I) -> T

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<'a, T> Captures<'a> for T
where T: ?Sized,