1 use std::marker::PhantomData;
3 use rustc_data_structures::snapshot_vec as sv;
4 use rustc_data_structures::undo_log::{Rollback, UndoLogs};
5 use rustc_data_structures::unify as ut;
6 use rustc_middle::infer::unify_key::RegionVidKey;
7 use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey};
10 infer::{region_constraints, type_variable, InferCtxtInner},
14 pub struct Snapshot<'tcx> {
15 pub(crate) undo_len: usize,
16 _marker: PhantomData<&'tcx ()>,
19 /// Records the "undo" data for a single operation that affects some form of inference variable.
21 pub(crate) enum UndoLog<'tcx> {
22 OpaqueTypes(OpaqueTypeKey<'tcx>, Option<OpaqueHiddenType<'tcx>>),
23 TypeVariables(type_variable::UndoLog<'tcx>),
24 ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
25 IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
26 FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>),
27 RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
28 RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
29 ProjectionCache(traits::UndoLog<'tcx>),
33 macro_rules! impl_from {
34 ($($ctor: ident ($ty: ty),)*) => {
36 impl<'tcx> From<$ty> for UndoLog<'tcx> {
37 fn from(x: $ty) -> Self {
38 UndoLog::$ctor(x.into())
45 // Upcast from a single kind of "undoable action" to the general enum
47 RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
48 TypeVariables(type_variable::UndoLog<'tcx>),
50 TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'tcx>>>),
51 TypeVariables(sv::UndoLog<ut::Delegate<ty::TyVid>>),
52 TypeVariables(sv::UndoLog<type_variable::Delegate>),
53 TypeVariables(type_variable::Instantiate),
55 IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
57 FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>),
59 ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
61 RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
62 ProjectionCache(traits::UndoLog<'tcx>),
65 /// The Rollback trait defines how to rollback a particular action.
66 impl<'tcx> Rollback<UndoLog<'tcx>> for InferCtxtInner<'tcx> {
67 fn reverse(&mut self, undo: UndoLog<'tcx>) {
69 UndoLog::OpaqueTypes(key, idx) => self.opaque_type_storage.remove(key, idx),
70 UndoLog::TypeVariables(undo) => self.type_variable_storage.reverse(undo),
71 UndoLog::ConstUnificationTable(undo) => self.const_unification_storage.reverse(undo),
72 UndoLog::IntUnificationTable(undo) => self.int_unification_storage.reverse(undo),
73 UndoLog::FloatUnificationTable(undo) => self.float_unification_storage.reverse(undo),
74 UndoLog::RegionConstraintCollector(undo) => {
75 self.region_constraint_storage.as_mut().unwrap().reverse(undo)
77 UndoLog::RegionUnificationTable(undo) => {
78 self.region_constraint_storage.as_mut().unwrap().unification_table.reverse(undo)
80 UndoLog::ProjectionCache(undo) => self.projection_cache.reverse(undo),
81 UndoLog::PushRegionObligation => {
82 self.region_obligations.pop();
88 /// The combined undo log for all the various unification tables. For each change to the storage
89 /// for any kind of inference variable, we record an UndoLog entry in the vector here.
90 #[derive(Clone, Default)]
91 pub(crate) struct InferCtxtUndoLogs<'tcx> {
92 logs: Vec<UndoLog<'tcx>>,
93 num_open_snapshots: usize,
96 /// The UndoLogs trait defines how we undo a particular kind of action (of type T). We can undo any
97 /// action that is convertible into an UndoLog (per the From impls above).
98 impl<'tcx, T> UndoLogs<T> for InferCtxtUndoLogs<'tcx>
100 UndoLog<'tcx>: From<T>,
103 fn num_open_snapshots(&self) -> usize {
104 self.num_open_snapshots
108 fn push(&mut self, undo: T) {
109 if self.in_snapshot() {
110 self.logs.push(undo.into())
114 fn clear(&mut self) {
116 self.num_open_snapshots = 0;
119 fn extend<J>(&mut self, undos: J)
122 J: IntoIterator<Item = T>,
124 if self.in_snapshot() {
125 self.logs.extend(undos.into_iter().map(UndoLog::from))
130 impl<'tcx> InferCtxtInner<'tcx> {
131 pub fn rollback_to(&mut self, snapshot: Snapshot<'tcx>) {
132 debug!("rollback_to({})", snapshot.undo_len);
133 self.undo_log.assert_open_snapshot(&snapshot);
135 while self.undo_log.logs.len() > snapshot.undo_len {
136 let undo = self.undo_log.logs.pop().unwrap();
140 if self.undo_log.num_open_snapshots == 1 {
141 // The root snapshot. It's safe to clear the undo log because
142 // there's no snapshot further out that we might need to roll back
144 assert!(snapshot.undo_len == 0);
145 self.undo_log.logs.clear();
148 self.undo_log.num_open_snapshots -= 1;
151 pub fn commit(&mut self, snapshot: Snapshot<'tcx>) {
152 debug!("commit({})", snapshot.undo_len);
154 if self.undo_log.num_open_snapshots == 1 {
155 // The root snapshot. It's safe to clear the undo log because
156 // there's no snapshot further out that we might need to roll back
158 assert!(snapshot.undo_len == 0);
159 self.undo_log.logs.clear();
162 self.undo_log.num_open_snapshots -= 1;
166 impl<'tcx> InferCtxtUndoLogs<'tcx> {
167 pub fn start_snapshot(&mut self) -> Snapshot<'tcx> {
168 self.num_open_snapshots += 1;
169 Snapshot { undo_len: self.logs.len(), _marker: PhantomData }
172 pub(crate) fn region_constraints_in_snapshot(
175 ) -> impl Iterator<Item = &'_ region_constraints::UndoLog<'tcx>> + Clone {
176 self.logs[s.undo_len..].iter().filter_map(|log| match log {
177 UndoLog::RegionConstraintCollector(log) => Some(log),
182 pub(crate) fn opaque_types_in_snapshot(&self, s: &Snapshot<'tcx>) -> bool {
183 self.logs[s.undo_len..].iter().any(|log| matches!(log, UndoLog::OpaqueTypes(..)))
186 pub(crate) fn region_constraints(
188 ) -> impl Iterator<Item = &'_ region_constraints::UndoLog<'tcx>> + Clone {
189 self.logs.iter().filter_map(|log| match log {
190 UndoLog::RegionConstraintCollector(log) => Some(log),
195 fn assert_open_snapshot(&self, snapshot: &Snapshot<'tcx>) {
196 // Failures here may indicate a failure to follow a stack discipline.
197 assert!(self.logs.len() >= snapshot.undo_len);
198 assert!(self.num_open_snapshots > 0);
202 impl<'tcx> std::ops::Index<usize> for InferCtxtUndoLogs<'tcx> {
203 type Output = UndoLog<'tcx>;
205 fn index(&self, key: usize) -> &Self::Output {
210 impl<'tcx> std::ops::IndexMut<usize> for InferCtxtUndoLogs<'tcx> {
211 fn index_mut(&mut self, key: usize) -> &mut Self::Output {