Add roll back infrastructure for opaque type caches

This commit is contained in:
Oli Scherer 2021-08-13 17:19:26 +00:00 committed by Oli Scherer
parent dca1e7aa5a
commit d49b0746f6
10 changed files with 113 additions and 37 deletions

View File

@ -190,7 +190,8 @@ pub(crate) fn type_check<'mir, 'tcx>(
liveness::generate(&mut cx, body, elements, flow_inits, move_data, location_table);
translate_outlives_facts(&mut cx);
let opaque_type_values = mem::take(&mut infcx.inner.borrow_mut().opaque_types);
let opaque_type_values =
infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
opaque_type_values
.into_iter()
@ -1315,8 +1316,7 @@ fn eq_opaque_type_and_type(
// have to solve any bounds (e.g., `-> impl Iterator` needs to
// prove that `T: Iterator` where `T` is the type we
// instantiated it with).
let opaque_type_map = self.infcx.inner.borrow().opaque_types.clone();
for (opaque_type_key, opaque_decl) in opaque_type_map {
for (opaque_type_key, opaque_decl) in self.infcx.opaque_types() {
self.fully_perform_op(
locations,
ConstraintCategory::OpaqueType,

View File

@ -30,6 +30,11 @@ pub fn insert(&mut self, k: K, v: V) -> Option<V> {
}
}
/// Removes the entry from the map and returns the removed value
pub fn remove(&mut self, k: &K) -> Option<V> {
self.0.iter().position(|(k2, _)| k2 == k).map(|pos| self.0.remove(pos).1)
}
/// Gets a reference to the value in the entry.
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
where

View File

@ -66,17 +66,18 @@ fn relate_with_variance<T: Relate<'tcx>>(
self.relate(a, b)
}
#[instrument(skip(self), level = "debug")]
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
if a == b {
return Ok(a);
}
trace!(a = ?a.kind(), b = ?b.kind());
let infcx = self.fields.infcx;
let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
debug!("{}.tys: replacements ({:?}, {:?})", self.tag(), a, b);
trace!(a = ?a.kind(), b = ?b.kind(), "replacements");
match (a.kind(), b.kind()) {
(&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {

View File

@ -5,7 +5,7 @@
pub use self::SubregionOrigin::*;
pub use self::ValuePairs::*;
use self::opaque_types::OpaqueTypeMap;
use self::opaque_types::OpaqueTypeStorage;
pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
@ -190,20 +190,10 @@ pub struct InferCtxtInner<'tcx> {
/// that all type inference variables have been bound and so forth.
region_obligations: Vec<(hir::HirId, RegionObligation<'tcx>)>,
/// Caches for opaque type inference.
pub opaque_type_storage: OpaqueTypeStorage<'tcx>,
undo_log: InferCtxtUndoLogs<'tcx>,
// Opaque types found in explicit return types and their
// associated fresh inference variable. Writeback resolves these
// variables to get the concrete type, which can be used to
// 'de-opaque' OpaqueTypeDecl outside of type inference.
pub opaque_types: OpaqueTypeMap<'tcx>,
/// A map from inference variables created from opaque
/// type instantiations (`ty::Infer`) to the actual opaque
/// type (`ty::Opaque`). Used during fallback to map unconstrained
/// opaque type inference variables to their corresponding
/// opaque type.
pub opaque_types_vars: FxHashMap<Ty<'tcx>, Ty<'tcx>>,
}
impl<'tcx> InferCtxtInner<'tcx> {
@ -217,8 +207,7 @@ fn new() -> InferCtxtInner<'tcx> {
float_unification_storage: ut::UnificationTableStorage::new(),
region_constraint_storage: Some(RegionConstraintStorage::new()),
region_obligations: vec![],
opaque_types: Default::default(),
opaque_types_vars: Default::default(),
opaque_type_storage: Default::default(),
}
}
@ -237,6 +226,11 @@ fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
self.type_variable_storage.with_log(&mut self.undo_log)
}
#[inline]
fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
self.opaque_type_storage.with_log(&mut self.undo_log)
}
#[inline]
fn int_unification_table(
&mut self,

View File

@ -14,6 +14,10 @@
pub type OpaqueTypeMap<'tcx> = VecMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>;
mod table;
pub use table::{OpaqueTypeStorage, OpaqueTypeTable};
/// Information about the opaque types whose values we
/// are inferring in this function (these are the `impl Trait` that
/// appear in the return type).
@ -352,6 +356,10 @@ fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<hir::OpaqueTyOrigin>
};
in_definition_scope.then_some(*origin)
}
pub fn opaque_types(&self) -> OpaqueTypeMap<'tcx> {
self.inner.borrow().opaque_type_storage.opaque_types()
}
}
// Visitor that requires that (almost) all regions in the type visited outlive
@ -513,7 +521,9 @@ fn fold_opaque_ty(
// Use the same type variable if the exact same opaque type appears more
// than once in the return type (e.g., if it's passed to a type alias).
if let Some(opaque_defn) = infcx.inner.borrow().opaque_types.get(&opaque_type_key) {
if let Some(opaque_defn) =
infcx.inner.borrow().opaque_type_storage.get_decl(&opaque_type_key)
{
debug!("re-using cached concrete type {:?}", opaque_defn.concrete_ty.kind());
return opaque_defn.concrete_ty;
}
@ -530,14 +540,10 @@ fn fold_opaque_ty(
// Foo, impl Bar)`.
let definition_span = self.value_span;
{
let mut infcx = self.infcx.inner.borrow_mut();
infcx.opaque_types.insert(
OpaqueTypeKey { def_id, substs },
OpaqueTypeDecl { opaque_type: ty, definition_span, concrete_ty: ty_var, origin },
);
infcx.opaque_types_vars.insert(ty_var, ty);
}
self.infcx.inner.borrow_mut().opaque_types().register(
OpaqueTypeKey { def_id, substs },
OpaqueTypeDecl { opaque_type: ty, definition_span, concrete_ty: ty_var, origin },
);
debug!("generated new type inference var {:?}", ty_var.kind());

View File

@ -0,0 +1,69 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::undo_log::UndoLogs;
use rustc_middle::ty::{OpaqueTypeKey, Ty};
use crate::infer::InferCtxtUndoLogs;
use super::{OpaqueTypeDecl, OpaqueTypeMap};
#[derive(Default)]
pub struct OpaqueTypeStorage<'tcx> {
// Opaque types found in explicit return types and their
// associated fresh inference variable. Writeback resolves these
// variables to get the concrete type, which can be used to
// 'de-opaque' OpaqueTypeDecl, after typeck is done with all functions.
pub opaque_types: OpaqueTypeMap<'tcx>,
/// A map from inference variables created from opaque
/// type instantiations (`ty::Infer`) to the actual opaque
/// type (`ty::Opaque`). Used during fallback to map unconstrained
/// opaque type inference variables to their corresponding
/// opaque type.
pub opaque_types_vars: FxHashMap<Ty<'tcx>, Ty<'tcx>>,
}
impl<'tcx> OpaqueTypeStorage<'tcx> {
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"),
Some(decl) => assert_ne!(self.opaque_types_vars.remove(decl.concrete_ty), None),
}
}
pub fn get_decl(&self, key: &OpaqueTypeKey<'tcx>) -> Option<&OpaqueTypeDecl<'tcx>> {
self.opaque_types.get(key)
}
pub fn get_opaque_type_for_infer_var(&self, key: Ty<'tcx>) -> Option<Ty<'tcx>> {
self.opaque_types_vars.get(key).copied()
}
pub fn opaque_types(&self) -> OpaqueTypeMap<'tcx> {
self.opaque_types.clone()
}
pub fn take_opaque_types(&mut self) -> OpaqueTypeMap<'tcx> {
std::mem::take(&mut self.opaque_types)
}
#[inline]
pub(crate) fn with_log<'a>(
&'a mut self,
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
) -> OpaqueTypeTable<'a, 'tcx> {
OpaqueTypeTable { storage: self, undo_log }
}
}
pub struct OpaqueTypeTable<'a, 'tcx> {
storage: &'a mut OpaqueTypeStorage<'tcx>,
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
}
impl<'a, 'tcx> OpaqueTypeTable<'a, 'tcx> {
pub fn register(&mut self, key: OpaqueTypeKey<'tcx>, decl: OpaqueTypeDecl<'tcx>) {
self.undo_log.push(key);
self.storage.opaque_types.insert(key, decl);
self.storage.opaque_types_vars.insert(decl.concrete_ty, decl.opaque_type);
}
}

View File

@ -4,7 +4,7 @@
use rustc_data_structures::undo_log::{Rollback, UndoLogs};
use rustc_data_structures::unify as ut;
use rustc_middle::infer::unify_key::RegionVidKey;
use rustc_middle::ty;
use rustc_middle::ty::{self, OpaqueTypeKey};
use crate::{
infer::{region_constraints, type_variable, InferCtxtInner},
@ -18,6 +18,7 @@ pub struct Snapshot<'tcx> {
/// Records the "undo" data for a single operation that affects some form of inference variable.
pub(crate) enum UndoLog<'tcx> {
OpaqueTypes(OpaqueTypeKey<'tcx>),
TypeVariables(type_variable::UndoLog<'tcx>),
ConstUnificationTable(sv::UndoLog<ut::Delegate<ty::ConstVid<'tcx>>>),
IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
@ -42,6 +43,7 @@ fn from(x: $ty) -> Self {
// Upcast from a single kind of "undoable action" to the general enum
impl_from! {
OpaqueTypes(OpaqueTypeKey<'tcx>),
RegionConstraintCollector(region_constraints::UndoLog<'tcx>),
TypeVariables(type_variable::UndoLog<'tcx>),
@ -64,6 +66,7 @@ fn from(x: $ty) -> Self {
impl<'tcx> Rollback<UndoLog<'tcx>> for InferCtxtInner<'tcx> {
fn reverse(&mut self, undo: UndoLog<'tcx>) {
match undo {
UndoLog::OpaqueTypes(key) => self.opaque_type_storage.remove(key),
UndoLog::TypeVariables(undo) => self.type_variable_storage.reverse(undo),
UndoLog::ConstUnificationTable(undo) => self.const_unification_storage.reverse(undo),
UndoLog::IntUnificationTable(undo) => self.int_unification_storage.reverse(undo),

View File

@ -647,8 +647,7 @@ fn check_opaque_meets_bounds<'tcx>(
infcx.instantiate_opaque_types(hir_id, param_env, opaque_ty, span),
);
let opaque_type_map = infcx.inner.borrow().opaque_types.clone();
for (OpaqueTypeKey { def_id, substs }, opaque_defn) in opaque_type_map {
for (OpaqueTypeKey { def_id, substs }, opaque_defn) in infcx.opaque_types() {
let hidden_type = tcx.type_of(def_id).subst(tcx, substs);
trace!(?hidden_type);
match infcx.at(&misc_cause, param_env).eq(opaque_defn.concrete_ty, hidden_type) {

View File

@ -176,7 +176,7 @@ fn fallback_opaque_type_vars(&self, ty: Ty<'tcx>) -> bool {
.type_var_origin(ty)
.map(|origin| origin.span)
.unwrap_or(rustc_span::DUMMY_SP);
let oty = self.inner.borrow().opaque_types_vars.get(ty).copied();
let oty = self.inner.borrow().opaque_type_storage.get_opaque_type_for_infer_var(ty);
if let Some(opaque_ty) = oty {
debug!(
"fallback_opaque_type_vars(ty={:?}): falling back to opaque type {:?}",

View File

@ -498,8 +498,7 @@ fn visit_generator_interior_types(&mut self) {
#[instrument(skip(self, span), level = "debug")]
fn visit_opaque_types(&mut self, span: Span) {
let opaque_types = self.fcx.infcx.inner.borrow().opaque_types.clone();
for (opaque_type_key, opaque_defn) in opaque_types {
for (opaque_type_key, opaque_defn) in self.fcx.infcx.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);