4 extern crate rustc_macros;
6 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
7 use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
9 use std::mem::discriminant;
12 /// Flags that we track on types. These flags are propagated upwards
13 /// through the type during type construction, so that we can quickly check
14 /// whether the type has various kinds of types in it without recursing
15 /// over the type itself.
16 pub struct TypeFlags: u32 {
17 // Does this have parameters? Used to determine whether substitution is
19 /// Does this have `Param`?
20 const HAS_TY_PARAM = 1 << 0;
21 /// Does this have `ReEarlyBound`?
22 const HAS_RE_PARAM = 1 << 1;
23 /// Does this have `ConstKind::Param`?
24 const HAS_CT_PARAM = 1 << 2;
26 const NEEDS_SUBST = TypeFlags::HAS_TY_PARAM.bits
27 | TypeFlags::HAS_RE_PARAM.bits
28 | TypeFlags::HAS_CT_PARAM.bits;
30 /// Does this have `Infer`?
31 const HAS_TY_INFER = 1 << 3;
32 /// Does this have `ReVar`?
33 const HAS_RE_INFER = 1 << 4;
34 /// Does this have `ConstKind::Infer`?
35 const HAS_CT_INFER = 1 << 5;
37 /// Does this have inference variables? Used to determine whether
38 /// inference is required.
39 const NEEDS_INFER = TypeFlags::HAS_TY_INFER.bits
40 | TypeFlags::HAS_RE_INFER.bits
41 | TypeFlags::HAS_CT_INFER.bits;
43 /// Does this have `Placeholder`?
44 const HAS_TY_PLACEHOLDER = 1 << 6;
45 /// Does this have `RePlaceholder`?
46 const HAS_RE_PLACEHOLDER = 1 << 7;
47 /// Does this have `ConstKind::Placeholder`?
48 const HAS_CT_PLACEHOLDER = 1 << 8;
50 /// `true` if there are "names" of regions and so forth
51 /// that are local to a particular fn/inferctxt
52 const HAS_FREE_LOCAL_REGIONS = 1 << 9;
54 /// `true` if there are "names" of types and regions and so forth
55 /// that are local to a particular fn
56 const HAS_FREE_LOCAL_NAMES = TypeFlags::HAS_TY_PARAM.bits
57 | TypeFlags::HAS_CT_PARAM.bits
58 | TypeFlags::HAS_TY_INFER.bits
59 | TypeFlags::HAS_CT_INFER.bits
60 | TypeFlags::HAS_TY_PLACEHOLDER.bits
61 | TypeFlags::HAS_CT_PLACEHOLDER.bits
62 | TypeFlags::HAS_FREE_LOCAL_REGIONS.bits;
64 /// Does this have `Projection`?
65 const HAS_TY_PROJECTION = 1 << 10;
66 /// Does this have `Opaque`?
67 const HAS_TY_OPAQUE = 1 << 11;
68 /// Does this have `ConstKind::Unevaluated`?
69 const HAS_CT_PROJECTION = 1 << 12;
71 /// Could this type be normalized further?
72 const HAS_PROJECTION = TypeFlags::HAS_TY_PROJECTION.bits
73 | TypeFlags::HAS_TY_OPAQUE.bits
74 | TypeFlags::HAS_CT_PROJECTION.bits;
76 /// Is an error type/const reachable?
77 const HAS_ERROR = 1 << 13;
79 /// Does this have any region that "appears free" in the type?
80 /// Basically anything but `ReLateBound` and `ReErased`.
81 const HAS_FREE_REGIONS = 1 << 14;
83 /// Does this have any `ReLateBound` regions? Used to check
84 /// if a global bound is safe to evaluate.
85 const HAS_RE_LATE_BOUND = 1 << 15;
87 /// Does this have any `ReErased` regions?
88 const HAS_RE_ERASED = 1 << 16;
90 /// Does this value have parameters/placeholders/inference variables which could be
91 /// replaced later, in a way that would change the results of `impl` specialization?
92 const STILL_FURTHER_SPECIALIZABLE = 1 << 17;
96 rustc_index::newtype_index! {
97 /// A [De Bruijn index][dbi] is a standard means of representing
98 /// regions (and perhaps later types) in a higher-ranked setting. In
99 /// particular, imagine a type like this:
101 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
104 /// | +------------+ 0 | |
106 /// +----------------------------------+ 1 |
108 /// +----------------------------------------------+ 0
110 /// In this type, there are two binders (the outer fn and the inner
111 /// fn). We need to be able to determine, for any given region, which
112 /// fn type it is bound by, the inner or the outer one. There are
113 /// various ways you can do this, but a De Bruijn index is one of the
114 /// more convenient and has some nice properties. The basic idea is to
115 /// count the number of binders, inside out. Some examples should help
116 /// clarify what I mean.
118 /// Let's start with the reference type `&'b isize` that is the first
119 /// argument to the inner function. This region `'b` is assigned a De
120 /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
121 /// fn). The region `'a` that appears in the second argument type (`&'a
122 /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
123 /// second-innermost binder". (These indices are written on the arrows
126 /// What is interesting is that De Bruijn index attached to a particular
127 /// variable will vary depending on where it appears. For example,
128 /// the final type `&'a char` also refers to the region `'a` declared on
129 /// the outermost fn. But this time, this reference is not nested within
130 /// any other binders (i.e., it is not an argument to the inner fn, but
131 /// rather the outer one). Therefore, in this case, it is assigned a
132 /// De Bruijn index of 0, because the innermost binder in that location
135 /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
136 pub struct DebruijnIndex {
137 DEBUG_FORMAT = "DebruijnIndex({})",
143 /// Returns the resulting index when this value is moved into
144 /// `amount` number of new binders. So, e.g., if you had
146 /// for<'a> fn(&'a x)
148 /// and you wanted to change it to
150 /// for<'a> fn(for<'b> fn(&'a x))
152 /// you would need to shift the index for `'a` into a new binder.
154 pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
155 DebruijnIndex::from_u32(self.as_u32() + amount)
158 /// Update this index in place by shifting it "in" through
159 /// `amount` number of binders.
160 pub fn shift_in(&mut self, amount: u32) {
161 *self = self.shifted_in(amount);
164 /// Returns the resulting index when this value is moved out from
165 /// `amount` number of new binders.
167 pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
168 DebruijnIndex::from_u32(self.as_u32() - amount)
171 /// Update in place by shifting out from `amount` binders.
172 pub fn shift_out(&mut self, amount: u32) {
173 *self = self.shifted_out(amount);
176 /// Adjusts any De Bruijn indices so as to make `to_binder` the
177 /// innermost binder. That is, if we have something bound at `to_binder`,
178 /// it will now be bound at INNERMOST. This is an appropriate thing to do
179 /// when moving a region out from inside binders:
182 /// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
183 /// // Binder: D3 D2 D1 ^^
186 /// Here, the region `'a` would have the De Bruijn index D3,
187 /// because it is the bound 3 binders out. However, if we wanted
188 /// to refer to that region `'a` in the second argument (the `_`),
189 /// those two binders would not be in scope. In that case, we
190 /// might invoke `shift_out_to_binder(D3)`. This would adjust the
191 /// De Bruijn index of `'a` to D1 (the innermost binder).
193 /// If we invoke `shift_out_to_binder` and the region is in fact
194 /// bound by one of the binders we are shifting out of, that is an
195 /// error (and should fail an assertion failure).
196 pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
197 self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
201 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
202 #[derive(Encodable, Decodable)]
213 pub fn name_str(&self) -> &'static str {
215 IntTy::Isize => "isize",
220 IntTy::I128 => "i128",
224 pub fn bit_width(&self) -> Option<u64> {
226 IntTy::Isize => return None,
235 pub fn normalize(&self, target_width: u32) -> Self {
237 IntTy::Isize => match target_width {
248 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
249 #[derive(Encodable, Decodable)]
260 pub fn name_str(&self) -> &'static str {
262 UintTy::Usize => "usize",
264 UintTy::U16 => "u16",
265 UintTy::U32 => "u32",
266 UintTy::U64 => "u64",
267 UintTy::U128 => "u128",
271 pub fn bit_width(&self) -> Option<u64> {
273 UintTy::Usize => return None,
282 pub fn normalize(&self, target_width: u32) -> Self {
284 UintTy::Usize => match target_width {
295 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
296 #[derive(Encodable, Decodable)]
303 pub fn name_str(self) -> &'static str {
305 FloatTy::F32 => "f32",
306 FloatTy::F64 => "f64",
310 pub fn bit_width(self) -> u64 {
318 #[derive(Clone, Copy, PartialEq, Eq)]
319 pub enum IntVarValue {
324 #[derive(Clone, Copy, PartialEq, Eq)]
325 pub struct FloatVarValue(pub FloatTy);
327 /// A **ty**pe **v**ariable **ID**.
328 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
333 /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
334 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
339 /// An **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
340 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
341 pub struct FloatVid {
345 /// A placeholder for a type that hasn't been inferred yet.
347 /// E.g., if we have an empty array (`[]`), then we create a fresh
348 /// type variable for the element type since we won't know until it's
349 /// used what the element type is supposed to be.
350 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
354 /// An integral type variable (`{integer}`).
356 /// These are created when the compiler sees an integer literal like
357 /// `1` that could be several different types (`u8`, `i32`, `u32`, etc.).
358 /// We don't know until it's used what type it's supposed to be, so
359 /// we create a fresh type variable.
361 /// A floating-point type variable (`{float}`).
363 /// These are created when the compiler sees an float literal like
364 /// `1.0` that could be either an `f32` or an `f64`.
365 /// We don't know until it's used what type it's supposed to be, so
366 /// we create a fresh type variable.
369 /// A [`FreshTy`][Self::FreshTy] is one that is generated as a replacement
370 /// for an unbound type variable. This is convenient for caching etc. See
371 /// `rustc_infer::infer::freshen` for more details.
373 /// Compare with [`TyVar`][Self::TyVar].
375 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`IntVar`][Self::IntVar].
377 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`FloatVar`][Self::FloatVar].
381 /// Raw `TyVid` are used as the unification key for `sub_relations`;
382 /// they carry no values.
383 impl UnifyKey for TyVid {
385 fn index(&self) -> u32 {
388 fn from_index(i: u32) -> TyVid {
391 fn tag() -> &'static str {
396 impl EqUnifyValue for IntVarValue {}
398 impl UnifyKey for IntVid {
399 type Value = Option<IntVarValue>;
400 fn index(&self) -> u32 {
403 fn from_index(i: u32) -> IntVid {
406 fn tag() -> &'static str {
411 impl EqUnifyValue for FloatVarValue {}
413 impl UnifyKey for FloatVid {
414 type Value = Option<FloatVarValue>;
415 fn index(&self) -> u32 {
418 fn from_index(i: u32) -> FloatVid {
419 FloatVid { index: i }
421 fn tag() -> &'static str {
426 #[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash)]
428 Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
429 Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
430 Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
431 Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
435 /// `a.xform(b)` combines the variance of a context with the
436 /// variance of a type with the following meaning. If we are in a
437 /// context with variance `a`, and we encounter a type argument in
438 /// a position with variance `b`, then `a.xform(b)` is the new
439 /// variance with which the argument appears.
445 /// Here, the "ambient" variance starts as covariant. `*mut T` is
446 /// invariant with respect to `T`, so the variance in which the
447 /// `Vec<i32>` appears is `Covariant.xform(Invariant)`, which
448 /// yields `Invariant`. Now, the type `Vec<T>` is covariant with
449 /// respect to its type argument `T`, and hence the variance of
450 /// the `i32` here is `Invariant.xform(Covariant)`, which results
451 /// (again) in `Invariant`.
455 /// fn(*const Vec<i32>, *mut Vec<i32)
457 /// The ambient variance is covariant. A `fn` type is
458 /// contravariant with respect to its parameters, so the variance
459 /// within which both pointer types appear is
460 /// `Covariant.xform(Contravariant)`, or `Contravariant`. `*const
461 /// T` is covariant with respect to `T`, so the variance within
462 /// which the first `Vec<i32>` appears is
463 /// `Contravariant.xform(Covariant)` or `Contravariant`. The same
464 /// is true for its `i32` argument. In the `*mut T` case, the
465 /// variance of `Vec<i32>` is `Contravariant.xform(Invariant)`,
466 /// and hence the outermost type is `Invariant` with respect to
467 /// `Vec<i32>` (and its `i32` argument).
469 /// Source: Figure 1 of "Taming the Wildcards:
470 /// Combining Definition- and Use-Site Variance" published in PLDI'11.
471 pub fn xform(self, v: Variance) -> Variance {
473 // Figure 1, column 1.
474 (Variance::Covariant, Variance::Covariant) => Variance::Covariant,
475 (Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
476 (Variance::Covariant, Variance::Invariant) => Variance::Invariant,
477 (Variance::Covariant, Variance::Bivariant) => Variance::Bivariant,
479 // Figure 1, column 2.
480 (Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
481 (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
482 (Variance::Contravariant, Variance::Invariant) => Variance::Invariant,
483 (Variance::Contravariant, Variance::Bivariant) => Variance::Bivariant,
485 // Figure 1, column 3.
486 (Variance::Invariant, _) => Variance::Invariant,
488 // Figure 1, column 4.
489 (Variance::Bivariant, _) => Variance::Bivariant,
494 impl<CTX> HashStable<CTX> for DebruijnIndex {
495 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
496 self.as_u32().hash_stable(ctx, hasher);
500 impl<CTX> HashStable<CTX> for IntTy {
501 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
502 discriminant(self).hash_stable(ctx, hasher);
506 impl<CTX> HashStable<CTX> for UintTy {
507 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
508 discriminant(self).hash_stable(ctx, hasher);
512 impl<CTX> HashStable<CTX> for FloatTy {
513 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
514 discriminant(self).hash_stable(ctx, hasher);
518 impl<CTX> HashStable<CTX> for InferTy {
519 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
522 TyVar(v) => v.index.hash_stable(ctx, hasher),
523 IntVar(v) => v.index.hash_stable(ctx, hasher),
524 FloatVar(v) => v.index.hash_stable(ctx, hasher),
525 FreshTy(v) | FreshIntTy(v) | FreshFloatTy(v) => v.hash_stable(ctx, hasher),
530 impl<CTX> HashStable<CTX> for Variance {
531 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
532 discriminant(self).hash_stable(ctx, hasher);
536 impl fmt::Debug for IntVarValue {
537 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
539 IntVarValue::IntType(ref v) => v.fmt(f),
540 IntVarValue::UintType(ref v) => v.fmt(f),
545 impl fmt::Debug for FloatVarValue {
546 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
551 impl fmt::Debug for TyVid {
552 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
553 write!(f, "_#{}t", self.index)
557 impl fmt::Debug for IntVid {
558 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
559 write!(f, "_#{}i", self.index)
563 impl fmt::Debug for FloatVid {
564 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
565 write!(f, "_#{}f", self.index)
569 impl fmt::Debug for InferTy {
570 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
573 TyVar(ref v) => v.fmt(f),
574 IntVar(ref v) => v.fmt(f),
575 FloatVar(ref v) => v.fmt(f),
576 FreshTy(v) => write!(f, "FreshTy({:?})", v),
577 FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
578 FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
583 impl fmt::Debug for Variance {
584 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
585 f.write_str(match *self {
586 Variance::Covariant => "+",
587 Variance::Contravariant => "-",
588 Variance::Invariant => "o",
589 Variance::Bivariant => "*",
594 impl fmt::Display for InferTy {
595 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
598 TyVar(_) => write!(f, "_"),
599 IntVar(_) => write!(f, "{}", "{integer}"),
600 FloatVar(_) => write!(f, "{}", "{float}"),
601 FreshTy(v) => write!(f, "FreshTy({})", v),
602 FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
603 FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v),