]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_infer/infer/type_variable.rs
Improve naming
[rust.git] / src / librustc_infer / infer / type_variable.rs
index 47f7d7641369383c4624f0d07435f46322e5b0a2..ed8ee3b655d1bfe3ea122a4c2a0ee9daad3a458d 100644 (file)
@@ -3,7 +3,7 @@
 use rustc_span::symbol::Symbol;
 use rustc_span::Span;
 
-use crate::infer::Logs;
+use crate::infer::InferCtxtUndoLogs;
 
 use rustc_data_structures::snapshot_vec as sv;
 use rustc_data_structures::unify as ut;
 use std::marker::PhantomData;
 use std::ops::Range;
 
-use rustc_data_structures::undo_log::{Rollback, Snapshots, UndoLogs};
+use rustc_data_structures::undo_log::{Rollback, UndoLogs};
 
+/// Represents a single undo-able action that affects a type inference variable.
 pub(crate) enum UndoLog<'tcx> {
     EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
     SubRelation(sv::UndoLog<ut::Delegate<ty::TyVid>>),
     Values(sv::UndoLog<Delegate>),
 }
 
+/// Convert from a specific kind of undo to the more general UndoLog
 impl<'tcx> From<sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>> for UndoLog<'tcx> {
     fn from(l: sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>) -> Self {
         UndoLog::EqRelation(l)
     }
 }
 
+/// Convert from a specific kind of undo to the more general UndoLog
 impl<'tcx> From<sv::UndoLog<ut::Delegate<ty::TyVid>>> for UndoLog<'tcx> {
     fn from(l: sv::UndoLog<ut::Delegate<ty::TyVid>>) -> Self {
         UndoLog::SubRelation(l)
     }
 }
 
+/// Convert from a specific kind of undo to the more general UndoLog
 impl<'tcx> From<sv::UndoLog<Delegate>> for UndoLog<'tcx> {
     fn from(l: sv::UndoLog<Delegate>) -> Self {
         UndoLog::Values(l)
     }
 }
 
+/// Convert from a specific kind of undo to the more general UndoLog
 impl<'tcx> From<Instantiate> for UndoLog<'tcx> {
     fn from(l: Instantiate) -> Self {
         UndoLog::Values(sv::UndoLog::Other(l))
     }
 }
 
-pub(crate) struct RollbackView<'tcx, 'a> {
-    pub(crate) eq_relations: &'a mut ut::UnificationStorage<TyVidEqKey<'tcx>>,
-    pub(crate) sub_relations: &'a mut ut::UnificationStorage<ty::TyVid>,
-    pub(crate) values: &'a mut Vec<TypeVariableData>,
-}
-
-impl<'tcx, 'a> From<&'a mut TypeVariableStorage<'tcx>> for RollbackView<'tcx, 'a> {
-    fn from(l: &'a mut TypeVariableStorage<'tcx>) -> Self {
-        let TypeVariableStorage { eq_relations, sub_relations, values } = l;
-        Self { eq_relations, sub_relations, values }
-    }
-}
-
-impl<'tcx> Rollback<UndoLog<'tcx>> for RollbackView<'tcx, '_> {
+impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
     fn reverse(&mut self, undo: UndoLog<'tcx>) {
         match undo {
             UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
@@ -67,12 +59,12 @@ fn reverse(&mut self, undo: UndoLog<'tcx>) {
 }
 
 pub struct TypeVariableStorage<'tcx> {
-    values: Vec<TypeVariableData>,
+    values: sv::SnapshotVecStorage<Delegate>,
 
     /// Two variables are unified in `eq_relations` when we have a
     /// constraint `?X == ?Y`. This table also stores, for each key,
     /// the known value.
-    eq_relations: ut::UnificationStorage<TyVidEqKey<'tcx>>,
+    eq_relations: ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
 
     /// Two variables are unified in `sub_relations` when we have a
     /// constraint `?X <: ?Y` *or* a constraint `?Y <: ?X`. This second
@@ -91,17 +83,17 @@ pub struct TypeVariableStorage<'tcx> {
     /// This is reasonable because, in Rust, subtypes have the same
     /// "skeleton" and hence there is no possible type such that
     /// (e.g.)  `Box<?3> <: ?3` for any `?3`.
-    sub_relations: ut::UnificationStorage<ty::TyVid>,
+    sub_relations: ut::UnificationTableStorage<ty::TyVid>,
 }
 
-pub struct TypeVariableTable<'tcx, 'a> {
-    values: &'a mut Vec<TypeVariableData>,
+pub struct TypeVariableTable<'a, 'tcx> {
+    values: &'a mut sv::SnapshotVecStorage<Delegate>,
 
-    eq_relations: &'a mut ut::UnificationStorage<TyVidEqKey<'tcx>>,
+    eq_relations: &'a mut ut::UnificationTableStorage<TyVidEqKey<'tcx>>,
 
-    sub_relations: &'a mut ut::UnificationStorage<ty::TyVid>,
+    sub_relations: &'a mut ut::UnificationTableStorage<ty::TyVid>,
 
-    undo_log: &'a mut Logs<'tcx>,
+    undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
 }
 
 #[derive(Copy, Clone, Debug)]
@@ -172,28 +164,28 @@ pub(crate) struct Instantiate {
 impl<'tcx> TypeVariableStorage<'tcx> {
     pub fn new() -> TypeVariableStorage<'tcx> {
         TypeVariableStorage {
-            values: Vec::new(),
-            eq_relations: ut::UnificationStorage::new(),
-            sub_relations: ut::UnificationStorage::new(),
+            values: sv::SnapshotVecStorage::new(),
+            eq_relations: ut::UnificationTableStorage::new(),
+            sub_relations: ut::UnificationTableStorage::new(),
         }
     }
 
     pub(crate) fn with_log<'a>(
         &'a mut self,
-        undo_log: &'a mut Logs<'tcx>,
-    ) -> TypeVariableTable<'tcx, 'a> {
+        undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
+    ) -> TypeVariableTable<'a, 'tcx> {
         let TypeVariableStorage { values, eq_relations, sub_relations } = self;
         TypeVariableTable { values, eq_relations, sub_relations, undo_log }
     }
 }
 
-impl<'tcx> TypeVariableTable<'tcx, '_> {
+impl<'tcx> TypeVariableTable<'_, 'tcx> {
     /// Returns the diverges flag given when `vid` was created.
     ///
     /// Note that this function does not return care whether
     /// `vid` has been unified with something else or not.
     pub fn var_diverges(&self, vid: ty::TyVid) -> bool {
-        self.values.get(vid.index as usize).unwrap().diverging
+        self.values.get(vid.index as usize).diverging
     }
 
     /// Returns the origin that was given when `vid` was created.
@@ -201,7 +193,7 @@ pub fn var_diverges(&self, vid: ty::TyVid) -> bool {
     /// Note that this function does not return care whether
     /// `vid` has been unified with something else or not.
     pub fn var_origin(&self, vid: ty::TyVid) -> &TypeVariableOrigin {
-        &self.values.get(vid.index as usize).unwrap().origin
+        &self.values.get(vid.index as usize).origin
     }
 
     /// Records that `a == b`, depending on `dir`.
@@ -340,28 +332,18 @@ pub fn snapshot(&mut self) -> Snapshot<'tcx> {
         Snapshot { value_count: self.eq_relations().len() as u32, _marker: PhantomData }
     }
 
-    fn values(&mut self) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut Logs<'tcx>> {
-        sv::SnapshotVec::with_log(self.values, self.undo_log)
+    fn values(
+        &mut self,
+    ) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut InferCtxtUndoLogs<'tcx>> {
+        self.values.with_log(self.undo_log)
     }
 
-    fn eq_relations(
-        &mut self,
-    ) -> ut::UnificationTable<
-        ut::InPlace<
-            TyVidEqKey<'tcx>,
-            &mut ut::UnificationStorage<TyVidEqKey<'tcx>>,
-            &mut Logs<'tcx>,
-        >,
-    > {
-        ut::UnificationTable::with_log(self.eq_relations, self.undo_log)
+    fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
+        self.eq_relations.with_log(self.undo_log)
     }
 
-    fn sub_relations(
-        &mut self,
-    ) -> ut::UnificationTable<
-        ut::InPlace<ty::TyVid, &mut ut::UnificationStorage<ty::TyVid>, &mut Logs<'tcx>>,
-    > {
-        ut::UnificationTable::with_log(self.sub_relations, self.undo_log)
+    fn sub_relations(&mut self) -> super::UnificationTable<'_, 'tcx, ty::TyVid> {
+        self.sub_relations.with_log(self.undo_log)
     }
 
     /// Returns a range of the type variables created during the snapshot.
@@ -374,7 +356,7 @@ pub fn vars_since_snapshot(
         (
             range.start..range.end,
             (range.start.index..range.end.index)
-                .map(|index| self.values.get(index as usize).unwrap().origin)
+                .map(|index| self.values.get(index as usize).origin)
                 .collect(),
         )
     }