1 use rustc_hir::def_id::DefId;
2 use rustc_middle::ty::{self, Ty, TyVid};
3 use rustc_span::symbol::Symbol;
6 use crate::infer::InferCtxtUndoLogs;
8 use rustc_data_structures::snapshot_vec as sv;
9 use rustc_data_structures::unify as ut;
11 use std::marker::PhantomData;
14 use rustc_data_structures::undo_log::{Rollback, UndoLogs};
16 /// Represents a single undo-able action that affects a type inference variable.
17 pub(crate) enum UndoLog<'tcx> {
18 EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
19 SubRelation(sv::UndoLog<ut::Delegate<ty::TyVid>>),
20 Values(sv::UndoLog<Delegate>),
23 /// Convert from a specific kind of undo to the more general UndoLog
24 impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for UndoLog<'tcx> {
25 fn from(l: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) -> Self {
26 UndoLog::EqRelation(l)
30 /// Convert from a specific kind of undo to the more general UndoLog
31 impl<'tcx> From<sv::UndoLog<ut::Delegate<ty::TyVid>>> for UndoLog<'tcx> {
32 fn from(l: sv::UndoLog<ut::Delegate<ty::TyVid>>) -> Self {
33 UndoLog::SubRelation(l)
37 /// Convert from a specific kind of undo to the more general UndoLog
38 impl<'tcx> From<sv::UndoLog<Delegate>> for UndoLog<'tcx> {
39 fn from(l: sv::UndoLog<Delegate>) -> Self {
44 /// Convert from a specific kind of undo to the more general UndoLog
45 impl<'tcx> From<Instantiate> for UndoLog<'tcx> {
46 fn from(l: Instantiate) -> Self {
47 UndoLog::Values(sv::UndoLog::Other(l))
51 impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
52 fn reverse(&mut self, undo: UndoLog<'tcx>) {
54 UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
55 UndoLog::SubRelation(undo) => self.sub_relations.reverse(undo),
56 UndoLog::Values(undo) => self.values.reverse(undo),
61 pub struct TypeVariableStorage<'tcx> {
62 values: sv::SnapshotVecStorage<Delegate>,
64 /// Two variables are unified in `eq_relations` when we have a
65 /// constraint `?X == ?Y`. This table also stores, for each key,
67 eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
69 /// Two variables are unified in `sub_relations` when we have a
70 /// constraint `?X <: ?Y` *or* a constraint `?Y <: ?X`. This second
71 /// table exists only to help with the occurs check. In particular,
72 /// we want to report constraints like these as an occurs check
78 /// This works because `?1` and `?3` are unified in the
79 /// `sub_relations` relation (not in `eq_relations`). Then when we
80 /// process the `Box<?3> <: ?1` constraint, we do an occurs check
81 /// on `Box<?3>` and find a potential cycle.
83 /// This is reasonable because, in Rust, subtypes have the same
84 /// "skeleton" and hence there is no possible type such that
85 /// (e.g.) `Box<?3> <: ?3` for any `?3`.
86 sub_relations: ut::UnificationTableStorage<ty::TyVid>,
89 pub struct TypeVariableTable<'a, 'tcx> {
90 values: &'a mut sv::SnapshotVecStorage<Delegate>,
92 eq_relations: &'a mut ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
94 sub_relations: &'a mut ut::UnificationTableStorage<ty::TyVid>,
96 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
99 #[derive(Copy, Clone, Debug)]
100 pub struct TypeVariableOrigin {
101 pub kind: TypeVariableOriginKind,
105 /// Reasons to create a type inference variable
106 #[derive(Copy, Clone, Debug)]
107 pub enum TypeVariableOriginKind {
109 NormalizeProjectionType,
111 TypeParameterDefinition(Symbol, Option<DefId>),
113 /// One of the upvars or closure kind parameters in a `ClosureSubsts`
114 /// (before it has been determined).
115 // FIXME(eddyb) distinguish upvar inference variables from the rest.
117 SubstitutionPlaceholder,
124 pub(crate) struct TypeVariableData {
125 origin: TypeVariableOrigin,
129 #[derive(Copy, Clone, Debug)]
130 pub enum TypeVariableValue<'tcx> {
131 Known { value: Ty<'tcx> },
132 Unknown { universe: ty::UniverseIndex },
135 impl<'tcx> TypeVariableValue<'tcx> {
136 /// If this value is known, returns the type it is known to be.
137 /// Otherwise, `None`.
138 pub fn known(&self) -> Option<Ty<'tcx>> {
140 TypeVariableValue::Unknown { .. } => None,
141 TypeVariableValue::Known { value } => Some(value),
145 pub fn is_unknown(&self) -> bool {
147 TypeVariableValue::Unknown { .. } => true,
148 TypeVariableValue::Known { .. } => false,
153 pub(crate) struct Instantiate {
157 pub(crate) struct Delegate;
159 impl<'tcx> TypeVariableStorage<'tcx> {
160 pub fn new() -> TypeVariableStorage<'tcx> {
161 TypeVariableStorage {
162 values: sv::SnapshotVecStorage::new(),
163 eq_relations: ut::UnificationTableStorage::new(),
164 sub_relations: ut::UnificationTableStorage::new(),
168 pub(crate) fn with_log<'a>(
170 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
171 ) -> TypeVariableTable<'a, 'tcx> {
172 let TypeVariableStorage { values, eq_relations, sub_relations } = self;
173 TypeVariableTable { values, eq_relations, sub_relations, undo_log }
177 impl<'tcx> TypeVariableTable<'_, 'tcx> {
178 /// Returns the diverges flag given when `vid` was created.
180 /// Note that this function does not return care whether
181 /// `vid` has been unified with something else or not.
182 pub fn var_diverges(&self, vid: ty::TyVid) -> bool {
183 self.values.get(vid.index as usize).diverging
186 /// Returns the origin that was given when `vid` was created.
188 /// Note that this function does not return care whether
189 /// `vid` has been unified with something else or not.
190 pub fn var_origin(&self, vid: ty::TyVid) -> &TypeVariableOrigin {
191 &self.values.get(vid.index as usize).origin
194 /// Records that `a == b`, depending on `dir`.
196 /// Precondition: neither `a` nor `b` are known.
197 pub fn equate(&mut self, a: ty::TyVid, b: ty::TyVid) {
198 debug_assert!(self.probe(a).is_unknown());
199 debug_assert!(self.probe(b).is_unknown());
200 self.eq_relations().union(a, b);
201 self.sub_relations().union(a, b);
204 /// Records that `a <: b`, depending on `dir`.
206 /// Precondition: neither `a` nor `b` are known.
207 pub fn sub(&mut self, a: ty::TyVid, b: ty::TyVid) {
208 debug_assert!(self.probe(a).is_unknown());
209 debug_assert!(self.probe(b).is_unknown());
210 self.sub_relations().union(a, b);
213 /// Instantiates `vid` with the type `ty`.
215 /// Precondition: `vid` must not have been previously instantiated.
216 pub fn instantiate(&mut self, vid: ty::TyVid, ty: Ty<'tcx>) {
217 let vid = self.root_var(vid);
218 debug_assert!(self.probe(vid).is_unknown());
220 self.eq_relations().probe_value(vid).is_unknown(),
221 "instantiating type variable `{:?}` twice: new-value = {:?}, old-value={:?}",
224 self.eq_relations().probe_value(vid)
226 self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
228 // Hack: we only need this so that `types_escaping_snapshot`
229 // can see what has been unified; see the Delegate impl for
231 self.undo_log.push(Instantiate { vid });
234 /// Creates a new type variable.
236 /// - `diverging`: indicates if this is a "diverging" type
237 /// variable, e.g., one created as the type of a `return`
238 /// expression. The code in this module doesn't care if a
239 /// variable is diverging, but the main Rust type-checker will
240 /// sometimes "unify" such variables with the `!` or `()` types.
241 /// - `origin`: indicates *why* the type variable was created.
242 /// The code in this module doesn't care, but it can be useful
243 /// for improving error messages.
246 universe: ty::UniverseIndex,
248 origin: TypeVariableOrigin,
250 let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
252 let sub_key = self.sub_relations().new_key(());
253 assert_eq!(eq_key.vid, sub_key);
255 let index = self.values().push(TypeVariableData { origin, diverging });
256 assert_eq!(eq_key.vid.index, index as u32);
259 "new_var(index={:?}, universe={:?}, diverging={:?}, origin={:?}",
260 eq_key.vid, universe, diverging, origin,
266 /// Returns the number of type variables created thus far.
267 pub fn num_vars(&self) -> usize {
271 /// Returns the "root" variable of `vid` in the `eq_relations`
272 /// equivalence table. All type variables that have been equated
273 /// will yield the same root variable (per the union-find
274 /// algorithm), so `root_var(a) == root_var(b)` implies that `a ==
275 /// b` (transitively).
276 pub fn root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
277 self.eq_relations().find(vid).vid
280 /// Returns the "root" variable of `vid` in the `sub_relations`
281 /// equivalence table. All type variables that have been are
282 /// related via equality or subtyping will yield the same root
283 /// variable (per the union-find algorithm), so `sub_root_var(a)
284 /// == sub_root_var(b)` implies that:
286 /// exists X. (a <: X || X <: a) && (b <: X || X <: b)
287 pub fn sub_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
288 self.sub_relations().find(vid)
291 /// Returns `true` if `a` and `b` have same "sub-root" (i.e., exists some
292 /// type X such that `forall i in {a, b}. (i <: X || X <: i)`.
293 pub fn sub_unified(&mut self, a: ty::TyVid, b: ty::TyVid) -> bool {
294 self.sub_root_var(a) == self.sub_root_var(b)
297 /// Retrieves the type to which `vid` has been instantiated, if
299 pub fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
300 self.inlined_probe(vid)
303 /// An always-inlined variant of `probe`, for very hot call sites.
305 pub fn inlined_probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {
306 self.eq_relations().inlined_probe_value(vid)
309 /// If `t` is a type-inference variable, and it has been
310 /// instantiated, then return the with which it was
311 /// instantiated. Otherwise, returns `t`.
312 pub fn replace_if_possible(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
314 ty::Infer(ty::TyVar(v)) => match self.probe(v) {
315 TypeVariableValue::Unknown { .. } => t,
316 TypeVariableValue::Known { value } => value,
324 ) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut InferCtxtUndoLogs<'tcx>> {
325 self.values.with_log(self.undo_log)
328 fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
329 self.eq_relations.with_log(self.undo_log)
332 fn sub_relations(&mut self) -> super::UnificationTable<'_, 'tcx, ty::TyVid> {
333 self.sub_relations.with_log(self.undo_log)
336 /// Returns a range of the type variables created during the snapshot.
337 pub fn vars_since_snapshot(
340 ) -> (Range<TyVid>, Vec<TypeVariableOrigin>) {
341 let range = TyVid { index: value_count as u32 }..TyVid { index: self.num_vars() as u32 };
343 range.start..range.end,
344 (range.start.index..range.end.index)
345 .map(|index| self.values.get(index as usize).origin)
350 /// Finds the set of type variables that existed *before* `s`
351 /// but which have only been unified since `s` started, and
352 /// return the types with which they were unified. So if we had
353 /// a type variable `V0`, then we started the snapshot, then we
354 /// created a type variable `V1`, unified `V0` with `T0`, and
355 /// unified `V1` with `T1`, this function would return `{T0}`.
356 pub fn types_escaping_snapshot(&mut self, s: &super::Snapshot<'tcx>) -> Vec<Ty<'tcx>> {
357 let mut new_elem_threshold = u32::MAX;
358 let mut escaping_types = Vec::new();
359 let actions_since_snapshot = self.undo_log.actions_since_snapshot(s);
360 debug!("actions_since_snapshot.len() = {}", actions_since_snapshot.len());
361 for i in 0..actions_since_snapshot.len() {
362 let actions_since_snapshot = self.undo_log.actions_since_snapshot(s);
363 match actions_since_snapshot[i] {
364 super::UndoLog::TypeVariables(UndoLog::Values(sv::UndoLog::NewElem(index))) => {
365 // if any new variables were created during the
366 // snapshot, remember the lower index (which will
367 // always be the first one we see). Note that this
368 // action must precede those variables being
370 new_elem_threshold = cmp::min(new_elem_threshold, index as u32);
371 debug!("NewElem({}) new_elem_threshold={}", index, new_elem_threshold);
374 super::UndoLog::TypeVariables(UndoLog::Values(sv::UndoLog::Other(
375 Instantiate { vid, .. },
377 if vid.index < new_elem_threshold {
378 // quick check to see if this variable was
379 // created since the snapshot started or not.
380 let mut eq_relations = ut::UnificationTable::with_log(
381 &mut *self.eq_relations,
384 let escaping_type = match eq_relations.probe_value(vid) {
385 TypeVariableValue::Unknown { .. } => bug!(),
386 TypeVariableValue::Known { value } => value,
388 escaping_types.push(escaping_type);
390 debug!("SpecifyVar({:?}) new_elem_threshold={}", vid, new_elem_threshold);
400 /// Returns indices of all variables that are not yet
402 pub fn unsolved_variables(&mut self) -> Vec<ty::TyVid> {
403 (0..self.values.len())
405 let vid = ty::TyVid { index: i as u32 };
406 match self.probe(vid) {
407 TypeVariableValue::Unknown { .. } => Some(vid),
408 TypeVariableValue::Known { .. } => None,
415 impl sv::SnapshotVecDelegate for Delegate {
416 type Value = TypeVariableData;
417 type Undo = Instantiate;
419 fn reverse(_values: &mut Vec<TypeVariableData>, _action: Instantiate) {
420 // We don't actually have to *do* anything to reverse an
421 // instantiation; the value for a variable is stored in the
422 // `eq_relations` and hence its rollback code will handle
423 // it. In fact, we could *almost* just remove the
424 // `SnapshotVec` entirely, except that we would have to
425 // reproduce *some* of its logic, since we want to know which
426 // type variables have been instantiated since the snapshot
427 // was started, so we can implement `types_escaping_snapshot`.
429 // (If we extended the `UnificationTable` to let us see which
430 // values have been unified and so forth, that might also
435 ///////////////////////////////////////////////////////////////////////////
437 /// These structs (a newtyped TyVid) are used as the unification key
438 /// for the `eq_relations`; they carry a `TypeVariableValue` along
440 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
441 pub(crate) struct TyVidEqKey<'tcx> {
444 // in the table, we map each ty-vid to one of these:
445 phantom: PhantomData<TypeVariableValue<'tcx>>,
448 impl<'tcx> From<ty::TyVid> for TyVidEqKey<'tcx> {
449 fn from(vid: ty::TyVid) -> Self {
450 TyVidEqKey { vid, phantom: PhantomData }
454 impl<'tcx> ut::UnifyKey for TyVidEqKey<'tcx> {
455 type Value = TypeVariableValue<'tcx>;
456 fn index(&self) -> u32 {
459 fn from_index(i: u32) -> Self {
460 TyVidEqKey::from(ty::TyVid { index: i })
462 fn tag() -> &'static str {
467 impl<'tcx> ut::UnifyValue for TypeVariableValue<'tcx> {
468 type Error = ut::NoError;
470 fn unify_values(value1: &Self, value2: &Self) -> Result<Self, ut::NoError> {
471 match (value1, value2) {
472 // We never equate two type variables, both of which
473 // have known types. Instead, we recursively equate
475 (&TypeVariableValue::Known { .. }, &TypeVariableValue::Known { .. }) => {
476 bug!("equating two type variables, both of which have known types")
479 // If one side is known, prefer that one.
480 (&TypeVariableValue::Known { .. }, &TypeVariableValue::Unknown { .. }) => Ok(*value1),
481 (&TypeVariableValue::Unknown { .. }, &TypeVariableValue::Known { .. }) => Ok(*value2),
483 // If both sides are *unknown*, it hardly matters, does it?
485 &TypeVariableValue::Unknown { universe: universe1 },
486 &TypeVariableValue::Unknown { universe: universe2 },
488 // If we unify two unbound variables, ?T and ?U, then whatever
489 // value they wind up taking (which must be the same value) must
490 // be nameable by both universes. Therefore, the resulting
491 // universe is the minimum of the two universes, because that is
492 // the one which contains the fewest names in scope.
493 let universe = cmp::min(universe1, universe2);
494 Ok(TypeVariableValue::Unknown { universe })