1 #![feature(associated_type_defaults)]
2 #![feature(fmt_helpers_for_derive)]
3 #![feature(min_specialization)]
4 #![feature(never_type)]
5 #![feature(rustc_attrs)]
6 #![feature(unwrap_infallible)]
7 #![deny(rustc::untranslatable_diagnostic)]
8 #![deny(rustc::diagnostic_outside_of_impl)]
9
10 #[macro_use]
11 extern crate bitflags;
12 #[macro_use]
13 extern crate rustc_macros;
14
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
16 use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
17 use smallvec::SmallVec;
18 use std::fmt;
19 use std::fmt::Debug;
20 use std::hash::Hash;
21 use std::mem::discriminant;
22
23 pub mod codec;
24 pub mod fold;
25 pub mod sty;
26 pub mod ty_info;
27 pub mod visit;
28
29 #[macro_use]
30 mod macros;
31 mod structural_impls;
32
33 pub use codec::*;
34 pub use sty::*;
35 pub use ty_info::*;
36
37 /// Needed so we can use #[derive(HashStable_Generic)]
38 pub trait HashStableContext {}
39
40 pub trait Interner: Sized {
41 type AdtDef: Clone + Debug + Hash + Ord;
42 type SubstsRef: Clone + Debug + Hash + Ord;
43 type DefId: Clone + Debug + Hash + Ord;
44 type Binder<T>;
45 type Ty: Clone + Debug + Hash + Ord;
46 type Const: Clone + Debug + Hash + Ord;
47 type Region: Clone + Debug + Hash + Ord;
48 type Predicate;
49 type TypeAndMut: Clone + Debug + Hash + Ord;
50 type Mutability: Clone + Debug + Hash + Ord;
51 type Movability: Clone + Debug + Hash + Ord;
52 type PolyFnSig: Clone + Debug + Hash + Ord;
53 type ListBinderExistentialPredicate: Clone + Debug + Hash + Ord;
54 type BinderListTy: Clone + Debug + Hash + Ord;
55 type ListTy: Clone + Debug + Hash + Ord + IntoIterator<Item = Self::Ty>;
56 type AliasTy: Clone + Debug + Hash + Ord;
57 type ParamTy: Clone + Debug + Hash + Ord;
58 type BoundTy: Clone + Debug + Hash + Ord;
59 type PlaceholderType: Clone + Debug + Hash + Ord;
60 type ErrorGuaranteed: Clone + Debug + Hash + Ord;
61 type PredicateKind: Clone + Debug + Hash + PartialEq + Eq;
62 type AllocId: Clone + Debug + Hash + Ord;
63
64 type InferConst: Clone + Debug + Hash + Ord;
65 type AliasConst: Clone + Debug + Hash + Ord;
66 type PlaceholderConst: Clone + Debug + Hash + Ord;
67 type ParamConst: Clone + Debug + Hash + Ord;
68 type BoundConst: Clone + Debug + Hash + Ord;
69 type InferTy: Clone + Debug + Hash + Ord;
70 type ValueConst: Clone + Debug + Hash + Ord;
71 type ExprConst: Clone + Debug + Hash + Ord;
72
73 type EarlyBoundRegion: Clone + Debug + Hash + Ord;
74 type BoundRegion: Clone + Debug + Hash + Ord;
75 type FreeRegion: Clone + Debug + Hash + Ord;
76 type RegionVid: Clone + Debug + Hash + Ord;
77 type PlaceholderRegion: Clone + Debug + Hash + Ord;
78
ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Self::Mutability)79 fn ty_and_mut_to_parts(ty_and_mut: Self::TypeAndMut) -> (Self::Ty, Self::Mutability);
mutability_is_mut(mutbl: Self::Mutability) -> bool80 fn mutability_is_mut(mutbl: Self::Mutability) -> bool;
81 }
82
83 /// Imagine you have a function `F: FnOnce(&[T]) -> R`, plus an iterator `iter`
84 /// that produces `T` items. You could combine them with
85 /// `f(&iter.collect::<Vec<_>>())`, but this requires allocating memory for the
86 /// `Vec`.
87 ///
88 /// This trait allows for faster implementations, intended for cases where the
89 /// number of items produced by the iterator is small. There is a blanket impl
90 /// for `T` items, but there is also a fallible impl for `Result<T, E>` items.
91 pub trait CollectAndApply<T, R>: Sized {
92 type Output;
93
94 /// Produce a result of type `Self::Output` from `iter`. The result will
95 /// typically be produced by applying `f` on the elements produced by
96 /// `iter`, though this may not happen in some impls, e.g. if an error
97 /// occurred during iteration.
collect_and_apply<I, F>(iter: I, f: F) -> Self::Output where I: Iterator<Item = Self>, F: FnOnce(&[T]) -> R98 fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
99 where
100 I: Iterator<Item = Self>,
101 F: FnOnce(&[T]) -> R;
102 }
103
104 /// The blanket impl that always collects all elements and applies `f`.
105 impl<T, R> CollectAndApply<T, R> for T {
106 type Output = R;
107
108 /// Equivalent to `f(&iter.collect::<Vec<_>>())`.
collect_and_apply<I, F>(mut iter: I, f: F) -> R where I: Iterator<Item = T>, F: FnOnce(&[T]) -> R,109 fn collect_and_apply<I, F>(mut iter: I, f: F) -> R
110 where
111 I: Iterator<Item = T>,
112 F: FnOnce(&[T]) -> R,
113 {
114 // This code is hot enough that it's worth specializing for the most
115 // common length lists, to avoid the overhead of `SmallVec` creation.
116 // Lengths 0, 1, and 2 typically account for ~95% of cases. If
117 // `size_hint` is incorrect a panic will occur via an `unwrap` or an
118 // `assert`.
119 match iter.size_hint() {
120 (0, Some(0)) => {
121 assert!(iter.next().is_none());
122 f(&[])
123 }
124 (1, Some(1)) => {
125 let t0 = iter.next().unwrap();
126 assert!(iter.next().is_none());
127 f(&[t0])
128 }
129 (2, Some(2)) => {
130 let t0 = iter.next().unwrap();
131 let t1 = iter.next().unwrap();
132 assert!(iter.next().is_none());
133 f(&[t0, t1])
134 }
135 _ => f(&iter.collect::<SmallVec<[_; 8]>>()),
136 }
137 }
138 }
139
140 /// A fallible impl that will fail, without calling `f`, if there are any
141 /// errors during collection.
142 impl<T, R, E> CollectAndApply<T, R> for Result<T, E> {
143 type Output = Result<R, E>;
144
145 /// Equivalent to `Ok(f(&iter.collect::<Result<Vec<_>>>()?))`.
collect_and_apply<I, F>(mut iter: I, f: F) -> Result<R, E> where I: Iterator<Item = Result<T, E>>, F: FnOnce(&[T]) -> R,146 fn collect_and_apply<I, F>(mut iter: I, f: F) -> Result<R, E>
147 where
148 I: Iterator<Item = Result<T, E>>,
149 F: FnOnce(&[T]) -> R,
150 {
151 // This code is hot enough that it's worth specializing for the most
152 // common length lists, to avoid the overhead of `SmallVec` creation.
153 // Lengths 0, 1, and 2 typically account for ~95% of cases. If
154 // `size_hint` is incorrect a panic will occur via an `unwrap` or an
155 // `assert`, unless a failure happens first, in which case the result
156 // will be an error anyway.
157 Ok(match iter.size_hint() {
158 (0, Some(0)) => {
159 assert!(iter.next().is_none());
160 f(&[])
161 }
162 (1, Some(1)) => {
163 let t0 = iter.next().unwrap()?;
164 assert!(iter.next().is_none());
165 f(&[t0])
166 }
167 (2, Some(2)) => {
168 let t0 = iter.next().unwrap()?;
169 let t1 = iter.next().unwrap()?;
170 assert!(iter.next().is_none());
171 f(&[t0, t1])
172 }
173 _ => f(&iter.collect::<Result<SmallVec<[_; 8]>, _>>()?),
174 })
175 }
176 }
177
178 bitflags! {
179 /// Flags that we track on types. These flags are propagated upwards
180 /// through the type during type construction, so that we can quickly check
181 /// whether the type has various kinds of types in it without recursing
182 /// over the type itself.
183 pub struct TypeFlags: u32 {
184 // Does this have parameters? Used to determine whether substitution is
185 // required.
186 /// Does this have `Param`?
187 const HAS_TY_PARAM = 1 << 0;
188 /// Does this have `ReEarlyBound`?
189 const HAS_RE_PARAM = 1 << 1;
190 /// Does this have `ConstKind::Param`?
191 const HAS_CT_PARAM = 1 << 2;
192
193 const HAS_PARAM = TypeFlags::HAS_TY_PARAM.bits
194 | TypeFlags::HAS_RE_PARAM.bits
195 | TypeFlags::HAS_CT_PARAM.bits;
196
197 /// Does this have `Infer`?
198 const HAS_TY_INFER = 1 << 3;
199 /// Does this have `ReVar`?
200 const HAS_RE_INFER = 1 << 4;
201 /// Does this have `ConstKind::Infer`?
202 const HAS_CT_INFER = 1 << 5;
203
204 /// Does this have inference variables? Used to determine whether
205 /// inference is required.
206 const HAS_INFER = TypeFlags::HAS_TY_INFER.bits
207 | TypeFlags::HAS_RE_INFER.bits
208 | TypeFlags::HAS_CT_INFER.bits;
209
210 /// Does this have `Placeholder`?
211 const HAS_TY_PLACEHOLDER = 1 << 6;
212 /// Does this have `RePlaceholder`?
213 const HAS_RE_PLACEHOLDER = 1 << 7;
214 /// Does this have `ConstKind::Placeholder`?
215 const HAS_CT_PLACEHOLDER = 1 << 8;
216
217 /// `true` if there are "names" of regions and so forth
218 /// that are local to a particular fn/inferctxt
219 const HAS_FREE_LOCAL_REGIONS = 1 << 9;
220
221 /// `true` if there are "names" of types and regions and so forth
222 /// that are local to a particular fn
223 const HAS_FREE_LOCAL_NAMES = TypeFlags::HAS_TY_PARAM.bits
224 | TypeFlags::HAS_CT_PARAM.bits
225 | TypeFlags::HAS_TY_INFER.bits
226 | TypeFlags::HAS_CT_INFER.bits
227 | TypeFlags::HAS_TY_PLACEHOLDER.bits
228 | TypeFlags::HAS_CT_PLACEHOLDER.bits
229 // We consider 'freshened' types and constants
230 // to depend on a particular fn.
231 // The freshening process throws away information,
232 // which can make things unsuitable for use in a global
233 // cache. Note that there is no 'fresh lifetime' flag -
234 // freshening replaces all lifetimes with `ReErased`,
235 // which is different from how types/const are freshened.
236 | TypeFlags::HAS_TY_FRESH.bits
237 | TypeFlags::HAS_CT_FRESH.bits
238 | TypeFlags::HAS_FREE_LOCAL_REGIONS.bits
239 | TypeFlags::HAS_RE_ERASED.bits;
240
241 /// Does this have `Projection`?
242 const HAS_TY_PROJECTION = 1 << 10;
243 /// Does this have `Inherent`?
244 const HAS_TY_INHERENT = 1 << 11;
245 /// Does this have `Opaque`?
246 const HAS_TY_OPAQUE = 1 << 12;
247 /// Does this have `ConstKind::Unevaluated`?
248 const HAS_CT_PROJECTION = 1 << 13;
249
250 /// Could this type be normalized further?
251 const HAS_PROJECTION = TypeFlags::HAS_TY_PROJECTION.bits
252 | TypeFlags::HAS_TY_OPAQUE.bits
253 | TypeFlags::HAS_TY_INHERENT.bits
254 | TypeFlags::HAS_CT_PROJECTION.bits;
255
256 /// Is an error type/const reachable?
257 const HAS_ERROR = 1 << 14;
258
259 /// Does this have any region that "appears free" in the type?
260 /// Basically anything but `ReLateBound` and `ReErased`.
261 const HAS_FREE_REGIONS = 1 << 15;
262
263 /// Does this have any `ReLateBound` regions?
264 const HAS_RE_LATE_BOUND = 1 << 16;
265 /// Does this have any `Bound` types?
266 const HAS_TY_LATE_BOUND = 1 << 17;
267 /// Does this have any `ConstKind::Bound` consts?
268 const HAS_CT_LATE_BOUND = 1 << 18;
269 /// Does this have any bound variables?
270 /// Used to check if a global bound is safe to evaluate.
271 const HAS_LATE_BOUND = TypeFlags::HAS_RE_LATE_BOUND.bits
272 | TypeFlags::HAS_TY_LATE_BOUND.bits
273 | TypeFlags::HAS_CT_LATE_BOUND.bits;
274
275 /// Does this have any `ReErased` regions?
276 const HAS_RE_ERASED = 1 << 19;
277
278 /// Does this value have parameters/placeholders/inference variables which could be
279 /// replaced later, in a way that would change the results of `impl` specialization?
280 const STILL_FURTHER_SPECIALIZABLE = 1 << 20;
281
282 /// Does this value have `InferTy::FreshTy/FreshIntTy/FreshFloatTy`?
283 const HAS_TY_FRESH = 1 << 21;
284
285 /// Does this value have `InferConst::Fresh`?
286 const HAS_CT_FRESH = 1 << 22;
287
288 /// Does this have `Generator` or `GeneratorWitness`?
289 const HAS_TY_GENERATOR = 1 << 23;
290 }
291 }
292
293 rustc_index::newtype_index! {
294 /// A [De Bruijn index][dbi] is a standard means of representing
295 /// regions (and perhaps later types) in a higher-ranked setting. In
296 /// particular, imagine a type like this:
297 /// ```ignore (illustrative)
298 /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
299 /// // ^ ^ | | |
300 /// // | | | | |
301 /// // | +------------+ 0 | |
302 /// // | | |
303 /// // +----------------------------------+ 1 |
304 /// // | |
305 /// // +----------------------------------------------+ 0
306 /// ```
307 /// In this type, there are two binders (the outer fn and the inner
308 /// fn). We need to be able to determine, for any given region, which
309 /// fn type it is bound by, the inner or the outer one. There are
310 /// various ways you can do this, but a De Bruijn index is one of the
311 /// more convenient and has some nice properties. The basic idea is to
312 /// count the number of binders, inside out. Some examples should help
313 /// clarify what I mean.
314 ///
315 /// Let's start with the reference type `&'b isize` that is the first
316 /// argument to the inner function. This region `'b` is assigned a De
317 /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
318 /// fn). The region `'a` that appears in the second argument type (`&'a
319 /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
320 /// second-innermost binder". (These indices are written on the arrows
321 /// in the diagram).
322 ///
323 /// What is interesting is that De Bruijn index attached to a particular
324 /// variable will vary depending on where it appears. For example,
325 /// the final type `&'a char` also refers to the region `'a` declared on
326 /// the outermost fn. But this time, this reference is not nested within
327 /// any other binders (i.e., it is not an argument to the inner fn, but
328 /// rather the outer one). Therefore, in this case, it is assigned a
329 /// De Bruijn index of 0, because the innermost binder in that location
330 /// is the outer fn.
331 ///
332 /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
333 #[derive(HashStable_Generic)]
334 #[debug_format = "DebruijnIndex({})"]
335 pub struct DebruijnIndex {
336 const INNERMOST = 0;
337 }
338 }
339
340 impl DebruijnIndex {
341 /// Returns the resulting index when this value is moved into
342 /// `amount` number of new binders. So, e.g., if you had
343 ///
344 /// for<'a> fn(&'a x)
345 ///
346 /// and you wanted to change it to
347 ///
348 /// for<'a> fn(for<'b> fn(&'a x))
349 ///
350 /// you would need to shift the index for `'a` into a new binder.
351 #[inline]
352 #[must_use]
shifted_in(self, amount: u32) -> DebruijnIndex353 pub fn shifted_in(self, amount: u32) -> DebruijnIndex {
354 DebruijnIndex::from_u32(self.as_u32() + amount)
355 }
356
357 /// Update this index in place by shifting it "in" through
358 /// `amount` number of binders.
359 #[inline]
shift_in(&mut self, amount: u32)360 pub fn shift_in(&mut self, amount: u32) {
361 *self = self.shifted_in(amount);
362 }
363
364 /// Returns the resulting index when this value is moved out from
365 /// `amount` number of new binders.
366 #[inline]
367 #[must_use]
shifted_out(self, amount: u32) -> DebruijnIndex368 pub fn shifted_out(self, amount: u32) -> DebruijnIndex {
369 DebruijnIndex::from_u32(self.as_u32() - amount)
370 }
371
372 /// Update in place by shifting out from `amount` binders.
373 #[inline]
shift_out(&mut self, amount: u32)374 pub fn shift_out(&mut self, amount: u32) {
375 *self = self.shifted_out(amount);
376 }
377
378 /// Adjusts any De Bruijn indices so as to make `to_binder` the
379 /// innermost binder. That is, if we have something bound at `to_binder`,
380 /// it will now be bound at INNERMOST. This is an appropriate thing to do
381 /// when moving a region out from inside binders:
382 ///
383 /// ```ignore (illustrative)
384 /// for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
385 /// // Binder: D3 D2 D1 ^^
386 /// ```
387 ///
388 /// Here, the region `'a` would have the De Bruijn index D3,
389 /// because it is the bound 3 binders out. However, if we wanted
390 /// to refer to that region `'a` in the second argument (the `_`),
391 /// those two binders would not be in scope. In that case, we
392 /// might invoke `shift_out_to_binder(D3)`. This would adjust the
393 /// De Bruijn index of `'a` to D1 (the innermost binder).
394 ///
395 /// If we invoke `shift_out_to_binder` and the region is in fact
396 /// bound by one of the binders we are shifting out of, that is an
397 /// error (and should fail an assertion failure).
398 #[inline]
shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self399 pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self {
400 self.shifted_out(to_binder.as_u32() - INNERMOST.as_u32())
401 }
402 }
403
debug_bound_var<T: std::fmt::Write>( fmt: &mut T, debruijn: DebruijnIndex, var: impl std::fmt::Debug, ) -> Result<(), std::fmt::Error>404 pub fn debug_bound_var<T: std::fmt::Write>(
405 fmt: &mut T,
406 debruijn: DebruijnIndex,
407 var: impl std::fmt::Debug,
408 ) -> Result<(), std::fmt::Error> {
409 if debruijn == INNERMOST {
410 write!(fmt, "^{:?}", var)
411 } else {
412 write!(fmt, "^{}_{:?}", debruijn.index(), var)
413 }
414 }
415
416 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
417 #[derive(Encodable, Decodable, HashStable_Generic)]
418 pub enum IntTy {
419 Isize,
420 I8,
421 I16,
422 I32,
423 I64,
424 I128,
425 }
426
427 impl IntTy {
name_str(&self) -> &'static str428 pub fn name_str(&self) -> &'static str {
429 match *self {
430 IntTy::Isize => "isize",
431 IntTy::I8 => "i8",
432 IntTy::I16 => "i16",
433 IntTy::I32 => "i32",
434 IntTy::I64 => "i64",
435 IntTy::I128 => "i128",
436 }
437 }
438
bit_width(&self) -> Option<u64>439 pub fn bit_width(&self) -> Option<u64> {
440 Some(match *self {
441 IntTy::Isize => return None,
442 IntTy::I8 => 8,
443 IntTy::I16 => 16,
444 IntTy::I32 => 32,
445 IntTy::I64 => 64,
446 IntTy::I128 => 128,
447 })
448 }
449
normalize(&self, target_width: u32) -> Self450 pub fn normalize(&self, target_width: u32) -> Self {
451 match self {
452 IntTy::Isize => match target_width {
453 16 => IntTy::I16,
454 32 => IntTy::I32,
455 64 => IntTy::I64,
456 _ => unreachable!(),
457 },
458 _ => *self,
459 }
460 }
461
to_unsigned(self) -> UintTy462 pub fn to_unsigned(self) -> UintTy {
463 match self {
464 IntTy::Isize => UintTy::Usize,
465 IntTy::I8 => UintTy::U8,
466 IntTy::I16 => UintTy::U16,
467 IntTy::I32 => UintTy::U32,
468 IntTy::I64 => UintTy::U64,
469 IntTy::I128 => UintTy::U128,
470 }
471 }
472 }
473
474 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
475 #[derive(Encodable, Decodable, HashStable_Generic)]
476 pub enum UintTy {
477 Usize,
478 U8,
479 U16,
480 U32,
481 U64,
482 U128,
483 }
484
485 impl UintTy {
name_str(&self) -> &'static str486 pub fn name_str(&self) -> &'static str {
487 match *self {
488 UintTy::Usize => "usize",
489 UintTy::U8 => "u8",
490 UintTy::U16 => "u16",
491 UintTy::U32 => "u32",
492 UintTy::U64 => "u64",
493 UintTy::U128 => "u128",
494 }
495 }
496
bit_width(&self) -> Option<u64>497 pub fn bit_width(&self) -> Option<u64> {
498 Some(match *self {
499 UintTy::Usize => return None,
500 UintTy::U8 => 8,
501 UintTy::U16 => 16,
502 UintTy::U32 => 32,
503 UintTy::U64 => 64,
504 UintTy::U128 => 128,
505 })
506 }
507
normalize(&self, target_width: u32) -> Self508 pub fn normalize(&self, target_width: u32) -> Self {
509 match self {
510 UintTy::Usize => match target_width {
511 16 => UintTy::U16,
512 32 => UintTy::U32,
513 64 => UintTy::U64,
514 _ => unreachable!(),
515 },
516 _ => *self,
517 }
518 }
519
to_signed(self) -> IntTy520 pub fn to_signed(self) -> IntTy {
521 match self {
522 UintTy::Usize => IntTy::Isize,
523 UintTy::U8 => IntTy::I8,
524 UintTy::U16 => IntTy::I16,
525 UintTy::U32 => IntTy::I32,
526 UintTy::U64 => IntTy::I64,
527 UintTy::U128 => IntTy::I128,
528 }
529 }
530 }
531
532 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
533 #[derive(Encodable, Decodable, HashStable_Generic)]
534 pub enum FloatTy {
535 F32,
536 F64,
537 }
538
539 impl FloatTy {
name_str(self) -> &'static str540 pub fn name_str(self) -> &'static str {
541 match self {
542 FloatTy::F32 => "f32",
543 FloatTy::F64 => "f64",
544 }
545 }
546
bit_width(self) -> u64547 pub fn bit_width(self) -> u64 {
548 match self {
549 FloatTy::F32 => 32,
550 FloatTy::F64 => 64,
551 }
552 }
553 }
554
555 #[derive(Clone, Copy, PartialEq, Eq)]
556 pub enum IntVarValue {
557 IntType(IntTy),
558 UintType(UintTy),
559 }
560
561 #[derive(Clone, Copy, PartialEq, Eq)]
562 pub struct FloatVarValue(pub FloatTy);
563
564 rustc_index::newtype_index! {
565 /// A **ty**pe **v**ariable **ID**.
566 #[debug_format = "?{}t"]
567 pub struct TyVid {}
568 }
569
570 /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
571 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
572 pub struct IntVid {
573 pub index: u32,
574 }
575
576 /// An **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
577 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
578 pub struct FloatVid {
579 pub index: u32,
580 }
581
582 /// A placeholder for a type that hasn't been inferred yet.
583 ///
584 /// E.g., if we have an empty array (`[]`), then we create a fresh
585 /// type variable for the element type since we won't know until it's
586 /// used what the element type is supposed to be.
587 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
588 pub enum InferTy {
589 /// A type variable.
590 TyVar(TyVid),
591 /// An integral type variable (`{integer}`).
592 ///
593 /// These are created when the compiler sees an integer literal like
594 /// `1` that could be several different types (`u8`, `i32`, `u32`, etc.).
595 /// We don't know until it's used what type it's supposed to be, so
596 /// we create a fresh type variable.
597 IntVar(IntVid),
598 /// A floating-point type variable (`{float}`).
599 ///
600 /// These are created when the compiler sees an float literal like
601 /// `1.0` that could be either an `f32` or an `f64`.
602 /// We don't know until it's used what type it's supposed to be, so
603 /// we create a fresh type variable.
604 FloatVar(FloatVid),
605
606 /// A [`FreshTy`][Self::FreshTy] is one that is generated as a replacement
607 /// for an unbound type variable. This is convenient for caching etc. See
608 /// `rustc_infer::infer::freshen` for more details.
609 ///
610 /// Compare with [`TyVar`][Self::TyVar].
611 FreshTy(u32),
612 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`IntVar`][Self::IntVar].
613 FreshIntTy(u32),
614 /// Like [`FreshTy`][Self::FreshTy], but as a replacement for [`FloatVar`][Self::FloatVar].
615 FreshFloatTy(u32),
616 }
617
618 /// Raw `TyVid` are used as the unification key for `sub_relations`;
619 /// they carry no values.
620 impl UnifyKey for TyVid {
621 type Value = ();
622 #[inline]
index(&self) -> u32623 fn index(&self) -> u32 {
624 self.as_u32()
625 }
626 #[inline]
from_index(i: u32) -> TyVid627 fn from_index(i: u32) -> TyVid {
628 TyVid::from_u32(i)
629 }
tag() -> &'static str630 fn tag() -> &'static str {
631 "TyVid"
632 }
633 }
634
635 impl EqUnifyValue for IntVarValue {}
636
637 impl UnifyKey for IntVid {
638 type Value = Option<IntVarValue>;
639 #[inline] // make this function eligible for inlining - it is quite hot.
index(&self) -> u32640 fn index(&self) -> u32 {
641 self.index
642 }
643 #[inline]
from_index(i: u32) -> IntVid644 fn from_index(i: u32) -> IntVid {
645 IntVid { index: i }
646 }
tag() -> &'static str647 fn tag() -> &'static str {
648 "IntVid"
649 }
650 }
651
652 impl EqUnifyValue for FloatVarValue {}
653
654 impl UnifyKey for FloatVid {
655 type Value = Option<FloatVarValue>;
656 #[inline]
index(&self) -> u32657 fn index(&self) -> u32 {
658 self.index
659 }
660 #[inline]
from_index(i: u32) -> FloatVid661 fn from_index(i: u32) -> FloatVid {
662 FloatVid { index: i }
663 }
tag() -> &'static str664 fn tag() -> &'static str {
665 "FloatVid"
666 }
667 }
668
669 #[derive(Copy, Clone, PartialEq, Eq, Decodable, Encodable, Hash, HashStable_Generic)]
670 #[rustc_pass_by_value]
671 pub enum Variance {
672 Covariant, // T<A> <: T<B> iff A <: B -- e.g., function return type
673 Invariant, // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
674 Contravariant, // T<A> <: T<B> iff B <: A -- e.g., function param type
675 Bivariant, // T<A> <: T<B> -- e.g., unused type parameter
676 }
677
678 impl Variance {
679 /// `a.xform(b)` combines the variance of a context with the
680 /// variance of a type with the following meaning. If we are in a
681 /// context with variance `a`, and we encounter a type argument in
682 /// a position with variance `b`, then `a.xform(b)` is the new
683 /// variance with which the argument appears.
684 ///
685 /// Example 1:
686 /// ```ignore (illustrative)
687 /// *mut Vec<i32>
688 /// ```
689 /// Here, the "ambient" variance starts as covariant. `*mut T` is
690 /// invariant with respect to `T`, so the variance in which the
691 /// `Vec<i32>` appears is `Covariant.xform(Invariant)`, which
692 /// yields `Invariant`. Now, the type `Vec<T>` is covariant with
693 /// respect to its type argument `T`, and hence the variance of
694 /// the `i32` here is `Invariant.xform(Covariant)`, which results
695 /// (again) in `Invariant`.
696 ///
697 /// Example 2:
698 /// ```ignore (illustrative)
699 /// fn(*const Vec<i32>, *mut Vec<i32)
700 /// ```
701 /// The ambient variance is covariant. A `fn` type is
702 /// contravariant with respect to its parameters, so the variance
703 /// within which both pointer types appear is
704 /// `Covariant.xform(Contravariant)`, or `Contravariant`. `*const
705 /// T` is covariant with respect to `T`, so the variance within
706 /// which the first `Vec<i32>` appears is
707 /// `Contravariant.xform(Covariant)` or `Contravariant`. The same
708 /// is true for its `i32` argument. In the `*mut T` case, the
709 /// variance of `Vec<i32>` is `Contravariant.xform(Invariant)`,
710 /// and hence the outermost type is `Invariant` with respect to
711 /// `Vec<i32>` (and its `i32` argument).
712 ///
713 /// Source: Figure 1 of "Taming the Wildcards:
714 /// Combining Definition- and Use-Site Variance" published in PLDI'11.
xform(self, v: Variance) -> Variance715 pub fn xform(self, v: Variance) -> Variance {
716 match (self, v) {
717 // Figure 1, column 1.
718 (Variance::Covariant, Variance::Covariant) => Variance::Covariant,
719 (Variance::Covariant, Variance::Contravariant) => Variance::Contravariant,
720 (Variance::Covariant, Variance::Invariant) => Variance::Invariant,
721 (Variance::Covariant, Variance::Bivariant) => Variance::Bivariant,
722
723 // Figure 1, column 2.
724 (Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
725 (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
726 (Variance::Contravariant, Variance::Invariant) => Variance::Invariant,
727 (Variance::Contravariant, Variance::Bivariant) => Variance::Bivariant,
728
729 // Figure 1, column 3.
730 (Variance::Invariant, _) => Variance::Invariant,
731
732 // Figure 1, column 4.
733 (Variance::Bivariant, _) => Variance::Bivariant,
734 }
735 }
736 }
737
738 impl<CTX> HashStable<CTX> for InferTy {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)739 fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
740 use InferTy::*;
741 discriminant(self).hash_stable(ctx, hasher);
742 match self {
743 TyVar(_) | IntVar(_) | FloatVar(_) => {
744 panic!("type variables should not be hashed: {self:?}")
745 }
746 FreshTy(v) | FreshIntTy(v) | FreshFloatTy(v) => v.hash_stable(ctx, hasher),
747 }
748 }
749 }
750
751 impl fmt::Debug for IntVarValue {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result752 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
753 match *self {
754 IntVarValue::IntType(ref v) => v.fmt(f),
755 IntVarValue::UintType(ref v) => v.fmt(f),
756 }
757 }
758 }
759
760 impl fmt::Debug for FloatVarValue {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result761 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
762 self.0.fmt(f)
763 }
764 }
765
766 impl fmt::Debug for IntVid {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result767 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
768 write!(f, "?{}i", self.index)
769 }
770 }
771
772 impl fmt::Debug for FloatVid {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result773 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
774 write!(f, "?{}f", self.index)
775 }
776 }
777
778 impl fmt::Debug for InferTy {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result779 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
780 use InferTy::*;
781 match *self {
782 TyVar(ref v) => v.fmt(f),
783 IntVar(ref v) => v.fmt(f),
784 FloatVar(ref v) => v.fmt(f),
785 FreshTy(v) => write!(f, "FreshTy({v:?})"),
786 FreshIntTy(v) => write!(f, "FreshIntTy({v:?})"),
787 FreshFloatTy(v) => write!(f, "FreshFloatTy({v:?})"),
788 }
789 }
790 }
791
792 impl fmt::Debug for Variance {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result793 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
794 f.write_str(match *self {
795 Variance::Covariant => "+",
796 Variance::Contravariant => "-",
797 Variance::Invariant => "o",
798 Variance::Bivariant => "*",
799 })
800 }
801 }
802
803 impl fmt::Display for InferTy {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result804 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
805 use InferTy::*;
806 match *self {
807 TyVar(_) => write!(f, "_"),
808 IntVar(_) => write!(f, "{}", "{integer}"),
809 FloatVar(_) => write!(f, "{}", "{float}"),
810 FreshTy(v) => write!(f, "FreshTy({v})"),
811 FreshIntTy(v) => write!(f, "FreshIntTy({v})"),
812 FreshFloatTy(v) => write!(f, "FreshFloatTy({v})"),
813 }
814 }
815 }
816
817 rustc_index::newtype_index! {
818 /// "Universes" are used during type- and trait-checking in the
819 /// presence of `for<..>` binders to control what sets of names are
820 /// visible. Universes are arranged into a tree: the root universe
821 /// contains names that are always visible. Each child then adds a new
822 /// set of names that are visible, in addition to those of its parent.
823 /// We say that the child universe "extends" the parent universe with
824 /// new names.
825 ///
826 /// To make this more concrete, consider this program:
827 ///
828 /// ```ignore (illustrative)
829 /// struct Foo { }
830 /// fn bar<T>(x: T) {
831 /// let y: for<'a> fn(&'a u8, Foo) = ...;
832 /// }
833 /// ```
834 ///
835 /// The struct name `Foo` is in the root universe U0. But the type
836 /// parameter `T`, introduced on `bar`, is in an extended universe U1
837 /// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
838 /// of `bar`, we cannot name `T`. Then, within the type of `y`, the
839 /// region `'a` is in a universe U2 that extends U1, because we can
840 /// name it inside the fn type but not outside.
841 ///
842 /// Universes are used to do type- and trait-checking around these
843 /// "forall" binders (also called **universal quantification**). The
844 /// idea is that when, in the body of `bar`, we refer to `T` as a
845 /// type, we aren't referring to any type in particular, but rather a
846 /// kind of "fresh" type that is distinct from all other types we have
847 /// actually declared. This is called a **placeholder** type, and we
848 /// use universes to talk about this. In other words, a type name in
849 /// universe 0 always corresponds to some "ground" type that the user
850 /// declared, but a type name in a non-zero universe is a placeholder
851 /// type -- an idealized representative of "types in general" that we
852 /// use for checking generic functions.
853 #[derive(HashStable_Generic)]
854 #[debug_format = "U{}"]
855 pub struct UniverseIndex {}
856 }
857
858 impl UniverseIndex {
859 pub const ROOT: UniverseIndex = UniverseIndex::from_u32(0);
860
861 /// Returns the "next" universe index in order -- this new index
862 /// is considered to extend all previous universes. This
863 /// corresponds to entering a `forall` quantifier. So, for
864 /// example, suppose we have this type in universe `U`:
865 ///
866 /// ```ignore (illustrative)
867 /// for<'a> fn(&'a u32)
868 /// ```
869 ///
870 /// Once we "enter" into this `for<'a>` quantifier, we are in a
871 /// new universe that extends `U` -- in this new universe, we can
872 /// name the region `'a`, but that region was not nameable from
873 /// `U` because it was not in scope there.
next_universe(self) -> UniverseIndex874 pub fn next_universe(self) -> UniverseIndex {
875 UniverseIndex::from_u32(self.private.checked_add(1).unwrap())
876 }
877
878 /// Returns `true` if `self` can name a name from `other` -- in other words,
879 /// if the set of names in `self` is a superset of those in
880 /// `other` (`self >= other`).
can_name(self, other: UniverseIndex) -> bool881 pub fn can_name(self, other: UniverseIndex) -> bool {
882 self.private >= other.private
883 }
884
885 /// Returns `true` if `self` cannot name some names from `other` -- in other
886 /// words, if the set of names in `self` is a strict subset of
887 /// those in `other` (`self < other`).
cannot_name(self, other: UniverseIndex) -> bool888 pub fn cannot_name(self, other: UniverseIndex) -> bool {
889 self.private < other.private
890 }
891 }
892