]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/unify_key.rs
Rollup merge of #61557 - alexcrichton:build-less, r=pietroalbini
[rust.git] / src / librustc / infer / unify_key.rs
1 use crate::ty::{self, FloatVarValue, IntVarValue, Ty, TyCtxt, InferConst};
2 use crate::mir::interpret::ConstValue;
3 use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue, UnificationTable};
4 use rustc_data_structures::unify::InPlace;
5 use syntax_pos::{Span, DUMMY_SP};
6 use syntax::symbol::InternedString;
7
8 use std::cmp;
9 use std::marker::PhantomData;
10 use std::cell::RefMut;
11
12 pub trait ToType {
13     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
14 }
15
16 impl UnifyKey for ty::IntVid {
17     type Value = Option<IntVarValue>;
18     fn index(&self) -> u32 { self.index }
19     fn from_index(i: u32) -> ty::IntVid { ty::IntVid { index: i } }
20     fn tag() -> &'static str { "IntVid" }
21 }
22
23 impl EqUnifyValue for IntVarValue {}
24
25 #[derive(PartialEq, Copy, Clone, Debug)]
26 pub struct RegionVidKey {
27     /// The minimum region vid in the unification set. This is needed
28     /// to have a canonical name for a type to prevent infinite
29     /// recursion.
30     pub min_vid: ty::RegionVid
31 }
32
33 impl UnifyValue for RegionVidKey {
34     type Error = NoError;
35
36     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
37         let min_vid = if value1.min_vid.index() < value2.min_vid.index() {
38             value1.min_vid
39         } else {
40             value2.min_vid
41         };
42
43         Ok(RegionVidKey { min_vid })
44     }
45 }
46
47 impl UnifyKey for ty::RegionVid {
48     type Value = RegionVidKey;
49     fn index(&self) -> u32 { u32::from(*self) }
50     fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid::from(i) }
51     fn tag() -> &'static str { "RegionVid" }
52 }
53
54 impl ToType for IntVarValue {
55     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
56         match *self {
57             ty::IntType(i) => tcx.mk_mach_int(i),
58             ty::UintType(i) => tcx.mk_mach_uint(i),
59         }
60     }
61 }
62
63 // Floating point type keys
64
65 impl UnifyKey for ty::FloatVid {
66     type Value = Option<FloatVarValue>;
67     fn index(&self) -> u32 { self.index }
68     fn from_index(i: u32) -> ty::FloatVid { ty::FloatVid { index: i } }
69     fn tag() -> &'static str { "FloatVid" }
70 }
71
72 impl EqUnifyValue for FloatVarValue {}
73
74 impl ToType for FloatVarValue {
75     fn to_type<'a, 'gcx, 'tcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
76         tcx.mk_mach_float(self.0)
77     }
78 }
79
80 // Generic consts.
81
82 #[derive(Copy, Clone, Debug)]
83 pub struct ConstVariableOrigin {
84     pub kind: ConstVariableOriginKind,
85     pub span: Span,
86 }
87
88 /// Reasons to create a const inference variable
89 #[derive(Copy, Clone, Debug)]
90 pub enum ConstVariableOriginKind {
91     MiscVariable,
92     ConstInference,
93     ConstParameterDefinition(InternedString),
94     SubstitutionPlaceholder,
95 }
96
97 #[derive(Copy, Clone, Debug)]
98 pub enum ConstVariableValue<'tcx> {
99     Known { value: &'tcx ty::Const<'tcx> },
100     Unknown { universe: ty::UniverseIndex },
101 }
102
103 impl<'tcx> ConstVariableValue<'tcx> {
104     /// If this value is known, returns the const it is known to be.
105     /// Otherwise, `None`.
106     pub fn known(&self) -> Option<&'tcx ty::Const<'tcx>> {
107         match *self {
108             ConstVariableValue::Unknown { .. } => None,
109             ConstVariableValue::Known { value } => Some(value),
110         }
111     }
112
113     pub fn is_unknown(&self) -> bool {
114         match *self {
115             ConstVariableValue::Unknown { .. } => true,
116             ConstVariableValue::Known { .. } => false,
117         }
118     }
119 }
120
121 #[derive(Copy, Clone, Debug)]
122 pub struct ConstVarValue<'tcx> {
123     pub origin: ConstVariableOrigin,
124     pub val: ConstVariableValue<'tcx>,
125 }
126
127 impl<'tcx> UnifyKey for ty::ConstVid<'tcx> {
128     type Value = ConstVarValue<'tcx>;
129     fn index(&self) -> u32 { self.index }
130     fn from_index(i: u32) -> Self { ty::ConstVid { index: i, phantom: PhantomData } }
131     fn tag() -> &'static str { "ConstVid" }
132 }
133
134 impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
135     type Error = (&'tcx ty::Const<'tcx>, &'tcx ty::Const<'tcx>);
136
137     fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
138         let val = match (value1.val, value2.val) {
139             (
140                 ConstVariableValue::Known { .. },
141                 ConstVariableValue::Known { .. }
142             ) => {
143                 bug!("equating two const variables, both of which have known values")
144             }
145
146             // If one side is known, prefer that one.
147             (ConstVariableValue::Known { .. }, ConstVariableValue::Unknown { .. }) => {
148                 Ok(value1.val)
149             }
150             (ConstVariableValue::Unknown { .. }, ConstVariableValue::Known { .. }) => {
151                 Ok(value2.val)
152             }
153
154             // If both sides are *unknown*, it hardly matters, does it?
155             (ConstVariableValue::Unknown { universe: universe1 },
156              ConstVariableValue::Unknown { universe: universe2 }) =>  {
157                 // If we unify two unbound variables, ?T and ?U, then whatever
158                 // value they wind up taking (which must be the same value) must
159                 // be nameable by both universes. Therefore, the resulting
160                 // universe is the minimum of the two universes, because that is
161                 // the one which contains the fewest names in scope.
162                 let universe = cmp::min(universe1, universe2);
163                 Ok(ConstVariableValue::Unknown { universe })
164             }
165         }?;
166
167         Ok(ConstVarValue {
168             origin: ConstVariableOrigin {
169                 kind: ConstVariableOriginKind::ConstInference,
170                 span: DUMMY_SP,
171             },
172             val,
173         })
174     }
175 }
176
177 impl<'tcx> EqUnifyValue for &'tcx ty::Const<'tcx> {}
178
179 pub fn replace_if_possible(
180     mut table: RefMut<'_, UnificationTable<InPlace<ty::ConstVid<'tcx>>>>,
181     c: &'tcx ty::Const<'tcx>
182 ) -> &'tcx ty::Const<'tcx> {
183     if let ty::Const { val: ConstValue::Infer(InferConst::Var(vid)), .. } = c {
184         match table.probe_value(*vid).val.known() {
185             Some(c) => c,
186             None => c,
187         }
188     } else {
189         c
190     }
191 }