]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_type_ir/src/sty.rs
Rollup merge of #97516 - RalfJung:atomics, r=joshtriplett
[rust.git] / compiler / rustc_type_ir / src / sty.rs
1 #![allow(rustc::usage_of_ty_tykind)]
2
3 use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
4 use std::{fmt, hash};
5
6 use crate::DebruijnIndex;
7 use crate::FloatTy;
8 use crate::IntTy;
9 use crate::Interner;
10 use crate::TyDecoder;
11 use crate::TyEncoder;
12 use crate::UintTy;
13 use crate::UniverseIndex;
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 /// Defines the kinds of types used by the type system.
22 ///
23 /// Types written by the user start out as `hir::TyKind` and get
24 /// converted to this representation using `AstConv::ast_ty_to_ty`.
25 #[rustc_diagnostic_item = "IrTyKind"]
26 pub enum TyKind<I: Interner> {
27     /// The primitive boolean type. Written as `bool`.
28     Bool,
29
30     /// The primitive character type; holds a Unicode scalar value
31     /// (a non-surrogate code point). Written as `char`.
32     Char,
33
34     /// A primitive signed integer type. For example, `i32`.
35     Int(IntTy),
36
37     /// A primitive unsigned integer type. For example, `u32`.
38     Uint(UintTy),
39
40     /// A primitive floating-point type. For example, `f64`.
41     Float(FloatTy),
42
43     /// Algebraic data types (ADT). For example: structures, enumerations and unions.
44     ///
45     /// For example, the type `List<i32>` would be represented using the `AdtDef`
46     /// for `struct List<T>` and the substs `[i32]`.
47     ///
48     /// Note that generic parameters in fields only get lazily substituted
49     /// by using something like `adt_def.all_fields().map(|field| field.ty(tcx, substs))`.
50     Adt(I::AdtDef, I::SubstsRef),
51
52     /// An unsized FFI type that is opaque to Rust. Written as `extern type T`.
53     Foreign(I::DefId),
54
55     /// The pointee of a string slice. Written as `str`.
56     Str,
57
58     /// An array with the given length. Written as `[T; N]`.
59     Array(I::Ty, I::Const),
60
61     /// The pointee of an array slice. Written as `[T]`.
62     Slice(I::Ty),
63
64     /// A raw pointer. Written as `*mut T` or `*const T`
65     RawPtr(I::TypeAndMut),
66
67     /// A reference; a pointer with an associated lifetime. Written as
68     /// `&'a mut T` or `&'a T`.
69     Ref(I::Region, I::Ty, I::Mutability),
70
71     /// The anonymous type of a function declaration/definition. Each
72     /// function has a unique type.
73     ///
74     /// For the function `fn foo() -> i32 { 3 }` this type would be
75     /// shown to the user as `fn() -> i32 {foo}`.
76     ///
77     /// For example the type of `bar` here:
78     /// ```rust
79     /// fn foo() -> i32 { 1 }
80     /// let bar = foo; // bar: fn() -> i32 {foo}
81     /// ```
82     FnDef(I::DefId, I::SubstsRef),
83
84     /// A pointer to a function. Written as `fn() -> i32`.
85     ///
86     /// Note that both functions and closures start out as either
87     /// [FnDef] or [Closure] which can be then be coerced to this variant.
88     ///
89     /// For example the type of `bar` here:
90     ///
91     /// ```rust
92     /// fn foo() -> i32 { 1 }
93     /// let bar: fn() -> i32 = foo;
94     /// ```
95     FnPtr(I::PolyFnSig),
96
97     /// A trait object. Written as `dyn for<'b> Trait<'b, Assoc = u32> + Send + 'a`.
98     Dynamic(I::ListBinderExistentialPredicate, I::Region),
99
100     /// The anonymous type of a closure. Used to represent the type of `|a| a`.
101     ///
102     /// Closure substs contain both the - potentially substituted - generic parameters
103     /// of its parent and some synthetic parameters. See the documentation for
104     /// `ClosureSubsts` for more details.
105     Closure(I::DefId, I::SubstsRef),
106
107     /// The anonymous type of a generator. Used to represent the type of
108     /// `|a| yield a`.
109     ///
110     /// For more info about generator substs, visit the documentation for
111     /// `GeneratorSubsts`.
112     Generator(I::DefId, I::SubstsRef, I::Movability),
113
114     /// A type representing the types stored inside a generator.
115     /// This should only appear as part of the `GeneratorSubsts`.
116     ///
117     /// Note that the captured variables for generators are stored separately
118     /// using a tuple in the same way as for closures.
119     ///
120     /// Unlike upvars, the witness can reference lifetimes from
121     /// inside of the generator itself. To deal with them in
122     /// the type of the generator, we convert them to higher ranked
123     /// lifetimes bound by the witness itself.
124     ///
125     /// Looking at the following example, the witness for this generator
126     /// may end up as something like `for<'a> [Vec<i32>, &'a Vec<i32>]`:
127     ///
128     /// ```ignore UNSOLVED (ask @compiler-errors, should this error? can we just swap the yields?)
129     /// #![feature(generators)]
130     /// |a| {
131     ///     let x = &vec![3];
132     ///     yield a;
133     ///     yield x[0];
134     /// }
135     /// # ;
136     /// ```
137     GeneratorWitness(I::BinderListTy),
138
139     /// The never type `!`.
140     Never,
141
142     /// A tuple type. For example, `(i32, bool)`.
143     Tuple(I::ListTy),
144
145     /// The projection of an associated type. For example,
146     /// `<T as Trait<..>>::N`.
147     Projection(I::ProjectionTy),
148
149     /// Opaque (`impl Trait`) type found in a return type.
150     ///
151     /// The `DefId` comes either from
152     /// * the `impl Trait` ast::Ty node,
153     /// * or the `type Foo = impl Trait` declaration
154     ///
155     /// For RPIT the substitutions are for the generics of the function,
156     /// while for TAIT it is used for the generic parameters of the alias.
157     ///
158     /// During codegen, `tcx.type_of(def_id)` can be used to get the underlying type.
159     Opaque(I::DefId, I::SubstsRef),
160
161     /// A type parameter; for example, `T` in `fn f<T>(x: T) {}`.
162     Param(I::ParamTy),
163
164     /// Bound type variable, used to represent the `'a` in `for<'a> fn(&'a ())`.
165     ///
166     /// For canonical queries, we replace inference variables with bound variables,
167     /// so e.g. when checking whether `&'_ (): Trait<_>` holds, we canonicalize that to
168     /// `for<'a, T> &'a (): Trait<T>` and then convert the introduced bound variables
169     /// back to inference variables in a new inference context when inside of the query.
170     ///
171     /// See the `rustc-dev-guide` for more details about
172     /// [higher-ranked trait bounds][1] and [canonical queries][2].
173     ///
174     /// [1]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
175     /// [2]: https://rustc-dev-guide.rust-lang.org/traits/canonical-queries.html
176     Bound(DebruijnIndex, I::BoundTy),
177
178     /// A placeholder type, used during higher ranked subtyping to instantiate
179     /// bound variables.
180     Placeholder(I::PlaceholderType),
181
182     /// A type variable used during type checking.
183     ///
184     /// Similar to placeholders, inference variables also live in a universe to
185     /// correctly deal with higher ranked types. Though unlike placeholders,
186     /// that universe is stored in the `InferCtxt` instead of directly
187     /// inside of the type.
188     Infer(I::InferTy),
189
190     /// A placeholder for a type which could not be computed; this is
191     /// propagated to avoid useless error messages.
192     Error(I::DelaySpanBugEmitted),
193 }
194
195 impl<I: Interner> TyKind<I> {
196     #[inline]
197     pub fn is_primitive(&self) -> bool {
198         matches!(self, Bool | Char | Int(_) | Uint(_) | Float(_))
199     }
200 }
201
202 // This is manually implemented for `TyKind` because `std::mem::discriminant`
203 // returns an opaque value that is `PartialEq` but not `PartialOrd`
204 #[inline]
205 const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize {
206     match value {
207         Bool => 0,
208         Char => 1,
209         Int(_) => 2,
210         Uint(_) => 3,
211         Float(_) => 4,
212         Adt(_, _) => 5,
213         Foreign(_) => 6,
214         Str => 7,
215         Array(_, _) => 8,
216         Slice(_) => 9,
217         RawPtr(_) => 10,
218         Ref(_, _, _) => 11,
219         FnDef(_, _) => 12,
220         FnPtr(_) => 13,
221         Dynamic(_, _) => 14,
222         Closure(_, _) => 15,
223         Generator(_, _, _) => 16,
224         GeneratorWitness(_) => 17,
225         Never => 18,
226         Tuple(_) => 19,
227         Projection(_) => 20,
228         Opaque(_, _) => 21,
229         Param(_) => 22,
230         Bound(_, _) => 23,
231         Placeholder(_) => 24,
232         Infer(_) => 25,
233         Error(_) => 26,
234     }
235 }
236
237 // This is manually implemented because a derive would require `I: Clone`
238 impl<I: Interner> Clone for TyKind<I> {
239     fn clone(&self) -> Self {
240         match self {
241             Bool => Bool,
242             Char => Char,
243             Int(i) => Int(i.clone()),
244             Uint(u) => Uint(u.clone()),
245             Float(f) => Float(f.clone()),
246             Adt(d, s) => Adt(d.clone(), s.clone()),
247             Foreign(d) => Foreign(d.clone()),
248             Str => Str,
249             Array(t, c) => Array(t.clone(), c.clone()),
250             Slice(t) => Slice(t.clone()),
251             RawPtr(t) => RawPtr(t.clone()),
252             Ref(r, t, m) => Ref(r.clone(), t.clone(), m.clone()),
253             FnDef(d, s) => FnDef(d.clone(), s.clone()),
254             FnPtr(s) => FnPtr(s.clone()),
255             Dynamic(p, r) => Dynamic(p.clone(), r.clone()),
256             Closure(d, s) => Closure(d.clone(), s.clone()),
257             Generator(d, s, m) => Generator(d.clone(), s.clone(), m.clone()),
258             GeneratorWitness(g) => GeneratorWitness(g.clone()),
259             Never => Never,
260             Tuple(t) => Tuple(t.clone()),
261             Projection(p) => Projection(p.clone()),
262             Opaque(d, s) => Opaque(d.clone(), s.clone()),
263             Param(p) => Param(p.clone()),
264             Bound(d, b) => Bound(d.clone(), b.clone()),
265             Placeholder(p) => Placeholder(p.clone()),
266             Infer(t) => Infer(t.clone()),
267             Error(e) => Error(e.clone()),
268         }
269     }
270 }
271
272 // This is manually implemented because a derive would require `I: PartialEq`
273 impl<I: Interner> PartialEq for TyKind<I> {
274     #[inline]
275     fn eq(&self, other: &TyKind<I>) -> bool {
276         let __self_vi = tykind_discriminant(self);
277         let __arg_1_vi = tykind_discriminant(other);
278         if __self_vi == __arg_1_vi {
279             match (&*self, &*other) {
280                 (&Int(ref __self_0), &Int(ref __arg_1_0)) => __self_0 == __arg_1_0,
281                 (&Uint(ref __self_0), &Uint(ref __arg_1_0)) => __self_0 == __arg_1_0,
282                 (&Float(ref __self_0), &Float(ref __arg_1_0)) => __self_0 == __arg_1_0,
283                 (&Adt(ref __self_0, ref __self_1), &Adt(ref __arg_1_0, ref __arg_1_1)) => {
284                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
285                 }
286                 (&Foreign(ref __self_0), &Foreign(ref __arg_1_0)) => __self_0 == __arg_1_0,
287                 (&Array(ref __self_0, ref __self_1), &Array(ref __arg_1_0, ref __arg_1_1)) => {
288                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
289                 }
290                 (&Slice(ref __self_0), &Slice(ref __arg_1_0)) => __self_0 == __arg_1_0,
291                 (&RawPtr(ref __self_0), &RawPtr(ref __arg_1_0)) => __self_0 == __arg_1_0,
292                 (
293                     &Ref(ref __self_0, ref __self_1, ref __self_2),
294                     &Ref(ref __arg_1_0, ref __arg_1_1, ref __arg_1_2),
295                 ) => __self_0 == __arg_1_0 && __self_1 == __arg_1_1 && __self_2 == __arg_1_2,
296                 (&FnDef(ref __self_0, ref __self_1), &FnDef(ref __arg_1_0, ref __arg_1_1)) => {
297                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
298                 }
299                 (&FnPtr(ref __self_0), &FnPtr(ref __arg_1_0)) => __self_0 == __arg_1_0,
300                 (&Dynamic(ref __self_0, ref __self_1), &Dynamic(ref __arg_1_0, ref __arg_1_1)) => {
301                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
302                 }
303                 (&Closure(ref __self_0, ref __self_1), &Closure(ref __arg_1_0, ref __arg_1_1)) => {
304                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
305                 }
306                 (
307                     &Generator(ref __self_0, ref __self_1, ref __self_2),
308                     &Generator(ref __arg_1_0, ref __arg_1_1, ref __arg_1_2),
309                 ) => __self_0 == __arg_1_0 && __self_1 == __arg_1_1 && __self_2 == __arg_1_2,
310                 (&GeneratorWitness(ref __self_0), &GeneratorWitness(ref __arg_1_0)) => {
311                     __self_0 == __arg_1_0
312                 }
313                 (&Tuple(ref __self_0), &Tuple(ref __arg_1_0)) => __self_0 == __arg_1_0,
314                 (&Projection(ref __self_0), &Projection(ref __arg_1_0)) => __self_0 == __arg_1_0,
315                 (&Opaque(ref __self_0, ref __self_1), &Opaque(ref __arg_1_0, ref __arg_1_1)) => {
316                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
317                 }
318                 (&Param(ref __self_0), &Param(ref __arg_1_0)) => __self_0 == __arg_1_0,
319                 (&Bound(ref __self_0, ref __self_1), &Bound(ref __arg_1_0, ref __arg_1_1)) => {
320                     __self_0 == __arg_1_0 && __self_1 == __arg_1_1
321                 }
322                 (&Placeholder(ref __self_0), &Placeholder(ref __arg_1_0)) => __self_0 == __arg_1_0,
323                 (&Infer(ref __self_0), &Infer(ref __arg_1_0)) => __self_0 == __arg_1_0,
324                 (&Error(ref __self_0), &Error(ref __arg_1_0)) => __self_0 == __arg_1_0,
325                 _ => true,
326             }
327         } else {
328             false
329         }
330     }
331 }
332
333 // This is manually implemented because a derive would require `I: Eq`
334 impl<I: Interner> Eq for TyKind<I> {}
335
336 // This is manually implemented because a derive would require `I: PartialOrd`
337 impl<I: Interner> PartialOrd for TyKind<I> {
338     #[inline]
339     fn partial_cmp(&self, other: &TyKind<I>) -> Option<Ordering> {
340         Some(Ord::cmp(self, other))
341     }
342 }
343
344 // This is manually implemented because a derive would require `I: Ord`
345 impl<I: Interner> Ord for TyKind<I> {
346     #[inline]
347     fn cmp(&self, other: &TyKind<I>) -> Ordering {
348         let __self_vi = tykind_discriminant(self);
349         let __arg_1_vi = tykind_discriminant(other);
350         if __self_vi == __arg_1_vi {
351             match (&*self, &*other) {
352                 (&Int(ref __self_0), &Int(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
353                 (&Uint(ref __self_0), &Uint(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
354                 (&Float(ref __self_0), &Float(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
355                 (&Adt(ref __self_0, ref __self_1), &Adt(ref __arg_1_0, ref __arg_1_1)) => {
356                     match Ord::cmp(__self_0, __arg_1_0) {
357                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
358                         cmp => cmp,
359                     }
360                 }
361                 (&Foreign(ref __self_0), &Foreign(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
362                 (&Array(ref __self_0, ref __self_1), &Array(ref __arg_1_0, ref __arg_1_1)) => {
363                     match Ord::cmp(__self_0, __arg_1_0) {
364                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
365                         cmp => cmp,
366                     }
367                 }
368                 (&Slice(ref __self_0), &Slice(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
369                 (&RawPtr(ref __self_0), &RawPtr(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
370                 (
371                     &Ref(ref __self_0, ref __self_1, ref __self_2),
372                     &Ref(ref __arg_1_0, ref __arg_1_1, ref __arg_1_2),
373                 ) => match Ord::cmp(__self_0, __arg_1_0) {
374                     Ordering::Equal => match Ord::cmp(__self_1, __arg_1_1) {
375                         Ordering::Equal => Ord::cmp(__self_2, __arg_1_2),
376                         cmp => cmp,
377                     },
378                     cmp => cmp,
379                 },
380                 (&FnDef(ref __self_0, ref __self_1), &FnDef(ref __arg_1_0, ref __arg_1_1)) => {
381                     match Ord::cmp(__self_0, __arg_1_0) {
382                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
383                         cmp => cmp,
384                     }
385                 }
386                 (&FnPtr(ref __self_0), &FnPtr(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
387                 (&Dynamic(ref __self_0, ref __self_1), &Dynamic(ref __arg_1_0, ref __arg_1_1)) => {
388                     match Ord::cmp(__self_0, __arg_1_0) {
389                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
390                         cmp => cmp,
391                     }
392                 }
393                 (&Closure(ref __self_0, ref __self_1), &Closure(ref __arg_1_0, ref __arg_1_1)) => {
394                     match Ord::cmp(__self_0, __arg_1_0) {
395                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
396                         cmp => cmp,
397                     }
398                 }
399                 (
400                     &Generator(ref __self_0, ref __self_1, ref __self_2),
401                     &Generator(ref __arg_1_0, ref __arg_1_1, ref __arg_1_2),
402                 ) => match Ord::cmp(__self_0, __arg_1_0) {
403                     Ordering::Equal => match Ord::cmp(__self_1, __arg_1_1) {
404                         Ordering::Equal => Ord::cmp(__self_2, __arg_1_2),
405                         cmp => cmp,
406                     },
407                     cmp => cmp,
408                 },
409                 (&GeneratorWitness(ref __self_0), &GeneratorWitness(ref __arg_1_0)) => {
410                     Ord::cmp(__self_0, __arg_1_0)
411                 }
412                 (&Tuple(ref __self_0), &Tuple(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
413                 (&Projection(ref __self_0), &Projection(ref __arg_1_0)) => {
414                     Ord::cmp(__self_0, __arg_1_0)
415                 }
416                 (&Opaque(ref __self_0, ref __self_1), &Opaque(ref __arg_1_0, ref __arg_1_1)) => {
417                     match Ord::cmp(__self_0, __arg_1_0) {
418                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
419                         cmp => cmp,
420                     }
421                 }
422                 (&Param(ref __self_0), &Param(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
423                 (&Bound(ref __self_0, ref __self_1), &Bound(ref __arg_1_0, ref __arg_1_1)) => {
424                     match Ord::cmp(__self_0, __arg_1_0) {
425                         Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
426                         cmp => cmp,
427                     }
428                 }
429                 (&Placeholder(ref __self_0), &Placeholder(ref __arg_1_0)) => {
430                     Ord::cmp(__self_0, __arg_1_0)
431                 }
432                 (&Infer(ref __self_0), &Infer(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
433                 (&Error(ref __self_0), &Error(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
434                 _ => Ordering::Equal,
435             }
436         } else {
437             Ord::cmp(&__self_vi, &__arg_1_vi)
438         }
439     }
440 }
441
442 // This is manually implemented because a derive would require `I: Hash`
443 impl<I: Interner> hash::Hash for TyKind<I> {
444     fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
445         match (&*self,) {
446             (&Int(ref __self_0),) => {
447                 hash::Hash::hash(&tykind_discriminant(self), state);
448                 hash::Hash::hash(__self_0, state)
449             }
450             (&Uint(ref __self_0),) => {
451                 hash::Hash::hash(&tykind_discriminant(self), state);
452                 hash::Hash::hash(__self_0, state)
453             }
454             (&Float(ref __self_0),) => {
455                 hash::Hash::hash(&tykind_discriminant(self), state);
456                 hash::Hash::hash(__self_0, state)
457             }
458             (&Adt(ref __self_0, ref __self_1),) => {
459                 hash::Hash::hash(&tykind_discriminant(self), state);
460                 hash::Hash::hash(__self_0, state);
461                 hash::Hash::hash(__self_1, state)
462             }
463             (&Foreign(ref __self_0),) => {
464                 hash::Hash::hash(&tykind_discriminant(self), state);
465                 hash::Hash::hash(__self_0, state)
466             }
467             (&Array(ref __self_0, ref __self_1),) => {
468                 hash::Hash::hash(&tykind_discriminant(self), state);
469                 hash::Hash::hash(__self_0, state);
470                 hash::Hash::hash(__self_1, state)
471             }
472             (&Slice(ref __self_0),) => {
473                 hash::Hash::hash(&tykind_discriminant(self), state);
474                 hash::Hash::hash(__self_0, state)
475             }
476             (&RawPtr(ref __self_0),) => {
477                 hash::Hash::hash(&tykind_discriminant(self), state);
478                 hash::Hash::hash(__self_0, state)
479             }
480             (&Ref(ref __self_0, ref __self_1, ref __self_2),) => {
481                 hash::Hash::hash(&tykind_discriminant(self), state);
482                 hash::Hash::hash(__self_0, state);
483                 hash::Hash::hash(__self_1, state);
484                 hash::Hash::hash(__self_2, state)
485             }
486             (&FnDef(ref __self_0, ref __self_1),) => {
487                 hash::Hash::hash(&tykind_discriminant(self), state);
488                 hash::Hash::hash(__self_0, state);
489                 hash::Hash::hash(__self_1, state)
490             }
491             (&FnPtr(ref __self_0),) => {
492                 hash::Hash::hash(&tykind_discriminant(self), state);
493                 hash::Hash::hash(__self_0, state)
494             }
495             (&Dynamic(ref __self_0, ref __self_1),) => {
496                 hash::Hash::hash(&tykind_discriminant(self), state);
497                 hash::Hash::hash(__self_0, state);
498                 hash::Hash::hash(__self_1, state)
499             }
500             (&Closure(ref __self_0, ref __self_1),) => {
501                 hash::Hash::hash(&tykind_discriminant(self), state);
502                 hash::Hash::hash(__self_0, state);
503                 hash::Hash::hash(__self_1, state)
504             }
505             (&Generator(ref __self_0, ref __self_1, ref __self_2),) => {
506                 hash::Hash::hash(&tykind_discriminant(self), state);
507                 hash::Hash::hash(__self_0, state);
508                 hash::Hash::hash(__self_1, state);
509                 hash::Hash::hash(__self_2, state)
510             }
511             (&GeneratorWitness(ref __self_0),) => {
512                 hash::Hash::hash(&tykind_discriminant(self), state);
513                 hash::Hash::hash(__self_0, state)
514             }
515             (&Tuple(ref __self_0),) => {
516                 hash::Hash::hash(&tykind_discriminant(self), state);
517                 hash::Hash::hash(__self_0, state)
518             }
519             (&Projection(ref __self_0),) => {
520                 hash::Hash::hash(&tykind_discriminant(self), state);
521                 hash::Hash::hash(__self_0, state)
522             }
523             (&Opaque(ref __self_0, ref __self_1),) => {
524                 hash::Hash::hash(&tykind_discriminant(self), state);
525                 hash::Hash::hash(__self_0, state);
526                 hash::Hash::hash(__self_1, state)
527             }
528             (&Param(ref __self_0),) => {
529                 hash::Hash::hash(&tykind_discriminant(self), state);
530                 hash::Hash::hash(__self_0, state)
531             }
532             (&Bound(ref __self_0, ref __self_1),) => {
533                 hash::Hash::hash(&tykind_discriminant(self), state);
534                 hash::Hash::hash(__self_0, state);
535                 hash::Hash::hash(__self_1, state)
536             }
537             (&Placeholder(ref __self_0),) => {
538                 hash::Hash::hash(&tykind_discriminant(self), state);
539                 hash::Hash::hash(__self_0, state)
540             }
541             (&Infer(ref __self_0),) => {
542                 hash::Hash::hash(&tykind_discriminant(self), state);
543                 hash::Hash::hash(__self_0, state)
544             }
545             (&Error(ref __self_0),) => {
546                 hash::Hash::hash(&tykind_discriminant(self), state);
547                 hash::Hash::hash(__self_0, state)
548             }
549             _ => hash::Hash::hash(&tykind_discriminant(self), state),
550         }
551     }
552 }
553
554 // This is manually implemented because a derive would require `I: Debug`
555 impl<I: Interner> fmt::Debug for TyKind<I> {
556     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
557         match (&*self,) {
558             (&Bool,) => fmt::Formatter::write_str(f, "Bool"),
559             (&Char,) => fmt::Formatter::write_str(f, "Char"),
560             (&Int(ref __self_0),) => {
561                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Int");
562                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
563                 fmt::DebugTuple::finish(debug_trait_builder)
564             }
565             (&Uint(ref __self_0),) => {
566                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Uint");
567                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
568                 fmt::DebugTuple::finish(debug_trait_builder)
569             }
570             (&Float(ref __self_0),) => {
571                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Float");
572                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
573                 fmt::DebugTuple::finish(debug_trait_builder)
574             }
575             (&Adt(ref __self_0, ref __self_1),) => {
576                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Adt");
577                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
578                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
579                 fmt::DebugTuple::finish(debug_trait_builder)
580             }
581             (&Foreign(ref __self_0),) => {
582                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Foreign");
583                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
584                 fmt::DebugTuple::finish(debug_trait_builder)
585             }
586             (&Str,) => fmt::Formatter::write_str(f, "Str"),
587             (&Array(ref __self_0, ref __self_1),) => {
588                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Array");
589                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
590                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
591                 fmt::DebugTuple::finish(debug_trait_builder)
592             }
593             (&Slice(ref __self_0),) => {
594                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Slice");
595                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
596                 fmt::DebugTuple::finish(debug_trait_builder)
597             }
598             (&RawPtr(ref __self_0),) => {
599                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "RawPtr");
600                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
601                 fmt::DebugTuple::finish(debug_trait_builder)
602             }
603             (&Ref(ref __self_0, ref __self_1, ref __self_2),) => {
604                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Ref");
605                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
606                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
607                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_2);
608                 fmt::DebugTuple::finish(debug_trait_builder)
609             }
610             (&FnDef(ref __self_0, ref __self_1),) => {
611                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "FnDef");
612                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
613                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
614                 fmt::DebugTuple::finish(debug_trait_builder)
615             }
616             (&FnPtr(ref __self_0),) => {
617                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "FnPtr");
618                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
619                 fmt::DebugTuple::finish(debug_trait_builder)
620             }
621             (&Dynamic(ref __self_0, ref __self_1),) => {
622                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Dynamic");
623                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
624                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
625                 fmt::DebugTuple::finish(debug_trait_builder)
626             }
627             (&Closure(ref __self_0, ref __self_1),) => {
628                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Closure");
629                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
630                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
631                 fmt::DebugTuple::finish(debug_trait_builder)
632             }
633             (&Generator(ref __self_0, ref __self_1, ref __self_2),) => {
634                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Generator");
635                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
636                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
637                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_2);
638                 fmt::DebugTuple::finish(debug_trait_builder)
639             }
640             (&GeneratorWitness(ref __self_0),) => {
641                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "GeneratorWitness");
642                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
643                 fmt::DebugTuple::finish(debug_trait_builder)
644             }
645             (&Never,) => fmt::Formatter::write_str(f, "Never"),
646             (&Tuple(ref __self_0),) => {
647                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Tuple");
648                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
649                 fmt::DebugTuple::finish(debug_trait_builder)
650             }
651             (&Projection(ref __self_0),) => {
652                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Projection");
653                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
654                 fmt::DebugTuple::finish(debug_trait_builder)
655             }
656             (&Opaque(ref __self_0, ref __self_1),) => {
657                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Opaque");
658                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
659                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
660                 fmt::DebugTuple::finish(debug_trait_builder)
661             }
662             (&Param(ref __self_0),) => {
663                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Param");
664                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
665                 fmt::DebugTuple::finish(debug_trait_builder)
666             }
667             (&Bound(ref __self_0, ref __self_1),) => {
668                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Bound");
669                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
670                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_1);
671                 fmt::DebugTuple::finish(debug_trait_builder)
672             }
673             (&Placeholder(ref __self_0),) => {
674                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Placeholder");
675                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
676                 fmt::DebugTuple::finish(debug_trait_builder)
677             }
678             (&Infer(ref __self_0),) => {
679                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Infer");
680                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
681                 fmt::DebugTuple::finish(debug_trait_builder)
682             }
683             (&Error(ref __self_0),) => {
684                 let debug_trait_builder = &mut fmt::Formatter::debug_tuple(f, "Error");
685                 let _ = fmt::DebugTuple::field(debug_trait_builder, &__self_0);
686                 fmt::DebugTuple::finish(debug_trait_builder)
687             }
688         }
689     }
690 }
691
692 // This is manually implemented because a derive would require `I: Encodable`
693 impl<I: Interner, E: TyEncoder> Encodable<E> for TyKind<I>
694 where
695     I::DelaySpanBugEmitted: Encodable<E>,
696     I::AdtDef: Encodable<E>,
697     I::SubstsRef: Encodable<E>,
698     I::DefId: Encodable<E>,
699     I::Ty: Encodable<E>,
700     I::Const: Encodable<E>,
701     I::Region: Encodable<E>,
702     I::TypeAndMut: Encodable<E>,
703     I::Mutability: Encodable<E>,
704     I::Movability: Encodable<E>,
705     I::PolyFnSig: Encodable<E>,
706     I::ListBinderExistentialPredicate: Encodable<E>,
707     I::BinderListTy: Encodable<E>,
708     I::ListTy: Encodable<E>,
709     I::ProjectionTy: Encodable<E>,
710     I::ParamTy: Encodable<E>,
711     I::BoundTy: Encodable<E>,
712     I::PlaceholderType: Encodable<E>,
713     I::InferTy: Encodable<E>,
714     I::DelaySpanBugEmitted: Encodable<E>,
715     I::PredicateKind: Encodable<E>,
716     I::AllocId: Encodable<E>,
717 {
718     fn encode(&self, e: &mut E) {
719         let disc = tykind_discriminant(self);
720         match self {
721             Bool => e.emit_enum_variant(disc, |_| {}),
722             Char => e.emit_enum_variant(disc, |_| {}),
723             Int(i) => e.emit_enum_variant(disc, |e| {
724                 i.encode(e);
725             }),
726             Uint(u) => e.emit_enum_variant(disc, |e| {
727                 u.encode(e);
728             }),
729             Float(f) => e.emit_enum_variant(disc, |e| {
730                 f.encode(e);
731             }),
732             Adt(adt, substs) => e.emit_enum_variant(disc, |e| {
733                 adt.encode(e);
734                 substs.encode(e);
735             }),
736             Foreign(def_id) => e.emit_enum_variant(disc, |e| {
737                 def_id.encode(e);
738             }),
739             Str => e.emit_enum_variant(disc, |_| {}),
740             Array(t, c) => e.emit_enum_variant(disc, |e| {
741                 t.encode(e);
742                 c.encode(e);
743             }),
744             Slice(t) => e.emit_enum_variant(disc, |e| {
745                 t.encode(e);
746             }),
747             RawPtr(tam) => e.emit_enum_variant(disc, |e| {
748                 tam.encode(e);
749             }),
750             Ref(r, t, m) => e.emit_enum_variant(disc, |e| {
751                 r.encode(e);
752                 t.encode(e);
753                 m.encode(e);
754             }),
755             FnDef(def_id, substs) => e.emit_enum_variant(disc, |e| {
756                 def_id.encode(e);
757                 substs.encode(e);
758             }),
759             FnPtr(polyfnsig) => e.emit_enum_variant(disc, |e| {
760                 polyfnsig.encode(e);
761             }),
762             Dynamic(l, r) => e.emit_enum_variant(disc, |e| {
763                 l.encode(e);
764                 r.encode(e);
765             }),
766             Closure(def_id, substs) => e.emit_enum_variant(disc, |e| {
767                 def_id.encode(e);
768                 substs.encode(e);
769             }),
770             Generator(def_id, substs, m) => e.emit_enum_variant(disc, |e| {
771                 def_id.encode(e);
772                 substs.encode(e);
773                 m.encode(e);
774             }),
775             GeneratorWitness(b) => e.emit_enum_variant(disc, |e| {
776                 b.encode(e);
777             }),
778             Never => e.emit_enum_variant(disc, |_| {}),
779             Tuple(substs) => e.emit_enum_variant(disc, |e| {
780                 substs.encode(e);
781             }),
782             Projection(p) => e.emit_enum_variant(disc, |e| {
783                 p.encode(e);
784             }),
785             Opaque(def_id, substs) => e.emit_enum_variant(disc, |e| {
786                 def_id.encode(e);
787                 substs.encode(e);
788             }),
789             Param(p) => e.emit_enum_variant(disc, |e| {
790                 p.encode(e);
791             }),
792             Bound(d, b) => e.emit_enum_variant(disc, |e| {
793                 d.encode(e);
794                 b.encode(e);
795             }),
796             Placeholder(p) => e.emit_enum_variant(disc, |e| {
797                 p.encode(e);
798             }),
799             Infer(i) => e.emit_enum_variant(disc, |e| {
800                 i.encode(e);
801             }),
802             Error(d) => e.emit_enum_variant(disc, |e| {
803                 d.encode(e);
804             }),
805         }
806     }
807 }
808
809 // This is manually implemented because a derive would require `I: Decodable`
810 impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for TyKind<I>
811 where
812     I::DelaySpanBugEmitted: Decodable<D>,
813     I::AdtDef: Decodable<D>,
814     I::SubstsRef: Decodable<D>,
815     I::DefId: Decodable<D>,
816     I::Ty: Decodable<D>,
817     I::Const: Decodable<D>,
818     I::Region: Decodable<D>,
819     I::TypeAndMut: Decodable<D>,
820     I::Mutability: Decodable<D>,
821     I::Movability: Decodable<D>,
822     I::PolyFnSig: Decodable<D>,
823     I::ListBinderExistentialPredicate: Decodable<D>,
824     I::BinderListTy: Decodable<D>,
825     I::ListTy: Decodable<D>,
826     I::ProjectionTy: Decodable<D>,
827     I::ParamTy: Decodable<D>,
828     I::BoundTy: Decodable<D>,
829     I::PlaceholderType: Decodable<D>,
830     I::InferTy: Decodable<D>,
831     I::DelaySpanBugEmitted: Decodable<D>,
832     I::PredicateKind: Decodable<D>,
833     I::AllocId: Decodable<D>,
834 {
835     fn decode(d: &mut D) -> Self {
836         match Decoder::read_usize(d) {
837             0 => Bool,
838             1 => Char,
839             2 => Int(Decodable::decode(d)),
840             3 => Uint(Decodable::decode(d)),
841             4 => Float(Decodable::decode(d)),
842             5 => Adt(Decodable::decode(d), Decodable::decode(d)),
843             6 => Foreign(Decodable::decode(d)),
844             7 => Str,
845             8 => Array(Decodable::decode(d), Decodable::decode(d)),
846             9 => Slice(Decodable::decode(d)),
847             10 => RawPtr(Decodable::decode(d)),
848             11 => Ref(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
849             12 => FnDef(Decodable::decode(d), Decodable::decode(d)),
850             13 => FnPtr(Decodable::decode(d)),
851             14 => Dynamic(Decodable::decode(d), Decodable::decode(d)),
852             15 => Closure(Decodable::decode(d), Decodable::decode(d)),
853             16 => Generator(Decodable::decode(d), Decodable::decode(d), Decodable::decode(d)),
854             17 => GeneratorWitness(Decodable::decode(d)),
855             18 => Never,
856             19 => Tuple(Decodable::decode(d)),
857             20 => Projection(Decodable::decode(d)),
858             21 => Opaque(Decodable::decode(d), Decodable::decode(d)),
859             22 => Param(Decodable::decode(d)),
860             23 => Bound(Decodable::decode(d), Decodable::decode(d)),
861             24 => Placeholder(Decodable::decode(d)),
862             25 => Infer(Decodable::decode(d)),
863             26 => Error(Decodable::decode(d)),
864             _ => panic!(
865                 "{}",
866                 format!(
867                     "invalid enum variant tag while decoding `{}`, expected 0..{}",
868                     "TyKind", 27,
869                 )
870             ),
871         }
872     }
873 }
874
875 // This is not a derived impl because a derive would require `I: HashStable`
876 #[allow(rustc::usage_of_ty_tykind)]
877 impl<CTX, I: Interner> HashStable<CTX> for TyKind<I>
878 where
879     I::AdtDef: HashStable<CTX>,
880     I::DefId: HashStable<CTX>,
881     I::SubstsRef: HashStable<CTX>,
882     I::Ty: HashStable<CTX>,
883     I::Const: HashStable<CTX>,
884     I::TypeAndMut: HashStable<CTX>,
885     I::PolyFnSig: HashStable<CTX>,
886     I::ListBinderExistentialPredicate: HashStable<CTX>,
887     I::Region: HashStable<CTX>,
888     I::Movability: HashStable<CTX>,
889     I::Mutability: HashStable<CTX>,
890     I::BinderListTy: HashStable<CTX>,
891     I::ListTy: HashStable<CTX>,
892     I::ProjectionTy: HashStable<CTX>,
893     I::BoundTy: HashStable<CTX>,
894     I::ParamTy: HashStable<CTX>,
895     I::PlaceholderType: HashStable<CTX>,
896     I::InferTy: HashStable<CTX>,
897     I::DelaySpanBugEmitted: HashStable<CTX>,
898 {
899     #[inline]
900     fn hash_stable(
901         &self,
902         __hcx: &mut CTX,
903         __hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
904     ) {
905         std::mem::discriminant(self).hash_stable(__hcx, __hasher);
906         match self {
907             Bool => {}
908             Char => {}
909             Int(i) => {
910                 i.hash_stable(__hcx, __hasher);
911             }
912             Uint(u) => {
913                 u.hash_stable(__hcx, __hasher);
914             }
915             Float(f) => {
916                 f.hash_stable(__hcx, __hasher);
917             }
918             Adt(adt, substs) => {
919                 adt.hash_stable(__hcx, __hasher);
920                 substs.hash_stable(__hcx, __hasher);
921             }
922             Foreign(def_id) => {
923                 def_id.hash_stable(__hcx, __hasher);
924             }
925             Str => {}
926             Array(t, c) => {
927                 t.hash_stable(__hcx, __hasher);
928                 c.hash_stable(__hcx, __hasher);
929             }
930             Slice(t) => {
931                 t.hash_stable(__hcx, __hasher);
932             }
933             RawPtr(tam) => {
934                 tam.hash_stable(__hcx, __hasher);
935             }
936             Ref(r, t, m) => {
937                 r.hash_stable(__hcx, __hasher);
938                 t.hash_stable(__hcx, __hasher);
939                 m.hash_stable(__hcx, __hasher);
940             }
941             FnDef(def_id, substs) => {
942                 def_id.hash_stable(__hcx, __hasher);
943                 substs.hash_stable(__hcx, __hasher);
944             }
945             FnPtr(polyfnsig) => {
946                 polyfnsig.hash_stable(__hcx, __hasher);
947             }
948             Dynamic(l, r) => {
949                 l.hash_stable(__hcx, __hasher);
950                 r.hash_stable(__hcx, __hasher);
951             }
952             Closure(def_id, substs) => {
953                 def_id.hash_stable(__hcx, __hasher);
954                 substs.hash_stable(__hcx, __hasher);
955             }
956             Generator(def_id, substs, m) => {
957                 def_id.hash_stable(__hcx, __hasher);
958                 substs.hash_stable(__hcx, __hasher);
959                 m.hash_stable(__hcx, __hasher);
960             }
961             GeneratorWitness(b) => {
962                 b.hash_stable(__hcx, __hasher);
963             }
964             Never => {}
965             Tuple(substs) => {
966                 substs.hash_stable(__hcx, __hasher);
967             }
968             Projection(p) => {
969                 p.hash_stable(__hcx, __hasher);
970             }
971             Opaque(def_id, substs) => {
972                 def_id.hash_stable(__hcx, __hasher);
973                 substs.hash_stable(__hcx, __hasher);
974             }
975             Param(p) => {
976                 p.hash_stable(__hcx, __hasher);
977             }
978             Bound(d, b) => {
979                 d.hash_stable(__hcx, __hasher);
980                 b.hash_stable(__hcx, __hasher);
981             }
982             Placeholder(p) => {
983                 p.hash_stable(__hcx, __hasher);
984             }
985             Infer(i) => {
986                 i.hash_stable(__hcx, __hasher);
987             }
988             Error(d) => {
989                 d.hash_stable(__hcx, __hasher);
990             }
991         }
992     }
993 }
994
995 /// Representation of regions. Note that the NLL checker uses a distinct
996 /// representation of regions. For this reason, it internally replaces all the
997 /// regions with inference variables -- the index of the variable is then used
998 /// to index into internal NLL data structures. See `rustc_const_eval::borrow_check`
999 /// module for more information.
1000 ///
1001 /// Note: operations are on the wrapper `Region` type, which is interned,
1002 /// rather than this type.
1003 ///
1004 /// ## The Region lattice within a given function
1005 ///
1006 /// In general, the region lattice looks like
1007 ///
1008 /// ```text
1009 /// static ----------+-----...------+       (greatest)
1010 /// |                |              |
1011 /// early-bound and  |              |
1012 /// free regions     |              |
1013 /// |                |              |
1014 /// |                |              |
1015 /// empty(root)   placeholder(U1)   |
1016 /// |            /                  |
1017 /// |           /         placeholder(Un)
1018 /// empty(U1) --         /
1019 /// |                   /
1020 /// ...                /
1021 /// |                 /
1022 /// empty(Un) --------                      (smallest)
1023 /// ```
1024 ///
1025 /// Early-bound/free regions are the named lifetimes in scope from the
1026 /// function declaration. They have relationships to one another
1027 /// determined based on the declared relationships from the
1028 /// function.
1029 ///
1030 /// Note that inference variables and bound regions are not included
1031 /// in this diagram. In the case of inference variables, they should
1032 /// be inferred to some other region from the diagram.  In the case of
1033 /// bound regions, they are excluded because they don't make sense to
1034 /// include -- the diagram indicates the relationship between free
1035 /// regions.
1036 ///
1037 /// ## Inference variables
1038 ///
1039 /// During region inference, we sometimes create inference variables,
1040 /// represented as `ReVar`. These will be inferred by the code in
1041 /// `infer::lexical_region_resolve` to some free region from the
1042 /// lattice above (the minimal region that meets the
1043 /// constraints).
1044 ///
1045 /// During NLL checking, where regions are defined differently, we
1046 /// also use `ReVar` -- in that case, the index is used to index into
1047 /// the NLL region checker's data structures. The variable may in fact
1048 /// represent either a free region or an inference variable, in that
1049 /// case.
1050 ///
1051 /// ## Bound Regions
1052 ///
1053 /// These are regions that are stored behind a binder and must be substituted
1054 /// with some concrete region before being used. There are two kind of
1055 /// bound regions: early-bound, which are bound in an item's `Generics`,
1056 /// and are substituted by an `InternalSubsts`, and late-bound, which are part of
1057 /// higher-ranked types (e.g., `for<'a> fn(&'a ())`), and are substituted by
1058 /// the likes of `liberate_late_bound_regions`. The distinction exists
1059 /// because higher-ranked lifetimes aren't supported in all places. See [1][2].
1060 ///
1061 /// Unlike `Param`s, bound regions are not supposed to exist "in the wild"
1062 /// outside their binder, e.g., in types passed to type inference, and
1063 /// should first be substituted (by placeholder regions, free regions,
1064 /// or region variables).
1065 ///
1066 /// ## Placeholder and Free Regions
1067 ///
1068 /// One often wants to work with bound regions without knowing their precise
1069 /// identity. For example, when checking a function, the lifetime of a borrow
1070 /// can end up being assigned to some region parameter. In these cases,
1071 /// it must be ensured that bounds on the region can't be accidentally
1072 /// assumed without being checked.
1073 ///
1074 /// To do this, we replace the bound regions with placeholder markers,
1075 /// which don't satisfy any relation not explicitly provided.
1076 ///
1077 /// There are two kinds of placeholder regions in rustc: `ReFree` and
1078 /// `RePlaceholder`. When checking an item's body, `ReFree` is supposed
1079 /// to be used. These also support explicit bounds: both the internally-stored
1080 /// *scope*, which the region is assumed to outlive, as well as other
1081 /// relations stored in the `FreeRegionMap`. Note that these relations
1082 /// aren't checked when you `make_subregion` (or `eq_types`), only by
1083 /// `resolve_regions_and_report_errors`.
1084 ///
1085 /// When working with higher-ranked types, some region relations aren't
1086 /// yet known, so you can't just call `resolve_regions_and_report_errors`.
1087 /// `RePlaceholder` is designed for this purpose. In these contexts,
1088 /// there's also the risk that some inference variable laying around will
1089 /// get unified with your placeholder region: if you want to check whether
1090 /// `for<'a> Foo<'_>: 'a`, and you substitute your bound region `'a`
1091 /// with a placeholder region `'%a`, the variable `'_` would just be
1092 /// instantiated to the placeholder region `'%a`, which is wrong because
1093 /// the inference variable is supposed to satisfy the relation
1094 /// *for every value of the placeholder region*. To ensure that doesn't
1095 /// happen, you can use `leak_check`. This is more clearly explained
1096 /// by the [rustc dev guide].
1097 ///
1098 /// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
1099 /// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
1100 /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
1101 pub enum RegionKind<I: Interner> {
1102     /// Region bound in a type or fn declaration which will be
1103     /// substituted 'early' -- that is, at the same time when type
1104     /// parameters are substituted.
1105     ReEarlyBound(I::EarlyBoundRegion),
1106
1107     /// Region bound in a function scope, which will be substituted when the
1108     /// function is called.
1109     ReLateBound(DebruijnIndex, I::BoundRegion),
1110
1111     /// When checking a function body, the types of all arguments and so forth
1112     /// that refer to bound region parameters are modified to refer to free
1113     /// region parameters.
1114     ReFree(I::FreeRegion),
1115
1116     /// Static data that has an "infinite" lifetime. Top in the region lattice.
1117     ReStatic,
1118
1119     /// A region variable. Should not exist outside of type inference.
1120     ReVar(I::RegionVid),
1121
1122     /// A placeholder region -- basically, the higher-ranked version of `ReFree`.
1123     /// Should not exist outside of type inference.
1124     RePlaceholder(I::PlaceholderRegion),
1125
1126     /// Empty lifetime is for data that is never accessed.  We tag the
1127     /// empty lifetime with a universe -- the idea is that we don't
1128     /// want `exists<'a> { forall<'b> { 'b: 'a } }` to be satisfiable.
1129     /// Therefore, the `'empty` in a universe `U` is less than all
1130     /// regions visible from `U`, but not less than regions not visible
1131     /// from `U`.
1132     ReEmpty(UniverseIndex),
1133
1134     /// Erased region, used by trait selection, in MIR and during codegen.
1135     ReErased,
1136 }
1137
1138 // This is manually implemented for `RegionKind` because `std::mem::discriminant`
1139 // returns an opaque value that is `PartialEq` but not `PartialOrd`
1140 #[inline]
1141 const fn regionkind_discriminant<I: Interner>(value: &RegionKind<I>) -> usize {
1142     match value {
1143         ReEarlyBound(_) => 0,
1144         ReLateBound(_, _) => 1,
1145         ReFree(_) => 2,
1146         ReStatic => 3,
1147         ReVar(_) => 4,
1148         RePlaceholder(_) => 5,
1149         ReEmpty(_) => 6,
1150         ReErased => 7,
1151     }
1152 }
1153
1154 // This is manually implemented because a derive would require `I: Copy`
1155 impl<I: Interner> Copy for RegionKind<I>
1156 where
1157     I::EarlyBoundRegion: Copy,
1158     I::BoundRegion: Copy,
1159     I::FreeRegion: Copy,
1160     I::RegionVid: Copy,
1161     I::PlaceholderRegion: Copy,
1162 {
1163 }
1164
1165 // This is manually implemented because a derive would require `I: Clone`
1166 impl<I: Interner> Clone for RegionKind<I> {
1167     fn clone(&self) -> Self {
1168         match self {
1169             ReEarlyBound(a) => ReEarlyBound(a.clone()),
1170             ReLateBound(a, b) => ReLateBound(a.clone(), b.clone()),
1171             ReFree(a) => ReFree(a.clone()),
1172             ReStatic => ReStatic,
1173             ReVar(a) => ReVar(a.clone()),
1174             RePlaceholder(a) => RePlaceholder(a.clone()),
1175             ReEmpty(a) => ReEmpty(a.clone()),
1176             ReErased => ReErased,
1177         }
1178     }
1179 }
1180
1181 // This is manually implemented because a derive would require `I: PartialEq`
1182 impl<I: Interner> PartialEq for RegionKind<I> {
1183     #[inline]
1184     fn eq(&self, other: &RegionKind<I>) -> bool {
1185         let __self_vi = regionkind_discriminant(self);
1186         let __arg_1_vi = regionkind_discriminant(other);
1187         if __self_vi == __arg_1_vi {
1188             match (&*self, &*other) {
1189                 (&ReEarlyBound(ref __self_0), &ReEarlyBound(ref __arg_1_0)) => {
1190                     __self_0 == __arg_1_0
1191                 }
1192                 (
1193                     &ReLateBound(ref __self_0, ref __self_1),
1194                     &ReLateBound(ref __arg_1_0, ref __arg_1_1),
1195                 ) => __self_0 == __arg_1_0 && __self_1 == __arg_1_1,
1196                 (&ReFree(ref __self_0), &ReFree(ref __arg_1_0)) => __self_0 == __arg_1_0,
1197                 (&ReStatic, &ReStatic) => true,
1198                 (&ReVar(ref __self_0), &ReVar(ref __arg_1_0)) => __self_0 == __arg_1_0,
1199                 (&RePlaceholder(ref __self_0), &RePlaceholder(ref __arg_1_0)) => {
1200                     __self_0 == __arg_1_0
1201                 }
1202                 (&ReEmpty(ref __self_0), &ReEmpty(ref __arg_1_0)) => __self_0 == __arg_1_0,
1203                 (&ReErased, &ReErased) => true,
1204                 _ => true,
1205             }
1206         } else {
1207             false
1208         }
1209     }
1210 }
1211
1212 // This is manually implemented because a derive would require `I: Eq`
1213 impl<I: Interner> Eq for RegionKind<I> {}
1214
1215 // This is manually implemented because a derive would require `I: PartialOrd`
1216 impl<I: Interner> PartialOrd for RegionKind<I> {
1217     #[inline]
1218     fn partial_cmp(&self, other: &RegionKind<I>) -> Option<Ordering> {
1219         Some(Ord::cmp(self, other))
1220     }
1221 }
1222
1223 // This is manually implemented because a derive would require `I: Ord`
1224 impl<I: Interner> Ord for RegionKind<I> {
1225     #[inline]
1226     fn cmp(&self, other: &RegionKind<I>) -> Ordering {
1227         let __self_vi = regionkind_discriminant(self);
1228         let __arg_1_vi = regionkind_discriminant(other);
1229         if __self_vi == __arg_1_vi {
1230             match (&*self, &*other) {
1231                 (&ReEarlyBound(ref __self_0), &ReEarlyBound(ref __arg_1_0)) => {
1232                     Ord::cmp(__self_0, __arg_1_0)
1233                 }
1234                 (
1235                     &ReLateBound(ref __self_0, ref __self_1),
1236                     &ReLateBound(ref __arg_1_0, ref __arg_1_1),
1237                 ) => match Ord::cmp(__self_0, __arg_1_0) {
1238                     Ordering::Equal => Ord::cmp(__self_1, __arg_1_1),
1239                     cmp => cmp,
1240                 },
1241                 (&ReFree(ref __self_0), &ReFree(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
1242                 (&ReStatic, &ReStatic) => Ordering::Equal,
1243                 (&ReVar(ref __self_0), &ReVar(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
1244                 (&RePlaceholder(ref __self_0), &RePlaceholder(ref __arg_1_0)) => {
1245                     Ord::cmp(__self_0, __arg_1_0)
1246                 }
1247                 (&ReEmpty(ref __self_0), &ReEmpty(ref __arg_1_0)) => Ord::cmp(__self_0, __arg_1_0),
1248                 (&ReErased, &ReErased) => Ordering::Equal,
1249                 _ => Ordering::Equal,
1250             }
1251         } else {
1252             Ord::cmp(&__self_vi, &__arg_1_vi)
1253         }
1254     }
1255 }
1256
1257 // This is manually implemented because a derive would require `I: Hash`
1258 impl<I: Interner> hash::Hash for RegionKind<I> {
1259     fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () {
1260         match (&*self,) {
1261             (&ReEarlyBound(ref __self_0),) => {
1262                 hash::Hash::hash(&regionkind_discriminant(self), state);
1263                 hash::Hash::hash(__self_0, state)
1264             }
1265             (&ReLateBound(ref __self_0, ref __self_1),) => {
1266                 hash::Hash::hash(&regionkind_discriminant(self), state);
1267                 hash::Hash::hash(__self_0, state);
1268                 hash::Hash::hash(__self_1, state)
1269             }
1270             (&ReFree(ref __self_0),) => {
1271                 hash::Hash::hash(&regionkind_discriminant(self), state);
1272                 hash::Hash::hash(__self_0, state)
1273             }
1274             (&ReStatic,) => {
1275                 hash::Hash::hash(&regionkind_discriminant(self), state);
1276             }
1277             (&ReVar(ref __self_0),) => {
1278                 hash::Hash::hash(&regionkind_discriminant(self), state);
1279                 hash::Hash::hash(__self_0, state)
1280             }
1281             (&RePlaceholder(ref __self_0),) => {
1282                 hash::Hash::hash(&regionkind_discriminant(self), state);
1283                 hash::Hash::hash(__self_0, state)
1284             }
1285             (&ReEmpty(ref __self_0),) => {
1286                 hash::Hash::hash(&regionkind_discriminant(self), state);
1287                 hash::Hash::hash(__self_0, state)
1288             }
1289             (&ReErased,) => {
1290                 hash::Hash::hash(&regionkind_discriminant(self), state);
1291             }
1292         }
1293     }
1294 }
1295
1296 // This is manually implemented because a derive would require `I: Debug`
1297 impl<I: Interner> fmt::Debug for RegionKind<I> {
1298     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1299         match self {
1300             ReEarlyBound(ref data) => write!(f, "ReEarlyBound({:?})", data),
1301
1302             ReLateBound(binder_id, ref bound_region) => {
1303                 write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region)
1304             }
1305
1306             ReFree(ref fr) => fr.fmt(f),
1307
1308             ReStatic => write!(f, "ReStatic"),
1309
1310             ReVar(ref vid) => vid.fmt(f),
1311
1312             RePlaceholder(placeholder) => write!(f, "RePlaceholder({:?})", placeholder),
1313
1314             ReEmpty(ui) => write!(f, "ReEmpty({:?})", ui),
1315
1316             ReErased => write!(f, "ReErased"),
1317         }
1318     }
1319 }
1320
1321 // This is manually implemented because a derive would require `I: Encodable`
1322 impl<I: Interner, E: TyEncoder> Encodable<E> for RegionKind<I>
1323 where
1324     I::EarlyBoundRegion: Encodable<E>,
1325     I::BoundRegion: Encodable<E>,
1326     I::FreeRegion: Encodable<E>,
1327     I::RegionVid: Encodable<E>,
1328     I::PlaceholderRegion: Encodable<E>,
1329 {
1330     fn encode(&self, e: &mut E) {
1331         let disc = regionkind_discriminant(self);
1332         match self {
1333             ReEarlyBound(a) => e.emit_enum_variant(disc, |e| {
1334                 a.encode(e);
1335             }),
1336             ReLateBound(a, b) => e.emit_enum_variant(disc, |e| {
1337                 a.encode(e);
1338                 b.encode(e);
1339             }),
1340             ReFree(a) => e.emit_enum_variant(disc, |e| {
1341                 a.encode(e);
1342             }),
1343             ReStatic => e.emit_enum_variant(disc, |_| {}),
1344             ReVar(a) => e.emit_enum_variant(disc, |e| {
1345                 a.encode(e);
1346             }),
1347             RePlaceholder(a) => e.emit_enum_variant(disc, |e| {
1348                 a.encode(e);
1349             }),
1350             ReEmpty(a) => e.emit_enum_variant(disc, |e| {
1351                 a.encode(e);
1352             }),
1353             ReErased => e.emit_enum_variant(disc, |_| {}),
1354         }
1355     }
1356 }
1357
1358 // This is manually implemented because a derive would require `I: Decodable`
1359 impl<I: Interner, D: TyDecoder<I = I>> Decodable<D> for RegionKind<I>
1360 where
1361     I::EarlyBoundRegion: Decodable<D>,
1362     I::BoundRegion: Decodable<D>,
1363     I::FreeRegion: Decodable<D>,
1364     I::RegionVid: Decodable<D>,
1365     I::PlaceholderRegion: Decodable<D>,
1366 {
1367     fn decode(d: &mut D) -> Self {
1368         match Decoder::read_usize(d) {
1369             0 => ReEarlyBound(Decodable::decode(d)),
1370             1 => ReLateBound(Decodable::decode(d), Decodable::decode(d)),
1371             2 => ReFree(Decodable::decode(d)),
1372             3 => ReStatic,
1373             4 => ReVar(Decodable::decode(d)),
1374             5 => RePlaceholder(Decodable::decode(d)),
1375             6 => ReEmpty(Decodable::decode(d)),
1376             7 => ReErased,
1377             _ => panic!(
1378                 "{}",
1379                 format!(
1380                     "invalid enum variant tag while decoding `{}`, expected 0..{}",
1381                     "RegionKind", 8,
1382                 )
1383             ),
1384         }
1385     }
1386 }
1387
1388 // This is not a derived impl because a derive would require `I: HashStable`
1389 impl<CTX, I: Interner> HashStable<CTX> for RegionKind<I>
1390 where
1391     I::EarlyBoundRegion: HashStable<CTX>,
1392     I::BoundRegion: HashStable<CTX>,
1393     I::FreeRegion: HashStable<CTX>,
1394     I::RegionVid: HashStable<CTX>,
1395     I::PlaceholderRegion: HashStable<CTX>,
1396 {
1397     #[inline]
1398     fn hash_stable(
1399         &self,
1400         hcx: &mut CTX,
1401         hasher: &mut rustc_data_structures::stable_hasher::StableHasher,
1402     ) {
1403         std::mem::discriminant(self).hash_stable(hcx, hasher);
1404         match self {
1405             ReErased | ReStatic => {
1406                 // No variant fields to hash for these ...
1407             }
1408             ReEmpty(universe) => {
1409                 universe.hash_stable(hcx, hasher);
1410             }
1411             ReLateBound(db, br) => {
1412                 db.hash_stable(hcx, hasher);
1413                 br.hash_stable(hcx, hasher);
1414             }
1415             ReEarlyBound(eb) => {
1416                 eb.hash_stable(hcx, hasher);
1417             }
1418             ReFree(ref free_region) => {
1419                 free_region.hash_stable(hcx, hasher);
1420             }
1421             RePlaceholder(p) => {
1422                 p.hash_stable(hcx, hasher);
1423             }
1424             ReVar(reg) => {
1425                 reg.hash_stable(hcx, hasher);
1426             }
1427         }
1428     }
1429 }