Auto merge of #99509 - lcnr:commit_unconditionally, r=jackh726

remove `commit_unconditionally`

`commit_unconditionally` is a noop unless we somehow inspect the current state of our snapshot. The only thing which does that is the leak check which was only used in one place where `commit_if_ok` is probably at least as, or even more, correct.

r? rust-lang/types
This commit is contained in:
bors 2022-08-03 01:55:20 +00:00
commit b759b2efad
4 changed files with 164 additions and 195 deletions

View File

@ -840,18 +840,6 @@ fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) {
self.inner.borrow_mut().commit(undo_snapshot);
}
/// Executes `f` and commit the bindings.
#[instrument(skip(self, f), level = "debug")]
pub fn commit_unconditionally<R, F>(&self, f: F) -> R
where
F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
{
let snapshot = self.start_snapshot();
let r = f(&snapshot);
self.commit_from(snapshot);
r
}
/// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
#[instrument(skip(self, f), level = "debug")]
pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>

View File

@ -144,7 +144,6 @@ fn confirm_projection_candidate(
obligation: &TraitObligation<'tcx>,
idx: usize,
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
self.infcx.commit_unconditionally(|_| {
let tcx = self.tcx();
let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
@ -204,7 +203,6 @@ fn confirm_projection_candidate(
}
Ok(obligations)
})
}
fn confirm_param_candidate(
@ -347,19 +345,16 @@ fn vtable_auto_impl(
ensure_sufficient_stack(|| {
let cause = obligation.derived_cause(BuiltinDerivedObligation);
let trait_obligations: Vec<PredicateObligation<'_>> =
self.infcx.commit_unconditionally(|_| {
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
let trait_ref = self.infcx.replace_bound_vars_with_placeholders(poly_trait_ref);
self.impl_or_trait_obligations(
let trait_obligations: Vec<PredicateObligation<'_>> = self.impl_or_trait_obligations(
&cause,
obligation.recursion_depth + 1,
obligation.param_env,
trait_def_id,
&trait_ref.substs,
obligation.predicate,
)
});
);
let mut obligations = self.collect_predicates_for_types(
obligation.param_env,
@ -388,7 +383,6 @@ fn confirm_impl_candidate(
// First, create the substitutions by matching the impl again,
// this time not in a probe.
self.infcx.commit_unconditionally(|_| {
let substs = self.rematch_impl(impl_def_id, obligation);
debug!(?substs, "impl substs");
ensure_sufficient_stack(|| {
@ -401,7 +395,6 @@ fn confirm_impl_candidate(
obligation.predicate,
)
})
})
}
fn vtable_impl(
@ -647,7 +640,6 @@ fn confirm_trait_alias_candidate(
) -> ImplSourceTraitAliasData<'tcx, PredicateObligation<'tcx>> {
debug!(?obligation, ?alias_def_id, "confirm_trait_alias_candidate");
self.infcx.commit_unconditionally(|_| {
let predicate = self.infcx().replace_bound_vars_with_placeholders(obligation.predicate);
let trait_ref = predicate.trait_ref;
let trait_def_id = trait_ref.def_id;
@ -665,7 +657,6 @@ fn confirm_trait_alias_candidate(
debug!(?trait_def_id, ?trait_obligations, "trait alias obligations");
ImplSourceTraitAliasData { alias_def_id, substs, nested: trait_obligations }
})
}
fn confirm_generator_candidate(
@ -763,7 +754,6 @@ fn confirm_poly_trait_refs(
// Normalize the obligation and expected trait refs together, because why not
let Normalized { obligations: nested, value: (obligation_trait_ref, expected_trait_ref) } =
ensure_sufficient_stack(|| {
self.infcx.commit_unconditionally(|_| {
normalize_with_depth(
self,
obligation.param_env,
@ -771,7 +761,6 @@ fn confirm_poly_trait_refs(
obligation.recursion_depth + 1,
(obligation_trait_ref, expected_trait_ref),
)
})
});
self.infcx
@ -1147,7 +1136,6 @@ fn confirm_const_destruct_candidate(
// first check it like a regular impl candidate.
// This is copied from confirm_impl_candidate but remaps the predicate to `~const Drop` beforehand.
if let Some(impl_def_id) = impl_def_id {
let obligations = self.infcx.commit_unconditionally(|_| {
let mut new_obligation = obligation.clone();
new_obligation.predicate = new_obligation.predicate.map_bound(|mut trait_pred| {
trait_pred.trait_ref.def_id = drop_trait;
@ -1163,7 +1151,7 @@ fn confirm_const_destruct_candidate(
span: obligation.cause.span,
}))
});
ensure_sufficient_stack(|| {
let obligations = ensure_sufficient_stack(|| {
self.vtable_impl(
impl_def_id,
substs,
@ -1172,7 +1160,6 @@ fn confirm_const_destruct_candidate(
new_obligation.param_env,
obligation.predicate,
)
})
});
nested.extend(obligations.nested);
}
@ -1223,7 +1210,6 @@ fn confirm_const_destruct_candidate(
// If we have a projection type, make sure to normalize it so we replace it
// with a fresh infer variable
ty::Projection(..) => {
self.infcx.commit_unconditionally(|_| {
let predicate = normalize_with_depth_to(
self,
obligation.param_env,
@ -1232,9 +1218,7 @@ fn confirm_const_destruct_candidate(
self_ty
.rebind(ty::TraitPredicate {
trait_ref: ty::TraitRef {
def_id: self
.tcx()
.require_lang_item(LangItem::Destruct, None),
def_id: self.tcx().require_lang_item(LangItem::Destruct, None),
substs: self.tcx().mk_substs_trait(nested_ty, &[]),
},
constness: ty::BoundConstness::ConstIfConst,
@ -1250,7 +1234,6 @@ fn confirm_const_destruct_candidate(
obligation.param_env,
predicate,
));
});
}
// If we have any other type (e.g. an ADT), just register a nested obligation

View File

@ -2084,7 +2084,6 @@ fn collect_predicates_for_types(
.flat_map(|ty| {
let ty: ty::Binder<'tcx, Ty<'tcx>> = types.rebind(*ty); // <----/
self.infcx.commit_unconditionally(|_| {
let placeholder_ty = self.infcx.replace_bound_vars_with_placeholders(ty);
let Normalized { value: normalized_ty, mut obligations } =
ensure_sufficient_stack(|| {
@ -2108,7 +2107,6 @@ fn collect_predicates_for_types(
obligations.push(placeholder_obligation);
obligations
})
})
.collect()
}

View File

@ -737,7 +737,7 @@ fn coerce_from_safe_fn<F, G>(
F: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>,
G: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>,
{
self.commit_unconditionally(|snapshot| {
self.commit_if_ok(|snapshot| {
let result = if let ty::FnPtr(fn_ty_b) = b.kind()
&& let (hir::Unsafety::Normal, hir::Unsafety::Unsafe) =
(fn_ty_a.unsafety(), fn_ty_b.unsafety())