Candidate preference
There are multiple ways to prove Trait and NormalizesTo goals.
Each such option is called a Candidate.
If there are multiple applicable candidates, we prefer some candidates over others.
We store the relevant information in their CandidateSource.
This preference may result in incorrect inference or region constraints and would therefore be unsound during coherence. Because of this, we simply try to merge all candidates in coherence.
Trait goals
Trait goals merge their applicable candidates in fn merge_trait_candidates.
This document provides additional details and references to explain why we’ve got the current preference rules.
CandidateSource::BuiltinImpl(BuiltinImplSource::Trivial))
Trivial builtin impls are builtin impls which are known to be always applicable for well-formed types.
This means that if one exists, using another candidate should never have fewer constraints.
We currently only consider Sized - and MetaSized - impls to be trivial.
This is necessary to prevent a lifetime error for the following pattern
This preference is incorrect in case the builtin impl has a nested goal which relies on a non-param where-clause
, T);
Sized>
where
: Sized,
: Sized,
CandidateSource::ParamEnv
Once there’s at least one non-global ParamEnv candidate, we prefer all ParamEnv candidates over other candidate kinds.
A where-bound is global if it is not higher-ranked and doesn’t contain any generic parameters.
It may contain 'static.
We try to apply where-bounds over other candidates as users tends to have the most control over them, so they can most easily adjust them in case our candidate preference is incorrect.
Preference over Impl candidates
This is necessary to avoid region errors in the following example
We also need this as shadowed impls can result in currently ambiguous solver cycles: trait-system-refactor-initiative#76. Without preference, we’d be forced to fail with ambiguity errors if the where-bound results in region constraints to avoid incompleteness.
This preference causes a lot of issues. See #24066. Most of the issues are caused by preferring where-bounds over impls even, if the where-bound guides type inference:
However, even if we only apply this preference if the where-bound doesn’t guide inference, it may still result in incorrect lifetime constraints:
Preference over AliasBound candidates
This is necessary to avoid region errors in the following example
It can also result in unnecessary constraints
Why no preference for global where-bounds
Global where-bounds are either fully implied by an impl or unsatisfiable.
If they are unsatisfiable, we don’t really care what happens.
If a where-bound is fully implied, then using the impl to prove the trait goal cannot result in additional constraints.
For trait goals, this is only useful for where-bounds which use 'static:
More importantly, by using impls here, we prevent global where-bounds from shadowing impls when normalizing associated types. There are no known issues from preferring impls over global where-bounds.
Why still consider global where-bounds
Given that we just use impls even if there exists a global where-bounds, you may ask why we don’t just ignore these global where-bounds entirely: we use them to weaken the inference guidance from non-global where-bounds.
Without a global where-bound, we currently prefer non-global where bounds even though there would be an applicable impl as well. By adding a non-global where-bound, this unnecessary inference guidance is disabled, allowing the following to compile:
;
CandidateSource::AliasBound
We prefer alias-bound candidates over impls. We currently use this preference to guide type inference, causing the following to compile. I personally don’t think this preference is desirable 🤷
This preference also avoids ambiguity due to region constraints, I don’t know whether people rely on this in practice.
CandidateSource::BuiltinImpl(BuiltinImplSource::Object(_))
We prefer builtin trait object impls over user-written impls. This is unsound and should be remoed in the future. See #57893 and #141347 for more details.
NormalizesTo goals
The candidate preference behavior during normalization is implemented in fn assemble_and_merge_candidates.
We always consider AliasBound candidates
In case the where-bound does not specify the associated item, we consider AliasBound candidates instead of treating the alias as rigid, even though the trait goal was proven via a ParamEnv candidate.
// Elaborating the environment results in a `T::Assoc: Super` where-bound.
// This where-bound must not prevent normalization via the `Super<Assoc = u32>`
// item bound.
Using such an alias can result in additional region constraints, cc #133044.
We prefer ParamEnv candidates over AliasBound
While we use AliasBound candidates if the where-bound does not specify the associated type, in case it does, we prefer the where-bound.
This is necessary for the following example:
// Make sure we prefer the `I::IntoIterator: Iterator<Item = ()>`
// where-bound over the `I::Intoiterator: Iterator<Item = I::Item>`
// alias-bound.
We always consider where-bounds
Even if the trait goal was proven via an impl, we still prefer ParamEnv candidates, if any exist.
We prefer “orphaned” where-bounds
We add “orphaned” Projection clauses into the ParamEnv when normalizing item bounds of GATs and RPITIT in fn check_type_bounds.
We need to prefer these ParamEnv candidates over impls and other where-bounds.
I don’t fully understand the cases where this preference is actually necessary and haven’t been able to exploit this in fun ways yet, but 🤷
We prefer global where-bounds over impls
This is necessary for the following to compile. I don’t know whether anything relies on it in practice 🤷
This means normalization can result in additional region constraints, cc #133044.
Trait where-bounds shadow impls
Normalization of associated items does not consider impls if the corresponding trait goal has been proven via a ParamEnv or AliasBound candidate.
This means that for where-bounds which do not constrain associated types, the associated types remain rigid.
Using impls results in different region constraints
This is necessary to avoid unnecessary region constraints from applying impls.
RPITIT type_of cycles
We currently have to avoid impl candidates if there are where-bounds to avoid query cycles for RPITIT, see #139762. It feels desirable to me to stop relying on auto-trait leakage of during RPITIT computation to remove this issue, see #139788.
use Future;
Trait definition cannot use associated types from always applicable impls
The T: Trait assumption in the trait definition prevents it from normalizing
<Self as Trait>::Assoc to T by using the blanket impl.
This feels like a somewhat desirable constraint, if not incredibly so.
;