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 /// The never type `!`.
166 /// A tuple type. For example, `(i32, bool)`.
169 /// A projection or opaque type. Both of these types
170 Alias(AliasKind, I::AliasTy),
172 /// A type parameter; for example, `T` in `fn f<T>(x: T) {}`.
175 /// Bound type variable, used to represent the `'a` in `for<'a> fn(&'a ())`.
177 /// For canonical queries, we replace inference variables with bound variables,
178 /// so e.g. when checking whether `&'_ (): Trait<_>` holds, we canonicalize that to
179 /// `for<'a, T> &'a (): Trait<T>` and then convert the introduced bound variables
180 /// back to inference variables in a new inference context when inside of the query.
182 /// See the `rustc-dev-guide` for more details about
183 /// [higher-ranked trait bounds][1] and [canonical queries][2].
185 /// [1]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
186 /// [2]: https://rustc-dev-guide.rust-lang.org/traits/canonical-queries.html
187 Bound(DebruijnIndex, I::BoundTy),
189 /// A placeholder type, used during higher ranked subtyping to instantiate
191 Placeholder(I::PlaceholderType),
193 /// A type variable used during type checking.
195 /// Similar to placeholders, inference variables also live in a universe to
196 /// correctly deal with higher ranked types. Though unlike placeholders,
197 /// that universe is stored in the `InferCtxt` instead of directly
198 /// inside of the type.
201 /// A placeholder for a type which could not be computed; this is
202 /// propagated to avoid useless error messages.
203 Error(I::ErrorGuaranteed),
206 impl<I: Interner> TyKind<I> {
208 pub fn is_primitive(&self) -> bool {
209 matches!(self, Bool | Char | Int(_) | Uint(_) | Float(_))
213 // This is manually implemented for `TyKind` because `std::mem::discriminant`
214 // returns an opaque value that is `PartialEq` but not `PartialOrd`
216 const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize {
234 Generator(_, _, _) => 16,
235 GeneratorWitness(_) => 17,
241 Placeholder(_) => 23,
247 // This is manually implemented because a derive would require `I: Clone`
248 impl<I: Interner> Clone for TyKind<I> {
249 fn clone(&self) -> Self {
253 Int(i) => Int(i.clone()),
254 Uint(u) => Uint(u.clone()),
255 Float(f) => Float(f.clone()),
256 Adt(d, s) => Adt(d.clone(), s.clone()),
257 Foreign(d) => Foreign(d.clone()),
259 Array(t, c) => Array(t.clone(), c.clone()),
260 Slice(t) => Slice(t.clone()),
261 RawPtr(t) => RawPtr(t.clone()),
262 Ref(r, t, m) => Ref(r.clone(), t.clone(), m.clone()),
263 FnDef(d, s) => FnDef(d.clone(), s.clone()),
264 FnPtr(s) => FnPtr(s.clone()),
265 Dynamic(p, r, repr) => Dynamic(p.clone(), r.clone(), repr.clone()),
266 Closure(d, s) => Closure(d.clone(), s.clone()),
267 Generator(d, s, m) => Generator(d.clone(), s.clone(), m.clone()),
268 GeneratorWitness(g) => GeneratorWitness(g.clone()),
270 Tuple(t) => Tuple(t.clone()),
271 Alias(k, p) => Alias(*k, p.clone()),
272 Param(p) => Param(p.clone()),
273 Bound(d, b) => Bound(d.clone(), b.clone()),
274 Placeholder(p) => Placeholder(p.clone()),
275 Infer(t) => Infer(t.clone()),
276 Error(e) => Error(e.clone()),
281 // This is manually implemented because a derive would require `I: PartialEq`
282 impl<I: Interner> PartialEq for TyKind<I> {
284 fn eq(&self, other: &TyKind<I>) -> bool {
285 tykind_discriminant(self) == tykind_discriminant(other)
286 && match (self, other) {
287 (Int(a_i), Int(b_i)) => a_i == b_i,
288 (Uint(a_u), Uint(b_u)) => a_u == b_u,
289 (Float(a_f), Float(b_f)) => a_f == b_f,
290 (Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s,
291 (Foreign(a_d), Foreign(b_d)) => a_d == b_d,
292 (Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c,
293 (Slice(a_t), Slice(b_t)) => a_t == b_t,
294 (RawPtr(a_t), RawPtr(b_t)) => a_t == b_t,
295 (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,
296 (FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s,
297 (FnPtr(a_s), FnPtr(b_s)) => a_s == b_s,
298 (Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
299 a_p == b_p && a_r == b_r && a_repr == b_repr
301 (Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s,
302 (Generator(a_d, a_s, a_m), Generator(b_d, b_s, b_m)) => {
303 a_d == b_d && a_s == b_s && a_m == b_m
305 (GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g == b_g,
306 (Tuple(a_t), Tuple(b_t)) => a_t == b_t,
307 (Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p,
308 (Param(a_p), Param(b_p)) => a_p == b_p,
309 (Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b,
310 (Placeholder(a_p), Placeholder(b_p)) => a_p == b_p,
311 (Infer(a_t), Infer(b_t)) => a_t == b_t,
312 (Error(a_e), Error(b_e)) => a_e == b_e,
313 (Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true,
317 "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
325 // This is manually implemented because a derive would require `I: Eq`
326 impl<I: Interner> Eq for TyKind<I> {}
328 // This is manually implemented because a derive would require `I: PartialOrd`
329 impl<I: Interner> PartialOrd for TyKind<I> {
331 fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
332 Some(self.cmp(other))
336 // This is manually implemented because a derive would require `I: Ord`
337 impl<I: Interner> Ord for TyKind<I> {
339 fn cmp(&self, other: &TyKind<I>) -> Ordering {
340 tykind_discriminant(self).cmp(&tykind_discriminant(other)).then_with(|| {
341 match (self, other) {
342 (Int(a_i), Int(b_i)) => a_i.cmp(b_i),
343 (Uint(a_u), Uint(b_u)) => a_u.cmp(b_u),
344 (Float(a_f), Float(b_f)) => a_f.cmp(b_f),
345 (Adt(a_d, a_s), Adt(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
346 (Foreign(a_d), Foreign(b_d)) => a_d.cmp(b_d),
347 (Array(a_t, a_c), Array(b_t, b_c)) => a_t.cmp(b_t).then_with(|| a_c.cmp(b_c)),
348 (Slice(a_t), Slice(b_t)) => a_t.cmp(b_t),
349 (RawPtr(a_t), RawPtr(b_t)) => a_t.cmp(b_t),
350 (Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => {
351 a_r.cmp(b_r).then_with(|| a_t.cmp(b_t).then_with(|| a_m.cmp(b_m)))
353 (FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d.cmp(b_d).then_with(|| a_s.cmp(b_s)),
354 (FnPtr(a_s), FnPtr(b_s)) => a_s.cmp(b_s),
355 (Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
356 a_p.cmp(b_p).then_with(|| a_r.cmp(b_r).then_with(|| a_repr.cmp(b_repr)))
358 (Closure(a_p, a_s), Closure(b_p, b_s)) => a_p.cmp(b_p).then_with(|| a_s.cmp(b_s)),
359 (Generator(a_d, a_s, a_m), Generator(b_d, b_s, b_m)) => {
360 a_d.cmp(b_d).then_with(|| a_s.cmp(b_s).then_with(|| a_m.cmp(b_m)))
362 (GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g.cmp(b_g),
363 (Tuple(a_t), Tuple(b_t)) => a_t.cmp(b_t),
364 (Alias(a_i, a_p), Alias(b_i, b_p)) => a_i.cmp(b_i).then_with(|| a_p.cmp(b_p)),
365 (Param(a_p), Param(b_p)) => a_p.cmp(b_p),
366 (Bound(a_d, a_b), Bound(b_d, b_b)) => a_d.cmp(b_d).then_with(|| a_b.cmp(b_b)),
367 (Placeholder(a_p), Placeholder(b_p)) => a_p.cmp(b_p),
368 (Infer(a_t), Infer(b_t)) => a_t.cmp(b_t),
369 (Error(a_e), Error(b_e)) => a_e.cmp(b_e),
370 (Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => Ordering::Equal,
372 debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
380 // This is manually implemented because a derive would require `I: Hash`
381 impl<I: Interner> hash::Hash for TyKind<I> {
382 fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
383 tykind_discriminant(self).hash(state);
385 Int(i) => i.hash(state),
386 Uint(u) => u.hash(state),
387 Float(f) => f.hash(state),
392 Foreign(d) => d.hash(state),
397 Slice(t) => t.hash(state),
398 RawPtr(t) => t.hash(state),
408 FnPtr(s) => s.hash(state),
409 Dynamic(p, r, repr) => {
418 Generator(d, s, m) => {
423 GeneratorWitness(g) => g.hash(state),
424 Tuple(t) => t.hash(state),
429 Param(p) => p.hash(state),
434 Placeholder(p) => p.hash(state),
435 Infer(t) => t.hash(state),
436 Error(e) => e.hash(state),
437 Bool | Char | Str | Never => (),
442 // This is manually implemented because a derive would require `I: Debug`
443 impl<I: Interner> fmt::Debug for TyKind<I> {
444 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
446 Bool => f.write_str("Bool"),
447 Char => f.write_str("Char"),
448 Int(i) => f.debug_tuple_field1_finish("Int", i),
449 Uint(u) => f.debug_tuple_field1_finish("Uint", u),
450 Float(float) => f.debug_tuple_field1_finish("Float", float),
451 Adt(d, s) => f.debug_tuple_field2_finish("Adt", d, s),
452 Foreign(d) => f.debug_tuple_field1_finish("Foreign", d),
453 Str => f.write_str("Str"),
454 Array(t, c) => f.debug_tuple_field2_finish("Array", t, c),
455 Slice(t) => f.debug_tuple_field1_finish("Slice", t),
456 RawPtr(t) => f.debug_tuple_field1_finish("RawPtr", t),
457 Ref(r, t, m) => f.debug_tuple_field3_finish("Ref", r, t, m),
458 FnDef(d, s) => f.debug_tuple_field2_finish("FnDef", d, s),
459 FnPtr(s) => f.debug_tuple_field1_finish("FnPtr", s),
460 Dynamic(p, r, repr) => f.debug_tuple_field3_finish("Dynamic", p, r, repr),
461 Closure(d, s) => f.debug_tuple_field2_finish("Closure", d, s),
462 Generator(d, s, m) => f.debug_tuple_field3_finish("Generator", d, s, m),
463 GeneratorWitness(g) => f.debug_tuple_field1_finish("GeneratorWitness", g),
464 Never => f.write_str("Never"),
465 Tuple(t) => f.debug_tuple_field1_finish("Tuple", t),
466 Alias(i, a) => f.debug_tuple_field2_finish("Alias", i, a),
467 Param(p) => f.debug_tuple_field1_finish("Param", p),
468 Bound(d, b) => f.debug_tuple_field2_finish("Bound", d, b),
469 Placeholder(p) => f.debug_tuple_field1_finish("Placeholder", p),
470 Infer(t) => f.debug_tuple_field1_finish("Infer", t),
471 TyKind::Error(e) => f.debug_tuple_field1_finish("Error", e),
476 // This is manually implemented because a derive would require `I: Encodable`
477 impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I>
479 I::ErrorGuaranteed: Encodable<E>,
480 I::AdtDef: Encodable<E>,
481 I::SubstsRef: Encodable<E>,
482 I::DefId: Encodable<E>,
484 I::Const: Encodable<E>,
485 I::Region: Encodable<E>,
486 I::TypeAndMut: Encodable<E>,
487 I::Mutability: Encodable<E>,
488 I::Movability: Encodable<E>,
489 I::PolyFnSig: Encodable<E>,
490 I::ListBinderExistentialPredicate: Encodable<E>,
491 I::BinderListTy: Encodable<E>,
492 I::ListTy: Encodable<E>,
493 I::AliasTy: Encodable<E>,
494 I::ParamTy: Encodable<E>,
495 I::BoundTy: Encodable<E>,
496 I::PlaceholderType: Encodable<E>,
497 I::InferTy: Encodable<E>,
498 I::PredicateKind: Encodable<E>,
499 I::AllocId: Encodable<E>,
501 fn encode(&self, e: &mut E) {
502 let disc = tykind_discriminant(self);
504 Bool => e.emit_enum_variant(disc, |_| {}),
505 Char => e.emit_enum_variant(disc, |_| {}),
506 Int(i) => e.emit_enum_variant(disc, |e| {
509 Uint(u) => e.emit_enum_variant(disc, |e| {
512 Float(f) => e.emit_enum_variant(disc, |e| {
515 Adt(adt, substs) => e.emit_enum_variant(disc, |e| {
519 Foreign(def_id) => e.emit_enum_variant(disc, |e| {
522 Str => e.emit_enum_variant(disc, |_| {}),
523 Array(t, c) => e.emit_enum_variant(disc, |e| {
527 Slice(t) => e.emit_enum_variant(disc, |e| {
530 RawPtr(tam) => e.emit_enum_variant(disc, |e| {
533 Ref(r, t, m) => e.emit_enum_variant(disc, |e| {
538 FnDef(def_id, substs) => e.emit_enum_variant(disc, |e| {
542 FnPtr(polyfnsig) => e.emit_enum_variant(disc, |e| {
545 Dynamic(l, r, repr) => e.emit_enum_variant(disc, |e| {
550 Closure(def_id, substs) => e.emit_enum_variant(disc, |e| {
554 Generator(def_id, substs, m) => e.emit_enum_variant(disc, |e| {
559 GeneratorWitness(b) => e.emit_enum_variant(disc, |e| {
562 Never => e.emit_enum_variant(disc, |_| {}),
563 Tuple(substs) => e.emit_enum_variant(disc, |e| {
566 Alias(k, p) => e.emit_enum_variant(disc, |e| {
570 Param(p) => e.emit_enum_variant(disc, |e| {
573 Bound(d, b) => e.emit_enum_variant(disc, |e| {
577 Placeholder(p) => e.emit_enum_variant(disc, |e| {
580 Infer(i) => e.emit_enum_variant(disc, |e| {
583 Error(d) => e.emit_enum_variant(disc, |e| {
590 // This is manually implemented because a derive would require `I: Decodable`
591 impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for TyKind<I>
593 I::ErrorGuaranteed: Decodable<D>,
594 I::AdtDef: Decodable<D>,
595 I::SubstsRef: Decodable<D>,
596 I::DefId: Decodable<D>,
598 I::Const: Decodable<D>,
599 I::Region: Decodable<D>,
600 I::TypeAndMut: Decodable<D>,
601 I::Mutability: Decodable<D>,
602 I::Movability: Decodable<D>,
603 I::PolyFnSig: Decodable<D>,
604 I::ListBinderExistentialPredicate: Decodable<D>,
605 I::BinderListTy: Decodable<D>,
606 I::ListTy: Decodable<D>,
607 I::AliasTy: Decodable<D>,
608 I::ParamTy: Decodable<D>,
609 I::AliasTy: Decodable<D>,
610 I::BoundTy: Decodable<D>,
611 I::PlaceholderType: Decodable<D>,
612 I::InferTy: Decodable<D>,
613 I::PredicateKind: Decodable<D>,
614 I::AllocId: Decodable<D>,
616 fn decode(d: &mut D) -> Self {
617 match Decoder::read_usize(d) {
620 2 => Int(Decodable::decode(d)),
621 3 => Uint(Decodable::decode(d)),
622 4 => Float(Decodable::decode(d)),
623 5 => Adt(Decodable::decode(d), Decodable::decode(d)),
624 6 => Foreign(Decodable::decode(d)),
626 8 => Array(Decodable::decode(d), Decodable::decode(d)),
627 9 => Slice(Decodable::decode(d)),
628 10 => RawPtr(Decodable::decode(d)),
629 11 => Ref(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
630 12 => FnDef(Decodable::decode(d), Decodable::decode(d)),
631 13 => FnPtr(Decodable::decode(d)),
632 14 => Dynamic(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
633 15 => Closure(Decodable::decode(d), Decodable::decode(d)),
634 16 => Generator(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
635 17 => GeneratorWitness(Decodable::decode(d)),
637 19 => Tuple(Decodable::decode(d)),
638 20 => Alias(Decodable::decode(d), Decodable::decode(d)),
639 21 => Param(Decodable::decode(d)),
640 22 => Bound(Decodable::decode(d), Decodable::decode(d)),
641 23 => Placeholder(Decodable::decode(d)),
642 24 => Infer(Decodable::decode(d)),
643 25 => Error(Decodable::decode(d)),
647 "invalid enum variant tag while decoding `{}`, expected 0..{}",
655 // This is not a derived impl because a derive would require `I: HashStable`
656 #[allow(rustc::usage_of_ty_tykind)]
657 impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for TyKind<I>
659 I::AdtDef: HashStable<CTX>,
660 I::DefId: HashStable<CTX>,
661 I::SubstsRef: HashStable<CTX>,
662 I::Ty: HashStable<CTX>,
663 I::Const: HashStable<CTX>,
664 I::TypeAndMut: HashStable<CTX>,
665 I::PolyFnSig: HashStable<CTX>,
666 I::ListBinderExistentialPredicate: HashStable<CTX>,
667 I::Region: HashStable<CTX>,
668 I::Movability: HashStable<CTX>,
669 I::Mutability: HashStable<CTX>,
670 I::BinderListTy: HashStable<CTX>,
671 I::ListTy: HashStable<CTX>,
672 I::AliasTy: HashStable<CTX>,
673 I::BoundTy: HashStable<CTX>,
674 I::ParamTy: HashStable<CTX>,
675 I::PlaceholderType: HashStable<CTX>,
676 I::InferTy: HashStable<CTX>,
677 I::ErrorGuaranteed: HashStable<CTX>,
683 __hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
685 std::mem::discriminant(self).hash_stable(__hcx, __hasher);
690 i.hash_stable(__hcx, __hasher);
693 u.hash_stable(__hcx, __hasher);
696 f.hash_stable(__hcx, __hasher);
698 Adt(adt, substs) => {
699 adt.hash_stable(__hcx, __hasher);
700 substs.hash_stable(__hcx, __hasher);
703 def_id.hash_stable(__hcx, __hasher);
707 t.hash_stable(__hcx, __hasher);
708 c.hash_stable(__hcx, __hasher);
711 t.hash_stable(__hcx, __hasher);
714 tam.hash_stable(__hcx, __hasher);
717 r.hash_stable(__hcx, __hasher);
718 t.hash_stable(__hcx, __hasher);
719 m.hash_stable(__hcx, __hasher);
721 FnDef(def_id, substs) => {
722 def_id.hash_stable(__hcx, __hasher);
723 substs.hash_stable(__hcx, __hasher);
725 FnPtr(polyfnsig) => {
726 polyfnsig.hash_stable(__hcx, __hasher);
728 Dynamic(l, r, repr) => {
729 l.hash_stable(__hcx, __hasher);
730 r.hash_stable(__hcx, __hasher);
731 repr.hash_stable(__hcx, __hasher);
733 Closure(def_id, substs) => {
734 def_id.hash_stable(__hcx, __hasher);
735 substs.hash_stable(__hcx, __hasher);
737 Generator(def_id, substs, m) => {
738 def_id.hash_stable(__hcx, __hasher);
739 substs.hash_stable(__hcx, __hasher);
740 m.hash_stable(__hcx, __hasher);
742 GeneratorWitness(b) => {
743 b.hash_stable(__hcx, __hasher);
747 substs.hash_stable(__hcx, __hasher);
750 k.hash_stable(__hcx, __hasher);
751 p.hash_stable(__hcx, __hasher);
754 p.hash_stable(__hcx, __hasher);
757 d.hash_stable(__hcx, __hasher);
758 b.hash_stable(__hcx, __hasher);
761 p.hash_stable(__hcx, __hasher);
764 i.hash_stable(__hcx, __hasher);
767 d.hash_stable(__hcx, __hasher);
773 /// Representation of regions. Note that the NLL checker uses a distinct
774 /// representation of regions. For this reason, it internally replaces all the
775 /// regions with inference variables -- the index of the variable is then used
776 /// to index into internal NLL data structures. See `rustc_const_eval::borrow_check`
777 /// module for more information.
779 /// Note: operations are on the wrapper `Region` type, which is interned,
780 /// rather than this type.
782 /// ## The Region lattice within a given function
784 /// In general, the region lattice looks like
787 /// static ----------+-----...------+ (greatest)
789 /// early-bound and | |
793 /// empty(root) placeholder(U1) |
795 /// | / placeholder(Un)
800 /// empty(Un) -------- (smallest)
803 /// Early-bound/free regions are the named lifetimes in scope from the
804 /// function declaration. They have relationships to one another
805 /// determined based on the declared relationships from the
808 /// Note that inference variables and bound regions are not included
809 /// in this diagram. In the case of inference variables, they should
810 /// be inferred to some other region from the diagram. In the case of
811 /// bound regions, they are excluded because they don't make sense to
812 /// include -- the diagram indicates the relationship between free
815 /// ## Inference variables
817 /// During region inference, we sometimes create inference variables,
818 /// represented as `ReVar`. These will be inferred by the code in
819 /// `infer::lexical_region_resolve` to some free region from the
820 /// lattice above (the minimal region that meets the
823 /// During NLL checking, where regions are defined differently, we
824 /// also use `ReVar` -- in that case, the index is used to index into
825 /// the NLL region checker's data structures. The variable may in fact
826 /// represent either a free region or an inference variable, in that
831 /// These are regions that are stored behind a binder and must be substituted
832 /// with some concrete region before being used. There are two kind of
833 /// bound regions: early-bound, which are bound in an item's `Generics`,
834 /// and are substituted by an `InternalSubsts`, and late-bound, which are part of
835 /// higher-ranked types (e.g., `for<'a> fn(&'a ())`), and are substituted by
836 /// the likes of `liberate_late_bound_regions`. The distinction exists
837 /// because higher-ranked lifetimes aren't supported in all places. See [1][2].
839 /// Unlike `Param`s, bound regions are not supposed to exist "in the wild"
840 /// outside their binder, e.g., in types passed to type inference, and
841 /// should first be substituted (by placeholder regions, free regions,
842 /// or region variables).
844 /// ## Placeholder and Free Regions
846 /// One often wants to work with bound regions without knowing their precise
847 /// identity. For example, when checking a function, the lifetime of a borrow
848 /// can end up being assigned to some region parameter. In these cases,
849 /// it must be ensured that bounds on the region can't be accidentally
850 /// assumed without being checked.
852 /// To do this, we replace the bound regions with placeholder markers,
853 /// which don't satisfy any relation not explicitly provided.
855 /// There are two kinds of placeholder regions in rustc: `ReFree` and
856 /// `RePlaceholder`. When checking an item's body, `ReFree` is supposed
857 /// to be used. These also support explicit bounds: both the internally-stored
858 /// *scope*, which the region is assumed to outlive, as well as other
859 /// relations stored in the `FreeRegionMap`. Note that these relations
860 /// aren't checked when you `make_subregion` (or `eq_types`), only by
861 /// `resolve_regions_and_report_errors`.
863 /// When working with higher-ranked types, some region relations aren't
864 /// yet known, so you can't just call `resolve_regions_and_report_errors`.
865 /// `RePlaceholder` is designed for this purpose. In these contexts,
866 /// there's also the risk that some inference variable laying around will
867 /// get unified with your placeholder region: if you want to check whether
868 /// `for<'a> Foo<'_>: 'a`, and you substitute your bound region `'a`
869 /// with a placeholder region `'%a`, the variable `'_` would just be
870 /// instantiated to the placeholder region `'%a`, which is wrong because
871 /// the inference variable is supposed to satisfy the relation
872 /// *for every value of the placeholder region*. To ensure that doesn't
873 /// happen, you can use `leak_check`. This is more clearly explained
874 /// by the [rustc dev guide].
876 /// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
877 /// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
878 /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
879 pub enum RegionKind<I: Interner> {
880 /// Region bound in a type or fn declaration which will be
881 /// substituted 'early' -- that is, at the same time when type
882 /// parameters are substituted.
883 ReEarlyBound(I::EarlyBoundRegion),
885 /// Region bound in a function scope, which will be substituted when the
886 /// function is called.
887 ReLateBound(DebruijnIndex, I::BoundRegion),
889 /// When checking a function body, the types of all arguments and so forth
890 /// that refer to bound region parameters are modified to refer to free
891 /// region parameters.
892 ReFree(I::FreeRegion),
894 /// Static data that has an "infinite" lifetime. Top in the region lattice.
897 /// A region variable. Should not exist outside of type inference.
900 /// A placeholder region -- basically, the higher-ranked version of `ReFree`.
901 /// Should not exist outside of type inference.
902 RePlaceholder(I::PlaceholderRegion),
904 /// Erased region, used by trait selection, in MIR and during codegen.
908 // This is manually implemented for `RegionKind` because `std::mem::discriminant`
909 // returns an opaque value that is `PartialEq` but not `PartialOrd`
911 const fn regionkind_discriminant<I: Interner>(value: &RegionKind<I>) -> usize {
913 ReEarlyBound(_) => 0,
914 ReLateBound(_, _) => 1,
918 RePlaceholder(_) => 5,
923 // This is manually implemented because a derive would require `I: Copy`
924 impl<I: Interner> Copy for RegionKind<I>
926 I::EarlyBoundRegion: Copy,
927 I::BoundRegion: Copy,
930 I::PlaceholderRegion: Copy,
934 // This is manually implemented because a derive would require `I: Clone`
935 impl<I: Interner> Clone for RegionKind<I> {
936 fn clone(&self) -> Self {
938 ReEarlyBound(r) => ReEarlyBound(r.clone()),
939 ReLateBound(d, r) => ReLateBound(d.clone(), r.clone()),
940 ReFree(r) => ReFree(r.clone()),
941 ReStatic => ReStatic,
942 ReVar(r) => ReVar(r.clone()),
943 RePlaceholder(r) => RePlaceholder(r.clone()),
944 ReErased => ReErased,
949 // This is manually implemented because a derive would require `I: PartialEq`
950 impl<I: Interner> PartialEq for RegionKind<I> {
952 fn eq(&self, other: &RegionKind<I>) -> bool {
953 regionkind_discriminant(self) == regionkind_discriminant(other)
954 && match (self, other) {
955 (ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r == b_r,
956 (ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => a_d == b_d && a_r == b_r,
957 (ReFree(a_r), ReFree(b_r)) => a_r == b_r,
958 (ReStatic, ReStatic) => true,
959 (ReVar(a_r), ReVar(b_r)) => a_r == b_r,
960 (RePlaceholder(a_r), RePlaceholder(b_r)) => a_r == b_r,
961 (ReErased, ReErased) => true,
965 "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
973 // This is manually implemented because a derive would require `I: Eq`
974 impl<I: Interner> Eq for RegionKind<I> {}
976 // This is manually implemented because a derive would require `I: PartialOrd`
977 impl<I: Interner> PartialOrd for RegionKind<I> {
979 fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
980 Some(self.cmp(other))
984 // This is manually implemented because a derive would require `I: Ord`
985 impl<I: Interner> Ord for RegionKind<I> {
987 fn cmp(&self, other: &RegionKind<I>) -> Ordering {
988 regionkind_discriminant(self).cmp(®ionkind_discriminant(other)).then_with(|| {
989 match (self, other) {
990 (ReEarlyBound(a_r), ReEarlyBound(b_r)) => a_r.cmp(b_r),
991 (ReLateBound(a_d, a_r), ReLateBound(b_d, b_r)) => {
992 a_d.cmp(b_d).then_with(|| a_r.cmp(b_r))
994 (ReFree(a_r), ReFree(b_r)) => a_r.cmp(b_r),
995 (ReStatic, ReStatic) => Ordering::Equal,
996 (ReVar(a_r), ReVar(b_r)) => a_r.cmp(b_r),
997 (RePlaceholder(a_r), RePlaceholder(b_r)) => a_r.cmp(b_r),
998 (ReErased, ReErased) => Ordering::Equal,
1000 debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
1008 // This is manually implemented because a derive would require `I: Hash`
1009 impl<I: Interner> hash::Hash for RegionKind<I> {
1010 fn hash<H: hash::Hasher>(&self, state: &mut H) -> () {
1011 regionkind_discriminant(self).hash(state);
1013 ReEarlyBound(r) => r.hash(state),
1014 ReLateBound(d, r) => {
1018 ReFree(r) => r.hash(state),
1020 ReVar(r) => r.hash(state),
1021 RePlaceholder(r) => r.hash(state),
1027 // This is manually implemented because a derive would require `I: Debug`
1028 impl<I: Interner> fmt::Debug for RegionKind<I> {
1029 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1031 ReEarlyBound(data) => write!(f, "ReEarlyBound({:?})", data),
1033 ReLateBound(binder_id, bound_region) => {
1034 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
1037 ReFree(fr) => fr.fmt(f),
1039 ReStatic => f.write_str("ReStatic"),
1041 ReVar(vid) => vid.fmt(f),
1043 RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
1045 ReErased => f.write_str("ReErased"),
1050 // This is manually implemented because a derive would require `I: Encodable`
1051 impl<I: Interner, E: TyEncoder> Encodable<E> for RegionKind<I>
1053 I::EarlyBoundRegion: Encodable<E>,
1054 I::BoundRegion: Encodable<E>,
1055 I::FreeRegion: Encodable<E>,
1056 I::RegionVid: Encodable<E>,
1057 I::PlaceholderRegion: Encodable<E>,
1059 fn encode(&self, e: &mut E) {
1060 let disc = regionkind_discriminant(self);
1062 ReEarlyBound(a) => e.emit_enum_variant(disc, |e| {
1065 ReLateBound(a, b) => e.emit_enum_variant(disc, |e| {
1069 ReFree(a) => e.emit_enum_variant(disc, |e| {
1072 ReStatic => e.emit_enum_variant(disc, |_| {}),
1073 ReVar(a) => e.emit_enum_variant(disc, |e| {
1076 RePlaceholder(a) => e.emit_enum_variant(disc, |e| {
1079 ReErased => e.emit_enum_variant(disc, |_| {}),
1084 // This is manually implemented because a derive would require `I: Decodable`
1085 impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for RegionKind<I>
1087 I::EarlyBoundRegion: Decodable<D>,
1088 I::BoundRegion: Decodable<D>,
1089 I::FreeRegion: Decodable<D>,
1090 I::RegionVid: Decodable<D>,
1091 I::PlaceholderRegion: Decodable<D>,
1093 fn decode(d: &mut D) -> Self {
1094 match Decoder::read_usize(d) {
1095 0 => ReEarlyBound(Decodable::decode(d)),
1096 1 => ReLateBound(Decodable::decode(d), Decodable::decode(d)),
1097 2 => ReFree(Decodable::decode(d)),
1099 4 => ReVar(Decodable::decode(d)),
1100 5 => RePlaceholder(Decodable::decode(d)),
1105 "invalid enum variant tag while decoding `{}`, expected 0..{}",
1113 // This is not a derived impl because a derive would require `I: HashStable`
1114 impl<CTX: HashStableContext, I: Interner> HashStable<CTX> for RegionKind<I>
1116 I::EarlyBoundRegion: HashStable<CTX>,
1117 I::BoundRegion: HashStable<CTX>,
1118 I::FreeRegion: HashStable<CTX>,
1119 I::RegionVid: HashStable<CTX>,
1120 I::PlaceholderRegion: HashStable<CTX>,
1126 hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
1128 std::mem::discriminant(self).hash_stable(hcx, hasher);
1130 ReErased | ReStatic => {
1131 // No variant fields to hash for these ...
1133 ReLateBound(d, r) => {
1134 d.hash_stable(hcx, hasher);
1135 r.hash_stable(hcx, hasher);
1137 ReEarlyBound(r) => {
1138 r.hash_stable(hcx, hasher);
1141 r.hash_stable(hcx, hasher);
1143 RePlaceholder(r) => {
1144 r.hash_stable(hcx, hasher);
1147 panic!("region variables should not be hashed: {self:?}")