1//! This crate is an abstraction layer, shared between rustc and rust-analyzer, to help with the
2//! overlapping responsibilities (like type inference and trait solving), reduce duplication, and
3//! maintain consistent behavior between the two implementations.
4//!
5//! It defines fundamental interfaces for types, predicates, and the context required by the next
6//! trait solver.
7//!
8//! Both rustc and rust-analyzer immplement these traits for their own concrete implementations, and
9//! `rustc_next_trait_solver` is written to be generic over these abstractions.
10//!
11//! In addition to these interfaces, it also contains components built on top of the abstraction
12//! layer, for example elaboration logic, and the search graph machinery used by the solver, as well
13//! as items that do not need compiler-specific implementations.
14//!
15//! Note that rust-analyzer is built with a stable compiler, while rustc uses unstable features, so
16//! this crate and some of its dependencies need to separate unstable code under the `nightly`
17//! feature.
18//!
19//! There are more details available in a [dedicated dev-guide
20//! chapter](https://rustc-dev-guide.rust-lang.org/solve/sharing-crates-with-rust-analyzer.html).
2122#![cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir")]
23// tidy-alphabetical-start
24#![allow(rustc::direct_use_of_rustc_type_ir)]
25#![allow(rustc::usage_of_ty_tykind)]
26#![allow(rustc::usage_of_type_ir_inherent)]
27#![allow(rustc::usage_of_type_ir_traits)]
28#![cfg_attr(feature = "nightly", allow(internal_features))]
29#![cfg_attr(feature = "nightly", feature(associated_type_defaults))]
30#![cfg_attr(feature = "nightly", feature(negative_impls))]
31#![cfg_attr(feature = "nightly", feature(register_tool))]
32#![cfg_attr(feature = "nightly", feature(rustc_attrs))]
33#![cfg_attr(feature = "nightly", register_tool(lint))]
34#![deny(rustc::missing_generic_type_visitable_derive)]
35// tidy-alphabetical-end
3637extern crate self as rustc_type_ir;
3839use std::fmt;
40use std::hash::Hash;
4142use rustc_abi::{FieldIdx, VariantIdx};
43#[cfg(feature = "nightly")]
44use rustc_macros::{Decodable, Encodable, StableHash};
4546// These modules are `pub` since they are not glob-imported.
47pub mod data_structures;
48pub mod elaborate;
49pub mod error;
50pub mod fast_reject;
51#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_inherent")]
52pub mod inherent;
53pub mod intern;
54pub mod ir_print;
55pub mod lang_items;
56pub mod lift;
57pub mod outlives;
58pub mod region_constraint;
59pub mod relate;
60pub mod search_graph;
61pub mod solve;
62pub mod sty;
63pub mod transmute;
64pub mod walk;
6566// These modules are not `pub` since they are glob-imported.
67#[macro_use]
68mod macros;
69mod binder;
70mod canonical;
71mod const_kind;
72mod flags;
73mod fold;
74mod generic_arg;
75mod generic_visit;
76mod infer_ctxt;
77mod interner;
78mod opaque_ty;
79mod param_env;
80mod pattern;
81mod predicate;
82mod predicate_kind;
83mod region_kind;
84#[cfg(feature = "nightly")]
85mod serialize;
86mod term_kind;
87mod ty;
88mod ty_info;
89mod ty_kind;
90mod universe;
91mod unnormalized;
92mod upcast;
93mod visit;
9495pub use AliasTyKind::*;
96pub use InferTy::*;
97pub use RegionKind::*;
98pub use TyKind::*;
99pub use Variance::*;
100pub use binder::{Placeholder, *};
101pub use canonical::*;
102pub use const_kind::*;
103pub use flags::*;
104pub use fold::*;
105pub use generic_arg::*;
106pub use generic_visit::*;
107pub use infer_ctxt::*;
108pub use interner::*;
109pub use opaque_ty::*;
110pub use param_env::*;
111pub use pattern::*;
112pub use predicate::*;
113pub use predicate_kind::*;
114pub use region_kind::*;
115pub use rustc_ast_ir::{FloatTy, IntTy, Movability, Mutability, Pinnedness, UintTy};
116#[cfg(feature = "nightly")]
117pub use serialize::*;
118pub use sty::*;
119pub use term_kind::*;
120pub use ty::{Alias, *};
121pub use ty_info::*;
122pub use ty_kind::*;
123pub use universe::*;
124pub use unnormalized::Unnormalized;
125pub use upcast::*;
126pub use visit::*;
127128#[automatically_derived]impl ::core::marker::Copy for DebruijnIndex { }
pub const INNERMOST: DebruijnIndex = DebruijnIndex::from_u32(0);
impl DebruijnIndex {
#[doc = r" Maximum value the index can take, as a `u32`."]
pub const MAX_AS_U32: u32 = 0xFFFF_FF00;
#[doc = r" Maximum value the index can take."]
pub const MAX: Self = Self::from_u32(0xFFFF_FF00);
#[doc = r" Zero value of the index."]
pub const ZERO: Self = Self::from_u32(0);
#[doc = r" Creates a new index from a given `usize`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_usize(value: usize) -> Self {
if !(value <= (0xFFFF_FF00 as usize)) {
::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
};
unsafe { Self::from_u32_unchecked(value as u32) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u32(value: u32) -> Self {
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u16`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u16(value: u16) -> Self {
let value = value as u32;
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Safety"]
#[doc = r""]
#[doc =
r" The provided value must be less than or equal to the maximum value for the newtype."]
#[doc =
r" Providing a value outside this range is undefined due to layout restrictions."]
#[doc = r""]
#[doc = r" Prefer using `from_u32`."]
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self {
private_use_as_methods_instead: unsafe {
std::mem::transmute(value)
},
}
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn index(self) -> usize { self.as_usize() }
#[doc = r" Extracts the value of this index as a `u32`."]
#[inline]
pub const fn as_u32(self) -> u32 {
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for DebruijnIndex {
type Output = Self;
#[inline]
fn add(self, other: usize) -> Self {
Self::from_usize(self.index() + other)
}
}
impl std::ops::AddAssign<usize> for DebruijnIndex {
#[inline]
fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for DebruijnIndex {
#[inline]
fn new(value: usize) -> Self { Self::from_usize(value) }
#[inline]
fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for DebruijnIndex {
#[inline]
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
<usize as
::std::iter::Step>::steps_between(&Self::index(*start),
&Self::index(*end))
}
#[inline]
fn forward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_add(u).map(Self::from_usize)
}
#[inline]
fn backward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
#[inline]
fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_add(u);
(Self::from_usize(s), o)
}
#[inline]
fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_sub(u);
(Self::from_usize(s), o)
}
}
impl ::std::cmp::Ord for DebruijnIndex {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for DebruijnIndex {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ::rustc_data_structures::stable_hash::StableHash for DebruijnIndex {
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
hcx: &mut __Hcx,
hasher: &mut ::rustc_data_structures::stable_hash::StableHasher) {
self.as_u32().stable_hash(hcx, hasher)
}
}
impl From<DebruijnIndex> for u32 {
#[inline]
fn from(v: DebruijnIndex) -> u32 { v.as_u32() }
}
impl From<DebruijnIndex> for usize {
#[inline]
fn from(v: DebruijnIndex) -> usize { v.as_usize() }
}
impl From<usize> for DebruijnIndex {
#[inline]
fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for DebruijnIndex {
#[inline]
fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for DebruijnIndex {}
impl ::std::cmp::PartialEq for DebruijnIndex {
fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for DebruijnIndex { }
impl ::std::hash::Hash for DebruijnIndex {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for
DebruijnIndex {
fn decode(d: &mut D) -> Self { Self::from_u32(d.read_u32()) }
}
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for
DebruijnIndex {
fn encode(&self, e: &mut E) { e.emit_u32(self.as_u32()); }
}
impl ::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! {
129/// A [De Bruijn index][dbi] is a standard means of representing
130 /// regions (and perhaps later types) in a higher-ranked setting. In
131 /// particular, imagine a type like this:
132 /// ```ignore (illustrative)
133 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
134 /// // ^ ^ | | |
135 /// // | | | | |
136 /// // | +------------+ 0 | |
137 /// // | | |
138 /// // +----------------------------------+ 1 |
139 /// // | |
140 /// // +----------------------------------------------+ 0
141 /// ```
142 /// In this type, there are two binders (the outer fn and the inner
143 /// fn). We need to be able to determine, for any given region, which
144 /// fn type it is bound by, the inner or the outer one. There are
145 /// various ways you can do this, but a De Bruijn index is one of the
146 /// more convenient and has some nice properties. The basic idea is to
147 /// count the number of binders, inside out. Some examples should help
148 /// clarify what I mean.
149 ///
150 /// Let's start with the reference type `&'b isize` that is the first
151 /// argument to the inner function. This region `'b` is assigned a De
152 /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
153 /// fn). The region `'a` that appears in the second argument type (`&'a
154 /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
155 /// second-innermost binder". (These indices are written on the arrows
156 /// in the diagram).
157 ///
158 /// What is interesting is that De Bruijn index attached to a particular
159 /// variable will vary depending on where it appears. For example,
160 /// the final type `&'a char` also refers to the region `'a` declared on
161 /// the outermost fn. But this time, this reference is not nested within
162 /// any other binders (i.e., it is not an argument to the inner fn, but
163 /// rather the outer one). Therefore, in this case, it is assigned a
164 /// De Bruijn index of 0, because the innermost binder in that location
165 /// is the outer fn.
166 ///
167 /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
168#[stable_hash]
169 #[encodable]
170 #[orderable]
171 #[debug_format = "DebruijnIndex({})"]
172 #[gate_rustc_only]
173pub struct DebruijnIndex {
174const INNERMOST = 0;
175 }
176}177178impl DebruijnIndex {
179/// Returns the resulting index when this value is moved into
180 /// `amount` number of new binders. So, e.g., if you had
181 ///
182 /// for<'a> fn(&'a x)
183 ///
184 /// and you wanted to change it to
185 ///
186 /// for<'a> fn(for<'b> fn(&'a x))
187 ///
188 /// you would need to shift the index for `'a` into a new binder.
189#[inline]
190 #[must_use]
191pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
192DebruijnIndex::from_u32(self.as_u32() + amount)
193 }
194195/// Update this index in place by shifting it "in" through
196 /// `amount` number of binders.
197#[inline]
198pub fn shift_in(&mut self, amount: u32) {
199*self = self.shifted_in(amount);
200 }
201202/// Returns the resulting index when this value is moved out from
203 /// `amount` number of new binders.
204#[inline]
205 #[must_use]
206pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
207DebruijnIndex::from_u32(self.as_u32() - amount)
208 }
209210/// Update in place by shifting out from `amount` binders.
211#[inline]
212pub fn shift_out(&mut self, amount: u32) {
213*self = self.shifted_out(amount);
214 }
215216/// Adjusts any De Bruijn indices so as to make `to_binder` the
217 /// innermost binder. That is, if we have something bound at `to_binder`,
218 /// it will now be bound at INNERMOST. This is an appropriate thing to do
219 /// when moving a region out from inside binders:
220 ///
221 /// ```ignore (illustrative)
222 /// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
223 /// // Binder: D3 D2 D1 ^^
224 /// ```
225 ///
226 /// Here, the region `'a` would have the De Bruijn index D3,
227 /// because it is the bound 3 binders out. However, if we wanted
228 /// to refer to that region `'a` in the second argument (the `_`),
229 /// those two binders would not be in scope. In that case, we
230 /// might invoke `shift_out_to_binder(D3)`. This would adjust the
231 /// De Bruijn index of `'a` to D1 (the innermost binder).
232 ///
233 /// If we invoke `shift_out_to_binder` and the region is in fact
234 /// bound by one of the binders we are shifting out of, that is an
235 /// error (and should fail an assertion failure).
236#[inline]
237pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
238self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
239 }
240}
241242pub fn debug_bound_var<T: std::fmt::Write>(
243 fmt: &mut T,
244 bound_index: BoundVarIndexKind,
245 var: impl std::fmt::Debug,
246) -> Result<(), std::fmt::Error> {
247match bound_index {
248 BoundVarIndexKind::Bound(debruijn) => {
249if debruijn == INNERMOST {
250fmt.write_fmt(format_args!("^{0:?}", var))write!(fmt, "^{var:?}")251 } else {
252fmt.write_fmt(format_args!("^{0}_{1:?}", debruijn.index(), var))write!(fmt, "^{}_{:?}", debruijn.index(), var)253 }
254 }
255 BoundVarIndexKind::Canonical => {
256fmt.write_fmt(format_args!("^c_{0:?}", var))write!(fmt, "^c_{:?}", var)257 }
258 }
259}
260261#[derive(#[automatically_derived]impl ::core::marker::Copy for Variance { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for Variance {
}
#[automatically_derived]impl ::core::clone::Clone for Variance {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::StructuralPartialEq for Variance
{
}
#[automatically_derived]impl ::core::cmp::PartialEq for Variance {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for Variance { }Eq, #[automatically_derived]impl ::core::hash::Hash for Variance {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state)
}
}Hash)]
262#[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);
}
}
};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) {
match *self {
Variance::Covariant => 0u8.stable_hash(__hcx, __hasher),
Variance::Invariant => 1u8.stable_hash(__hcx, __hasher),
Variance::Contravariant => 2u8.stable_hash(__hcx, __hasher),
Variance::Bivariant => 3u8.stable_hash(__hcx, __hasher),
}
match *self {
Variance::Covariant => {}
Variance::Invariant => {}
Variance::Contravariant => {}
Variance::Bivariant => {}
}
}
}
};StableHash))]
263#[cfg_attr(
264 feature = "nightly",
265 cfg_attr(bootstrap, rustc_pass_by_value),
266 cfg_attr(not(bootstrap), lint::pass_by_value)
267)]
268pub enum Variance {
269 Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
270Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
271Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
272Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
273}
274275impl Variance {
276/// `a.xform(b)` combines the variance of a context with the
277 /// variance of a type with the following meaning. If we are in a
278 /// context with variance `a`, and we encounter a type argument in
279 /// a position with variance `b`, then `a.xform(b)` is the new
280 /// variance with which the argument appears.
281 ///
282 /// Example 1:
283 /// ```ignore (illustrative)
284 /// *mut Vec<i32>
285 /// ```
286 /// Here, the "ambient" variance starts as covariant. `*mut T` is
287 /// invariant with respect to `T`, so the variance in which the
288 /// `Vec<i32>` appears is `Covariant.xform(Invariant)`, which
289 /// yields `Invariant`. Now, the type `Vec<T>` is covariant with
290 /// respect to its type argument `T`, and hence the variance of
291 /// the `i32` here is `Invariant.xform(Covariant)`, which results
292 /// (again) in `Invariant`.
293 ///
294 /// Example 2:
295 /// ```ignore (illustrative)
296 /// fn(*const Vec<i32>, *mut Vec<i32)
297 /// ```
298 /// The ambient variance is covariant. A `fn` type is
299 /// contravariant with respect to its parameters, so the variance
300 /// within which both pointer types appear is
301 /// `Covariant.xform(Contravariant)`, or `Contravariant`. `*const
302 /// T` is covariant with respect to `T`, so the variance within
303 /// which the first `Vec<i32>` appears is
304 /// `Contravariant.xform(Covariant)` or `Contravariant`. The same
305 /// is true for its `i32` argument. In the `*mut T` case, the
306 /// variance of `Vec<i32>` is `Contravariant.xform(Invariant)`,
307 /// and hence the outermost type is `Invariant` with respect to
308 /// `Vec<i32>` (and its `i32` argument).
309 ///
310 /// Source: Figure 1 of "Taming the Wildcards:
311 /// Combining Definition- and Use-Site Variance" published in PLDI'11.
312pub fn xform(self, v: Variance) -> Variance {
313match (self, v) {
314// Figure 1, column 1.
315(Variance::Covariant, Variance::Covariant) => Variance::Covariant,
316 (Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
317 (Variance::Covariant, Variance::Invariant) => Variance::Invariant,
318 (Variance::Covariant, Variance::Bivariant) => Variance::Bivariant,
319320// Figure 1, column 2.
321(Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
322 (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
323 (Variance::Contravariant, Variance::Invariant) => Variance::Invariant,
324 (Variance::Contravariant, Variance::Bivariant) => Variance::Bivariant,
325326// Figure 1, column 3.
327(Variance::Invariant, _) => Variance::Invariant,
328329// Figure 1, column 4.
330(Variance::Bivariant, _) => Variance::Bivariant,
331 }
332 }
333}
334335impl fmt::Debugfor Variance {
336fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
337f.write_str(match *self {
338 Variance::Covariant => "+",
339 Variance::Contravariant => "-",
340 Variance::Invariant => "o",
341 Variance::Bivariant => "*",
342 })
343 }
344}
345346#[automatically_derived]impl ::core::marker::Copy for UniverseIndex { }
impl UniverseIndex {
#[doc = r" Maximum value the index can take, as a `u32`."]
pub const MAX_AS_U32: u32 = 0xFFFF_FF00;
#[doc = r" Maximum value the index can take."]
pub const MAX: Self = Self::from_u32(0xFFFF_FF00);
#[doc = r" Zero value of the index."]
pub const ZERO: Self = Self::from_u32(0);
#[doc = r" Creates a new index from a given `usize`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_usize(value: usize) -> Self {
if !(value <= (0xFFFF_FF00 as usize)) {
::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
};
unsafe { Self::from_u32_unchecked(value as u32) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u32(value: u32) -> Self {
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u16`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u16(value: u16) -> Self {
let value = value as u32;
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Safety"]
#[doc = r""]
#[doc =
r" The provided value must be less than or equal to the maximum value for the newtype."]
#[doc =
r" Providing a value outside this range is undefined due to layout restrictions."]
#[doc = r""]
#[doc = r" Prefer using `from_u32`."]
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self {
private_use_as_methods_instead: unsafe {
std::mem::transmute(value)
},
}
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn index(self) -> usize { self.as_usize() }
#[doc = r" Extracts the value of this index as a `u32`."]
#[inline]
pub const fn as_u32(self) -> u32 {
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for UniverseIndex {
type Output = Self;
#[inline]
fn add(self, other: usize) -> Self {
Self::from_usize(self.index() + other)
}
}
impl std::ops::AddAssign<usize> for UniverseIndex {
#[inline]
fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for UniverseIndex {
#[inline]
fn new(value: usize) -> Self { Self::from_usize(value) }
#[inline]
fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for UniverseIndex {
#[inline]
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
<usize as
::std::iter::Step>::steps_between(&Self::index(*start),
&Self::index(*end))
}
#[inline]
fn forward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_add(u).map(Self::from_usize)
}
#[inline]
fn backward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
#[inline]
fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_add(u);
(Self::from_usize(s), o)
}
#[inline]
fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_sub(u);
(Self::from_usize(s), o)
}
}
impl ::std::cmp::Ord for UniverseIndex {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for UniverseIndex {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ::rustc_data_structures::stable_hash::StableHash for UniverseIndex {
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
hcx: &mut __Hcx,
hasher: &mut ::rustc_data_structures::stable_hash::StableHasher) {
self.as_u32().stable_hash(hcx, hasher)
}
}
impl std::default::Default for UniverseIndex {
#[inline]
fn default() -> Self { Self::ZERO }
}
impl From<UniverseIndex> for u32 {
#[inline]
fn from(v: UniverseIndex) -> u32 { v.as_u32() }
}
impl From<UniverseIndex> for usize {
#[inline]
fn from(v: UniverseIndex) -> usize { v.as_usize() }
}
impl From<usize> for UniverseIndex {
#[inline]
fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for UniverseIndex {
#[inline]
fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for UniverseIndex {}
impl ::std::cmp::PartialEq for UniverseIndex {
fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for UniverseIndex { }
impl ::std::hash::Hash for UniverseIndex {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for
UniverseIndex {
fn decode(d: &mut D) -> Self { Self::from_u32(d.read_u32()) }
}
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for
UniverseIndex {
fn encode(&self, e: &mut E) { e.emit_u32(self.as_u32()); }
}
impl ::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! {
347/// "Universes" are used during type- and trait-checking in the
348 /// presence of `for<..>` binders to control what sets of names are
349 /// visible. Universes are arranged into a tree: the root universe
350 /// contains names that are always visible. Each child then adds a new
351 /// set of names that are visible, in addition to those of its parent.
352 /// We say that the child universe "extends" the parent universe with
353 /// new names.
354 ///
355 /// To make this more concrete, consider this program:
356 ///
357 /// ```ignore (illustrative)
358 /// struct Foo { }
359 /// fn bar<T>(x: T) {
360 /// let y: for<'a> fn(&'a u8, Foo) = ...;
361 /// }
362 /// ```
363 ///
364 /// The struct name `Foo` is in the root universe U0. But the type
365 /// parameter `T`, introduced on `bar`, is in an extended universe U1
366 /// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
367 /// of `bar`, we cannot name `T`. Then, within the type of `y`, the
368 /// region `'a` is in a universe U2 that extends U1, because we can
369 /// name it inside the fn type but not outside.
370 ///
371 /// Universes are used to do type- and trait-checking around these
372 /// "forall" binders (also called **universal quantification**). The
373 /// idea is that when, in the body of `bar`, we refer to `T` as a
374 /// type, we aren't referring to any type in particular, but rather a
375 /// kind of "fresh" type that is distinct from all other types we have
376 /// actually declared. This is called a **placeholder** type, and we
377 /// use universes to talk about this. In other words, a type name in
378 /// universe 0 always corresponds to some "ground" type that the user
379 /// declared, but a type name in a non-zero universe is a placeholder
380 /// type -- an idealized representative of "types in general" that we
381 /// use for checking generic functions.
382#[stable_hash]
383 #[default]
384 #[encodable]
385 #[orderable]
386 #[debug_format = "U{}"]
387 #[gate_rustc_only]
388pub struct UniverseIndex {}
389}390391impl UniverseIndex {
392pub const ROOT: UniverseIndex = UniverseIndex::ZERO;
393394/// Returns the "next" universe index in order -- this new index
395 /// is considered to extend all previous universes. This
396 /// corresponds to entering a `forall` quantifier. So, for
397 /// example, suppose we have this type in universe `U`:
398 ///
399 /// ```ignore (illustrative)
400 /// for<'a> fn(&'a u32)
401 /// ```
402 ///
403 /// Once we "enter" into this `for<'a>` quantifier, we are in a
404 /// new universe that extends `U` -- in this new universe, we can
405 /// name the region `'a`, but that region was not nameable from
406 /// `U` because it was not in scope there.
407pub fn next_universe(self) -> UniverseIndex {
408UniverseIndex::from_u32(self.as_u32().checked_add(1).unwrap())
409 }
410411/// Returns `true` if `self` can name a name from `other` -- in other words,
412 /// if the set of names in `self` is a superset of those in
413 /// `other` (`self >= other`).
414pub fn can_name(self, other: UniverseIndex) -> bool {
415self >= other416 }
417418/// Returns `true` if `self` cannot name some names from `other` -- in other
419 /// words, if the set of names in `self` is a strict subset of
420 /// those in `other` (`self < other`).
421pub fn cannot_name(self, other: UniverseIndex) -> bool {
422self < other423 }
424425/// Returns `true` if `self` is the root universe, otherwise false.
426pub fn is_root(self) -> bool {
427self == Self::ROOT428 }
429}
430431#[automatically_derived]impl ::core::marker::Copy for BoundVar { }
impl BoundVar {
#[doc = r" Maximum value the index can take, as a `u32`."]
pub const MAX_AS_U32: u32 = 0xFFFF_FF00;
#[doc = r" Maximum value the index can take."]
pub const MAX: Self = Self::from_u32(0xFFFF_FF00);
#[doc = r" Zero value of the index."]
pub const ZERO: Self = Self::from_u32(0);
#[doc = r" Creates a new index from a given `usize`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_usize(value: usize) -> Self {
if !(value <= (0xFFFF_FF00 as usize)) {
::core::panicking::panic("assertion failed: value <= (0xFFFF_FF00 as usize)")
};
unsafe { Self::from_u32_unchecked(value as u32) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u32(value: u32) -> Self {
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u16`."]
#[doc = r""]
#[doc = r" # Panics"]
#[doc = r""]
#[doc = r" Will panic if `value` exceeds `MAX`."]
#[inline]
pub const fn from_u16(value: u16) -> Self {
let value = value as u32;
if !(value <= 0xFFFF_FF00) {
::core::panicking::panic("assertion failed: value <= 0xFFFF_FF00")
};
unsafe { Self::from_u32_unchecked(value) }
}
#[doc = r" Creates a new index from a given `u32`."]
#[doc = r""]
#[doc = r" # Safety"]
#[doc = r""]
#[doc =
r" The provided value must be less than or equal to the maximum value for the newtype."]
#[doc =
r" Providing a value outside this range is undefined due to layout restrictions."]
#[doc = r""]
#[doc = r" Prefer using `from_u32`."]
#[inline]
pub const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self {
private_use_as_methods_instead: unsafe {
std::mem::transmute(value)
},
}
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn index(self) -> usize { self.as_usize() }
#[doc = r" Extracts the value of this index as a `u32`."]
#[inline]
pub const fn as_u32(self) -> u32 {
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
#[doc = r" Extracts the value of this index as a `usize`."]
#[inline]
pub const fn as_usize(self) -> usize { self.as_u32() as usize }
}
impl std::ops::Add<usize> for BoundVar {
type Output = Self;
#[inline]
fn add(self, other: usize) -> Self {
Self::from_usize(self.index() + other)
}
}
impl std::ops::AddAssign<usize> for BoundVar {
#[inline]
fn add_assign(&mut self, other: usize) { *self = *self + other; }
}
impl rustc_index::Idx for BoundVar {
#[inline]
fn new(value: usize) -> Self { Self::from_usize(value) }
#[inline]
fn index(self) -> usize { self.as_usize() }
}
impl ::std::iter::Step for BoundVar {
#[inline]
fn steps_between(start: &Self, end: &Self) -> (usize, Option<usize>) {
<usize as
::std::iter::Step>::steps_between(&Self::index(*start),
&Self::index(*end))
}
#[inline]
fn forward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_add(u).map(Self::from_usize)
}
#[inline]
fn backward_checked(start: Self, u: usize) -> Option<Self> {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
#[inline]
fn forward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_add(u);
(Self::from_usize(s), o)
}
#[inline]
fn backward_overflowing(start: Self, u: usize) -> (Self, bool) {
let (s, o) = Self::index(start).overflowing_sub(u);
(Self::from_usize(s), o)
}
}
impl ::std::cmp::Ord for BoundVar {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for BoundVar {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl ::rustc_data_structures::stable_hash::StableHash for BoundVar {
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
hcx: &mut __Hcx,
hasher: &mut ::rustc_data_structures::stable_hash::StableHasher) {
self.as_u32().stable_hash(hcx, hasher)
}
}
impl From<BoundVar> for u32 {
#[inline]
fn from(v: BoundVar) -> u32 { v.as_u32() }
}
impl From<BoundVar> for usize {
#[inline]
fn from(v: BoundVar) -> usize { v.as_usize() }
}
impl From<usize> for BoundVar {
#[inline]
fn from(value: usize) -> Self { Self::from_usize(value) }
}
impl From<u32> for BoundVar {
#[inline]
fn from(value: u32) -> Self { Self::from_u32(value) }
}
impl ::std::cmp::Eq for BoundVar {}
impl ::std::cmp::PartialEq for BoundVar {
fn eq(&self, other: &Self) -> bool { self.as_u32().eq(&other.as_u32()) }
}
impl ::std::marker::StructuralPartialEq for BoundVar { }
impl ::std::hash::Hash for BoundVar {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
impl<D: ::rustc_serialize::Decoder> ::rustc_serialize::Decodable<D> for
BoundVar {
fn decode(d: &mut D) -> Self { Self::from_u32(d.read_u32()) }
}
impl<E: ::rustc_serialize::Encoder> ::rustc_serialize::Encodable<E> for
BoundVar {
fn encode(&self, e: &mut E) { e.emit_u32(self.as_u32()); }
}
impl ::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! {
432#[stable_hash]
433 #[encodable]
434 #[orderable]
435 #[debug_format = "{}"]
436 #[gate_rustc_only]
437pub struct BoundVar {}
438}439440/// Represents the various closure traits in the language. This
441/// will determine the type of the environment (`self`, in the
442/// desugaring) argument that the closure expects.
443///
444/// You can get the environment type of a closure using
445/// `tcx.closure_env_ty()`.
446#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for ClosureKind {
}
#[automatically_derived]impl ::core::clone::Clone for ClosureKind {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for ClosureKind { }Copy, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
ClosureKind {
}
#[automatically_derived]impl ::core::cmp::PartialEq for ClosureKind {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for ClosureKind { }Eq, #[automatically_derived]impl ::core::hash::Hash for ClosureKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state)
}
}Hash, #[automatically_derived]impl ::core::fmt::Debug for ClosureKind {
#[inline]
#[rustc_trivial_field_reads]
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)]
447#[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);
}
}
};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) {
match *self {
ClosureKind::Fn => 0u8.stable_hash(__hcx, __hasher),
ClosureKind::FnMut => 1u8.stable_hash(__hcx, __hasher),
ClosureKind::FnOnce => 2u8.stable_hash(__hcx, __hasher),
}
match *self {
ClosureKind::Fn => {}
ClosureKind::FnMut => {}
ClosureKind::FnOnce => {}
}
}
}
};StableHash))]
448pub enum ClosureKind {
449 Fn,
450 FnMut,
451 FnOnce,
452}
453454impl ClosureKind {
455/// This is the initial value used when doing upvar inference.
456pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
457458pub const fn as_str(self) -> &'static str {
459match self {
460 ClosureKind::Fn => "Fn",
461 ClosureKind::FnMut => "FnMut",
462 ClosureKind::FnOnce => "FnOnce",
463 }
464 }
465466/// Returns `true` if a type that impls this closure kind
467 /// must also implement `other`.
468#[rustfmt::skip]
469pub fn extends(self, other: ClosureKind) -> bool {
470use ClosureKind::*;
471match (self, other) {
472 (Fn, Fn | FnMut | FnOnce)
473 | (FnMut, FnMut | FnOnce)
474 | (FnOnce, FnOnce) => true,
475_ => false,
476 }
477 }
478}
479480impl fmt::Displayfor ClosureKind {
481fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
482self.as_str().fmt(f)
483 }
484}
485486pub struct FieldInfo<I: Interner> {
487pub base: I::Ty,
488pub ty: I::Ty,
489pub variant: Option<I::Symbol>,
490pub variant_idx: VariantIdx,
491pub name: I::Symbol,
492pub field_idx: FieldIdx,
493}