1 use rustc_data_structures::undo_log::UndoLogs;
2 use rustc_hir::OpaqueTyOrigin;
3 use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty};
4 use rustc_span::DUMMY_SP;
6 use crate::infer::{InferCtxtUndoLogs, UndoLog};
8 use super::{OpaqueTypeDecl, OpaqueTypeMap};
10 #[derive(Default, Debug, Clone)]
11 pub struct OpaqueTypeStorage<'tcx> {
12 /// Opaque types found in explicit return types and their
13 /// associated fresh inference variable. Writeback resolves these
14 /// variables to get the concrete type, which can be used to
15 /// 'de-opaque' OpaqueTypeDecl, after typeck is done with all functions.
16 pub opaque_types: OpaqueTypeMap<'tcx>,
19 impl<'tcx> OpaqueTypeStorage<'tcx> {
20 #[instrument(level = "debug")]
21 pub(crate) fn remove(&mut self, key: OpaqueTypeKey<'tcx>, idx: Option<OpaqueHiddenType<'tcx>>) {
22 if let Some(idx) = idx {
23 self.opaque_types.get_mut(&key).unwrap().hidden_type = idx;
25 match self.opaque_types.remove(&key) {
26 None => bug!("reverted opaque type inference that was never registered: {:?}", key),
33 pub(crate) fn with_log<'a>(
35 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
36 ) -> OpaqueTypeTable<'a, 'tcx> {
37 OpaqueTypeTable { storage: self, undo_log }
41 impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
43 if !self.opaque_types.is_empty() {
45 tcx.sess.delay_span_bug(DUMMY_SP, &format!("{:?}", self.opaque_types))
51 pub struct OpaqueTypeTable<'a, 'tcx> {
52 storage: &'a mut OpaqueTypeStorage<'tcx>,
54 undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
57 impl<'a, 'tcx> OpaqueTypeTable<'a, 'tcx> {
58 #[instrument(skip(self), level = "debug")]
59 pub(crate) fn register(
61 key: OpaqueTypeKey<'tcx>,
62 hidden_type: OpaqueHiddenType<'tcx>,
63 origin: OpaqueTyOrigin,
64 ) -> Option<Ty<'tcx>> {
65 if let Some(decl) = self.storage.opaque_types.get_mut(&key) {
66 let prev = std::mem::replace(&mut decl.hidden_type, hidden_type);
67 self.undo_log.push(UndoLog::OpaqueTypes(key, Some(prev)));
70 let decl = OpaqueTypeDecl { hidden_type, origin };
71 self.storage.opaque_types.insert(key, decl);
72 self.undo_log.push(UndoLog::OpaqueTypes(key, None));