1 #![allow(rustc::usage_of_ty_tykind)]
3 use std::cmp::Ordering;
6 use crate::DebruijnIndex;
8 use crate::HashStableContext;
15 use self::RegionKind::*;
18 use rustc_data_structures::stable_hasher::HashStable;
19 use rustc_serialize::{Decodable, Decoder, Encodable};
21 /// Specifies how a trait object is represented.
22 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
23 #[derive(Encodable, Decodable, HashStable_Generic)]
25 /// An unsized `dyn Trait` object
27 /// A sized `dyn* Trait` object
29 /// These objects are represented as a `(data, vtable)` pair where `data` is a ptr-sized value
30 /// (often a pointer to the real object, but not necessarily) and `vtable` is a pointer to
31 /// the vtable for `dyn* Trait`. The representation is essentially the same as `&dyn Trait`
32 /// or similar, but the drop function included in the vtable is responsible for freeing the
33 /// underlying storage if needed. This allows a `dyn*` object to be treated agnostically with
34 /// respect to whether it points to a `Box<T>`, `Rc<T>`, etc.
38 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
39 #[derive(Encodable, Decodable, HashStable_Generic)]
45 /// Defines the kinds of types used by the type system.
47 /// Types written by the user start out as `hir::TyKind` and get
48 /// converted to this representation using `AstConv::ast_ty_to_ty`.
49 #[rustc_diagnostic_item = "IrTyKind"]
50 pub enum TyKind<I: Interner> {
51 /// The primitive boolean type. Written as `bool`.
54 /// The primitive character type; holds a Unicode scalar value
55 /// (a non-surrogate code point). Written as `char`.
58 /// A primitive signed integer type. For example, `i32`.
61 /// A primitive unsigned integer type. For example, `u32`.
64 /// A primitive floating-point type. For example, `f64`.
67 /// Algebraic data types (ADT). For example: structures, enumerations and unions.
69 /// For example, the type `List<i32>` would be represented using the `AdtDef`
70 /// for `struct List<T>` and the substs `[i32]`.
72 /// Note that generic parameters in fields only get lazily substituted
73 /// by using something like `adt_def.all_fields().map(|field| field.ty(tcx, substs))`.
74 Adt(I::AdtDef, I::SubstsRef),
76 /// An unsized FFI type that is opaque to Rust. Written as `extern type T`.
79 /// The pointee of a string slice. Written as `str`.
82 /// An array with the given length. Written as `[T; N]`.
83 Array(I::Ty, I::Const),
85 /// The pointee of an array slice. Written as `[T]`.
88 /// A raw pointer. Written as `*mut T` or `*const T`
89 RawPtr(I::TypeAndMut),
91 /// A reference; a pointer with an associated lifetime. Written as
92 /// `&'a mut T` or `&'a T`.
93 Ref(I::Region, I::Ty, I::Mutability),
95 /// The anonymous type of a function declaration/definition. Each
96 /// function has a unique type.
98 /// For the function `fn foo() -> i32 { 3 }` this type would be
99 /// shown to the user as `fn() -> i32 {foo}`.
101 /// For example the type of `bar` here:
103 /// fn foo() -> i32 { 1 }
104 /// let bar = foo; // bar: fn() -> i32 {foo}
106 FnDef(I::DefId, I::SubstsRef),
108 /// A pointer to a function. Written as `fn() -> i32`.
110 /// Note that both functions and closures start out as either
111 /// [FnDef] or [Closure] which can be then be coerced to this variant.
113 /// For example the type of `bar` here:
116 /// fn foo() -> i32 { 1 }
117 /// let bar: fn() -> i32 = foo;
121 /// A trait object. Written as `dyn for<'b> Trait<'b, Assoc = u32> + Send + 'a`.
122 Dynamic(I::ListBinderExistentialPredicate, I::Region, DynKind),
124 /// The anonymous type of a closure. Used to represent the type of `|a| a`.
126 /// Closure substs contain both the - potentially substituted - generic parameters
127 /// of its parent and some synthetic parameters. See the documentation for
128 /// `ClosureSubsts` for more details.
129 Closure(I::DefId, I::SubstsRef),
131 /// The anonymous type of a generator. Used to represent the type of
134 /// For more info about generator substs, visit the documentation for
135 /// `GeneratorSubsts`.
136 Generator(I::DefId, I::SubstsRef, I::Movability),
138 /// A type representing the types stored inside a generator.
139 /// This should only appear as part of the `GeneratorSubsts`.
141 /// Note that the captured variables for generators are stored separately
142 /// using a tuple in the same way as for closures.
144 /// Unlike upvars, the witness can reference lifetimes from
145 /// inside of the generator itself. To deal with them in
146 /// the type of the generator, we convert them to higher ranked
147 /// lifetimes bound by the witness itself.
149 /// Looking at the following example, the witness for this generator
150 /// may end up as something like `for<'a> [Vec<i32>, &'a Vec<i32>]`:
152 /// ```ignore UNSOLVED (ask @compiler-errors, should this error? can we just swap the yields?)
153 /// #![feature(generators)]
155 /// let x = &vec![3];
161 GeneratorWitness(I::BinderListTy),
163 /// A type representing the types stored inside a generator.
164 /// This should only appear as part of the `GeneratorSubsts`.
166 /// Unlike upvars, the witness can reference lifetimes from
167 /// inside of the generator itself. To deal with them in
168 /// the type of the generator, we convert them to higher ranked
169 /// lifetimes bound by the witness itself.
171 /// This variant is only using when `drop_tracking_mir` is set.
172 /// This contains the `DefId` and the `SubstRef` of the generator.
173 /// The actual witness types are computed on MIR by the `mir_generator_witnesses` query.
175 /// Looking at the following example, the witness for this generator
176 /// may end up as something like `for<'a> [Vec<i32>, &'a Vec<i32>]`:
178 /// ```ignore UNSOLVED (ask @compiler-errors, should this error? can we just swap the yields?)
179 /// #![feature(generators)]
181 /// let x = &vec![3];
187 GeneratorWitnessMIR(I::DefId, I::SubstsRef),
189 /// The never type `!`.
192 /// A tuple type. For example, `(i32, bool)`.
195 /// A projection or opaque type. Both of these types
196 Alias(AliasKind, I::AliasTy),
198 /// A type parameter; for example, `T` in `fn f<T>(x: T) {}`.
201 /// Bound type variable, used to represent the `'a` in `for<'a> fn(&'a ())`.
203 /// For canonical queries, we replace inference variables with bound variables,
204 /// so e.g. when checking whether `&'_ (): Trait<_>` holds, we canonicalize that to
205 /// `for<'a, T> &'a (): Trait<T>` and then convert the introduced bound variables
206 /// back to inference variables in a new inference context when inside of the query.
208 /// See the `rustc-dev-guide` for more details about
209 /// [higher-ranked trait bounds][1] and [canonical queries][2].
211 /// [1]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
212 /// [2]: https://rustc-dev-guide.rust-lang.org/traits/canonical-queries.html
213 Bound(DebruijnIndex, I::BoundTy),
215 /// A placeholder type, used during higher ranked subtyping to instantiate
217 Placeholder(I::PlaceholderType),
219 /// A type variable used during type checking.
221 /// Similar to placeholders, inference variables also live in a universe to
222 /// correctly deal with higher ranked types. Though unlike placeholders,
223 /// that universe is stored in the `InferCtxt` instead of directly
224 /// inside of the type.
227 /// A placeholder for a type which could not be computed; this is
228 /// propagated to avoid useless error messages.
229 Error(I::ErrorGuaranteed),
232 impl<I: Interner> TyKind<I> {
234 pub fn is_primitive(&self) -> bool {
235 matches!(self, Bool | Char | Int(_) | Uint(_) | Float(_))
239 // This is manually implemented for `TyKind` because `std::mem::discriminant`
240 // returns an opaque value that is `PartialEq` but not `PartialOrd`
242 const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize {
260 Generator(_, _, _) => 16,
261 GeneratorWitness(_) => 17,
267 Placeholder(_) => 23,
270 GeneratorWitnessMIR(_, _) => 26,
274 // This is manually implemented because a derive would require `I: Clone`
275 impl<I: Interner> Clone for TyKind<I> {
276 fn clone(&self) -> Self {
282 Float(f) => Float(*f),
283 Adt(d, s) => Adt(d.clone(), s.clone()),
284 Foreign(d) => Foreign(d.clone()),
286 Array(t, c) => Array(t.clone(), c.clone()),
287 Slice(t) => Slice(t.clone()),
288 RawPtr(t) => RawPtr(t.clone()),
289 Ref(r, t, m) => Ref(r.clone(), t.clone(), m.clone()),
290 FnDef(d, s) => FnDef(d.clone(), s.clone()),
291 FnPtr(s) => FnPtr(s.clone()),
292 Dynamic(p, r, repr) => Dynamic(p.clone(), r.clone(), *repr),
293 Closure(d, s) => Closure(d.clone(), s.clone()),
294 Generator(d, s, m) => Generator(d.clone(), s.clone(), m.clone()),
295 GeneratorWitness(g) => GeneratorWitness(g.clone()),
296 GeneratorWitnessMIR(d, s) => GeneratorWitnessMIR(d.clone(), s.clone()),
298 Tuple(t) => Tuple(t.clone()),
299 Alias(k, p) => Alias(*k, p.clone()),
300 Param(p) => Param(p.clone()),
301 Bound(d, b) => Bound(*d, b.clone()),
302 Placeholder(p) => Placeholder(p.clone()),
303 Infer(t) => Infer(t.clone()),
304 Error(e) => Error(e.clone()),
309 // This is manually implemented because a derive would require `I: PartialEq`
310 impl<I: Interner> PartialEq for TyKind<I> {
312 fn eq(&self, other: &TyKind<I>) -> bool {
313 tykind_discriminant(self) == tykind_discriminant(other)
314 && match (self, other) {
315 (Int(a_i), Int(b_i)) => a_i == b_i,
316 (Uint(a_u), Uint(b_u)) => a_u == b_u,
317 (Float(a_f), Float(b_f)) => a_f == b_f,
318 (Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s,
319 (Foreign(a_d), Foreign(b_d)) => a_d == b_d,
320 (Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c,
321 (Slice(a_t), Slice(b_t)) => a_t == b_t,
322 (RawPtr(a_t), RawPtr(b_t)) => a_t == b_t,
323 (Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => a_r == b_r && a_t == b_t && a_m == b_m,
324 (FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s,
325 (FnPtr(a_s), FnPtr(b_s)) => a_s == b_s,
326 (Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
327 a_p == b_p && a_r == b_r && a_repr == b_repr
329 (Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s,
330 (Generator(a_d, a_s, a_m), Generator(b_d, b_s, b_m)) => {
331 a_d == b_d && a_s == b_s && a_m == b_m
333 (GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g == b_g,
335 &GeneratorWitnessMIR(ref a_d, ref a_s),
336 &GeneratorWitnessMIR(ref b_d, ref b_s),
337 ) => a_d == b_d && a_s == b_s,
338 (Tuple(a_t), Tuple(b_t)) => a_t == b_t,
339 (Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p,
340 (Param(a_p), Param(b_p)) => a_p == b_p,
341 (Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b,
342 (Placeholder(a_p), Placeholder(b_p)) => a_p == b_p,
343 (Infer(a_t), Infer(b_t)) => a_t == b_t,
344 (Error(a_e), Error(b_e)) => a_e == b_e,
345 (Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true,
349 "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
357 // This is manually implemented because a derive would require `I: Eq`
358 impl<I: Interner> Eq for TyKind<I> {}
360 // This is manually implemented because a derive would require `I: PartialOrd`
361 impl<I: Interner> PartialOrd for TyKind<I> {
363 fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
364 Some(self.cmp(other))
368 // This is manually implemented because a derive would require `I: Ord`
369 impl<I: Interner> Ord for TyKind<I> {
371 fn cmp(&self, other: &TyKind<I>) -> Ordering {
372 tykind_discriminant(self).cmp(&tykind_discriminant(other)).then_with(|| {
373 match (self, other) {
374 (Int(a_i), Int(b_i)) => a_i.cmp(b_i),
375 (Uint(a_u), Uint(b_u)) => a_u.cmp(b_u),
376 (Float(a_f), Float(b_f)) => a_f.cmp(b_f),
377 (Adt(a_d, a_s), Adt(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
378 (Foreign(a_d), Foreign(b_d)) => a_d.cmp(b_d),
379 (Array(a_t, a_c), Array(b_t, b_c)) => a_t.cmp(b_t).then_with(|| a_c.cmp(b_c)),
380 (Slice(a_t), Slice(b_t)) => a_t.cmp(b_t),
381 (RawPtr(a_t), RawPtr(b_t)) => a_t.cmp(b_t),
382 (Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => {
383 a_r.cmp(b_r).then_with(|| a_t.cmp(b_t).then_with(|| a_m.cmp(b_m)))
385 (FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
386 (FnPtr(a_s), FnPtr(b_s)) => a_s.cmp(b_s),
387 (Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
388 a_p.cmp(b_p).then_with(|| a_r.cmp(b_r).then_with(|| a_repr.cmp(b_repr)))
390 (Closure(a_p, a_s), Closure(b_p, b_s)) => a_p.cmp(b_p).then_with(|| a_s.cmp(b_s)),
391 (Generator(a_d, a_s, a_m), Generator(b_d, b_s, b_m)) => {
392 a_d.cmp(b_d).then_with(|| a_s.cmp(b_s).then_with(|| a_m.cmp(b_m)))
394 (GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g.cmp(b_g),
396 &GeneratorWitnessMIR(ref a_d, ref a_s),
397 &GeneratorWitnessMIR(ref b_d, ref b_s),
398 ) => match Ord::cmp(a_d, b_d) {
399 Ordering::Equal => Ord::cmp(a_s, b_s),
402 (Tuple(a_t), Tuple(b_t)) => a_t.cmp(b_t),
403 (Alias(a_i, a_p), Alias(b_i, b_p)) => a_i.cmp(b_i).then_with(|| a_p.cmp(b_p)),
404 (Param(a_p), Param(b_p)) => a_p.cmp(b_p),
405 (Bound(a_d, a_b), Bound(b_d, b_b)) => a_d.cmp(b_d).then_with(|| a_b.cmp(b_b)),
406 (Placeholder(a_p), Placeholder(b_p)) => a_p.cmp(b_p),
407 (Infer(a_t), Infer(b_t)) => a_t.cmp(b_t),
408 (Error(a_e), Error(b_e)) => a_e.cmp(b_e),
409 (Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => Ordering::Equal,
411 debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
419 // This is manually implemented because a derive would require `I: Hash`
420 impl<I: Interner> hash::Hash for TyKind<I> {
421 fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
422 tykind_discriminant(self).hash(state);
424 Int(i) => i.hash(state),
425 Uint(u) => u.hash(state),
426 Float(f) => f.hash(state),
431 Foreign(d) => d.hash(state),
436 Slice(t) => t.hash(state),
437 RawPtr(t) => t.hash(state),
447 FnPtr(s) => s.hash(state),
448 Dynamic(p, r, repr) => {
457 Generator(d, s, m) => {
462 GeneratorWitness(g) => g.hash(state),
463 GeneratorWitnessMIR(d, s) => {
467 Tuple(t) => t.hash(state),
472 Param(p) => p.hash(state),
477 Placeholder(p) => p.hash(state),
478 Infer(t) => t.hash(state),
479 Error(e) => e.hash(state),
480 Bool | Char | Str | Never => (),
485 // This is manually implemented because a derive would require `I: Debug`
486 impl<I: Interner> fmt::Debug for TyKind<I> {
487 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
489 Bool => f.write_str("Bool"),
490 Char => f.write_str("Char"),
491 Int(i) => f.debug_tuple_field1_finish("Int", i),
492 Uint(u) => f.debug_tuple_field1_finish("Uint", u),
493 Float(float) => f.debug_tuple_field1_finish("Float", float),
494 Adt(d, s) => f.debug_tuple_field2_finish("Adt", d, s),
495 Foreign(d) => f.debug_tuple_field1_finish("Foreign", d),
496 Str => f.write_str("Str"),
497 Array(t, c) => f.debug_tuple_field2_finish("Array", t, c),
498 Slice(t) => f.debug_tuple_field1_finish("Slice", t),
499 RawPtr(t) => f.debug_tuple_field1_finish("RawPtr", t),
500 Ref(r, t, m) => f.debug_tuple_field3_finish("Ref", r, t, m),
501 FnDef(d, s) => f.debug_tuple_field2_finish("FnDef", d, s),
502 FnPtr(s) => f.debug_tuple_field1_finish("FnPtr", s),
503 Dynamic(p, r, repr) => f.debug_tuple_field3_finish("Dynamic", p, r, repr),
504 Closure(d, s) => f.debug_tuple_field2_finish("Closure", d, s),
505 Generator(d, s, m) => f.debug_tuple_field3_finish("Generator", d, s, m),
506 GeneratorWitness(g) => f.debug_tuple_field1_finish("GeneratorWitness", g),
507 GeneratorWitnessMIR(d, s) => f.debug_tuple_field2_finish("GeneratorWitnessMIR", d, s),
508 Never => f.write_str("Never"),
509 Tuple(t) => f.debug_tuple_field1_finish("Tuple", t),
510 Alias(i, a) => f.debug_tuple_field2_finish("Alias", i, a),
511 Param(p) => f.debug_tuple_field1_finish("Param", p),
512 Bound(d, b) => f.debug_tuple_field2_finish("Bound", d, b),
513 Placeholder(p) => f.debug_tuple_field1_finish("Placeholder", p),
514 Infer(t) => f.debug_tuple_field1_finish("Infer", t),
515 TyKind::Error(e) => f.debug_tuple_field1_finish("Error", e),
520 // This is manually implemented because a derive would require `I: Encodable`
521 impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I>
523 I::ErrorGuaranteed: Encodable<E>,
524 I::AdtDef: Encodable<E>,
525 I::SubstsRef: Encodable<E>,
526 I::DefId: Encodable<E>,
528 I::Const: Encodable<E>,
529 I::Region: Encodable<E>,
530 I::TypeAndMut: Encodable<E>,
531 I::Mutability: Encodable<E>,
532 I::Movability: Encodable<E>,
533 I::PolyFnSig: Encodable<E>,
534 I::ListBinderExistentialPredicate: Encodable<E>,
535 I::BinderListTy: Encodable<E>,
536 I::ListTy: Encodable<E>,
537 I::AliasTy: Encodable<E>,
538 I::ParamTy: Encodable<E>,
539 I::BoundTy: Encodable<E>,
540 I::PlaceholderType: Encodable<E>,
541 I::InferTy: Encodable<E>,
542 I::PredicateKind: Encodable<E>,
543 I::AllocId: Encodable<E>,
545 fn encode(&self, e: &mut E) {
546 let disc = tykind_discriminant(self);
548 Bool => e.emit_enum_variant(disc, |_| {}),
549 Char => e.emit_enum_variant(disc, |_| {}),
550 Int(i) => e.emit_enum_variant(disc, |e| {
553 Uint(u) => e.emit_enum_variant(disc, |e| {
556 Float(f) => e.emit_enum_variant(disc, |e| {
559 Adt(adt, substs) => e.emit_enum_variant(disc, |e| {
563 Foreign(def_id) => e.emit_enum_variant(disc, |e| {
566 Str => e.emit_enum_variant(disc, |_| {}),
567 Array(t, c) => e.emit_enum_variant(disc, |e| {
571 Slice(t) => e.emit_enum_variant(disc, |e| {
574 RawPtr(tam) => e.emit_enum_variant(disc, |e| {
577 Ref(r, t, m) => e.emit_enum_variant(disc, |e| {
582 FnDef(def_id, substs) => e.emit_enum_variant(disc, |e| {
586 FnPtr(polyfnsig) => e.emit_enum_variant(disc, |e| {
589 Dynamic(l, r, repr) => e.emit_enum_variant(disc, |e| {
594 Closure(def_id, substs) => e.emit_enum_variant(disc, |e| {
598 Generator(def_id, substs, m) => e.emit_enum_variant(disc, |e| {
603 GeneratorWitness(b) => e.emit_enum_variant(disc, |e| {
606 GeneratorWitnessMIR(def_id, substs) => e.emit_enum_variant(disc, |e| {
610 Never => e.emit_enum_variant(disc, |_| {}),
611 Tuple(substs) => e.emit_enum_variant(disc, |e| {
614 Alias(k, p) => e.emit_enum_variant(disc, |e| {
618 Param(p) => e.emit_enum_variant(disc, |e| {
621 Bound(d, b) => e.emit_enum_variant(disc, |e| {
625 Placeholder(p) => e.emit_enum_variant(disc, |e| {
628 Infer(i) => e.emit_enum_variant(disc, |e| {
631 Error(d) => e.emit_enum_variant(disc, |e| {
638 // This is manually implemented because a derive would require `I: Decodable`
639 impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for TyKind<I>
641 I::ErrorGuaranteed: Decodable<D>,
642 I::AdtDef: Decodable<D>,
643 I::SubstsRef: Decodable<D>,
644 I::DefId: Decodable<D>,
646 I::Const: Decodable<D>,
647 I::Region: Decodable<D>,
648 I::TypeAndMut: Decodable<D>,
649 I::Mutability: Decodable<D>,
650 I::Movability: Decodable<D>,
651 I::PolyFnSig: Decodable<D>,
652 I::ListBinderExistentialPredicate: Decodable<D>,
653 I::BinderListTy: Decodable<D>,
654 I::ListTy: Decodable<D>,
655 I::AliasTy: Decodable<D>,
656 I::ParamTy: Decodable<D>,
657 I::AliasTy: Decodable<D>,
658 I::BoundTy: Decodable<D>,
659 I::PlaceholderType: Decodable<D>,
660 I::InferTy: Decodable<D>,
661 I::PredicateKind: Decodable<D>,
662 I::AllocId: Decodable<D>,
664 fn decode(d: &mut D) -> Self {
665 match Decoder::read_usize(d) {
668 2 => Int(Decodable::decode(d)),
669 3 => Uint(Decodable::decode(d)),
670 4 => Float(Decodable::decode(d)),
671 5 => Adt(Decodable::decode(d), Decodable::decode(d)),
672 6 => Foreign(Decodable::decode(d)),
674 8 => Array(Decodable::decode(d), Decodable::decode(d)),
675 9 => Slice(Decodable::decode(d)),
676 10 => RawPtr(Decodable::decode(d)),
677 11 => Ref(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
678 12 => FnDef(Decodable::decode(d), Decodable::decode(d)),
679 13 => FnPtr(Decodable::decode(d)),
680 14 => Dynamic(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
681 15 => Closure(Decodable::decode(d), Decodable::decode(d)),
682 16 => Generator(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
683 17 => GeneratorWitness(Decodable::decode(d)),
685 19 => Tuple(Decodable::decode(d)),
686 20 => Alias(Decodable::decode(d), Decodable::decode(d)),
687 21 => Param(Decodable::decode(d)),
688 22 => Bound(Decodable::decode(d), Decodable::decode(d)),
689 23 => Placeholder(Decodable::decode(d)),
690 24 => Infer(Decodable::decode(d)),
691 25 => Error(Decodable::decode(d)),
692 26 => GeneratorWitnessMIR(Decodable::decode(d), Decodable::decode(d)),
696 "invalid enum variant tag while decoding `{}`, expected 0..{}",
704 // This is not a derived impl because a derive would require `I: HashStable`
705 #[allow(rustc::usage_of_ty_tykind)]
706 impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for TyKind<I>
708 I::AdtDef: HashStable<CTX>,
709 I::DefId: HashStable<CTX>,
710 I::SubstsRef: HashStable<CTX>,
711 I::Ty: HashStable<CTX>,
712 I::Const: HashStable<CTX>,
713 I::TypeAndMut: HashStable<CTX>,
714 I::PolyFnSig: HashStable<CTX>,
715 I::ListBinderExistentialPredicate: HashStable<CTX>,
716 I::Region: HashStable<CTX>,
717 I::Movability: HashStable<CTX>,
718 I::Mutability: HashStable<CTX>,
719 I::BinderListTy: HashStable<CTX>,
720 I::ListTy: HashStable<CTX>,
721 I::AliasTy: HashStable<CTX>,
722 I::BoundTy: HashStable<CTX>,
723 I::ParamTy: HashStable<CTX>,
724 I::PlaceholderType: HashStable<CTX>,
725 I::InferTy: HashStable<CTX>,
726 I::ErrorGuaranteed: HashStable<CTX>,
732 __hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
734 std::mem::discriminant(self).hash_stable(__hcx, __hasher);
739 i.hash_stable(__hcx, __hasher);
742 u.hash_stable(__hcx, __hasher);
745 f.hash_stable(__hcx, __hasher);
747 Adt(adt, substs) => {
748 adt.hash_stable(__hcx, __hasher);
749 substs.hash_stable(__hcx, __hasher);
752 def_id.hash_stable(__hcx, __hasher);
756 t.hash_stable(__hcx, __hasher);
757 c.hash_stable(__hcx, __hasher);
760 t.hash_stable(__hcx, __hasher);
763 tam.hash_stable(__hcx, __hasher);
766 r.hash_stable(__hcx, __hasher);
767 t.hash_stable(__hcx, __hasher);
768 m.hash_stable(__hcx, __hasher);
770 FnDef(def_id, substs) => {
771 def_id.hash_stable(__hcx, __hasher);
772 substs.hash_stable(__hcx, __hasher);
774 FnPtr(polyfnsig) => {
775 polyfnsig.hash_stable(__hcx, __hasher);
777 Dynamic(l, r, repr) => {
778 l.hash_stable(__hcx, __hasher);
779 r.hash_stable(__hcx, __hasher);
780 repr.hash_stable(__hcx, __hasher);
782 Closure(def_id, substs) => {
783 def_id.hash_stable(__hcx, __hasher);
784 substs.hash_stable(__hcx, __hasher);
786 Generator(def_id, substs, m) => {
787 def_id.hash_stable(__hcx, __hasher);
788 substs.hash_stable(__hcx, __hasher);
789 m.hash_stable(__hcx, __hasher);
791 GeneratorWitness(b) => {
792 b.hash_stable(__hcx, __hasher);
794 GeneratorWitnessMIR(def_id, substs) => {
795 def_id.hash_stable(__hcx, __hasher);
796 substs.hash_stable(__hcx, __hasher);
800 substs.hash_stable(__hcx, __hasher);
803 k.hash_stable(__hcx, __hasher);
804 p.hash_stable(__hcx, __hasher);
807 p.hash_stable(__hcx, __hasher);
810 d.hash_stable(__hcx, __hasher);
811 b.hash_stable(__hcx, __hasher);
814 p.hash_stable(__hcx, __hasher);
817 i.hash_stable(__hcx, __hasher);
820 d.hash_stable(__hcx, __hasher);
826 /// Representation of regions. Note that the NLL checker uses a distinct
827 /// representation of regions. For this reason, it internally replaces all the
828 /// regions with inference variables -- the index of the variable is then used
829 /// to index into internal NLL data structures. See `rustc_const_eval::borrow_check`
830 /// module for more information.
832 /// Note: operations are on the wrapper `Region` type, which is interned,
833 /// rather than this type.
835 /// ## The Region lattice within a given function
837 /// In general, the region lattice looks like
840 /// static ----------+-----...------+ (greatest)
842 /// early-bound and | |
846 /// empty(root) placeholder(U1) |
848 /// | / placeholder(Un)
853 /// empty(Un) -------- (smallest)
856 /// Early-bound/free regions are the named lifetimes in scope from the
857 /// function declaration. They have relationships to one another
858 /// determined based on the declared relationships from the
861 /// Note that inference variables and bound regions are not included
862 /// in this diagram. In the case of inference variables, they should
863 /// be inferred to some other region from the diagram. In the case of
864 /// bound regions, they are excluded because they don't make sense to
865 /// include -- the diagram indicates the relationship between free
868 /// ## Inference variables
870 /// During region inference, we sometimes create inference variables,
871 /// represented as `ReVar`. These will be inferred by the code in
872 /// `infer::lexical_region_resolve` to some free region from the
873 /// lattice above (the minimal region that meets the
876 /// During NLL checking, where regions are defined differently, we
877 /// also use `ReVar` -- in that case, the index is used to index into
878 /// the NLL region checker's data structures. The variable may in fact
879 /// represent either a free region or an inference variable, in that
884 /// These are regions that are stored behind a binder and must be substituted
885 /// with some concrete region before being used. There are two kind of
886 /// bound regions: early-bound, which are bound in an item's `Generics`,
887 /// and are substituted by an `InternalSubsts`, and late-bound, which are part of
888 /// higher-ranked types (e.g., `for<'a> fn(&'a ())`), and are substituted by
889 /// the likes of `liberate_late_bound_regions`. The distinction exists
890 /// because higher-ranked lifetimes aren't supported in all places. See [1][2].
892 /// Unlike `Param`s, bound regions are not supposed to exist "in the wild"
893 /// outside their binder, e.g., in types passed to type inference, and
894 /// should first be substituted (by placeholder regions, free regions,
895 /// or region variables).
897 /// ## Placeholder and Free Regions
899 /// One often wants to work with bound regions without knowing their precise
900 /// identity. For example, when checking a function, the lifetime of a borrow
901 /// can end up being assigned to some region parameter. In these cases,
902 /// it must be ensured that bounds on the region can't be accidentally
903 /// assumed without being checked.
905 /// To do this, we replace the bound regions with placeholder markers,
906 /// which don't satisfy any relation not explicitly provided.
908 /// There are two kinds of placeholder regions in rustc: `ReFree` and
909 /// `RePlaceholder`. When checking an item's body, `ReFree` is supposed
910 /// to be used. These also support explicit bounds: both the internally-stored
911 /// *scope*, which the region is assumed to outlive, as well as other
912 /// relations stored in the `FreeRegionMap`. Note that these relations
913 /// aren't checked when you `make_subregion` (or `eq_types`), only by
914 /// `resolve_regions_and_report_errors`.
916 /// When working with higher-ranked types, some region relations aren't
917 /// yet known, so you can't just call `resolve_regions_and_report_errors`.
918 /// `RePlaceholder` is designed for this purpose. In these contexts,
919 /// there's also the risk that some inference variable laying around will
920 /// get unified with your placeholder region: if you want to check whether
921 /// `for<'a> Foo<'_>: 'a`, and you substitute your bound region `'a`
922 /// with a placeholder region `'%a`, the variable `'_` would just be
923 /// instantiated to the placeholder region `'%a`, which is wrong because
924 /// the inference variable is supposed to satisfy the relation
925 /// *for every value of the placeholder region*. To ensure that doesn't
926 /// happen, you can use `leak_check`. This is more clearly explained
927 /// by the [rustc dev guide].
929 /// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
930 /// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
931 /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
932 pub enum RegionKind<I: Interner> {
933 /// Region bound in a type or fn declaration which will be
934 /// substituted 'early' -- that is, at the same time when type
935 /// parameters are substituted.
936 ReEarlyBound(I::EarlyBoundRegion),
938 /// Region bound in a function scope, which will be substituted when the
939 /// function is called.
940 ReLateBound(DebruijnIndex, I::BoundRegion),
942 /// When checking a function body, the types of all arguments and so forth
943 /// that refer to bound region parameters are modified to refer to free
944 /// region parameters.
945 ReFree(I::FreeRegion),
947 /// Static data that has an "infinite" lifetime. Top in the region lattice.
950 /// A region variable. Should not exist outside of type inference.
953 /// A placeholder region -- basically, the higher-ranked version of `ReFree`.
954 /// Should not exist outside of type inference.
955 RePlaceholder(I::PlaceholderRegion),
957 /// Erased region, used by trait selection, in MIR and during codegen.
961 // This is manually implemented for `RegionKind` because `std::mem::discriminant`
962 // returns an opaque value that is `PartialEq` but not `PartialOrd`
964 const fn regionkind_discriminant<I: Interner>(value: &RegionKind<I>) -> usize {
966 ReEarlyBound(_) => 0,
967 ReLateBound(_, _) => 1,
971 RePlaceholder(_) => 5,
976 // This is manually implemented because a derive would require `I: Copy`
977 impl<I: Interner> Copy for RegionKind<I>
979 I::EarlyBoundRegion: Copy,
980 I::BoundRegion: Copy,
983 I::PlaceholderRegion: Copy,
987 // This is manually implemented because a derive would require `I: Clone`
988 impl<I: Interner> Clone for RegionKind<I> {
989 fn clone(&self) -> Self {
991 ReEarlyBound(r) => ReEarlyBound(r.clone()),
992 ReLateBound(d, r) => ReLateBound(*d, r.clone()),
993 ReFree(r) => ReFree(r.clone()),
994 ReStatic => ReStatic,
995 ReVar(r) => ReVar(r.clone()),
996 RePlaceholder(r) => RePlaceholder(r.clone()),
997 ReErased => ReErased,
1002 // This is manually implemented because a derive would require `I: PartialEq`
1003 impl<I: Interner> PartialEq for RegionKind<I> {
1005 fn eq(&self, other: &RegionKind<I>) -> bool {
1006 regionkind_discriminant(self) == regionkind_discriminant(other)
1007 && match (self, other) {
1008 (ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r == b_r,
1009 (ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => a_d == b_d && a_r == b_r,
1010 (ReFree(a_r), ReFree(b_r)) => a_r == b_r,
1011 (ReStatic, ReStatic) => true,
1012 (ReVar(a_r), ReVar(b_r)) => a_r == b_r,
1013 (RePlaceholder(a_r), RePlaceholder(b_r)) => a_r == b_r,
1014 (ReErased, ReErased) => true,
1018 "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
1026 // This is manually implemented because a derive would require `I: Eq`
1027 impl<I: Interner> Eq for RegionKind<I> {}
1029 // This is manually implemented because a derive would require `I: PartialOrd`
1030 impl<I: Interner> PartialOrd for RegionKind<I> {
1032 fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
1033 Some(self.cmp(other))
1037 // This is manually implemented because a derive would require `I: Ord`
1038 impl<I: Interner> Ord for RegionKind<I> {
1040 fn cmp(&self, other: &RegionKind<I>) -> Ordering {
1041 regionkind_discriminant(self).cmp(®ionkind_discriminant(other)).then_with(|| {
1042 match (self, other) {
1043 (ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r.cmp(b_r),
1044 (ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => {
1045 a_d.cmp(b_d).then_with(|| a_r.cmp(b_r))
1047 (ReFree(a_r), ReFree(b_r)) => a_r.cmp(b_r),
1048 (ReStatic, ReStatic) => Ordering::Equal,
1049 (ReVar(a_r), ReVar(b_r)) => a_r.cmp(b_r),
1050 (RePlaceholder(a_r), RePlaceholder(b_r)) => a_r.cmp(b_r),
1051 (ReErased, ReErased) => Ordering::Equal,
1053 debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
1061 // This is manually implemented because a derive would require `I: Hash`
1062 impl<I: Interner> hash::Hash for RegionKind<I> {
1063 fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {
1064 regionkind_discriminant(self).hash(state);
1066 ReEarlyBound(r) => r.hash(state),
1067 ReLateBound(d, r) => {
1071 ReFree(r) => r.hash(state),
1073 ReVar(r) => r.hash(state),
1074 RePlaceholder(r) => r.hash(state),
1080 // This is manually implemented because a derive would require `I: Debug`
1081 impl<I: Interner> fmt::Debug for RegionKind<I> {
1082 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1084 ReEarlyBound(data) => write!(f, "ReEarlyBound({data:?})"),
1086 ReLateBound(binder_id, bound_region) => {
1087 write!(f, "ReLateBound({binder_id:?}, {bound_region:?})")
1090 ReFree(fr) => fr.fmt(f),
1092 ReStatic => f.write_str("ReStatic"),
1094 ReVar(vid) => vid.fmt(f),
1096 RePlaceholder(placeholder) => write!(f, "RePlaceholder({placeholder:?})"),
1098 ReErased => f.write_str("ReErased"),
1103 // This is manually implemented because a derive would require `I: Encodable`
1104 impl<I: Interner, E: TyEncoder> Encodable<E> for RegionKind<I>
1106 I::EarlyBoundRegion: Encodable<E>,
1107 I::BoundRegion: Encodable<E>,
1108 I::FreeRegion: Encodable<E>,
1109 I::RegionVid: Encodable<E>,
1110 I::PlaceholderRegion: Encodable<E>,
1112 fn encode(&self, e: &mut E) {
1113 let disc = regionkind_discriminant(self);
1115 ReEarlyBound(a) => e.emit_enum_variant(disc, |e| {
1118 ReLateBound(a, b) => e.emit_enum_variant(disc, |e| {
1122 ReFree(a) => e.emit_enum_variant(disc, |e| {
1125 ReStatic => e.emit_enum_variant(disc, |_| {}),
1126 ReVar(a) => e.emit_enum_variant(disc, |e| {
1129 RePlaceholder(a) => e.emit_enum_variant(disc, |e| {
1132 ReErased => e.emit_enum_variant(disc, |_| {}),
1137 // This is manually implemented because a derive would require `I: Decodable`
1138 impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for RegionKind<I>
1140 I::EarlyBoundRegion: Decodable<D>,
1141 I::BoundRegion: Decodable<D>,
1142 I::FreeRegion: Decodable<D>,
1143 I::RegionVid: Decodable<D>,
1144 I::PlaceholderRegion: Decodable<D>,
1146 fn decode(d: &mut D) -> Self {
1147 match Decoder::read_usize(d) {
1148 0 => ReEarlyBound(Decodable::decode(d)),
1149 1 => ReLateBound(Decodable::decode(d), Decodable::decode(d)),
1150 2 => ReFree(Decodable::decode(d)),
1152 4 => ReVar(Decodable::decode(d)),
1153 5 => RePlaceholder(Decodable::decode(d)),
1158 "invalid enum variant tag while decoding `{}`, expected 0..{}",
1166 // This is not a derived impl because a derive would require `I: HashStable`
1167 impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>
1169 I::EarlyBoundRegion: HashStable<CTX>,
1170 I::BoundRegion: HashStable<CTX>,
1171 I::FreeRegion: HashStable<CTX>,
1172 I::RegionVid: HashStable<CTX>,
1173 I::PlaceholderRegion: HashStable<CTX>,
1179 hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
1181 std::mem::discriminant(self).hash_stable(hcx, hasher);
1183 ReErased | ReStatic => {
1184 // No variant fields to hash for these ...
1186 ReLateBound(d, r) => {
1187 d.hash_stable(hcx, hasher);
1188 r.hash_stable(hcx, hasher);
1190 ReEarlyBound(r) => {
1191 r.hash_stable(hcx, hasher);
1194 r.hash_stable(hcx, hasher);
1196 RePlaceholder(r) => {
1197 r.hash_stable(hcx, hasher);
1200 panic!("region variables should not be hashed: {self:?}")