]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/unify_key.rs
Remove Ty prefix from Ty{Adt|Array|Slice|RawPtr|Ref|FnDef|FnPtr|Dynamic|Closure|Gener...
[rust.git] / src / librustc / infer / unify_key.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt};
12 use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue};
13
14 pub trait ToType {
15     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
16 }
17
18 impl UnifyKey for ty::IntVid {
19     type Value = Option<IntVarValue>;
20     fn index(&self) -> u32 { self.index }
21     fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
22     fn tag() -> &'static str { "IntVid" }
23 }
24
25 impl EqUnifyValue for IntVarValue {
26 }
27
28 #[derive(PartialEq, Copy, Clone, Debug)]
29 pub struct RegionVidKey {
30     /// The minimum region vid in the unification set. This is needed
31     /// to have a canonical name for a type to prevent infinite
32     /// recursion.
33     pub min_vid: ty::RegionVid
34 }
35
36 impl UnifyValue for RegionVidKey {
37     type Error = NoError;
38
39     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
40         let min_vid = if value1.min_vid.index() < value2.min_vid.index() {
41             value1.min_vid
42         } else {
43             value2.min_vid
44         };
45
46         Ok(RegionVidKey { min_vid: min_vid })
47     }
48 }
49
50 impl UnifyKey for ty::RegionVid {
51     type Value = RegionVidKey;
52     fn index(&self) -> u32 { self.0 }
53     fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid(i) }
54     fn tag() -> &'static str { "RegionVid" }
55 }
56
57 impl ToType for IntVarValue {
58     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
59         match *self {
60             ty::IntType(i) => tcx.mk_mach_int(i),
61             ty::UintType(i) => tcx.mk_mach_uint(i),
62         }
63     }
64 }
65
66 // Floating point type keys
67
68 impl UnifyKey for ty::FloatVid {
69     type Value = Option<FloatVarValue>;
70     fn index(&self) -> u32 { self.index }
71     fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
72     fn tag() -> &'static str { "FloatVid" }
73 }
74
75 impl EqUnifyValue for FloatVarValue {
76 }
77
78 impl ToType for FloatVarValue {
79     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
80         tcx.mk_mach_float(self.0)
81     }
82 }