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 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::AliasRelate(_, _, _) => {
54 FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch })
55 }
56 ty::PredicateKind::Subtype(pred) => {
57 let (a, b) = infcx.enter_forall_and_leak_universe(
58 obligation.predicate.kind().rebind((pred.a, pred.b)),
59 );
60 let expected_found = ExpectedFound::new(a, b);
61 FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
62 }
63 ty::PredicateKind::Coerce(pred) => {
64 let (a, b) = infcx.enter_forall_and_leak_universe(
65 obligation.predicate.kind().rebind((pred.a, pred.b)),
66 );
67 let expected_found = ExpectedFound::new(b, a);
68 FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found))
69 }
70 ty::PredicateKind::Clause(_)
71 | ty::PredicateKind::DynCompatible(_)
72 | ty::PredicateKind::Ambiguous => {
73 FulfillmentErrorCode::Select(SelectionError::Unimplemented)
74 }
75 ty::PredicateKind::ConstEquate(..) | ty::PredicateKind::NormalizesTo(..) => {
76 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected goal: {0:?}",
obligation))bug!("unexpected goal: {obligation:?}")
77 }
78 };
79
80 FulfillmentError { obligation, code, root_obligation }
81}
82
83pub(super) fn fulfillment_error_for_stalled<'tcx>(
84 infcx: &InferCtxt<'tcx>,
85 root_obligation: PredicateObligation<'tcx>,
86) -> FulfillmentError<'tcx> {
87 let (code, refine_obligation) = infcx.probe(|_| {
88 match <&SolverDelegate<'tcx>>::from(infcx).evaluate_root_goal(
89 root_obligation.as_goal(),
90 root_obligation.cause.span,
91 None,
92 ) {
93 Ok(GoalEvaluation {
94 certainty:
95 Certainty::Maybe(MaybeInfo {
96 cause: MaybeCause::Ambiguity,
97 opaque_types_jank: _,
98 stalled_on_coroutines: _,
99 }),
100 ..
101 }) => (FulfillmentErrorCode::Ambiguity { overflow: None }, true),
102 Ok(GoalEvaluation {
103 certainty:
104 Certainty::Maybe(MaybeInfo {
105 cause:
106 MaybeCause::Overflow { suggest_increasing_limit, keep_constraints: _ },
107 opaque_types_jank: _,
108 stalled_on_coroutines: _,
109 }),
110 ..
111 }) => (
112 FulfillmentErrorCode::Ambiguity { overflow: Some(suggest_increasing_limit) },
113 false,
120 ),
121 Ok(GoalEvaluation { certainty: Certainty::Yes, .. }) => {
122 ::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!(
123 root_obligation.cause.span,
124 "did not expect successful goal when collecting ambiguity errors for `{:?}`",
125 infcx.resolve_vars_if_possible(root_obligation.predicate),
126 )
127 }
128 Err(_) => {
129 ::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!(
130 root_obligation.cause.span,
131 "did not expect selection error when collecting ambiguity errors for `{:?}`",
132 infcx.resolve_vars_if_possible(root_obligation.predicate),
133 )
134 }
135 }
136 });
137
138 FulfillmentError {
139 obligation: if refine_obligation {
140 find_best_leaf_obligation(infcx, &root_obligation, true)
141 } else {
142 root_obligation.clone()
143 },
144 code,
145 root_obligation,
146 }
147}
148
149pub(super) fn fulfillment_error_for_overflow<'tcx>(
150 infcx: &InferCtxt<'tcx>,
151 root_obligation: PredicateObligation<'tcx>,
152) -> FulfillmentError<'tcx> {
153 FulfillmentError {
154 obligation: find_best_leaf_obligation(infcx, &root_obligation, true),
155 code: FulfillmentErrorCode::Ambiguity { overflow: Some(true) },
156 root_obligation,
157 }
158}
159
160x;#[instrument(level = "debug", skip(infcx), ret)]
161fn find_best_leaf_obligation<'tcx>(
162 infcx: &InferCtxt<'tcx>,
163 obligation: &PredicateObligation<'tcx>,
164 consider_ambiguities: bool,
165) -> PredicateObligation<'tcx> {
166 let obligation = infcx.resolve_vars_if_possible(obligation.clone());
167 let obligation = infcx
173 .fudge_inference_if_ok(|| {
174 infcx
175 .visit_proof_tree(
176 obligation.as_goal(),
177 &mut BestObligation { obligation: obligation.clone(), consider_ambiguities },
178 )
179 .break_value()
180 .ok_or(())
181 .map(|o| (o.cause.clone(), o))
184 })
185 .map(|(cause, o)| PredicateObligation { cause, ..o })
186 .unwrap_or(obligation);
187 deeply_normalize_for_diagnostics(infcx, obligation.param_env, obligation)
188}
189
190struct BestObligation<'tcx> {
191 obligation: PredicateObligation<'tcx>,
192 consider_ambiguities: bool,
193}
194
195impl<'tcx> BestObligation<'tcx> {
196 fn with_derived_obligation(
197 &mut self,
198 derived_obligation: PredicateObligation<'tcx>,
199 and_then: impl FnOnce(&mut Self) -> <Self as ProofTreeVisitor<'tcx>>::Result,
200 ) -> <Self as ProofTreeVisitor<'tcx>>::Result {
201 let old_obligation = std::mem::replace(&mut self.obligation, derived_obligation);
202 let res = and_then(self);
203 self.obligation = old_obligation;
204 res
205 }
206
207 fn non_trivial_candidates<'a>(
212 &self,
213 goal: &'a inspect::InspectGoal<'a, 'tcx>,
214 ) -> Vec<inspect::InspectCandidate<'a, 'tcx>> {
215 let mut candidates = goal.candidates();
216 match self.consider_ambiguities {
217 true => {
218 candidates.retain(|candidate| candidate.result().is_ok());
222 }
223 false => {
224 candidates.retain(|c| !#[allow(non_exhaustive_omitted_patterns)] match c.kind() {
inspect::ProbeKind::RigidAlias { .. } => true,
_ => false,
}matches!(c.kind(), inspect::ProbeKind::RigidAlias { .. }));
227 if candidates.len() > 1 {
231 candidates.retain(|candidate| {
232 goal.infcx().probe(|_| {
233 candidate.instantiate_nested_goals(self.span()).iter().any(
234 |nested_goal| {
235 #[allow(non_exhaustive_omitted_patterns)] match nested_goal.source() {
GoalSource::ImplWhereBound | GoalSource::AliasBoundConstCondition |
GoalSource::AliasWellFormed => true,
_ => false,
}matches!(
236 nested_goal.source(),
237 GoalSource::ImplWhereBound
238 | GoalSource::AliasBoundConstCondition
239 | GoalSource::AliasWellFormed
240 ) && nested_goal.result().is_err()
241 },
242 )
243 })
244 });
245 }
246 }
247 }
248
249 candidates
250 }
251
252 fn visit_well_formed_goal(
256 &mut self,
257 candidate: &inspect::InspectCandidate<'_, 'tcx>,
258 term: ty::Term<'tcx>,
259 ) -> ControlFlow<PredicateObligation<'tcx>> {
260 let infcx = candidate.goal().infcx();
261 let param_env = candidate.goal().goal().param_env;
262 let body_id = self.obligation.cause.body_id;
263
264 for obligation in wf::unnormalized_obligations(infcx, param_env, term, self.span(), body_id)
265 .into_iter()
266 .flatten()
267 {
268 let nested_goal = candidate.instantiate_proof_tree_for_nested_goal(
269 GoalSource::Misc,
270 obligation.as_goal(),
271 self.span(),
272 );
273 match (self.consider_ambiguities, nested_goal.result()) {
275 (
276 true,
277 Ok(Certainty::Maybe(MaybeInfo {
278 cause: MaybeCause::Ambiguity,
279 opaque_types_jank: _,
280 stalled_on_coroutines: _,
281 })),
282 )
283 | (false, Err(_)) => {}
284 _ => continue,
285 }
286
287 self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
288 }
289
290 ControlFlow::Break(self.obligation.clone())
291 }
292
293 fn detect_error_in_self_ty_normalization(
297 &mut self,
298 goal: &inspect::InspectGoal<'_, 'tcx>,
299 self_ty: Ty<'tcx>,
300 ) -> ControlFlow<PredicateObligation<'tcx>> {
301 if !!self.consider_ambiguities {
::core::panicking::panic("assertion failed: !self.consider_ambiguities")
};assert!(!self.consider_ambiguities);
302 let tcx = goal.infcx().tcx;
303 if let ty::Alias(alias) = *self_ty.kind() {
304 let infer_term = goal.infcx().next_ty_var(self.obligation.cause.span);
305 let pred =
306 ty::ProjectionPredicate { projection_term: alias.into(), term: infer_term.into() };
307 let obligation =
308 Obligation::new(tcx, self.obligation.cause.clone(), goal.goal().param_env, pred);
309 self.with_derived_obligation(obligation, |this| {
310 goal.infcx().visit_proof_tree_at_depth(
311 goal.goal().with(tcx, pred),
312 goal.depth() + 1,
313 this,
314 )
315 })
316 } else {
317 ControlFlow::Continue(())
318 }
319 }
320
321 fn detect_trait_error_in_higher_ranked_projection(
329 &mut self,
330 goal: &inspect::InspectGoal<'_, 'tcx>,
331 ) -> ControlFlow<PredicateObligation<'tcx>> {
332 let tcx = goal.infcx().tcx;
333 if let Some(projection_clause) = goal.goal().predicate.as_projection_clause()
334 && !projection_clause.bound_vars().is_empty()
335 {
336 let pred = projection_clause.map_bound(|proj| proj.projection_term.trait_ref(tcx));
337 let obligation = Obligation::new(
338 tcx,
339 self.obligation.cause.clone(),
340 goal.goal().param_env,
341 deeply_normalize_for_diagnostics(goal.infcx(), goal.goal().param_env, pred),
342 );
343 self.with_derived_obligation(obligation, |this| {
344 goal.infcx().visit_proof_tree_at_depth(
345 goal.goal().with(tcx, pred),
346 goal.depth() + 1,
347 this,
348 )
349 })
350 } else {
351 ControlFlow::Continue(())
352 }
353 }
354
355 fn detect_non_well_formed_assoc_item(
362 &mut self,
363 goal: &inspect::InspectGoal<'_, 'tcx>,
364 alias: ty::AliasTerm<'tcx>,
365 ) -> ControlFlow<PredicateObligation<'tcx>> {
366 let tcx = goal.infcx().tcx;
367 let obligation = Obligation::new(
368 tcx,
369 self.obligation.cause.clone(),
370 goal.goal().param_env,
371 alias.trait_ref(tcx),
372 );
373 self.with_derived_obligation(obligation, |this| {
374 goal.infcx().visit_proof_tree_at_depth(
375 goal.goal().with(tcx, alias.trait_ref(tcx)),
376 goal.depth() + 1,
377 this,
378 )
379 })
380 }
381
382 fn detect_error_from_empty_candidates(
385 &mut self,
386 goal: &inspect::InspectGoal<'_, 'tcx>,
387 ) -> ControlFlow<PredicateObligation<'tcx>> {
388 let pred_kind = goal.goal().predicate.kind();
389
390 match pred_kind.no_bound_vars() {
391 Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))) => {
392 self.detect_error_in_self_ty_normalization(goal, pred.self_ty())?;
393 }
394 Some(ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)))
395 if pred.projection_term.kind.is_trait_projection() =>
396 {
397 self.detect_error_in_self_ty_normalization(goal, pred.projection_term.self_ty())?;
398 self.detect_non_well_formed_assoc_item(goal, pred.projection_term)?;
399 }
400 Some(_) | None => {}
401 }
402
403 ControlFlow::Break(self.obligation.clone())
404 }
405}
406
407impl<'tcx> ProofTreeVisitor<'tcx> for BestObligation<'tcx> {
408 type Result = ControlFlow<PredicateObligation<'tcx>>;
409
410 fn span(&self) -> rustc_span::Span {
411 self.obligation.cause.span
412 }
413
414 #[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(414u32),
::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:447",
"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(447u32),
::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:494",
"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(494u32),
::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()))]
415 fn visit_goal(&mut self, goal: &inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
416 let tcx = goal.infcx().tcx;
417 match (self.consider_ambiguities, goal.result()) {
419 (
420 true,
421 Ok(Certainty::Maybe(MaybeInfo {
422 cause: MaybeCause::Ambiguity,
423 opaque_types_jank: _,
424 stalled_on_coroutines: _,
425 })),
426 )
427 | (false, Err(_)) => {}
428 _ => return ControlFlow::Continue(()),
429 }
430
431 let pred = goal.goal().predicate;
432
433 let candidates = self.non_trivial_candidates(goal);
434 let candidate = match candidates.as_slice() {
435 [candidate] => candidate,
436 [] => return self.detect_error_from_empty_candidates(goal),
437 _ => return ControlFlow::Break(self.obligation.clone()),
438 };
439
440 if let inspect::ProbeKind::TraitCandidate {
442 source: CandidateSource::Impl(impl_def_id),
443 result: _,
444 } = candidate.kind()
445 && tcx.do_not_recommend_impl(impl_def_id)
446 {
447 trace!("#[diagnostic::do_not_recommend] -> exit");
448 return ControlFlow::Break(self.obligation.clone());
449 }
450
451 let child_mode = match pred.kind().skip_binder() {
454 ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => {
455 ChildMode::Trait(pred.kind().rebind(trait_pred))
456 }
457 ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(host_pred)) => {
458 ChildMode::Host(pred.kind().rebind(host_pred))
459 }
460 ty::PredicateKind::Clause(ty::ClauseKind::Projection(projection))
461 if projection.projection_term.kind.is_trait_projection() =>
462 {
463 ChildMode::Trait(pred.kind().rebind(ty::TraitPredicate {
464 trait_ref: projection.projection_term.trait_ref(tcx),
465 polarity: ty::PredicatePolarity::Positive,
466 }))
467 }
468 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
469 return self.visit_well_formed_goal(candidate, term);
470 }
471 _ => ChildMode::PassThrough,
472 };
473
474 let nested_goals = candidate.instantiate_nested_goals(self.span());
475
476 for nested_goal in &nested_goals {
484 if let Some(poly_trait_pred) = nested_goal.goal().predicate.as_trait_clause()
485 && tcx.is_lang_item(poly_trait_pred.def_id(), LangItem::FnPtrTrait)
486 && let Err(NoSolution) = nested_goal.result()
487 {
488 return ControlFlow::Break(self.obligation.clone());
489 }
490 }
491
492 let mut impl_where_bound_count = 0;
493 for nested_goal in nested_goals {
494 trace!(nested_goal = ?(nested_goal.goal(), nested_goal.source(), nested_goal.result()));
495
496 let nested_pred = nested_goal.goal().predicate;
497
498 let make_obligation = |cause| Obligation {
499 cause,
500 param_env: nested_goal.goal().param_env,
501 predicate: nested_pred,
502 recursion_depth: self.obligation.recursion_depth + 1,
503 };
504
505 let obligation;
506 match (child_mode, nested_goal.source()) {
507 (
508 ChildMode::Trait(_) | ChildMode::Host(_),
509 GoalSource::Misc | GoalSource::TypeRelating | GoalSource::NormalizeGoal(_),
510 ) => {
511 continue;
512 }
513 (ChildMode::Trait(parent_trait_pred), GoalSource::ImplWhereBound) => {
514 obligation = make_obligation(derive_cause(
515 tcx,
516 candidate.kind(),
517 self.obligation.cause.clone(),
518 impl_where_bound_count,
519 parent_trait_pred,
520 ));
521 impl_where_bound_count += 1;
522 }
523 (
524 ChildMode::Host(parent_host_pred),
525 GoalSource::ImplWhereBound | GoalSource::AliasBoundConstCondition,
526 ) => {
527 obligation = make_obligation(derive_host_cause(
528 tcx,
529 candidate.kind(),
530 self.obligation.cause.clone(),
531 impl_where_bound_count,
532 parent_host_pred,
533 ));
534 impl_where_bound_count += 1;
535 }
536 (ChildMode::PassThrough, _)
537 | (_, GoalSource::AliasWellFormed | GoalSource::AliasBoundConstCondition) => {
538 obligation = make_obligation(self.obligation.cause.clone());
539 }
540 }
541
542 self.with_derived_obligation(obligation, |this| nested_goal.visit_with(this))?;
543 }
544
545 if let Some(ty::PredicateKind::AliasRelate(lhs, rhs, _)) = pred.kind().no_bound_vars() {
548 goal.infcx().visit_proof_tree_at_depth(
549 goal.goal().with(tcx, ty::ClauseKind::WellFormed(lhs)),
550 goal.depth() + 1,
551 self,
552 )?;
553 goal.infcx().visit_proof_tree_at_depth(
554 goal.goal().with(tcx, ty::ClauseKind::WellFormed(rhs)),
555 goal.depth() + 1,
556 self,
557 )?;
558 }
559
560 self.detect_trait_error_in_higher_ranked_projection(goal)?;
561
562 ControlFlow::Break(self.obligation.clone())
563 }
564}
565
566#[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)]
567enum ChildMode<'tcx> {
568 Trait(ty::PolyTraitPredicate<'tcx>),
572 Host(ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>),
576 PassThrough,
580}
581
582fn derive_cause<'tcx>(
583 tcx: TyCtxt<'tcx>,
584 candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
585 mut cause: ObligationCause<'tcx>,
586 idx: usize,
587 parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
588) -> ObligationCause<'tcx> {
589 match candidate_kind {
590 inspect::ProbeKind::TraitCandidate {
591 source: CandidateSource::Impl(impl_def_id),
592 result: _,
593 } => {
594 if let Some((_, span)) =
595 tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx)
596 {
597 cause = cause.derived_cause(parent_trait_pred, |derived| {
598 ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
599 derived,
600 impl_or_alias_def_id: impl_def_id,
601 impl_def_predicate_index: Some(idx),
602 span,
603 }))
604 })
605 }
606 }
607 inspect::ProbeKind::TraitCandidate {
608 source: CandidateSource::BuiltinImpl(..),
609 result: _,
610 } => {
611 cause = cause.derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerived);
612 }
613 _ => {}
614 };
615 cause
616}
617
618fn derive_host_cause<'tcx>(
619 tcx: TyCtxt<'tcx>,
620 candidate_kind: inspect::ProbeKind<TyCtxt<'tcx>>,
621 mut cause: ObligationCause<'tcx>,
622 idx: usize,
623 parent_host_pred: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
624) -> ObligationCause<'tcx> {
625 match candidate_kind {
626 inspect::ProbeKind::TraitCandidate {
627 source: CandidateSource::Impl(impl_def_id),
628 result: _,
629 } => {
630 if let Some((_, span)) = tcx
631 .predicates_of(impl_def_id)
632 .instantiate_identity(tcx)
633 .into_iter()
634 .chain(tcx.const_conditions(impl_def_id).instantiate_identity(tcx).into_iter().map(
635 |(trait_ref, span)| {
636 (
637 trait_ref.to_host_effect_clause(
638 tcx,
639 parent_host_pred.skip_binder().constness,
640 ),
641 span,
642 )
643 },
644 ))
645 .nth(idx)
646 {
647 cause =
648 cause.derived_host_cause(parent_host_pred, |derived| {
649 ObligationCauseCode::ImplDerivedHost(Box::new(
650 traits::ImplDerivedHostCause { derived, impl_def_id, span },
651 ))
652 })
653 }
654 }
655 inspect::ProbeKind::TraitCandidate {
656 source: CandidateSource::BuiltinImpl(..),
657 result: _,
658 } => {
659 cause =
660 cause.derived_host_cause(parent_host_pred, ObligationCauseCode::BuiltinDerivedHost);
661 }
662 _ => {}
663 };
664 cause
665}