Skip to content

avoid hangs if there are 10 billion where-clauses #144574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::delegate::SolverDelegate;
use crate::solve::inspect::ProbeKind;
use crate::solve::{
BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
MaybeCause, NoSolution, ParamEnvSource, QueryResult,
MaybeCause, NoSolution, ParamEnvSource, QueryResult, has_no_inference_or_external_constraints,
};

enum AliasBoundKind {
Expand Down Expand Up @@ -112,6 +112,13 @@ where
alias_ty: ty::AliasTy<I>,
) -> Vec<Candidate<I>>;

fn assemble_param_env_candidates_fast_path(
_ecx: &mut EvalCtxt<'_, D>,
_goal: Goal<I, Self>,
) -> Option<Candidate<I>> {
None
}

fn probe_and_consider_param_env_candidate(
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, Self>,
Expand Down Expand Up @@ -395,9 +402,28 @@ where

match assemble_from {
AssembleCandidatesFrom::All => {
self.assemble_impl_candidates(goal, &mut candidates);
self.assemble_builtin_impl_candidates(goal, &mut candidates);
self.assemble_object_bound_candidates(goal, &mut candidates);
// For performance we only assemble impls if there are no candidates
// which would shadow them. This is necessary to avoid hangs in rayon.
//
// We always assemble builtin impls as trivial builtin impls have a higher
// priority than where-clauses.
//
// We only do this if any such candidate applies without any constraints
// as we may want to weaken inference guidance in the future and don't want
// to worry about causing major performance regressions when doing so.
if TypingMode::Coherence == self.typing_mode()
|| !candidates.iter().any(|c| {
matches!(
c.source,
CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
| CandidateSource::AliasBound
) && has_no_inference_or_external_constraints(c.result)
})
{
self.assemble_impl_candidates(goal, &mut candidates);
self.assemble_object_bound_candidates(goal, &mut candidates);
}
}
AssembleCandidatesFrom::EnvAndBounds => {}
}
Expand Down Expand Up @@ -564,8 +590,13 @@ where
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
for assumption in goal.param_env.caller_bounds().iter() {
candidates.extend(G::probe_and_consider_param_env_candidate(self, goal, assumption));
if let Some(candidate) = G::assemble_param_env_candidates_fast_path(self, goal) {
candidates.push(candidate);
} else {
for assumption in goal.param_env.caller_bounds().iter() {
candidates
.extend(G::probe_and_consider_param_env_candidate(self, goal, assumption));
}
}
}

Expand Down Expand Up @@ -1015,7 +1046,7 @@ where
/// The `i32: From<T::Assoc>` bound is non-global before normalization, but is global after.
/// Since the old trait solver normalized param-envs eagerly, we want to emulate this
/// behavior lazily.
fn characterize_param_env_assumption(
pub(super) fn characterize_param_env_assumption(
&mut self,
param_env: I::ParamEnv,
assumption: I::Clause,
Expand Down
45 changes: 44 additions & 1 deletion compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Dealing with trait goals, i.e. `T: Trait<'a, U>`.

use std::cell::Cell;

use rustc_type_ir::data_structures::IndexSet;
use rustc_type_ir::fast_reject::DeepRejectCtxt;
use rustc_type_ir::inherent::*;
Expand All @@ -14,7 +16,7 @@ use tracing::{debug, instrument, trace};
use crate::delegate::SolverDelegate;
use crate::solve::assembly::structural_traits::{self, AsyncCallableRelevantTypes};
use crate::solve::assembly::{self, AllowInferenceConstraints, AssembleCandidatesFrom, Candidate};
use crate::solve::inspect::ProbeKind;
use crate::solve::inspect::{self, ProbeKind};
use crate::solve::{
BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeCause,
NoSolution, ParamEnvSource, QueryResult, has_only_region_constraints,
Expand All @@ -41,6 +43,47 @@ where
self.def_id()
}

fn assemble_param_env_candidates_fast_path(
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, Self>,
) -> Option<Candidate<I>> {
// This is kind of a mess. We need to detect whether there's an applicable
// `ParamEnv` candidate without fully normalizing other candidates to avoid the
// hang in trait-system-refactor-initiative#210. This currently uses structural
// identity, which means it does not apply if there are normalizeable aliases
// in the environment or if the goal is higher ranked.
//
// We generally need such a fast path if multiple potentially applicable where-bounds
// contain aliases whose normalization tries to apply all these where-bounds yet again.
// This can easily result in exponential blowup.
for assumption in goal.param_env.caller_bounds().iter().filter_map(|c| c.as_trait_clause())
{
if assumption.no_bound_vars().is_some_and(|c| c == goal.predicate) {
let source = Cell::new(CandidateSource::ParamEnv(ParamEnvSource::Global));
let Ok(candidate) = ecx
.probe(|result: &QueryResult<I>| inspect::ProbeKind::TraitCandidate {
source: source.get(),
result: *result,
})
.enter(|ecx| {
source.set(ecx.characterize_param_env_assumption(
goal.param_env,
assumption.upcast(ecx.cx()),
)?);
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
})
.map(|result| Candidate { source: source.get(), result })
else {
continue;
};
if candidate.source == CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) {
return Some(candidate);
}
}
}
None
}

fn consider_additional_alias_assumptions(
_ecx: &mut EvalCtxt<'_, D>,
_goal: Goal<I, Self>,
Expand Down
Loading