Skip to content

Commit

Permalink
Add some sanity assertions to make sure we use the opaque types corre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
oli-obk committed Feb 2, 2022
1 parent d49b074 commit a4c1cec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 11 additions & 1 deletion compiler/rustc_infer/src/infer/opaque_types/table.rs
Expand Up @@ -6,7 +6,7 @@ use crate::infer::InferCtxtUndoLogs;

use super::{OpaqueTypeDecl, OpaqueTypeMap};

#[derive(Default)]
#[derive(Default, Debug)]
pub struct OpaqueTypeStorage<'tcx> {
// Opaque types found in explicit return types and their
// associated fresh inference variable. Writeback resolves these
Expand All @@ -23,6 +23,7 @@ pub struct OpaqueTypeStorage<'tcx> {
}

impl<'tcx> OpaqueTypeStorage<'tcx> {
#[instrument(level = "debug")]
pub(crate) fn remove(&mut self, key: OpaqueTypeKey<'tcx>) {
match self.opaque_types.remove(&key) {
None => bug!("reverted opaque type inference that was never registered"),
Expand All @@ -42,6 +43,7 @@ impl<'tcx> OpaqueTypeStorage<'tcx> {
self.opaque_types.clone()
}

#[instrument(level = "debug")]
pub fn take_opaque_types(&mut self) -> OpaqueTypeMap<'tcx> {
std::mem::take(&mut self.opaque_types)
}
Expand All @@ -54,13 +56,21 @@ impl<'tcx> OpaqueTypeStorage<'tcx> {
OpaqueTypeTable { storage: self, undo_log }
}
}

impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
fn drop(&mut self) {
assert!(self.opaque_types.is_empty(), "{:?}", self.opaque_types);
}
}

pub struct OpaqueTypeTable<'a, 'tcx> {
storage: &'a mut OpaqueTypeStorage<'tcx>,

undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
}

impl<'a, 'tcx> OpaqueTypeTable<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
pub fn register(&mut self, key: OpaqueTypeKey<'tcx>, decl: OpaqueTypeDecl<'tcx>) {
self.undo_log.push(key);
self.storage.opaque_types.insert(key, decl);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_typeck/src/check/writeback.rs
Expand Up @@ -498,7 +498,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {

#[instrument(skip(self, span), level = "debug")]
fn visit_opaque_types(&mut self, span: Span) {
for (opaque_type_key, opaque_defn) in self.fcx.infcx.opaque_types() {
let opaque_types =
self.fcx.infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
for (opaque_type_key, opaque_defn) in opaque_types {
let hir_id =
self.tcx().hir().local_def_id_to_hir_id(opaque_type_key.def_id.expect_local());
let instantiated_ty = self.resolve(opaque_defn.concrete_ty, &hir_id);
Expand Down

0 comments on commit a4c1cec

Please sign in to comment.