Module rustc_middle::ty

source ·
Expand description

Defines how the compiler represents types internally.

Two important entities in this module are:

For more information, see “The ty module: representing types” in the rustc-dev-guide.





  • The definition of a user-defined type, e.g., a struct, enum, or union.
  • Represents the projection of an associated type.
  • Information about an associated item
  • A list of ty::AssocItems in definition order that allows for efficient lookup by name.
  • Binder is a binder for higher-ranked lifetimes or types. It is part of the compiler’s representation for things like for<'a> Fn(&'a isize) (which would be represented by the type PolyTraitRef == Binder<'tcx, TraitRef>). Note that when we instantiate, erase, or otherwise “discharge” these bound vars, we change the type from Binder<'tcx, T> to just T (see e.g., liberate_late_bound_regions).
  • A “canonicalized” type V is one where all free inference variables have been rewritten to “canonical vars”. These are numbered starting from 0 in order of first appearance.
  • 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.
  • Part of MinCaptureInformationMap; describes the capture kind (&, &mut, move) for a particular capture as well as identifying the part of the source code that triggered this capture to occur.
  • A composite describing a Place that is captured by a closure.
  • A subset of predicates which can be assumed by the trait solver. They show up in an item’s where clauses, hence the name Clause, and may either be user-written (such as traits) or may be inserted during lowering.
  • A closure can be modeled as a struct that looks like:
  • Struct returned by split().
  • Encodes that we have to coerce from the a type to the b type.
  • Use this rather than ConstData, whenever possible.
  • Typed constant value.
  • A type for representing any integer. Only used for printing.
  • A const variable ID.
  • Similar to ClosureArgs; see the above documentation for more.
  • See docs for explanation of how each argument is used.
  • A map for the local crate mapping each type to a vector of its inherent impls. This is not meant to be used outside of coherence; rather, you should request the vector for a specific type via tcx.inherent_impls(def_id) so as to minimize your dependencies (constructing this map requires touching the entire crate).
  • The crate outlives map is computed during typeck and contains the outlives of every item in the local crate. You should not use it directly, because to do so will make your pass dependent on the HIR of every item in the local crate. Instead, use tcx.inferred_outlives_of() to get the outlives for a particular item.
  • The crate variances map is computed during typeck and contains the variance of every item in the local crate. You should not use it directly, because to do so will make your pass dependent on the HIR of every item in the local crate. Instead, use tcx.variances_of() to get the variance for a particular item.
  • This is used to get a reference to a GlobalCtxt if one is available.
  • 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:
  • Parameter attributes that can only be determined by examining the body of a function instead of just its signature.
  • The constituent parts of a type level constant of kind ADT or array.
  • Similar to super::Binder except that it tracks early bound generics, i.e. struct Foo<T>(T) needs T instantiated immediately. This type primarily exists to avoid forgetting to call instantiate.
  • An effect variable ID.
  • A ProjectionPredicate for an ExistentialTraitRef.
  • An existential reference to a trait, where Self is erased. For example, the trait object Trait<'a, 'b, X, Y> is:
  • The same as TyCtxtFeed, but does not contain a TyCtxt. Use this to pass around when you have a TyCtxt elsewhere. Just an optimization to save space and not store hundreds of TyCtxtFeed in the resolver.
  • A floating-point (f32 or f64) type variable ID.
  • Signature of a function type, which we have arbitrarily decided to use to refer to the input/output types.
  • This struct contains information regarding a free parameter region, either a ReEarlyParam or ReLateParam.
  • An entity in the Rust type system, which can be one of several kinds (types, lifetimes, and consts). To reduce memory usage, a GenericArg is an interned pointer, with the lowest 2 bits being reserved for a tag to indicate the type (Ty, Region, or Const) it points to.
  • Bounds on generics.
  • Information about the formal type/lifetime parameters associated with an item or method. Analogous to hir::Generics.
  • See TyCtxt for details about this type.
  • The “header” of an impl is everything outside the body: a Self type, a trait ref (in the case of a trait impl), and a set of predicates (from the bounds / where-clauses).
  • An inline const is modeled like
  • Struct returned by split().
  • A monomorphized InstanceDef.
  • Represents the bounds declared on a particular set of type parameters. Should eventually be generalized into a flag list of where-clauses. You can obtain an InstantiatedPredicates list from a GenericPredicates by using the instantiate method. Note that this method reflects an important semantic invariant of InstantiatedPredicates: while the GenericPredicates are expressed in terms of the bound type parameters of the impl/trait/whatever, an InstantiatedPredicates instance represented a set of bounds for some particular instantiation, meaning that the generic parameters have been instantiated with their values.
  • An integral (u32, i32, usize, etc.) type variable ID.
  • The parameter representation of late-bound function parameters, “some region at least as big as the scope fr.scope”.
  • Used by the new solver. Unlike a ProjectionPredicate this can only be proven by actually normalizing alias.
  • A: B
  • When interacting with the type system we must provide information about the environment. ParamEnv is the type that represents this information. See the dev guide chapter for more information.
  • ParamTag 🔒
  • 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 another.
  • A statement that can be proven by a trait solver. This includes things that may show up in where clauses, such as trait predicates and projection predicates, and also things that are emitted as part of type checking such as ObjectSafe predicate which is emitted when a type is coerced to a trait object.
  • This kind of predicate has no direct correspondent in the syntax, but it roughly corresponds to the syntactic forms:
  • Use this rather than RegionKind, whenever possible.
  • A region (lifetime) variable ID.
  • Represents the repr options provided by the user.
  • Resolutions that should only be used for lowering. This struct is meant to be consumed by lowering.
  • RvalueScopes is a mapping from sub-expressions to extended lifetime as determined by rules laid out in rustc_hir_analysis::check::rvalue_scopes.
  • The raw bytes of a simple value.
  • Collect al types that have an implicit 'static obligation that we could suggest '_ for.
  • Encodes that a must be a subtype of b. The a_is_expected flag indicates whether the a type is the type that we should label as “expected” when presenting user diagnostics.
  • Collect al types that have an implicit 'static obligation that we could suggest '_ for.
  • A complete reference to a trait. These take numerous guises in syntax, but perhaps the most recognizable form is in a where-clause:
  • Use this rather than TyKind, whenever possible.
  • The central data structure of the compiler. It stores references to the various arenas and also houses the results of the various compiler queries that have been performed. See the rustc dev guide for more details.
  • This struct should only be created by create_def.
  • A type variable ID.
  • 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.
  • An unevaluated (potentially generic) constant used in the type-system.
  • “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.
  • Upvars do not get their own NodeId. Instead, we use the pair of the original var ID (that is, the root variable that is referenced by the upvar) and the ID of the closure expression.
  • Stores the user-given args to reach some fully qualified path (e.g., <T>::Item or <T as Trait>::Item).
  • Specifies the user-given self type. In the case of a path that refers to a member in an inherent impl, this self type is sometimes needed to constrain the type parameters on the impl. For example, in this code:
  • Definition of a variant – a struct’s fields or an enum variant.
  • A helper type that you can wrap round your own type in order to automatically cache the stable hash, type flags and debruijn index on creation and not recompute it whenever the information is needed. This is only done in incremental mode. You can also opt out of caching by using StableHash::ZERO for the hash, in which case the hash gets computed each time. This is useful if you have values that you intern but never (can?) use for stable hashing.



  • Captures are represented using fields inside a structure. This represents accessing self in the closure structure
  • The shorthand encoding uses an enum’s variant index usize and is offset by this value so it never matches a real variant. This offset is also chosen so that the first byte is never < 0x80.
  • TAG_MASK 🔒
  • TYPE_TAG 🔒


  • Imagine you have a function F: FnOnce(&[T]) -> R, plus an iterator iter that produces T items. You could combine them with f(&iter.collect::<Vec<_>>()), but this requires allocating memory for the Vec.
  • 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.
  • A trait implemented for all X<'a> types that can be safely and efficiently converted to X<'tcx> as long as they are part of the provided TyCtxt<'tcx>. This can be done, for example, for Ty<'tcx> or GenericArgsRef<'tcx> by looking them up in their respective interners.
  • Common capabilities of placeholder kinds
  • Trait for decoding to a reference.
  • This trait is implemented for every type that can be folded, providing the skeleton of the traversal.
  • 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_with method if the primary argument doesn’t satisfy a particular condition.
  • This trait is implemented for every type that can be visited, providing the skeleton of the traversal.
  • 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.


Type Aliases§