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                let assemble_impls = match self.typing_mode() {
470                    TypingMode::Coherence => true,
471                    TypingMode::Analysis { .. }
472                    | TypingMode::Borrowck { .. }
473                    | TypingMode::PostBorrowckAnalysis { .. }
474                    | TypingMode::PostAnalysis => !candidates.iter().any(|c| {
475                        #[allow(non_exhaustive_omitted_patterns)] match c.source {
    CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) |
        CandidateSource::AliasBound(_) => true,
    _ => false,
}matches!(
476                            c.source,
477                            CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
478                                | CandidateSource::AliasBound(_)
479                        ) && has_no_inference_or_external_constraints(c.result)
480                    }),
481                };
482                if assemble_impls {
483                    self.assemble_impl_candidates(goal, &mut candidates);
484                    self.assemble_object_bound_candidates(goal, &mut candidates);
485                }
486            }
487            AssembleCandidatesFrom::EnvAndBounds => {
488                // This is somewhat inconsistent and may make #57893 slightly easier to exploit.
489                // However, it matches the behavior of the old solver. See
490                // `tests/ui/traits/next-solver/normalization-shadowing/use_object_if_empty_env.rs`.
491                if #[allow(non_exhaustive_omitted_patterns)] match normalized_self_ty.kind() {
    ty::Dynamic(..) => true,
    _ => false,
}matches!(normalized_self_ty.kind(), ty::Dynamic(..))
492                    && !candidates.iter().any(|c| #[allow(non_exhaustive_omitted_patterns)] match c.source {
    CandidateSource::ParamEnv(_) => true,
    _ => false,
}matches!(c.source, CandidateSource::ParamEnv(_)))
493                {
494                    self.assemble_object_bound_candidates(goal, &mut candidates);
495                }
496            }
497        }
498
499        (candidates, failed_candidate_info)
500    }
501
502    pub(super) fn forced_ambiguity(
503        &mut self,
504        cause: MaybeCause,
505    ) -> Result<Candidate<I>, NoSolution> {
506        // This may fail if `try_evaluate_added_goals` overflows because it
507        // fails to reach a fixpoint but ends up getting an error after
508        // running for some additional step.
509        //
510        // FIXME(@lcnr): While I believe an error here to be possible, we
511        // currently don't have any test which actually triggers it. @lqd
512        // created a minimization for an ICE in typenum, but that one no
513        // longer fails here. cc trait-system-refactor-initiative#105.
514        let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
515        let certainty = Certainty::Maybe { cause, opaque_types_jank: OpaqueTypesJank::AllGood };
516        self.probe_trait_candidate(source)
517            .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
518    }
519
520    #[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(520u32),
                                    ::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)]
521    fn assemble_impl_candidates<G: GoalKind<D>>(
522        &mut self,
523        goal: Goal<I, G>,
524        candidates: &mut Vec<Candidate<I>>,
525    ) {
526        let cx = self.cx();
527        cx.for_each_relevant_impl(
528            goal.predicate.trait_def_id(cx),
529            goal.predicate.self_ty(),
530            |impl_def_id| {
531                // For every `default impl`, there's always a non-default `impl`
532                // that will *also* apply. There's no reason to register a candidate
533                // for this impl, since it is *not* proof that the trait goal holds.
534                if cx.impl_is_default(impl_def_id) {
535                    return;
536                }
537                match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
538                    ecx.evaluate_added_goals_and_make_canonical_response(certainty)
539                }) {
540                    Ok(candidate) => candidates.push(candidate),
541                    Err(NoSolution) => (),
542                }
543            },
544        );
545    }
546
547    #[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(547u32),
                                    ::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)]
548    fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
549        &mut self,
550        goal: Goal<I, G>,
551        candidates: &mut Vec<Candidate<I>>,
552    ) {
553        let cx = self.cx();
554        let trait_def_id = goal.predicate.trait_def_id(cx);
555
556        // N.B. When assembling built-in candidates for lang items that are also
557        // `auto` traits, then the auto trait candidate that is assembled in
558        // `consider_auto_trait_candidate` MUST be disqualified to remain sound.
559        //
560        // Instead of adding the logic here, it's a better idea to add it in
561        // `EvalCtxt::disqualify_auto_trait_candidate_due_to_possible_impl` in
562        // `solve::trait_goals` instead.
563        let result = if let Err(guar) = goal.predicate.error_reported() {
564            G::consider_error_guaranteed_candidate(self, guar)
565        } else if cx.trait_is_auto(trait_def_id) {
566            G::consider_auto_trait_candidate(self, goal)
567        } else if cx.trait_is_alias(trait_def_id) {
568            G::consider_trait_alias_candidate(self, goal)
569        } else {
570            match cx.as_trait_lang_item(trait_def_id) {
571                Some(SolverTraitLangItem::Sized) => {
572                    G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
573                }
574                Some(SolverTraitLangItem::MetaSized) => {
575                    G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
576                }
577                Some(SolverTraitLangItem::PointeeSized) => {
578                    unreachable!("`PointeeSized` is removed during lowering");
579                }
580                Some(
581                    SolverTraitLangItem::Copy
582                    | SolverTraitLangItem::Clone
583                    | SolverTraitLangItem::TrivialClone,
584                ) => G::consider_builtin_copy_clone_candidate(self, goal),
585                Some(SolverTraitLangItem::Fn) => {
586                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
587                }
588                Some(SolverTraitLangItem::FnMut) => {
589                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
590                }
591                Some(SolverTraitLangItem::FnOnce) => {
592                    G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
593                }
594                Some(SolverTraitLangItem::AsyncFn) => {
595                    G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
596                }
597                Some(SolverTraitLangItem::AsyncFnMut) => {
598                    G::consider_builtin_async_fn_trait_candidates(
599                        self,
600                        goal,
601                        ty::ClosureKind::FnMut,
602                    )
603                }
604                Some(SolverTraitLangItem::AsyncFnOnce) => {
605                    G::consider_builtin_async_fn_trait_candidates(
606                        self,
607                        goal,
608                        ty::ClosureKind::FnOnce,
609                    )
610                }
611                Some(SolverTraitLangItem::FnPtrTrait) => {
612                    G::consider_builtin_fn_ptr_trait_candidate(self, goal)
613                }
614                Some(SolverTraitLangItem::AsyncFnKindHelper) => {
615                    G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
616                }
617                Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
618                Some(SolverTraitLangItem::PointeeTrait) => {
619                    G::consider_builtin_pointee_candidate(self, goal)
620                }
621                Some(SolverTraitLangItem::Future) => {
622                    G::consider_builtin_future_candidate(self, goal)
623                }
624                Some(SolverTraitLangItem::Iterator) => {
625                    G::consider_builtin_iterator_candidate(self, goal)
626                }
627                Some(SolverTraitLangItem::FusedIterator) => {
628                    G::consider_builtin_fused_iterator_candidate(self, goal)
629                }
630                Some(SolverTraitLangItem::AsyncIterator) => {
631                    G::consider_builtin_async_iterator_candidate(self, goal)
632                }
633                Some(SolverTraitLangItem::Coroutine) => {
634                    G::consider_builtin_coroutine_candidate(self, goal)
635                }
636                Some(SolverTraitLangItem::DiscriminantKind) => {
637                    G::consider_builtin_discriminant_kind_candidate(self, goal)
638                }
639                Some(SolverTraitLangItem::Destruct) => {
640                    G::consider_builtin_destruct_candidate(self, goal)
641                }
642                Some(SolverTraitLangItem::TransmuteTrait) => {
643                    G::consider_builtin_transmute_candidate(self, goal)
644                }
645                Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
646                    G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
647                }
648                Some(SolverTraitLangItem::Field) => G::consider_builtin_field_candidate(self, goal),
649                _ => Err(NoSolution),
650            }
651        };
652
653        candidates.extend(result);
654
655        // There may be multiple unsize candidates for a trait with several supertraits:
656        // `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
657        if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
658            candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
659        }
660    }
661
662    #[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(662u32),
                                    ::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)]
663    fn assemble_param_env_candidates<G: GoalKind<D>>(
664        &mut self,
665        goal: Goal<I, G>,
666        candidates: &mut Vec<Candidate<I>>,
667        failed_candidate_info: &mut FailedCandidateInfo,
668    ) {
669        for assumption in goal.param_env.caller_bounds().iter() {
670            match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
671                Ok(candidate) => candidates.push(candidate),
672                Err(head_usages) => {
673                    failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
674                }
675            }
676        }
677    }
678
679    #[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(679u32),
                                    ::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)]
680    fn assemble_alias_bound_candidates<G: GoalKind<D>>(
681        &mut self,
682        goal: Goal<I, G>,
683        candidates: &mut Vec<Candidate<I>>,
684    ) {
685        let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
686            ecx.assemble_alias_bound_candidates_recur(
687                goal.predicate.self_ty(),
688                goal,
689                candidates,
690                AliasBoundKind::SelfBounds,
691            );
692        });
693    }
694
695    /// For some deeply nested `<T>::A::B::C::D` rigid associated type,
696    /// we should explore the item bounds for all levels, since the
697    /// `associated_type_bounds` feature means that a parent associated
698    /// type may carry bounds for a nested associated type.
699    ///
700    /// If we have a projection, check that its self type is a rigid projection.
701    /// If so, continue searching by recursively calling after normalization.
702    // FIXME: This may recurse infinitely, but I can't seem to trigger it without
703    // hitting another overflow error something. Add a depth parameter needed later.
704    fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
705        &mut self,
706        self_ty: I::Ty,
707        goal: Goal<I, G>,
708        candidates: &mut Vec<Candidate<I>>,
709        consider_self_bounds: AliasBoundKind,
710    ) {
711        let alias_ty = match self_ty.kind() {
712            ty::Bool
713            | ty::Char
714            | ty::Int(_)
715            | ty::Uint(_)
716            | ty::Float(_)
717            | ty::Adt(_, _)
718            | ty::Foreign(_)
719            | ty::Str
720            | ty::Array(_, _)
721            | ty::Pat(_, _)
722            | ty::Slice(_)
723            | ty::RawPtr(_, _)
724            | ty::Ref(_, _, _)
725            | ty::FnDef(_, _)
726            | ty::FnPtr(..)
727            | ty::UnsafeBinder(_)
728            | ty::Dynamic(..)
729            | ty::Closure(..)
730            | ty::CoroutineClosure(..)
731            | ty::Coroutine(..)
732            | ty::CoroutineWitness(..)
733            | ty::Never
734            | ty::Tuple(_)
735            | ty::Param(_)
736            | ty::Placeholder(..)
737            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
738            | ty::Error(_) => return,
739            ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
740                {
    ::core::panicking::panic_fmt(format_args!("unexpected self type for `{0:?}`",
            goal));
}panic!("unexpected self type for `{goal:?}`")
741            }
742
743            ty::Infer(ty::TyVar(_)) => {
744                // If we hit infer when normalizing the self type of an alias,
745                // then bail with ambiguity. We should never encounter this on
746                // the *first* iteration of this recursive function.
747                if let Ok(result) =
748                    self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
749                {
750                    candidates.push(Candidate {
751                        source: CandidateSource::AliasBound(consider_self_bounds),
752                        result,
753                        head_usages: CandidateHeadUsages::default(),
754                    });
755                }
756                return;
757            }
758
759            ty::Alias(
760                alias_ty @ AliasTy { kind: ty::Projection { .. } | ty::Opaque { .. }, .. },
761            ) => alias_ty,
762            ty::Alias(AliasTy { kind: ty::Inherent { .. } | ty::Free { .. }, .. }) => {
763                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"));
764                return;
765            }
766        };
767
768        match consider_self_bounds {
769            AliasBoundKind::SelfBounds => {
770                for assumption in self
771                    .cx()
772                    .item_self_bounds(alias_ty.kind.def_id())
773                    .iter_instantiated(self.cx(), alias_ty.args)
774                {
775                    candidates.extend(G::probe_and_consider_implied_clause(
776                        self,
777                        CandidateSource::AliasBound(consider_self_bounds),
778                        goal,
779                        assumption,
780                        [],
781                    ));
782                }
783            }
784            AliasBoundKind::NonSelfBounds => {
785                for assumption in self
786                    .cx()
787                    .item_non_self_bounds(alias_ty.kind.def_id())
788                    .iter_instantiated(self.cx(), alias_ty.args)
789                {
790                    candidates.extend(G::probe_and_consider_implied_clause(
791                        self,
792                        CandidateSource::AliasBound(consider_self_bounds),
793                        goal,
794                        assumption,
795                        [],
796                    ));
797                }
798            }
799        }
800
801        candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
802
803        if !#[allow(non_exhaustive_omitted_patterns)] match alias_ty.kind {
    ty::Projection { .. } => true,
    _ => false,
}matches!(alias_ty.kind, ty::Projection { .. }) {
804            return;
805        }
806
807        // Recurse on the self type of the projection.
808        match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
809            Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
810                next_self_ty,
811                goal,
812                candidates,
813                AliasBoundKind::NonSelfBounds,
814            ),
815            Err(NoSolution) => {}
816        }
817    }
818
819    #[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(819u32),
                                    ::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)]
820    fn assemble_object_bound_candidates<G: GoalKind<D>>(
821        &mut self,
822        goal: Goal<I, G>,
823        candidates: &mut Vec<Candidate<I>>,
824    ) {
825        let cx = self.cx();
826        if cx.is_sizedness_trait(goal.predicate.trait_def_id(cx)) {
827            // `dyn MetaSized` is valid, but should get its `MetaSized` impl from
828            // being `dyn` (SizedCandidate), not from the object candidate.
829            return;
830        }
831
832        let self_ty = goal.predicate.self_ty();
833        let bounds = match self_ty.kind() {
834            ty::Bool
835            | ty::Char
836            | ty::Int(_)
837            | ty::Uint(_)
838            | ty::Float(_)
839            | ty::Adt(_, _)
840            | ty::Foreign(_)
841            | ty::Str
842            | ty::Array(_, _)
843            | ty::Pat(_, _)
844            | ty::Slice(_)
845            | ty::RawPtr(_, _)
846            | ty::Ref(_, _, _)
847            | ty::FnDef(_, _)
848            | ty::FnPtr(..)
849            | ty::UnsafeBinder(_)
850            | ty::Alias(..)
851            | ty::Closure(..)
852            | ty::CoroutineClosure(..)
853            | ty::Coroutine(..)
854            | ty::CoroutineWitness(..)
855            | ty::Never
856            | ty::Tuple(_)
857            | ty::Param(_)
858            | ty::Placeholder(..)
859            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
860            | ty::Error(_) => return,
861            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
862            | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
863            ty::Dynamic(bounds, ..) => bounds,
864        };
865
866        // Do not consider built-in object impls for dyn-incompatible types.
867        if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
868            return;
869        }
870
871        // Consider all of the auto-trait and projection bounds, which don't
872        // need to be recorded as a `BuiltinImplSource::Object` since they don't
873        // really have a vtable base...
874        for bound in bounds.iter() {
875            match bound.skip_binder() {
876                ty::ExistentialPredicate::Trait(_) => {
877                    // Skip principal
878                }
879                ty::ExistentialPredicate::Projection(_)
880                | ty::ExistentialPredicate::AutoTrait(_) => {
881                    candidates.extend(G::probe_and_consider_object_bound_candidate(
882                        self,
883                        CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
884                        goal,
885                        bound.with_self_ty(cx, self_ty),
886                    ));
887                }
888            }
889        }
890
891        // FIXME: We only need to do *any* of this if we're considering a trait goal,
892        // since we don't need to look at any supertrait or anything if we are doing
893        // a projection goal.
894        if let Some(principal) = bounds.principal() {
895            let principal_trait_ref = principal.with_self_ty(cx, self_ty);
896            for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
897                candidates.extend(G::probe_and_consider_object_bound_candidate(
898                    self,
899                    CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
900                    goal,
901                    assumption.upcast(cx),
902                ));
903            }
904        }
905    }
906
907    /// In coherence we have to not only care about all impls we know about, but
908    /// also consider impls which may get added in a downstream or sibling crate
909    /// or which an upstream impl may add in a minor release.
910    ///
911    /// To do so we return a single ambiguous candidate in case such an unknown
912    /// impl could apply to the current goal.
913    #[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(913u32),
                                    ::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)]
914    fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
915        &mut self,
916        goal: Goal<I, G>,
917    ) -> Result<Candidate<I>, NoSolution> {
918        self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
919            let cx = ecx.cx();
920            let trait_ref = goal.predicate.trait_ref(cx);
921            if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
922                Err(NoSolution)
923            } else {
924                // While the trait bound itself may be unknowable, we may be able to
925                // prove that a super trait is not implemented. For this, we recursively
926                // prove the super trait bounds of the current goal.
927                //
928                // We skip the goal itself as that one would cycle.
929                let predicate: I::Predicate = trait_ref.upcast(cx);
930                ecx.add_goals(
931                    GoalSource::Misc,
932                    elaborate::elaborate(cx, [predicate])
933                        .skip(1)
934                        .map(|predicate| goal.with(cx, predicate)),
935                );
936                ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
937            }
938        })
939    }
940}
941
942pub(super) enum AllowInferenceConstraints {
943    Yes,
944    No,
945}
946
947impl<D, I> EvalCtxt<'_, D>
948where
949    D: SolverDelegate<Interner = I>,
950    I: Interner,
951{
952    /// Check whether we can ignore impl candidates due to specialization.
953    ///
954    /// This is only necessary for `feature(specialization)` and seems quite ugly.
955    pub(super) fn filter_specialized_impls(
956        &mut self,
957        allow_inference_constraints: AllowInferenceConstraints,
958        candidates: &mut Vec<Candidate<I>>,
959    ) {
960        match self.typing_mode() {
961            TypingMode::Coherence => return,
962            TypingMode::Analysis { .. }
963            | TypingMode::Borrowck { .. }
964            | TypingMode::PostBorrowckAnalysis { .. }
965            | TypingMode::PostAnalysis => {}
966        }
967
968        let mut i = 0;
969        'outer: while i < candidates.len() {
970            let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
971                i += 1;
972                continue;
973            };
974
975            for (j, c) in candidates.iter().enumerate() {
976                if i == j {
977                    continue;
978                }
979
980                let CandidateSource::Impl(other_def_id) = c.source else {
981                    continue;
982                };
983
984                // See if we can toss out `victim` based on specialization.
985                //
986                // While this requires us to know *for sure* that the `lhs` impl applies
987                // we still use modulo regions here. This is fine as specialization currently
988                // assumes that specializing impls have to be always applicable, meaning that
989                // the only allowed region constraints may be constraints also present on the default impl.
990                if #[allow(non_exhaustive_omitted_patterns)] match allow_inference_constraints {
    AllowInferenceConstraints::Yes => true,
    _ => false,
}matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
991                    || has_only_region_constraints(c.result)
992                {
993                    if self.cx().impl_specializes(other_def_id, victim_def_id) {
994                        candidates.remove(i);
995                        continue 'outer;
996                    }
997                }
998            }
999
1000            i += 1;
1001        }
1002    }
1003
1004    /// If the self type is the hidden type of an opaque, try to assemble
1005    /// candidates for it by consider its item bounds and by using blanket
1006    /// impls. This is used to incompletely guide type inference when handling
1007    /// non-defining uses in the defining scope.
1008    ///
1009    /// We otherwise just fail fail with ambiguity. Even if we're using an
1010    /// opaque type item bound or a blank impls, we still force its certainty
1011    /// to be `Maybe` so that we properly prove this goal later.
1012    ///
1013    /// See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/182>
1014    /// for why this is necessary.
1015    fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
1016        &mut self,
1017        goal: Goal<I, G>,
1018        assemble_from: AssembleCandidatesFrom,
1019        candidates: &mut Vec<Candidate<I>>,
1020    ) {
1021        let self_ty = goal.predicate.self_ty();
1022        // We only use this hack during HIR typeck.
1023        let opaque_types = match self.typing_mode() {
1024            TypingMode::Analysis { .. } => self.opaques_with_sub_unified_hidden_type(self_ty),
1025            TypingMode::Coherence
1026            | TypingMode::Borrowck { .. }
1027            | TypingMode::PostBorrowckAnalysis { .. }
1028            | TypingMode::PostAnalysis => ::alloc::vec::Vec::new()vec![],
1029        };
1030
1031        if opaque_types.is_empty() {
1032            candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
1033            return;
1034        }
1035
1036        for &alias_ty in &opaque_types {
1037            {
    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:1037",
                        "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(1037u32),
                        ::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:?}");
1038
1039            struct ReplaceOpaque<I: Interner> {
1040                cx: I,
1041                alias_ty: ty::AliasTy<I>,
1042                self_ty: I::Ty,
1043            }
1044            impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
1045                fn cx(&self) -> I {
1046                    self.cx
1047                }
1048                fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1049                    if let ty::Alias(alias_ty) = ty.kind() {
1050                        if alias_ty == self.alias_ty {
1051                            return self.self_ty;
1052                        }
1053                    }
1054                    ty.super_fold_with(self)
1055                }
1056            }
1057
1058            // We look at all item-bounds of the opaque, replacing the
1059            // opaque with the current self type before considering
1060            // them as a candidate. Imagine e've got `?x: Trait<?y>`
1061            // and `?x` has been sub-unified with the hidden type of
1062            // `impl Trait<u32>`, We take the item bound `opaque: Trait<u32>`
1063            // and replace all occurrences of `opaque` with `?x`. This results
1064            // in a `?x: Trait<u32>` alias-bound candidate.
1065            for item_bound in self
1066                .cx()
1067                .item_self_bounds(alias_ty.kind.def_id())
1068                .iter_instantiated(self.cx(), alias_ty.args)
1069            {
1070                let assumption =
1071                    item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1072                candidates.extend(G::probe_and_match_goal_against_assumption(
1073                    self,
1074                    CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
1075                    goal,
1076                    assumption,
1077                    |ecx| {
1078                        // We want to reprove this goal once we've inferred the
1079                        // hidden type, so we force the certainty to `Maybe`.
1080                        ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1081                    },
1082                ));
1083            }
1084        }
1085
1086        // If the self type is sub unified with any opaque type, we also look at blanket
1087        // impls for it.
1088        //
1089        // See tests/ui/impl-trait/non-defining-uses/use-blanket-impl.rs for an example.
1090        if assemble_from.should_assemble_impl_candidates() {
1091            let cx = self.cx();
1092            cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1093                // For every `default impl`, there's always a non-default `impl`
1094                // that will *also* apply. There's no reason to register a candidate
1095                // for this impl, since it is *not* proof that the trait goal holds.
1096                if cx.impl_is_default(impl_def_id) {
1097                    return;
1098                }
1099
1100                match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1101                    if ecx.shallow_resolve(self_ty).is_ty_var() {
1102                        // We force the certainty of impl candidates to be `Maybe`.
1103                        let certainty = certainty.and(Certainty::AMBIGUOUS);
1104                        ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1105                    } else {
1106                        // We don't want to use impls if they constrain the opaque.
1107                        //
1108                        // FIXME(trait-system-refactor-initiative#229): This isn't
1109                        // perfect yet as it still allows us to incorrectly constrain
1110                        // other inference variables.
1111                        Err(NoSolution)
1112                    }
1113                }) {
1114                    Ok(candidate) => candidates.push(candidate),
1115                    Err(NoSolution) => (),
1116                }
1117            });
1118        }
1119
1120        if candidates.is_empty() {
1121            let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
1122            let certainty = Certainty::Maybe {
1123                cause: MaybeCause::Ambiguity,
1124                opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
1125            };
1126            candidates
1127                .extend(self.probe_trait_candidate(source).enter(|this| {
1128                    this.evaluate_added_goals_and_make_canonical_response(certainty)
1129                }));
1130        }
1131    }
1132
1133    /// Assemble and merge candidates for goals which are related to an underlying trait
1134    /// goal. Right now, this is normalizes-to and host effect goals.
1135    ///
1136    /// We sadly can't simply take all possible candidates for normalization goals
1137    /// and check whether they result in the same constraints. We want to make sure
1138    /// that trying to normalize an alias doesn't result in constraints which aren't
1139    /// otherwise required.
1140    ///
1141    /// Most notably, when proving a trait goal by via a where-bound, we should not
1142    /// normalize via impls which have stricter region constraints than the where-bound:
1143    ///
1144    /// ```rust
1145    /// trait Trait<'a> {
1146    ///     type Assoc;
1147    /// }
1148    ///
1149    /// impl<'a, T: 'a> Trait<'a> for T {
1150    ///     type Assoc = u32;
1151    /// }
1152    ///
1153    /// fn with_bound<'a, T: Trait<'a>>(_value: T::Assoc) {}
1154    /// ```
1155    ///
1156    /// The where-bound of `with_bound` doesn't specify the associated type, so we would
1157    /// only be able to normalize `<T as Trait<'a>>::Assoc` by using the impl. This impl
1158    /// adds a `T: 'a` bound however, which would result in a region error. Given that the
1159    /// user explicitly wrote that `T: Trait<'a>` holds, this is undesirable and we instead
1160    /// treat the alias as rigid.
1161    ///
1162    /// See trait-system-refactor-initiative#124 for more details.
1163    x;#[instrument(level = "debug", skip_all, fields(proven_via, goal), ret)]
1164    pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1165        &mut self,
1166        proven_via: Option<TraitGoalProvenVia>,
1167        goal: Goal<I, G>,
1168        inject_forced_ambiguity_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> Option<QueryResult<I>>,
1169        inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1170    ) -> QueryResult<I> {
1171        let Some(proven_via) = proven_via else {
1172            // We don't care about overflow. If proving the trait goal overflowed, then
1173            // it's enough to report an overflow error for that, we don't also have to
1174            // overflow during normalization.
1175            //
1176            // We use `forced_ambiguity` here over `make_ambiguous_response_no_constraints`
1177            // because the former will also record a built-in candidate in the inspector.
1178            return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1179        };
1180
1181        match proven_via {
1182            TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1183                // Even when a trait bound has been proven using a where-bound, we
1184                // still need to consider alias-bounds for normalization, see
1185                // `tests/ui/next-solver/alias-bound-shadowed-by-env.rs`.
1186                let (mut candidates, _) = self
1187                    .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1188                debug!(?candidates);
1189
1190                // If the trait goal has been proven by using the environment, we want to treat
1191                // aliases as rigid if there are no applicable projection bounds in the environment.
1192                if candidates.is_empty() {
1193                    return inject_normalize_to_rigid_candidate(self);
1194                }
1195
1196                // If we're normalizing an GAT, we bail if using a where-bound would constrain
1197                // its generic arguments.
1198                if let Some(result) = inject_forced_ambiguity_candidate(self) {
1199                    return result;
1200                }
1201
1202                // We still need to prefer where-bounds over alias-bounds however.
1203                // See `tests/ui/winnowing/norm-where-bound-gt-alias-bound.rs`.
1204                if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1205                    candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1206                }
1207
1208                if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1209                    Ok(response)
1210                } else {
1211                    self.flounder(&candidates)
1212                }
1213            }
1214            TraitGoalProvenVia::Misc => {
1215                let (mut candidates, _) =
1216                    self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1217
1218                // Prefer "orphaned" param-env normalization predicates, which are used
1219                // (for example, and ideally only) when proving item bounds for an impl.
1220                if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1221                    candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1222                }
1223
1224                // We drop specialized impls to allow normalization via a final impl here. In case
1225                // the specializing impl has different inference constraints from the specialized
1226                // impl, proving the trait goal is already ambiguous, so we never get here. This
1227                // means we can just ignore inference constraints and don't have to special-case
1228                // constraining the normalized-to `term`.
1229                self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1230                if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1231                    Ok(response)
1232                } else {
1233                    self.flounder(&candidates)
1234                }
1235            }
1236        }
1237    }
1238
1239    /// Compute whether a param-env assumption is global or non-global after normalizing it.
1240    ///
1241    /// This is necessary because, for example, given:
1242    ///
1243    /// ```ignore,rust
1244    /// where
1245    ///     T: Trait<Assoc = u32>,
1246    ///     i32: From<T::Assoc>,
1247    /// ```
1248    ///
1249    /// The `i32: From<T::Assoc>` bound is non-global before normalization, but is global after.
1250    /// Since the old trait solver normalized param-envs eagerly, we want to emulate this
1251    /// behavior lazily.
1252    fn characterize_param_env_assumption(
1253        &mut self,
1254        param_env: I::ParamEnv,
1255        assumption: I::Clause,
1256    ) -> Result<(CandidateSource<I>, Certainty), NoSolution> {
1257        // FIXME: This should be fixed, but it also requires changing the behavior
1258        // in the old solver which is currently relied on.
1259        if assumption.has_bound_vars() {
1260            return Ok((CandidateSource::ParamEnv(ParamEnvSource::NonGlobal), Certainty::Yes));
1261        }
1262
1263        match assumption.visit_with(&mut FindParamInClause {
1264            ecx: self,
1265            param_env,
1266            universes: ::alloc::vec::Vec::new()vec![],
1267            recursion_depth: 0,
1268        }) {
1269            ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1270            ControlFlow::Break(Ok(certainty)) => {
1271                Ok((CandidateSource::ParamEnv(ParamEnvSource::NonGlobal), certainty))
1272            }
1273            ControlFlow::Continue(()) => {
1274                Ok((CandidateSource::ParamEnv(ParamEnvSource::Global), Certainty::Yes))
1275            }
1276        }
1277    }
1278}
1279
1280struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1281    ecx: &'a mut EvalCtxt<'b, D>,
1282    param_env: I::ParamEnv,
1283    universes: Vec<Option<ty::UniverseIndex>>,
1284    recursion_depth: usize,
1285}
1286
1287impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1288where
1289    D: SolverDelegate<Interner = I>,
1290    I: Interner,
1291{
1292    // - `Continue(())`: no generic parameter was found, the type is global
1293    // - `Break(Ok(Certainty::Yes))`: a generic parameter was found, the type is non-global
1294    // - `Break(Ok(Certainty::Maybe(_)))`: the recursion limit reached, assume that the type is non-global
1295    // - `Break(Err(NoSolution))`: normalization failed
1296    type Result = ControlFlow<Result<Certainty, NoSolution>>;
1297
1298    fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1299        self.universes.push(None);
1300        t.super_visit_with(self)?;
1301        self.universes.pop();
1302        ControlFlow::Continue(())
1303    }
1304
1305    fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1306        let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1307        let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1308            return ControlFlow::Break(Err(NoSolution));
1309        };
1310
1311        match ty.kind() {
1312            ty::Placeholder(p) => {
1313                if p.universe() == ty::UniverseIndex::ROOT {
1314                    ControlFlow::Break(Ok(Certainty::Yes))
1315                } else {
1316                    ControlFlow::Continue(())
1317                }
1318            }
1319            ty::Infer(_) => ControlFlow::Break(Ok(Certainty::AMBIGUOUS)),
1320            _ if ty.has_type_flags(
1321                TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_INFER | TypeFlags::HAS_ALIAS,
1322            ) =>
1323            {
1324                self.recursion_depth += 1;
1325                if self.recursion_depth > self.ecx.cx().recursion_limit() {
1326                    return ControlFlow::Break(Ok(Certainty::Maybe {
1327                        cause: MaybeCause::Overflow {
1328                            suggest_increasing_limit: true,
1329                            keep_constraints: false,
1330                        },
1331                        opaque_types_jank: OpaqueTypesJank::AllGood,
1332                    }));
1333                }
1334                let result = ty.super_visit_with(self);
1335                self.recursion_depth -= 1;
1336                result
1337            }
1338            _ => ControlFlow::Continue(()),
1339        }
1340    }
1341
1342    fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1343        let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1344        let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1345            return ControlFlow::Break(Err(NoSolution));
1346        };
1347
1348        match ct.kind() {
1349            ty::ConstKind::Placeholder(p) => {
1350                if p.universe() == ty::UniverseIndex::ROOT {
1351                    ControlFlow::Break(Ok(Certainty::Yes))
1352                } else {
1353                    ControlFlow::Continue(())
1354                }
1355            }
1356            ty::ConstKind::Infer(_) => ControlFlow::Break(Ok(Certainty::AMBIGUOUS)),
1357            _ if ct.has_type_flags(
1358                TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_INFER | TypeFlags::HAS_ALIAS,
1359            ) =>
1360            {
1361                // FIXME(mgca): we should also check the recursion limit here
1362                ct.super_visit_with(self)
1363            }
1364            _ => ControlFlow::Continue(()),
1365        }
1366    }
1367
1368    fn visit_region(&mut self, r: I::Region) -> Self::Result {
1369        match self.ecx.eager_resolve_region(r).kind() {
1370            ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1371            ty::RePlaceholder(p) => {
1372                if p.universe() == ty::UniverseIndex::ROOT {
1373                    ControlFlow::Break(Ok(Certainty::Yes))
1374                } else {
1375                    ControlFlow::Continue(())
1376                }
1377            }
1378            ty::ReVar(_) => ControlFlow::Break(Ok(Certainty::Yes)),
1379            ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1380                {
    ::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")
1381            }
1382        }
1383    }
1384}