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::Alias(_, alias_const) => {
38                    alias_const.type_of(infcx.tcx).skip_norm_wip()
39                }
40                ty::ConstKind::Param(param_ct) => {
41                    param_ct.find_const_ty_from_env(obligation.param_env)
42                }
43                ty::ConstKind::Value(cv) => cv.ty,
44                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!(
45                    obligation.cause.span,
46                    "ConstArgHasWrongType failed but we don't know how to compute type for {kind:?}"
47                ),
48            };
49            FulfillmentErrorCode::Select(SelectionError::ConstArgHasWrongType {
50                ct,
51                ct_ty,
52                expected_ty,
53            })
54        }
55        ty::PredicateKind::AliasRelate(_, _, _) => {
56            FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch })
57        }
58        ty::PredicateKind::Subtype(pred) => {
59            let (a, b) = infcx.enter_forall_and_leak_universe(
60                obligation.predicate.kind().rebind((pred.a, pred.b)),
61            );
62            let expected_found = ExpectedFound::new(a, b);
63            FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
64        }
65        ty::PredicateKind::Coerce(pred) => {
66            let (a, b) = infcx.enter_forall_and_leak_universe(
67                obligation.predicate.kind().rebind((pred.a, pred.b)),
68            );
69            let expected_found = ExpectedFound::new(b, a);
70            FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
71        }
72        ty::PredicateKind::Clause(_)
73        | ty::PredicateKind::DynCompatible(_)
74        | ty::PredicateKind::Ambiguous => {
75            FulfillmentErrorCode::Select(SelectionError::Unimplemented)
76        }
77        ty::PredicateKind::ConstEquate(..) | ty::PredicateKind::NormalizesTo(..) => {
78            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected goal: {0:?}",
        obligation))bug!("unexpected goal: {obligation:?}")
79        }
80    };
81
82    FulfillmentError { obligation, code, root_obligation }
83}
84
85pub(super) fn fulfillment_error_for_stalled<'tcx>(
86    infcx: &InferCtxt<'tcx>,
87    root_obligation: PredicateObligation<'tcx>,
88) -> FulfillmentError<'tcx> {
89    let (code, refine_obligation) = infcx.probe(|_| {
90        match <&SolverDelegate<'tcx>>::from(infcx).evaluate_root_goal(
91            root_obligation.as_goal(),
92            root_obligation.cause.span,
93            None,
94        ) {
95            Ok(GoalEvaluation {
96                certainty:
97                    Certainty::Maybe(MaybeInfo {
98                        cause: MaybeCause::Ambiguity,
99                        opaque_types_jank: _,
100                        stalled_on_coroutines: _,
101                    }),
102                ..
103            }) => (FulfillmentErrorCode::Ambiguity { overflow: None }, true),
104            Ok(GoalEvaluation {
105                certainty:
106                    Certainty::Maybe(MaybeInfo {
107                        cause:
108                            MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: _ },
109                        opaque_types_jank: _,
110                        stalled_on_coroutines: _,
111                    }),
112                ..
113            }) => (
114                FulfillmentErrorCode::Ambiguity { overflow: Some(suggest_increasing_limit) },
115                // Don't look into overflows because we treat overflows weirdly anyways.
116                // We discard the inference constraints from overflowing goals, so
117                // recomputing the goal again during `find_best_leaf_obligation` may apply
118                // inference guidance that makes other goals go from ambig -> pass, for example.
119                //
120                // FIXME: We should probably just look into overflows here.
121                false,
122            ),
123            Ok(GoalEvaluation { certainty: Certainty::Yes, .. }) => {
124                ::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!(
125                    root_obligation.cause.span,
126                    "did not expect successful goal when collecting ambiguity errors for `{:?}`",
127                    infcx.resolve_vars_if_possible(root_obligation.predicate),
128                )
129            }
130            Err(_) => {
131                ::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!(
132                    root_obligation.cause.span,
133                    "did not expect selection error when collecting ambiguity errors for `{:?}`",
134                    infcx.resolve_vars_if_possible(root_obligation.predicate),
135                )
136            }
137        }
138    });
139
140    FulfillmentError {
141        obligation: if refine_obligation {
142            find_best_leaf_obligation(infcx, &root_obligation, true)
143        } else {
144            root_obligation.clone()
145        },
146        code,
147        root_obligation,
148    }
149}
150
151pub(super) fn fulfillment_error_for_overflow<'tcx>(
152    infcx: &InferCtxt<'tcx>,
153    root_obligation: PredicateObligation<'tcx>,
154) -> FulfillmentError<'tcx> {
155    FulfillmentError {
156        obligation: find_best_leaf_obligation(infcx, &root_obligation, true),
157        code: FulfillmentErrorCode::Ambiguity { overflow: Some(true) },
158        root_obligation,
159    }
160}
161
162x;#[instrument(level = "debug", skip(infcx), ret)]
163fn find_best_leaf_obligation<'tcx>(
164    infcx: &InferCtxt<'tcx>,
165    obligation: &PredicateObligation<'tcx>,
166    consider_ambiguities: bool,
167) -> PredicateObligation<'tcx> {
168    let obligation = infcx.resolve_vars_if_possible(obligation.clone());
169    // FIXME: we use a probe here as the `BestObligation` visitor does not
170    // check whether it uses candidates which get shadowed by where-bounds.
171    //
172    // We should probably fix the visitor to not do so instead, as this also
173    // means the leaf obligation may be incorrect.
174    let obligation = infcx
175        .fudge_inference_if_ok(|| {
176            infcx
177                .visit_proof_tree(
178                    obligation.as_goal(),
179                    &mut BestObligation { obligation: obligation.clone(), consider_ambiguities },
180                )
181                .break_value()
182                .ok_or(())
183                // walk around the fact that the cause in `Obligation` is ignored by folders so that
184                // we can properly fudge the infer vars in cause code.
185                .map(|o| (o.cause.clone(), o))
186        })
187        .map(|(cause, o)| PredicateObligation { cause, ..o })
188        .unwrap_or(obligation);
189    deeply_normalize_for_diagnostics(infcx, obligation.param_env, obligation)
190}
191
192struct BestObligation<'tcx> {
193    obligation: PredicateObligation<'tcx>,
194    consider_ambiguities: bool,
195}
196
197impl<'tcx> BestObligation<'tcx> {
198    fn with_derived_obligation(
199        &mut self,
200        derived_obligation: PredicateObligation<'tcx>,
201        and_then: impl FnOnce(&mut Self) -> <Self as ProofTreeVisitor<'tcx>>::Result,
202    ) -> <Self as ProofTreeVisitor<'tcx>>::Result {
203        let old_obligation = std::mem::replace(&mut self.obligation, derived_obligation);
204        let res = and_then(self);
205        self.obligation = old_obligation;
206        res
207    }
208
209    /// Filter out the candidates that aren't interesting to visit for the
210    /// purposes of reporting errors. For ambiguities, we only consider
211    /// candidates that may hold. For errors, we only consider candidates that
212    /// *don't* hold and which have impl-where clauses that also don't hold.
213    fn non_trivial_candidates<'a>(
214        &self,
215        goal: &'a inspect::InspectGoal<'a, 'tcx>,
216    ) -> Vec<inspect::InspectCandidate<'a, 'tcx>> {
217        let mut candidates = goal.candidates();
218        match self.consider_ambiguities {
219            true => {
220                // If we have an ambiguous obligation, we must consider *all* candidates
221                // that hold, or else we may guide inference causing other goals to go
222                // from ambig -> pass/fail.
223                candidates.retain(|candidate| candidate.result().is_ok());
224            }
225            false => {
226                // We always handle rigid alias candidates separately as we may not add them for
227                // aliases whose trait bound doesn't hold.
228                candidates.retain(|c| !#[allow(non_exhaustive_omitted_patterns)] match c.kind() {
    inspect::ProbeKind::RigidAlias { .. } => true,
    _ => false,
}matches!(c.kind(), inspect::ProbeKind::RigidAlias { .. }));
229                // If we have >1 candidate, one may still be due to "boring" reasons, like
230                // an alias-relate that failed to hold when deeply evaluated. We really
231                // don't care about reasons like this.
232                if candidates.len() > 1 {
233                    candidates.retain(|candidate| {
234                        goal.infcx().probe(|_| {
235                            candidate.instantiate_nested_goals(self.span()).iter().any(
236                                |nested_goal| {
237                                    #[allow(non_exhaustive_omitted_patterns)] match nested_goal.source() {
    GoalSource::ImplWhereBound | GoalSource::AliasBoundConstCondition |
        GoalSource::AliasWellFormed => true,
    _ => false,
}matches!(
238                                        nested_goal.source(),
239                                        GoalSource::ImplWhereBound
240                                            | GoalSource::AliasBoundConstCondition
241                                            | GoalSource::AliasWellFormed
242                                    ) && nested_goal.result().is_err()
243                                },
244                            )
245                        })
246                    });
247                }
248            }
249        }
250
251        candidates
252    }
253
254    /// HACK: We walk the nested obligations for a well-formed arg manually,
255    /// since there's nontrivial logic in `wf.rs` to set up an obligation cause.
256    /// Ideally we'd be able to track this better.
257    fn visit_well_formed_goal(
258        &mut self,
259        candidate: &inspect::InspectCandidate<'_, 'tcx>,
260        term: ty::Term<'tcx>,
261    ) -> ControlFlow<PredicateObligation<'tcx>> {
262        let infcx = candidate.goal().infcx();
263        let param_env = candidate.goal().goal().param_env;
264        let body_id = self.obligation.cause.body_id;
265
266        for obligation in wf::unnormalized_obligations(infcx, param_env, term, self.span(), body_id)
267            .into_iter()
268            .flatten()
269        {
270            let nested_goal = candidate.instantiate_proof_tree_for_nested_goal(
271                GoalSource::Misc,
272                obligation.as_goal(),
273                self.span(),
274            );
275            // Skip nested goals that aren't the *reason* for our goal's failure.
276            match (self.consider_ambiguities, nested_goal.result()) {
277                (
278                    true,
279                    Ok(Certainty::Maybe(MaybeInfo {
280                        cause: MaybeCause::Ambiguity,
281                        opaque_types_jank: _,
282                        stalled_on_coroutines: _,
283                    })),
284                )
285                | (false, Err(_)) => {}
286                _ => continue,
287            }
288
289            self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
290        }
291
292        ControlFlow::Break(self.obligation.clone())
293    }
294
295    /// If a normalization of an associated item or a trait goal fails without trying any
296    /// candidates it's likely that normalizing its self type failed. We manually detect
297    /// such cases here.
298    fn detect_error_in_self_ty_normalization(
299        &mut self,
300        goal: &inspect::InspectGoal<'_, 'tcx>,
301        self_ty: Ty<'tcx>,
302    ) -> ControlFlow<PredicateObligation<'tcx>> {
303        if !!self.consider_ambiguities {
    ::core::panicking::panic("assertion failed: !self.consider_ambiguities")
};assert!(!self.consider_ambiguities);
304        let tcx = goal.infcx().tcx;
305        if let ty::Alias(_, alias) = *self_ty.kind() {
306            let infer_term = goal.infcx().next_ty_var(self.obligation.cause.span);
307            let pred =
308                ty::ProjectionPredicate { projection_term: alias.into(), term: infer_term.into() };
309            let obligation =
310                Obligation::new(tcx, self.obligation.cause.clone(), goal.goal().param_env, pred);
311            self.with_derived_obligation(obligation, |this| {
312                goal.infcx().visit_proof_tree_at_depth(
313                    goal.goal().with(tcx, pred),
314                    goal.depth() + 1,
315                    this,
316                )
317            })
318        } else {
319            ControlFlow::Continue(())
320        }
321    }
322
323    /// When a higher-ranked projection goal fails, check that the corresponding
324    /// higher-ranked trait goal holds or not. This is because the process of
325    /// instantiating and then re-canonicalizing the binder of the projection goal
326    /// forces us to be unable to see that the leak check failed in the nested
327    /// `NormalizesTo` goal, so we don't fall back to the rigid projection check
328    /// that should catch when a projection goal fails due to an unsatisfied trait
329    /// goal.
330    fn detect_trait_error_in_higher_ranked_projection(
331        &mut self,
332        goal: &inspect::InspectGoal<'_, 'tcx>,
333    ) -> ControlFlow<PredicateObligation<'tcx>> {
334        let tcx = goal.infcx().tcx;
335        if let Some(projection_clause) = goal.goal().predicate.as_projection_clause()
336            && !projection_clause.bound_vars().is_empty()
337        {
338            let pred = projection_clause.map_bound(|proj| proj.projection_term.trait_ref(tcx));
339            let obligation = Obligation::new(
340                tcx,
341                self.obligation.cause.clone(),
342                goal.goal().param_env,
343                deeply_normalize_for_diagnostics(goal.infcx(), goal.goal().param_env, pred),
344            );
345            self.with_derived_obligation(obligation, |this| {
346                goal.infcx().visit_proof_tree_at_depth(
347                    goal.goal().with(tcx, pred),
348                    goal.depth() + 1,
349                    this,
350                )
351            })
352        } else {
353            ControlFlow::Continue(())
354        }
355    }
356
357    /// It is likely that `NormalizesTo` failed without any applicable candidates
358    /// because the alias is not well-formed.
359    ///
360    /// As we only enter `RigidAlias` candidates if the trait bound of the associated type
361    /// holds, we discard these candidates in `non_trivial_candidates` and always manually
362    /// check this here.
363    fn detect_non_well_formed_assoc_item(
364        &mut self,
365        goal: &inspect::InspectGoal<'_, 'tcx>,
366        alias: ty::AliasTerm<'tcx>,
367    ) -> ControlFlow<PredicateObligation<'tcx>> {
368        let tcx = goal.infcx().tcx;
369        let obligation = Obligation::new(
370            tcx,
371            self.obligation.cause.clone(),
372            goal.goal().param_env,
373            alias.trait_ref(tcx),
374        );
375        self.with_derived_obligation(obligation, |this| {
376            goal.infcx().visit_proof_tree_at_depth(
377                goal.goal().with(tcx, alias.trait_ref(tcx)),
378                goal.depth() + 1,
379                this,
380            )
381        })
382    }
383
384    /// If we have no candidates, then it's likely that there is a
385    /// non-well-formed alias in the goal.
386    fn detect_error_from_empty_candidates(
387        &mut self,
388        goal: &inspect::InspectGoal<'_, 'tcx>,
389    ) -> ControlFlow<PredicateObligation<'tcx>> {
390        let pred_kind = goal.goal().predicate.kind();
391
392        match pred_kind.no_bound_vars() {
393            Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))) => {
394                self.detect_error_in_self_ty_normalization(goal, pred.self_ty())?;
395            }
396            Some(ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)))
397                if pred.projection_term.kind.is_trait_projection() =>
398            {
399                self.detect_error_in_self_ty_normalization(goal, pred.projection_term.self_ty())?;
400                self.detect_non_well_formed_assoc_item(goal, pred.projection_term)?;
401            }
402            Some(_) | None => {}
403        }
404
405        ControlFlow::Break(self.obligation.clone())
406    }
407}
408
409impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
410    type Result = ControlFlow<PredicateObligation<'tcx>>;
411
412    fn span(&self) -> rustc_span::Span {
413        self.obligation.cause.span
414    }
415
416    #[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(416u32),
                                    ::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:449",
                                        "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(449u32),
                                        ::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::Clause(ty::ClauseKind::Projection(projection))
                        if projection.projection_term.kind.is_trait_projection() =>
                        {
                        ChildMode::Trait(pred.kind().rebind(ty::TraitPredicate {
                                    trait_ref: projection.projection_term.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:496",
                                        "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(496u32),
                                        ::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()))]
417    fn visit_goal(&mut self, goal: &inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
418        let tcx = goal.infcx().tcx;
419        // Skip goals that aren't the *reason* for our goal's failure.
420        match (self.consider_ambiguities, goal.result()) {
421            (
422                true,
423                Ok(Certainty::Maybe(MaybeInfo {
424                    cause: MaybeCause::Ambiguity,
425                    opaque_types_jank: _,
426                    stalled_on_coroutines: _,
427                })),
428            )
429            | (false, Err(_)) => {}
430            _ => return ControlFlow::Continue(()),
431        }
432
433        let pred = goal.goal().predicate;
434
435        let candidates = self.non_trivial_candidates(goal);
436        let candidate = match candidates.as_slice() {
437            [candidate] => candidate,
438            [] => return self.detect_error_from_empty_candidates(goal),
439            _ => return ControlFlow::Break(self.obligation.clone()),
440        };
441
442        // Don't walk into impls that have `do_not_recommend`.
443        if let inspect::ProbeKind::TraitCandidate {
444            source: CandidateSource::Impl(impl_def_id),
445            result: _,
446        } = candidate.kind()
447            && tcx.do_not_recommend_impl(impl_def_id)
448        {
449            trace!("#[diagnostic::do_not_recommend] -> exit");
450            return ControlFlow::Break(self.obligation.clone());
451        }
452
453        // FIXME: Also, what about considering >1 layer up the stack? May be necessary
454        // for normalizes-to.
455        let child_mode = match pred.kind().skip_binder() {
456            ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => {
457                ChildMode::Trait(pred.kind().rebind(trait_pred))
458            }
459            ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(host_pred)) => {
460                ChildMode::Host(pred.kind().rebind(host_pred))
461            }
462            ty::PredicateKind::Clause(ty::ClauseKind::Projection(projection))
463                if projection.projection_term.kind.is_trait_projection() =>
464            {
465                ChildMode::Trait(pred.kind().rebind(ty::TraitPredicate {
466                    trait_ref: projection.projection_term.trait_ref(tcx),
467                    polarity: ty::PredicatePolarity::Positive,
468                }))
469            }
470            ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
471                return self.visit_well_formed_goal(candidate, term);
472            }
473            _ => ChildMode::PassThrough,
474        };
475
476        let nested_goals = candidate.instantiate_nested_goals(self.span());
477
478        // If the candidate requires some `T: FnPtr` bound which does not hold should not be treated as
479        // an actual candidate, instead we should treat them as if the impl was never considered to
480        // have potentially applied. As if `impl<A, R> Trait for for<..> fn(..A) -> R` was written
481        // instead of `impl<T: FnPtr> Trait for T`.
482        //
483        // We do this as a separate loop so that we do not choose to tell the user about some nested
484        // goal before we encounter a `T: FnPtr` nested goal.
485        for nested_goal in &nested_goals {
486            if let Some(poly_trait_pred) = nested_goal.goal().predicate.as_trait_clause()
487                && tcx.is_lang_item(poly_trait_pred.def_id(), LangItem::FnPtrTrait)
488                && let Err(NoSolution) = nested_goal.result()
489            {
490                return ControlFlow::Break(self.obligation.clone());
491            }
492        }
493
494        let mut impl_where_bound_count = 0;
495        for nested_goal in nested_goals {
496            trace!(nested_goal = ?(nested_goal.goal(), nested_goal.source(), nested_goal.result()));
497
498            let nested_pred = nested_goal.goal().predicate;
499
500            let make_obligation = |cause| Obligation {
501                cause,
502                param_env: nested_goal.goal().param_env,
503                predicate: nested_pred,
504                recursion_depth: self.obligation.recursion_depth + 1,
505            };
506
507            let obligation;
508            match (child_mode, nested_goal.source()) {
509                (
510                    ChildMode::Trait(_) | ChildMode::Host(_),
511                    GoalSource::Misc | GoalSource::TypeRelating | GoalSource::NormalizeGoal(_),
512                ) => {
513                    continue;
514                }
515                (ChildMode::Trait(parent_trait_pred), GoalSource::ImplWhereBound) => {
516                    obligation = make_obligation(derive_cause(
517                        tcx,
518                        candidate.kind(),
519                        self.obligation.cause.clone(),
520                        impl_where_bound_count,
521                        parent_trait_pred,
522                    ));
523                    impl_where_bound_count += 1;
524                }
525                (
526                    ChildMode::Host(parent_host_pred),
527                    GoalSource::ImplWhereBound | GoalSource::AliasBoundConstCondition,
528                ) => {
529                    obligation = make_obligation(derive_host_cause(
530                        tcx,
531                        candidate.kind(),
532                        self.obligation.cause.clone(),
533                        impl_where_bound_count,
534                        parent_host_pred,
535                    ));
536                    impl_where_bound_count += 1;
537                }
538                (ChildMode::PassThrough, _)
539                | (_, GoalSource::AliasWellFormed | GoalSource::AliasBoundConstCondition) => {
540                    obligation = make_obligation(self.obligation.cause.clone());
541                }
542            }
543
544            self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
545        }
546
547        // alias-relate may fail because the lhs or rhs can't be normalized,
548        // and therefore is treated as rigid.
549        if let Some(ty::PredicateKind::AliasRelate(lhs, rhs, _)) = pred.kind().no_bound_vars() {
550            goal.infcx().visit_proof_tree_at_depth(
551                goal.goal().with(tcx, ty::ClauseKind::WellFormed(lhs)),
552                goal.depth() + 1,
553                self,
554            )?;
555            goal.infcx().visit_proof_tree_at_depth(
556                goal.goal().with(tcx, ty::ClauseKind::WellFormed(rhs)),
557                goal.depth() + 1,
558                self,
559            )?;
560        }
561
562        self.detect_trait_error_in_higher_ranked_projection(goal)?;
563
564        ControlFlow::Break(self.obligation.clone())
565    }
566}
567
568#[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)]
569enum ChildMode<'tcx> {
570    // Try to derive an `ObligationCause::{ImplDerived,BuiltinDerived}`,
571    // and skip all `GoalSource::Misc`, which represent useless obligations
572    // such as alias-eq which may not hold.
573    Trait(ty::PolyTraitPredicate<'tcx>),
574    // Try to derive an `ObligationCause::{ImplDerived,BuiltinDerived}`,
575    // and skip all `GoalSource::Misc`, which represent useless obligations
576    // such as alias-eq which may not hold.
577    Host(ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>),
578    // Skip trying to derive an `ObligationCause` from this obligation, and
579    // report *all* sub-obligations as if they came directly from the parent
580    // obligation.
581    PassThrough,
582}
583
584fn derive_cause<'tcx>(
585    tcx: TyCtxt<'tcx>,
586    candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
587    mut cause: ObligationCause<'tcx>,
588    idx: usize,
589    parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
590) -> ObligationCause<'tcx> {
591    match candidate_kind {
592        inspect::ProbeKind::TraitCandidate {
593            source: CandidateSource::Impl(impl_def_id),
594            result: _,
595        } => {
596            if let Some((_, span)) =
597                tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx)
598            {
599                cause = cause.derived_cause(parent_trait_pred, |derived| {
600                    ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
601                        derived,
602                        impl_or_alias_def_id: impl_def_id,
603                        impl_def_predicate_index: Some(idx),
604                        span,
605                    }))
606                })
607            }
608        }
609        inspect::ProbeKind::TraitCandidate {
610            source: CandidateSource::BuiltinImpl(..),
611            result: _,
612        } => {
613            cause = cause.derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerived);
614        }
615        _ => {}
616    };
617    cause
618}
619
620fn derive_host_cause<'tcx>(
621    tcx: TyCtxt<'tcx>,
622    candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
623    mut cause: ObligationCause<'tcx>,
624    idx: usize,
625    parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
626) -> ObligationCause<'tcx> {
627    match candidate_kind {
628        inspect::ProbeKind::TraitCandidate {
629            source: CandidateSource::Impl(impl_def_id),
630            result: _,
631        } => {
632            if let Some((_, span)) = tcx
633                .predicates_of(impl_def_id)
634                .instantiate_identity(tcx)
635                .into_iter()
636                .chain(tcx.const_conditions(impl_def_id).instantiate_identity(tcx).into_iter().map(
637                    |(trait_ref, span)| {
638                        (
639                            trait_ref.to_host_effect_clause(
640                                tcx,
641                                parent_host_pred.skip_binder().constness,
642                            ),
643                            span,
644                        )
645                    },
646                ))
647                .nth(idx)
648            {
649                cause =
650                    cause.derived_host_cause(parent_host_pred, |derived| {
651                        ObligationCauseCode::ImplDerivedHost(Box::new(
652                            traits::ImplDerivedHostCause { derived, impl_def_id, span },
653                        ))
654                    })
655            }
656        }
657        inspect::ProbeKind::TraitCandidate {
658            source: CandidateSource::BuiltinImpl(..),
659            result: _,
660        } => {
661            cause =
662                cause.derived_host_cause(parent_host_pred, ObligationCauseCode::BuiltinDerivedHost);
663        }
664        _ => {}
665    };
666    cause
667}