]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/unify_key.rs
Drive-by comment fixes
[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 #[derive(PartialEq, Copy, Clone, Debug)]
18 pub struct RegionVidKey {
19     /// The minimum region vid in the unification set. This is needed
20     /// to have a canonical name for a type to prevent infinite
21     /// recursion.
22     pub min_vid: ty::RegionVid
23 }
24
25 impl UnifyValue for RegionVidKey {
26     type Error = NoError;
27
28     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
29         let min_vid = if value1.min_vid.index() < value2.min_vid.index() {
30             value1.min_vid
31         } else {
32             value2.min_vid
33         };
34
35         Ok(RegionVidKey { min_vid })
36     }
37 }
38
39 impl UnifyKey for ty::RegionVid {
40     type Value = RegionVidKey;
41     fn index(&self) -> u32 { u32::from(*self) }
42     fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid::from(i) }
43     fn tag() -> &'static str { "RegionVid" }
44 }
45
46 impl ToType for IntVarValue {
47     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
48         match *self {
49             ty::IntType(i) => tcx.mk_mach_int(i),
50             ty::UintType(i) => tcx.mk_mach_uint(i),
51         }
52     }
53 }
54
55 // Floating point type keys
56
57 impl UnifyKey for ty::FloatVid {
58     type Value = Option<FloatVarValue>;
59     fn index(&self) -> u32 { self.index }
60     fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
61     fn tag() -> &'static str { "FloatVid" }
62 }
63
64 impl EqUnifyValue for FloatVarValue {}
65
66 impl ToType for FloatVarValue {
67     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
68         tcx.mk_mach_float(self.0)
69     }
70 }