Expand description
This crate is an abstraction layer, shared between rustc and rust-analyzer, to help with the overlapping responsibilities (like type inference and trait solving), reduce duplication, and maintain consistent behavior between the two implementations.
It defines fundamental interfaces for types, predicates, and the context required by the next trait solver.
Both rustc and rust-analyzer immplement these traits for their own concrete implementations, and
rustc_next_trait_solver is written to be generic over these abstractions.
In addition to these interfaces, it also contains components built on top of the abstraction layer, for example elaboration logic, and the search graph machinery used by the solver, as well as items that do not need compiler-specific implementations.
Note that rust-analyzer is built with a stable compiler, while rustc uses unstable features, so
this crate and some of its dependencies need to separate unstable code under the nightly
feature.
There are more details available in a dedicated dev-guide chapter.
Re-exports§
pub use AliasTyKind::*;pub use InferTy::*;pub use RegionKind::*;pub use TyKind::*;pub use Variance::*;pub use sty::*;
Modules§
- binder 🔒
- canonical 🔒
- const_
kind 🔒 - data_
structures - elaborate
- error
- fast_
reject - flags 🔒
- fold 🔒
- A folding traversal mechanism for complex data structures that contain type information.
- generic_
arg 🔒 - generic_
visit 🔒 - Special visiting used by rust-analyzer only.
- infer_
ctxt 🔒 - inherent
- Set of traits which are used to emulate the inherent impls that are present in
rustc_middle. It is customary to glob-importrustc_type_ir::inherent::*to bring all of these traits into scope when programming in interner-agnostic settings, and to avoid importing any of these directly elsewhere (i.e. specify the full path for an implementation downstream). - intern
- interner 🔒
- ir_
print - lang_
items - lift
- macros 🔒
- opaque_
ty 🔒 - outlives
- The outlives relation
T: 'aor'a: 'b. This code frequently refers to rules defined in RFC 1214 (OutlivesFooBar), so see that RFC for reference. - pattern 🔒
- predicate 🔒
- predicate_
kind 🔒 - region_
constraint - The bulk of the logic for implementing
-Zassumptions-on-binders - region_
kind 🔒 - relate
- search_
graph - The search graph is responsible for caching and cycle detection in the trait solver. Making sure that caching doesn’t result in soundness bugs or unstable query results is very challenging and makes this one of the most-involved self-contained components of the compiler.
- serialize 🔒
- solve
- sty
- term_
kind 🔒 - ty 🔒
- ty_info 🔒
- ty_kind 🔒
- universe 🔒
- unnormalized 🔒
- upcast 🔒
- visit 🔒
- A visiting traversal mechanism for complex data structures that contain type information.
- walk
- An iterator over the type substructure. WARNING: this does not keep track of the region depth.
Macros§
- Trivial
Type Traversal Impls - Used for types that are
Copyand which do not care arena allocated data (i.e., don’t need to be folded). - try_
visit - walk_
visitable_ list
Structs§
- Alias
- Represents an alias of a type, constant, or other term-like item.
- Binder
Binderis a binder for higher-ranked lifetimes or types. It is part of the compiler’s representation for things likefor<'a> Fn(&'a isize)(which would be represented by the typePolyTraitRef == Binder<I, TraitRef>).- Bound
Const - Bound
Region - BoundTy
- Bound
Var - Canonical
- A “canonicalized” type
Vis one where all free inference variables have been rewritten to “canonical vars”. These are numbered starting from 0 in order of first appearance. - Canonical
Param EnvCache - Canonical
Param EnvCache Entry - Canonical
Query Input - Canonical
VarValues - A set of values corresponding to the canonical variables from some
Canonical. You can give these values tocanonical_value.instantiateto instantiate them into the canonical value at the right places. - Canonicalizer
State - State used and modified by a canonicalizer during canonicalization. To avoid many allocations,
this state is reused by many canonicalizers from a single
InferCtxt. - Closure
Args - A closure can be modeled as a struct that looks like:
- Closure
Args Parts - Struct returned by
split(). - Coerce
Predicate - Encodes that we have to coerce from the
atype to thebtype. - Const
Vid - A
constvariable ID. - Coroutine
Args - Similar to
ClosureArgs; see the above documentation for more. - Coroutine
Args Parts - Coroutine
Closure Args - Coroutine
Closure Args Parts - See docs for explanation of how each argument is used.
- Coroutine
Closure Signature - Coroutine
Witness Types - Debruijn
Index - A De Bruijn index is a standard means of representing regions (and perhaps later types) in a higher-ranked setting. In particular, imagine a type like this:
- Early
Binder - Similar to
Binderexcept that it tracks early bound generics, i.e.struct Foo<T>(T)needsTinstantiated immediately. This type primarily exists to avoid forgetting to callinstantiate. - Early
Binder Iter - Existential
Projection - A
ProjectionClausefor anExistentialTraitRef. - Existential
Trait Ref - An existential reference to a trait where the self type
Selfis erased. - Field
Info - Flag
Computation - Float
Vid - A floating-point (
f32orf64) type variable ID. - FnHeader
- FnSig
- FnSig
Kind - Contains the packed non-type fields of a function signature.
- FnSig
Tys - GenSig
- Host
Effect Clause - IntVid
- An integral (
u32,i32,usize, etc.) type variable ID. - Iter
Identity Copied - Iter
Instantiated - Iter
Instantiated Copied - MayBe
Erased - Normalizes
To - Used by the new solver to normalize an alias. This always expects the
termto be an unconstrained inference variable which is used as the output. - Opaque
Type Key - Outlives
Clause A: 'region- Placeholder
- The “placeholder index” fully defines a placeholder region, type, or const. Placeholders are identified by both a universe, as well as a name residing within that universe. Distinct bound regions/types/consts within the same universe simply have an unknown relationship to one
- Projection
Clause - This kind of predicate has no direct correspondent in the syntax, but it roughly corresponds to the syntactic forms:
- Region
EqPredicate 'a == 'b. For the rationale behind having this instead of a pair of bidirectional'a: 'band'b: 'a, see this discusstion on Zulip.- Region
Folder - Folds over the substructure of a type, visiting its component types and all regions that occur free within it.
- Region
Vid - A region variable ID.
- Subtype
Predicate - Encodes that
amust be a subtype ofb. Thea_is_expectedflag indicates whether theatype is the type that we should label as “expected” when presenting user diagnostics. - Trait
Clause - Trait
Ref - A complete reference to a trait.
- TyVid
- A type variable ID.
- Type
AndMut - Type
Flags - Flags that we track on types. These flags are propagated upwards through the type during type construction, so that we can quickly check whether the type has various kinds of types in it without recursing over the type itself.
- Typing
Mode EqWrapper - We want to highly discourage using equality checks on typing modes.
Instead you should match, exhaustively, so when we ever modify the enum we get a compile
error. Only use
TypingModeEqWrapperwhen you really really really have to. Prefer unwrappingTypingModeEqWrapperin apis that should return aTypingModewhenever possible, and if you ever get anTypingModeEqWrapper, prefer unwrapping it and matching on it exhaustively. - Universe
Index - “Universes” are used during type- and trait-checking in the
presence of
for<..>binders to control what sets of names are visible. Universes are arranged into a tree: the root universe contains names that are always visible. Each child then adds a new set of names that are visible, in addition to those of its parent. We say that the child universe “extends” the parent universe with new names. - Unnormalized
- A wrapper for values that need normalization.
- Unsafe
Binder Inner - Validate
Bound Vars - With
Cached Type Info - A helper type that you can wrap round your own type in order to automatically cache the type flags and debruijn index on creation and not recompute it whenever the information is needed.
Enums§
- Alias
Const Kind - AliasConstKind is extremely similar to AliasTyKind, and likely should be reasoned about and handled in very similar ways. The documentation for AliasTyKind/etc. may be helpful when learning about AliasConstKind.
- Alias
Term Kind - Alias
TyKind - Anon
Const Kind - Bound
Constness - Bound
Region Kind - Bound
TyKind - Bound
VarIndex Kind - Okay, we do something fun for
Boundtypes/regions/consts: Specifically, we distinguish between canonically bound things andfor<>bound things. And, really, it comes down to caching during canonicalization and instantiation. - Bound
Variable Kind - Canonical
VarKind - Information about a canonical variable that is included with the canonical value. This is sufficient information for code to create a copy of the canonical value in some other inference context, with fresh inference variables replacing the canonical values.
- Cant
BeErased - Clause
Kind - A clause is something that can appear in where bounds or be inferred by implied bounds.
- Clause
Polarity - Polarity for a trait clause.
- Closure
Kind - Represents the various closure traits in the language. This
will determine the type of the environment (
self, in the desugaring) argument that the closure expects. - Const
Kind - Represents a constant in Rust.
- Existential
Predicate - FloatTy
- Float
VarValue - Generic
ArgKind - Impl
Polarity - Infer
Const - An inference variable for a const, for use in const generics.
- InferTy
- A placeholder for a type that hasn’t been inferred yet.
- IntTy
- IntVar
Value - IsRigid
- Whether an alias type is rigid or potentially normalizeable.
- Movability
- The movability of a coroutine / closure literal:
whether a coroutine contains self-references, causing it to be
!Unpin. - Mutability
- Pattern
Kind - Pinnedness
- Predicate
Kind - Region
Constraint - Region
Kind - Representation of regions. Note that the NLL checker uses a distinct
representation of regions. For this reason, it internally replaces all the
regions with inference variables – the index of the variable is then used
to index into internal NLL data structures. See
rustc_const_eval::borrow_checkmodule for more information. - Splatted
ArgIndex Error - Error type for splatted argument index errors.
- Term
Kind - TyKind
- Defines the kinds of types used by the type system.
- Typing
Mode - The current typing mode of an inference context. We unfortunately have some slightly different typing rules depending on the current context. See the doc comment for each variant for how and why they are used.
- UintTy
- ValTree
Kind - This datastructure is used to represent the value of constants used in the type system.
- Variance
Constants§
Traits§
- Collect
AndApply - Imagine you have a function
F: FnOnce(&[T]) -> R, plus an iteratoriterthat producesTitems. You could combine them withf(&iter.collect::<Vec<_>>()), but this requires allocating memory for theVec. - Fallible
Type Folder - This trait is implemented for every folding traversal. There is a fold method defined for every type of interest. Each such method has a default that does an “identity” fold.
- Flags
- Generic
Type Visitable - This trait is implemented for every type that can be visited, providing the skeleton of the traversal.
- Infer
Ctxt Like InferCtxtLikeis one of the two traits abstracting over the InferCtxt, which had to be split due to coherence reasons:- Interner
- The central trait in the shared abstraction layer, specifying all implementation-specific details for rustc and rust-analyzer.
- Interner
Decoder - A decoder that can reconstruct interned type IR values by supplying the interner that owns the decoded data.
- Lift
Into - An interner whose associated types can be lifted into another interner
J. - Type
Foldable - This trait is implemented for every type that can be folded, providing the skeleton of the traversal.
- Type
Folder - This trait is implemented for every infallible folding traversal. There is
a fold method defined for every type of interest. Each such method has a
default that does an “identity” fold. Implementations of these methods
often fall back to a
super_fold_withmethod if the primary argument doesn’t satisfy a particular condition. - Type
Super Foldable - Type
Super Visitable - Type
Visitable - This trait is implemented for every type that can be visited, providing the skeleton of the traversal.
- Type
Visitable Ext - Type
Visitor - This trait is implemented for every visiting traversal. There is a visit method defined for every type of interest. Each such method has a default that recurses into the type’s fields in a non-custom fashion.
- Typing
Mode Erased Status - Upcast
- An
Into-like trait that takesTyCtxtto perform interner-specific transformations. - Upcast
From - A
From-like trait that takesTyCtxtto perform interner-specific transformations. - Visitor
Result - Similar to the
Trytrait, but also implemented for().
Functions§
- debug_
bound_ var - eager_
resolve_ vars - Resolves ty, region, and const vars to their inferred values or their root vars.
- fold_
regions - max_
universe - The largest universe a variable or placeholder was from in
t - max_
universe_ of_ infer_ vars - The largest universe a variable was from in
t - max_
universe_ of_ placeholders - The largest universe a placeholder was from in
t - may_
use_ unstable_ feature - set_
aliases_ to_ non_ rigid - This function should ideally only be used if either the
TypingModeor theParamEnvdiffers from the environment the aliases were normalized in. - set_
aliases_ to_ rigid - set_
opaques_ to_ non_ rigid - set_
type_ aliases_ to_ rigid - shift_
region - shift_
vars