diff --git a/compiler/rustc_infer/src/infer/higher_ranked/README.md b/compiler/rustc_infer/src/infer/higher_ranked/README.md deleted file mode 100644 index 533d0ef7e6c4b..0000000000000 --- a/compiler/rustc_infer/src/infer/higher_ranked/README.md +++ /dev/null @@ -1,8 +0,0 @@ -To learn more about how Higher-ranked trait bounds work in the _old_ trait -solver, see [this chapter][oldhrtb] of the rustc-dev-guide. - -To learn more about how they work in the _new_ trait solver, see [this -chapter][newhrtb]. - -[oldhrtb]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html -[newhrtb]: https://rustc-dev-guide.rust-lang.org/borrow_check/region_inference.html#placeholders-and-universes diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 73a25637e1ade..15cdd6a910e77 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1,23 +1,25 @@ -pub use self::at::DefineOpaqueTypes; -pub use self::freshen::TypeFreshener; -pub use self::lexical_region_resolve::RegionResolutionError; -pub use self::BoundRegionConversionTime::*; -pub use self::RegionVariableOrigin::*; -pub use self::SubregionOrigin::*; -pub use self::ValuePairs::*; +pub use at::DefineOpaqueTypes; +pub use freshen::TypeFreshener; +pub use lexical_region_resolve::RegionResolutionError; +pub use relate::combine::CombineFields; pub use relate::combine::ObligationEmittingRelation; -use rustc_data_structures::captures::Captures; -use rustc_data_structures::undo_log::UndoLogs; -use rustc_middle::infer::unify_key::EffectVarValue; -use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey}; - -use self::opaque_types::OpaqueTypeStorage; -pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog}; +pub use relate::StructurallyRelateAliases; +pub use rustc_middle::ty::IntVarValue; +pub use BoundRegionConversionTime::*; +pub use RegionVariableOrigin::*; +pub use SubregionOrigin::*; +pub use ValuePairs::*; use crate::traits::{ self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine, TraitEngineExt, }; - +use error_reporting::TypeErrCtxt; +use free_regions::RegionRelations; +use lexical_region_resolve::LexicalRegionResolutions; +use opaque_types::OpaqueTypeStorage; +use region_constraints::{GenericKind, VarInfos, VerifyBound}; +use region_constraints::{RegionConstraintCollector, RegionConstraintStorage}; +use rustc_data_structures::captures::Captures; use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::sync::Lrc; @@ -27,7 +29,9 @@ use rustc_errors::{Diag, DiagCtxt, ErrorGuaranteed}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues}; use rustc_middle::infer::unify_key::ConstVariableValue; +use rustc_middle::infer::unify_key::EffectVarValue; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType}; +use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey}; use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult}; use rustc_middle::mir::ConstraintCategory; use rustc_middle::traits::{select, DefiningAnchor}; @@ -36,33 +40,21 @@ use rustc_middle::ty::fold::BoundVarReplacerDelegate; use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::relate::RelateResult; use rustc_middle::ty::visit::TypeVisitableExt; -pub use rustc_middle::ty::IntVarValue; use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtxt}; use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid}; use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef}; use rustc_span::symbol::Symbol; use rustc_span::Span; - +use snapshot::undo_log::InferCtxtUndoLogs; use std::cell::{Cell, RefCell}; use std::fmt; - -use self::error_reporting::TypeErrCtxt; -use self::free_regions::RegionRelations; -use self::lexical_region_resolve::LexicalRegionResolutions; -use self::region_constraints::{GenericKind, VarInfos, VerifyBound}; -use self::region_constraints::{ - RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot, -}; -pub use self::relate::combine::CombineFields; -pub use self::relate::StructurallyRelateAliases; -use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; +use type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; pub mod at; pub mod canonical; pub mod error_reporting; pub mod free_regions; mod freshen; -mod fudge; mod lexical_region_resolve; pub mod opaque_types; pub mod outlives; @@ -70,8 +62,8 @@ mod projection; pub mod region_constraints; mod relate; pub mod resolve; +pub(crate) mod snapshot; pub mod type_variable; -mod undo_log; #[must_use] #[derive(Debug)] @@ -738,13 +730,6 @@ impl<'tcx> InferOk<'tcx, ()> { } } -#[must_use = "once you start a snapshot, you should always consume it"] -pub struct CombinedSnapshot<'tcx> { - undo_snapshot: Snapshot<'tcx>, - region_constraints_snapshot: RegionSnapshot, - universe: ty::UniverseIndex, -} - impl<'tcx> InferCtxt<'tcx> { pub fn dcx(&self) -> &'tcx DiagCtxt { self.tcx.dcx() @@ -842,90 +827,6 @@ impl<'tcx> InferCtxt<'tcx> { } } - pub fn in_snapshot(&self) -> bool { - UndoLogs::>::in_snapshot(&self.inner.borrow_mut().undo_log) - } - - pub fn num_open_snapshots(&self) -> usize { - UndoLogs::>::num_open_snapshots(&self.inner.borrow_mut().undo_log) - } - - fn start_snapshot(&self) -> CombinedSnapshot<'tcx> { - debug!("start_snapshot()"); - - let mut inner = self.inner.borrow_mut(); - - CombinedSnapshot { - undo_snapshot: inner.undo_log.start_snapshot(), - region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(), - universe: self.universe(), - } - } - - #[instrument(skip(self, snapshot), level = "debug")] - fn rollback_to(&self, snapshot: CombinedSnapshot<'tcx>) { - let CombinedSnapshot { undo_snapshot, region_constraints_snapshot, universe } = snapshot; - - self.universe.set(universe); - - let mut inner = self.inner.borrow_mut(); - inner.rollback_to(undo_snapshot); - inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot); - } - - #[instrument(skip(self, snapshot), level = "debug")] - fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>) { - let CombinedSnapshot { undo_snapshot, region_constraints_snapshot: _, universe: _ } = - snapshot; - - self.inner.borrow_mut().commit(undo_snapshot); - } - - /// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`. - #[instrument(skip(self, f), level = "debug")] - pub fn commit_if_ok(&self, f: F) -> Result - where - F: FnOnce(&CombinedSnapshot<'tcx>) -> Result, - { - let snapshot = self.start_snapshot(); - let r = f(&snapshot); - debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok()); - match r { - Ok(_) => { - self.commit_from(snapshot); - } - Err(_) => { - self.rollback_to(snapshot); - } - } - r - } - - /// Execute `f` then unroll any bindings it creates. - #[instrument(skip(self, f), level = "debug")] - pub fn probe(&self, f: F) -> R - where - F: FnOnce(&CombinedSnapshot<'tcx>) -> R, - { - let snapshot = self.start_snapshot(); - let r = f(&snapshot); - self.rollback_to(snapshot); - r - } - - /// Scan the constraints produced since `snapshot` and check whether - /// we added any region constraints. - pub fn region_constraints_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool { - self.inner - .borrow_mut() - .unwrap_region_constraints() - .region_constraints_added_in_snapshot(&snapshot.undo_snapshot) - } - - pub fn opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool { - self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot) - } - pub fn can_sub(&self, param_env: ty::ParamEnv<'tcx>, expected: T, actual: T) -> bool where T: at::ToTrace<'tcx>, diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types/mod.rs similarity index 100% rename from compiler/rustc_infer/src/infer/opaque_types.rs rename to compiler/rustc_infer/src/infer/opaque_types/mod.rs diff --git a/compiler/rustc_infer/src/infer/opaque_types/table.rs b/compiler/rustc_infer/src/infer/opaque_types/table.rs index 9f49ed00219c9..a7ddf47543628 100644 --- a/compiler/rustc_infer/src/infer/opaque_types/table.rs +++ b/compiler/rustc_infer/src/infer/opaque_types/table.rs @@ -1,7 +1,7 @@ use rustc_data_structures::undo_log::UndoLogs; use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty}; -use crate::infer::{InferCtxtUndoLogs, UndoLog}; +use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, UndoLog}; use super::{OpaqueTypeDecl, OpaqueTypeMap}; diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index 8dd3a1f40cc10..fe323982ec03c 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -63,9 +63,8 @@ use crate::infer::outlives::components::{push_outlives_components, Component}; use crate::infer::outlives::env::RegionBoundPairs; use crate::infer::outlives::verify::VerifyBoundCx; use crate::infer::resolve::OpportunisticRegionResolver; -use crate::infer::{ - self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, UndoLog, VerifyBound, -}; +use crate::infer::snapshot::undo_log::UndoLog; +use crate::infer::{self, GenericKind, InferCtxt, RegionObligation, SubregionOrigin, VerifyBound}; use crate::traits::{ObligationCause, ObligationCauseCode}; use rustc_data_structures::undo_log::UndoLogs; use rustc_middle::mir::ConstraintCategory; diff --git a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs index 9e2f3a10b6a6e..06f8dd4a4c6cd 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs @@ -1,9 +1,7 @@ use super::*; -use crate::infer::CombinedSnapshot; -use rustc_data_structures::{ - fx::FxIndexMap, - graph::{scc::Sccs, vec_graph::VecGraph}, -}; +use crate::infer::snapshot::CombinedSnapshot; +use rustc_data_structures::fx::FxIndexMap; +use rustc_data_structures::graph::{scc::Sccs, vec_graph::VecGraph}; use rustc_index::Idx; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::relate::RelateResult; diff --git a/compiler/rustc_infer/src/infer/region_constraints/mod.rs b/compiler/rustc_infer/src/infer/region_constraints/mod.rs index ee97dd3680759..0f3f2bc5fa634 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/mod.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/mod.rs @@ -3,9 +3,8 @@ use self::CombineMapType::*; use self::UndoLog::*; -use super::{ - InferCtxtUndoLogs, MiscVariable, RegionVariableOrigin, Rollback, Snapshot, SubregionOrigin, -}; +use super::{MiscVariable, RegionVariableOrigin, Rollback, SubregionOrigin}; +use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, Snapshot}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync::Lrc; @@ -360,7 +359,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> { /// /// Not legal during a snapshot. pub fn into_infos_and_data(self) -> (VarInfos, RegionConstraintData<'tcx>) { - assert!(!UndoLogs::>::in_snapshot(&self.undo_log)); + assert!(!UndoLogs::>::in_snapshot(&self.undo_log)); (mem::take(&mut self.storage.var_infos), mem::take(&mut self.storage.data)) } @@ -377,7 +376,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> { /// /// Not legal during a snapshot. pub fn take_and_reset_data(&mut self) -> RegionConstraintData<'tcx> { - assert!(!UndoLogs::>::in_snapshot(&self.undo_log)); + assert!(!UndoLogs::>::in_snapshot(&self.undo_log)); // If you add a new field to `RegionConstraintCollector`, you // should think carefully about whether it needs to be cleared diff --git a/compiler/rustc_infer/src/infer/relate/higher_ranked.rs b/compiler/rustc_infer/src/infer/relate/higher_ranked.rs index f30e366c19850..d5030fed82fc0 100644 --- a/compiler/rustc_infer/src/infer/relate/higher_ranked.rs +++ b/compiler/rustc_infer/src/infer/relate/higher_ranked.rs @@ -1,7 +1,7 @@ //! Helper routines for higher-ranked things. See the `doc` module at //! the end of the file for details. -use crate::infer::CombinedSnapshot; +use crate::infer::snapshot::CombinedSnapshot; use crate::infer::InferCtxt; use rustc_middle::ty::fold::FnMutDelegate; use rustc_middle::ty::relate::RelateResult; diff --git a/compiler/rustc_infer/src/infer/fudge.rs b/compiler/rustc_infer/src/infer/snapshot/fudge.rs similarity index 98% rename from compiler/rustc_infer/src/infer/fudge.rs rename to compiler/rustc_infer/src/infer/snapshot/fudge.rs index 99033922bdf76..14de461cd17eb 100644 --- a/compiler/rustc_infer/src/infer/fudge.rs +++ b/compiler/rustc_infer/src/infer/snapshot/fudge.rs @@ -2,9 +2,9 @@ use rustc_middle::infer::unify_key::{ConstVariableOriginKind, ConstVariableValue use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::{self, ConstVid, FloatVid, IntVid, RegionVid, Ty, TyCtxt, TyVid}; -use super::type_variable::TypeVariableOrigin; -use super::InferCtxt; -use super::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable}; +use crate::infer::type_variable::TypeVariableOrigin; +use crate::infer::InferCtxt; +use crate::infer::{ConstVariableOrigin, RegionVariableOrigin, UnificationTable}; use rustc_data_structures::snapshot_vec as sv; use rustc_data_structures::unify as ut; diff --git a/compiler/rustc_infer/src/infer/snapshot/mod.rs b/compiler/rustc_infer/src/infer/snapshot/mod.rs new file mode 100644 index 0000000000000..9eef1471b1af2 --- /dev/null +++ b/compiler/rustc_infer/src/infer/snapshot/mod.rs @@ -0,0 +1,102 @@ +use super::region_constraints::RegionSnapshot; +use super::InferCtxt; +use rustc_data_structures::undo_log::UndoLogs; +use rustc_middle::ty; + +mod fudge; +pub(crate) mod undo_log; + +use undo_log::{Snapshot, UndoLog}; + +#[must_use = "once you start a snapshot, you should always consume it"] +pub struct CombinedSnapshot<'tcx> { + pub(super) undo_snapshot: Snapshot<'tcx>, + region_constraints_snapshot: RegionSnapshot, + universe: ty::UniverseIndex, +} + +impl<'tcx> InferCtxt<'tcx> { + pub fn in_snapshot(&self) -> bool { + UndoLogs::>::in_snapshot(&self.inner.borrow_mut().undo_log) + } + + pub fn num_open_snapshots(&self) -> usize { + UndoLogs::>::num_open_snapshots(&self.inner.borrow_mut().undo_log) + } + + fn start_snapshot(&self) -> CombinedSnapshot<'tcx> { + debug!("start_snapshot()"); + + let mut inner = self.inner.borrow_mut(); + + CombinedSnapshot { + undo_snapshot: inner.undo_log.start_snapshot(), + region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(), + universe: self.universe(), + } + } + + #[instrument(skip(self, snapshot), level = "debug")] + fn rollback_to(&self, snapshot: CombinedSnapshot<'tcx>) { + let CombinedSnapshot { undo_snapshot, region_constraints_snapshot, universe } = snapshot; + + self.universe.set(universe); + + let mut inner = self.inner.borrow_mut(); + inner.rollback_to(undo_snapshot); + inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot); + } + + #[instrument(skip(self, snapshot), level = "debug")] + fn commit_from(&self, snapshot: CombinedSnapshot<'tcx>) { + let CombinedSnapshot { undo_snapshot, region_constraints_snapshot: _, universe: _ } = + snapshot; + + self.inner.borrow_mut().commit(undo_snapshot); + } + + /// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`. + #[instrument(skip(self, f), level = "debug")] + pub fn commit_if_ok(&self, f: F) -> Result + where + F: FnOnce(&CombinedSnapshot<'tcx>) -> Result, + { + let snapshot = self.start_snapshot(); + let r = f(&snapshot); + debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok()); + match r { + Ok(_) => { + self.commit_from(snapshot); + } + Err(_) => { + self.rollback_to(snapshot); + } + } + r + } + + /// Execute `f` then unroll any bindings it creates. + #[instrument(skip(self, f), level = "debug")] + pub fn probe(&self, f: F) -> R + where + F: FnOnce(&CombinedSnapshot<'tcx>) -> R, + { + let snapshot = self.start_snapshot(); + let r = f(&snapshot); + self.rollback_to(snapshot); + r + } + + /// Scan the constraints produced since `snapshot` and check whether + /// we added any region constraints. + pub fn region_constraints_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool { + self.inner + .borrow_mut() + .unwrap_region_constraints() + .region_constraints_added_in_snapshot(&snapshot.undo_snapshot) + } + + pub fn opaque_types_added_in_snapshot(&self, snapshot: &CombinedSnapshot<'tcx>) -> bool { + self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot) + } +} diff --git a/compiler/rustc_infer/src/infer/undo_log.rs b/compiler/rustc_infer/src/infer/snapshot/undo_log.rs similarity index 100% rename from compiler/rustc_infer/src/infer/undo_log.rs rename to compiler/rustc_infer/src/infer/snapshot/undo_log.rs diff --git a/compiler/rustc_infer/src/traits/project.rs b/compiler/rustc_infer/src/traits/project.rs index afba2e50a23c5..31ceb2343324d 100644 --- a/compiler/rustc_infer/src/traits/project.rs +++ b/compiler/rustc_infer/src/traits/project.rs @@ -2,7 +2,7 @@ use super::PredicateObligation; -use crate::infer::InferCtxtUndoLogs; +use crate::infer::snapshot::undo_log::InferCtxtUndoLogs; use rustc_data_structures::{ snapshot_map::{self, SnapshotMapRef, SnapshotMapStorage},