pub struct SelectionContext<'cx, 'tcx> {
    pub infcx: &'cx InferCtxt<'tcx>,
    freshener: TypeFreshener<'cx, 'tcx>,
    intercrate_ambiguity_causes: Option<FxIndexSet<IntercrateAmbiguityCause>>,
    query_mode: TraitQueryMode,
}

Fields§

§infcx: &'cx InferCtxt<'tcx>§freshener: TypeFreshener<'cx, 'tcx>

Freshener used specifically for entries on the obligation stack. This ensures that all entries on the stack at one time will have the same set of placeholder entries, which is important for checking for trait bounds that recursively require themselves.

§intercrate_ambiguity_causes: Option<FxIndexSet<IntercrateAmbiguityCause>>

If intercrate is set, we remember predicates which were considered ambiguous because of impls potentially added in other crates. This is used in coherence to give improved diagnostics. We don’t do his until we detect a coherence error because it can lead to false overflow results (#47139) and because always computing it may negatively impact performance.

§query_mode: TraitQueryMode

The mode that trait queries run in, which informs our error handling policy. In essence, canonicalized queries need their errors propagated rather than immediately reported because we do not have accurate spans.

Implementations§

Given an obligation like <SomeTrait for T>, searches the obligations that the caller supplied to find out whether it is listed among them.

Never affects the inference environment.

Checks for the artificial impl that the compiler will create for an obligation like X : FnMut<..> where X is a closure type.

Note: the type parameters on a closure candidate are modeled as output type parameters and hence do not affect whether this trait is a match or not. They will be unified during the confirmation step.

Implements one of the Fn() family for a fn pointer.

Searches for impls that might apply to obligation.

Searches for impls that might apply to obligation.

Temporary migration for #89190

Searches for unsizing that might apply to obligation.

Assembles the trait which are built-in to the language itself: Copy, Clone and Sized.

This handles the case where an auto trait Foo impl is being used. The idea is that the impl applies to X : Foo if the following conditions are met:

  1. For each constituent type Y in X, Y : Foo holds
  2. For each where-clause C declared on Foo, [Self => X] C holds.

See confirm_auto_impl_candidate.

In the case of closure types and fn pointers, we currently treat the input type parameters on the trait as outputs. This means that when we have a match we have only considered the self type, so we have to go back and make sure to relate the argument types too. This is kind of wrong, but since we control the full set of impls, also not that wrong, and it DOES yield better error messages (since we don’t report errors as if there is no applicable impl, but rather report errors are about mismatched argument types.

Here is an example. Imagine we have a closure expression and we desugared it so that the type of the expression is Closure, and Closure expects i32 as argument. Then it is “as if” the compiler generated this impl:

impl Fn(i32) for Closure { ... }

Now imagine our obligation is Closure: Fn(usize). So far we have matched the self type Closure. At this point we’ll compare the i32 to usize and generate an error.

Note that this checking occurs after the impl has selected, because these output type parameters should not affect the selection of the impl. Therefore, if there is a mismatch, we report an error to the user.

Enables tracking of intercrate ambiguity causes. See the documentation of Self::intercrate_ambiguity_causes for more.

Gets the intercrate ambiguity causes collected since tracking was enabled and disables tracking at the same time. If tracking is not enabled, just returns an empty vector.

Attempts to satisfy the obligation. If successful, this will affect the surrounding type environment by performing unification.

Evaluates whether the obligation obligation can be satisfied (by any means).

Evaluates whether the obligation obligation can be satisfied and returns an EvaluationResult. This is meant for the initial call.

Evaluates the predicates in predicates recursively. Note that this applies projections in the predicates, and therefore is run within an inference probe.

If there is any previous entry on the stack that precisely matches this obligation, then we can assume that the obligation is satisfied for now (still all other conditions must be met of course). One obvious case this comes up is marker traits like Send. Think of a linked list:

struct List<T> { data: T, next: Option<Box<List<T>>> }

Box<List<T>> will be Send if T is Send and Option<Box<List<T>>> is Send, and in turn Option<Box<List<T>>> is Send if Box<List<T>> is Send.

Note that we do this comparison using the fresh_trait_ref fields. Because these have all been freshened using self.freshener, we can be sure that (a) this will not affect the inferencer state and (b) that if we see two fresh regions with the same index, they refer to the same unbound type variable.

For defaulted traits, we use a co-inductive strategy to solve, so that recursion is ok. This routine returns true if the top of the stack (cycle[0]):

  • is a defaulted trait,
  • it also appears in the backtrace at some position X,
  • all the predicates at positions X.. between X and the top are also defaulted traits.

Further evaluates candidate to decide whether all type parameters match and whether nested obligations are met. Returns whether candidate remains viable after this further scrutiny.

For various reasons, it’s possible for a subobligation to have a lower recursion_depth than the obligation used to create it. Projection sub-obligations may be returned from the projection cache, which results in obligations with an ‘old’ recursion_depth. Additionally, methods like InferCtxt.subtype_predicate produce subobligations without taking in a ‘parent’ depth, causing the generated subobligations to have a recursion_depth of 0.

To ensure that obligation_depth never decreases, we force all subobligations to have at least the depth of the original obligation.

Checks that the recursion limit has not been exceeded.

The weird return type of this function allows it to be used with the try (?) operator within certain functions.

filter_impls filters constant trait obligations and candidates that have a positive impl for a negative goal and a negative impl for a positive goal

filter_reservation_impls filter reservation impl for any goal as ambiguous

Returns true if the global caches can be used.

Determines whether can we safely cache the result of selecting an obligation. This is almost always true, except when dealing with certain ParamCandidates.

Ordinarily, a ParamCandidate will contain no inference variables, since it was usually produced directly from a DefId. However, certain cases (currently only librustdoc’s blanket impl finder), a ParamEnv may be explicitly constructed with inference types. When this is the case, we do not want to cache the resulting selection candidate. This is due to the fact that it might not always be possible to equate the obligation’s trait ref and the candidate’s trait ref, if more constraints end up getting added to an inference variable.

Because of this, we always want to re-run the full selection process for our obligation the next time we see it, since we might end up picking a different SelectionCandidate (or none at all).

Matches a predicate against the bounds of its self type.

Given an obligation like <T as Foo>::Bar: Baz where the self type is a projection, look at the bounds of T::Bar, see if we can find a Baz bound. We return indexes into the list returned by tcx.item_bounds for any applicable bounds.

Equates the trait in obligation with trait bound. If the two traits can be equated and the normalized trait bound doesn’t contain inference variables or placeholders, the normalized bound is returned.

Return Yes if the obligation’s predicate type applies to the env_predicate, and No if it does not. Return Ambiguous in the case that the projection type is a GAT, and applying this env_predicate constrains any of the obligation’s GAT substitutions.

This behavior is a somewhat of a hack to prevent over-constraining inference variables in cases like #91762.

Returns true if victim should be dropped in favor of other. Generally speaking we will drop duplicate candidates and prefer where-clause candidates.

See the comment for “SelectionCandidate” for more details.

For default impls, we need to break apart a type into its “constituent types” – meaning, the types that it contains.

Here are some (simple) examples:

(i32, u32) -> [i32, u32]
Foo where struct Foo { x: i32, y: u32 } -> [i32, u32]
Bar<i32> where struct Bar<T> { x: T, y: u32 } -> [i32, u32]
Zed<i32> where enum Zed { A(T), B(u32) } -> [i32, u32]

Normalize where_clause_trait_ref and try to match it against obligation. If successful, return any predicates that result from the normalization.

Returns Ok if poly_trait_ref being true implies that the obligation is satisfied.

Returns the obligations that are implied by instantiating an impl or trait. The obligations are substituted and fully normalized. This is used when confirming an impl or default impl.

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.

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