1 #![feature(min_specialization)]
6 extern crate rustc_macros;
8 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9 use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
11 use std::mem::discriminant;
14 /// Flags that we track on types. These flags are propagated upwards
15 /// through the type during type construction, so that we can quickly check
16 /// whether the type has various kinds of types in it without recursing
17 /// over the type itself.
18 pub struct TypeFlags: u32 {
19 // Does this have parameters? Used to determine whether substitution is
21 /// Does this have `Param`?
22 const HAS_TY_PARAM = 1 << 0;
23 /// Does this have `ReEarlyBound`?
24 const HAS_RE_PARAM = 1 << 1;
25 /// Does this have `ConstKind::Param`?
26 const HAS_CT_PARAM = 1 << 2;
28 const NEEDS_SUBST = TypeFlags::HAS_TY_PARAM.bits
29 | TypeFlags::HAS_RE_PARAM.bits
30 | TypeFlags::HAS_CT_PARAM.bits;
32 /// Does this have `Infer`?
33 const HAS_TY_INFER = 1 << 3;
34 /// Does this have `ReVar`?
35 const HAS_RE_INFER = 1 << 4;
36 /// Does this have `ConstKind::Infer`?
37 const HAS_CT_INFER = 1 << 5;
39 /// Does this have inference variables? Used to determine whether
40 /// inference is required.
41 const NEEDS_INFER = TypeFlags::HAS_TY_INFER.bits
42 | TypeFlags::HAS_RE_INFER.bits
43 | TypeFlags::HAS_CT_INFER.bits;
45 /// Does this have `Placeholder`?
46 const HAS_TY_PLACEHOLDER = 1 << 6;
47 /// Does this have `RePlaceholder`?
48 const HAS_RE_PLACEHOLDER = 1 << 7;
49 /// Does this have `ConstKind::Placeholder`?
50 const HAS_CT_PLACEHOLDER = 1 << 8;
52 /// `true` if there are "names" of regions and so forth
53 /// that are local to a particular fn/inferctxt
54 const HAS_FREE_LOCAL_REGIONS = 1 << 9;
56 /// `true` if there are "names" of types and regions and so forth
57 /// that are local to a particular fn
58 const HAS_FREE_LOCAL_NAMES = TypeFlags::HAS_TY_PARAM.bits
59 | TypeFlags::HAS_CT_PARAM.bits
60 | TypeFlags::HAS_TY_INFER.bits
61 | TypeFlags::HAS_CT_INFER.bits
62 | TypeFlags::HAS_TY_PLACEHOLDER.bits
63 | TypeFlags::HAS_CT_PLACEHOLDER.bits
64 // We consider 'freshened' types and constants
65 // to depend on a particular fn.
66 // The freshening process throws away information,
67 // which can make things unsuitable for use in a global
68 // cache. Note that there is no 'fresh lifetime' flag -
69 // freshening replaces all lifetimes with `ReErased`,
70 // which is different from how types/const are freshened.
71 | TypeFlags::HAS_TY_FRESH.bits
72 | TypeFlags::HAS_CT_FRESH.bits
73 | TypeFlags::HAS_FREE_LOCAL_REGIONS.bits;
75 /// Does this have `Projection`?
76 const HAS_TY_PROJECTION = 1 << 10;
77 /// Does this have `Opaque`?
78 const HAS_TY_OPAQUE = 1 << 11;
79 /// Does this have `ConstKind::Unevaluated`?
80 const HAS_CT_PROJECTION = 1 << 12;
82 /// Could this type be normalized further?
83 const HAS_PROJECTION = TypeFlags::HAS_TY_PROJECTION.bits
84 | TypeFlags::HAS_TY_OPAQUE.bits
85 | TypeFlags::HAS_CT_PROJECTION.bits;
87 /// Is an error type/const reachable?
88 const HAS_ERROR = 1 << 13;
90 /// Does this have any region that "appears free" in the type?
91 /// Basically anything but `ReLateBound` and `ReErased`.
92 const HAS_FREE_REGIONS = 1 << 14;
94 /// Does this have any `ReLateBound` regions? Used to check
95 /// if a global bound is safe to evaluate.
96 const HAS_RE_LATE_BOUND = 1 << 15;
98 /// Does this have any `ReErased` regions?
99 const HAS_RE_ERASED = 1 << 16;
101 /// Does this value have parameters/placeholders/inference variables which could be
102 /// replaced later, in a way that would change the results of `impl` specialization?
103 const STILL_FURTHER_SPECIALIZABLE = 1 << 17;
105 /// Does this value have `InferTy::FreshTy/FreshIntTy/FreshFloatTy`?
106 const HAS_TY_FRESH = 1 << 18;
108 /// Does this value have `InferConst::Fresh`?
109 const HAS_CT_FRESH = 1 << 19;
113 rustc_index::newtype_index! {
114 /// A [De Bruijn index][dbi] is a standard means of representing
115 /// regions (and perhaps later types) in a higher-ranked setting. In
116 /// particular, imagine a type like this:
118 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
121 /// | +------------+ 0 | |
123 /// +----------------------------------+ 1 |
125 /// +----------------------------------------------+ 0
127 /// In this type, there are two binders (the outer fn and the inner
128 /// fn). We need to be able to determine, for any given region, which
129 /// fn type it is bound by, the inner or the outer one. There are
130 /// various ways you can do this, but a De Bruijn index is one of the
131 /// more convenient and has some nice properties. The basic idea is to
132 /// count the number of binders, inside out. Some examples should help
133 /// clarify what I mean.
135 /// Let's start with the reference type `&'b isize` that is the first
136 /// argument to the inner function. This region `'b` is assigned a De
137 /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
138 /// fn). The region `'a` that appears in the second argument type (`&'a
139 /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
140 /// second-innermost binder". (These indices are written on the arrows
143 /// What is interesting is that De Bruijn index attached to a particular
144 /// variable will vary depending on where it appears. For example,
145 /// the final type `&'a char` also refers to the region `'a` declared on
146 /// the outermost fn. But this time, this reference is not nested within
147 /// any other binders (i.e., it is not an argument to the inner fn, but
148 /// rather the outer one). Therefore, in this case, it is assigned a
149 /// De Bruijn index of 0, because the innermost binder in that location
152 /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
153 pub struct DebruijnIndex {
154 DEBUG_FORMAT = "DebruijnIndex({})",
160 /// Returns the resulting index when this value is moved into
161 /// `amount` number of new binders. So, e.g., if you had
163 /// for<'a> fn(&'a x)
165 /// and you wanted to change it to
167 /// for<'a> fn(for<'b> fn(&'a x))
169 /// you would need to shift the index for `'a` into a new binder.
171 pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
172 DebruijnIndex::from_u32(self.as_u32() + amount)
175 /// Update this index in place by shifting it "in" through
176 /// `amount` number of binders.
177 pub fn shift_in(&mut self, amount: u32) {
178 *self = self.shifted_in(amount);
181 /// Returns the resulting index when this value is moved out from
182 /// `amount` number of new binders.
184 pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
185 DebruijnIndex::from_u32(self.as_u32() - amount)
188 /// Update in place by shifting out from `amount` binders.
189 pub fn shift_out(&mut self, amount: u32) {
190 *self = self.shifted_out(amount);
193 /// Adjusts any De Bruijn indices so as to make `to_binder` the
194 /// innermost binder. That is, if we have something bound at `to_binder`,
195 /// it will now be bound at INNERMOST. This is an appropriate thing to do
196 /// when moving a region out from inside binders:
199 /// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
200 /// // Binder: D3 D2 D1 ^^
203 /// Here, the region `'a` would have the De Bruijn index D3,
204 /// because it is the bound 3 binders out. However, if we wanted
205 /// to refer to that region `'a` in the second argument (the `_`),
206 /// those two binders would not be in scope. In that case, we
207 /// might invoke `shift_out_to_binder(D3)`. This would adjust the
208 /// De Bruijn index of `'a` to D1 (the innermost binder).
210 /// If we invoke `shift_out_to_binder` and the region is in fact
211 /// bound by one of the binders we are shifting out of, that is an
212 /// error (and should fail an assertion failure).
213 pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
214 self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
218 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
219 #[derive(Encodable, Decodable)]
230 pub fn name_str(&self) -> &'static str {
232 IntTy::Isize => "isize",
237 IntTy::I128 => "i128",
241 pub fn bit_width(&self) -> Option<u64> {
243 IntTy::Isize => return None,
252 pub fn normalize(&self, target_width: u32) -> Self {
254 IntTy::Isize => match target_width {
265 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
266 #[derive(Encodable, Decodable)]
277 pub fn name_str(&self) -> &'static str {
279 UintTy::Usize => "usize",
281 UintTy::U16 => "u16",
282 UintTy::U32 => "u32",
283 UintTy::U64 => "u64",
284 UintTy::U128 => "u128",
288 pub fn bit_width(&self) -> Option<u64> {
290 UintTy::Usize => return None,
299 pub fn normalize(&self, target_width: u32) -> Self {
301 UintTy::Usize => match target_width {
312 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
313 #[derive(Encodable, Decodable)]
320 pub fn name_str(self) -> &'static str {
322 FloatTy::F32 => "f32",
323 FloatTy::F64 => "f64",
327 pub fn bit_width(self) -> u64 {
335 #[derive(Clone, Copy, PartialEq, Eq)]
336 pub enum IntVarValue {
341 #[derive(Clone, Copy, PartialEq, Eq)]
342 pub struct FloatVarValue(pub FloatTy);
344 rustc_index::newtype_index! {
345 /// A **ty**pe **v**ariable **ID**.
347 DEBUG_FORMAT = "_#{}t"
351 /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
352 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
357 /// An **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
358 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
359 pub struct FloatVid {
363 /// A placeholder for a type that hasn't been inferred yet.
365 /// E.g., if we have an empty array (`[]`), then we create a fresh
366 /// type variable for the element type since we won't know until it's
367 /// used what the element type is supposed to be.
368 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
372 /// An integral type variable (`{integer}`).
374 /// These are created when the compiler sees an integer literal like
375 /// `1` that could be several different types (`u8`, `i32`, `u32`, etc.).
376 /// We don't know until it's used what type it's supposed to be, so
377 /// we create a fresh type variable.
379 /// A floating-point type variable (`{float}`).
381 /// These are created when the compiler sees an float literal like
382 /// `1.0` that could be either an `f32` or an `f64`.
383 /// We don't know until it's used what type it's supposed to be, so
384 /// we create a fresh type variable.
387 /// A [`FreshTy`][Self::FreshTy] is one that is generated as a replacement
388 /// for an unbound type variable. This is convenient for caching etc. See
389 /// `rustc_infer::infer::freshen` for more details.
391 /// Compare with [`TyVar`][Self::TyVar].
393 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`IntVar`][Self::IntVar].
395 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`FloatVar`][Self::FloatVar].
399 /// Raw `TyVid` are used as the unification key for `sub_relations`;
400 /// they carry no values.
401 impl UnifyKey for TyVid {
403 fn index(&self) -> u32 {
406 fn from_index(i: u32) -> TyVid {
409 fn tag() -> &'static str {
414 impl EqUnifyValue for IntVarValue {}
416 impl UnifyKey for IntVid {
417 type Value = Option<IntVarValue>;
418 #[inline] // make this function eligible for inlining - it is quite hot.
419 fn index(&self) -> u32 {
422 fn from_index(i: u32) -> IntVid {
425 fn tag() -> &'static str {
430 impl EqUnifyValue for FloatVarValue {}
432 impl UnifyKey for FloatVid {
433 type Value = Option<FloatVarValue>;
434 fn index(&self) -> u32 {
437 fn from_index(i: u32) -> FloatVid {
438 FloatVid { index: i }
440 fn tag() -> &'static str {
445 #[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash)]
447 Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
448 Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
449 Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
450 Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
454 /// `a.xform(b)` combines the variance of a context with the
455 /// variance of a type with the following meaning. If we are in a
456 /// context with variance `a`, and we encounter a type argument in
457 /// a position with variance `b`, then `a.xform(b)` is the new
458 /// variance with which the argument appears.
464 /// Here, the "ambient" variance starts as covariant. `*mut T` is
465 /// invariant with respect to `T`, so the variance in which the
466 /// `Vec<i32>` appears is `Covariant.xform(Invariant)`, which
467 /// yields `Invariant`. Now, the type `Vec<T>` is covariant with
468 /// respect to its type argument `T`, and hence the variance of
469 /// the `i32` here is `Invariant.xform(Covariant)`, which results
470 /// (again) in `Invariant`.
474 /// fn(*const Vec<i32>, *mut Vec<i32)
476 /// The ambient variance is covariant. A `fn` type is
477 /// contravariant with respect to its parameters, so the variance
478 /// within which both pointer types appear is
479 /// `Covariant.xform(Contravariant)`, or `Contravariant`. `*const
480 /// T` is covariant with respect to `T`, so the variance within
481 /// which the first `Vec<i32>` appears is
482 /// `Contravariant.xform(Covariant)` or `Contravariant`. The same
483 /// is true for its `i32` argument. In the `*mut T` case, the
484 /// variance of `Vec<i32>` is `Contravariant.xform(Invariant)`,
485 /// and hence the outermost type is `Invariant` with respect to
486 /// `Vec<i32>` (and its `i32` argument).
488 /// Source: Figure 1 of "Taming the Wildcards:
489 /// Combining Definition- and Use-Site Variance" published in PLDI'11.
490 pub fn xform(self, v: Variance) -> Variance {
492 // Figure 1, column 1.
493 (Variance::Covariant, Variance::Covariant) => Variance::Covariant,
494 (Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
495 (Variance::Covariant, Variance::Invariant) => Variance::Invariant,
496 (Variance::Covariant, Variance::Bivariant) => Variance::Bivariant,
498 // Figure 1, column 2.
499 (Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
500 (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
501 (Variance::Contravariant, Variance::Invariant) => Variance::Invariant,
502 (Variance::Contravariant, Variance::Bivariant) => Variance::Bivariant,
504 // Figure 1, column 3.
505 (Variance::Invariant, _) => Variance::Invariant,
507 // Figure 1, column 4.
508 (Variance::Bivariant, _) => Variance::Bivariant,
513 impl<CTX> HashStable<CTX> for DebruijnIndex {
514 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
515 self.as_u32().hash_stable(ctx, hasher);
519 impl<CTX> HashStable<CTX> for IntTy {
520 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
521 discriminant(self).hash_stable(ctx, hasher);
525 impl<CTX> HashStable<CTX> for UintTy {
526 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
527 discriminant(self).hash_stable(ctx, hasher);
531 impl<CTX> HashStable<CTX> for FloatTy {
532 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
533 discriminant(self).hash_stable(ctx, hasher);
537 impl<CTX> HashStable<CTX> for InferTy {
538 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
540 discriminant(self).hash_stable(ctx, hasher);
542 TyVar(v) => v.as_u32().hash_stable(ctx, hasher),
543 IntVar(v) => v.index.hash_stable(ctx, hasher),
544 FloatVar(v) => v.index.hash_stable(ctx, hasher),
545 FreshTy(v) | FreshIntTy(v) | FreshFloatTy(v) => v.hash_stable(ctx, hasher),
550 impl<CTX> HashStable<CTX> for Variance {
551 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
552 discriminant(self).hash_stable(ctx, hasher);
556 impl fmt::Debug for IntVarValue {
557 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
559 IntVarValue::IntType(ref v) => v.fmt(f),
560 IntVarValue::UintType(ref v) => v.fmt(f),
565 impl fmt::Debug for FloatVarValue {
566 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
571 impl fmt::Debug for IntVid {
572 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
573 write!(f, "_#{}i", self.index)
577 impl fmt::Debug for FloatVid {
578 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
579 write!(f, "_#{}f", self.index)
583 impl fmt::Debug for InferTy {
584 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
587 TyVar(ref v) => v.fmt(f),
588 IntVar(ref v) => v.fmt(f),
589 FloatVar(ref v) => v.fmt(f),
590 FreshTy(v) => write!(f, "FreshTy({:?})", v),
591 FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
592 FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
597 impl fmt::Debug for Variance {
598 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
599 f.write_str(match *self {
600 Variance::Covariant => "+",
601 Variance::Contravariant => "-",
602 Variance::Invariant => "o",
603 Variance::Bivariant => "*",
608 impl fmt::Display for InferTy {
609 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
612 TyVar(_) => write!(f, "_"),
613 IntVar(_) => write!(f, "{}", "{integer}"),
614 FloatVar(_) => write!(f, "{}", "{float}"),
615 FreshTy(v) => write!(f, "FreshTy({})", v),
616 FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
617 FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v),