]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_type_ir/src/sty.rs
Auto merge of #107768 - matthiaskrgr:rollup-9u4cal4, r=matthiaskrgr
[rust.git] / compiler / rustc_type_ir / src / sty.rs
1 #![allow(rustc::usage_of_ty_tykind)]
2
3 use std::cmp::Ordering;
4 use std::{fmt, hash};
5
6 use crate::DebruijnIndex;
7 use crate::FloatTy;
8 use crate::HashStableContext;
9 use crate::IntTy;
10 use crate::Interner;
11 use crate::TyDecoder;
12 use crate::TyEncoder;
13 use crate::UintTy;
14
15 use self::RegionKind::*;
16 use self::TyKind::*;
17
18 use rustc_data_structures::stable_hasher::HashStable;
19 use rustc_serialize::{Decodable, Decoder, Encodable};
20
21 /// Specifies how a trait object is represented.
22 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
23 #[derive(Encodable, Decodable, HashStable_Generic)]
24 pub enum DynKind {
25     /// An unsized `dyn Trait` object
26     Dyn,
27     /// A sized `dyn* Trait` object
28     ///
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.
35     DynStar,
36 }
37
38 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
39 #[derive(Encodable, Decodable, HashStable_Generic)]
40 pub enum AliasKind {
41     Projection,
42     Opaque,
43 }
44
45 /// Defines the kinds of types used by the type system.
46 ///
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`.
52     Bool,
53
54     /// The primitive character type; holds a Unicode scalar value
55     /// (a non-surrogate code point). Written as `char`.
56     Char,
57
58     /// A primitive signed integer type. For example, `i32`.
59     Int(IntTy),
60
61     /// A primitive unsigned integer type. For example, `u32`.
62     Uint(UintTy),
63
64     /// A primitive floating-point type. For example, `f64`.
65     Float(FloatTy),
66
67     /// Algebraic data types (ADT). For example: structures, enumerations and unions.
68     ///
69     /// For example, the type `List<i32>` would be represented using the `AdtDef`
70     /// for `struct List<T>` and the substs `[i32]`.
71     ///
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),
75
76     /// An unsized FFI type that is opaque to Rust. Written as `extern type T`.
77     Foreign(I::DefId),
78
79     /// The pointee of a string slice. Written as `str`.
80     Str,
81
82     /// An array with the given length. Written as `[T; N]`.
83     Array(I::Ty, I::Const),
84
85     /// The pointee of an array slice. Written as `[T]`.
86     Slice(I::Ty),
87
88     /// A raw pointer. Written as `*mut T` or `*const T`
89     RawPtr(I::TypeAndMut),
90
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),
94
95     /// The anonymous type of a function declaration/definition. Each
96     /// function has a unique type.
97     ///
98     /// For the function `fn foo() -> i32 { 3 }` this type would be
99     /// shown to the user as `fn() -> i32 {foo}`.
100     ///
101     /// For example the type of `bar` here:
102     /// ```rust
103     /// fn foo() -> i32 { 1 }
104     /// let bar = foo; // bar: fn() -> i32 {foo}
105     /// ```
106     FnDef(I::DefId, I::SubstsRef),
107
108     /// A pointer to a function. Written as `fn() -> i32`.
109     ///
110     /// Note that both functions and closures start out as either
111     /// [FnDef] or [Closure] which can be then be coerced to this variant.
112     ///
113     /// For example the type of `bar` here:
114     ///
115     /// ```rust
116     /// fn foo() -> i32 { 1 }
117     /// let bar: fn() -> i32 = foo;
118     /// ```
119     FnPtr(I::PolyFnSig),
120
121     /// A trait object. Written as `dyn for<'b> Trait<'b, Assoc = u32> + Send + 'a`.
122     Dynamic(I::ListBinderExistentialPredicate, I::Region, DynKind),
123
124     /// The anonymous type of a closure. Used to represent the type of `|a| a`.
125     ///
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),
130
131     /// The anonymous type of a generator. Used to represent the type of
132     /// `|a| yield a`.
133     ///
134     /// For more info about generator substs, visit the documentation for
135     /// `GeneratorSubsts`.
136     Generator(I::DefId, I::SubstsRef, I::Movability),
137
138     /// A type representing the types stored inside a generator.
139     /// This should only appear as part of the `GeneratorSubsts`.
140     ///
141     /// Note that the captured variables for generators are stored separately
142     /// using a tuple in the same way as for closures.
143     ///
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.
148     ///
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>]`:
151     ///
152     /// ```ignore UNSOLVED (ask @compiler-errors, should this error? can we just swap the yields?)
153     /// #![feature(generators)]
154     /// |a| {
155     ///     let x = &vec![3];
156     ///     yield a;
157     ///     yield x[0];
158     /// }
159     /// # ;
160     /// ```
161     GeneratorWitness(I::BinderListTy),
162
163     /// A type representing the types stored inside a generator.
164     /// This should only appear as part of the `GeneratorSubsts`.
165     ///
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.
170     ///
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.
174     ///
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>]`:
177     ///
178     /// ```ignore UNSOLVED (ask @compiler-errors, should this error? can we just swap the yields?)
179     /// #![feature(generators)]
180     /// |a| {
181     ///     let x = &vec![3];
182     ///     yield a;
183     ///     yield x[0];
184     /// }
185     /// # ;
186     /// ```
187     GeneratorWitnessMIR(I::DefId, I::SubstsRef),
188
189     /// The never type `!`.
190     Never,
191
192     /// A tuple type. For example, `(i32, bool)`.
193     Tuple(I::ListTy),
194
195     /// A projection or opaque type. Both of these types
196     Alias(AliasKind, I::AliasTy),
197
198     /// A type parameter; for example, `T` in `fn f<T>(x: T) {}`.
199     Param(I::ParamTy),
200
201     /// Bound type variable, used to represent the `'a` in `for<'a> fn(&'a ())`.
202     ///
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.
207     ///
208     /// See the `rustc-dev-guide` for more details about
209     /// [higher-ranked trait bounds][1] and [canonical queries][2].
210     ///
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),
214
215     /// A placeholder type, used during higher ranked subtyping to instantiate
216     /// bound variables.
217     Placeholder(I::PlaceholderType),
218
219     /// A type variable used during type checking.
220     ///
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.
225     Infer(I::InferTy),
226
227     /// A placeholder for a type which could not be computed; this is
228     /// propagated to avoid useless error messages.
229     Error(I::ErrorGuaranteed),
230 }
231
232 impl<I: Interner> TyKind<I> {
233     #[inline]
234     pub fn is_primitive(&self) -> bool {
235         matches!(self, Bool | Char | Int(_) | Uint(_) | Float(_))
236     }
237 }
238
239 // This is manually implemented for `TyKind` because `std::mem::discriminant`
240 // returns an opaque value that is `PartialEq` but not `PartialOrd`
241 #[inline]
242 const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize {
243     match value {
244         Bool => 0,
245         Char => 1,
246         Int(_) => 2,
247         Uint(_) => 3,
248         Float(_) => 4,
249         Adt(_, _) => 5,
250         Foreign(_) => 6,
251         Str => 7,
252         Array(_, _) => 8,
253         Slice(_) => 9,
254         RawPtr(_) => 10,
255         Ref(_, _, _) => 11,
256         FnDef(_, _) => 12,
257         FnPtr(_) => 13,
258         Dynamic(..) => 14,
259         Closure(_, _) => 15,
260         Generator(_, _, _) => 16,
261         GeneratorWitness(_) => 17,
262         Never => 18,
263         Tuple(_) => 19,
264         Alias(_, _) => 20,
265         Param(_) => 21,
266         Bound(_, _) => 22,
267         Placeholder(_) => 23,
268         Infer(_) => 24,
269         Error(_) => 25,
270         GeneratorWitnessMIR(_, _) => 26,
271     }
272 }
273
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 {
277         match self {
278             Bool => Bool,
279             Char => Char,
280             Int(i) => Int(*i),
281             Uint(u) => Uint(*u),
282             Float(f) => Float(*f),
283             Adt(d, s) => Adt(d.clone(), s.clone()),
284             Foreign(d) => Foreign(d.clone()),
285             Str => Str,
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()),
297             Never => Never,
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()),
305         }
306     }
307 }
308
309 // This is manually implemented because a derive would require `I: PartialEq`
310 impl<I: Interner> PartialEq for TyKind<I> {
311     #[inline]
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
328                 }
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
332                 }
333                 (GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g == b_g,
334                 (
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,
346                 _ => {
347                     debug_assert!(
348                         false,
349                         "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
350                     );
351                     true
352                 }
353             }
354     }
355 }
356
357 // This is manually implemented because a derive would require `I: Eq`
358 impl<I: Interner> Eq for TyKind<I> {}
359
360 // This is manually implemented because a derive would require `I: PartialOrd`
361 impl<I: Interner> PartialOrd for TyKind<I> {
362     #[inline]
363     fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
364         Some(self.cmp(other))
365     }
366 }
367
368 // This is manually implemented because a derive would require `I: Ord`
369 impl<I: Interner> Ord for TyKind<I> {
370     #[inline]
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)))
384                 }
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)))
389                 }
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)))
393                 }
394                 (GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g.cmp(b_g),
395                 (
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),
400                     cmp => cmp,
401                 },
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,
410                 _ => {
411                     debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
412                     Ordering::Equal
413                 }
414             }
415         })
416     }
417 }
418
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);
423         match self {
424             Int(i) => i.hash(state),
425             Uint(u) => u.hash(state),
426             Float(f) => f.hash(state),
427             Adt(d, s) => {
428                 d.hash(state);
429                 s.hash(state)
430             }
431             Foreign(d) => d.hash(state),
432             Array(t, c) => {
433                 t.hash(state);
434                 c.hash(state)
435             }
436             Slice(t) => t.hash(state),
437             RawPtr(t) => t.hash(state),
438             Ref(r, t, m) => {
439                 r.hash(state);
440                 t.hash(state);
441                 m.hash(state)
442             }
443             FnDef(d, s) => {
444                 d.hash(state);
445                 s.hash(state)
446             }
447             FnPtr(s) => s.hash(state),
448             Dynamic(p, r, repr) => {
449                 p.hash(state);
450                 r.hash(state);
451                 repr.hash(state)
452             }
453             Closure(d, s) => {
454                 d.hash(state);
455                 s.hash(state)
456             }
457             Generator(d, s, m) => {
458                 d.hash(state);
459                 s.hash(state);
460                 m.hash(state)
461             }
462             GeneratorWitness(g) => g.hash(state),
463             GeneratorWitnessMIR(d, s) => {
464                 d.hash(state);
465                 s.hash(state);
466             }
467             Tuple(t) => t.hash(state),
468             Alias(i, p) => {
469                 i.hash(state);
470                 p.hash(state);
471             }
472             Param(p) => p.hash(state),
473             Bound(d, b) => {
474                 d.hash(state);
475                 b.hash(state)
476             }
477             Placeholder(p) => p.hash(state),
478             Infer(t) => t.hash(state),
479             Error(e) => e.hash(state),
480             Bool | Char | Str | Never => (),
481         }
482     }
483 }
484
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 {
488         match self {
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),
516         }
517     }
518 }
519
520 // This is manually implemented because a derive would require `I: Encodable`
521 impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I>
522 where
523     I::ErrorGuaranteed: Encodable<E>,
524     I::AdtDef: Encodable<E>,
525     I::SubstsRef: Encodable<E>,
526     I::DefId: Encodable<E>,
527     I::Ty: 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>,
544 {
545     fn encode(&self, e: &mut E) {
546         let disc = tykind_discriminant(self);
547         match self {
548             Bool => e.emit_enum_variant(disc, |_| {}),
549             Char => e.emit_enum_variant(disc, |_| {}),
550             Int(i) => e.emit_enum_variant(disc, |e| {
551                 i.encode(e);
552             }),
553             Uint(u) => e.emit_enum_variant(disc, |e| {
554                 u.encode(e);
555             }),
556             Float(f) => e.emit_enum_variant(disc, |e| {
557                 f.encode(e);
558             }),
559             Adt(adt, substs) => e.emit_enum_variant(disc, |e| {
560                 adt.encode(e);
561                 substs.encode(e);
562             }),
563             Foreign(def_id) => e.emit_enum_variant(disc, |e| {
564                 def_id.encode(e);
565             }),
566             Str => e.emit_enum_variant(disc, |_| {}),
567             Array(t, c) => e.emit_enum_variant(disc, |e| {
568                 t.encode(e);
569                 c.encode(e);
570             }),
571             Slice(t) => e.emit_enum_variant(disc, |e| {
572                 t.encode(e);
573             }),
574             RawPtr(tam) => e.emit_enum_variant(disc, |e| {
575                 tam.encode(e);
576             }),
577             Ref(r, t, m) => e.emit_enum_variant(disc, |e| {
578                 r.encode(e);
579                 t.encode(e);
580                 m.encode(e);
581             }),
582             FnDef(def_id, substs) => e.emit_enum_variant(disc, |e| {
583                 def_id.encode(e);
584                 substs.encode(e);
585             }),
586             FnPtr(polyfnsig) => e.emit_enum_variant(disc, |e| {
587                 polyfnsig.encode(e);
588             }),
589             Dynamic(l, r, repr) => e.emit_enum_variant(disc, |e| {
590                 l.encode(e);
591                 r.encode(e);
592                 repr.encode(e);
593             }),
594             Closure(def_id, substs) => e.emit_enum_variant(disc, |e| {
595                 def_id.encode(e);
596                 substs.encode(e);
597             }),
598             Generator(def_id, substs, m) => e.emit_enum_variant(disc, |e| {
599                 def_id.encode(e);
600                 substs.encode(e);
601                 m.encode(e);
602             }),
603             GeneratorWitness(b) => e.emit_enum_variant(disc, |e| {
604                 b.encode(e);
605             }),
606             GeneratorWitnessMIR(def_id, substs) => e.emit_enum_variant(disc, |e| {
607                 def_id.encode(e);
608                 substs.encode(e);
609             }),
610             Never => e.emit_enum_variant(disc, |_| {}),
611             Tuple(substs) => e.emit_enum_variant(disc, |e| {
612                 substs.encode(e);
613             }),
614             Alias(k, p) => e.emit_enum_variant(disc, |e| {
615                 k.encode(e);
616                 p.encode(e);
617             }),
618             Param(p) => e.emit_enum_variant(disc, |e| {
619                 p.encode(e);
620             }),
621             Bound(d, b) => e.emit_enum_variant(disc, |e| {
622                 d.encode(e);
623                 b.encode(e);
624             }),
625             Placeholder(p) => e.emit_enum_variant(disc, |e| {
626                 p.encode(e);
627             }),
628             Infer(i) => e.emit_enum_variant(disc, |e| {
629                 i.encode(e);
630             }),
631             Error(d) => e.emit_enum_variant(disc, |e| {
632                 d.encode(e);
633             }),
634         }
635     }
636 }
637
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>
640 where
641     I::ErrorGuaranteed: Decodable<D>,
642     I::AdtDef: Decodable<D>,
643     I::SubstsRef: Decodable<D>,
644     I::DefId: Decodable<D>,
645     I::Ty: 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>,
663 {
664     fn decode(d: &mut D) -> Self {
665         match Decoder::read_usize(d) {
666             0 => Bool,
667             1 => Char,
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)),
673             7 => Str,
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)),
684             18 => Never,
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)),
693             _ => panic!(
694                 "{}",
695                 format!(
696                     "invalid enum variant tag while decoding `{}`, expected 0..{}",
697                     "TyKind", 27,
698                 )
699             ),
700         }
701     }
702 }
703
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>
707 where
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>,
727 {
728     #[inline]
729     fn hash_stable(
730         &self,
731         __hcx: &mut CTX,
732         __hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
733     ) {
734         std::mem::discriminant(self).hash_stable(__hcx, __hasher);
735         match self {
736             Bool => {}
737             Char => {}
738             Int(i) => {
739                 i.hash_stable(__hcx, __hasher);
740             }
741             Uint(u) => {
742                 u.hash_stable(__hcx, __hasher);
743             }
744             Float(f) => {
745                 f.hash_stable(__hcx, __hasher);
746             }
747             Adt(adt, substs) => {
748                 adt.hash_stable(__hcx, __hasher);
749                 substs.hash_stable(__hcx, __hasher);
750             }
751             Foreign(def_id) => {
752                 def_id.hash_stable(__hcx, __hasher);
753             }
754             Str => {}
755             Array(t, c) => {
756                 t.hash_stable(__hcx, __hasher);
757                 c.hash_stable(__hcx, __hasher);
758             }
759             Slice(t) => {
760                 t.hash_stable(__hcx, __hasher);
761             }
762             RawPtr(tam) => {
763                 tam.hash_stable(__hcx, __hasher);
764             }
765             Ref(r, t, m) => {
766                 r.hash_stable(__hcx, __hasher);
767                 t.hash_stable(__hcx, __hasher);
768                 m.hash_stable(__hcx, __hasher);
769             }
770             FnDef(def_id, substs) => {
771                 def_id.hash_stable(__hcx, __hasher);
772                 substs.hash_stable(__hcx, __hasher);
773             }
774             FnPtr(polyfnsig) => {
775                 polyfnsig.hash_stable(__hcx, __hasher);
776             }
777             Dynamic(l, r, repr) => {
778                 l.hash_stable(__hcx, __hasher);
779                 r.hash_stable(__hcx, __hasher);
780                 repr.hash_stable(__hcx, __hasher);
781             }
782             Closure(def_id, substs) => {
783                 def_id.hash_stable(__hcx, __hasher);
784                 substs.hash_stable(__hcx, __hasher);
785             }
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);
790             }
791             GeneratorWitness(b) => {
792                 b.hash_stable(__hcx, __hasher);
793             }
794             GeneratorWitnessMIR(def_id, substs) => {
795                 def_id.hash_stable(__hcx, __hasher);
796                 substs.hash_stable(__hcx, __hasher);
797             }
798             Never => {}
799             Tuple(substs) => {
800                 substs.hash_stable(__hcx, __hasher);
801             }
802             Alias(k, p) => {
803                 k.hash_stable(__hcx, __hasher);
804                 p.hash_stable(__hcx, __hasher);
805             }
806             Param(p) => {
807                 p.hash_stable(__hcx, __hasher);
808             }
809             Bound(d, b) => {
810                 d.hash_stable(__hcx, __hasher);
811                 b.hash_stable(__hcx, __hasher);
812             }
813             Placeholder(p) => {
814                 p.hash_stable(__hcx, __hasher);
815             }
816             Infer(i) => {
817                 i.hash_stable(__hcx, __hasher);
818             }
819             Error(d) => {
820                 d.hash_stable(__hcx, __hasher);
821             }
822         }
823     }
824 }
825
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.
831 ///
832 /// Note: operations are on the wrapper `Region` type, which is interned,
833 /// rather than this type.
834 ///
835 /// ## The Region lattice within a given function
836 ///
837 /// In general, the region lattice looks like
838 ///
839 /// ```text
840 /// static ----------+-----...------+       (greatest)
841 /// |                |              |
842 /// early-bound and  |              |
843 /// free regions     |              |
844 /// |                |              |
845 /// |                |              |
846 /// empty(root)   placeholder(U1)   |
847 /// |            /                  |
848 /// |           /         placeholder(Un)
849 /// empty(U1) --         /
850 /// |                   /
851 /// ...                /
852 /// |                 /
853 /// empty(Un) --------                      (smallest)
854 /// ```
855 ///
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
859 /// function.
860 ///
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
866 /// regions.
867 ///
868 /// ## Inference variables
869 ///
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
874 /// constraints).
875 ///
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
880 /// case.
881 ///
882 /// ## Bound Regions
883 ///
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].
891 ///
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).
896 ///
897 /// ## Placeholder and Free Regions
898 ///
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.
904 ///
905 /// To do this, we replace the bound regions with placeholder markers,
906 /// which don't satisfy any relation not explicitly provided.
907 ///
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`.
915 ///
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].
928 ///
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),
937
938     /// Region bound in a function scope, which will be substituted when the
939     /// function is called.
940     ReLateBound(DebruijnIndex, I::BoundRegion),
941
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),
946
947     /// Static data that has an "infinite" lifetime. Top in the region lattice.
948     ReStatic,
949
950     /// A region variable. Should not exist outside of type inference.
951     ReVar(I::RegionVid),
952
953     /// A placeholder region -- basically, the higher-ranked version of `ReFree`.
954     /// Should not exist outside of type inference.
955     RePlaceholder(I::PlaceholderRegion),
956
957     /// Erased region, used by trait selection, in MIR and during codegen.
958     ReErased,
959 }
960
961 // This is manually implemented for `RegionKind` because `std::mem::discriminant`
962 // returns an opaque value that is `PartialEq` but not `PartialOrd`
963 #[inline]
964 const fn regionkind_discriminant<I: Interner>(value: &RegionKind<I>) -> usize {
965     match value {
966         ReEarlyBound(_) => 0,
967         ReLateBound(_, _) => 1,
968         ReFree(_) => 2,
969         ReStatic => 3,
970         ReVar(_) => 4,
971         RePlaceholder(_) => 5,
972         ReErased => 6,
973     }
974 }
975
976 // This is manually implemented because a derive would require `I: Copy`
977 impl<I: Interner> Copy for RegionKind<I>
978 where
979     I::EarlyBoundRegion: Copy,
980     I::BoundRegion: Copy,
981     I::FreeRegion: Copy,
982     I::RegionVid: Copy,
983     I::PlaceholderRegion: Copy,
984 {
985 }
986
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 {
990         match 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,
998         }
999     }
1000 }
1001
1002 // This is manually implemented because a derive would require `I: PartialEq`
1003 impl<I: Interner> PartialEq for RegionKind<I> {
1004     #[inline]
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,
1015                 _ => {
1016                     debug_assert!(
1017                         false,
1018                         "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
1019                     );
1020                     true
1021                 }
1022             }
1023     }
1024 }
1025
1026 // This is manually implemented because a derive would require `I: Eq`
1027 impl<I: Interner> Eq for RegionKind<I> {}
1028
1029 // This is manually implemented because a derive would require `I: PartialOrd`
1030 impl<I: Interner> PartialOrd for RegionKind<I> {
1031     #[inline]
1032     fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
1033         Some(self.cmp(other))
1034     }
1035 }
1036
1037 // This is manually implemented because a derive would require `I: Ord`
1038 impl<I: Interner> Ord for RegionKind<I> {
1039     #[inline]
1040     fn cmp(&self, other: &RegionKind<I>) -> Ordering {
1041         regionkind_discriminant(self).cmp(&regionkind_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))
1046                 }
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,
1052                 _ => {
1053                     debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
1054                     Ordering::Equal
1055                 }
1056             }
1057         })
1058     }
1059 }
1060
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);
1065         match self {
1066             ReEarlyBound(r) => r.hash(state),
1067             ReLateBound(d, r) => {
1068                 d.hash(state);
1069                 r.hash(state)
1070             }
1071             ReFree(r) => r.hash(state),
1072             ReStatic => (),
1073             ReVar(r) => r.hash(state),
1074             RePlaceholder(r) => r.hash(state),
1075             ReErased => (),
1076         }
1077     }
1078 }
1079
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 {
1083         match self {
1084             ReEarlyBound(data) => write!(f, "ReEarlyBound({data:?})"),
1085
1086             ReLateBound(binder_id, bound_region) => {
1087                 write!(f, "ReLateBound({binder_id:?}, {bound_region:?})")
1088             }
1089
1090             ReFree(fr) => fr.fmt(f),
1091
1092             ReStatic => f.write_str("ReStatic"),
1093
1094             ReVar(vid) => vid.fmt(f),
1095
1096             RePlaceholder(placeholder) => write!(f, "RePlaceholder({placeholder:?})"),
1097
1098             ReErased => f.write_str("ReErased"),
1099         }
1100     }
1101 }
1102
1103 // This is manually implemented because a derive would require `I: Encodable`
1104 impl<I: Interner, E: TyEncoder> Encodable<E> for RegionKind<I>
1105 where
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>,
1111 {
1112     fn encode(&self, e: &mut E) {
1113         let disc = regionkind_discriminant(self);
1114         match self {
1115             ReEarlyBound(a) => e.emit_enum_variant(disc, |e| {
1116                 a.encode(e);
1117             }),
1118             ReLateBound(a, b) => e.emit_enum_variant(disc, |e| {
1119                 a.encode(e);
1120                 b.encode(e);
1121             }),
1122             ReFree(a) => e.emit_enum_variant(disc, |e| {
1123                 a.encode(e);
1124             }),
1125             ReStatic => e.emit_enum_variant(disc, |_| {}),
1126             ReVar(a) => e.emit_enum_variant(disc, |e| {
1127                 a.encode(e);
1128             }),
1129             RePlaceholder(a) => e.emit_enum_variant(disc, |e| {
1130                 a.encode(e);
1131             }),
1132             ReErased => e.emit_enum_variant(disc, |_| {}),
1133         }
1134     }
1135 }
1136
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>
1139 where
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>,
1145 {
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)),
1151             3 => ReStatic,
1152             4 => ReVar(Decodable::decode(d)),
1153             5 => RePlaceholder(Decodable::decode(d)),
1154             6 => ReErased,
1155             _ => panic!(
1156                 "{}",
1157                 format!(
1158                     "invalid enum variant tag while decoding `{}`, expected 0..{}",
1159                     "RegionKind", 8,
1160                 )
1161             ),
1162         }
1163     }
1164 }
1165
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>
1168 where
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>,
1174 {
1175     #[inline]
1176     fn hash_stable(
1177         &self,
1178         hcx: &mut CTX,
1179         hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
1180     ) {
1181         std::mem::discriminant(self).hash_stable(hcx, hasher);
1182         match self {
1183             ReErased | ReStatic => {
1184                 // No variant fields to hash for these ...
1185             }
1186             ReLateBound(d, r) => {
1187                 d.hash_stable(hcx, hasher);
1188                 r.hash_stable(hcx, hasher);
1189             }
1190             ReEarlyBound(r) => {
1191                 r.hash_stable(hcx, hasher);
1192             }
1193             ReFree(r) => {
1194                 r.hash_stable(hcx, hasher);
1195             }
1196             RePlaceholder(r) => {
1197                 r.hash_stable(hcx, hasher);
1198             }
1199             ReVar(_) => {
1200                 panic!("region variables should not be hashed: {self:?}")
1201             }
1202         }
1203     }
1204 }