Skip to main content

rustc_infer/infer/snapshot/
undo_log.rs

1use std::marker::PhantomData;
2
3use rustc_data_structures::undo_log::{Rollback, UndoLogs};
4use rustc_data_structures::{assert_matches, snapshot_vec as sv, unify as ut};
5use rustc_middle::ty::{self, OpaqueTypeKey, ProvisionalHiddenType};
6use tracing::debug;
7
8use crate::infer::unify_key::{ConstVidKey, RegionVidKey};
9use crate::infer::{InferCtxtInner, region_constraints, type_variable};
10use crate::traits;
11
12pub struct Snapshot<'tcx> {
13    pub(crate) undo_len: usize,
14    _marker: PhantomData<&'tcx ()>,
15}
16
17/// Records the "undo" data for a single operation that affects some form of inference variable.
18#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for UndoLog<'tcx> {
    #[inline]
    fn clone(&self) -> UndoLog<'tcx> {
        match self {
            UndoLog::DuplicateOpaqueType => UndoLog::DuplicateOpaqueType,
            UndoLog::OpaqueTypes(__self_0, __self_1) =>
                UndoLog::OpaqueTypes(::core::clone::Clone::clone(__self_0),
                    ::core::clone::Clone::clone(__self_1)),
            UndoLog::TypeVariables(__self_0) =>
                UndoLog::TypeVariables(::core::clone::Clone::clone(__self_0)),
            UndoLog::ConstUnificationTable(__self_0) =>
                UndoLog::ConstUnificationTable(::core::clone::Clone::clone(__self_0)),
            UndoLog::IntUnificationTable(__self_0) =>
                UndoLog::IntUnificationTable(::core::clone::Clone::clone(__self_0)),
            UndoLog::FloatUnificationTable(__self_0) =>
                UndoLog::FloatUnificationTable(::core::clone::Clone::clone(__self_0)),
            UndoLog::RegionConstraintCollector(__self_0) =>
                UndoLog::RegionConstraintCollector(::core::clone::Clone::clone(__self_0)),
            UndoLog::RegionUnificationTable(__self_0) =>
                UndoLog::RegionUnificationTable(::core::clone::Clone::clone(__self_0)),
            UndoLog::ProjectionCache(__self_0) =>
                UndoLog::ProjectionCache(::core::clone::Clone::clone(__self_0)),
            UndoLog::PushTypeOutlivesConstraint =>
                UndoLog::PushTypeOutlivesConstraint,
            UndoLog::PushRegionAssumption => UndoLog::PushRegionAssumption,
            UndoLog::PushHirTypeckPotentiallyRegionDependentGoal =>
                UndoLog::PushHirTypeckPotentiallyRegionDependentGoal,
        }
    }
}Clone)]
19pub(crate) enum UndoLog<'tcx> {
20    DuplicateOpaqueType,
21    OpaqueTypes(OpaqueTypeKey<'tcx>, Option<ProvisionalHiddenType<'tcx>>),
22    TypeVariables(type_variable::UndoLog<'tcx>),
23    ConstUnificationTable(sv::UndoLog<ut::Delegate<ConstVidKey<'tcx>>>),
24    IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
25    FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>),
26    RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
27    RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
28    ProjectionCache(traits::UndoLog<'tcx>),
29    PushTypeOutlivesConstraint,
30    PushRegionAssumption,
31    PushHirTypeckPotentiallyRegionDependentGoal,
32}
33
34macro_rules! impl_from {
35    ($($ctor:ident ($ty:ty),)*) => {
36        $(
37        impl<'tcx> From<$ty> for UndoLog<'tcx> {
38            fn from(x: $ty) -> Self {
39                UndoLog::$ctor(x.into())
40            }
41        }
42        )*
43    }
44}
45
46// Upcast from a single kind of "undoable action" to the general enum
47impl<'tcx> From<traits::UndoLog<'tcx>> for UndoLog<'tcx> {
    fn from(x: traits::UndoLog<'tcx>) -> Self {
        UndoLog::ProjectionCache(x.into())
    }
}impl_from! {
48    RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
49
50    TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'tcx>>>),
51    TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidSubKey>>),
52    TypeVariables(type_variable::UndoLog<'tcx>),
53    IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
54    FloatUnificationTable(sv::UndoLog<ut::Delegate<ty::FloatVid>>),
55
56    ConstUnificationTable(sv::UndoLog<ut::Delegate<ConstVidKey<'tcx>>>),
57
58    RegionUnificationTable(sv::UndoLog<ut::Delegate<RegionVidKey<'tcx>>>),
59    ProjectionCache(traits::UndoLog<'tcx>),
60}
61
62/// The Rollback trait defines how to rollback a particular action.
63impl<'tcx> Rollback<UndoLog<'tcx>> for InferCtxtInner<'tcx> {
64    fn reverse(&mut self, undo: UndoLog<'tcx>) {
65        match undo {
66            UndoLog::DuplicateOpaqueType => self.opaque_type_storage.pop_duplicate_entry(),
67            UndoLog::OpaqueTypes(key, idx) => self.opaque_type_storage.remove(key, idx),
68            UndoLog::TypeVariables(undo) => self.type_variable_storage.reverse(undo),
69            UndoLog::ConstUnificationTable(undo) => self.const_unification_storage.reverse(undo),
70            UndoLog::IntUnificationTable(undo) => self.int_unification_storage.reverse(undo),
71            UndoLog::FloatUnificationTable(undo) => self.float_unification_storage.reverse(undo),
72            UndoLog::RegionConstraintCollector(undo) => {
73                self.region_constraint_storage.as_mut().unwrap().reverse(undo)
74            }
75            UndoLog::RegionUnificationTable(undo) => {
76                self.region_constraint_storage.as_mut().unwrap().unification_table.reverse(undo)
77            }
78            UndoLog::ProjectionCache(undo) => self.projection_cache.reverse(undo),
79            UndoLog::PushTypeOutlivesConstraint => {
80                let popped = self.region_obligations.pop();
81                match popped {
    Some(_) => {}
    ref left_val => {
        ::core::panicking::assert_matches_failed(left_val, "Some(_)",
            ::core::option::Option::Some(format_args!("pushed region constraint but could not pop it")));
    }
};assert_matches!(popped, Some(_), "pushed region constraint but could not pop it");
82            }
83            UndoLog::PushRegionAssumption => {
84                let popped = self.region_assumptions.pop();
85                match popped {
    Some(_) => {}
    ref left_val => {
        ::core::panicking::assert_matches_failed(left_val, "Some(_)",
            ::core::option::Option::Some(format_args!("pushed region assumption but could not pop it")));
    }
};assert_matches!(popped, Some(_), "pushed region assumption but could not pop it");
86            }
87            UndoLog::PushHirTypeckPotentiallyRegionDependentGoal => {
88                let popped = self.hir_typeck_potentially_region_dependent_goals.pop();
89                match popped {
    Some(_) => {}
    ref left_val => {
        ::core::panicking::assert_matches_failed(left_val, "Some(_)",
            ::core::option::Option::Some(format_args!("pushed goal but could not pop it")));
    }
};assert_matches!(popped, Some(_), "pushed goal but could not pop it");
90            }
91        }
92    }
93}
94
95/// The combined undo log for all the various unification tables. For each change to the storage
96/// for any kind of inference variable, we record an UndoLog entry in the vector here.
97#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for InferCtxtUndoLogs<'tcx> {
    #[inline]
    fn clone(&self) -> InferCtxtUndoLogs<'tcx> {
        InferCtxtUndoLogs {
            logs: ::core::clone::Clone::clone(&self.logs),
            num_open_snapshots: ::core::clone::Clone::clone(&self.num_open_snapshots),
        }
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::default::Default for InferCtxtUndoLogs<'tcx> {
    #[inline]
    fn default() -> InferCtxtUndoLogs<'tcx> {
        InferCtxtUndoLogs {
            logs: ::core::default::Default::default(),
            num_open_snapshots: ::core::default::Default::default(),
        }
    }
}Default)]
98pub(crate) struct InferCtxtUndoLogs<'tcx> {
99    logs: Vec<UndoLog<'tcx>>,
100    num_open_snapshots: usize,
101}
102
103/// The UndoLogs trait defines how we undo a particular kind of action (of type T). We can undo any
104/// action that is convertible into an UndoLog (per the From impls above).
105impl<'tcx, T> UndoLogs<T> for InferCtxtUndoLogs<'tcx>
106where
107    UndoLog<'tcx>: From<T>,
108{
109    #[inline]
110    fn num_open_snapshots(&self) -> usize {
111        self.num_open_snapshots
112    }
113
114    #[inline]
115    fn push(&mut self, undo: T) {
116        if self.in_snapshot() {
117            self.logs.push(undo.into())
118        }
119    }
120
121    fn clear(&mut self) {
122        self.logs.clear();
123        self.num_open_snapshots = 0;
124    }
125
126    fn extend<J>(&mut self, undos: J)
127    where
128        Self: Sized,
129        J: IntoIterator<Item = T>,
130    {
131        if self.in_snapshot() {
132            self.logs.extend(undos.into_iter().map(UndoLog::from))
133        }
134    }
135}
136
137impl<'tcx> InferCtxtInner<'tcx> {
138    pub fn rollback_to(&mut self, snapshot: Snapshot<'tcx>) {
139        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/snapshot/undo_log.rs:139",
                        "rustc_infer::infer::snapshot::undo_log",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/snapshot/undo_log.rs"),
                        ::tracing_core::__macro_support::Option::Some(139u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::snapshot::undo_log"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("rollback_to({0})",
                                                    snapshot.undo_len) as &dyn Value))])
            });
    } else { ; }
};debug!("rollback_to({})", snapshot.undo_len);
140        self.undo_log.assert_open_snapshot(&snapshot);
141
142        while self.undo_log.logs.len() > snapshot.undo_len {
143            let undo = self.undo_log.logs.pop().unwrap();
144            self.reverse(undo);
145        }
146
147        self.type_variable_storage.finalize_rollback();
148
149        if self.undo_log.num_open_snapshots == 1 {
150            // After the root snapshot the undo log should be empty.
151            if !(snapshot.undo_len == 0) {
    ::core::panicking::panic("assertion failed: snapshot.undo_len == 0")
};assert!(snapshot.undo_len == 0);
152            if !self.undo_log.logs.is_empty() {
    ::core::panicking::panic("assertion failed: self.undo_log.logs.is_empty()")
};assert!(self.undo_log.logs.is_empty());
153        }
154
155        self.undo_log.num_open_snapshots -= 1;
156    }
157
158    pub fn commit(&mut self, snapshot: Snapshot<'tcx>) {
159        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/snapshot/undo_log.rs:159",
                        "rustc_infer::infer::snapshot::undo_log",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/snapshot/undo_log.rs"),
                        ::tracing_core::__macro_support::Option::Some(159u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::snapshot::undo_log"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("commit({0})",
                                                    snapshot.undo_len) as &dyn Value))])
            });
    } else { ; }
};debug!("commit({})", snapshot.undo_len);
160
161        if self.undo_log.num_open_snapshots == 1 {
162            // The root snapshot. It's safe to clear the undo log because
163            // there's no snapshot further out that we might need to roll back
164            // to.
165            if !(snapshot.undo_len == 0) {
    ::core::panicking::panic("assertion failed: snapshot.undo_len == 0")
};assert!(snapshot.undo_len == 0);
166            self.undo_log.logs.clear();
167        }
168
169        self.undo_log.num_open_snapshots -= 1;
170    }
171}
172
173impl<'tcx> InferCtxtUndoLogs<'tcx> {
174    pub(crate) fn start_snapshot(&mut self) -> Snapshot<'tcx> {
175        self.num_open_snapshots += 1;
176        Snapshot { undo_len: self.logs.len(), _marker: PhantomData }
177    }
178
179    pub(crate) fn region_constraints_in_snapshot(
180        &self,
181        s: &Snapshot<'tcx>,
182    ) -> impl Iterator<Item = &'_ region_constraints::UndoLog<'tcx>> + Clone {
183        self.logs[s.undo_len..].iter().filter_map(|log| match log {
184            UndoLog::RegionConstraintCollector(log) => Some(log),
185            _ => None,
186        })
187    }
188
189    pub(crate) fn opaque_types_in_snapshot(&self, s: &Snapshot<'tcx>) -> bool {
190        self.logs[s.undo_len..].iter().any(|log| #[allow(non_exhaustive_omitted_patterns)] match log {
    UndoLog::OpaqueTypes(..) => true,
    _ => false,
}matches!(log, UndoLog::OpaqueTypes(..)))
191    }
192
193    fn assert_open_snapshot(&self, snapshot: &Snapshot<'tcx>) {
194        // Failures here may indicate a failure to follow a stack discipline.
195        if !(self.logs.len() >= snapshot.undo_len) {
    ::core::panicking::panic("assertion failed: self.logs.len() >= snapshot.undo_len")
};assert!(self.logs.len() >= snapshot.undo_len);
196        if !(self.num_open_snapshots > 0) {
    ::core::panicking::panic("assertion failed: self.num_open_snapshots > 0")
};assert!(self.num_open_snapshots > 0);
197    }
198}
199
200impl<'tcx> std::ops::Index<usize> for InferCtxtUndoLogs<'tcx> {
201    type Output = UndoLog<'tcx>;
202
203    fn index(&self, key: usize) -> &Self::Output {
204        &self.logs[key]
205    }
206}
207
208impl<'tcx> std::ops::IndexMut<usize> for InferCtxtUndoLogs<'tcx> {
209    fn index_mut(&mut self, key: usize) -> &mut Self::Output {
210        &mut self.logs[key]
211    }
212}