Skip to main content

rustc_next_trait_solver/solve/
trait_goals.rs

1//! Dealing with trait goals, i.e. `T: Trait<'a, U>`.
2
3use rustc_type_ir::data_structures::IndexSet;
4use rustc_type_ir::fast_reject::DeepRejectCtxt;
5use rustc_type_ir::inherent::*;
6use rustc_type_ir::lang_items::SolverTraitLangItem;
7use rustc_type_ir::solve::{
8    AliasBoundKind, CandidatePreferenceMode, CanonicalResponse, MaybeInfo,
9    NoSolutionOrRerunNonErased, OpaqueTypesJank, QueryResultOrRerunNonErased, RerunNonErased,
10    RerunReason, RerunResultExt, SizedTraitKind,
11};
12use rustc_type_ir::{
13    self as ty, FieldInfo, Interner, MayBeErased, Movability, PredicatePolarity, TraitPredicate,
14    TraitRef, TypeVisitableExt as _, TypingMode, Unnormalized, Upcast as _, elaborate,
15};
16use tracing::{debug, instrument, trace, warn};
17
18use crate::delegate::SolverDelegate;
19use crate::solve::assembly::structural_traits::{self, AsyncCallableRelevantTypes};
20use crate::solve::assembly::{
21    self, AllowInferenceConstraints, AssembleCandidatesFrom, Candidate, FailedCandidateInfo,
22};
23use crate::solve::inspect::ProbeKind;
24use crate::solve::{
25    BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeCause,
26    MergeCandidateInfo, NoSolution, ParamEnvSource, StalledOnCoroutines,
27    has_only_region_constraints,
28};
29
30impl<D, I> assembly::GoalKind<D> for TraitPredicate<I>
31where
32    D: SolverDelegate<Interner = I>,
33    I: Interner,
34{
35    fn self_ty(self) -> I::Ty {
36        self.self_ty()
37    }
38
39    fn trait_ref(self, _: I) -> ty::TraitRef<I> {
40        self.trait_ref
41    }
42
43    fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
44        self.with_replaced_self_ty(cx, self_ty)
45    }
46
47    fn trait_def_id(self, _: I) -> I::TraitId {
48        self.def_id()
49    }
50
51    fn consider_additional_alias_assumptions(
52        _ecx: &mut EvalCtxt<'_, D>,
53        _goal: Goal<I, Self>,
54        _alias_ty: ty::AliasTy<I>,
55    ) -> Vec<Candidate<I>> {
56        ::alloc::vec::Vec::new()vec![]
57    }
58
59    fn consider_impl_candidate(
60        ecx: &mut EvalCtxt<'_, D>,
61        goal: Goal<I, TraitPredicate<I>>,
62        impl_def_id: I::ImplId,
63        then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResultOrRerunNonErased<I>,
64    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
65        let cx = ecx.cx();
66
67        let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
68        if !DeepRejectCtxt::relate_rigid_infer(ecx.cx())
69            .args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args)
70        {
71            return Err(NoSolution.into());
72        }
73
74        // An upper bound of the certainty of this goal, used to lower the certainty
75        // of reservation impl to ambiguous during coherence.
76        let impl_polarity = cx.impl_polarity(impl_def_id);
77        let maximal_certainty = match (impl_polarity, goal.predicate.polarity) {
78            // In coherence mode, this is ambiguous. But outside of coherence, it's not a real impl.
79            (ty::ImplPolarity::Reservation, _) => {
80                if ecx.typing_mode().is_coherence() {
81                    Certainty::AMBIGUOUS
82                } else {
83                    return Err(NoSolution.into());
84                }
85            }
86
87            // Impl matches polarity
88            (ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive)
89            | (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => Certainty::Yes,
90
91            // Impl doesn't match polarity
92            (ty::ImplPolarity::Positive, ty::PredicatePolarity::Negative)
93            | (ty::ImplPolarity::Negative, ty::PredicatePolarity::Positive) => {
94                return Err(NoSolution.into());
95            }
96        };
97
98        ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
99            let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
100            ecx.record_impl_args(impl_args);
101            let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args).skip_norm_wip();
102
103            ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
104            let where_clause_bounds = cx
105                .predicates_of(impl_def_id.into())
106                .iter_instantiated(cx, impl_args)
107                .map(Unnormalized::skip_norm_wip)
108                .map(|pred| goal.with(cx, pred));
109            ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds)?;
110
111            // We currently elaborate all supertrait outlives obligations from impls.
112            // This can be removed when we actually do coinduction correctly, and prove
113            // all supertrait obligations unconditionally.
114            ecx.add_goals(
115                GoalSource::Misc,
116                cx.impl_super_outlives(impl_def_id)
117                    .iter_instantiated(cx, impl_args)
118                    .map(Unnormalized::skip_norm_wip)
119                    .map(|pred| goal.with(cx, pred)),
120            )?;
121
122            then(ecx, maximal_certainty)
123        })
124    }
125
126    fn consider_error_guaranteed_candidate(
127        ecx: &mut EvalCtxt<'_, D>,
128        _goal: Goal<I, Self>,
129        _guar: I::ErrorGuaranteed,
130    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
131        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
132            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
133    }
134
135    fn fast_reject_assumption(
136        ecx: &mut EvalCtxt<'_, D>,
137        goal: Goal<I, Self>,
138        assumption: I::Clause,
139    ) -> Result<(), NoSolution> {
140        fn trait_def_id_matches<I: Interner>(
141            cx: I,
142            clause_def_id: I::TraitId,
143            goal_def_id: I::TraitId,
144            polarity: PredicatePolarity,
145        ) -> bool {
146            clause_def_id == goal_def_id
147            // PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so
148            // check for a `MetaSized` supertrait being matched against a `Sized` assumption.
149            //
150            // `PointeeSized` bounds are syntactic sugar for a lack of bounds so don't need this.
151                || (polarity == PredicatePolarity::Positive
152                    && cx.is_trait_lang_item(clause_def_id, SolverTraitLangItem::Sized)
153                    && cx.is_trait_lang_item(goal_def_id, SolverTraitLangItem::MetaSized))
154        }
155
156        if let Some(trait_clause) = assumption.as_trait_clause()
157            && trait_clause.polarity() == goal.predicate.polarity
158            && trait_def_id_matches(
159                ecx.cx(),
160                trait_clause.def_id(),
161                goal.predicate.def_id(),
162                goal.predicate.polarity,
163            )
164            && DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
165                goal.predicate.trait_ref.args,
166                trait_clause.skip_binder().trait_ref.args,
167            )
168        {
169            return Ok(());
170        } else {
171            Err(NoSolution)
172        }
173    }
174
175    fn match_assumption(
176        ecx: &mut EvalCtxt<'_, D>,
177        goal: Goal<I, Self>,
178        assumption: I::Clause,
179        then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResultOrRerunNonErased<I>,
180    ) -> QueryResultOrRerunNonErased<I> {
181        let trait_clause = assumption.as_trait_clause().unwrap();
182
183        // PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so
184        // check for a `Sized` subtrait when looking for `MetaSized`. `PointeeSized` bounds
185        // are syntactic sugar for a lack of bounds so don't need this.
186        // We don't need to check polarity, `fast_reject_assumption` already rejected non-`Positive`
187        // polarity `Sized` assumptions as matching non-`Positive` `MetaSized` goals.
188        if ecx.cx().is_trait_lang_item(goal.predicate.def_id(), SolverTraitLangItem::MetaSized)
189            && ecx.cx().is_trait_lang_item(trait_clause.def_id(), SolverTraitLangItem::Sized)
190        {
191            let meta_sized_clause =
192                trait_predicate_with_def_id(ecx.cx(), trait_clause, goal.predicate.def_id());
193            return Self::match_assumption(ecx, goal, meta_sized_clause, then);
194        }
195
196        let assumption_trait_pred = ecx.instantiate_binder_with_infer(trait_clause);
197        ecx.eq(goal.param_env, goal.predicate.trait_ref, assumption_trait_pred.trait_ref)?;
198
199        then(ecx)
200    }
201
202    fn consider_auto_trait_candidate(
203        ecx: &mut EvalCtxt<'_, D>,
204        goal: Goal<I, Self>,
205    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
206        let cx = ecx.cx();
207        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
208            return Err(NoSolution.into());
209        }
210
211        if let Some(result) = ecx.disqualify_auto_trait_candidate_due_to_possible_impl(goal) {
212            return result;
213        }
214
215        // Only consider auto impls of unsafe traits when there are no unsafe
216        // fields.
217        if cx.trait_is_unsafe(goal.predicate.def_id())
218            && goal.predicate.self_ty().has_unsafe_fields()
219        {
220            return Err(NoSolution.into());
221        }
222
223        // We leak the implemented auto traits of opaques outside of their defining scope.
224        // This depends on `typeck` of the defining scope of that opaque, which may result in
225        // fatal query cycles.
226        //
227        // We only get to this point if we're outside of the defining scope as we'd otherwise
228        // be able to normalize the opaque type. We may also cycle in case `typeck` of a defining
229        // scope relies on the current context, e.g. either because it also leaks auto trait
230        // bounds of opaques defined in the current context or by evaluating the current item.
231        //
232        // To avoid this we don't try to leak auto trait bounds if they can also be proven via
233        // item bounds of the opaque. These bounds are always applicable as auto traits must not
234        // have any generic parameters. They would also get preferred over the impl candidate
235        // when merging candidates anyways.
236        //
237        // See tests/ui/impl-trait/auto-trait-leakage/avoid-query-cycle-via-item-bound.rs.
238        if let ty::Alias(is_rigid, ty::AliasTy { kind: ty::Opaque { def_id }, .. }) =
239            goal.predicate.self_ty().kind()
240        {
241            if true {
    if !(is_rigid == ty::IsRigid::Yes) {
        ::core::panicking::panic("assertion failed: is_rigid == ty::IsRigid::Yes")
    };
};debug_assert!(is_rigid == ty::IsRigid::Yes);
242            if ecx.opaque_accesses.might_rerun() {
243                ecx.opaque_accesses.rerun_always(RerunReason::AutoTraitLeakage)?;
244                return Err(NoSolution.into());
245            }
246
247            for item_bound in cx.item_self_bounds(def_id.into()).skip_binder() {
248                if item_bound
249                    .as_trait_clause()
250                    .is_some_and(|b| b.def_id() == goal.predicate.def_id())
251                {
252                    return Err(NoSolution.into());
253                }
254            }
255        }
256
257        // We need to make sure to stall any coroutines we are inferring to avoid query cycles.
258        if let Some(cand) = ecx.try_stall_coroutine(goal.predicate.self_ty()) {
259            return cand;
260        }
261
262        ecx.probe_and_evaluate_goal_for_constituent_tys(
263            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
264            goal,
265            structural_traits::instantiate_constituent_tys_for_auto_trait,
266        )
267    }
268
269    fn consider_trait_alias_candidate(
270        ecx: &mut EvalCtxt<'_, D>,
271        goal: Goal<I, Self>,
272    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
273        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
274            return Err(NoSolution.into());
275        }
276
277        let cx = ecx.cx();
278
279        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
280            let nested_obligations = cx
281                .predicates_of(goal.predicate.def_id().into())
282                .iter_instantiated(cx, goal.predicate.trait_ref.args)
283                .map(Unnormalized::skip_norm_wip)
284                .map(|p| goal.with(cx, p));
285            // While you could think of trait aliases to have a single builtin impl
286            // which uses its implied trait bounds as where-clauses, using
287            // `GoalSource::ImplWhereClause` here would be incorrect, as we also
288            // impl them, which means we're "stepping out of the impl constructor"
289            // again. To handle this, we treat these cycles as ambiguous for now.
290            ecx.add_goals(GoalSource::Misc, nested_obligations)?;
291            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
292        })
293    }
294
295    fn consider_builtin_sizedness_candidates(
296        ecx: &mut EvalCtxt<'_, D>,
297        goal: Goal<I, Self>,
298        sizedness: SizedTraitKind,
299    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
300        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
301            return Err(NoSolution.into());
302        }
303
304        ecx.probe_and_evaluate_goal_for_constituent_tys(
305            CandidateSource::BuiltinImpl(BuiltinImplSource::Trivial),
306            goal,
307            |ecx, ty| {
308                structural_traits::instantiate_constituent_tys_for_sizedness_trait(
309                    ecx, sizedness, ty,
310                )
311            },
312        )
313    }
314
315    fn consider_builtin_copy_clone_candidate(
316        ecx: &mut EvalCtxt<'_, D>,
317        goal: Goal<I, Self>,
318    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
319        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
320            return Err(NoSolution.into());
321        }
322
323        // We need to make sure to stall any coroutines we are inferring to avoid query cycles.
324        if let Some(cand) = ecx.try_stall_coroutine(goal.predicate.self_ty()) {
325            return cand;
326        }
327
328        ecx.probe_and_evaluate_goal_for_constituent_tys(
329            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
330            goal,
331            structural_traits::instantiate_constituent_tys_for_copy_clone_trait,
332        )
333    }
334
335    fn consider_builtin_fn_ptr_trait_candidate(
336        ecx: &mut EvalCtxt<'_, D>,
337        goal: Goal<I, Self>,
338    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
339        let self_ty = goal.predicate.self_ty();
340        match goal.predicate.polarity {
341            // impl FnPtr for FnPtr {}
342            ty::PredicatePolarity::Positive => {
343                if self_ty.is_fn_ptr() {
344                    ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
345                        ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
346                    })
347                } else {
348                    Err(NoSolution.into())
349                }
350            }
351            //  impl !FnPtr for T where T != FnPtr && T is rigid {}
352            ty::PredicatePolarity::Negative => {
353                // If a type is rigid and not a fn ptr, then we know for certain
354                // that it does *not* implement `FnPtr`.
355                if !self_ty.is_fn_ptr() && self_ty.is_known_rigid() {
356                    ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
357                        ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
358                    })
359                } else {
360                    Err(NoSolution.into())
361                }
362            }
363        }
364    }
365
366    fn consider_builtin_fn_trait_candidates(
367        ecx: &mut EvalCtxt<'_, D>,
368        goal: Goal<I, Self>,
369        goal_kind: ty::ClosureKind,
370    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
371        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
372            return Err(NoSolution.into());
373        }
374
375        let cx = ecx.cx();
376        let Some(tupled_inputs_and_output) =
377            structural_traits::extract_tupled_inputs_and_output_from_callable(
378                cx,
379                goal.predicate.self_ty(),
380                goal_kind,
381            )?
382        else {
383            return ecx.forced_ambiguity(MaybeInfo::AMBIGUOUS);
384        };
385        let (inputs, output) = ecx.instantiate_binder_with_infer(tupled_inputs_and_output);
386
387        // A built-in `Fn` impl only holds if the output is sized.
388        // (FIXME: technically we only need to check this if the type is a fn ptr...)
389        let output_is_sized_pred =
390            ty::TraitRef::new(cx, cx.require_trait_lang_item(SolverTraitLangItem::Sized), [output]);
391
392        let pred =
393            ty::TraitRef::new(cx, goal.predicate.def_id(), [goal.predicate.self_ty(), inputs])
394                .upcast(cx);
395        Self::probe_and_consider_implied_clause(
396            ecx,
397            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
398            goal,
399            pred,
400            [(GoalSource::ImplWhereBound, goal.with(cx, output_is_sized_pred))],
401        )
402    }
403
404    fn consider_builtin_async_fn_trait_candidates(
405        ecx: &mut EvalCtxt<'_, D>,
406        goal: Goal<I, Self>,
407        goal_kind: ty::ClosureKind,
408    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
409        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
410            return Err(NoSolution.into());
411        }
412
413        let cx = ecx.cx();
414        let (tupled_inputs_and_output_and_coroutine, nested_preds) =
415            structural_traits::extract_tupled_inputs_and_output_from_async_callable(
416                cx,
417                goal.predicate.self_ty(),
418                goal_kind,
419                // This region doesn't matter because we're throwing away the coroutine type
420                Region::new_static(cx),
421            )?;
422        let AsyncCallableRelevantTypes {
423            tupled_inputs_ty,
424            output_coroutine_ty,
425            coroutine_return_ty: _,
426        } = ecx.instantiate_binder_with_infer(tupled_inputs_and_output_and_coroutine);
427
428        // A built-in `AsyncFn` impl only holds if the output is sized.
429        // (FIXME: technically we only need to check this if the type is a fn ptr...)
430        let output_is_sized_pred = ty::TraitRef::new(
431            cx,
432            cx.require_trait_lang_item(SolverTraitLangItem::Sized),
433            [output_coroutine_ty],
434        );
435
436        let pred = ty::TraitRef::new(
437            cx,
438            goal.predicate.def_id(),
439            [goal.predicate.self_ty(), tupled_inputs_ty],
440        )
441        .upcast(cx);
442        Self::probe_and_consider_implied_clause(
443            ecx,
444            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
445            goal,
446            pred,
447            [goal.with(cx, output_is_sized_pred)]
448                .into_iter()
449                .chain(nested_preds.into_iter().map(|pred| goal.with(cx, pred)))
450                .map(|goal| (GoalSource::ImplWhereBound, goal)),
451        )
452    }
453
454    fn consider_builtin_async_fn_kind_helper_candidate(
455        ecx: &mut EvalCtxt<'_, D>,
456        goal: Goal<I, Self>,
457    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
458        let [closure_fn_kind_ty, goal_kind_ty] = *goal.predicate.trait_ref.args.as_slice() else {
459            ::core::panicking::panic("explicit panic");panic!();
460        };
461
462        let Some(closure_kind) = closure_fn_kind_ty.expect_ty().to_opt_closure_kind() else {
463            // We don't need to worry about the self type being an infer var.
464            return Err(NoSolution.into());
465        };
466        let goal_kind = goal_kind_ty.expect_ty().to_opt_closure_kind().unwrap();
467        if closure_kind.extends(goal_kind) {
468            ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
469                .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
470        } else {
471            Err(NoSolution.into())
472        }
473    }
474
475    /// ```rust, ignore (not valid rust syntax)
476    /// impl Tuple for () {}
477    /// impl Tuple for (T1,) {}
478    /// impl Tuple for (T1, T2) {}
479    /// impl Tuple for (T1, .., Tn) {}
480    /// ```
481    fn consider_builtin_tuple_candidate(
482        ecx: &mut EvalCtxt<'_, D>,
483        goal: Goal<I, Self>,
484    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
485        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
486            return Err(NoSolution.into());
487        }
488
489        if let ty::Tuple(..) = goal.predicate.self_ty().kind() {
490            ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
491                .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
492        } else {
493            Err(NoSolution.into())
494        }
495    }
496
497    fn consider_builtin_pointee_candidate(
498        ecx: &mut EvalCtxt<'_, D>,
499        goal: Goal<I, Self>,
500    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
501        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
502            return Err(NoSolution.into());
503        }
504
505        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
506            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
507    }
508
509    fn consider_builtin_future_candidate(
510        ecx: &mut EvalCtxt<'_, D>,
511        goal: Goal<I, Self>,
512    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
513        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
514            return Err(NoSolution.into());
515        }
516
517        let ty::Coroutine(def_id, _) = goal.predicate.self_ty().kind() else {
518            return Err(NoSolution.into());
519        };
520
521        // Coroutines are not futures unless they come from `async` desugaring
522        let cx = ecx.cx();
523        if !cx.coroutine_is_async(def_id) {
524            return Err(NoSolution.into());
525        }
526
527        // Async coroutine unconditionally implement `Future`
528        // Technically, we need to check that the future output type is Sized,
529        // but that's already proven by the coroutine being WF.
530        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
531            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
532    }
533
534    fn consider_builtin_iterator_candidate(
535        ecx: &mut EvalCtxt<'_, D>,
536        goal: Goal<I, Self>,
537    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
538        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
539            return Err(NoSolution.into());
540        }
541
542        let ty::Coroutine(def_id, _) = goal.predicate.self_ty().kind() else {
543            return Err(NoSolution.into());
544        };
545
546        // Coroutines are not iterators unless they come from `gen` desugaring
547        let cx = ecx.cx();
548        if !cx.coroutine_is_gen(def_id) {
549            return Err(NoSolution.into());
550        }
551
552        // Gen coroutines unconditionally implement `Iterator`
553        // Technically, we need to check that the iterator output type is Sized,
554        // but that's already proven by the coroutines being WF.
555        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
556            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
557    }
558
559    fn consider_builtin_fused_iterator_candidate(
560        ecx: &mut EvalCtxt<'_, D>,
561        goal: Goal<I, Self>,
562    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
563        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
564            return Err(NoSolution.into());
565        }
566
567        let ty::Coroutine(def_id, _) = goal.predicate.self_ty().kind() else {
568            return Err(NoSolution.into());
569        };
570
571        // Coroutines are not iterators unless they come from `gen` desugaring
572        let cx = ecx.cx();
573        if !cx.coroutine_is_gen(def_id) {
574            return Err(NoSolution.into());
575        }
576
577        // Gen coroutines unconditionally implement `FusedIterator`.
578        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
579            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
580    }
581
582    fn consider_builtin_async_iterator_candidate(
583        ecx: &mut EvalCtxt<'_, D>,
584        goal: Goal<I, Self>,
585    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
586        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
587            return Err(NoSolution.into());
588        }
589
590        let ty::Coroutine(def_id, _) = goal.predicate.self_ty().kind() else {
591            return Err(NoSolution.into());
592        };
593
594        // Coroutines are not iterators unless they come from `gen` desugaring
595        let cx = ecx.cx();
596        if !cx.coroutine_is_async_gen(def_id) {
597            return Err(NoSolution.into());
598        }
599
600        // Gen coroutines unconditionally implement `Iterator`
601        // Technically, we need to check that the iterator output type is Sized,
602        // but that's already proven by the coroutines being WF.
603        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
604            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
605    }
606
607    fn consider_builtin_coroutine_candidate(
608        ecx: &mut EvalCtxt<'_, D>,
609        goal: Goal<I, Self>,
610    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
611        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
612            return Err(NoSolution.into());
613        }
614
615        let self_ty = goal.predicate.self_ty();
616        let ty::Coroutine(def_id, args) = self_ty.kind() else {
617            return Err(NoSolution.into());
618        };
619
620        // `async`-desugared coroutines do not implement the coroutine trait
621        let cx = ecx.cx();
622        if !cx.is_general_coroutine(def_id) {
623            return Err(NoSolution.into());
624        }
625
626        let coroutine = args.as_coroutine();
627        Self::probe_and_consider_implied_clause(
628            ecx,
629            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
630            goal,
631            ty::TraitRef::new(cx, goal.predicate.def_id(), [self_ty, coroutine.resume_ty()])
632                .upcast(cx),
633            // Technically, we need to check that the coroutine types are Sized,
634            // but that's already proven by the coroutine being WF.
635            [],
636        )
637    }
638
639    fn consider_builtin_discriminant_kind_candidate(
640        ecx: &mut EvalCtxt<'_, D>,
641        goal: Goal<I, Self>,
642    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
643        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
644            return Err(NoSolution.into());
645        }
646
647        // `DiscriminantKind` is automatically implemented for every type.
648        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
649            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
650    }
651
652    fn consider_builtin_destruct_candidate(
653        ecx: &mut EvalCtxt<'_, D>,
654        goal: Goal<I, Self>,
655    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
656        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
657            return Err(NoSolution.into());
658        }
659
660        // `Destruct` is automatically implemented for every type in
661        // non-const environments.
662        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
663            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
664    }
665
666    fn consider_builtin_transmute_candidate(
667        ecx: &mut EvalCtxt<'_, D>,
668        goal: Goal<I, Self>,
669    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
670        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
671            return Err(NoSolution.into());
672        }
673
674        // `rustc_transmute` does not have support for type or const params
675        if goal.predicate.has_non_region_placeholders() {
676            return Err(NoSolution.into());
677        }
678
679        // Match the old solver by treating unresolved inference variables as
680        // ambiguous until `rustc_transmute` can compute their layout.
681        if goal.has_non_region_infer() {
682            return ecx.forced_ambiguity(MaybeInfo::AMBIGUOUS);
683        }
684
685        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(
686            |ecx| -> Result<_, NoSolutionOrRerunNonErased> {
687                let assume = ecx.structurally_normalize_const(
688                    goal.param_env,
689                    goal.predicate.trait_ref.args.const_at(2),
690                )?;
691
692                let certainty = ecx.is_transmutable(
693                    goal.predicate.trait_ref.args.type_at(0),
694                    goal.predicate.trait_ref.args.type_at(1),
695                    assume,
696                )?;
697                ecx.evaluate_added_goals_and_make_canonical_response(certainty)
698            },
699        )
700    }
701
702    /// NOTE: This is implemented as a built-in goal and not a set of impls like:
703    ///
704    /// ```rust,ignore (illustrative)
705    /// impl<T> BikeshedGuaranteedNoDrop for T where T: Copy {}
706    /// impl<T> BikeshedGuaranteedNoDrop for ManuallyDrop<T> {}
707    /// ```
708    ///
709    /// because these impls overlap, and I'd rather not build a coherence hack for
710    /// this harmless overlap.
711    ///
712    /// This trait is indirectly exposed on stable, so do *not* extend the set of types that
713    /// implement the trait without FCP!
714    fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
715        ecx: &mut EvalCtxt<'_, D>,
716        goal: Goal<I, Self>,
717    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
718        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
719            return Err(NoSolution.into());
720        }
721
722        let cx = ecx.cx();
723        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
724            let ty = goal.predicate.self_ty();
725            match ty.kind() {
726                // `&mut T` and `&T` always implement `BikeshedGuaranteedNoDrop`.
727                ty::Ref(..) => {}
728                // `ManuallyDrop<T>` always implements `BikeshedGuaranteedNoDrop`.
729                ty::Adt(def, _) if def.is_manually_drop() => {}
730                // Arrays and tuples implement `BikeshedGuaranteedNoDrop` only if
731                // their constituent types implement `BikeshedGuaranteedNoDrop`.
732                ty::Tuple(tys) => {
733                    ecx.add_goals(
734                        GoalSource::ImplWhereBound,
735                        tys.iter().map(|elem_ty| {
736                            goal.with(cx, ty::TraitRef::new(cx, goal.predicate.def_id(), [elem_ty]))
737                        }),
738                    )?;
739                }
740                ty::Array(elem_ty, _) => {
741                    ecx.add_goal(
742                        GoalSource::ImplWhereBound,
743                        goal.with(cx, ty::TraitRef::new(cx, goal.predicate.def_id(), [elem_ty])),
744                    )?;
745                }
746
747                // All other types implement `BikeshedGuaranteedNoDrop` only if
748                // they implement `Copy`. We could be smart here and short-circuit
749                // some trivially `Copy`/`!Copy` types, but there's no benefit.
750                ty::FnDef(..)
751                | ty::FnPtr(..)
752                | ty::Error(_)
753                | ty::Uint(_)
754                | ty::Int(_)
755                | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
756                | ty::Bool
757                | ty::Float(_)
758                | ty::Char
759                | ty::RawPtr(..)
760                | ty::Never
761                | ty::Pat(..)
762                | ty::Dynamic(..)
763                | ty::Str
764                | ty::Slice(_)
765                | ty::Foreign(..)
766                | ty::Adt(..)
767                | ty::Alias(..)
768                | ty::Param(_)
769                | ty::Placeholder(..)
770                | ty::Closure(..)
771                | ty::CoroutineClosure(..)
772                | ty::Coroutine(..)
773                | ty::UnsafeBinder(_)
774                | ty::CoroutineWitness(..) => {
775                    ecx.add_goal(
776                        GoalSource::ImplWhereBound,
777                        goal.with(
778                            cx,
779                            ty::TraitRef::new(
780                                cx,
781                                cx.require_trait_lang_item(SolverTraitLangItem::Copy),
782                                [ty],
783                            ),
784                        ),
785                    )?;
786                }
787
788                ty::Bound(..)
789                | ty::Infer(
790                    ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_),
791                ) => {
792                    { ::core::panicking::panic_fmt(format_args!("unexpected type `{0:?}`", ty)); }panic!("unexpected type `{ty:?}`")
793                }
794            }
795
796            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
797        })
798    }
799
800    /// ```ignore (builtin impl example)
801    /// trait Trait {
802    ///     fn foo(&self);
803    /// }
804    /// // results in the following builtin impl
805    /// impl<'a, T: Trait + 'a> Unsize<dyn Trait + 'a> for T {}
806    /// ```
807    fn consider_structural_builtin_unsize_candidates(
808        ecx: &mut EvalCtxt<'_, D>,
809        goal: Goal<I, Self>,
810    ) -> Result<Vec<Candidate<I>>, RerunNonErased> {
811        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
812            return Ok(::alloc::vec::Vec::new()vec![]);
813        }
814
815        let result = ecx.probe(|_| ProbeKind::UnsizeAssembly).enter(
816            |ecx| -> Result<Vec<Candidate<I>>, NoSolutionOrRerunNonErased> {
817                let a_ty = goal.predicate.self_ty();
818                // We need to normalize the b_ty since it's matched structurally
819                // in the other functions below.
820                let b_ty = ecx.structurally_normalize_ty(
821                    goal.param_env,
822                    goal.predicate.trait_ref.args.type_at(1),
823                )?;
824
825                let goal = goal.with(ecx.cx(), (a_ty, b_ty));
826                match (a_ty.kind(), b_ty.kind()) {
827                    (ty::Infer(ty::TyVar(..)), ..) => {
    ::core::panicking::panic_fmt(format_args!("unexpected infer {0:?} {1:?}",
            a_ty, b_ty));
}panic!("unexpected infer {a_ty:?} {b_ty:?}"),
828
829                    (_, ty::Infer(ty::TyVar(..))) => {
830                        Ok(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ecx.forced_ambiguity(MaybeInfo::AMBIGUOUS)?]))vec![ecx.forced_ambiguity(MaybeInfo::AMBIGUOUS)?])
831                    }
832
833                    // Trait upcasting, or `dyn Trait + Auto + 'a` -> `dyn Trait + 'b`.
834                    (ty::Dynamic(a_data, a_region), ty::Dynamic(b_data, b_region)) => Ok(ecx
835                        .consider_builtin_dyn_upcast_candidates(
836                            goal, a_data, a_region, b_data, b_region,
837                        )),
838
839                    // `T` -> `dyn Trait` unsizing.
840                    (_, ty::Dynamic(b_region, b_data)) => Ok(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ecx.consider_builtin_unsize_to_dyn_candidate(goal, b_region,
                        b_data)?]))vec![
841                        ecx.consider_builtin_unsize_to_dyn_candidate(goal, b_region, b_data)?,
842                    ]),
843
844                    // `[T; N]` -> `[T]` unsizing
845                    (ty::Array(a_elem_ty, ..), ty::Slice(b_elem_ty)) => {
846                        Ok(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ecx.consider_builtin_array_unsize(goal, a_elem_ty, b_elem_ty)?]))vec![ecx.consider_builtin_array_unsize(goal, a_elem_ty, b_elem_ty)?])
847                    }
848
849                    // `Struct<T>` -> `Struct<U>` where `T: Unsize<U>`
850                    (ty::Adt(a_def, a_args), ty::Adt(b_def, b_args))
851                        if a_def.is_struct() && a_def == b_def =>
852                    {
853                        Ok(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ecx.consider_builtin_struct_unsize(goal, a_def, a_args, b_args)?]))vec![ecx.consider_builtin_struct_unsize(goal, a_def, a_args, b_args)?])
854                    }
855
856                    _ => Err(NoSolution.into()),
857                }
858            },
859        );
860
861        match result.map_err_to_rerun()? {
862            Ok(resp) => Ok(resp),
863            Err(NoSolution) => Ok(::alloc::vec::Vec::new()vec![]),
864        }
865    }
866
867    fn consider_builtin_field_candidate(
868        ecx: &mut EvalCtxt<'_, D>,
869        goal: Goal<I, Self>,
870    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
871        if goal.predicate.polarity != ty::PredicatePolarity::Positive {
872            return Err(NoSolution.into());
873        }
874        if let ty::Adt(def, args) = goal.predicate.self_ty().kind()
875            && let Some(FieldInfo { base, ty, .. }) =
876                def.field_representing_type_info(ecx.cx(), args)
877            && {
878                let sized_trait = ecx.cx().require_trait_lang_item(SolverTraitLangItem::Sized);
879                // FIXME: add better support for builtin impls of traits that check for the bounds
880                // on the trait definition in std.
881
882                // NOTE: these bounds have to be kept in sync with the definition of the `Field`
883                // trait in `library/core/src/field.rs` as well as the old trait solver `fn
884                // assemble_candidates_for_field_trait` in
885                // `compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs`.
886                ecx.add_goal(
887                    GoalSource::ImplWhereBound,
888                    Goal {
889                        param_env: goal.param_env,
890                        predicate: TraitRef::new(ecx.cx(), sized_trait, [base]).upcast(ecx.cx()),
891                    },
892                )?;
893                ecx.add_goal(
894                    GoalSource::ImplWhereBound,
895                    Goal {
896                        param_env: goal.param_env,
897                        predicate: TraitRef::new(ecx.cx(), sized_trait, [ty]).upcast(ecx.cx()),
898                    },
899                )?;
900                // FIXME(field_projections): This function does some questionable incomplete stuff by
901                // returning `Err(NoSolution)` on ambiguity.
902                ecx.try_evaluate_added_goals()? == Certainty::Yes
903            }
904            && match base.kind() {
905                ty::Adt(def, _) => def.is_struct() && !def.is_packed(),
906                ty::Tuple(..) => true,
907                _ => false,
908            }
909        {
910            ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
911                .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
912        } else {
913            Err(NoSolution.into())
914        }
915    }
916}
917
918/// Small helper function to change the `def_id` of a trait predicate - this is not normally
919/// something that you want to do, as different traits will require different args and so making
920/// it easy to change the trait is something of a footgun, but it is useful in the narrow
921/// circumstance of changing from `MetaSized` to `Sized`, which happens as part of the lazy
922/// elaboration of sizedness candidates.
923#[inline(always)]
924fn trait_predicate_with_def_id<I: Interner>(
925    cx: I,
926    clause: ty::Binder<I, ty::TraitPredicate<I>>,
927    did: I::TraitId,
928) -> I::Clause {
929    clause
930        .map_bound(|c| TraitPredicate {
931            trait_ref: TraitRef::new_from_args(cx, did, c.trait_ref.args),
932            polarity: c.polarity,
933        })
934        .upcast(cx)
935}
936
937impl<D, I> EvalCtxt<'_, D>
938where
939    D: SolverDelegate<Interner = I>,
940    I: Interner,
941{
942    /// Trait upcasting allows for coercions between trait objects:
943    /// ```ignore (builtin impl example)
944    /// trait Super {}
945    /// trait Trait: Super {}
946    /// // results in builtin impls upcasting to a super trait
947    /// impl<'a, 'b: 'a> Unsize<dyn Super + 'a> for dyn Trait + 'b {}
948    /// // and impls removing auto trait bounds.
949    /// impl<'a, 'b: 'a> Unsize<dyn Trait + 'a> for dyn Trait + Send + 'b {}
950    /// ```
951    fn consider_builtin_dyn_upcast_candidates(
952        &mut self,
953        goal: Goal<I, (I::Ty, I::Ty)>,
954        a_data: I::BoundExistentialPredicates,
955        a_region: I::Region,
956        b_data: I::BoundExistentialPredicates,
957        b_region: I::Region,
958    ) -> Vec<Candidate<I>> {
959        let cx = self.cx();
960        let Goal { predicate: (a_ty, _b_ty), .. } = goal;
961
962        let mut responses = ::alloc::vec::Vec::new()vec![];
963        // If the principal def ids match (or are both none), then we're not doing
964        // trait upcasting. We're just removing auto traits (or shortening the lifetime).
965        let b_principal_def_id = b_data.principal_def_id();
966        if a_data.principal_def_id() == b_principal_def_id || b_principal_def_id.is_none() {
967            responses.extend(self.consider_builtin_upcast_to_principal(
968                goal,
969                CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
970                a_data,
971                a_region,
972                b_data,
973                b_region,
974                a_data.principal(),
975            ));
976        } else if let Some(a_principal) = a_data.principal() {
977            for (idx, new_a_principal) in
978                elaborate::supertraits(self.cx(), a_principal.with_self_ty(cx, a_ty))
979                    .enumerate()
980                    .skip(1)
981            {
982                responses.extend(self.consider_builtin_upcast_to_principal(
983                    goal,
984                    CandidateSource::BuiltinImpl(BuiltinImplSource::TraitUpcasting(idx)),
985                    a_data,
986                    a_region,
987                    b_data,
988                    b_region,
989                    Some(new_a_principal.map_bound(|trait_ref| {
990                        ty::ExistentialTraitRef::erase_self_ty(cx, trait_ref)
991                    })),
992                ));
993            }
994        }
995
996        responses
997    }
998
999    fn consider_builtin_unsize_to_dyn_candidate(
1000        &mut self,
1001        goal: Goal<I, (I::Ty, I::Ty)>,
1002        b_data: I::BoundExistentialPredicates,
1003        b_region: I::Region,
1004    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1005        let cx = self.cx();
1006        let Goal { predicate: (a_ty, _), .. } = goal;
1007
1008        // Can only unsize to an dyn-compatible trait.
1009        if b_data.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
1010            return Err(NoSolution.into());
1011        }
1012
1013        self.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
1014            // Check that the type implements all of the predicates of the trait object.
1015            // (i.e. the principal, all of the associated types match, and any auto traits)
1016            ecx.add_goals(
1017                GoalSource::ImplWhereBound,
1018                b_data.iter().map(|pred| goal.with(cx, pred.with_self_ty(cx, a_ty))),
1019            )?;
1020
1021            // The type must be `Sized` to be unsized.
1022            ecx.add_goal(
1023                GoalSource::ImplWhereBound,
1024                goal.with(
1025                    cx,
1026                    ty::TraitRef::new(
1027                        cx,
1028                        cx.require_trait_lang_item(SolverTraitLangItem::Sized),
1029                        [a_ty],
1030                    ),
1031                ),
1032            )?;
1033
1034            // The type must outlive the lifetime of the `dyn` we're unsizing into.
1035            ecx.add_goal(GoalSource::Misc, goal.with(cx, ty::OutlivesPredicate(a_ty, b_region)))?;
1036            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1037        })
1038    }
1039
1040    fn consider_builtin_upcast_to_principal(
1041        &mut self,
1042        goal: Goal<I, (I::Ty, I::Ty)>,
1043        source: CandidateSource<I>,
1044        a_data: I::BoundExistentialPredicates,
1045        a_region: I::Region,
1046        b_data: I::BoundExistentialPredicates,
1047        b_region: I::Region,
1048        upcast_principal: Option<ty::Binder<I, ty::ExistentialTraitRef<I>>>,
1049    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1050        let param_env = goal.param_env;
1051
1052        // We may upcast to auto traits that are either explicitly listed in
1053        // the object type's bounds, or implied by the principal trait ref's
1054        // supertraits.
1055        let a_auto_traits: IndexSet<I::TraitId> = a_data
1056            .auto_traits()
1057            .into_iter()
1058            .chain(a_data.principal_def_id().into_iter().flat_map(|principal_def_id| {
1059                elaborate::supertrait_def_ids(self.cx(), principal_def_id)
1060                    .filter(|def_id| self.cx().trait_is_auto(*def_id))
1061            }))
1062            .collect();
1063
1064        // More than one projection in a_ty's bounds may match the projection
1065        // in b_ty's bound. Use this to first determine *which* apply without
1066        // having any inference side-effects. We process obligations because
1067        // unification may initially succeed due to deferred projection equality.
1068        let projection_may_match =
1069            |ecx: &mut EvalCtxt<'_, D>,
1070             source_projection: ty::Binder<I, ty::ExistentialProjection<I>>,
1071             target_projection: ty::Binder<I, ty::ExistentialProjection<I>>| {
1072                source_projection.item_def_id() == target_projection.item_def_id()
1073                    && ecx
1074                        .probe(|_| ProbeKind::ProjectionCompatibility)
1075                        .enter(|ecx| {
1076                            ecx.enter_forall_with_assumptions(
1077                                target_projection,
1078                                param_env,
1079                                |ecx, target_projection| {
1080                                    let source_projection =
1081                                        ecx.instantiate_binder_with_infer(source_projection);
1082                                    ecx.eq(param_env, source_projection, target_projection)?;
1083                                    ecx.try_evaluate_added_goals()
1084                                },
1085                            )
1086                        })
1087                        .is_ok()
1088            };
1089
1090        self.probe_trait_candidate(source).enter(|ecx| {
1091            for bound in b_data.iter() {
1092                match bound.skip_binder() {
1093                    // Check that a's supertrait (upcast_principal) is compatible
1094                    // with the target (b_ty).
1095                    ty::ExistentialPredicate::Trait(target_principal) => {
1096                        let source_principal = upcast_principal.unwrap();
1097                        let target_principal = bound.rebind(target_principal);
1098                        ecx.enter_forall_with_assumptions(
1099                            target_principal,
1100                            param_env,
1101                            |ecx, target_principal| {
1102                                let source_principal =
1103                                    ecx.instantiate_binder_with_infer(source_principal);
1104                                ecx.eq(param_env, source_principal, target_principal)?;
1105                                ecx.try_evaluate_added_goals()
1106                            },
1107                        )?;
1108                    }
1109                    // Check that b_ty's projection is satisfied by exactly one of
1110                    // a_ty's projections. First, we look through the list to see if
1111                    // any match. If not, error. Then, if *more* than one matches, we
1112                    // return ambiguity. Otherwise, if exactly one matches, equate
1113                    // it with b_ty's projection.
1114                    ty::ExistentialPredicate::Projection(target_projection) => {
1115                        let target_projection = bound.rebind(target_projection);
1116                        let mut matching_projections =
1117                            a_data.projection_bounds().into_iter().filter(|source_projection| {
1118                                projection_may_match(ecx, *source_projection, target_projection)
1119                            });
1120                        let Some(source_projection) = matching_projections.next() else {
1121                            return Err(NoSolution.into());
1122                        };
1123                        if matching_projections.next().is_some() {
1124                            return ecx.evaluate_added_goals_and_make_canonical_response(
1125                                Certainty::AMBIGUOUS,
1126                            );
1127                        }
1128                        ecx.enter_forall_with_assumptions(
1129                            target_projection,
1130                            param_env,
1131                            |ecx, target_projection| {
1132                                let source_projection =
1133                                    ecx.instantiate_binder_with_infer(source_projection);
1134                                ecx.eq(param_env, source_projection, target_projection)?;
1135                                ecx.try_evaluate_added_goals()
1136                            },
1137                        )?;
1138                    }
1139                    // Check that b_ty's auto traits are present in a_ty's bounds.
1140                    ty::ExistentialPredicate::AutoTrait(def_id) => {
1141                        if !a_auto_traits.contains(&def_id) {
1142                            return Err(NoSolution.into());
1143                        }
1144                    }
1145                }
1146            }
1147
1148            // Also require that a_ty's lifetime outlives b_ty's lifetime.
1149            ecx.add_goal(
1150                GoalSource::ImplWhereBound,
1151                Goal::new(ecx.cx(), param_env, ty::OutlivesPredicate(a_region, b_region)),
1152            )?;
1153
1154            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1155        })
1156    }
1157
1158    /// We have the following builtin impls for arrays:
1159    /// ```ignore (builtin impl example)
1160    /// impl<T: ?Sized, const N: usize> Unsize<[T]> for [T; N] {}
1161    /// ```
1162    /// While the impl itself could theoretically not be builtin,
1163    /// the actual unsizing behavior is builtin. Its also easier to
1164    /// make all impls of `Unsize` builtin as we're able to use
1165    /// `#[rustc_deny_explicit_impl]` in this case.
1166    fn consider_builtin_array_unsize(
1167        &mut self,
1168        goal: Goal<I, (I::Ty, I::Ty)>,
1169        a_elem_ty: I::Ty,
1170        b_elem_ty: I::Ty,
1171    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1172        self.eq(goal.param_env, a_elem_ty, b_elem_ty)?;
1173        self.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
1174            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
1175    }
1176
1177    /// We generate a builtin `Unsize` impls for structs with generic parameters only
1178    /// mentioned by the last field.
1179    /// ```ignore (builtin impl example)
1180    /// struct Foo<T, U: ?Sized> {
1181    ///     sized_field: Vec<T>,
1182    ///     unsizable: Box<U>,
1183    /// }
1184    /// // results in the following builtin impl
1185    /// impl<T: ?Sized, U: ?Sized, V: ?Sized> Unsize<Foo<T, V>> for Foo<T, U>
1186    /// where
1187    ///     Box<U>: Unsize<Box<V>>,
1188    /// {}
1189    /// ```
1190    fn consider_builtin_struct_unsize(
1191        &mut self,
1192        goal: Goal<I, (I::Ty, I::Ty)>,
1193        def: I::AdtDef,
1194        a_args: I::GenericArgs,
1195        b_args: I::GenericArgs,
1196    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1197        let cx = self.cx();
1198        let Goal { predicate: (_a_ty, b_ty), .. } = goal;
1199
1200        let unsizing_params = cx.unsizing_params_for_adt(def.def_id());
1201        // We must be unsizing some type parameters. This also implies
1202        // that the struct has a tail field.
1203        if unsizing_params.is_empty() {
1204            return Err(NoSolution.into());
1205        }
1206
1207        let tail_field_ty = def.struct_tail_ty(cx).unwrap();
1208
1209        let a_tail_ty = tail_field_ty.instantiate(cx, a_args).skip_norm_wip();
1210        let b_tail_ty = tail_field_ty.instantiate(cx, b_args).skip_norm_wip();
1211
1212        // Instantiate just the unsizing params from B into A. The type after
1213        // this instantiation must be equal to B. This is so we don't unsize
1214        // unrelated type parameters.
1215        let new_a_args = cx.mk_args_from_iter(a_args.iter().enumerate().map(|(i, a)| {
1216            if unsizing_params.contains(i as u32) { b_args.get(i).unwrap() } else { a }
1217        }));
1218        let unsized_a_ty = Ty::new_adt(cx, def, new_a_args);
1219
1220        // Finally, we require that `TailA: Unsize<TailB>` for the tail field
1221        // types.
1222        self.eq(goal.param_env, unsized_a_ty, b_ty)?;
1223        self.add_goal(
1224            GoalSource::ImplWhereBound,
1225            goal.with(
1226                cx,
1227                ty::TraitRef::new(
1228                    cx,
1229                    cx.require_trait_lang_item(SolverTraitLangItem::Unsize),
1230                    [a_tail_ty, b_tail_ty],
1231                ),
1232            ),
1233        )?;
1234        self.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
1235            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
1236    }
1237
1238    // Return `Some` if there is an impl (built-in or user provided) that may
1239    // hold for the self type of the goal, which for coherence and soundness
1240    // purposes must disqualify the built-in auto impl assembled by considering
1241    // the type's constituent types.
1242    fn disqualify_auto_trait_candidate_due_to_possible_impl(
1243        &mut self,
1244        goal: Goal<I, TraitPredicate<I>>,
1245    ) -> Option<Result<Candidate<I>, NoSolutionOrRerunNonErased>> {
1246        let self_ty = goal.predicate.self_ty();
1247        let check_impls = || {
1248            let mut disqualifying_impl = None;
1249            self.cx().for_each_relevant_impl(goal.predicate.trait_ref, |impl_def_id| {
1250                disqualifying_impl = Some(impl_def_id);
1251            });
1252            if let Some(def_id) = disqualifying_impl {
1253                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/trait_goals.rs:1253",
                        "rustc_next_trait_solver::solve::trait_goals",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/trait_goals.rs"),
                        ::tracing_core::__macro_support::Option::Some(1253u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::trait_goals"),
                        ::tracing_core::field::FieldSet::new(&["message", "def_id",
                                        "goal"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("disqualified auto-trait implementation")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&def_id) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&goal) as
                                            &dyn Value))])
            });
    } else { ; }
};trace!(?def_id, ?goal, "disqualified auto-trait implementation");
1254                // No need to actually consider the candidate here,
1255                // since we do that in `consider_impl_candidate`.
1256                return Some(Err(NoSolution.into()));
1257            } else {
1258                None
1259            }
1260        };
1261
1262        match self_ty.kind() {
1263            // Stall int and float vars until they are resolved to a concrete
1264            // numerical type. That's because the check for impls below treats
1265            // int vars as matching any impl. Even if we filtered such impls,
1266            // we probably don't want to treat an `impl !AutoTrait for i32` as
1267            // disqualifying the built-in auto impl for `i64: AutoTrait` either.
1268            ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
1269                Some(self.forced_ambiguity(MaybeInfo::AMBIGUOUS))
1270            }
1271
1272            // Backward compatibility for default auto traits.
1273            // Test: ui/traits/default_auto_traits/extern-types.rs
1274            ty::Foreign(..) if self.cx().is_default_trait(goal.predicate.def_id()) => check_impls(),
1275
1276            // These types cannot be structurally decomposed into constituent
1277            // types, and therefore have no built-in auto impl.
1278            ty::Dynamic(..)
1279            | ty::Param(..)
1280            | ty::Foreign(..)
1281            | ty::Alias(
1282                ty::IsRigid::Yes,
1283                ty::AliasTy {
1284                    kind: ty::Projection { .. } | ty::Free { .. } | ty::Inherent { .. },
1285                    ..
1286                },
1287            )
1288            | ty::Placeholder(..) => Some(Err(NoSolution.into())),
1289
1290            // Coroutines have one special built-in candidate, `Unpin`, which
1291            // takes precedence over the structural auto trait candidate being
1292            // assembled.
1293            ty::Coroutine(def_id, _)
1294                if self
1295                    .cx()
1296                    .is_trait_lang_item(goal.predicate.def_id(), SolverTraitLangItem::Unpin) =>
1297            {
1298                match self.cx().coroutine_movability(def_id) {
1299                    Movability::Static => Some(Err(NoSolution.into())),
1300                    Movability::Movable => Some(
1301                        self.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
1302                            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1303                        }),
1304                    ),
1305                }
1306            }
1307
1308            // If we still have an alias here, it must be rigid. For opaques, it's always
1309            // okay to consider auto traits because that'll reveal its hidden type. For
1310            // non-opaque aliases, we will not assemble any candidates since there's no way
1311            // to further look into its type.
1312            ty::Alias(ty::IsRigid::Yes, ty::AliasTy { kind: ty::Opaque { .. }, .. }) => None,
1313
1314            // For rigid types, any possible implementation that could apply to
1315            // the type (even if after unification and processing nested goals
1316            // it does not hold) will disqualify the built-in auto impl.
1317            //
1318            // We've originally had a more permissive check here which resulted
1319            // in unsoundness, see #84857.
1320            ty::Bool
1321            | ty::Char
1322            | ty::Int(_)
1323            | ty::Uint(_)
1324            | ty::Float(_)
1325            | ty::Str
1326            | ty::Array(_, _)
1327            | ty::Pat(_, _)
1328            | ty::Slice(_)
1329            | ty::RawPtr(_, _)
1330            | ty::Ref(_, _, _)
1331            | ty::FnDef(_, _)
1332            | ty::FnPtr(..)
1333            | ty::Closure(..)
1334            | ty::CoroutineClosure(..)
1335            | ty::Coroutine(_, _)
1336            | ty::CoroutineWitness(..)
1337            | ty::Never
1338            | ty::Tuple(_)
1339            | ty::Adt(_, _)
1340            | ty::UnsafeBinder(_) => check_impls(),
1341            ty::Error(_) => None,
1342
1343            ty::Infer(_) | ty::Alias(ty::IsRigid::No, _) | ty::Bound(_, _) => {
1344                {
    ::core::panicking::panic_fmt(format_args!("unexpected type `{0:?}`",
            self_ty));
}panic!("unexpected type `{self_ty:?}`")
1345            }
1346        }
1347    }
1348
1349    /// Convenience function for traits that are structural, i.e. that only
1350    /// have nested subgoals that only change the self type. Unlike other
1351    /// evaluate-like helpers, this does a probe, so it doesn't need to be
1352    /// wrapped in one.
1353    fn probe_and_evaluate_goal_for_constituent_tys(
1354        &mut self,
1355        source: CandidateSource<I>,
1356        goal: Goal<I, TraitPredicate<I>>,
1357        constituent_tys: impl Fn(
1358            &EvalCtxt<'_, D>,
1359            I::Ty,
1360        ) -> Result<ty::Binder<I, Vec<I::Ty>>, NoSolution>,
1361    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1362        self.probe_trait_candidate(source).enter(|ecx| {
1363            let goals = ecx.enter_forall_with_assumptions(
1364                constituent_tys(ecx, goal.predicate.self_ty())?,
1365                goal.param_env,
1366                |ecx, tys| {
1367                    tys.into_iter()
1368                        .map(|ty| {
1369                            goal.with(ecx.cx(), goal.predicate.with_replaced_self_ty(ecx.cx(), ty))
1370                        })
1371                        .collect::<Vec<_>>()
1372                },
1373            );
1374            ecx.add_goals(GoalSource::ImplWhereBound, goals)?;
1375            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1376        })
1377    }
1378}
1379
1380/// How we've proven this trait goal.
1381///
1382/// This is used by `NormalizesTo` goals to only normalize
1383/// by using the same 'kind of candidate' we've used to prove
1384/// its corresponding trait goal. Most notably, we do not
1385/// normalize by using an impl if the trait goal has been
1386/// proven via a `ParamEnv` candidate.
1387///
1388/// This is necessary to avoid unnecessary region constraints,
1389/// see trait-system-refactor-initiative#125 for more details.
1390#[derive(#[automatically_derived]
impl ::core::fmt::Debug for TraitGoalProvenVia {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                TraitGoalProvenVia::Misc => "Misc",
                TraitGoalProvenVia::ParamEnv => "ParamEnv",
                TraitGoalProvenVia::AliasBound => "AliasBound",
            })
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for TraitGoalProvenVia {
    #[inline]
    fn clone(&self) -> TraitGoalProvenVia { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TraitGoalProvenVia { }Copy)]
1391pub(super) enum TraitGoalProvenVia {
1392    /// We've proven the trait goal by something which is
1393    /// is not a non-global where-bound or an alias-bound.
1394    ///
1395    /// This means we don't disable any candidates during
1396    /// normalization.
1397    Misc,
1398    ParamEnv,
1399    AliasBound,
1400}
1401
1402impl<D, I> EvalCtxt<'_, D>
1403where
1404    D: SolverDelegate<Interner = I>,
1405    I: Interner,
1406{
1407    /// FIXME(#57893): For backwards compatibility with the old trait solver implementation,
1408    /// we need to handle overlap between builtin and user-written impls for trait objects.
1409    ///
1410    /// This overlap is unsound in general and something which we intend to fix separately.
1411    /// To avoid blocking the stabilization of the trait solver, we add this hack to avoid
1412    /// breakage in cases which are *mostly fine*™. Importantly, this preference is strictly
1413    /// weaker than the old behavior.
1414    ///
1415    /// We only prefer builtin over user-written impls if there are no inference constraints.
1416    /// Importantly, we also only prefer the builtin impls for trait goals, and not during
1417    /// normalization. This means the only case where this special-case results in exploitable
1418    /// unsoundness should be lifetime dependent user-written impls.
1419    pub(super) fn unsound_prefer_builtin_dyn_impl(&mut self, candidates: &mut Vec<Candidate<I>>) {
1420        if self.typing_mode().is_coherence() {
1421            return;
1422        }
1423
1424        if candidates
1425            .iter()
1426            .find(|c| {
1427                #[allow(non_exhaustive_omitted_patterns)] match c.source {
    CandidateSource::BuiltinImpl(BuiltinImplSource::Object(_)) => true,
    _ => false,
}matches!(c.source, CandidateSource::BuiltinImpl(BuiltinImplSource::Object(_)))
1428            })
1429            .is_some_and(|c| has_only_region_constraints(c.result))
1430        {
1431            candidates.retain(|c| {
1432                if #[allow(non_exhaustive_omitted_patterns)] match c.source {
    CandidateSource::Impl(_) => true,
    _ => false,
}matches!(c.source, CandidateSource::Impl(_)) {
1433                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/trait_goals.rs:1433",
                        "rustc_next_trait_solver::solve::trait_goals",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/trait_goals.rs"),
                        ::tracing_core::__macro_support::Option::Some(1433u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::trait_goals"),
                        ::tracing_core::field::FieldSet::new(&["message", "c"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("unsoundly dropping impl in favor of builtin dyn-candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&c) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?c, "unsoundly dropping impl in favor of builtin dyn-candidate");
1434                    false
1435                } else {
1436                    true
1437                }
1438            });
1439        }
1440    }
1441
1442    x;#[instrument(level = "debug", skip(self), ret)]
1443    pub(super) fn merge_trait_candidates(
1444        &mut self,
1445        candidate_preference_mode: CandidatePreferenceMode,
1446        mut candidates: Vec<Candidate<I>>,
1447        failed_candidate_info: FailedCandidateInfo,
1448    ) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
1449        if self.typing_mode().is_coherence() {
1450            return if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1451                Ok((response, Some(TraitGoalProvenVia::Misc)))
1452            } else {
1453                self.flounder(&candidates).map(|r| (r, None))
1454            };
1455        }
1456
1457        // We prefer trivial builtin candidates, i.e. builtin impls without any
1458        // nested requirements, over all others. This is a fix for #53123 and
1459        // prevents where-bounds from accidentally extending the lifetime of a
1460        // variable.
1461        let mut trivial_builtin_impls = candidates.iter().filter(|c| {
1462            matches!(c.source, CandidateSource::BuiltinImpl(BuiltinImplSource::Trivial))
1463        });
1464        if let Some(candidate) = trivial_builtin_impls.next() {
1465            // There should only ever be a single trivial builtin candidate
1466            // as they would otherwise overlap.
1467            assert!(trivial_builtin_impls.next().is_none());
1468            return Ok((candidate.result, Some(TraitGoalProvenVia::Misc)));
1469        }
1470
1471        // Extract non-nested alias bound candidates, will be preferred over where bounds if
1472        // we're proving an auto-trait, sizedness trait or default trait.
1473        if matches!(candidate_preference_mode, CandidatePreferenceMode::Marker)
1474            && candidates.iter().any(|c| {
1475                matches!(c.source, CandidateSource::AliasBound(AliasBoundKind::SelfBounds))
1476            })
1477        {
1478            let alias_bounds: Vec<_> = candidates
1479                .extract_if(.., |c| matches!(c.source, CandidateSource::AliasBound(..)))
1480                .collect();
1481            return if let Some((response, _)) = self.try_merge_candidates(&alias_bounds) {
1482                Ok((response, Some(TraitGoalProvenVia::AliasBound)))
1483            } else {
1484                Ok((self.bail_with_ambiguity(&alias_bounds), None))
1485            };
1486        }
1487
1488        // If there are non-global where-bounds, prefer where-bounds
1489        // (including global ones) over everything else.
1490        let has_non_global_where_bounds = candidates
1491            .iter()
1492            .any(|c| matches!(c.source, CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)));
1493        if has_non_global_where_bounds {
1494            let where_bounds: Vec<_> = candidates
1495                .extract_if(.., |c| matches!(c.source, CandidateSource::ParamEnv(_)))
1496                .collect();
1497            let Some((response, info)) = self.try_merge_candidates(&where_bounds) else {
1498                return Ok((self.bail_with_ambiguity(&where_bounds), None));
1499            };
1500            match info {
1501                // If there's an always applicable candidate, the result of all
1502                // other candidates does not matter. This means we can ignore
1503                // them when checking whether we've reached a fixpoint.
1504                //
1505                // We always prefer the first always applicable candidate, even if a
1506                // later candidate is also always applicable and would result in fewer
1507                // reruns. We could slightly improve this by e.g. searching for another
1508                // always applicable candidate which doesn't depend on any cycle heads.
1509                //
1510                // NOTE: This is optimization is observable in case there is an always
1511                // applicable global candidate and another non-global candidate which only
1512                // applies because of a provisional result. I can't even think of a test
1513                // case where this would occur and even then, this would not be unsound.
1514                // Supporting this makes the code more involved, so I am just going to
1515                // ignore this for now.
1516                MergeCandidateInfo::AlwaysApplicable(i) => {
1517                    for (j, c) in where_bounds.into_iter().enumerate() {
1518                        if i != j {
1519                            self.ignore_candidate_head_usages(c.head_usages)
1520                        }
1521                    }
1522                    // If a where-bound does not apply, we don't actually get a
1523                    // candidate for it. We manually track the head usages
1524                    // of all failed `ParamEnv` candidates instead.
1525                    self.ignore_candidate_head_usages(failed_candidate_info.param_env_head_usages);
1526                }
1527                MergeCandidateInfo::EqualResponse => {}
1528            }
1529            return Ok((response, Some(TraitGoalProvenVia::ParamEnv)));
1530        }
1531
1532        // Next, prefer any alias bound (nested or otherwise).
1533        if candidates.iter().any(|c| matches!(c.source, CandidateSource::AliasBound(_))) {
1534            let alias_bounds: Vec<_> = candidates
1535                .extract_if(.., |c| matches!(c.source, CandidateSource::AliasBound(_)))
1536                .collect();
1537            return if let Some((response, _)) = self.try_merge_candidates(&alias_bounds) {
1538                Ok((response, Some(TraitGoalProvenVia::AliasBound)))
1539            } else {
1540                Ok((self.bail_with_ambiguity(&alias_bounds), None))
1541            };
1542        }
1543
1544        self.filter_specialized_impls(AllowInferenceConstraints::No, &mut candidates);
1545        self.unsound_prefer_builtin_dyn_impl(&mut candidates);
1546
1547        // If there are *only* global where bounds, then make sure to return that this
1548        // is still reported as being proven-via the param-env so that rigid projections
1549        // operate correctly. Otherwise, drop all global where-bounds before merging the
1550        // remaining candidates.
1551        let proven_via = if candidates
1552            .iter()
1553            .all(|c| matches!(c.source, CandidateSource::ParamEnv(ParamEnvSource::Global)))
1554        {
1555            TraitGoalProvenVia::ParamEnv
1556        } else {
1557            candidates
1558                .retain(|c| !matches!(c.source, CandidateSource::ParamEnv(ParamEnvSource::Global)));
1559            TraitGoalProvenVia::Misc
1560        };
1561
1562        if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1563            Ok((response, Some(proven_via)))
1564        } else {
1565            self.flounder(&candidates).map(|r| (r, None))
1566        }
1567    }
1568
1569    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("compute_trait_goal",
                                    "rustc_next_trait_solver::solve::trait_goals",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/trait_goals.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1569u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::trait_goals"),
                                    ::tracing_core::field::FieldSet::new(&["goal"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>),
                    NoSolutionOrRerunNonErased> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (candidates, failed_candidate_info) =
                self.assemble_and_evaluate_candidates(goal,
                        AssembleCandidatesFrom::All)?;
            let candidate_preference_mode =
                CandidatePreferenceMode::compute(self.cx(),
                    goal.predicate.def_id());
            self.merge_trait_candidates(candidate_preference_mode, candidates,
                    failed_candidate_info).map_err(Into::into)
        }
    }
}#[instrument(level = "trace", skip(self))]
1570    pub(super) fn compute_trait_goal(
1571        &mut self,
1572        goal: Goal<I, TraitPredicate<I>>,
1573    ) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolutionOrRerunNonErased>
1574    {
1575        let (candidates, failed_candidate_info) =
1576            self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All)?;
1577        let candidate_preference_mode =
1578            CandidatePreferenceMode::compute(self.cx(), goal.predicate.def_id());
1579        self.merge_trait_candidates(candidate_preference_mode, candidates, failed_candidate_info)
1580            .map_err(Into::into)
1581    }
1582
1583    fn try_stall_coroutine(
1584        &mut self,
1585        self_ty: I::Ty,
1586    ) -> Option<Result<Candidate<I>, NoSolutionOrRerunNonErased>> {
1587        if let ty::Coroutine(def_id, _) = self_ty.kind() {
1588            match self.typing_mode() {
1589                TypingMode::Typeck { defining_opaque_types_and_generators: stalled_generators } => {
1590                    if def_id.as_local().is_some_and(|def_id| stalled_generators.contains(&def_id))
1591                    {
1592                        return Some(self.forced_ambiguity(MaybeInfo {
1593                            cause: MaybeCause::Ambiguity,
1594                            opaque_types_jank: OpaqueTypesJank::AllGood,
1595                            stalled_on_coroutines: StalledOnCoroutines::Yes,
1596                        }));
1597                    }
1598                }
1599                TypingMode::ErasedNotCoherence(MayBeErased) => {
1600                    // Trying to continue here isn't worth it.
1601                    return Some(
1602                        match self.opaque_accesses.rerun_always(RerunReason::TryStallCoroutine) {
1603                            Err(e) => Err(e.into()),
1604                        },
1605                    );
1606                }
1607                TypingMode::Coherence
1608                | TypingMode::PostAnalysis
1609                | TypingMode::Codegen
1610                | TypingMode::PostTypeckUntilBorrowck { defining_opaque_types: _ }
1611                | TypingMode::PostBorrowck { defined_opaque_types: _ } => {}
1612            }
1613        }
1614
1615        None
1616    }
1617}