Skip to content

dont assemble shadowed impl candidates #144732

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

Merged
merged 2 commits into from
Aug 3, 2025
Merged
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
27 changes: 24 additions & 3 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 @@ -395,9 +395,30 @@ 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,
// see trait-system-refactor-initiative#109 for more details.
//
// 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.
// See trait-system-refactor-initiative#226 for some ideas here.
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
32 changes: 32 additions & 0 deletions tests/ui/traits/next-solver/cycles/rayon-hang-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@ compile-flags: -Znext-solver
//@ check-pass

// A regression test for trait-system-refactor-initiative#109.

trait ParallelIterator: Sized {
type Item;
}
trait IntoParallelIterator {
type Iter: ParallelIterator<Item = Self::Item>;
type Item;
}
impl<T: ParallelIterator> IntoParallelIterator for T {
type Iter = T;
type Item = T::Item;
}

macro_rules! multizip_impls {
($($T:ident),+) => {
fn foo<$( $T, )+>() where
$(
$T: IntoParallelIterator,
$T::Iter: ParallelIterator,
)+
($( $T, )+): IntoParallelIterator<Item = ($( $T::Item, )+)>,
{}
}
}

multizip_impls! { A, B, C, D, E, F, G, H, I, J, K, L }

fn main() {}
49 changes: 49 additions & 0 deletions tests/ui/traits/next-solver/cycles/rayon-hang-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//@ compile-flags: -Znext-solver
//@ check-pass

// A regression test for trait-system-refactor-initiative#109.
// Unlike `rayon-hang-1.rs` the cycles in this test are not
// unproductive, which causes the `AliasRelate` goal when trying
// to apply where-clauses to only error in the second iteration.
//
// This makes the exponential blowup to be significantly harder
// to avoid.

trait ParallelIterator: Sized {
type Item;
}

trait IntoParallelIteratorIndir {
type Iter: ParallelIterator<Item = Self::Item>;
type Item;
}
impl<I> IntoParallelIteratorIndir for I
where
Box<I>: IntoParallelIterator,
{
type Iter = <Box<I> as IntoParallelIterator>::Iter;
type Item = <Box<I> as IntoParallelIterator>::Item;
}
trait IntoParallelIterator {
type Iter: ParallelIterator<Item = Self::Item>;
type Item;
}
impl<T: ParallelIterator> IntoParallelIterator for T {
type Iter = T;
type Item = T::Item;
}

macro_rules! multizip_impls {
($($T:ident),+) => {
fn foo<'a, $( $T, )+>() where
$(
$T: IntoParallelIteratorIndir,
$T::Iter: ParallelIterator,
)+
{}
}
}

multizip_impls! { A, B, C, D, E, F, G, H, I, J, K, L }

fn main() {}
Loading