rustc_next_trait_solver/solve/assembly/
mod.rs

1//! Code shared by trait and projection goals for candidate assembly.
2
3pub(super) mod structural_traits;
4
5use std::cell::Cell;
6use std::ops::ControlFlow;
7
8use derive_where::derive_where;
9use rustc_type_ir::inherent::*;
10use rustc_type_ir::lang_items::SolverTraitLangItem;
11use rustc_type_ir::search_graph::CandidateHeadUsages;
12use rustc_type_ir::solve::SizedTraitKind;
13use rustc_type_ir::{
14    self as ty, Interner, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable,
15    TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Upcast,
16    elaborate,
17};
18use tracing::{debug, instrument};
19
20use super::trait_goals::TraitGoalProvenVia;
21use super::{has_only_region_constraints, inspect};
22use crate::delegate::SolverDelegate;
23use crate::solve::inspect::ProbeKind;
24use crate::solve::{
25    BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
26    MaybeCause, NoSolution, ParamEnvSource, QueryResult, has_no_inference_or_external_constraints,
27};
28
29enum AliasBoundKind {
30    SelfBounds,
31    NonSelfBounds,
32}
33
34/// A candidate is a possible way to prove a goal.
35///
36/// It consists of both the `source`, which describes how that goal would be proven,
37/// and the `result` when using the given `source`.
38#[derive_where(Debug; I: Interner)]
39pub(super) struct Candidate<I: Interner> {
40    pub(super) source: CandidateSource<I>,
41    pub(super) result: CanonicalResponse<I>,
42    pub(super) head_usages: CandidateHeadUsages,
43}
44
45/// Methods used to assemble candidates for either trait or projection goals.
46pub(super) trait GoalKind<D, I = <D as SolverDelegate>::Interner>:
47    TypeFoldable<I> + Copy + Eq + std::fmt::Display
48where
49    D: SolverDelegate<Interner = I>,
50    I: Interner,
51{
52    fn self_ty(self) -> I::Ty;
53
54    fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
55
56    fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
57
58    fn trait_def_id(self, cx: I) -> I::TraitId;
59
60    /// Consider a clause, which consists of a "assumption" and some "requirements",
61    /// to satisfy a goal. If the requirements hold, then attempt to satisfy our
62    /// goal by equating it with the assumption.
63    fn probe_and_consider_implied_clause(
64        ecx: &mut EvalCtxt<'_, D>,
65        parent_source: CandidateSource<I>,
66        goal: Goal<I, Self>,
67        assumption: I::Clause,
68        requirements: impl IntoIterator<Item = (GoalSource, Goal<I, I::Predicate>)>,
69    ) -> Result<Candidate<I>, NoSolution> {
70        Self::probe_and_match_goal_against_assumption(ecx, parent_source, goal, assumption, |ecx| {
71            for (nested_source, goal) in requirements {
72                ecx.add_goal(nested_source, goal);
73            }
74            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
75        })
76    }
77
78    /// Consider a clause specifically for a `dyn Trait` self type. This requires
79    /// additionally checking all of the supertraits and object bounds to hold,
80    /// since they're not implied by the well-formedness of the object type.
81    fn probe_and_consider_object_bound_candidate(
82        ecx: &mut EvalCtxt<'_, D>,
83        source: CandidateSource<I>,
84        goal: Goal<I, Self>,
85        assumption: I::Clause,
86    ) -> Result<Candidate<I>, NoSolution> {
87        Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
88            let cx = ecx.cx();
89            let ty::Dynamic(bounds, _, _) = goal.predicate.self_ty().kind() else {
90                panic!("expected object type in `probe_and_consider_object_bound_candidate`");
91            };
92            match structural_traits::predicates_for_object_candidate(
93                ecx,
94                goal.param_env,
95                goal.predicate.trait_ref(cx),
96                bounds,
97            ) {
98                Ok(requirements) => {
99                    ecx.add_goals(GoalSource::ImplWhereBound, requirements);
100                    ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
101                }
102                Err(_) => {
103                    ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
104                }
105            }
106        })
107    }
108
109    /// Assemble additional assumptions for an alias that are not included
110    /// in the item bounds of the alias. For now, this is limited to the
111    /// `explicit_implied_const_bounds` for an associated type.
112    fn consider_additional_alias_assumptions(
113        ecx: &mut EvalCtxt<'_, D>,
114        goal: Goal<I, Self>,
115        alias_ty: ty::AliasTy<I>,
116    ) -> Vec<Candidate<I>>;
117
118    fn probe_and_consider_param_env_candidate(
119        ecx: &mut EvalCtxt<'_, D>,
120        goal: Goal<I, Self>,
121        assumption: I::Clause,
122    ) -> Result<Candidate<I>, CandidateHeadUsages> {
123        match Self::fast_reject_assumption(ecx, goal, assumption) {
124            Ok(()) => {}
125            Err(NoSolution) => return Err(CandidateHeadUsages::default()),
126        }
127
128        // Dealing with `ParamEnv` candidates is a bit of a mess as we need to lazily
129        // check whether the candidate is global while considering normalization.
130        //
131        // We need to write into `source` inside of `match_assumption`, but need to access it
132        // in `probe` even if the candidate does not apply before we get there. We handle this
133        // by using a `Cell` here. We only ever write into it inside of `match_assumption`.
134        let source = Cell::new(CandidateSource::ParamEnv(ParamEnvSource::Global));
135        let (result, head_usages) = ecx
136            .probe(|result: &QueryResult<I>| inspect::ProbeKind::TraitCandidate {
137                source: source.get(),
138                result: *result,
139            })
140            .enter_single_candidate(|ecx| {
141                Self::match_assumption(ecx, goal, assumption, |ecx| {
142                    ecx.try_evaluate_added_goals()?;
143                    source.set(ecx.characterize_param_env_assumption(goal.param_env, assumption)?);
144                    ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
145                })
146            });
147
148        match result {
149            Ok(result) => Ok(Candidate { source: source.get(), result, head_usages }),
150            Err(NoSolution) => Err(head_usages),
151        }
152    }
153
154    /// Try equating an assumption predicate against a goal's predicate. If it
155    /// holds, then execute the `then` callback, which should do any additional
156    /// work, then produce a response (typically by executing
157    /// [`EvalCtxt::evaluate_added_goals_and_make_canonical_response`]).
158    fn probe_and_match_goal_against_assumption(
159        ecx: &mut EvalCtxt<'_, D>,
160        source: CandidateSource<I>,
161        goal: Goal<I, Self>,
162        assumption: I::Clause,
163        then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
164    ) -> Result<Candidate<I>, NoSolution> {
165        Self::fast_reject_assumption(ecx, goal, assumption)?;
166
167        ecx.probe_trait_candidate(source)
168            .enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
169    }
170
171    /// Try to reject the assumption based off of simple heuristics, such as [`ty::ClauseKind`]
172    /// and `DefId`.
173    fn fast_reject_assumption(
174        ecx: &mut EvalCtxt<'_, D>,
175        goal: Goal<I, Self>,
176        assumption: I::Clause,
177    ) -> Result<(), NoSolution>;
178
179    /// Relate the goal and assumption.
180    fn match_assumption(
181        ecx: &mut EvalCtxt<'_, D>,
182        goal: Goal<I, Self>,
183        assumption: I::Clause,
184        then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
185    ) -> QueryResult<I>;
186
187    fn consider_impl_candidate(
188        ecx: &mut EvalCtxt<'_, D>,
189        goal: Goal<I, Self>,
190        impl_def_id: I::ImplId,
191        then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResult<I>,
192    ) -> Result<Candidate<I>, NoSolution>;
193
194    /// If the predicate contained an error, we want to avoid emitting unnecessary trait
195    /// errors but still want to emit errors for other trait goals. We have some special
196    /// handling for this case.
197    ///
198    /// Trait goals always hold while projection goals never do. This is a bit arbitrary
199    /// but prevents incorrect normalization while hiding any trait errors.
200    fn consider_error_guaranteed_candidate(
201        ecx: &mut EvalCtxt<'_, D>,
202        guar: I::ErrorGuaranteed,
203    ) -> Result<Candidate<I>, NoSolution>;
204
205    /// A type implements an `auto trait` if its components do as well.
206    ///
207    /// These components are given by built-in rules from
208    /// [`structural_traits::instantiate_constituent_tys_for_auto_trait`].
209    fn consider_auto_trait_candidate(
210        ecx: &mut EvalCtxt<'_, D>,
211        goal: Goal<I, Self>,
212    ) -> Result<Candidate<I>, NoSolution>;
213
214    /// A trait alias holds if the RHS traits and `where` clauses hold.
215    fn consider_trait_alias_candidate(
216        ecx: &mut EvalCtxt<'_, D>,
217        goal: Goal<I, Self>,
218    ) -> Result<Candidate<I>, NoSolution>;
219
220    /// A type is `Sized` if its tail component is `Sized` and a type is `MetaSized` if its tail
221    /// component is `MetaSized`.
222    ///
223    /// These components are given by built-in rules from
224    /// [`structural_traits::instantiate_constituent_tys_for_sizedness_trait`].
225    fn consider_builtin_sizedness_candidates(
226        ecx: &mut EvalCtxt<'_, D>,
227        goal: Goal<I, Self>,
228        sizedness: SizedTraitKind,
229    ) -> Result<Candidate<I>, NoSolution>;
230
231    /// A type is `Copy` or `Clone` if its components are `Copy` or `Clone`.
232    ///
233    /// These components are given by built-in rules from
234    /// [`structural_traits::instantiate_constituent_tys_for_copy_clone_trait`].
235    fn consider_builtin_copy_clone_candidate(
236        ecx: &mut EvalCtxt<'_, D>,
237        goal: Goal<I, Self>,
238    ) -> Result<Candidate<I>, NoSolution>;
239
240    /// A type is a `FnPtr` if it is of `FnPtr` type.
241    fn consider_builtin_fn_ptr_trait_candidate(
242        ecx: &mut EvalCtxt<'_, D>,
243        goal: Goal<I, Self>,
244    ) -> Result<Candidate<I>, NoSolution>;
245
246    /// A callable type (a closure, fn def, or fn ptr) is known to implement the `Fn<A>`
247    /// family of traits where `A` is given by the signature of the type.
248    fn consider_builtin_fn_trait_candidates(
249        ecx: &mut EvalCtxt<'_, D>,
250        goal: Goal<I, Self>,
251        kind: ty::ClosureKind,
252    ) -> Result<Candidate<I>, NoSolution>;
253
254    /// An async closure is known to implement the `AsyncFn<A>` family of traits
255    /// where `A` is given by the signature of the type.
256    fn consider_builtin_async_fn_trait_candidates(
257        ecx: &mut EvalCtxt<'_, D>,
258        goal: Goal<I, Self>,
259        kind: ty::ClosureKind,
260    ) -> Result<Candidate<I>, NoSolution>;
261
262    /// Compute the built-in logic of the `AsyncFnKindHelper` helper trait, which
263    /// is used internally to delay computation for async closures until after
264    /// upvar analysis is performed in HIR typeck.
265    fn consider_builtin_async_fn_kind_helper_candidate(
266        ecx: &mut EvalCtxt<'_, D>,
267        goal: Goal<I, Self>,
268    ) -> Result<Candidate<I>, NoSolution>;
269
270    /// `Tuple` is implemented if the `Self` type is a tuple.
271    fn consider_builtin_tuple_candidate(
272        ecx: &mut EvalCtxt<'_, D>,
273        goal: Goal<I, Self>,
274    ) -> Result<Candidate<I>, NoSolution>;
275
276    /// `Pointee` is always implemented.
277    ///
278    /// See the projection implementation for the `Metadata` types for all of
279    /// the built-in types. For structs, the metadata type is given by the struct
280    /// tail.
281    fn consider_builtin_pointee_candidate(
282        ecx: &mut EvalCtxt<'_, D>,
283        goal: Goal<I, Self>,
284    ) -> Result<Candidate<I>, NoSolution>;
285
286    /// A coroutine (that comes from an `async` desugaring) is known to implement
287    /// `Future<Output = O>`, where `O` is given by the coroutine's return type
288    /// that was computed during type-checking.
289    fn consider_builtin_future_candidate(
290        ecx: &mut EvalCtxt<'_, D>,
291        goal: Goal<I, Self>,
292    ) -> Result<Candidate<I>, NoSolution>;
293
294    /// A coroutine (that comes from a `gen` desugaring) is known to implement
295    /// `Iterator<Item = O>`, where `O` is given by the generator's yield type
296    /// that was computed during type-checking.
297    fn consider_builtin_iterator_candidate(
298        ecx: &mut EvalCtxt<'_, D>,
299        goal: Goal<I, Self>,
300    ) -> Result<Candidate<I>, NoSolution>;
301
302    /// A coroutine (that comes from a `gen` desugaring) is known to implement
303    /// `FusedIterator`
304    fn consider_builtin_fused_iterator_candidate(
305        ecx: &mut EvalCtxt<'_, D>,
306        goal: Goal<I, Self>,
307    ) -> Result<Candidate<I>, NoSolution>;
308
309    fn consider_builtin_async_iterator_candidate(
310        ecx: &mut EvalCtxt<'_, D>,
311        goal: Goal<I, Self>,
312    ) -> Result<Candidate<I>, NoSolution>;
313
314    /// A coroutine (that doesn't come from an `async` or `gen` desugaring) is known to
315    /// implement `Coroutine<R, Yield = Y, Return = O>`, given the resume, yield,
316    /// and return types of the coroutine computed during type-checking.
317    fn consider_builtin_coroutine_candidate(
318        ecx: &mut EvalCtxt<'_, D>,
319        goal: Goal<I, Self>,
320    ) -> Result<Candidate<I>, NoSolution>;
321
322    fn consider_builtin_discriminant_kind_candidate(
323        ecx: &mut EvalCtxt<'_, D>,
324        goal: Goal<I, Self>,
325    ) -> Result<Candidate<I>, NoSolution>;
326
327    fn consider_builtin_destruct_candidate(
328        ecx: &mut EvalCtxt<'_, D>,
329        goal: Goal<I, Self>,
330    ) -> Result<Candidate<I>, NoSolution>;
331
332    fn consider_builtin_transmute_candidate(
333        ecx: &mut EvalCtxt<'_, D>,
334        goal: Goal<I, Self>,
335    ) -> Result<Candidate<I>, NoSolution>;
336
337    fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
338        ecx: &mut EvalCtxt<'_, D>,
339        goal: Goal<I, Self>,
340    ) -> Result<Candidate<I>, NoSolution>;
341
342    /// Consider (possibly several) candidates to upcast or unsize a type to another
343    /// type, excluding the coercion of a sized type into a `dyn Trait`.
344    ///
345    /// We return the `BuiltinImplSource` for each candidate as it is needed
346    /// for unsize coercion in hir typeck and because it is difficult to
347    /// otherwise recompute this for codegen. This is a bit of a mess but the
348    /// easiest way to maintain the existing behavior for now.
349    fn consider_structural_builtin_unsize_candidates(
350        ecx: &mut EvalCtxt<'_, D>,
351        goal: Goal<I, Self>,
352    ) -> Vec<Candidate<I>>;
353}
354
355/// Allows callers of `assemble_and_evaluate_candidates` to choose whether to limit
356/// candidate assembly to param-env and alias-bound candidates.
357///
358/// On top of being a micro-optimization, as it avoids doing unnecessary work when
359/// a param-env trait bound candidate shadows impls for normalization, this is also
360/// required to prevent query cycles due to RPITIT inference. See the issue at:
361/// <https://github.com/rust-lang/trait-system-refactor-initiative/issues/173>.
362pub(super) enum AssembleCandidatesFrom {
363    All,
364    /// Only assemble candidates from the environment and alias bounds, ignoring
365    /// user-written and built-in impls. We only expect `ParamEnv` and `AliasBound`
366    /// candidates to be assembled.
367    EnvAndBounds,
368}
369
370impl AssembleCandidatesFrom {
371    fn should_assemble_impl_candidates(&self) -> bool {
372        match self {
373            AssembleCandidatesFrom::All => true,
374            AssembleCandidatesFrom::EnvAndBounds => false,
375        }
376    }
377}
378
379/// This is currently used to track the [CandidateHeadUsages] of all failed `ParamEnv`
380/// candidates. This is then used to ignore their head usages in case there's another
381/// always applicable `ParamEnv` candidate. Look at how `param_env_head_usages` is
382/// used in the code for more details.
383///
384/// We could easily extend this to also ignore head usages of other ignored candidates.
385/// However, we currently don't have any tests where this matters and the complexity of
386/// doing so does not feel worth it for now.
387#[derive(Debug)]
388pub(super) struct FailedCandidateInfo {
389    pub param_env_head_usages: CandidateHeadUsages,
390}
391
392impl<D, I> EvalCtxt<'_, D>
393where
394    D: SolverDelegate<Interner = I>,
395    I: Interner,
396{
397    pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
398        &mut self,
399        goal: Goal<I, G>,
400        assemble_from: AssembleCandidatesFrom,
401    ) -> (Vec<Candidate<I>>, FailedCandidateInfo) {
402        let mut candidates = vec![];
403        let mut failed_candidate_info =
404            FailedCandidateInfo { param_env_head_usages: CandidateHeadUsages::default() };
405        let Ok(normalized_self_ty) =
406            self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
407        else {
408            return (candidates, failed_candidate_info);
409        };
410
411        let goal: Goal<I, G> = goal
412            .with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
413
414        if normalized_self_ty.is_ty_var() {
415            debug!("self type has been normalized to infer");
416            self.try_assemble_bounds_via_registered_opaques(goal, assemble_from, &mut candidates);
417            return (candidates, failed_candidate_info);
418        }
419
420        // Vars that show up in the rest of the goal substs may have been constrained by
421        // normalizing the self type as well, since type variables are not uniquified.
422        let goal = self.resolve_vars_if_possible(goal);
423
424        if let TypingMode::Coherence = self.typing_mode()
425            && let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
426        {
427            candidates.push(candidate);
428            return (candidates, failed_candidate_info);
429        }
430
431        self.assemble_alias_bound_candidates(goal, &mut candidates);
432        self.assemble_param_env_candidates(goal, &mut candidates, &mut failed_candidate_info);
433
434        match assemble_from {
435            AssembleCandidatesFrom::All => {
436                self.assemble_builtin_impl_candidates(goal, &mut candidates);
437                // For performance we only assemble impls if there are no candidates
438                // which would shadow them. This is necessary to avoid hangs in rayon,
439                // see trait-system-refactor-initiative#109 for more details.
440                //
441                // We always assemble builtin impls as trivial builtin impls have a higher
442                // priority than where-clauses.
443                //
444                // We only do this if any such candidate applies without any constraints
445                // as we may want to weaken inference guidance in the future and don't want
446                // to worry about causing major performance regressions when doing so.
447                // See trait-system-refactor-initiative#226 for some ideas here.
448                if TypingMode::Coherence == self.typing_mode()
449                    || !candidates.iter().any(|c| {
450                        matches!(
451                            c.source,
452                            CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
453                                | CandidateSource::AliasBound
454                        ) && has_no_inference_or_external_constraints(c.result)
455                    })
456                {
457                    self.assemble_impl_candidates(goal, &mut candidates);
458                    self.assemble_object_bound_candidates(goal, &mut candidates);
459                }
460            }
461            AssembleCandidatesFrom::EnvAndBounds => {}
462        }
463
464        (candidates, failed_candidate_info)
465    }
466
467    pub(super) fn forced_ambiguity(
468        &mut self,
469        cause: MaybeCause,
470    ) -> Result<Candidate<I>, NoSolution> {
471        // This may fail if `try_evaluate_added_goals` overflows because it
472        // fails to reach a fixpoint but ends up getting an error after
473        // running for some additional step.
474        //
475        // cc trait-system-refactor-initiative#105
476        let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
477        let certainty = Certainty::Maybe(cause);
478        self.probe_trait_candidate(source)
479            .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
480    }
481
482    #[instrument(level = "trace", skip_all)]
483    fn assemble_impl_candidates<G: GoalKind<D>>(
484        &mut self,
485        goal: Goal<I, G>,
486        candidates: &mut Vec<Candidate<I>>,
487    ) {
488        let cx = self.cx();
489        cx.for_each_relevant_impl(
490            goal.predicate.trait_def_id(cx),
491            goal.predicate.self_ty(),
492            |impl_def_id| {
493                // For every `default impl`, there's always a non-default `impl`
494                // that will *also* apply. There's no reason to register a candidate
495                // for this impl, since it is *not* proof that the trait goal holds.
496                if cx.impl_is_default(impl_def_id) {
497                    return;
498                }
499                match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
500                    ecx.evaluate_added_goals_and_make_canonical_response(certainty)
501                }) {
502                    Ok(candidate) => candidates.push(candidate),
503                    Err(NoSolution) => (),
504                }
505            },
506        );
507    }
508
509    #[instrument(level = "trace", skip_all)]
510    fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
511        &mut self,
512        goal: Goal<I, G>,
513        candidates: &mut Vec<Candidate<I>>,
514    ) {
515        let cx = self.cx();
516        let trait_def_id = goal.predicate.trait_def_id(cx);
517
518        // N.B. When assembling built-in candidates for lang items that are also
519        // `auto` traits, then the auto trait candidate that is assembled in
520        // `consider_auto_trait_candidate` MUST be disqualified to remain sound.
521        //
522        // Instead of adding the logic here, it's a better idea to add it in
523        // `EvalCtxt::disqualify_auto_trait_candidate_due_to_possible_impl` in
524        // `solve::trait_goals` instead.
525        let result = if let Err(guar) = goal.predicate.error_reported() {
526            G::consider_error_guaranteed_candidate(self, guar)
527        } else if cx.trait_is_auto(trait_def_id) {
528            G::consider_auto_trait_candidate(self, goal)
529        } else if cx.trait_is_alias(trait_def_id) {
530            G::consider_trait_alias_candidate(self, goal)
531        } else {
532            match cx.as_trait_lang_item(trait_def_id) {
533                Some(SolverTraitLangItem::Sized) => {
534                    G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
535                }
536                Some(SolverTraitLangItem::MetaSized) => {
537                    G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
538                }
539                Some(SolverTraitLangItem::PointeeSized) => {
540                    unreachable!("`PointeeSized` is removed during lowering");
541                }
542                Some(SolverTraitLangItem::Copy | SolverTraitLangItem::Clone) => {
543                    G::consider_builtin_copy_clone_candidate(self, goal)
544                }
545                Some(SolverTraitLangItem::Fn) => {
546                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
547                }
548                Some(SolverTraitLangItem::FnMut) => {
549                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
550                }
551                Some(SolverTraitLangItem::FnOnce) => {
552                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
553                }
554                Some(SolverTraitLangItem::AsyncFn) => {
555                    G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
556                }
557                Some(SolverTraitLangItem::AsyncFnMut) => {
558                    G::consider_builtin_async_fn_trait_candidates(
559                        self,
560                        goal,
561                        ty::ClosureKind::FnMut,
562                    )
563                }
564                Some(SolverTraitLangItem::AsyncFnOnce) => {
565                    G::consider_builtin_async_fn_trait_candidates(
566                        self,
567                        goal,
568                        ty::ClosureKind::FnOnce,
569                    )
570                }
571                Some(SolverTraitLangItem::FnPtrTrait) => {
572                    G::consider_builtin_fn_ptr_trait_candidate(self, goal)
573                }
574                Some(SolverTraitLangItem::AsyncFnKindHelper) => {
575                    G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
576                }
577                Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
578                Some(SolverTraitLangItem::PointeeTrait) => {
579                    G::consider_builtin_pointee_candidate(self, goal)
580                }
581                Some(SolverTraitLangItem::Future) => {
582                    G::consider_builtin_future_candidate(self, goal)
583                }
584                Some(SolverTraitLangItem::Iterator) => {
585                    G::consider_builtin_iterator_candidate(self, goal)
586                }
587                Some(SolverTraitLangItem::FusedIterator) => {
588                    G::consider_builtin_fused_iterator_candidate(self, goal)
589                }
590                Some(SolverTraitLangItem::AsyncIterator) => {
591                    G::consider_builtin_async_iterator_candidate(self, goal)
592                }
593                Some(SolverTraitLangItem::Coroutine) => {
594                    G::consider_builtin_coroutine_candidate(self, goal)
595                }
596                Some(SolverTraitLangItem::DiscriminantKind) => {
597                    G::consider_builtin_discriminant_kind_candidate(self, goal)
598                }
599                Some(SolverTraitLangItem::Destruct) => {
600                    G::consider_builtin_destruct_candidate(self, goal)
601                }
602                Some(SolverTraitLangItem::TransmuteTrait) => {
603                    G::consider_builtin_transmute_candidate(self, goal)
604                }
605                Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
606                    G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
607                }
608                _ => Err(NoSolution),
609            }
610        };
611
612        candidates.extend(result);
613
614        // There may be multiple unsize candidates for a trait with several supertraits:
615        // `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
616        if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
617            candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
618        }
619    }
620
621    #[instrument(level = "trace", skip_all)]
622    fn assemble_param_env_candidates<G: GoalKind<D>>(
623        &mut self,
624        goal: Goal<I, G>,
625        candidates: &mut Vec<Candidate<I>>,
626        failed_candidate_info: &mut FailedCandidateInfo,
627    ) {
628        for assumption in goal.param_env.caller_bounds().iter() {
629            match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
630                Ok(candidate) => candidates.push(candidate),
631                Err(head_usages) => {
632                    failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
633                }
634            }
635        }
636    }
637
638    #[instrument(level = "trace", skip_all)]
639    fn assemble_alias_bound_candidates<G: GoalKind<D>>(
640        &mut self,
641        goal: Goal<I, G>,
642        candidates: &mut Vec<Candidate<I>>,
643    ) {
644        let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
645            ecx.assemble_alias_bound_candidates_recur(
646                goal.predicate.self_ty(),
647                goal,
648                candidates,
649                AliasBoundKind::SelfBounds,
650            );
651        });
652    }
653
654    /// For some deeply nested `<T>::A::B::C::D` rigid associated type,
655    /// we should explore the item bounds for all levels, since the
656    /// `associated_type_bounds` feature means that a parent associated
657    /// type may carry bounds for a nested associated type.
658    ///
659    /// If we have a projection, check that its self type is a rigid projection.
660    /// If so, continue searching by recursively calling after normalization.
661    // FIXME: This may recurse infinitely, but I can't seem to trigger it without
662    // hitting another overflow error something. Add a depth parameter needed later.
663    fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
664        &mut self,
665        self_ty: I::Ty,
666        goal: Goal<I, G>,
667        candidates: &mut Vec<Candidate<I>>,
668        consider_self_bounds: AliasBoundKind,
669    ) {
670        let (kind, alias_ty) = match self_ty.kind() {
671            ty::Bool
672            | ty::Char
673            | ty::Int(_)
674            | ty::Uint(_)
675            | ty::Float(_)
676            | ty::Adt(_, _)
677            | ty::Foreign(_)
678            | ty::Str
679            | ty::Array(_, _)
680            | ty::Pat(_, _)
681            | ty::Slice(_)
682            | ty::RawPtr(_, _)
683            | ty::Ref(_, _, _)
684            | ty::FnDef(_, _)
685            | ty::FnPtr(..)
686            | ty::UnsafeBinder(_)
687            | ty::Dynamic(..)
688            | ty::Closure(..)
689            | ty::CoroutineClosure(..)
690            | ty::Coroutine(..)
691            | ty::CoroutineWitness(..)
692            | ty::Never
693            | ty::Tuple(_)
694            | ty::Param(_)
695            | ty::Placeholder(..)
696            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
697            | ty::Error(_) => return,
698            ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
699                panic!("unexpected self type for `{goal:?}`")
700            }
701
702            ty::Infer(ty::TyVar(_)) => {
703                // If we hit infer when normalizing the self type of an alias,
704                // then bail with ambiguity. We should never encounter this on
705                // the *first* iteration of this recursive function.
706                if let Ok(result) =
707                    self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
708                {
709                    candidates.push(Candidate {
710                        source: CandidateSource::AliasBound,
711                        result,
712                        head_usages: CandidateHeadUsages::default(),
713                    });
714                }
715                return;
716            }
717
718            ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
719            ty::Alias(ty::Inherent | ty::Free, _) => {
720                self.cx().delay_bug(format!("could not normalize {self_ty:?}, it is not WF"));
721                return;
722            }
723        };
724
725        match consider_self_bounds {
726            AliasBoundKind::SelfBounds => {
727                for assumption in self
728                    .cx()
729                    .item_self_bounds(alias_ty.def_id)
730                    .iter_instantiated(self.cx(), alias_ty.args)
731                {
732                    candidates.extend(G::probe_and_consider_implied_clause(
733                        self,
734                        CandidateSource::AliasBound,
735                        goal,
736                        assumption,
737                        [],
738                    ));
739                }
740            }
741            AliasBoundKind::NonSelfBounds => {
742                for assumption in self
743                    .cx()
744                    .item_non_self_bounds(alias_ty.def_id)
745                    .iter_instantiated(self.cx(), alias_ty.args)
746                {
747                    candidates.extend(G::probe_and_consider_implied_clause(
748                        self,
749                        CandidateSource::AliasBound,
750                        goal,
751                        assumption,
752                        [],
753                    ));
754                }
755            }
756        }
757
758        candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
759
760        if kind != ty::Projection {
761            return;
762        }
763
764        // Recurse on the self type of the projection.
765        match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
766            Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
767                next_self_ty,
768                goal,
769                candidates,
770                AliasBoundKind::NonSelfBounds,
771            ),
772            Err(NoSolution) => {}
773        }
774    }
775
776    #[instrument(level = "trace", skip_all)]
777    fn assemble_object_bound_candidates<G: GoalKind<D>>(
778        &mut self,
779        goal: Goal<I, G>,
780        candidates: &mut Vec<Candidate<I>>,
781    ) {
782        let cx = self.cx();
783        if !cx.trait_may_be_implemented_via_object(goal.predicate.trait_def_id(cx)) {
784            return;
785        }
786
787        let self_ty = goal.predicate.self_ty();
788        let bounds = match self_ty.kind() {
789            ty::Bool
790            | ty::Char
791            | ty::Int(_)
792            | ty::Uint(_)
793            | ty::Float(_)
794            | ty::Adt(_, _)
795            | ty::Foreign(_)
796            | ty::Str
797            | ty::Array(_, _)
798            | ty::Pat(_, _)
799            | ty::Slice(_)
800            | ty::RawPtr(_, _)
801            | ty::Ref(_, _, _)
802            | ty::FnDef(_, _)
803            | ty::FnPtr(..)
804            | ty::UnsafeBinder(_)
805            | ty::Alias(..)
806            | ty::Closure(..)
807            | ty::CoroutineClosure(..)
808            | ty::Coroutine(..)
809            | ty::CoroutineWitness(..)
810            | ty::Never
811            | ty::Tuple(_)
812            | ty::Param(_)
813            | ty::Placeholder(..)
814            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
815            | ty::Error(_) => return,
816            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
817            | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
818            ty::Dynamic(bounds, ..) => bounds,
819        };
820
821        // Do not consider built-in object impls for dyn-incompatible types.
822        if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
823            return;
824        }
825
826        // Consider all of the auto-trait and projection bounds, which don't
827        // need to be recorded as a `BuiltinImplSource::Object` since they don't
828        // really have a vtable base...
829        for bound in bounds.iter() {
830            match bound.skip_binder() {
831                ty::ExistentialPredicate::Trait(_) => {
832                    // Skip principal
833                }
834                ty::ExistentialPredicate::Projection(_)
835                | ty::ExistentialPredicate::AutoTrait(_) => {
836                    candidates.extend(G::probe_and_consider_object_bound_candidate(
837                        self,
838                        CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
839                        goal,
840                        bound.with_self_ty(cx, self_ty),
841                    ));
842                }
843            }
844        }
845
846        // FIXME: We only need to do *any* of this if we're considering a trait goal,
847        // since we don't need to look at any supertrait or anything if we are doing
848        // a projection goal.
849        if let Some(principal) = bounds.principal() {
850            let principal_trait_ref = principal.with_self_ty(cx, self_ty);
851            for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
852                candidates.extend(G::probe_and_consider_object_bound_candidate(
853                    self,
854                    CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
855                    goal,
856                    assumption.upcast(cx),
857                ));
858            }
859        }
860    }
861
862    /// In coherence we have to not only care about all impls we know about, but
863    /// also consider impls which may get added in a downstream or sibling crate
864    /// or which an upstream impl may add in a minor release.
865    ///
866    /// To do so we return a single ambiguous candidate in case such an unknown
867    /// impl could apply to the current goal.
868    #[instrument(level = "trace", skip_all)]
869    fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
870        &mut self,
871        goal: Goal<I, G>,
872    ) -> Result<Candidate<I>, NoSolution> {
873        self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
874            let cx = ecx.cx();
875            let trait_ref = goal.predicate.trait_ref(cx);
876            if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
877                Err(NoSolution)
878            } else {
879                // While the trait bound itself may be unknowable, we may be able to
880                // prove that a super trait is not implemented. For this, we recursively
881                // prove the super trait bounds of the current goal.
882                //
883                // We skip the goal itself as that one would cycle.
884                let predicate: I::Predicate = trait_ref.upcast(cx);
885                ecx.add_goals(
886                    GoalSource::Misc,
887                    elaborate::elaborate(cx, [predicate])
888                        .skip(1)
889                        .map(|predicate| goal.with(cx, predicate)),
890                );
891                ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
892            }
893        })
894    }
895}
896
897pub(super) enum AllowInferenceConstraints {
898    Yes,
899    No,
900}
901
902impl<D, I> EvalCtxt<'_, D>
903where
904    D: SolverDelegate<Interner = I>,
905    I: Interner,
906{
907    /// Check whether we can ignore impl candidates due to specialization.
908    ///
909    /// This is only necessary for `feature(specialization)` and seems quite ugly.
910    pub(super) fn filter_specialized_impls(
911        &mut self,
912        allow_inference_constraints: AllowInferenceConstraints,
913        candidates: &mut Vec<Candidate<I>>,
914    ) {
915        match self.typing_mode() {
916            TypingMode::Coherence => return,
917            TypingMode::Analysis { .. }
918            | TypingMode::Borrowck { .. }
919            | TypingMode::PostBorrowckAnalysis { .. }
920            | TypingMode::PostAnalysis => {}
921        }
922
923        let mut i = 0;
924        'outer: while i < candidates.len() {
925            let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
926                i += 1;
927                continue;
928            };
929
930            for (j, c) in candidates.iter().enumerate() {
931                if i == j {
932                    continue;
933                }
934
935                let CandidateSource::Impl(other_def_id) = c.source else {
936                    continue;
937                };
938
939                // See if we can toss out `victim` based on specialization.
940                //
941                // While this requires us to know *for sure* that the `lhs` impl applies
942                // we still use modulo regions here. This is fine as specialization currently
943                // assumes that specializing impls have to be always applicable, meaning that
944                // the only allowed region constraints may be constraints also present on the default impl.
945                if matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
946                    || has_only_region_constraints(c.result)
947                {
948                    if self.cx().impl_specializes(other_def_id, victim_def_id) {
949                        candidates.remove(i);
950                        continue 'outer;
951                    }
952                }
953            }
954
955            i += 1;
956        }
957    }
958
959    /// If the self type is the hidden type of an opaque, try to assemble
960    /// candidates for it by consider its item bounds and by using blanket
961    /// impls. This is used to incompletely guide type inference when handling
962    /// non-defining uses in the defining scope.
963    ///
964    /// We otherwise just fail fail with ambiguity. Even if we're using an
965    /// opaque type item bound or a blank impls, we still force its certainty
966    /// to be `Maybe` so that we properly prove this goal later.
967    ///
968    /// See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/182>
969    /// for why this is necessary.
970    fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
971        &mut self,
972        goal: Goal<I, G>,
973        assemble_from: AssembleCandidatesFrom,
974        candidates: &mut Vec<Candidate<I>>,
975    ) {
976        let self_ty = goal.predicate.self_ty();
977        // If the self type is sub unified with any opaque type, we
978        // also look at blanket impls for it.
979        let mut assemble_blanket_impls = false;
980        for alias_ty in self.opaques_with_sub_unified_hidden_type(self_ty) {
981            assemble_blanket_impls = true;
982            debug!("self ty is sub unified with {alias_ty:?}");
983
984            struct ReplaceOpaque<I: Interner> {
985                cx: I,
986                alias_ty: ty::AliasTy<I>,
987                self_ty: I::Ty,
988            }
989            impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
990                fn cx(&self) -> I {
991                    self.cx
992                }
993                fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
994                    if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
995                        if alias_ty == self.alias_ty {
996                            return self.self_ty;
997                        }
998                    }
999                    ty.super_fold_with(self)
1000                }
1001            }
1002
1003            // We look at all item-bounds of the opaque, replacing the
1004            // opaque with the current self type before considering
1005            // them as a candidate. Imagine e've got `?x: Trait<?y>`
1006            // and `?x` has been sub-unified with the hidden type of
1007            // `impl Trait<u32>`, We take the item bound `opaque: Trait<u32>`
1008            // and replace all occurrences of `opaque` with `?x`. This results
1009            // in a `?x: Trait<u32>` alias-bound candidate.
1010            for item_bound in self
1011                .cx()
1012                .item_self_bounds(alias_ty.def_id)
1013                .iter_instantiated(self.cx(), alias_ty.args)
1014            {
1015                let assumption =
1016                    item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1017                candidates.extend(G::probe_and_match_goal_against_assumption(
1018                    self,
1019                    CandidateSource::AliasBound,
1020                    goal,
1021                    assumption,
1022                    |ecx| {
1023                        // We want to reprove this goal once we've inferred the
1024                        // hidden type, so we force the certainty to `Maybe`.
1025                        ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1026                    },
1027                ));
1028            }
1029        }
1030
1031        // We also need to consider blanket impls for not-yet-defined opaque types.
1032        //
1033        // See tests/ui/impl-trait/non-defining-uses/use-blanket-impl.rs for an example.
1034        if assemble_blanket_impls && assemble_from.should_assemble_impl_candidates() {
1035            let cx = self.cx();
1036            cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1037                // For every `default impl`, there's always a non-default `impl`
1038                // that will *also* apply. There's no reason to register a candidate
1039                // for this impl, since it is *not* proof that the trait goal holds.
1040                if cx.impl_is_default(impl_def_id) {
1041                    return;
1042                }
1043
1044                match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1045                    if ecx.shallow_resolve(self_ty).is_ty_var() {
1046                        // We force the certainty of impl candidates to be `Maybe`.
1047                        let certainty = certainty.and(Certainty::AMBIGUOUS);
1048                        ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1049                    } else {
1050                        // We don't want to use impls if they constrain the opaque.
1051                        //
1052                        // FIXME(trait-system-refactor-initiative#229): This isn't
1053                        // perfect yet as it still allows us to incorrectly constrain
1054                        // other inference variables.
1055                        Err(NoSolution)
1056                    }
1057                }) {
1058                    Ok(candidate) => candidates.push(candidate),
1059                    Err(NoSolution) => (),
1060                }
1061            });
1062        }
1063
1064        if candidates.is_empty() {
1065            candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
1066        }
1067    }
1068
1069    /// Assemble and merge candidates for goals which are related to an underlying trait
1070    /// goal. Right now, this is normalizes-to and host effect goals.
1071    ///
1072    /// We sadly can't simply take all possible candidates for normalization goals
1073    /// and check whether they result in the same constraints. We want to make sure
1074    /// that trying to normalize an alias doesn't result in constraints which aren't
1075    /// otherwise required.
1076    ///
1077    /// Most notably, when proving a trait goal by via a where-bound, we should not
1078    /// normalize via impls which have stricter region constraints than the where-bound:
1079    ///
1080    /// ```rust
1081    /// trait Trait<'a> {
1082    ///     type Assoc;
1083    /// }
1084    ///
1085    /// impl<'a, T: 'a> Trait<'a> for T {
1086    ///     type Assoc = u32;
1087    /// }
1088    ///
1089    /// fn with_bound<'a, T: Trait<'a>>(_value: T::Assoc) {}
1090    /// ```
1091    ///
1092    /// The where-bound of `with_bound` doesn't specify the associated type, so we would
1093    /// only be able to normalize `<T as Trait<'a>>::Assoc` by using the impl. This impl
1094    /// adds a `T: 'a` bound however, which would result in a region error. Given that the
1095    /// user explicitly wrote that `T: Trait<'a>` holds, this is undesirable and we instead
1096    /// treat the alias as rigid.
1097    ///
1098    /// See trait-system-refactor-initiative#124 for more details.
1099    #[instrument(level = "debug", skip(self, inject_normalize_to_rigid_candidate), ret)]
1100    pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1101        &mut self,
1102        proven_via: Option<TraitGoalProvenVia>,
1103        goal: Goal<I, G>,
1104        inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1105    ) -> QueryResult<I> {
1106        let Some(proven_via) = proven_via else {
1107            // We don't care about overflow. If proving the trait goal overflowed, then
1108            // it's enough to report an overflow error for that, we don't also have to
1109            // overflow during normalization.
1110            //
1111            // We use `forced_ambiguity` here over `make_ambiguous_response_no_constraints`
1112            // because the former will also record a built-in candidate in the inspector.
1113            return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1114        };
1115
1116        match proven_via {
1117            TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1118                // Even when a trait bound has been proven using a where-bound, we
1119                // still need to consider alias-bounds for normalization, see
1120                // `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
1121                let (mut candidates, _) = self
1122                    .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1123
1124                // We still need to prefer where-bounds over alias-bounds however.
1125                // See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
1126                if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1127                    candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1128                } else if candidates.is_empty() {
1129                    // If the trait goal has been proven by using the environment, we want to treat
1130                    // aliases as rigid if there are no applicable projection bounds in the environment.
1131                    return inject_normalize_to_rigid_candidate(self);
1132                }
1133
1134                if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1135                    Ok(response)
1136                } else {
1137                    self.flounder(&candidates)
1138                }
1139            }
1140            TraitGoalProvenVia::Misc => {
1141                let (mut candidates, _) =
1142                    self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1143
1144                // Prefer "orphaned" param-env normalization predicates, which are used
1145                // (for example, and ideally only) when proving item bounds for an impl.
1146                if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1147                    candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1148                }
1149
1150                // We drop specialized impls to allow normalization via a final impl here. In case
1151                // the specializing impl has different inference constraints from the specialized
1152                // impl, proving the trait goal is already ambiguous, so we never get here. This
1153                // means we can just ignore inference constraints and don't have to special-case
1154                // constraining the normalized-to `term`.
1155                self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1156                if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1157                    Ok(response)
1158                } else {
1159                    self.flounder(&candidates)
1160                }
1161            }
1162        }
1163    }
1164
1165    /// Compute whether a param-env assumption is global or non-global after normalizing it.
1166    ///
1167    /// This is necessary because, for example, given:
1168    ///
1169    /// ```ignore,rust
1170    /// where
1171    ///     T: Trait<Assoc = u32>,
1172    ///     i32: From<T::Assoc>,
1173    /// ```
1174    ///
1175    /// The `i32: From<T::Assoc>` bound is non-global before normalization, but is global after.
1176    /// Since the old trait solver normalized param-envs eagerly, we want to emulate this
1177    /// behavior lazily.
1178    fn characterize_param_env_assumption(
1179        &mut self,
1180        param_env: I::ParamEnv,
1181        assumption: I::Clause,
1182    ) -> Result<CandidateSource<I>, NoSolution> {
1183        // FIXME: This should be fixed, but it also requires changing the behavior
1184        // in the old solver which is currently relied on.
1185        if assumption.has_bound_vars() {
1186            return Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal));
1187        }
1188
1189        match assumption.visit_with(&mut FindParamInClause {
1190            ecx: self,
1191            param_env,
1192            universes: vec![],
1193        }) {
1194            ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1195            ControlFlow::Break(Ok(())) => Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)),
1196            ControlFlow::Continue(()) => Ok(CandidateSource::ParamEnv(ParamEnvSource::Global)),
1197        }
1198    }
1199}
1200
1201struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1202    ecx: &'a mut EvalCtxt<'b, D>,
1203    param_env: I::ParamEnv,
1204    universes: Vec<Option<ty::UniverseIndex>>,
1205}
1206
1207impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1208where
1209    D: SolverDelegate<Interner = I>,
1210    I: Interner,
1211{
1212    type Result = ControlFlow<Result<(), NoSolution>>;
1213
1214    fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1215        self.universes.push(None);
1216        t.super_visit_with(self)?;
1217        self.universes.pop();
1218        ControlFlow::Continue(())
1219    }
1220
1221    fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1222        let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1223        let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1224            return ControlFlow::Break(Err(NoSolution));
1225        };
1226
1227        if let ty::Placeholder(p) = ty.kind() {
1228            if p.universe() == ty::UniverseIndex::ROOT {
1229                ControlFlow::Break(Ok(()))
1230            } else {
1231                ControlFlow::Continue(())
1232            }
1233        } else if ty.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1234            ty.super_visit_with(self)
1235        } else {
1236            ControlFlow::Continue(())
1237        }
1238    }
1239
1240    fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1241        let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1242        let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1243            return ControlFlow::Break(Err(NoSolution));
1244        };
1245
1246        if let ty::ConstKind::Placeholder(p) = ct.kind() {
1247            if p.universe() == ty::UniverseIndex::ROOT {
1248                ControlFlow::Break(Ok(()))
1249            } else {
1250                ControlFlow::Continue(())
1251            }
1252        } else if ct.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1253            ct.super_visit_with(self)
1254        } else {
1255            ControlFlow::Continue(())
1256        }
1257    }
1258
1259    fn visit_region(&mut self, r: I::Region) -> Self::Result {
1260        match self.ecx.eager_resolve_region(r).kind() {
1261            ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1262            ty::RePlaceholder(p) => {
1263                if p.universe() == ty::UniverseIndex::ROOT {
1264                    ControlFlow::Break(Ok(()))
1265                } else {
1266                    ControlFlow::Continue(())
1267                }
1268            }
1269            ty::ReVar(_) => ControlFlow::Break(Ok(())),
1270            ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1271                unreachable!("unexpected region in param-env clause")
1272            }
1273        }
1274    }
1275}