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