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