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                // This is somewhat inconsistent and may make #57893 slightly easier to exploit.
459                // However, it matches the behavior of the old solver. See
460                // `tests/ui/traits/next-solver/normalization-shadowing/use_object_if_empty_env.rs`.
461                if matches!(normalized_self_ty.kind(), ty::Dynamic(..))
462                    && !candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
463                {
464                    self.assemble_object_bound_candidates(goal, &mut candidates);
465                }
466            }
467        }
468
469        (candidates, failed_candidate_info)
470    }
471
472    pub(super) fn forced_ambiguity(
473        &mut self,
474        cause: MaybeCause,
475    ) -> Result<Candidate<I>, NoSolution> {
476        // This may fail if `try_evaluate_added_goals` overflows because it
477        // fails to reach a fixpoint but ends up getting an error after
478        // running for some additional step.
479        //
480        // FIXME(@lcnr): While I believe an error here to be possible, we
481        // currently don't have any test which actually triggers it. @lqd
482        // created a minimization for an ICE in typenum, but that one no
483        // longer fails here. cc trait-system-refactor-initiative#105.
484        let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
485        let certainty = Certainty::Maybe { cause, opaque_types_jank: OpaqueTypesJank::AllGood };
486        self.probe_trait_candidate(source)
487            .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
488    }
489
490    #[instrument(level = "trace", skip_all)]
491    fn assemble_impl_candidates<G: GoalKind<D>>(
492        &mut self,
493        goal: Goal<I, G>,
494        candidates: &mut Vec<Candidate<I>>,
495    ) {
496        let cx = self.cx();
497        cx.for_each_relevant_impl(
498            goal.predicate.trait_def_id(cx),
499            goal.predicate.self_ty(),
500            |impl_def_id| {
501                // For every `default impl`, there's always a non-default `impl`
502                // that will *also* apply. There's no reason to register a candidate
503                // for this impl, since it is *not* proof that the trait goal holds.
504                if cx.impl_is_default(impl_def_id) {
505                    return;
506                }
507                match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
508                    ecx.evaluate_added_goals_and_make_canonical_response(certainty)
509                }) {
510                    Ok(candidate) => candidates.push(candidate),
511                    Err(NoSolution) => (),
512                }
513            },
514        );
515    }
516
517    #[instrument(level = "trace", skip_all)]
518    fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
519        &mut self,
520        goal: Goal<I, G>,
521        candidates: &mut Vec<Candidate<I>>,
522    ) {
523        let cx = self.cx();
524        let trait_def_id = goal.predicate.trait_def_id(cx);
525
526        // N.B. When assembling built-in candidates for lang items that are also
527        // `auto` traits, then the auto trait candidate that is assembled in
528        // `consider_auto_trait_candidate` MUST be disqualified to remain sound.
529        //
530        // Instead of adding the logic here, it's a better idea to add it in
531        // `EvalCtxt::disqualify_auto_trait_candidate_due_to_possible_impl` in
532        // `solve::trait_goals` instead.
533        let result = if let Err(guar) = goal.predicate.error_reported() {
534            G::consider_error_guaranteed_candidate(self, guar)
535        } else if cx.trait_is_auto(trait_def_id) {
536            G::consider_auto_trait_candidate(self, goal)
537        } else if cx.trait_is_alias(trait_def_id) {
538            G::consider_trait_alias_candidate(self, goal)
539        } else {
540            match cx.as_trait_lang_item(trait_def_id) {
541                Some(SolverTraitLangItem::Sized) => {
542                    G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
543                }
544                Some(SolverTraitLangItem::MetaSized) => {
545                    G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
546                }
547                Some(SolverTraitLangItem::PointeeSized) => {
548                    unreachable!("`PointeeSized` is removed during lowering");
549                }
550                Some(
551                    SolverTraitLangItem::Copy
552                    | SolverTraitLangItem::Clone
553                    | SolverTraitLangItem::TrivialClone,
554                ) => G::consider_builtin_copy_clone_candidate(self, goal),
555                Some(SolverTraitLangItem::Fn) => {
556                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
557                }
558                Some(SolverTraitLangItem::FnMut) => {
559                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
560                }
561                Some(SolverTraitLangItem::FnOnce) => {
562                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
563                }
564                Some(SolverTraitLangItem::AsyncFn) => {
565                    G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
566                }
567                Some(SolverTraitLangItem::AsyncFnMut) => {
568                    G::consider_builtin_async_fn_trait_candidates(
569                        self,
570                        goal,
571                        ty::ClosureKind::FnMut,
572                    )
573                }
574                Some(SolverTraitLangItem::AsyncFnOnce) => {
575                    G::consider_builtin_async_fn_trait_candidates(
576                        self,
577                        goal,
578                        ty::ClosureKind::FnOnce,
579                    )
580                }
581                Some(SolverTraitLangItem::FnPtrTrait) => {
582                    G::consider_builtin_fn_ptr_trait_candidate(self, goal)
583                }
584                Some(SolverTraitLangItem::AsyncFnKindHelper) => {
585                    G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
586                }
587                Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
588                Some(SolverTraitLangItem::PointeeTrait) => {
589                    G::consider_builtin_pointee_candidate(self, goal)
590                }
591                Some(SolverTraitLangItem::Future) => {
592                    G::consider_builtin_future_candidate(self, goal)
593                }
594                Some(SolverTraitLangItem::Iterator) => {
595                    G::consider_builtin_iterator_candidate(self, goal)
596                }
597                Some(SolverTraitLangItem::FusedIterator) => {
598                    G::consider_builtin_fused_iterator_candidate(self, goal)
599                }
600                Some(SolverTraitLangItem::AsyncIterator) => {
601                    G::consider_builtin_async_iterator_candidate(self, goal)
602                }
603                Some(SolverTraitLangItem::Coroutine) => {
604                    G::consider_builtin_coroutine_candidate(self, goal)
605                }
606                Some(SolverTraitLangItem::DiscriminantKind) => {
607                    G::consider_builtin_discriminant_kind_candidate(self, goal)
608                }
609                Some(SolverTraitLangItem::Destruct) => {
610                    G::consider_builtin_destruct_candidate(self, goal)
611                }
612                Some(SolverTraitLangItem::TransmuteTrait) => {
613                    G::consider_builtin_transmute_candidate(self, goal)
614                }
615                Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
616                    G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
617                }
618                _ => Err(NoSolution),
619            }
620        };
621
622        candidates.extend(result);
623
624        // There may be multiple unsize candidates for a trait with several supertraits:
625        // `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
626        if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
627            candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
628        }
629    }
630
631    #[instrument(level = "trace", skip_all)]
632    fn assemble_param_env_candidates<G: GoalKind<D>>(
633        &mut self,
634        goal: Goal<I, G>,
635        candidates: &mut Vec<Candidate<I>>,
636        failed_candidate_info: &mut FailedCandidateInfo,
637    ) {
638        for assumption in goal.param_env.caller_bounds().iter() {
639            match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
640                Ok(candidate) => candidates.push(candidate),
641                Err(head_usages) => {
642                    failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
643                }
644            }
645        }
646    }
647
648    #[instrument(level = "trace", skip_all)]
649    fn assemble_alias_bound_candidates<G: GoalKind<D>>(
650        &mut self,
651        goal: Goal<I, G>,
652        candidates: &mut Vec<Candidate<I>>,
653    ) {
654        let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
655            ecx.assemble_alias_bound_candidates_recur(
656                goal.predicate.self_ty(),
657                goal,
658                candidates,
659                AliasBoundKind::SelfBounds,
660            );
661        });
662    }
663
664    /// For some deeply nested `<T>::A::B::C::D` rigid associated type,
665    /// we should explore the item bounds for all levels, since the
666    /// `associated_type_bounds` feature means that a parent associated
667    /// type may carry bounds for a nested associated type.
668    ///
669    /// If we have a projection, check that its self type is a rigid projection.
670    /// If so, continue searching by recursively calling after normalization.
671    // FIXME: This may recurse infinitely, but I can't seem to trigger it without
672    // hitting another overflow error something. Add a depth parameter needed later.
673    fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
674        &mut self,
675        self_ty: I::Ty,
676        goal: Goal<I, G>,
677        candidates: &mut Vec<Candidate<I>>,
678        consider_self_bounds: AliasBoundKind,
679    ) {
680        let (kind, alias_ty) = match self_ty.kind() {
681            ty::Bool
682            | ty::Char
683            | ty::Int(_)
684            | ty::Uint(_)
685            | ty::Float(_)
686            | ty::Adt(_, _)
687            | ty::Foreign(_)
688            | ty::Str
689            | ty::Array(_, _)
690            | ty::Pat(_, _)
691            | ty::Slice(_)
692            | ty::RawPtr(_, _)
693            | ty::Ref(_, _, _)
694            | ty::FnDef(_, _)
695            | ty::FnPtr(..)
696            | ty::UnsafeBinder(_)
697            | ty::Dynamic(..)
698            | ty::Closure(..)
699            | ty::CoroutineClosure(..)
700            | ty::Coroutine(..)
701            | ty::CoroutineWitness(..)
702            | ty::Never
703            | ty::Tuple(_)
704            | ty::Param(_)
705            | ty::Placeholder(..)
706            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
707            | ty::Error(_) => return,
708            ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
709                panic!("unexpected self type for `{goal:?}`")
710            }
711
712            ty::Infer(ty::TyVar(_)) => {
713                // If we hit infer when normalizing the self type of an alias,
714                // then bail with ambiguity. We should never encounter this on
715                // the *first* iteration of this recursive function.
716                if let Ok(result) =
717                    self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
718                {
719                    candidates.push(Candidate {
720                        source: CandidateSource::AliasBound(consider_self_bounds),
721                        result,
722                        head_usages: CandidateHeadUsages::default(),
723                    });
724                }
725                return;
726            }
727
728            ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
729            ty::Alias(ty::Inherent | ty::Free, _) => {
730                self.cx().delay_bug(format!("could not normalize {self_ty:?}, it is not WF"));
731                return;
732            }
733        };
734
735        match consider_self_bounds {
736            AliasBoundKind::SelfBounds => {
737                for assumption in self
738                    .cx()
739                    .item_self_bounds(alias_ty.def_id)
740                    .iter_instantiated(self.cx(), alias_ty.args)
741                {
742                    candidates.extend(G::probe_and_consider_implied_clause(
743                        self,
744                        CandidateSource::AliasBound(consider_self_bounds),
745                        goal,
746                        assumption,
747                        [],
748                    ));
749                }
750            }
751            AliasBoundKind::NonSelfBounds => {
752                for assumption in self
753                    .cx()
754                    .item_non_self_bounds(alias_ty.def_id)
755                    .iter_instantiated(self.cx(), alias_ty.args)
756                {
757                    candidates.extend(G::probe_and_consider_implied_clause(
758                        self,
759                        CandidateSource::AliasBound(consider_self_bounds),
760                        goal,
761                        assumption,
762                        [],
763                    ));
764                }
765            }
766        }
767
768        candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
769
770        if kind != ty::Projection {
771            return;
772        }
773
774        // Recurse on the self type of the projection.
775        match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
776            Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
777                next_self_ty,
778                goal,
779                candidates,
780                AliasBoundKind::NonSelfBounds,
781            ),
782            Err(NoSolution) => {}
783        }
784    }
785
786    #[instrument(level = "trace", skip_all)]
787    fn assemble_object_bound_candidates<G: GoalKind<D>>(
788        &mut self,
789        goal: Goal<I, G>,
790        candidates: &mut Vec<Candidate<I>>,
791    ) {
792        let cx = self.cx();
793        if !cx.trait_may_be_implemented_via_object(goal.predicate.trait_def_id(cx)) {
794            return;
795        }
796
797        let self_ty = goal.predicate.self_ty();
798        let bounds = match self_ty.kind() {
799            ty::Bool
800            | ty::Char
801            | ty::Int(_)
802            | ty::Uint(_)
803            | ty::Float(_)
804            | ty::Adt(_, _)
805            | ty::Foreign(_)
806            | ty::Str
807            | ty::Array(_, _)
808            | ty::Pat(_, _)
809            | ty::Slice(_)
810            | ty::RawPtr(_, _)
811            | ty::Ref(_, _, _)
812            | ty::FnDef(_, _)
813            | ty::FnPtr(..)
814            | ty::UnsafeBinder(_)
815            | ty::Alias(..)
816            | ty::Closure(..)
817            | ty::CoroutineClosure(..)
818            | ty::Coroutine(..)
819            | ty::CoroutineWitness(..)
820            | ty::Never
821            | ty::Tuple(_)
822            | ty::Param(_)
823            | ty::Placeholder(..)
824            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
825            | ty::Error(_) => return,
826            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
827            | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
828            ty::Dynamic(bounds, ..) => bounds,
829        };
830
831        // Do not consider built-in object impls for dyn-incompatible types.
832        if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
833            return;
834        }
835
836        // Consider all of the auto-trait and projection bounds, which don't
837        // need to be recorded as a `BuiltinImplSource::Object` since they don't
838        // really have a vtable base...
839        for bound in bounds.iter() {
840            match bound.skip_binder() {
841                ty::ExistentialPredicate::Trait(_) => {
842                    // Skip principal
843                }
844                ty::ExistentialPredicate::Projection(_)
845                | ty::ExistentialPredicate::AutoTrait(_) => {
846                    candidates.extend(G::probe_and_consider_object_bound_candidate(
847                        self,
848                        CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
849                        goal,
850                        bound.with_self_ty(cx, self_ty),
851                    ));
852                }
853            }
854        }
855
856        // FIXME: We only need to do *any* of this if we're considering a trait goal,
857        // since we don't need to look at any supertrait or anything if we are doing
858        // a projection goal.
859        if let Some(principal) = bounds.principal() {
860            let principal_trait_ref = principal.with_self_ty(cx, self_ty);
861            for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
862                candidates.extend(G::probe_and_consider_object_bound_candidate(
863                    self,
864                    CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
865                    goal,
866                    assumption.upcast(cx),
867                ));
868            }
869        }
870    }
871
872    /// In coherence we have to not only care about all impls we know about, but
873    /// also consider impls which may get added in a downstream or sibling crate
874    /// or which an upstream impl may add in a minor release.
875    ///
876    /// To do so we return a single ambiguous candidate in case such an unknown
877    /// impl could apply to the current goal.
878    #[instrument(level = "trace", skip_all)]
879    fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
880        &mut self,
881        goal: Goal<I, G>,
882    ) -> Result<Candidate<I>, NoSolution> {
883        self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
884            let cx = ecx.cx();
885            let trait_ref = goal.predicate.trait_ref(cx);
886            if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
887                Err(NoSolution)
888            } else {
889                // While the trait bound itself may be unknowable, we may be able to
890                // prove that a super trait is not implemented. For this, we recursively
891                // prove the super trait bounds of the current goal.
892                //
893                // We skip the goal itself as that one would cycle.
894                let predicate: I::Predicate = trait_ref.upcast(cx);
895                ecx.add_goals(
896                    GoalSource::Misc,
897                    elaborate::elaborate(cx, [predicate])
898                        .skip(1)
899                        .map(|predicate| goal.with(cx, predicate)),
900                );
901                ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
902            }
903        })
904    }
905}
906
907pub(super) enum AllowInferenceConstraints {
908    Yes,
909    No,
910}
911
912impl<D, I> EvalCtxt<'_, D>
913where
914    D: SolverDelegate<Interner = I>,
915    I: Interner,
916{
917    /// Check whether we can ignore impl candidates due to specialization.
918    ///
919    /// This is only necessary for `feature(specialization)` and seems quite ugly.
920    pub(super) fn filter_specialized_impls(
921        &mut self,
922        allow_inference_constraints: AllowInferenceConstraints,
923        candidates: &mut Vec<Candidate<I>>,
924    ) {
925        match self.typing_mode() {
926            TypingMode::Coherence => return,
927            TypingMode::Analysis { .. }
928            | TypingMode::Borrowck { .. }
929            | TypingMode::PostBorrowckAnalysis { .. }
930            | TypingMode::PostAnalysis => {}
931        }
932
933        let mut i = 0;
934        'outer: while i < candidates.len() {
935            let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
936                i += 1;
937                continue;
938            };
939
940            for (j, c) in candidates.iter().enumerate() {
941                if i == j {
942                    continue;
943                }
944
945                let CandidateSource::Impl(other_def_id) = c.source else {
946                    continue;
947                };
948
949                // See if we can toss out `victim` based on specialization.
950                //
951                // While this requires us to know *for sure* that the `lhs` impl applies
952                // we still use modulo regions here. This is fine as specialization currently
953                // assumes that specializing impls have to be always applicable, meaning that
954                // the only allowed region constraints may be constraints also present on the default impl.
955                if matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
956                    || has_only_region_constraints(c.result)
957                {
958                    if self.cx().impl_specializes(other_def_id, victim_def_id) {
959                        candidates.remove(i);
960                        continue 'outer;
961                    }
962                }
963            }
964
965            i += 1;
966        }
967    }
968
969    /// If the self type is the hidden type of an opaque, try to assemble
970    /// candidates for it by consider its item bounds and by using blanket
971    /// impls. This is used to incompletely guide type inference when handling
972    /// non-defining uses in the defining scope.
973    ///
974    /// We otherwise just fail fail with ambiguity. Even if we're using an
975    /// opaque type item bound or a blank impls, we still force its certainty
976    /// to be `Maybe` so that we properly prove this goal later.
977    ///
978    /// See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/182>
979    /// for why this is necessary.
980    fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
981        &mut self,
982        goal: Goal<I, G>,
983        assemble_from: AssembleCandidatesFrom,
984        candidates: &mut Vec<Candidate<I>>,
985    ) {
986        let self_ty = goal.predicate.self_ty();
987        // We only use this hack during HIR typeck.
988        let opaque_types = match self.typing_mode() {
989            TypingMode::Analysis { .. } => self.opaques_with_sub_unified_hidden_type(self_ty),
990            TypingMode::Coherence
991            | TypingMode::Borrowck { .. }
992            | TypingMode::PostBorrowckAnalysis { .. }
993            | TypingMode::PostAnalysis => vec![],
994        };
995
996        if opaque_types.is_empty() {
997            candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
998            return;
999        }
1000
1001        for &alias_ty in &opaque_types {
1002            debug!("self ty is sub unified with {alias_ty:?}");
1003
1004            struct ReplaceOpaque<I: Interner> {
1005                cx: I,
1006                alias_ty: ty::AliasTy<I>,
1007                self_ty: I::Ty,
1008            }
1009            impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
1010                fn cx(&self) -> I {
1011                    self.cx
1012                }
1013                fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1014                    if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
1015                        if alias_ty == self.alias_ty {
1016                            return self.self_ty;
1017                        }
1018                    }
1019                    ty.super_fold_with(self)
1020                }
1021            }
1022
1023            // We look at all item-bounds of the opaque, replacing the
1024            // opaque with the current self type before considering
1025            // them as a candidate. Imagine e've got `?x: Trait<?y>`
1026            // and `?x` has been sub-unified with the hidden type of
1027            // `impl Trait<u32>`, We take the item bound `opaque: Trait<u32>`
1028            // and replace all occurrences of `opaque` with `?x`. This results
1029            // in a `?x: Trait<u32>` alias-bound candidate.
1030            for item_bound in self
1031                .cx()
1032                .item_self_bounds(alias_ty.def_id)
1033                .iter_instantiated(self.cx(), alias_ty.args)
1034            {
1035                let assumption =
1036                    item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1037                candidates.extend(G::probe_and_match_goal_against_assumption(
1038                    self,
1039                    CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
1040                    goal,
1041                    assumption,
1042                    |ecx| {
1043                        // We want to reprove this goal once we've inferred the
1044                        // hidden type, so we force the certainty to `Maybe`.
1045                        ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1046                    },
1047                ));
1048            }
1049        }
1050
1051        // If the self type is sub unified with any opaque type, we also look at blanket
1052        // impls for it.
1053        //
1054        // See tests/ui/impl-trait/non-defining-uses/use-blanket-impl.rs for an example.
1055        if assemble_from.should_assemble_impl_candidates() {
1056            let cx = self.cx();
1057            cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1058                // For every `default impl`, there's always a non-default `impl`
1059                // that will *also* apply. There's no reason to register a candidate
1060                // for this impl, since it is *not* proof that the trait goal holds.
1061                if cx.impl_is_default(impl_def_id) {
1062                    return;
1063                }
1064
1065                match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1066                    if ecx.shallow_resolve(self_ty).is_ty_var() {
1067                        // We force the certainty of impl candidates to be `Maybe`.
1068                        let certainty = certainty.and(Certainty::AMBIGUOUS);
1069                        ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1070                    } else {
1071                        // We don't want to use impls if they constrain the opaque.
1072                        //
1073                        // FIXME(trait-system-refactor-initiative#229): This isn't
1074                        // perfect yet as it still allows us to incorrectly constrain
1075                        // other inference variables.
1076                        Err(NoSolution)
1077                    }
1078                }) {
1079                    Ok(candidate) => candidates.push(candidate),
1080                    Err(NoSolution) => (),
1081                }
1082            });
1083        }
1084
1085        if candidates.is_empty() {
1086            let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
1087            let certainty = Certainty::Maybe {
1088                cause: MaybeCause::Ambiguity,
1089                opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
1090            };
1091            candidates
1092                .extend(self.probe_trait_candidate(source).enter(|this| {
1093                    this.evaluate_added_goals_and_make_canonical_response(certainty)
1094                }));
1095        }
1096    }
1097
1098    /// Assemble and merge candidates for goals which are related to an underlying trait
1099    /// goal. Right now, this is normalizes-to and host effect goals.
1100    ///
1101    /// We sadly can't simply take all possible candidates for normalization goals
1102    /// and check whether they result in the same constraints. We want to make sure
1103    /// that trying to normalize an alias doesn't result in constraints which aren't
1104    /// otherwise required.
1105    ///
1106    /// Most notably, when proving a trait goal by via a where-bound, we should not
1107    /// normalize via impls which have stricter region constraints than the where-bound:
1108    ///
1109    /// ```rust
1110    /// trait Trait<'a> {
1111    ///     type Assoc;
1112    /// }
1113    ///
1114    /// impl<'a, T: 'a> Trait<'a> for T {
1115    ///     type Assoc = u32;
1116    /// }
1117    ///
1118    /// fn with_bound<'a, T: Trait<'a>>(_value: T::Assoc) {}
1119    /// ```
1120    ///
1121    /// The where-bound of `with_bound` doesn't specify the associated type, so we would
1122    /// only be able to normalize `<T as Trait<'a>>::Assoc` by using the impl. This impl
1123    /// adds a `T: 'a` bound however, which would result in a region error. Given that the
1124    /// user explicitly wrote that `T: Trait<'a>` holds, this is undesirable and we instead
1125    /// treat the alias as rigid.
1126    ///
1127    /// See trait-system-refactor-initiative#124 for more details.
1128    #[instrument(level = "debug", skip(self, inject_normalize_to_rigid_candidate), ret)]
1129    pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1130        &mut self,
1131        proven_via: Option<TraitGoalProvenVia>,
1132        goal: Goal<I, G>,
1133        inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1134    ) -> QueryResult<I> {
1135        let Some(proven_via) = proven_via else {
1136            // We don't care about overflow. If proving the trait goal overflowed, then
1137            // it's enough to report an overflow error for that, we don't also have to
1138            // overflow during normalization.
1139            //
1140            // We use `forced_ambiguity` here over `make_ambiguous_response_no_constraints`
1141            // because the former will also record a built-in candidate in the inspector.
1142            return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1143        };
1144
1145        match proven_via {
1146            TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1147                // Even when a trait bound has been proven using a where-bound, we
1148                // still need to consider alias-bounds for normalization, see
1149                // `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
1150                let (mut candidates, _) = self
1151                    .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1152
1153                // We still need to prefer where-bounds over alias-bounds however.
1154                // See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
1155                if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1156                    candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1157                } else if candidates.is_empty() {
1158                    // If the trait goal has been proven by using the environment, we want to treat
1159                    // aliases as rigid if there are no applicable projection bounds in the environment.
1160                    return inject_normalize_to_rigid_candidate(self);
1161                }
1162
1163                if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1164                    Ok(response)
1165                } else {
1166                    self.flounder(&candidates)
1167                }
1168            }
1169            TraitGoalProvenVia::Misc => {
1170                let (mut candidates, _) =
1171                    self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1172
1173                // Prefer "orphaned" param-env normalization predicates, which are used
1174                // (for example, and ideally only) when proving item bounds for an impl.
1175                if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1176                    candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1177                }
1178
1179                // We drop specialized impls to allow normalization via a final impl here. In case
1180                // the specializing impl has different inference constraints from the specialized
1181                // impl, proving the trait goal is already ambiguous, so we never get here. This
1182                // means we can just ignore inference constraints and don't have to special-case
1183                // constraining the normalized-to `term`.
1184                self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1185                if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1186                    Ok(response)
1187                } else {
1188                    self.flounder(&candidates)
1189                }
1190            }
1191        }
1192    }
1193
1194    /// Compute whether a param-env assumption is global or non-global after normalizing it.
1195    ///
1196    /// This is necessary because, for example, given:
1197    ///
1198    /// ```ignore,rust
1199    /// where
1200    ///     T: Trait<Assoc = u32>,
1201    ///     i32: From<T::Assoc>,
1202    /// ```
1203    ///
1204    /// The `i32: From<T::Assoc>` bound is non-global before normalization, but is global after.
1205    /// Since the old trait solver normalized param-envs eagerly, we want to emulate this
1206    /// behavior lazily.
1207    fn characterize_param_env_assumption(
1208        &mut self,
1209        param_env: I::ParamEnv,
1210        assumption: I::Clause,
1211    ) -> Result<CandidateSource<I>, NoSolution> {
1212        // FIXME: This should be fixed, but it also requires changing the behavior
1213        // in the old solver which is currently relied on.
1214        if assumption.has_bound_vars() {
1215            return Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal));
1216        }
1217
1218        match assumption.visit_with(&mut FindParamInClause {
1219            ecx: self,
1220            param_env,
1221            universes: vec![],
1222        }) {
1223            ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1224            ControlFlow::Break(Ok(())) => Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)),
1225            ControlFlow::Continue(()) => Ok(CandidateSource::ParamEnv(ParamEnvSource::Global)),
1226        }
1227    }
1228}
1229
1230struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1231    ecx: &'a mut EvalCtxt<'b, D>,
1232    param_env: I::ParamEnv,
1233    universes: Vec<Option<ty::UniverseIndex>>,
1234}
1235
1236impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1237where
1238    D: SolverDelegate<Interner = I>,
1239    I: Interner,
1240{
1241    type Result = ControlFlow<Result<(), NoSolution>>;
1242
1243    fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1244        self.universes.push(None);
1245        t.super_visit_with(self)?;
1246        self.universes.pop();
1247        ControlFlow::Continue(())
1248    }
1249
1250    fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1251        let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1252        let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1253            return ControlFlow::Break(Err(NoSolution));
1254        };
1255
1256        if let ty::Placeholder(p) = ty.kind() {
1257            if p.universe() == ty::UniverseIndex::ROOT {
1258                ControlFlow::Break(Ok(()))
1259            } else {
1260                ControlFlow::Continue(())
1261            }
1262        } else if ty.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1263            ty.super_visit_with(self)
1264        } else {
1265            ControlFlow::Continue(())
1266        }
1267    }
1268
1269    fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1270        let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1271        let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1272            return ControlFlow::Break(Err(NoSolution));
1273        };
1274
1275        if let ty::ConstKind::Placeholder(p) = ct.kind() {
1276            if p.universe() == ty::UniverseIndex::ROOT {
1277                ControlFlow::Break(Ok(()))
1278            } else {
1279                ControlFlow::Continue(())
1280            }
1281        } else if ct.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1282            ct.super_visit_with(self)
1283        } else {
1284            ControlFlow::Continue(())
1285        }
1286    }
1287
1288    fn visit_region(&mut self, r: I::Region) -> Self::Result {
1289        match self.ecx.eager_resolve_region(r).kind() {
1290            ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1291            ty::RePlaceholder(p) => {
1292                if p.universe() == ty::UniverseIndex::ROOT {
1293                    ControlFlow::Break(Ok(()))
1294                } else {
1295                    ControlFlow::Continue(())
1296                }
1297            }
1298            ty::ReVar(_) => ControlFlow::Break(Ok(())),
1299            ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1300                unreachable!("unexpected region in param-env clause")
1301            }
1302        }
1303    }
1304}