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 // The `evaluate_obligation` query does not return further
65 // obligations. If it evaluates an obligation with an opaque
66 // type, that opaque type may get compared to another type,
67 // constraining it. We would lose this information.
68 // FIXME: differentiate between crate-local opaque types
69 // and opaque types from other crates, as only opaque types
70 // from the local crate can possibly be a local name
71 | TypeFlags::HAS_TY_OPAQUE.bits
72 // We consider 'freshened' types and constants
73 // to depend on a particular fn.
74 // The freshening process throws away information,
75 // which can make things unsuitable for use in a global
76 // cache. Note that there is no 'fresh lifetime' flag -
77 // freshening replaces all lifetimes with `ReErased`,
78 // which is different from how types/const are freshened.
79 | TypeFlags::HAS_TY_FRESH.bits
80 | TypeFlags::HAS_CT_FRESH.bits
81 | TypeFlags::HAS_FREE_LOCAL_REGIONS.bits;
83 /// Does this have `Projection`?
84 const HAS_TY_PROJECTION = 1 << 10;
85 /// Does this have `Opaque`?
86 const HAS_TY_OPAQUE = 1 << 11;
87 /// Does this have `ConstKind::Unevaluated`?
88 const HAS_CT_PROJECTION = 1 << 12;
90 /// Could this type be normalized further?
91 const HAS_PROJECTION = TypeFlags::HAS_TY_PROJECTION.bits
92 | TypeFlags::HAS_TY_OPAQUE.bits
93 | TypeFlags::HAS_CT_PROJECTION.bits;
95 /// Is an error type/const reachable?
96 const HAS_ERROR = 1 << 13;
98 /// Does this have any region that "appears free" in the type?
99 /// Basically anything but `ReLateBound` and `ReErased`.
100 const HAS_FREE_REGIONS = 1 << 14;
102 /// Does this have any `ReLateBound` regions? Used to check
103 /// if a global bound is safe to evaluate.
104 const HAS_RE_LATE_BOUND = 1 << 15;
106 /// Does this have any `ReErased` regions?
107 const HAS_RE_ERASED = 1 << 16;
109 /// Does this value have parameters/placeholders/inference variables which could be
110 /// replaced later, in a way that would change the results of `impl` specialization?
111 const STILL_FURTHER_SPECIALIZABLE = 1 << 17;
113 /// Does this value have `InferTy::FreshTy/FreshIntTy/FreshFloatTy`?
114 const HAS_TY_FRESH = 1 << 18;
116 /// Does this value have `InferConst::Fresh`?
117 const HAS_CT_FRESH = 1 << 19;
121 rustc_index::newtype_index! {
122 /// A [De Bruijn index][dbi] is a standard means of representing
123 /// regions (and perhaps later types) in a higher-ranked setting. In
124 /// particular, imagine a type like this:
125 /// ```ignore (illustrative)
126 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
129 /// // | +------------+ 0 | |
131 /// // +----------------------------------+ 1 |
133 /// // +----------------------------------------------+ 0
135 /// In this type, there are two binders (the outer fn and the inner
136 /// fn). We need to be able to determine, for any given region, which
137 /// fn type it is bound by, the inner or the outer one. There are
138 /// various ways you can do this, but a De Bruijn index is one of the
139 /// more convenient and has some nice properties. The basic idea is to
140 /// count the number of binders, inside out. Some examples should help
141 /// clarify what I mean.
143 /// Let's start with the reference type `&'b isize` that is the first
144 /// argument to the inner function. This region `'b` is assigned a De
145 /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
146 /// fn). The region `'a` that appears in the second argument type (`&'a
147 /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
148 /// second-innermost binder". (These indices are written on the arrows
151 /// What is interesting is that De Bruijn index attached to a particular
152 /// variable will vary depending on where it appears. For example,
153 /// the final type `&'a char` also refers to the region `'a` declared on
154 /// the outermost fn. But this time, this reference is not nested within
155 /// any other binders (i.e., it is not an argument to the inner fn, but
156 /// rather the outer one). Therefore, in this case, it is assigned a
157 /// De Bruijn index of 0, because the innermost binder in that location
160 /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
161 pub struct DebruijnIndex {
162 DEBUG_FORMAT = "DebruijnIndex({})",
168 /// Returns the resulting index when this value is moved into
169 /// `amount` number of new binders. So, e.g., if you had
171 /// for<'a> fn(&'a x)
173 /// and you wanted to change it to
175 /// for<'a> fn(for<'b> fn(&'a x))
177 /// you would need to shift the index for `'a` into a new binder.
179 pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
180 DebruijnIndex::from_u32(self.as_u32() + amount)
183 /// Update this index in place by shifting it "in" through
184 /// `amount` number of binders.
185 pub fn shift_in(&mut self, amount: u32) {
186 *self = self.shifted_in(amount);
189 /// Returns the resulting index when this value is moved out from
190 /// `amount` number of new binders.
192 pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
193 DebruijnIndex::from_u32(self.as_u32() - amount)
196 /// Update in place by shifting out from `amount` binders.
197 pub fn shift_out(&mut self, amount: u32) {
198 *self = self.shifted_out(amount);
201 /// Adjusts any De Bruijn indices so as to make `to_binder` the
202 /// innermost binder. That is, if we have something bound at `to_binder`,
203 /// it will now be bound at INNERMOST. This is an appropriate thing to do
204 /// when moving a region out from inside binders:
206 /// ```ignore (illustrative)
207 /// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
208 /// // Binder: D3 D2 D1 ^^
211 /// Here, the region `'a` would have the De Bruijn index D3,
212 /// because it is the bound 3 binders out. However, if we wanted
213 /// to refer to that region `'a` in the second argument (the `_`),
214 /// those two binders would not be in scope. In that case, we
215 /// might invoke `shift_out_to_binder(D3)`. This would adjust the
216 /// De Bruijn index of `'a` to D1 (the innermost binder).
218 /// If we invoke `shift_out_to_binder` and the region is in fact
219 /// bound by one of the binders we are shifting out of, that is an
220 /// error (and should fail an assertion failure).
221 pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
222 self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
226 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
227 #[derive(Encodable, Decodable)]
238 pub fn name_str(&self) -> &'static str {
240 IntTy::Isize => "isize",
245 IntTy::I128 => "i128",
249 pub fn bit_width(&self) -> Option<u64> {
251 IntTy::Isize => return None,
260 pub fn normalize(&self, target_width: u32) -> Self {
262 IntTy::Isize => match target_width {
273 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
274 #[derive(Encodable, Decodable)]
285 pub fn name_str(&self) -> &'static str {
287 UintTy::Usize => "usize",
289 UintTy::U16 => "u16",
290 UintTy::U32 => "u32",
291 UintTy::U64 => "u64",
292 UintTy::U128 => "u128",
296 pub fn bit_width(&self) -> Option<u64> {
298 UintTy::Usize => return None,
307 pub fn normalize(&self, target_width: u32) -> Self {
309 UintTy::Usize => match target_width {
320 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
321 #[derive(Encodable, Decodable)]
328 pub fn name_str(self) -> &'static str {
330 FloatTy::F32 => "f32",
331 FloatTy::F64 => "f64",
335 pub fn bit_width(self) -> u64 {
343 #[derive(Clone, Copy, PartialEq, Eq)]
344 pub enum IntVarValue {
349 #[derive(Clone, Copy, PartialEq, Eq)]
350 pub struct FloatVarValue(pub FloatTy);
352 rustc_index::newtype_index! {
353 /// A **ty**pe **v**ariable **ID**.
355 DEBUG_FORMAT = "_#{}t"
359 /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
360 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
365 /// An **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
366 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
367 pub struct FloatVid {
371 /// A placeholder for a type that hasn't been inferred yet.
373 /// E.g., if we have an empty array (`[]`), then we create a fresh
374 /// type variable for the element type since we won't know until it's
375 /// used what the element type is supposed to be.
376 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
380 /// An integral type variable (`{integer}`).
382 /// These are created when the compiler sees an integer literal like
383 /// `1` that could be several different types (`u8`, `i32`, `u32`, etc.).
384 /// We don't know until it's used what type it's supposed to be, so
385 /// we create a fresh type variable.
387 /// A floating-point type variable (`{float}`).
389 /// These are created when the compiler sees an float literal like
390 /// `1.0` that could be either an `f32` or an `f64`.
391 /// We don't know until it's used what type it's supposed to be, so
392 /// we create a fresh type variable.
395 /// A [`FreshTy`][Self::FreshTy] is one that is generated as a replacement
396 /// for an unbound type variable. This is convenient for caching etc. See
397 /// `rustc_infer::infer::freshen` for more details.
399 /// Compare with [`TyVar`][Self::TyVar].
401 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`IntVar`][Self::IntVar].
403 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`FloatVar`][Self::FloatVar].
407 /// Raw `TyVid` are used as the unification key for `sub_relations`;
408 /// they carry no values.
409 impl UnifyKey for TyVid {
412 fn index(&self) -> u32 {
416 fn from_index(i: u32) -> TyVid {
419 fn tag() -> &'static str {
424 impl EqUnifyValue for IntVarValue {}
426 impl UnifyKey for IntVid {
427 type Value = Option<IntVarValue>;
428 #[inline] // make this function eligible for inlining - it is quite hot.
429 fn index(&self) -> u32 {
433 fn from_index(i: u32) -> IntVid {
436 fn tag() -> &'static str {
441 impl EqUnifyValue for FloatVarValue {}
443 impl UnifyKey for FloatVid {
444 type Value = Option<FloatVarValue>;
446 fn index(&self) -> u32 {
450 fn from_index(i: u32) -> FloatVid {
451 FloatVid { index: i }
453 fn tag() -> &'static str {
458 #[derive(Copy, Clone, PartialEq, Decodable, Encodable, Hash)]
460 Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
461 Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
462 Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
463 Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
467 /// `a.xform(b)` combines the variance of a context with the
468 /// variance of a type with the following meaning. If we are in a
469 /// context with variance `a`, and we encounter a type argument in
470 /// a position with variance `b`, then `a.xform(b)` is the new
471 /// variance with which the argument appears.
474 /// ```ignore (illustrative)
477 /// Here, the "ambient" variance starts as covariant. `*mut T` is
478 /// invariant with respect to `T`, so the variance in which the
479 /// `Vec<i32>` appears is `Covariant.xform(Invariant)`, which
480 /// yields `Invariant`. Now, the type `Vec<T>` is covariant with
481 /// respect to its type argument `T`, and hence the variance of
482 /// the `i32` here is `Invariant.xform(Covariant)`, which results
483 /// (again) in `Invariant`.
486 /// ```ignore (illustrative)
487 /// fn(*const Vec<i32>, *mut Vec<i32)
489 /// The ambient variance is covariant. A `fn` type is
490 /// contravariant with respect to its parameters, so the variance
491 /// within which both pointer types appear is
492 /// `Covariant.xform(Contravariant)`, or `Contravariant`. `*const
493 /// T` is covariant with respect to `T`, so the variance within
494 /// which the first `Vec<i32>` appears is
495 /// `Contravariant.xform(Covariant)` or `Contravariant`. The same
496 /// is true for its `i32` argument. In the `*mut T` case, the
497 /// variance of `Vec<i32>` is `Contravariant.xform(Invariant)`,
498 /// and hence the outermost type is `Invariant` with respect to
499 /// `Vec<i32>` (and its `i32` argument).
501 /// Source: Figure 1 of "Taming the Wildcards:
502 /// Combining Definition- and Use-Site Variance" published in PLDI'11.
503 pub fn xform(self, v: Variance) -> Variance {
505 // Figure 1, column 1.
506 (Variance::Covariant, Variance::Covariant) => Variance::Covariant,
507 (Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
508 (Variance::Covariant, Variance::Invariant) => Variance::Invariant,
509 (Variance::Covariant, Variance::Bivariant) => Variance::Bivariant,
511 // Figure 1, column 2.
512 (Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
513 (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
514 (Variance::Contravariant, Variance::Invariant) => Variance::Invariant,
515 (Variance::Contravariant, Variance::Bivariant) => Variance::Bivariant,
517 // Figure 1, column 3.
518 (Variance::Invariant, _) => Variance::Invariant,
520 // Figure 1, column 4.
521 (Variance::Bivariant, _) => Variance::Bivariant,
526 impl<CTX> HashStable<CTX> for DebruijnIndex {
527 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
528 self.as_u32().hash_stable(ctx, hasher);
532 impl<CTX> HashStable<CTX> for IntTy {
533 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
534 discriminant(self).hash_stable(ctx, hasher);
538 impl<CTX> HashStable<CTX> for UintTy {
539 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
540 discriminant(self).hash_stable(ctx, hasher);
544 impl<CTX> HashStable<CTX> for FloatTy {
545 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
546 discriminant(self).hash_stable(ctx, hasher);
550 impl<CTX> HashStable<CTX> for InferTy {
551 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
553 discriminant(self).hash_stable(ctx, hasher);
555 TyVar(v) => v.as_u32().hash_stable(ctx, hasher),
556 IntVar(v) => v.index.hash_stable(ctx, hasher),
557 FloatVar(v) => v.index.hash_stable(ctx, hasher),
558 FreshTy(v) | FreshIntTy(v) | FreshFloatTy(v) => v.hash_stable(ctx, hasher),
563 impl<CTX> HashStable<CTX> for Variance {
564 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
565 discriminant(self).hash_stable(ctx, hasher);
569 impl fmt::Debug for IntVarValue {
570 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
572 IntVarValue::IntType(ref v) => v.fmt(f),
573 IntVarValue::UintType(ref v) => v.fmt(f),
578 impl fmt::Debug for FloatVarValue {
579 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
584 impl fmt::Debug for IntVid {
585 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
586 write!(f, "_#{}i", self.index)
590 impl fmt::Debug for FloatVid {
591 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
592 write!(f, "_#{}f", self.index)
596 impl fmt::Debug for InferTy {
597 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
600 TyVar(ref v) => v.fmt(f),
601 IntVar(ref v) => v.fmt(f),
602 FloatVar(ref v) => v.fmt(f),
603 FreshTy(v) => write!(f, "FreshTy({:?})", v),
604 FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v),
605 FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v),
610 impl fmt::Debug for Variance {
611 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
612 f.write_str(match *self {
613 Variance::Covariant => "+",
614 Variance::Contravariant => "-",
615 Variance::Invariant => "o",
616 Variance::Bivariant => "*",
621 impl fmt::Display for InferTy {
622 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
625 TyVar(_) => write!(f, "_"),
626 IntVar(_) => write!(f, "{}", "{integer}"),
627 FloatVar(_) => write!(f, "{}", "{float}"),
628 FreshTy(v) => write!(f, "FreshTy({})", v),
629 FreshIntTy(v) => write!(f, "FreshIntTy({})", v),
630 FreshFloatTy(v) => write!(f, "FreshFloatTy({})", v),