From: Markus Westerlind Date: Fri, 17 Apr 2020 06:54:28 +0000 (+0200) Subject: Fix review comments X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=bc2fc7fb80d6f55a018e24de07d1ebdbc79d9136;p=rust.git Fix review comments --- diff --git a/src/librustc_infer/infer/mod.rs b/src/librustc_infer/infer/mod.rs index 0e0ab8550c6..67d330ac892 100644 --- a/src/librustc_infer/infer/mod.rs +++ b/src/librustc_infer/infer/mod.rs @@ -13,7 +13,7 @@ use rustc_ast::ast; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::sync::Lrc; -use rustc_data_structures::undo_log::{Rollback, Snapshots}; +use rustc_data_structures::undo_log::Rollback; use rustc_data_structures::unify as ut; use rustc_errors::DiagnosticBuilder; use rustc_hir as hir; @@ -151,16 +151,16 @@ pub struct InferCtxtInner<'tcx> { /// We instantiate `UnificationTable` with `bounds` because the types /// that might instantiate a general type variable have an order, /// represented by its upper and lower bounds. - type_variables: type_variable::TypeVariableStorage<'tcx>, + type_variable_storage: type_variable::TypeVariableStorage<'tcx>, /// Map from const parameter variable to the kind of const it represents. - const_unification_table: ut::UnificationTableStorage>, + const_unification_storage: ut::UnificationTableStorage>, /// Map from integral variable to the kind of integer it represents. - int_unification_table: ut::UnificationTableStorage, + int_unification_storage: ut::UnificationTableStorage, /// Map from floating variable to the kind of float it represents. - float_unification_table: ut::UnificationTableStorage, + float_unification_storage: ut::UnificationTableStorage, /// Tracks the set of region variables and the constraints between them. /// This is initially `Some(_)` but when @@ -209,11 +209,11 @@ impl<'tcx> InferCtxtInner<'tcx> { fn new() -> InferCtxtInner<'tcx> { InferCtxtInner { projection_cache: Default::default(), - type_variables: type_variable::TypeVariableStorage::new(), + type_variable_storage: type_variable::TypeVariableStorage::new(), undo_log: InferCtxtUndoLogs::default(), - const_unification_table: ut::UnificationTableStorage::new(), - int_unification_table: ut::UnificationTableStorage::new(), - float_unification_table: ut::UnificationTableStorage::new(), + const_unification_storage: ut::UnificationTableStorage::new(), + int_unification_storage: ut::UnificationTableStorage::new(), + float_unification_storage: ut::UnificationTableStorage::new(), region_constraints: Some(RegionConstraintStorage::new()), region_obligations: vec![], } @@ -223,12 +223,12 @@ pub fn region_obligations(&self) -> &[(hir::HirId, RegionObligation<'tcx>)] { &self.region_obligations } - pub fn projection_cache(&mut self) -> traits::ProjectionCache<'tcx, '_> { + pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> { self.projection_cache.with_log(&mut self.undo_log) } - fn type_variables(&mut self) -> type_variable::TypeVariableTable<'tcx, '_> { - self.type_variables.with_log(&mut self.undo_log) + fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> { + self.type_variable_storage.with_log(&mut self.undo_log) } fn int_unification_table( @@ -240,7 +240,7 @@ fn int_unification_table( &mut InferCtxtUndoLogs<'tcx>, >, > { - self.int_unification_table.with_log(&mut self.undo_log) + self.int_unification_storage.with_log(&mut self.undo_log) } fn float_unification_table( @@ -252,7 +252,7 @@ fn float_unification_table( &mut InferCtxtUndoLogs<'tcx>, >, > { - self.float_unification_table.with_log(&mut self.undo_log) + self.float_unification_storage.with_log(&mut self.undo_log) } fn const_unification_table( @@ -264,7 +264,7 @@ fn const_unification_table( &mut InferCtxtUndoLogs<'tcx>, >, > { - self.const_unification_table.with_log(&mut self.undo_log) + self.const_unification_storage.with_log(&mut self.undo_log) } pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'tcx, '_> { @@ -868,29 +868,7 @@ fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'a, 'tcx>) { self.in_snapshot.set(was_in_snapshot); self.universe.set(universe); - let InferCtxtInner { - type_variables, - const_unification_table, - int_unification_table, - float_unification_table, - region_constraints, - projection_cache, - region_obligations, - undo_log, - .. - } = &mut *self.inner.borrow_mut(); - undo_log.rollback_to( - || undo_log::RollbackView { - type_variables, - const_unification_table, - int_unification_table, - float_unification_table, - region_constraints: region_constraints.as_mut().unwrap(), - projection_cache, - region_obligations, - }, - undo_snapshot, - ); + self.inner.borrow_mut().rollback_to(undo_snapshot); } fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) { @@ -900,8 +878,7 @@ fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) { self.in_snapshot.set(was_in_snapshot); - let mut inner = self.inner.borrow_mut(); - inner.undo_log.commit(undo_snapshot); + self.inner.borrow_mut().commit(undo_snapshot); } /// Executes `f` and commit the bindings. diff --git a/src/librustc_infer/infer/type_variable.rs b/src/librustc_infer/infer/type_variable.rs index 78803eea3bc..68f84d4d1c3 100644 --- a/src/librustc_infer/infer/type_variable.rs +++ b/src/librustc_infer/infer/type_variable.rs @@ -11,7 +11,7 @@ use std::marker::PhantomData; use std::ops::Range; -use rustc_data_structures::undo_log::{Rollback, Snapshots, UndoLogs}; +use rustc_data_structures::undo_log::{Rollback, UndoLogs}; pub(crate) enum UndoLog<'tcx> { EqRelation(sv::UndoLog>>), @@ -81,7 +81,7 @@ pub struct TypeVariableStorage<'tcx> { sub_relations: ut::UnificationTableStorage, } -pub struct TypeVariableTable<'tcx, 'a> { +pub struct TypeVariableTable<'a, 'tcx> { values: &'a mut sv::SnapshotVecStorage, eq_relations: &'a mut ut::UnificationTableStorage>, @@ -168,13 +168,13 @@ pub fn new() -> TypeVariableStorage<'tcx> { pub(crate) fn with_log<'a>( &'a mut self, undo_log: &'a mut InferCtxtUndoLogs<'tcx>, - ) -> TypeVariableTable<'tcx, 'a> { + ) -> TypeVariableTable<'a, 'tcx> { let TypeVariableStorage { values, eq_relations, sub_relations } = self; TypeVariableTable { values, eq_relations, sub_relations, undo_log } } } -impl<'tcx> TypeVariableTable<'tcx, '_> { +impl<'tcx> TypeVariableTable<'_, 'tcx> { /// Returns the diverges flag given when `vid` was created. /// /// Note that this function does not return care whether diff --git a/src/librustc_infer/infer/undo_log.rs b/src/librustc_infer/infer/undo_log.rs index 0d94e32754a..5bb8ce8f66b 100644 --- a/src/librustc_infer/infer/undo_log.rs +++ b/src/librustc_infer/infer/undo_log.rs @@ -1,16 +1,12 @@ use std::marker::PhantomData; use rustc_data_structures::snapshot_vec as sv; -use rustc_data_structures::undo_log::{Rollback, Snapshots, UndoLogs}; +use rustc_data_structures::undo_log::{Rollback, UndoLogs}; use rustc_data_structures::unify as ut; -use rustc_hir as hir; use rustc_middle::ty; use crate::{ - infer::{ - region_constraints::{self, RegionConstraintStorage}, - type_variable, RegionObligation, - }, + infer::{region_constraints, type_variable, InferCtxtInner}, traits, }; @@ -19,6 +15,7 @@ pub struct Snapshot<'tcx> { _marker: PhantomData<&'tcx ()>, } +/// Records the 'undo' data fora single operation that affects some form of inference variable. pub(crate) enum UndoLog<'tcx> { TypeVariables(type_variable::UndoLog<'tcx>), ConstUnificationTable(sv::UndoLog>>), @@ -96,26 +93,18 @@ fn from(l: traits::UndoLog<'tcx>) -> Self { } } -pub(super) struct RollbackView<'tcx, 'a> { - pub(super) type_variables: &'a mut type_variable::TypeVariableStorage<'tcx>, - pub(super) const_unification_table: &'a mut ut::UnificationTableStorage>, - pub(super) int_unification_table: &'a mut ut::UnificationTableStorage, - pub(super) float_unification_table: &'a mut ut::UnificationTableStorage, - pub(super) region_constraints: &'a mut RegionConstraintStorage<'tcx>, - pub(super) projection_cache: &'a mut traits::ProjectionCacheStorage<'tcx>, - pub(super) region_obligations: &'a mut Vec<(hir::HirId, RegionObligation<'tcx>)>, -} - -impl<'tcx> Rollback> for RollbackView<'tcx, '_> { +impl<'tcx> Rollback> for InferCtxtInner<'tcx> { fn reverse(&mut self, undo: UndoLog<'tcx>) { match undo { - UndoLog::TypeVariables(undo) => self.type_variables.reverse(undo), - UndoLog::ConstUnificationTable(undo) => self.const_unification_table.reverse(undo), - UndoLog::IntUnificationTable(undo) => self.int_unification_table.reverse(undo), - UndoLog::FloatUnificationTable(undo) => self.float_unification_table.reverse(undo), - UndoLog::RegionConstraintCollector(undo) => self.region_constraints.reverse(undo), + 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), + UndoLog::FloatUnificationTable(undo) => self.float_unification_storage.reverse(undo), + UndoLog::RegionConstraintCollector(undo) => { + self.region_constraints.as_mut().unwrap().reverse(undo) + } UndoLog::RegionUnificationTable(undo) => { - self.region_constraints.unification_table.reverse(undo) + self.region_constraints.as_mut().unwrap().unification_table.reverse(undo) } UndoLog::ProjectionCache(undo) => self.projection_cache.reverse(undo), UndoLog::PushRegionObligation => { @@ -163,58 +152,52 @@ fn extend(&mut self, undos: J) } } -impl<'tcx> Snapshots> for InferCtxtUndoLogs<'tcx> { - type Snapshot = Snapshot<'tcx>; - fn actions_since_snapshot(&self, snapshot: &Self::Snapshot) -> &[UndoLog<'tcx>] { - &self.logs[snapshot.undo_len..] - } - - fn start_snapshot(&mut self) -> Self::Snapshot { - self.num_open_snapshots += 1; - Snapshot { undo_len: self.logs.len(), _marker: PhantomData } - } - - fn rollback_to(&mut self, values: impl FnOnce() -> R, snapshot: Self::Snapshot) - where - R: Rollback>, - { +impl<'tcx> InferCtxtInner<'tcx> { + pub fn rollback_to(&mut self, snapshot: Snapshot<'tcx>) { debug!("rollback_to({})", snapshot.undo_len); - self.assert_open_snapshot(&snapshot); + self.undo_log.assert_open_snapshot(&snapshot); - if self.logs.len() > snapshot.undo_len { - let mut values = values(); - while self.logs.len() > snapshot.undo_len { - values.reverse(self.logs.pop().unwrap()); - } + while self.undo_log.logs.len() > snapshot.undo_len { + let undo = self.undo_log.logs.pop().unwrap(); + self.reverse(undo); } - if self.num_open_snapshots == 1 { + if self.undo_log.num_open_snapshots == 1 { // The root snapshot. It's safe to clear the undo log because // there's no snapshot further out that we might need to roll back // to. assert!(snapshot.undo_len == 0); - self.logs.clear(); + self.undo_log.logs.clear(); } - self.num_open_snapshots -= 1; + self.undo_log.num_open_snapshots -= 1; } - fn commit(&mut self, snapshot: Self::Snapshot) { + pub fn commit(&mut self, snapshot: Snapshot<'tcx>) { debug!("commit({})", snapshot.undo_len); - if self.num_open_snapshots == 1 { + if self.undo_log.num_open_snapshots == 1 { // The root snapshot. It's safe to clear the undo log because // there's no snapshot further out that we might need to roll back // to. assert!(snapshot.undo_len == 0); - self.logs.clear(); + self.undo_log.logs.clear(); } - self.num_open_snapshots -= 1; + self.undo_log.num_open_snapshots -= 1; } } impl<'tcx> InferCtxtUndoLogs<'tcx> { + pub fn actions_since_snapshot(&self, snapshot: &Snapshot<'tcx>) -> &[UndoLog<'tcx>] { + &self.logs[snapshot.undo_len..] + } + + pub fn start_snapshot(&mut self) -> Snapshot<'tcx> { + self.num_open_snapshots += 1; + Snapshot { undo_len: self.logs.len(), _marker: PhantomData } + } + pub(crate) fn region_constraints_in_snapshot( &self, s: &Snapshot<'tcx>, diff --git a/src/librustc_infer/traits/project.rs b/src/librustc_infer/traits/project.rs index 17bc54eed40..f0d21a7d022 100644 --- a/src/librustc_infer/traits/project.rs +++ b/src/librustc_infer/traits/project.rs @@ -65,7 +65,7 @@ pub fn with(self, value: U) -> Normalized<'tcx, U> { // // FIXME: we probably also want some sort of cross-infcx cache here to // reduce the amount of duplication. Let's see what we get with the Chalk reforms. -pub struct ProjectionCache<'tcx, 'a> { +pub struct ProjectionCache<'a, 'tcx> { map: &'a mut SnapshotMapStorage, ProjectionCacheEntry<'tcx>>, undo_log: &'a mut InferCtxtUndoLogs<'tcx>, } @@ -98,12 +98,12 @@ impl<'tcx> ProjectionCacheStorage<'tcx> { pub(crate) fn with_log<'a>( &'a mut self, undo_log: &'a mut InferCtxtUndoLogs<'tcx>, - ) -> ProjectionCache<'tcx, 'a> { + ) -> ProjectionCache<'a, 'tcx> { ProjectionCache { map: &mut self.map, undo_log } } } -impl<'tcx> ProjectionCache<'tcx, '_> { +impl<'tcx> ProjectionCache<'_, 'tcx> { fn map( &mut self, ) -> SnapshotMapRef<