Skip to main content

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