diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index c9a42dddac42b..221593d6eadb5 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -454,7 +454,16 @@ where self.assemble_object_bound_candidates(goal, &mut candidates); } } - AssembleCandidatesFrom::EnvAndBounds => {} + AssembleCandidatesFrom::EnvAndBounds => { + // This is somewhat inconsistent and may make #57893 slightly easier to exploit. + // However, it matches the behavior of the old solver. See + // `tests/ui/traits/next-solver/normalization-shadowing/use_object_if_empty_env.rs`. + if matches!(normalized_self_ty.kind(), ty::Dynamic(..)) + && !candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) + { + self.assemble_object_bound_candidates(goal, &mut candidates); + } + } } (candidates, failed_candidate_info) diff --git a/tests/ui/traits/next-solver/normalization-shadowing/use_object_if_empty_env.rs b/tests/ui/traits/next-solver/normalization-shadowing/use_object_if_empty_env.rs new file mode 100644 index 0000000000000..c0bd2b49117b0 --- /dev/null +++ b/tests/ui/traits/next-solver/normalization-shadowing/use_object_if_empty_env.rs @@ -0,0 +1,19 @@ +//@ compile-flags: -Znext-solver +//@ check-pass + +// Regression test for trait-system-refactor-initiative#244 + +trait Trait { + type Assoc; +} + +// We have param env candidate for the trait goal but not the projection. +// Under such circumstance, consider object candidate if the self_ty is trait object. +fn foo(x: as Trait>::Assoc) -> T +where + dyn Trait: Trait, +{ + x +} + +fn main() {}