1
Fork 0

Auto merge of #118742 - lcnr:infer-var-cleanup, r=compiler-errors

refactor infer var storage

cleanup the code, discovered while working on #118725
This commit is contained in:
bors 2023-12-12 03:23:37 +00:00
commit 5f5d248602
7 changed files with 51 additions and 106 deletions

View file

@ -24,9 +24,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
self.fulfillment_cx.borrow_mut().pending_obligations() self.fulfillment_cx.borrow_mut().pending_obligations()
); );
let fallback_occured = self.fallback_types() | self.fallback_effects(); let fallback_occurred = self.fallback_types() | self.fallback_effects();
if !fallback_occured { if !fallback_occurred {
return; return;
} }
@ -57,24 +57,25 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
} }
fn fallback_types(&self) -> bool { fn fallback_types(&self) -> bool {
// Check if we have any unsolved variables. If not, no need for fallback. // Check if we have any unresolved variables. If not, no need for fallback.
let unsolved_variables = self.unsolved_variables(); let unresolved_variables = self.unresolved_variables();
if unsolved_variables.is_empty() { if unresolved_variables.is_empty() {
return false; return false;
} }
let diverging_fallback = self.calculate_diverging_fallback(&unsolved_variables); let diverging_fallback = self.calculate_diverging_fallback(&unresolved_variables);
// We do fallback in two passes, to try to generate // We do fallback in two passes, to try to generate
// better error messages. // better error messages.
// The first time, we do *not* replace opaque types. // The first time, we do *not* replace opaque types.
for ty in unsolved_variables { let mut fallback_occurred = false;
for ty in unresolved_variables {
debug!("unsolved_variable = {:?}", ty); debug!("unsolved_variable = {:?}", ty);
self.fallback_if_possible(ty, &diverging_fallback); fallback_occurred |= self.fallback_if_possible(ty, &diverging_fallback);
} }
true fallback_occurred
} }
fn fallback_effects(&self) -> bool { fn fallback_effects(&self) -> bool {
@ -84,9 +85,8 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
return false; return false;
} }
// not setting `fallback_has_occured` here because that field is only used for type fallback // not setting the `fallback_has_occured` field here because
// diagnostics. // that field is only used for type fallback diagnostics.
for effect in unsolved_effects { for effect in unsolved_effects {
let expected = self.tcx.consts.true_; let expected = self.tcx.consts.true_;
let cause = self.misc(rustc_span::DUMMY_SP); let cause = self.misc(rustc_span::DUMMY_SP);
@ -122,7 +122,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
&self, &self,
ty: Ty<'tcx>, ty: Ty<'tcx>,
diverging_fallback: &UnordMap<Ty<'tcx>, Ty<'tcx>>, diverging_fallback: &UnordMap<Ty<'tcx>, Ty<'tcx>>,
) { ) -> bool {
// Careful: we do NOT shallow-resolve `ty`. We know that `ty` // Careful: we do NOT shallow-resolve `ty`. We know that `ty`
// is an unsolved variable, and we determine its fallback // is an unsolved variable, and we determine its fallback
// based solely on how it was created, not what other type // based solely on how it was created, not what other type
@ -147,7 +147,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64, ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64,
_ => match diverging_fallback.get(&ty) { _ => match diverging_fallback.get(&ty) {
Some(&fallback_ty) => fallback_ty, Some(&fallback_ty) => fallback_ty,
None => return, None => return false,
}, },
}; };
debug!("fallback_if_possible(ty={:?}): defaulting to `{:?}`", ty, fallback); debug!("fallback_if_possible(ty={:?}): defaulting to `{:?}`", ty, fallback);
@ -159,6 +159,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
.unwrap_or(rustc_span::DUMMY_SP); .unwrap_or(rustc_span::DUMMY_SP);
self.demand_eqtype(span, ty, fallback); self.demand_eqtype(span, ty, fallback);
self.fallback_has_occurred.set(true); self.fallback_has_occurred.set(true);
true
} }
/// The "diverging fallback" system is rather complicated. This is /// The "diverging fallback" system is rather complicated. This is
@ -230,9 +231,9 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
/// any variable that has an edge into `D`. /// any variable that has an edge into `D`.
fn calculate_diverging_fallback( fn calculate_diverging_fallback(
&self, &self,
unsolved_variables: &[Ty<'tcx>], unresolved_variables: &[Ty<'tcx>],
) -> UnordMap<Ty<'tcx>, Ty<'tcx>> { ) -> UnordMap<Ty<'tcx>, Ty<'tcx>> {
debug!("calculate_diverging_fallback({:?})", unsolved_variables); debug!("calculate_diverging_fallback({:?})", unresolved_variables);
// Construct a coercion graph where an edge `A -> B` indicates // Construct a coercion graph where an edge `A -> B` indicates
// a type variable is that is coerced // a type variable is that is coerced
@ -240,7 +241,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
// Extract the unsolved type inference variable vids; note that some // Extract the unsolved type inference variable vids; note that some
// unsolved variables are integer/float variables and are excluded. // unsolved variables are integer/float variables and are excluded.
let unsolved_vids = unsolved_variables.iter().filter_map(|ty| ty.ty_vid()); let unsolved_vids = unresolved_variables.iter().filter_map(|ty| ty.ty_vid());
// Compute the diverging root vids D -- that is, the root vid of // Compute the diverging root vids D -- that is, the root vid of
// those type variables that (a) are the target of a coercion from // those type variables that (a) are the target of a coercion from

View file

@ -166,10 +166,7 @@ impl<'tcx> InferCtxt<'tcx> {
} }
fn take_opaque_types_for_query_response(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> { fn take_opaque_types_for_query_response(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types) self.take_opaque_types().into_iter().map(|(k, v)| (k, v.hidden_type.ty)).collect()
.into_iter()
.map(|(k, v)| (k, v.hidden_type.ty))
.collect()
} }
/// Given the (canonicalized) result to a canonical query, /// Given the (canonicalized) result to a canonical query,

View file

@ -295,7 +295,7 @@ where
ty::Covariant | ty::Contravariant => (), ty::Covariant | ty::Contravariant => (),
} }
let origin = *inner.type_variables().var_origin(vid); let origin = inner.type_variables().var_origin(vid);
let new_var_id = let new_var_id =
inner.type_variables().new_var(self.for_universe, origin); inner.type_variables().new_var(self.for_universe, origin);
let u = Ty::new_var(self.tcx(), new_var_id); let u = Ty::new_var(self.tcx(), new_var_id);

View file

@ -98,6 +98,8 @@ pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
/// call to `start_snapshot` and `rollback_to`. /// call to `start_snapshot` and `rollback_to`.
#[derive(Clone)] #[derive(Clone)]
pub struct InferCtxtInner<'tcx> { pub struct InferCtxtInner<'tcx> {
undo_log: InferCtxtUndoLogs<'tcx>,
/// Cache for projections. /// Cache for projections.
/// ///
/// This cache is snapshotted along with the infcx. /// This cache is snapshotted along with the infcx.
@ -162,8 +164,6 @@ pub struct InferCtxtInner<'tcx> {
/// that all type inference variables have been bound and so forth. /// that all type inference variables have been bound and so forth.
region_obligations: Vec<RegionObligation<'tcx>>, region_obligations: Vec<RegionObligation<'tcx>>,
undo_log: InferCtxtUndoLogs<'tcx>,
/// Caches for opaque type inference. /// Caches for opaque type inference.
opaque_type_storage: OpaqueTypeStorage<'tcx>, opaque_type_storage: OpaqueTypeStorage<'tcx>,
} }
@ -171,9 +171,10 @@ pub struct InferCtxtInner<'tcx> {
impl<'tcx> InferCtxtInner<'tcx> { impl<'tcx> InferCtxtInner<'tcx> {
fn new() -> InferCtxtInner<'tcx> { fn new() -> InferCtxtInner<'tcx> {
InferCtxtInner { InferCtxtInner {
undo_log: InferCtxtUndoLogs::default(),
projection_cache: Default::default(), projection_cache: Default::default(),
type_variable_storage: type_variable::TypeVariableStorage::new(), type_variable_storage: type_variable::TypeVariableStorage::new(),
undo_log: InferCtxtUndoLogs::default(),
const_unification_storage: ut::UnificationTableStorage::new(), const_unification_storage: ut::UnificationTableStorage::new(),
int_unification_storage: ut::UnificationTableStorage::new(), int_unification_storage: ut::UnificationTableStorage::new(),
float_unification_storage: ut::UnificationTableStorage::new(), float_unification_storage: ut::UnificationTableStorage::new(),
@ -783,7 +784,7 @@ impl<'tcx> InferCtxt<'tcx> {
pub fn type_var_origin(&self, ty: Ty<'tcx>) -> Option<TypeVariableOrigin> { pub fn type_var_origin(&self, ty: Ty<'tcx>) -> Option<TypeVariableOrigin> {
match *ty.kind() { match *ty.kind() {
ty::Infer(ty::TyVar(vid)) => { ty::Infer(ty::TyVar(vid)) => {
Some(*self.inner.borrow_mut().type_variables().var_origin(vid)) Some(self.inner.borrow_mut().type_variables().var_origin(vid))
} }
_ => None, _ => None,
} }
@ -793,11 +794,11 @@ impl<'tcx> InferCtxt<'tcx> {
freshen::TypeFreshener::new(self) freshen::TypeFreshener::new(self)
} }
pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> { pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>> {
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
let mut vars: Vec<Ty<'_>> = inner let mut vars: Vec<Ty<'_>> = inner
.type_variables() .type_variables()
.unsolved_variables() .unresolved_variables()
.into_iter() .into_iter()
.map(|t| Ty::new_var(self.tcx, t)) .map(|t| Ty::new_var(self.tcx, t))
.collect(); .collect();
@ -1306,12 +1307,7 @@ impl<'tcx> InferCtxt<'tcx> {
pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin { pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
let inner = &mut *inner; let inner = &mut *inner;
inner inner.unwrap_region_constraints().var_origin(vid)
.region_constraint_storage
.as_mut()
.expect("regions already resolved")
.with_log(&mut inner.undo_log)
.var_origin(vid)
} }
/// Clone the list of variable regions. This is used only during NLL processing /// Clone the list of variable regions. This is used only during NLL processing

View file

@ -134,7 +134,7 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for UnresolvedTypeOrConstFinder<'a, 'tc
if let TypeVariableOrigin { if let TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(_, _), kind: TypeVariableOriginKind::TypeParameterDefinition(_, _),
span, span,
} = *ty_vars.var_origin(ty_vid) } = ty_vars.var_origin(ty_vid)
{ {
Some(span) Some(span)
} else { } else {

View file

@ -1,4 +1,5 @@
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::ty::{self, Ty, TyVid}; use rustc_middle::ty::{self, Ty, TyVid};
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use rustc_span::Span; use rustc_span::Span;
@ -11,14 +12,13 @@ use std::cmp;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ops::Range; use std::ops::Range;
use rustc_data_structures::undo_log::{Rollback, UndoLogs}; use rustc_data_structures::undo_log::Rollback;
/// Represents a single undo-able action that affects a type inference variable. /// Represents a single undo-able action that affects a type inference variable.
#[derive(Clone)] #[derive(Clone)]
pub(crate) enum UndoLog<'tcx> { pub(crate) enum UndoLog<'tcx> {
EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>), EqRelation(sv::UndoLog<ut::Delegate<TyVidEqKey<'tcx>>>),
SubRelation(sv::UndoLog<ut::Delegate<ty::TyVid>>), SubRelation(sv::UndoLog<ut::Delegate<ty::TyVid>>),
Values(sv::UndoLog<Delegate>),
} }
/// Convert from a specific kind of undo to the more general UndoLog /// Convert from a specific kind of undo to the more general UndoLog
@ -35,34 +35,19 @@ impl<'tcx> From<sv::UndoLog<ut::Delegate<ty::TyVid>>> for UndoLog<'tcx> {
} }
} }
/// Convert from a specific kind of undo to the more general UndoLog
impl<'tcx> From<sv::UndoLog<Delegate>> for UndoLog<'tcx> {
fn from(l: sv::UndoLog<Delegate>) -> Self {
UndoLog::Values(l)
}
}
/// Convert from a specific kind of undo to the more general UndoLog
impl<'tcx> From<Instantiate> for UndoLog<'tcx> {
fn from(l: Instantiate) -> Self {
UndoLog::Values(sv::UndoLog::Other(l))
}
}
impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> { impl<'tcx> Rollback<UndoLog<'tcx>> for TypeVariableStorage<'tcx> {
fn reverse(&mut self, undo: UndoLog<'tcx>) { fn reverse(&mut self, undo: UndoLog<'tcx>) {
match undo { match undo {
UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo), UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
UndoLog::SubRelation(undo) => self.sub_relations.reverse(undo), UndoLog::SubRelation(undo) => self.sub_relations.reverse(undo),
UndoLog::Values(undo) => self.values.reverse(undo),
} }
} }
} }
#[derive(Clone)] #[derive(Clone)]
pub struct TypeVariableStorage<'tcx> { pub struct TypeVariableStorage<'tcx> {
values: sv::SnapshotVecStorage<Delegate>, /// The origins of each type variable.
values: IndexVec<TyVid, TypeVariableData>,
/// Two variables are unified in `eq_relations` when we have a /// Two variables are unified in `eq_relations` when we have a
/// constraint `?X == ?Y`. This table also stores, for each key, /// constraint `?X == ?Y`. This table also stores, for each key,
/// the known value. /// the known value.
@ -168,15 +153,10 @@ impl<'tcx> TypeVariableValue<'tcx> {
} }
} }
#[derive(Clone)]
pub(crate) struct Instantiate;
pub(crate) struct Delegate;
impl<'tcx> TypeVariableStorage<'tcx> { impl<'tcx> TypeVariableStorage<'tcx> {
pub fn new() -> TypeVariableStorage<'tcx> { pub fn new() -> TypeVariableStorage<'tcx> {
TypeVariableStorage { TypeVariableStorage {
values: sv::SnapshotVecStorage::new(), values: Default::default(),
eq_relations: ut::UnificationTableStorage::new(), eq_relations: ut::UnificationTableStorage::new(),
sub_relations: ut::UnificationTableStorage::new(), sub_relations: ut::UnificationTableStorage::new(),
} }
@ -194,6 +174,11 @@ impl<'tcx> TypeVariableStorage<'tcx> {
pub(crate) fn eq_relations_ref(&self) -> &ut::UnificationTableStorage<TyVidEqKey<'tcx>> { pub(crate) fn eq_relations_ref(&self) -> &ut::UnificationTableStorage<TyVidEqKey<'tcx>> {
&self.eq_relations &self.eq_relations
} }
pub(super) fn finalize_rollback(&mut self) {
debug_assert!(self.values.len() >= self.eq_relations.len());
self.values.truncate(self.eq_relations.len());
}
} }
impl<'tcx> TypeVariableTable<'_, 'tcx> { impl<'tcx> TypeVariableTable<'_, 'tcx> {
@ -201,8 +186,8 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
/// ///
/// Note that this function does not return care whether /// Note that this function does not return care whether
/// `vid` has been unified with something else or not. /// `vid` has been unified with something else or not.
pub fn var_origin(&self, vid: ty::TyVid) -> &TypeVariableOrigin { pub fn var_origin(&self, vid: ty::TyVid) -> TypeVariableOrigin {
&self.storage.values.get(vid.as_usize()).origin self.storage.values[vid].origin
} }
/// Records that `a == b`, depending on `dir`. /// Records that `a == b`, depending on `dir`.
@ -237,11 +222,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
self.eq_relations().probe_value(vid) self.eq_relations().probe_value(vid)
); );
self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty }); self.eq_relations().union_value(vid, TypeVariableValue::Known { value: ty });
// Hack: we only need this so that `types_escaping_snapshot`
// can see what has been unified; see the Delegate impl for
// more details.
self.undo_log.push(Instantiate);
} }
/// Creates a new type variable. /// Creates a new type variable.
@ -262,14 +242,14 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe }); let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe });
let sub_key = self.sub_relations().new_key(()); let sub_key = self.sub_relations().new_key(());
assert_eq!(eq_key.vid, sub_key); debug_assert_eq!(eq_key.vid, sub_key);
let index = self.values().push(TypeVariableData { origin }); let index = self.storage.values.push(TypeVariableData { origin });
assert_eq!(eq_key.vid.as_u32(), index as u32); debug_assert_eq!(eq_key.vid, index);
debug!("new_var(index={:?}, universe={:?}, origin={:?})", eq_key.vid, universe, origin); debug!("new_var(index={:?}, universe={:?}, origin={:?})", eq_key.vid, universe, origin);
eq_key.vid index
} }
/// Returns the number of type variables created thus far. /// Returns the number of type variables created thus far.
@ -329,13 +309,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
} }
} }
#[inline]
fn values(
&mut self,
) -> sv::SnapshotVec<Delegate, &mut Vec<TypeVariableData>, &mut InferCtxtUndoLogs<'tcx>> {
self.storage.values.with_log(self.undo_log)
}
#[inline] #[inline]
fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> { fn eq_relations(&mut self) -> super::UnificationTable<'_, 'tcx, TyVidEqKey<'tcx>> {
self.storage.eq_relations.with_log(self.undo_log) self.storage.eq_relations.with_log(self.undo_log)
@ -354,16 +327,14 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars()); let range = TyVid::from_usize(value_count)..TyVid::from_usize(self.num_vars());
( (
range.start..range.end, range.start..range.end,
(range.start.as_usize()..range.end.as_usize()) (range.start..range.end).map(|index| self.var_origin(index)).collect(),
.map(|index| self.storage.values.get(index).origin)
.collect(),
) )
} }
/// Returns indices of all variables that are not yet /// Returns indices of all variables that are not yet
/// instantiated. /// instantiated.
pub fn unsolved_variables(&mut self) -> Vec<ty::TyVid> { pub fn unresolved_variables(&mut self) -> Vec<ty::TyVid> {
(0..self.storage.values.len()) (0..self.num_vars())
.filter_map(|i| { .filter_map(|i| {
let vid = ty::TyVid::from_usize(i); let vid = ty::TyVid::from_usize(i);
match self.probe(vid) { match self.probe(vid) {
@ -375,26 +346,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
} }
} }
impl sv::SnapshotVecDelegate for Delegate {
type Value = TypeVariableData;
type Undo = Instantiate;
fn reverse(_values: &mut Vec<TypeVariableData>, _action: Instantiate) {
// We don't actually have to *do* anything to reverse an
// instantiation; the value for a variable is stored in the
// `eq_relations` and hence its rollback code will handle
// it. In fact, we could *almost* just remove the
// `SnapshotVec` entirely, except that we would have to
// reproduce *some* of its logic, since we want to know which
// type variables have been instantiated since the snapshot
// was started, so we can implement `types_escaping_snapshot`.
//
// (If we extended the `UnificationTable` to let us see which
// values have been unified and so forth, that might also
// suffice.)
}
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/// These structs (a newtyped TyVid) are used as the unification key /// These structs (a newtyped TyVid) are used as the unification key

View file

@ -32,7 +32,7 @@ pub(crate) enum UndoLog<'tcx> {
} }
macro_rules! impl_from { macro_rules! impl_from {
($($ctor: ident ($ty: ty),)*) => { ($($ctor:ident ($ty:ty),)*) => {
$( $(
impl<'tcx> From<$ty> for UndoLog<'tcx> { impl<'tcx> From<$ty> for UndoLog<'tcx> {
fn from(x: $ty) -> Self { fn from(x: $ty) -> Self {
@ -50,8 +50,6 @@ impl_from! {
TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'tcx>>>), TypeVariables(sv::UndoLog<ut::Delegate<type_variable::TyVidEqKey<'tcx>>>),
TypeVariables(sv::UndoLog<ut::Delegate<ty::TyVid>>), TypeVariables(sv::UndoLog<ut::Delegate<ty::TyVid>>),
TypeVariables(sv::UndoLog<type_variable::Delegate>),
TypeVariables(type_variable::Instantiate),
IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>), IntUnificationTable(sv::UndoLog<ut::Delegate<ty::IntVid>>),
@ -140,6 +138,8 @@ impl<'tcx> InferCtxtInner<'tcx> {
self.reverse(undo); self.reverse(undo);
} }
self.type_variable_storage.finalize_rollback();
if self.undo_log.num_open_snapshots == 1 { if self.undo_log.num_open_snapshots == 1 {
// After the root snapshot the undo log should be empty. // After the root snapshot the undo log should be empty.
assert!(snapshot.undo_len == 0); assert!(snapshot.undo_len == 0);