]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/unify_key.rs
Rollup merge of #58272 - fitzgen:num-format-code-size, r=Mark-Simulacrum
[rust.git] / src / librustc / infer / unify_key.rs
1 use crate::ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt};
2 use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue};
3
4 pub trait ToType {
5     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
6 }
7
8 impl UnifyKey for ty::IntVid {
9     type Value = Option<IntVarValue>;
10     fn index(&self) -> u32 { self.index }
11     fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
12     fn tag() -> &'static str { "IntVid" }
13 }
14
15 impl EqUnifyValue for IntVarValue {
16 }
17
18 #[derive(PartialEq, Copy, Clone, Debug)]
19 pub struct RegionVidKey {
20     /// The minimum region vid in the unification set. This is needed
21     /// to have a canonical name for a type to prevent infinite
22     /// recursion.
23     pub min_vid: ty::RegionVid
24 }
25
26 impl UnifyValue for RegionVidKey {
27     type Error = NoError;
28
29     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
30         let min_vid = if value1.min_vid.index() < value2.min_vid.index() {
31             value1.min_vid
32         } else {
33             value2.min_vid
34         };
35
36         Ok(RegionVidKey { min_vid })
37     }
38 }
39
40 impl UnifyKey for ty::RegionVid {
41     type Value = RegionVidKey;
42     fn index(&self) -> u32 { u32::from(*self) }
43     fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid::from(i) }
44     fn tag() -> &'static str { "RegionVid" }
45 }
46
47 impl ToType for IntVarValue {
48     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
49         match *self {
50             ty::IntType(i) => tcx.mk_mach_int(i),
51             ty::UintType(i) => tcx.mk_mach_uint(i),
52         }
53     }
54 }
55
56 // Floating point type keys
57
58 impl UnifyKey for ty::FloatVid {
59     type Value = Option<FloatVarValue>;
60     fn index(&self) -> u32 { self.index }
61     fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
62     fn tag() -> &'static str { "FloatVid" }
63 }
64
65 impl EqUnifyValue for FloatVarValue {
66 }
67
68 impl ToType for FloatVarValue {
69     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
70         tcx.mk_mach_float(self.0)
71     }
72 }