Skip to main content

rustc_trait_selection/solve/fulfill/
derive_errors.rs

1use std::ops::ControlFlow;
2
3use rustc_hir::LangItem;
4use rustc_infer::infer::InferCtxt;
5use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause};
6use rustc_infer::traits::{
7    self, MismatchedProjectionTypes, Obligation, ObligationCause, ObligationCauseCode,
8    PredicateObligation, SelectionError,
9};
10use rustc_middle::traits::query::NoSolution;
11use rustc_middle::ty::error::{ExpectedFound, TypeError};
12use rustc_middle::ty::{self, Ty, TyCtxt};
13use rustc_middle::{bug, span_bug};
14use rustc_next_trait_solver::solve::{GoalEvaluation, MaybeInfo, SolverDelegateEvalExt as _};
15use tracing::{instrument, trace};
16
17use crate::solve::delegate::SolverDelegate;
18use crate::solve::inspect::{self, InferCtxtProofTreeExt, ProofTreeVisitor};
19use crate::solve::{Certainty, deeply_normalize_for_diagnostics};
20use crate::traits::{FulfillmentError, FulfillmentErrorCode, wf};
21
22pub(super) fn fulfillment_error_for_no_solution<'tcx>(
23    infcx: &InferCtxt<'tcx>,
24    root_obligation: PredicateObligation<'tcx>,
25) -> FulfillmentError<'tcx> {
26    let obligation = find_best_leaf_obligation(infcx, &root_obligation, false);
27
28    let code = match obligation.predicate.kind().skip_binder() {
29        ty::PredicateKind::Clause(ty::ClauseKind::Projection(_)) => {
30            FulfillmentErrorCode::Project(
31                // FIXME: This could be a `Sorts` if the term is a type
32                MismatchedProjectionTypes { err: TypeError::Mismatch },
33            )
34        }
35        ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, expected_ty)) => {
36            let ct_ty = match ct.kind() {
37                ty::ConstKind::Unevaluated(uv) => uv.type_of(infcx.tcx).skip_norm_wip(),
38                ty::ConstKind::Param(param_ct) => {
39                    param_ct.find_const_ty_from_env(obligation.param_env)
40                }
41                ty::ConstKind::Value(cv) => cv.ty,
42                kind => ::rustc_middle::util::bug::span_bug_fmt(obligation.cause.span,
    format_args!("ConstArgHasWrongType failed but we don\'t know how to compute type for {0:?}",
        kind))span_bug!(
43                    obligation.cause.span,
44                    "ConstArgHasWrongType failed but we don't know how to compute type for {kind:?}"
45                ),
46            };
47            FulfillmentErrorCode::Select(SelectionError::ConstArgHasWrongType {
48                ct,
49                ct_ty,
50                expected_ty,
51            })
52        }
53        ty::PredicateKind::NormalizesTo(..) => {
54            FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch })
55        }
56        ty::PredicateKind::AliasRelate(_, _, _) => {
57            FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch })
58        }
59        ty::PredicateKind::Subtype(pred) => {
60            let (a, b) = infcx.enter_forall_and_leak_universe(
61                obligation.predicate.kind().rebind((pred.a, pred.b)),
62            );
63            let expected_found = ExpectedFound::new(a, b);
64            FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
65        }
66        ty::PredicateKind::Coerce(pred) => {
67            let (a, b) = infcx.enter_forall_and_leak_universe(
68                obligation.predicate.kind().rebind((pred.a, pred.b)),
69            );
70            let expected_found = ExpectedFound::new(b, a);
71            FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
72        }
73        ty::PredicateKind::Clause(_)
74        | ty::PredicateKind::DynCompatible(_)
75        | ty::PredicateKind::Ambiguous => {
76            FulfillmentErrorCode::Select(SelectionError::Unimplemented)
77        }
78        ty::PredicateKind::ConstEquate(..) => {
79            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected goal: {0:?}",
        obligation))bug!("unexpected goal: {obligation:?}")
80        }
81    };
82
83    FulfillmentError { obligation, code, root_obligation }
84}
85
86pub(super) fn fulfillment_error_for_stalled<'tcx>(
87    infcx: &InferCtxt<'tcx>,
88    root_obligation: PredicateObligation<'tcx>,
89) -> FulfillmentError<'tcx> {
90    let (code, refine_obligation) = infcx.probe(|_| {
91        match <&SolverDelegate<'tcx>>::from(infcx).evaluate_root_goal(
92            root_obligation.as_goal(),
93            root_obligation.cause.span,
94            None,
95        ) {
96            Ok(GoalEvaluation {
97                certainty:
98                    Certainty::Maybe(MaybeInfo {
99                        cause: MaybeCause::Ambiguity,
100                        opaque_types_jank: _,
101                        stalled_on_coroutines: _,
102                    }),
103                ..
104            }) => (FulfillmentErrorCode::Ambiguity { overflow: None }, true),
105            Ok(GoalEvaluation {
106                certainty:
107                    Certainty::Maybe(MaybeInfo {
108                        cause:
109                            MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: _ },
110                        opaque_types_jank: _,
111                        stalled_on_coroutines: _,
112                    }),
113                ..
114            }) => (
115                FulfillmentErrorCode::Ambiguity { overflow: Some(suggest_increasing_limit) },
116                // Don't look into overflows because we treat overflows weirdly anyways.
117                // We discard the inference constraints from overflowing goals, so
118                // recomputing the goal again during `find_best_leaf_obligation` may apply
119                // inference guidance that makes other goals go from ambig -> pass, for example.
120                //
121                // FIXME: We should probably just look into overflows here.
122                false,
123            ),
124            Ok(GoalEvaluation { certainty: Certainty::Yes, .. }) => {
125                ::rustc_middle::util::bug::span_bug_fmt(root_obligation.cause.span,
    format_args!("did not expect successful goal when collecting ambiguity errors for `{0:?}`",
        infcx.resolve_vars_if_possible(root_obligation.predicate)))span_bug!(
126                    root_obligation.cause.span,
127                    "did not expect successful goal when collecting ambiguity errors for `{:?}`",
128                    infcx.resolve_vars_if_possible(root_obligation.predicate),
129                )
130            }
131            Err(_) => {
132                ::rustc_middle::util::bug::span_bug_fmt(root_obligation.cause.span,
    format_args!("did not expect selection error when collecting ambiguity errors for `{0:?}`",
        infcx.resolve_vars_if_possible(root_obligation.predicate)))span_bug!(
133                    root_obligation.cause.span,
134                    "did not expect selection error when collecting ambiguity errors for `{:?}`",
135                    infcx.resolve_vars_if_possible(root_obligation.predicate),
136                )
137            }
138        }
139    });
140
141    FulfillmentError {
142        obligation: if refine_obligation {
143            find_best_leaf_obligation(infcx, &root_obligation, true)
144        } else {
145            root_obligation.clone()
146        },
147        code,
148        root_obligation,
149    }
150}
151
152pub(super) fn fulfillment_error_for_overflow<'tcx>(
153    infcx: &InferCtxt<'tcx>,
154    root_obligation: PredicateObligation<'tcx>,
155) -> FulfillmentError<'tcx> {
156    FulfillmentError {
157        obligation: find_best_leaf_obligation(infcx, &root_obligation, true),
158        code: FulfillmentErrorCode::Ambiguity { overflow: Some(true) },
159        root_obligation,
160    }
161}
162
163x;#[instrument(level = "debug", skip(infcx), ret)]
164fn find_best_leaf_obligation<'tcx>(
165    infcx: &InferCtxt<'tcx>,
166    obligation: &PredicateObligation<'tcx>,
167    consider_ambiguities: bool,
168) -> PredicateObligation<'tcx> {
169    let obligation = infcx.resolve_vars_if_possible(obligation.clone());
170    // FIXME: we use a probe here as the `BestObligation` visitor does not
171    // check whether it uses candidates which get shadowed by where-bounds.
172    //
173    // We should probably fix the visitor to not do so instead, as this also
174    // means the leaf obligation may be incorrect.
175    let obligation = infcx
176        .fudge_inference_if_ok(|| {
177            infcx
178                .visit_proof_tree(
179                    obligation.as_goal(),
180                    &mut BestObligation { obligation: obligation.clone(), consider_ambiguities },
181                )
182                .break_value()
183                .ok_or(())
184                // walk around the fact that the cause in `Obligation` is ignored by folders so that
185                // we can properly fudge the infer vars in cause code.
186                .map(|o| (o.cause.clone(), o))
187        })
188        .map(|(cause, o)| PredicateObligation { cause, ..o })
189        .unwrap_or(obligation);
190    deeply_normalize_for_diagnostics(infcx, obligation.param_env, obligation)
191}
192
193struct BestObligation<'tcx> {
194    obligation: PredicateObligation<'tcx>,
195    consider_ambiguities: bool,
196}
197
198impl<'tcx> BestObligation<'tcx> {
199    fn with_derived_obligation(
200        &mut self,
201        derived_obligation: PredicateObligation<'tcx>,
202        and_then: impl FnOnce(&mut Self) -> <Self as ProofTreeVisitor<'tcx>>::Result,
203    ) -> <Self as ProofTreeVisitor<'tcx>>::Result {
204        let old_obligation = std::mem::replace(&mut self.obligation, derived_obligation);
205        let res = and_then(self);
206        self.obligation = old_obligation;
207        res
208    }
209
210    /// Filter out the candidates that aren't interesting to visit for the
211    /// purposes of reporting errors. For ambiguities, we only consider
212    /// candidates that may hold. For errors, we only consider candidates that
213    /// *don't* hold and which have impl-where clauses that also don't hold.
214    fn non_trivial_candidates<'a>(
215        &self,
216        goal: &'a inspect::InspectGoal<'a, 'tcx>,
217    ) -> Vec<inspect::InspectCandidate<'a, 'tcx>> {
218        let mut candidates = goal.candidates();
219        match self.consider_ambiguities {
220            true => {
221                // If we have an ambiguous obligation, we must consider *all* candidates
222                // that hold, or else we may guide inference causing other goals to go
223                // from ambig -> pass/fail.
224                candidates.retain(|candidate| candidate.result().is_ok());
225            }
226            false => {
227                // We always handle rigid alias candidates separately as we may not add them for
228                // aliases whose trait bound doesn't hold.
229                candidates.retain(|c| !#[allow(non_exhaustive_omitted_patterns)] match c.kind() {
    inspect::ProbeKind::RigidAlias { .. } => true,
    _ => false,
}matches!(c.kind(), inspect::ProbeKind::RigidAlias { .. }));
230                // If we have >1 candidate, one may still be due to "boring" reasons, like
231                // an alias-relate that failed to hold when deeply evaluated. We really
232                // don't care about reasons like this.
233                if candidates.len() > 1 {
234                    candidates.retain(|candidate| {
235                        goal.infcx().probe(|_| {
236                            candidate.instantiate_nested_goals(self.span()).iter().any(
237                                |nested_goal| {
238                                    #[allow(non_exhaustive_omitted_patterns)] match nested_goal.source() {
    GoalSource::ImplWhereBound | GoalSource::AliasBoundConstCondition |
        GoalSource::AliasWellFormed => true,
    _ => false,
}matches!(
239                                        nested_goal.source(),
240                                        GoalSource::ImplWhereBound
241                                            | GoalSource::AliasBoundConstCondition
242                                            | GoalSource::AliasWellFormed
243                                    ) && nested_goal.result().is_err()
244                                },
245                            )
246                        })
247                    });
248                }
249            }
250        }
251
252        candidates
253    }
254
255    /// HACK: We walk the nested obligations for a well-formed arg manually,
256    /// since there's nontrivial logic in `wf.rs` to set up an obligation cause.
257    /// Ideally we'd be able to track this better.
258    fn visit_well_formed_goal(
259        &mut self,
260        candidate: &inspect::InspectCandidate<'_, 'tcx>,
261        term: ty::Term<'tcx>,
262    ) -> ControlFlow<PredicateObligation<'tcx>> {
263        let infcx = candidate.goal().infcx();
264        let param_env = candidate.goal().goal().param_env;
265        let body_id = self.obligation.cause.body_id;
266
267        for obligation in wf::unnormalized_obligations(infcx, param_env, term, self.span(), body_id)
268            .into_iter()
269            .flatten()
270        {
271            let nested_goal = candidate.instantiate_proof_tree_for_nested_goal(
272                GoalSource::Misc,
273                obligation.as_goal(),
274                self.span(),
275            );
276            // Skip nested goals that aren't the *reason* for our goal's failure.
277            match (self.consider_ambiguities, nested_goal.result()) {
278                (
279                    true,
280                    Ok(Certainty::Maybe(MaybeInfo {
281                        cause: MaybeCause::Ambiguity,
282                        opaque_types_jank: _,
283                        stalled_on_coroutines: _,
284                    })),
285                )
286                | (false, Err(_)) => {}
287                _ => continue,
288            }
289
290            self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
291        }
292
293        ControlFlow::Break(self.obligation.clone())
294    }
295
296    /// If a normalization of an associated item or a trait goal fails without trying any
297    /// candidates it's likely that normalizing its self type failed. We manually detect
298    /// such cases here.
299    fn detect_error_in_self_ty_normalization(
300        &mut self,
301        goal: &inspect::InspectGoal<'_, 'tcx>,
302        self_ty: Ty<'tcx>,
303    ) -> ControlFlow<PredicateObligation<'tcx>> {
304        if !!self.consider_ambiguities {
    ::core::panicking::panic("assertion failed: !self.consider_ambiguities")
};assert!(!self.consider_ambiguities);
305        let tcx = goal.infcx().tcx;
306        if let ty::Alias(..) = self_ty.kind() {
307            let infer_term = goal.infcx().next_ty_var(self.obligation.cause.span);
308            let pred = ty::PredicateKind::AliasRelate(
309                self_ty.into(),
310                infer_term.into(),
311                ty::AliasRelationDirection::Equate,
312            );
313            let obligation =
314                Obligation::new(tcx, self.obligation.cause.clone(), goal.goal().param_env, pred);
315            self.with_derived_obligation(obligation, |this| {
316                goal.infcx().visit_proof_tree_at_depth(
317                    goal.goal().with(tcx, pred),
318                    goal.depth() + 1,
319                    this,
320                )
321            })
322        } else {
323            ControlFlow::Continue(())
324        }
325    }
326
327    /// When a higher-ranked projection goal fails, check that the corresponding
328    /// higher-ranked trait goal holds or not. This is because the process of
329    /// instantiating and then re-canonicalizing the binder of the projection goal
330    /// forces us to be unable to see that the leak check failed in the nested
331    /// `NormalizesTo` goal, so we don't fall back to the rigid projection check
332    /// that should catch when a projection goal fails due to an unsatisfied trait
333    /// goal.
334    fn detect_trait_error_in_higher_ranked_projection(
335        &mut self,
336        goal: &inspect::InspectGoal<'_, 'tcx>,
337    ) -> ControlFlow<PredicateObligation<'tcx>> {
338        let tcx = goal.infcx().tcx;
339        if let Some(projection_clause) = goal.goal().predicate.as_projection_clause()
340            && !projection_clause.bound_vars().is_empty()
341        {
342            let pred = projection_clause.map_bound(|proj| proj.projection_term.trait_ref(tcx));
343            let obligation = Obligation::new(
344                tcx,
345                self.obligation.cause.clone(),
346                goal.goal().param_env,
347                deeply_normalize_for_diagnostics(goal.infcx(), goal.goal().param_env, pred),
348            );
349            self.with_derived_obligation(obligation, |this| {
350                goal.infcx().visit_proof_tree_at_depth(
351                    goal.goal().with(tcx, pred),
352                    goal.depth() + 1,
353                    this,
354                )
355            })
356        } else {
357            ControlFlow::Continue(())
358        }
359    }
360
361    /// It is likely that `NormalizesTo` failed without any applicable candidates
362    /// because the alias is not well-formed.
363    ///
364    /// As we only enter `RigidAlias` candidates if the trait bound of the associated type
365    /// holds, we discard these candidates in `non_trivial_candidates` and always manually
366    /// check this here.
367    fn detect_non_well_formed_assoc_item(
368        &mut self,
369        goal: &inspect::InspectGoal<'_, 'tcx>,
370        alias: ty::AliasTerm<'tcx>,
371    ) -> ControlFlow<PredicateObligation<'tcx>> {
372        let tcx = goal.infcx().tcx;
373        let obligation = Obligation::new(
374            tcx,
375            self.obligation.cause.clone(),
376            goal.goal().param_env,
377            alias.trait_ref(tcx),
378        );
379        self.with_derived_obligation(obligation, |this| {
380            goal.infcx().visit_proof_tree_at_depth(
381                goal.goal().with(tcx, alias.trait_ref(tcx)),
382                goal.depth() + 1,
383                this,
384            )
385        })
386    }
387
388    /// If we have no candidates, then it's likely that there is a
389    /// non-well-formed alias in the goal.
390    fn detect_error_from_empty_candidates(
391        &mut self,
392        goal: &inspect::InspectGoal<'_, 'tcx>,
393    ) -> ControlFlow<PredicateObligation<'tcx>> {
394        let pred_kind = goal.goal().predicate.kind();
395
396        match pred_kind.no_bound_vars() {
397            Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))) => {
398                self.detect_error_in_self_ty_normalization(goal, pred.self_ty())?;
399            }
400            Some(ty::PredicateKind::NormalizesTo(pred))
401                if let ty::AliasTermKind::ProjectionTy { .. }
402                | ty::AliasTermKind::ProjectionConst { .. } = pred.alias.kind =>
403            {
404                self.detect_error_in_self_ty_normalization(goal, pred.alias.self_ty())?;
405                self.detect_non_well_formed_assoc_item(goal, pred.alias)?;
406            }
407            Some(_) | None => {}
408        }
409
410        ControlFlow::Break(self.obligation.clone())
411    }
412}
413
414impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
415    type Result = ControlFlow<PredicateObligation<'tcx>>;
416
417    fn span(&self) -> rustc_span::Span {
418        self.obligation.cause.span
419    }
420
421    #[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("visit_goal",
                                    "rustc_trait_selection::solve::fulfill::derive_errors",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(421u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::solve::fulfill::derive_errors"),
                                    ::tracing_core::field::FieldSet::new(&["goal"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&goal.goal())
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Self::Result = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = goal.infcx().tcx;
            match (self.consider_ambiguities, goal.result()) {
                (true,
                    Ok(Certainty::Maybe(MaybeInfo {
                    cause: MaybeCause::Ambiguity,
                    opaque_types_jank: _,
                    stalled_on_coroutines: _ }))) | (false, Err(_)) => {}
                _ => return ControlFlow::Continue(()),
            }
            let pred = goal.goal().predicate;
            let candidates = self.non_trivial_candidates(goal);
            let candidate =
                match candidates.as_slice() {
                    [candidate] => candidate,
                    [] => return self.detect_error_from_empty_candidates(goal),
                    _ => return ControlFlow::Break(self.obligation.clone()),
                };
            if let inspect::ProbeKind::TraitCandidate {
                        source: CandidateSource::Impl(impl_def_id), result: _ } =
                        candidate.kind() && tcx.do_not_recommend_impl(impl_def_id) {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs:454",
                                        "rustc_trait_selection::solve::fulfill::derive_errors",
                                        ::tracing::Level::TRACE,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs"),
                                        ::tracing_core::__macro_support::Option::Some(454u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::solve::fulfill::derive_errors"),
                                        ::tracing_core::field::FieldSet::new(&["message"],
                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                        ::tracing::metadata::Kind::EVENT)
                                };
                            ::tracing::callsite::DefaultCallsite::new(&META)
                        };
                    let enabled =
                        ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            {
                                let interest = __CALLSITE.interest();
                                !interest.is_never() &&
                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                        interest)
                            };
                    if enabled {
                        (|value_set: ::tracing::field::ValueSet|
                                    {
                                        let meta = __CALLSITE.metadata();
                                        ::tracing::Event::dispatch(meta, &value_set);
                                        ;
                                    })({
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = __CALLSITE.metadata().fields().iter();
                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&format_args!("#[diagnostic::do_not_recommend] -> exit")
                                                            as &dyn Value))])
                            });
                    } else { ; }
                };
                return ControlFlow::Break(self.obligation.clone());
            }
            let child_mode =
                match pred.kind().skip_binder() {
                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))
                        => {
                        ChildMode::Trait(pred.kind().rebind(trait_pred))
                    }
                    ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(host_pred))
                        => {
                        ChildMode::Host(pred.kind().rebind(host_pred))
                    }
                    ty::PredicateKind::NormalizesTo(normalizes_to) if
                        #[allow(non_exhaustive_omitted_patterns)] match normalizes_to.alias.kind
                            {
                            ty::AliasTermKind::ProjectionTy { .. } |
                                ty::AliasTermKind::ProjectionConst { .. } => true,
                            _ => false,
                        } => {
                        ChildMode::Trait(pred.kind().rebind(ty::TraitPredicate {
                                    trait_ref: normalizes_to.alias.trait_ref(tcx),
                                    polarity: ty::PredicatePolarity::Positive,
                                }))
                    }
                    ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term))
                        => {
                        return self.visit_well_formed_goal(candidate, term);
                    }
                    _ => ChildMode::PassThrough,
                };
            let nested_goals =
                candidate.instantiate_nested_goals(self.span());
            for nested_goal in &nested_goals {
                if let Some(poly_trait_pred) =
                                nested_goal.goal().predicate.as_trait_clause() &&
                            tcx.is_lang_item(poly_trait_pred.def_id(),
                                LangItem::FnPtrTrait) &&
                        let Err(NoSolution) = nested_goal.result() {
                    return ControlFlow::Break(self.obligation.clone());
                }
            }
            let mut impl_where_bound_count = 0;
            for nested_goal in nested_goals {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs:505",
                                        "rustc_trait_selection::solve::fulfill::derive_errors",
                                        ::tracing::Level::TRACE,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs"),
                                        ::tracing_core::__macro_support::Option::Some(505u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::solve::fulfill::derive_errors"),
                                        ::tracing_core::field::FieldSet::new(&["nested_goal"],
                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                        ::tracing::metadata::Kind::EVENT)
                                };
                            ::tracing::callsite::DefaultCallsite::new(&META)
                        };
                    let enabled =
                        ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            {
                                let interest = __CALLSITE.interest();
                                !interest.is_never() &&
                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                        interest)
                            };
                    if enabled {
                        (|value_set: ::tracing::field::ValueSet|
                                    {
                                        let meta = __CALLSITE.metadata();
                                        ::tracing::Event::dispatch(meta, &value_set);
                                        ;
                                    })({
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = __CALLSITE.metadata().fields().iter();
                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&(nested_goal.goal(),
                                                                            nested_goal.source(), nested_goal.result())) as
                                                            &dyn Value))])
                            });
                    } else { ; }
                };
                let nested_pred = nested_goal.goal().predicate;
                let make_obligation =
                    |cause|
                        Obligation {
                            cause,
                            param_env: nested_goal.goal().param_env,
                            predicate: nested_pred,
                            recursion_depth: self.obligation.recursion_depth + 1,
                        };
                let obligation;
                match (child_mode, nested_goal.source()) {
                    (ChildMode::Trait(_) | ChildMode::Host(_),
                        GoalSource::Misc | GoalSource::TypeRelating |
                        GoalSource::NormalizeGoal(_)) => {
                        continue;
                    }
                    (ChildMode::Trait(parent_trait_pred),
                        GoalSource::ImplWhereBound) => {
                        obligation =
                            make_obligation(derive_cause(tcx, candidate.kind(),
                                    self.obligation.cause.clone(), impl_where_bound_count,
                                    parent_trait_pred));
                        impl_where_bound_count += 1;
                    }
                    (ChildMode::Host(parent_host_pred),
                        GoalSource::ImplWhereBound |
                        GoalSource::AliasBoundConstCondition) => {
                        obligation =
                            make_obligation(derive_host_cause(tcx, candidate.kind(),
                                    self.obligation.cause.clone(), impl_where_bound_count,
                                    parent_host_pred));
                        impl_where_bound_count += 1;
                    }
                    (ChildMode::PassThrough, _) |
                        (_,
                        GoalSource::AliasWellFormed |
                        GoalSource::AliasBoundConstCondition) => {
                        obligation = make_obligation(self.obligation.cause.clone());
                    }
                }
                self.with_derived_obligation(obligation,
                        |this| nested_goal.visit_with(this))?;
            }
            if let Some(ty::PredicateKind::AliasRelate(lhs, rhs, _)) =
                    pred.kind().no_bound_vars() {
                goal.infcx().visit_proof_tree_at_depth(goal.goal().with(tcx,
                            ty::ClauseKind::WellFormed(lhs)), goal.depth() + 1, self)?;
                goal.infcx().visit_proof_tree_at_depth(goal.goal().with(tcx,
                            ty::ClauseKind::WellFormed(rhs)), goal.depth() + 1, self)?;
            }
            self.detect_trait_error_in_higher_ranked_projection(goal)?;
            ControlFlow::Break(self.obligation.clone())
        }
    }
}#[instrument(level = "trace", skip(self, goal), fields(goal = ?goal.goal()))]
422    fn visit_goal(&mut self, goal: &inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
423        let tcx = goal.infcx().tcx;
424        // Skip goals that aren't the *reason* for our goal's failure.
425        match (self.consider_ambiguities, goal.result()) {
426            (
427                true,
428                Ok(Certainty::Maybe(MaybeInfo {
429                    cause: MaybeCause::Ambiguity,
430                    opaque_types_jank: _,
431                    stalled_on_coroutines: _,
432                })),
433            )
434            | (false, Err(_)) => {}
435            _ => return ControlFlow::Continue(()),
436        }
437
438        let pred = goal.goal().predicate;
439
440        let candidates = self.non_trivial_candidates(goal);
441        let candidate = match candidates.as_slice() {
442            [candidate] => candidate,
443            [] => return self.detect_error_from_empty_candidates(goal),
444            _ => return ControlFlow::Break(self.obligation.clone()),
445        };
446
447        // Don't walk into impls that have `do_not_recommend`.
448        if let inspect::ProbeKind::TraitCandidate {
449            source: CandidateSource::Impl(impl_def_id),
450            result: _,
451        } = candidate.kind()
452            && tcx.do_not_recommend_impl(impl_def_id)
453        {
454            trace!("#[diagnostic::do_not_recommend] -> exit");
455            return ControlFlow::Break(self.obligation.clone());
456        }
457
458        // FIXME: Also, what about considering >1 layer up the stack? May be necessary
459        // for normalizes-to.
460        let child_mode = match pred.kind().skip_binder() {
461            ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => {
462                ChildMode::Trait(pred.kind().rebind(trait_pred))
463            }
464            ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(host_pred)) => {
465                ChildMode::Host(pred.kind().rebind(host_pred))
466            }
467            ty::PredicateKind::NormalizesTo(normalizes_to)
468                if matches!(
469                    normalizes_to.alias.kind,
470                    ty::AliasTermKind::ProjectionTy { .. }
471                        | ty::AliasTermKind::ProjectionConst { .. }
472                ) =>
473            {
474                ChildMode::Trait(pred.kind().rebind(ty::TraitPredicate {
475                    trait_ref: normalizes_to.alias.trait_ref(tcx),
476                    polarity: ty::PredicatePolarity::Positive,
477                }))
478            }
479            ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
480                return self.visit_well_formed_goal(candidate, term);
481            }
482            _ => ChildMode::PassThrough,
483        };
484
485        let nested_goals = candidate.instantiate_nested_goals(self.span());
486
487        // If the candidate requires some `T: FnPtr` bound which does not hold should not be treated as
488        // an actual candidate, instead we should treat them as if the impl was never considered to
489        // have potentially applied. As if `impl<A, R> Trait for for<..> fn(..A) -> R` was written
490        // instead of `impl<T: FnPtr> Trait for T`.
491        //
492        // We do this as a separate loop so that we do not choose to tell the user about some nested
493        // goal before we encounter a `T: FnPtr` nested goal.
494        for nested_goal in &nested_goals {
495            if let Some(poly_trait_pred) = nested_goal.goal().predicate.as_trait_clause()
496                && tcx.is_lang_item(poly_trait_pred.def_id(), LangItem::FnPtrTrait)
497                && let Err(NoSolution) = nested_goal.result()
498            {
499                return ControlFlow::Break(self.obligation.clone());
500            }
501        }
502
503        let mut impl_where_bound_count = 0;
504        for nested_goal in nested_goals {
505            trace!(nested_goal = ?(nested_goal.goal(), nested_goal.source(), nested_goal.result()));
506
507            let nested_pred = nested_goal.goal().predicate;
508
509            let make_obligation = |cause| Obligation {
510                cause,
511                param_env: nested_goal.goal().param_env,
512                predicate: nested_pred,
513                recursion_depth: self.obligation.recursion_depth + 1,
514            };
515
516            let obligation;
517            match (child_mode, nested_goal.source()) {
518                (
519                    ChildMode::Trait(_) | ChildMode::Host(_),
520                    GoalSource::Misc | GoalSource::TypeRelating | GoalSource::NormalizeGoal(_),
521                ) => {
522                    continue;
523                }
524                (ChildMode::Trait(parent_trait_pred), GoalSource::ImplWhereBound) => {
525                    obligation = make_obligation(derive_cause(
526                        tcx,
527                        candidate.kind(),
528                        self.obligation.cause.clone(),
529                        impl_where_bound_count,
530                        parent_trait_pred,
531                    ));
532                    impl_where_bound_count += 1;
533                }
534                (
535                    ChildMode::Host(parent_host_pred),
536                    GoalSource::ImplWhereBound | GoalSource::AliasBoundConstCondition,
537                ) => {
538                    obligation = make_obligation(derive_host_cause(
539                        tcx,
540                        candidate.kind(),
541                        self.obligation.cause.clone(),
542                        impl_where_bound_count,
543                        parent_host_pred,
544                    ));
545                    impl_where_bound_count += 1;
546                }
547                (ChildMode::PassThrough, _)
548                | (_, GoalSource::AliasWellFormed | GoalSource::AliasBoundConstCondition) => {
549                    obligation = make_obligation(self.obligation.cause.clone());
550                }
551            }
552
553            self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
554        }
555
556        // alias-relate may fail because the lhs or rhs can't be normalized,
557        // and therefore is treated as rigid.
558        if let Some(ty::PredicateKind::AliasRelate(lhs, rhs, _)) = pred.kind().no_bound_vars() {
559            goal.infcx().visit_proof_tree_at_depth(
560                goal.goal().with(tcx, ty::ClauseKind::WellFormed(lhs)),
561                goal.depth() + 1,
562                self,
563            )?;
564            goal.infcx().visit_proof_tree_at_depth(
565                goal.goal().with(tcx, ty::ClauseKind::WellFormed(rhs)),
566                goal.depth() + 1,
567                self,
568            )?;
569        }
570
571        self.detect_trait_error_in_higher_ranked_projection(goal)?;
572
573        ControlFlow::Break(self.obligation.clone())
574    }
575}
576
577#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ChildMode<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ChildMode::Trait(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Trait",
                    &__self_0),
            ChildMode::Host(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Host",
                    &__self_0),
            ChildMode::PassThrough =>
                ::core::fmt::Formatter::write_str(f, "PassThrough"),
        }
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ChildMode<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for ChildMode<'tcx> {
    #[inline]
    fn clone(&self) -> ChildMode<'tcx> {
        let _:
                ::core::clone::AssertParamIsClone<ty::PolyTraitPredicate<'tcx>>;
        let _:
                ::core::clone::AssertParamIsClone<ty::Binder<'tcx,
                ty::HostEffectPredicate<'tcx>>>;
        *self
    }
}Clone)]
578enum ChildMode<'tcx> {
579    // Try to derive an `ObligationCause::{ImplDerived,BuiltinDerived}`,
580    // and skip all `GoalSource::Misc`, which represent useless obligations
581    // such as alias-eq which may not hold.
582    Trait(ty::PolyTraitPredicate<'tcx>),
583    // Try to derive an `ObligationCause::{ImplDerived,BuiltinDerived}`,
584    // and skip all `GoalSource::Misc`, which represent useless obligations
585    // such as alias-eq which may not hold.
586    Host(ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>),
587    // Skip trying to derive an `ObligationCause` from this obligation, and
588    // report *all* sub-obligations as if they came directly from the parent
589    // obligation.
590    PassThrough,
591}
592
593fn derive_cause<'tcx>(
594    tcx: TyCtxt<'tcx>,
595    candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
596    mut cause: ObligationCause<'tcx>,
597    idx: usize,
598    parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
599) -> ObligationCause<'tcx> {
600    match candidate_kind {
601        inspect::ProbeKind::TraitCandidate {
602            source: CandidateSource::Impl(impl_def_id),
603            result: _,
604        } => {
605            if let Some((_, span)) =
606                tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx)
607            {
608                cause = cause.derived_cause(parent_trait_pred, |derived| {
609                    ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
610                        derived,
611                        impl_or_alias_def_id: impl_def_id,
612                        impl_def_predicate_index: Some(idx),
613                        span,
614                    }))
615                })
616            }
617        }
618        inspect::ProbeKind::TraitCandidate {
619            source: CandidateSource::BuiltinImpl(..),
620            result: _,
621        } => {
622            cause = cause.derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerived);
623        }
624        _ => {}
625    };
626    cause
627}
628
629fn derive_host_cause<'tcx>(
630    tcx: TyCtxt<'tcx>,
631    candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
632    mut cause: ObligationCause<'tcx>,
633    idx: usize,
634    parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
635) -> ObligationCause<'tcx> {
636    match candidate_kind {
637        inspect::ProbeKind::TraitCandidate {
638            source: CandidateSource::Impl(impl_def_id),
639            result: _,
640        } => {
641            if let Some((_, span)) = tcx
642                .predicates_of(impl_def_id)
643                .instantiate_identity(tcx)
644                .into_iter()
645                .chain(tcx.const_conditions(impl_def_id).instantiate_identity(tcx).into_iter().map(
646                    |(trait_ref, span)| {
647                        (
648                            trait_ref.to_host_effect_clause(
649                                tcx,
650                                parent_host_pred.skip_binder().constness,
651                            ),
652                            span,
653                        )
654                    },
655                ))
656                .nth(idx)
657            {
658                cause =
659                    cause.derived_host_cause(parent_host_pred, |derived| {
660                        ObligationCauseCode::ImplDerivedHost(Box::new(
661                            traits::ImplDerivedHostCause { derived, impl_def_id, span },
662                        ))
663                    })
664            }
665        }
666        inspect::ProbeKind::TraitCandidate {
667            source: CandidateSource::BuiltinImpl(..),
668            result: _,
669        } => {
670            cause =
671                cause.derived_host_cause(parent_host_pred, ObligationCauseCode::BuiltinDerivedHost);
672        }
673        _ => {}
674    };
675    cause
676}