Trait Resolution. See the rustc dev guide for more information on how this works.
pub use self::project::NormalizeExt;
pub use self::project::normalize_inherent_projection;
pub use self::project::normalize_projection_type;
pub use self::FulfillmentErrorCode::*;
pub use self::ImplSource::*;
pub use self::ObligationCauseCode::*;
pub use self::SelectionError::*;
- Support code for rustdoc and external tools. You really don’t want to be using this unless you need to.
- Checking that constant values used in types can be successfully evaluated.
- engine 🔒
- fulfill 🔒
- Miscellaneous type-system utilities that are too small to deserve their own modules.
- “Object safety” refers to the ability for a trait to be converted to an object. In general, traits may only be converted to an object if all of their methods meet certain criteria. In particular, they must:
- Code for projecting associated types out of trait references.
- Experimental types for the trait query interface. The methods defined in this module are all based on canonicalization, which makes a canonical query by replacing unbound inference variables and regions, so that results can be reused more broadly. The providers for the queries defined here can be found in
- select 🔒Candidate selection. See the rustc dev guide for more information on how this works.
- Logic and data structures related to impl specialization, explained in greater detail below.
- util 🔒
- The fulfillment context is used to drive trait resolution. It consists of a list of obligations that must be (eventually) satisfied. The job is to track which are satisfied, which yielded errors, and which are still pending. At any point, users can call
select_where_possible, and the fulfillment context will try to do selection, retaining only those obligations that remain ambiguous. This may be helpful in pushing type inference along. Once all type inference constraints have been generated, the method
select_all_or_errorcan be used to report any remaining ambiguous cases as errors.
- Identifies a particular impl in the source, along with a set of substitutions from the impl’s type/lifetime parameters. The
nestedvector corresponds to the nested obligations attached to the impl’s type parameters.
Obligationrepresents some trait reference (e.g.,
i32: Eq) for which the “impl_source” must be found. The process of finding an “impl_source” is called “resolving” the
Obligation. This process consists of either identifying an
impl Eq for i32) that satisfies the obligation, or else finding a bound that is in scope. The eventual result is usually a
- The reason why we incurred this obligation; used for error reporting.
- Used if you want to have pleasant experience when dealing with obligations outside of hir or mir typeck.
- Information pertinent to an overlapping impl error.
- The projection cache. Unlike the standard caches, this can include infcx-dependent type variables, therefore we have to roll the cache back each time we roll a snapshot back, to avoid assumptions on yet-unresolved inference variables. Types with placeholder regions also have to be removed when the respective snapshot ends.
- “Trait alias expansion” is the process of expanding a sequence of trait references into another sequence by transitively following all trait aliases. e.g. If you have bounds like
Foo + Send, a trait alias
trait Foo = Bar + Sync;, and another trait alias
trait Bar = Read + Write, then the bounds would expand to
Read + Write + Sync + Send. Expansion is done via a DFS (depth-first search), and the
visitedfield is used to avoid cycles.
- These are the error cases for
- The result of trait evaluation. The order is important here as the evaluation of a list is the maximum of the evaluations.
- Given the successful resolution of an obligation, the
ImplSourceindicates where the impl comes from.
- Reasons a method might not be object-safe.
- Indicates that trait evaluation caused overflow and in which pass.
- Depending on the stage of compilation, we want projection to be more or less conservative.
- Whether to skip the leak check, as part of a future compatibility warning step.
- The mode that trait queries run in.
- The ‘location’ at which we try to perform HIR-based wf checking. This information is used to obtain an
hir::Ty, which we can walk in order to obtain precise spans for any ‘nested’ types (e.g.
- Returns the object safety violations that affect astconv – currently,
Selfin supertraits. This is needed because
object_safety_violationscan’t be used during type collection.
- Normalize a type and process all resulting obligations, returning any errors.
- Given an upcast trait object described by
object, returns the index of the method
method_def_id(which should be part of
object.upcast_trait_ref) within the vtable for
- Normalizes the predicates and checks whether they hold in an empty environment. If this returns true, then either normalize encountered an error or one of the predicates did not hold. Used when creating vtables to check for unsatisfiable methods.
- Checks whether a trait’s associated item is impossible to reference on a given impl.
- We say a method is vtable safe if it can be invoked on a trait object. Note that object-safe traits can have some non-vtable-safe methods, so long as they require
Self: Sizedor otherwise ensure that they cannot be used when
Self = Trait.
- Normalizes the parameter environment, reporting errors if they occur.
- Checks the coherence orphan rules.
impl_def_idshould be the
DefIdof a trait impl. To pass, either the trait must be local, or else two conditions must be satisfied:
- If there are types that satisfy both impls, returns
Somewith a suitably-freshened
ImplHeaderwith those types substituted. Otherwise, returns
- FIXME(@lcnr): this function doesn’t seem right and shouldn’t exist?
- Creates predicate obligations from the generic bounds.
- This method traverses the structure of
ty, trying to find an instance of an ADT (i.e. struct or enum) that doesn’t implement the structural-match traits, or a generic type parameter (which cannot be determined to be structural-match).
- A specialized variant of
elaboratethat only elaborates trait references that may define the given associated item with the name
assoc_name. It uses the
super_predicates_that_define_assoc_itemquery to avoid enumerating super-predicates that aren’t related to
assoc_item. This is used when resolving types like
T::Itemand helps to avoid cycle errors (see e.g. #35237).
- Given a subst for the requested impl, translate it to a subst appropriate for the actual item definition (whether it be in that impl, a parent impl, or the trait).
- Like translate_args, but obligations from the parent implementation are registered with the provided
- Determines whether the type
tyis known to meet
boundand returns true if so. Returns false if
tyeither does not meet
boundor is not known to meet bound (note that this is conservative towards no impl, which is the opposite of the
- Casts a trait reference into a reference to one of its super traits; returns
target_trait_def_idis not a supertrait.
- When performing resolution, it is typically the case that there can be one of three outcomes: