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