Skip to content

Commit

Permalink
Auto merge of rust-lang#133566 - lcnr:fast-reject-perf, r=<try>
Browse files Browse the repository at this point in the history
fast-reject: add counter to avoid hangs

alternative approach to rust-lang#133524

r? `@compiler-errors`
  • Loading branch information
bors committed Nov 28, 2024
2 parents f005c74 + 4ebc6c8 commit 96e2f40
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn overlapping_impls(
// Before doing expensive operations like entering an inference context, do
// a quick check via fast_reject to tell if the impl headers could possibly
// unify.
let drcx = DeepRejectCtxt::relate_infer_infer(tcx);
let mut drcx = DeepRejectCtxt::relate_infer_infer(tcx);
let impl1_ref = tcx.impl_trait_ref(impl1_def_id);
let impl2_ref = tcx.impl_trait_ref(impl2_def_id);
let may_overlap = match (impl1_ref, impl2_ref) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn evaluate_host_effect_from_bounds<'tcx>(
obligation: &HostEffectObligation<'tcx>,
) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
let infcx = selcx.infcx;
let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
let mut drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
let mut candidate = None;

for predicate in obligation.param_env.caller_bounds() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>(
potentially_unnormalized_candidates: bool,
) {
let infcx = selcx.infcx;
let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
let mut drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
for predicate in env_predicates {
let bound_predicate = predicate.kind();
if let ty::ClauseKind::Projection(data) = predicate.kind().skip_binder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
.filter(|p| p.def_id() == stack.obligation.predicate.def_id())
.filter(|p| p.polarity() == stack.obligation.predicate.polarity());

let drcx = DeepRejectCtxt::relate_rigid_rigid(self.tcx());
let mut drcx = DeepRejectCtxt::relate_rigid_rigid(self.tcx());
let obligation_args = stack.obligation.predicate.skip_binder().trait_ref.args;
// Keep only those bounds which may apply, and propagate overflow if it occurs.
for bound in bounds {
Expand Down Expand Up @@ -548,7 +548,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
obligation: &PolyTraitObligation<'tcx>,
candidates: &mut SelectionCandidateSet<'tcx>,
) {
let drcx = DeepRejectCtxt::relate_rigid_infer(self.tcx());
let mut drcx = DeepRejectCtxt::relate_rigid_infer(self.tcx());
let obligation_args = obligation.predicate.skip_binder().trait_ref.args;
self.tcx().for_each_relevant_impl(
obligation.predicate.def_id(),
Expand Down
27 changes: 19 additions & 8 deletions compiler/rustc_type_ir/src/fast_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,41 +181,46 @@ impl<DefId> SimplifiedType<DefId> {
/// We also use this function during coherence. For coherence the
/// impls only have to overlap for some value, so we treat parameters
/// on both sides like inference variables.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct DeepRejectCtxt<
I: Interner,
const INSTANTIATE_LHS_WITH_INFER: bool,
const INSTANTIATE_RHS_WITH_INFER: bool,
> {
_interner: PhantomData<I>,
/// We bail if the walked types are too big to avoid hangs.
///
/// We could alternatively use a cache to deduplicate types but this
/// feels like unnecessary effort.
num_non_trivial_types: usize,
}

impl<I: Interner> DeepRejectCtxt<I, false, false> {
/// Treat parameters in both the lhs and the rhs as rigid.
pub fn relate_rigid_rigid(_interner: I) -> DeepRejectCtxt<I, false, false> {
DeepRejectCtxt { _interner: PhantomData }
DeepRejectCtxt { _interner: PhantomData, num_non_trivial_types: 0 }
}
}

impl<I: Interner> DeepRejectCtxt<I, true, true> {
/// Treat parameters in both the lhs and the rhs as infer vars.
pub fn relate_infer_infer(_interner: I) -> DeepRejectCtxt<I, true, true> {
DeepRejectCtxt { _interner: PhantomData }
DeepRejectCtxt { _interner: PhantomData, num_non_trivial_types: 0 }
}
}

impl<I: Interner> DeepRejectCtxt<I, false, true> {
/// Treat parameters in the lhs as rigid, and in rhs as infer vars.
pub fn relate_rigid_infer(_interner: I) -> DeepRejectCtxt<I, false, true> {
DeepRejectCtxt { _interner: PhantomData }
DeepRejectCtxt { _interner: PhantomData, num_non_trivial_types: 0 }
}
}

impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_WITH_INFER: bool>
DeepRejectCtxt<I, INSTANTIATE_LHS_WITH_INFER, INSTANTIATE_RHS_WITH_INFER>
{
pub fn args_may_unify(
self,
&mut self,
obligation_args: I::GenericArgs,
impl_args: I::GenericArgs,
) -> bool {
Expand All @@ -234,7 +239,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
})
}

pub fn types_may_unify(self, lhs: I::Ty, rhs: I::Ty) -> bool {
pub fn types_may_unify(&mut self, lhs: I::Ty, rhs: I::Ty) -> bool {
match rhs.kind() {
// Start by checking whether the `rhs` type may unify with
// pretty much everything. Just return `true` in that case.
Expand Down Expand Up @@ -273,6 +278,12 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
| ty::Placeholder(_) => {}
};

self.num_non_trivial_types += 1;
// Arbitrary cutoff.
if self.num_non_trivial_types >= 1024 {
return true;
}

// For purely rigid types, use structural equivalence.
match lhs.kind() {
ty::Ref(_, lhs_ty, lhs_mutbl) => match rhs.kind() {
Expand Down Expand Up @@ -417,7 +428,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
}
}

pub fn consts_may_unify(self, lhs: I::Const, rhs: I::Const) -> bool {
pub fn consts_may_unify(&mut self, lhs: I::Const, rhs: I::Const) -> bool {
match rhs.kind() {
ty::ConstKind::Param(_) => {
if INSTANTIATE_RHS_WITH_INFER {
Expand Down Expand Up @@ -465,7 +476,7 @@ impl<I: Interner, const INSTANTIATE_LHS_WITH_INFER: bool, const INSTANTIATE_RHS_
}
}

fn var_and_ty_may_unify(self, var: ty::InferTy, ty: I::Ty) -> bool {
fn var_and_ty_may_unify(&mut self, var: ty::InferTy, ty: I::Ty) -> bool {
if !ty.is_known_rigid() {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@ impl<'item> DocVisitor<'item> for TypeImplCollector<'_, '_, 'item> {
// Be aware of `tests/rustdoc/type-alias/deeply-nested-112515.rs` which might regress.
let Some(impl_did) = impl_item_id.as_def_id() else { continue };
let for_ty = self.cx.tcx().type_of(impl_did).skip_binder();
let reject_cx = DeepRejectCtxt::relate_infer_infer(self.cx.tcx());
if !reject_cx.types_may_unify(aliased_ty, for_ty) {
let mut drcx = DeepRejectCtxt::relate_infer_infer(self.cx.tcx());
if !drcx.types_may_unify(aliased_ty, for_ty) {
continue;
}
// Avoid duplicates
Expand Down

0 comments on commit 96e2f40

Please sign in to comment.