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