Skip to content

Commit ed6a7cc

Browse files
Remove save_and_restore_in_snapshot_flag
1 parent bdced83 commit ed6a7cc

File tree

3 files changed

+25
-53
lines changed
  • compiler
    • rustc_hir_typeck/src/fn_ctxt
    • rustc_infer/src/infer
    • rustc_trait_selection/src/traits/specialize

3 files changed

+25
-53
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
765765

766766
let expect_args = self
767767
.fudge_inference_if_ok(|| {
768-
let ocx = ObligationCtxt::new(self);
768+
let ocx = ObligationCtxt::new_in_snapshot(self);
769769

770770
// Attempt to apply a subtyping relationship between the formal
771771
// return type (likely containing type variables if the function

compiler/rustc_infer/src/infer/mod.rs

-26
Original file line numberDiff line numberDiff line change
@@ -778,32 +778,6 @@ impl<'tcx> InferCtxt<'tcx> {
778778
}
779779
}
780780

781-
/// Clear the "currently in a snapshot" flag, invoke the closure,
782-
/// then restore the flag to its original value. This flag is a
783-
/// debugging measure designed to detect cases where we start a
784-
/// snapshot, create type variables, and register obligations
785-
/// which may involve those type variables in the fulfillment cx,
786-
/// potentially leaving "dangling type variables" behind.
787-
/// In such cases, an assertion will fail when attempting to
788-
/// register obligations, within a snapshot. Very useful, much
789-
/// better than grovelling through megabytes of `RUSTC_LOG` output.
790-
///
791-
/// HOWEVER, in some cases the flag is unhelpful. In particular, we
792-
/// sometimes create a "mini-fulfilment-cx" in which we enroll
793-
/// obligations. As long as this fulfillment cx is fully drained
794-
/// before we return, this is not a problem, as there won't be any
795-
/// escaping obligations in the main cx. In those cases, you can
796-
/// use this function.
797-
pub fn save_and_restore_in_snapshot_flag<F, R>(&self, func: F) -> R
798-
where
799-
F: FnOnce(&Self) -> R,
800-
{
801-
let flag = self.in_snapshot.replace(false);
802-
let result = func(self);
803-
self.in_snapshot.set(flag);
804-
result
805-
}
806-
807781
fn start_snapshot(&self) -> CombinedSnapshot<'tcx> {
808782
debug!("start_snapshot()");
809783

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
1111
1212
pub mod specialization_graph;
13+
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _};
1314
use specialization_graph::GraphExt;
1415

1516
use crate::errors::NegativePositiveConflict;
1617
use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt};
18+
use crate::traits::engine::TraitEngineExt as _;
1719
use crate::traits::select::IntercrateAmbiguityCause;
1820
use crate::traits::{self, coherence, FutureCompatOverlapErrorKind, ObligationCause};
1921
use rustc_data_structures::fx::FxIndexSet;
@@ -200,36 +202,32 @@ fn fulfill_implication<'tcx>(
200202
return Err(());
201203
};
202204

205+
// Needs to be `in_snapshot` because this function is used to rebase
206+
// substitutions, which may happen inside of a select within a probe.
207+
let mut engine = <dyn TraitEngine<'tcx>>::new_in_snapshot(infcx.tcx);
203208
// attempt to prove all of the predicates for impl2 given those for impl1
204209
// (which are packed up in penv)
210+
engine.register_predicate_obligations(infcx, obligations.chain(more_obligations));
205211

206-
infcx.save_and_restore_in_snapshot_flag(|infcx| {
207-
let errors = traits::fully_solve_obligations(&infcx, obligations.chain(more_obligations));
208-
match &errors[..] {
209-
[] => {
210-
debug!(
211-
"fulfill_implication: an impl for {:?} specializes {:?}",
212-
source_trait, target_trait
213-
);
212+
let errors = engine.select_all_or_error(infcx);
213+
if !errors.is_empty() {
214+
// no dice!
215+
debug!(
216+
"fulfill_implication: for impls on {:?} and {:?}, \
217+
could not fulfill: {:?} given {:?}",
218+
source_trait,
219+
target_trait,
220+
errors,
221+
param_env.caller_bounds()
222+
);
223+
return Err(());
224+
}
214225

215-
// Now resolve the *substitution* we built for the target earlier, replacing
216-
// the inference variables inside with whatever we got from fulfillment.
217-
Ok(infcx.resolve_vars_if_possible(target_substs))
218-
}
219-
errors => {
220-
// no dice!
221-
debug!(
222-
"fulfill_implication: for impls on {:?} and {:?}, \
223-
could not fulfill: {:?} given {:?}",
224-
source_trait,
225-
target_trait,
226-
errors,
227-
param_env.caller_bounds()
228-
);
229-
Err(())
230-
}
231-
}
232-
})
226+
debug!("fulfill_implication: an impl for {:?} specializes {:?}", source_trait, target_trait);
227+
228+
// Now resolve the *substitution* we built for the target earlier, replacing
229+
// the inference variables inside with whatever we got from fulfillment.
230+
Ok(infcx.resolve_vars_if_possible(target_substs))
233231
}
234232

235233
// Query provider for `specialization_graph_of`.

0 commit comments

Comments
 (0)