1 use crate::ty::{self, FloatVarValue, InferConst, IntVarValue, Ty, TyCtxt};
2 use rustc_data_structures::snapshot_vec;
3 use rustc_data_structures::undo_log::UndoLogs;
4 use rustc_data_structures::unify::{
5 self, EqUnifyValue, InPlace, NoError, UnificationTable, UnifyKey, UnifyValue,
7 use rustc_span::symbol::Symbol;
8 use rustc_span::{Span, DUMMY_SP};
11 use std::marker::PhantomData;
14 fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
17 /// Raw `TyVid` are used as the unification key for `sub_relations`;
18 /// they carry no values.
19 impl UnifyKey for ty::TyVid {
21 fn index(&self) -> u32 {
24 fn from_index(i: u32) -> ty::TyVid {
25 ty::TyVid { index: i }
27 fn tag() -> &'static str {
32 impl UnifyKey for ty::IntVid {
33 type Value = Option<IntVarValue>;
34 fn index(&self) -> u32 {
37 fn from_index(i: u32) -> ty::IntVid {
38 ty::IntVid { index: i }
40 fn tag() -> &'static str {
45 impl EqUnifyValue for IntVarValue {}
47 #[derive(PartialEq, Copy, Clone, Debug)]
48 pub struct RegionVidKey {
49 /// The minimum region vid in the unification set. This is needed
50 /// to have a canonical name for a type to prevent infinite
52 pub min_vid: ty::RegionVid,
55 impl UnifyValue for RegionVidKey {
58 fn unify_values(value1: &Self, value2: &Self) -> Result<Self, NoError> {
59 let min_vid = if value1.min_vid.index() < value2.min_vid.index() {
65 Ok(RegionVidKey { min_vid })
69 impl UnifyKey for ty::RegionVid {
70 type Value = RegionVidKey;
71 fn index(&self) -> u32 {
74 fn from_index(i: u32) -> ty::RegionVid {
75 ty::RegionVid::from(i)
77 fn tag() -> &'static str {
82 impl ToType for IntVarValue {
83 fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
85 ty::IntType(i) => tcx.mk_mach_int(i),
86 ty::UintType(i) => tcx.mk_mach_uint(i),
91 // Floating point type keys
93 impl UnifyKey for ty::FloatVid {
94 type Value = Option<FloatVarValue>;
95 fn index(&self) -> u32 {
98 fn from_index(i: u32) -> ty::FloatVid {
99 ty::FloatVid { index: i }
101 fn tag() -> &'static str {
106 impl EqUnifyValue for FloatVarValue {}
108 impl ToType for FloatVarValue {
109 fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
110 tcx.mk_mach_float(self.0)
116 #[derive(Copy, Clone, Debug)]
117 pub struct ConstVariableOrigin {
118 pub kind: ConstVariableOriginKind,
122 /// Reasons to create a const inference variable
123 #[derive(Copy, Clone, Debug)]
124 pub enum ConstVariableOriginKind {
127 ConstParameterDefinition(Symbol),
128 SubstitutionPlaceholder,
131 #[derive(Copy, Clone, Debug)]
132 pub enum ConstVariableValue<'tcx> {
133 Known { value: &'tcx ty::Const<'tcx> },
134 Unknown { universe: ty::UniverseIndex },
137 impl<'tcx> ConstVariableValue<'tcx> {
138 /// If this value is known, returns the const it is known to be.
139 /// Otherwise, `None`.
140 pub fn known(&self) -> Option<&'tcx ty::Const<'tcx>> {
142 ConstVariableValue::Unknown { .. } => None,
143 ConstVariableValue::Known { value } => Some(value),
147 pub fn is_unknown(&self) -> bool {
149 ConstVariableValue::Unknown { .. } => true,
150 ConstVariableValue::Known { .. } => false,
155 #[derive(Copy, Clone, Debug)]
156 pub struct ConstVarValue<'tcx> {
157 pub origin: ConstVariableOrigin,
158 pub val: ConstVariableValue<'tcx>,
161 impl<'tcx> UnifyKey for ty::ConstVid<'tcx> {
162 type Value = ConstVarValue<'tcx>;
163 fn index(&self) -> u32 {
166 fn from_index(i: u32) -> Self {
167 ty::ConstVid { index: i, phantom: PhantomData }
169 fn tag() -> &'static str {
174 impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
175 type Error = (&'tcx ty::Const<'tcx>, &'tcx ty::Const<'tcx>);
177 fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
178 let val = match (value1.val, value2.val) {
179 (ConstVariableValue::Known { .. }, ConstVariableValue::Known { .. }) => {
180 bug!("equating two const variables, both of which have known values")
183 // If one side is known, prefer that one.
184 (ConstVariableValue::Known { .. }, ConstVariableValue::Unknown { .. }) => {
187 (ConstVariableValue::Unknown { .. }, ConstVariableValue::Known { .. }) => {
191 // If both sides are *unknown*, it hardly matters, does it?
193 ConstVariableValue::Unknown { universe: universe1 },
194 ConstVariableValue::Unknown { universe: universe2 },
196 // If we unify two unbound variables, ?T and ?U, then whatever
197 // value they wind up taking (which must be the same value) must
198 // be nameable by both universes. Therefore, the resulting
199 // universe is the minimum of the two universes, because that is
200 // the one which contains the fewest names in scope.
201 let universe = cmp::min(universe1, universe2);
202 Ok(ConstVariableValue::Unknown { universe })
207 origin: ConstVariableOrigin {
208 kind: ConstVariableOriginKind::ConstInference,
216 impl<'tcx> EqUnifyValue for &'tcx ty::Const<'tcx> {}
218 pub fn replace_if_possible<V, L>(
219 table: &mut UnificationTable<InPlace<ty::ConstVid<'tcx>, V, L>>,
220 c: &'tcx ty::Const<'tcx>,
221 ) -> &'tcx ty::Const<'tcx>
223 V: snapshot_vec::VecLike<unify::Delegate<ty::ConstVid<'tcx>>>,
224 L: UndoLogs<snapshot_vec::UndoLog<unify::Delegate<ty::ConstVid<'tcx>>>>,
226 if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = c {
227 match table.probe_value(*vid).val.known() {