1#![cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir")]
2// tidy-alphabetical-start
3#![allow(rustc::direct_use_of_rustc_type_ir)]
4#![allow(rustc::usage_of_ty_tykind)]
5#![allow(rustc::usage_of_type_ir_inherent)]
6#![allow(rustc::usage_of_type_ir_traits)]
7#![cfg_attr(feature = "nightly", allow(internal_features))]
8#![cfg_attr(feature = "nightly", feature(associated_type_defaults, rustc_attrs, negative_impls))]
9// tidy-alphabetical-end
1011extern crate self as rustc_type_ir;
1213use std::fmt;
14use std::hash::Hash;
1516use rustc_abi::{FieldIdx, VariantIdx};
17#[cfg(feature = "nightly")]
18use rustc_macros::{Decodable, Encodable, StableHash};
1920// These modules are `pub` since they are not glob-imported.
21pub mod data_structures;
22pub mod elaborate;
23pub mod error;
24pub mod fast_reject;
25#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_inherent")]
26pub mod inherent;
27pub mod ir_print;
28pub mod lang_items;
29pub mod lift;
30pub mod outlives;
31pub mod region_constraint;
32pub mod relate;
33pub mod search_graph;
34pub mod solve;
35pub mod walk;
3637// These modules are not `pub` since they are glob-imported.
38#[macro_use]
39mod macros;
40mod binder;
41mod canonical;
42mod const_kind;
43mod flags;
44mod fold;
45mod generic_arg;
46#[cfg(not(feature = "nightly"))]
47mod generic_visit;
48mod infer_ctxt;
49mod interner;
50mod opaque_ty;
51mod pattern;
52mod predicate;
53mod predicate_kind;
54mod region_kind;
55mod ty;
56mod ty_info;
57mod ty_kind;
58mod unnormalized;
59mod upcast;
60mod visit;
6162pub use AliasTyKind::*;
63pub use InferTy::*;
64pub use RegionKind::*;
65pub use TyKind::*;
66pub use Variance::*;
67pub use binder::{Placeholder, *};
68pub use canonical::*;
69pub use const_kind::*;
70pub use flags::*;
71pub use fold::*;
72pub use generic_arg::*;
73#[cfg(not(feature = "nightly"))]
74pub use generic_visit::*;
75pub use infer_ctxt::*;
76pub use interner::*;
77pub use opaque_ty::*;
78pub use pattern::*;
79pub use predicate::*;
80pub use predicate_kind::*;
81pub use region_kind::*;
82pub use rustc_ast_ir::{FloatTy, IntTy, Movability, Mutability, Pinnedness, UintTy};
83use rustc_type_ir_macros::GenericTypeVisitable;
84pub use ty::{Alias, AliasTerm, AliasTy, UnevaluatedConst};
85pub use ty_info::*;
86pub use ty_kind::*;
87pub use unnormalized::Unnormalized;
88pub use upcast::*;
89pub use visit::*;
9091impl ::std::fmt::Debug for DebruijnIndex {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("DebruijnIndex({0})", self.as_u32()))
}
}rustc_index::newtype_index! {
92/// A [De Bruijn index][dbi] is a standard means of representing
93 /// regions (and perhaps later types) in a higher-ranked setting. In
94 /// particular, imagine a type like this:
95 /// ```ignore (illustrative)
96 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
97 /// // ^ ^ | | |
98 /// // | | | | |
99 /// // | +------------+ 0 | |
100 /// // | | |
101 /// // +----------------------------------+ 1 |
102 /// // | |
103 /// // +----------------------------------------------+ 0
104 /// ```
105 /// In this type, there are two binders (the outer fn and the inner
106 /// fn). We need to be able to determine, for any given region, which
107 /// fn type it is bound by, the inner or the outer one. There are
108 /// various ways you can do this, but a De Bruijn index is one of the
109 /// more convenient and has some nice properties. The basic idea is to
110 /// count the number of binders, inside out. Some examples should help
111 /// clarify what I mean.
112 ///
113 /// Let's start with the reference type `&'b isize` that is the first
114 /// argument to the inner function. This region `'b` is assigned a De
115 /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
116 /// fn). The region `'a` that appears in the second argument type (`&'a
117 /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
118 /// second-innermost binder". (These indices are written on the arrows
119 /// in the diagram).
120 ///
121 /// What is interesting is that De Bruijn index attached to a particular
122 /// variable will vary depending on where it appears. For example,
123 /// the final type `&'a char` also refers to the region `'a` declared on
124 /// the outermost fn. But this time, this reference is not nested within
125 /// any other binders (i.e., it is not an argument to the inner fn, but
126 /// rather the outer one). Therefore, in this case, it is assigned a
127 /// De Bruijn index of 0, because the innermost binder in that location
128 /// is the outer fn.
129 ///
130 /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
131#[stable_hash]
132 #[encodable]
133 #[orderable]
134 #[debug_format = "DebruijnIndex({})"]
135 #[gate_rustc_only]
136pub struct DebruijnIndex {
137const INNERMOST = 0;
138 }
139}140141impl DebruijnIndex {
142/// Returns the resulting index when this value is moved into
143 /// `amount` number of new binders. So, e.g., if you had
144 ///
145 /// for<'a> fn(&'a x)
146 ///
147 /// and you wanted to change it to
148 ///
149 /// for<'a> fn(for<'b> fn(&'a x))
150 ///
151 /// you would need to shift the index for `'a` into a new binder.
152#[inline]
153 #[must_use]
154pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
155DebruijnIndex::from_u32(self.as_u32() + amount)
156 }
157158/// Update this index in place by shifting it "in" through
159 /// `amount` number of binders.
160#[inline]
161pub fn shift_in(&mut self, amount: u32) {
162*self = self.shifted_in(amount);
163 }
164165/// Returns the resulting index when this value is moved out from
166 /// `amount` number of new binders.
167#[inline]
168 #[must_use]
169pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
170DebruijnIndex::from_u32(self.as_u32() - amount)
171 }
172173/// Update in place by shifting out from `amount` binders.
174#[inline]
175pub fn shift_out(&mut self, amount: u32) {
176*self = self.shifted_out(amount);
177 }
178179/// Adjusts any De Bruijn indices so as to make `to_binder` the
180 /// innermost binder. That is, if we have something bound at `to_binder`,
181 /// it will now be bound at INNERMOST. This is an appropriate thing to do
182 /// when moving a region out from inside binders:
183 ///
184 /// ```ignore (illustrative)
185 /// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
186 /// // Binder: D3 D2 D1 ^^
187 /// ```
188 ///
189 /// Here, the region `'a` would have the De Bruijn index D3,
190 /// because it is the bound 3 binders out. However, if we wanted
191 /// to refer to that region `'a` in the second argument (the `_`),
192 /// those two binders would not be in scope. In that case, we
193 /// might invoke `shift_out_to_binder(D3)`. This would adjust the
194 /// De Bruijn index of `'a` to D1 (the innermost binder).
195 ///
196 /// If we invoke `shift_out_to_binder` and the region is in fact
197 /// bound by one of the binders we are shifting out of, that is an
198 /// error (and should fail an assertion failure).
199#[inline]
200pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
201self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
202 }
203}
204205pub fn debug_bound_var<T: std::fmt::Write>(
206 fmt: &mut T,
207 bound_index: BoundVarIndexKind,
208 var: impl std::fmt::Debug,
209) -> Result<(), std::fmt::Error> {
210match bound_index {
211 BoundVarIndexKind::Bound(debruijn) => {
212if debruijn == INNERMOST {
213fmt.write_fmt(format_args!("^{0:?}", var))write!(fmt, "^{var:?}")214 } else {
215fmt.write_fmt(format_args!("^{0}_{1:?}", debruijn.index(), var))write!(fmt, "^{}_{:?}", debruijn.index(), var)216 }
217 }
218 BoundVarIndexKind::Canonical => {
219fmt.write_fmt(format_args!("^c_{0:?}", var))write!(fmt, "^c_{:?}", var)220 }
221 }
222}
223224#[derive(#[automatically_derived]
impl ::core::marker::Copy for Variance { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Variance {
#[inline]
fn clone(&self) -> Variance { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Variance {
#[inline]
fn eq(&self, other: &Variance) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Variance {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Variance {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, GenericTypeVisitable)]
225#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for Variance {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { Variance::Covariant }
1usize => { Variance::Invariant }
2usize => { Variance::Contravariant }
3usize => { Variance::Bivariant }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Variance`, expected 0..4, actual {0}",
n));
}
}
}
}
};Decodable, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for Variance {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
Variance::Covariant => { 0usize }
Variance::Invariant => { 1usize }
Variance::Contravariant => { 2usize }
Variance::Bivariant => { 3usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
Variance::Covariant => {}
Variance::Invariant => {}
Variance::Contravariant => {}
Variance::Bivariant => {}
}
}
}
};Encodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for Variance {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
Variance::Covariant => {}
Variance::Invariant => {}
Variance::Contravariant => {}
Variance::Bivariant => {}
}
}
}
};StableHash))]
226#[cfg_attr(feature = "nightly", rustc_pass_by_value)]
227pub enum Variance {
228 Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
229Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
230Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
231Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
232}
233234impl Variance {
235/// `a.xform(b)` combines the variance of a context with the
236 /// variance of a type with the following meaning. If we are in a
237 /// context with variance `a`, and we encounter a type argument in
238 /// a position with variance `b`, then `a.xform(b)` is the new
239 /// variance with which the argument appears.
240 ///
241 /// Example 1:
242 /// ```ignore (illustrative)
243 /// *mut Vec<i32>
244 /// ```
245 /// Here, the "ambient" variance starts as covariant. `*mut T` is
246 /// invariant with respect to `T`, so the variance in which the
247 /// `Vec<i32>` appears is `Covariant.xform(Invariant)`, which
248 /// yields `Invariant`. Now, the type `Vec<T>` is covariant with
249 /// respect to its type argument `T`, and hence the variance of
250 /// the `i32` here is `Invariant.xform(Covariant)`, which results
251 /// (again) in `Invariant`.
252 ///
253 /// Example 2:
254 /// ```ignore (illustrative)
255 /// fn(*const Vec<i32>, *mut Vec<i32)
256 /// ```
257 /// The ambient variance is covariant. A `fn` type is
258 /// contravariant with respect to its parameters, so the variance
259 /// within which both pointer types appear is
260 /// `Covariant.xform(Contravariant)`, or `Contravariant`. `*const
261 /// T` is covariant with respect to `T`, so the variance within
262 /// which the first `Vec<i32>` appears is
263 /// `Contravariant.xform(Covariant)` or `Contravariant`. The same
264 /// is true for its `i32` argument. In the `*mut T` case, the
265 /// variance of `Vec<i32>` is `Contravariant.xform(Invariant)`,
266 /// and hence the outermost type is `Invariant` with respect to
267 /// `Vec<i32>` (and its `i32` argument).
268 ///
269 /// Source: Figure 1 of "Taming the Wildcards:
270 /// Combining Definition- and Use-Site Variance" published in PLDI'11.
271pub fn xform(self, v: Variance) -> Variance {
272match (self, v) {
273// Figure 1, column 1.
274(Variance::Covariant, Variance::Covariant) => Variance::Covariant,
275 (Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
276 (Variance::Covariant, Variance::Invariant) => Variance::Invariant,
277 (Variance::Covariant, Variance::Bivariant) => Variance::Bivariant,
278279// Figure 1, column 2.
280(Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
281 (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
282 (Variance::Contravariant, Variance::Invariant) => Variance::Invariant,
283 (Variance::Contravariant, Variance::Bivariant) => Variance::Bivariant,
284285// Figure 1, column 3.
286(Variance::Invariant, _) => Variance::Invariant,
287288// Figure 1, column 4.
289(Variance::Bivariant, _) => Variance::Bivariant,
290 }
291 }
292}
293294impl fmt::Debugfor Variance {
295fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296f.write_str(match *self {
297 Variance::Covariant => "+",
298 Variance::Contravariant => "-",
299 Variance::Invariant => "o",
300 Variance::Bivariant => "*",
301 })
302 }
303}
304305impl ::std::fmt::Debug for UniverseIndex {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("U{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
306/// "Universes" are used during type- and trait-checking in the
307 /// presence of `for<..>` binders to control what sets of names are
308 /// visible. Universes are arranged into a tree: the root universe
309 /// contains names that are always visible. Each child then adds a new
310 /// set of names that are visible, in addition to those of its parent.
311 /// We say that the child universe "extends" the parent universe with
312 /// new names.
313 ///
314 /// To make this more concrete, consider this program:
315 ///
316 /// ```ignore (illustrative)
317 /// struct Foo { }
318 /// fn bar<T>(x: T) {
319 /// let y: for<'a> fn(&'a u8, Foo) = ...;
320 /// }
321 /// ```
322 ///
323 /// The struct name `Foo` is in the root universe U0. But the type
324 /// parameter `T`, introduced on `bar`, is in an extended universe U1
325 /// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
326 /// of `bar`, we cannot name `T`. Then, within the type of `y`, the
327 /// region `'a` is in a universe U2 that extends U1, because we can
328 /// name it inside the fn type but not outside.
329 ///
330 /// Universes are used to do type- and trait-checking around these
331 /// "forall" binders (also called **universal quantification**). The
332 /// idea is that when, in the body of `bar`, we refer to `T` as a
333 /// type, we aren't referring to any type in particular, but rather a
334 /// kind of "fresh" type that is distinct from all other types we have
335 /// actually declared. This is called a **placeholder** type, and we
336 /// use universes to talk about this. In other words, a type name in
337 /// universe 0 always corresponds to some "ground" type that the user
338 /// declared, but a type name in a non-zero universe is a placeholder
339 /// type -- an idealized representative of "types in general" that we
340 /// use for checking generic functions.
341#[stable_hash]
342 #[encodable]
343 #[orderable]
344 #[debug_format = "U{}"]
345 #[gate_rustc_only]
346pub struct UniverseIndex {}
347}348349impl UniverseIndex {
350pub const ROOT: UniverseIndex = UniverseIndex::ZERO;
351352/// Returns the "next" universe index in order -- this new index
353 /// is considered to extend all previous universes. This
354 /// corresponds to entering a `forall` quantifier. So, for
355 /// example, suppose we have this type in universe `U`:
356 ///
357 /// ```ignore (illustrative)
358 /// for<'a> fn(&'a u32)
359 /// ```
360 ///
361 /// Once we "enter" into this `for<'a>` quantifier, we are in a
362 /// new universe that extends `U` -- in this new universe, we can
363 /// name the region `'a`, but that region was not nameable from
364 /// `U` because it was not in scope there.
365pub fn next_universe(self) -> UniverseIndex {
366UniverseIndex::from_u32(self.as_u32().checked_add(1).unwrap())
367 }
368369/// Returns `true` if `self` can name a name from `other` -- in other words,
370 /// if the set of names in `self` is a superset of those in
371 /// `other` (`self >= other`).
372pub fn can_name(self, other: UniverseIndex) -> bool {
373self >= other374 }
375376/// Returns `true` if `self` cannot name some names from `other` -- in other
377 /// words, if the set of names in `self` is a strict subset of
378 /// those in `other` (`self < other`).
379pub fn cannot_name(self, other: UniverseIndex) -> bool {
380self < other381 }
382383/// Returns `true` if `self` is the root universe, otherwise false.
384pub fn is_root(self) -> bool {
385self == Self::ROOT386 }
387}
388389impl Defaultfor UniverseIndex {
390fn default() -> Self {
391Self::ROOT392 }
393}
394395impl ::std::fmt::Debug for BoundVar {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
396#[stable_hash]
397 #[encodable]
398 #[orderable]
399 #[debug_format = "{}"]
400 #[gate_rustc_only]
401pub struct BoundVar {}
402}403404/// Represents the various closure traits in the language. This
405/// will determine the type of the environment (`self`, in the
406/// desugaring) argument that the closure expects.
407///
408/// You can get the environment type of a closure using
409/// `tcx.closure_env_ty()`.
410#[derive(#[automatically_derived]
impl ::core::clone::Clone for ClosureKind {
#[inline]
fn clone(&self) -> ClosureKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ClosureKind { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for ClosureKind {
#[inline]
fn eq(&self, other: &ClosureKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ClosureKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ClosureKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for ClosureKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ClosureKind::Fn => "Fn",
ClosureKind::FnMut => "FnMut",
ClosureKind::FnOnce => "FnOnce",
})
}
}Debug)]
411#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for ClosureKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
ClosureKind::Fn => { 0usize }
ClosureKind::FnMut => { 1usize }
ClosureKind::FnOnce => { 2usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
ClosureKind::Fn => {}
ClosureKind::FnMut => {}
ClosureKind::FnOnce => {}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for ClosureKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { ClosureKind::Fn }
1usize => { ClosureKind::FnMut }
2usize => { ClosureKind::FnOnce }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `ClosureKind`, expected 0..3, actual {0}",
n));
}
}
}
}
};Decodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for ClosureKind
{
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
ClosureKind::Fn => {}
ClosureKind::FnMut => {}
ClosureKind::FnOnce => {}
}
}
}
};StableHash))]
412pub enum ClosureKind {
413 Fn,
414 FnMut,
415 FnOnce,
416}
417418impl ClosureKind {
419/// This is the initial value used when doing upvar inference.
420pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
421422pub const fn as_str(self) -> &'static str {
423match self {
424 ClosureKind::Fn => "Fn",
425 ClosureKind::FnMut => "FnMut",
426 ClosureKind::FnOnce => "FnOnce",
427 }
428 }
429430/// Returns `true` if a type that impls this closure kind
431 /// must also implement `other`.
432#[rustfmt::skip]
433pub fn extends(self, other: ClosureKind) -> bool {
434use ClosureKind::*;
435match (self, other) {
436 (Fn, Fn | FnMut | FnOnce)
437 | (FnMut, FnMut | FnOnce)
438 | (FnOnce, FnOnce) => true,
439_ => false,
440 }
441 }
442}
443444impl fmt::Displayfor ClosureKind {
445fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
446self.as_str().fmt(f)
447 }
448}
449450pub struct FieldInfo<I: Interner> {
451pub base: I::Ty,
452pub ty: I::Ty,
453pub variant: Option<I::Symbol>,
454pub variant_idx: VariantIdx,
455pub name: I::Symbol,
456pub field_idx: FieldIdx,
457}