1pub(super) mod structural_traits;
4
5use std::cell::Cell;
6use std::ops::ControlFlow;
7
8use derive_where::derive_where;
9use rustc_type_ir::inherent::*;
10use rustc_type_ir::lang_items::SolverTraitLangItem;
11use rustc_type_ir::search_graph::CandidateHeadUsages;
12use rustc_type_ir::solve::{AliasBoundKind, SizedTraitKind};
13use rustc_type_ir::{
14 self as ty, Interner, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable,
15 TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Upcast,
16 elaborate,
17};
18use tracing::{debug, instrument};
19
20use super::trait_goals::TraitGoalProvenVia;
21use super::{has_only_region_constraints, inspect};
22use crate::delegate::SolverDelegate;
23use crate::solve::inspect::ProbeKind;
24use crate::solve::{
25 BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
26 MaybeCause, NoSolution, OpaqueTypesJank, ParamEnvSource, QueryResult,
27 has_no_inference_or_external_constraints,
28};
29
30#[automatically_derived]
impl<I: Interner> ::core::fmt::Debug for Candidate<I> where I: Interner {
fn fmt(&self, __f: &mut ::core::fmt::Formatter<'_>)
-> ::core::fmt::Result {
match self {
Candidate {
source: ref __field_source,
result: ref __field_result,
head_usages: ref __field_head_usages } => {
let mut __builder =
::core::fmt::Formatter::debug_struct(__f, "Candidate");
::core::fmt::DebugStruct::field(&mut __builder, "source",
__field_source);
::core::fmt::DebugStruct::field(&mut __builder, "result",
__field_result);
::core::fmt::DebugStruct::field(&mut __builder, "head_usages",
__field_head_usages);
::core::fmt::DebugStruct::finish(&mut __builder)
}
}
}
}#[derive_where(Debug; I: Interner)]
35pub(super) struct Candidate<I: Interner> {
36 pub(super) source: CandidateSource<I>,
37 pub(super) result: CanonicalResponse<I>,
38 pub(super) head_usages: CandidateHeadUsages,
39}
40
41pub(super) trait GoalKind<D, I = <D as SolverDelegate>::Interner>:
43 TypeFoldable<I> + Copy + Eq + std::fmt::Display
44where
45 D: SolverDelegate<Interner = I>,
46 I: Interner,
47{
48 fn self_ty(self) -> I::Ty;
49
50 fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
51
52 fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
53
54 fn trait_def_id(self, cx: I) -> I::TraitId;
55
56 fn probe_and_consider_implied_clause(
60 ecx: &mut EvalCtxt<'_, D>,
61 parent_source: CandidateSource<I>,
62 goal: Goal<I, Self>,
63 assumption: I::Clause,
64 requirements: impl IntoIterator<Item = (GoalSource, Goal<I, I::Predicate>)>,
65 ) -> Result<Candidate<I>, NoSolution> {
66 Self::probe_and_match_goal_against_assumption(ecx, parent_source, goal, assumption, |ecx| {
67 for (nested_source, goal) in requirements {
68 ecx.add_goal(nested_source, goal);
69 }
70 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
71 })
72 }
73
74 fn probe_and_consider_object_bound_candidate(
80 ecx: &mut EvalCtxt<'_, D>,
81 source: CandidateSource<I>,
82 goal: Goal<I, Self>,
83 assumption: I::Clause,
84 ) -> Result<Candidate<I>, NoSolution> {
85 Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
86 let cx = ecx.cx();
87 let ty::Dynamic(bounds, _) = goal.predicate.self_ty().kind() else {
88 {
::core::panicking::panic_fmt(format_args!("expected object type in `probe_and_consider_object_bound_candidate`"));
};panic!("expected object type in `probe_and_consider_object_bound_candidate`");
89 };
90 match structural_traits::predicates_for_object_candidate(
91 ecx,
92 goal.param_env,
93 goal.predicate.trait_ref(cx),
94 bounds,
95 ) {
96 Ok(requirements) => {
97 ecx.add_goals(GoalSource::ImplWhereBound, requirements);
98 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
99 }
100 Err(_) => {
101 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
102 }
103 }
104 })
105 }
106
107 fn consider_additional_alias_assumptions(
111 ecx: &mut EvalCtxt<'_, D>,
112 goal: Goal<I, Self>,
113 alias_ty: ty::AliasTy<I>,
114 ) -> Vec<Candidate<I>>;
115
116 fn probe_and_consider_param_env_candidate(
117 ecx: &mut EvalCtxt<'_, D>,
118 goal: Goal<I, Self>,
119 assumption: I::Clause,
120 ) -> Result<Candidate<I>, CandidateHeadUsages> {
121 match Self::fast_reject_assumption(ecx, goal, assumption) {
122 Ok(()) => {}
123 Err(NoSolution) => return Err(CandidateHeadUsages::default()),
124 }
125
126 let source = Cell::new(CandidateSource::ParamEnv(ParamEnvSource::Global));
133 let (result, head_usages) = ecx
134 .probe(|result: &QueryResult<I>| inspect::ProbeKind::TraitCandidate {
135 source: source.get(),
136 result: *result,
137 })
138 .enter_single_candidate(|ecx| {
139 Self::match_assumption(ecx, goal, assumption, |ecx| {
140 ecx.try_evaluate_added_goals()?;
141 let (src, certainty) =
142 ecx.characterize_param_env_assumption(goal.param_env, assumption)?;
143 source.set(src);
144 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
145 })
146 });
147
148 match result {
149 Ok(result) => Ok(Candidate { source: source.get(), result, head_usages }),
150 Err(NoSolution) => Err(head_usages),
151 }
152 }
153
154 fn probe_and_match_goal_against_assumption(
159 ecx: &mut EvalCtxt<'_, D>,
160 source: CandidateSource<I>,
161 goal: Goal<I, Self>,
162 assumption: I::Clause,
163 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
164 ) -> Result<Candidate<I>, NoSolution> {
165 Self::fast_reject_assumption(ecx, goal, assumption)?;
166
167 ecx.probe_trait_candidate(source)
168 .enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
169 }
170
171 fn fast_reject_assumption(
174 ecx: &mut EvalCtxt<'_, D>,
175 goal: Goal<I, Self>,
176 assumption: I::Clause,
177 ) -> Result<(), NoSolution>;
178
179 fn match_assumption(
181 ecx: &mut EvalCtxt<'_, D>,
182 goal: Goal<I, Self>,
183 assumption: I::Clause,
184 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
185 ) -> QueryResult<I>;
186
187 fn consider_impl_candidate(
188 ecx: &mut EvalCtxt<'_, D>,
189 goal: Goal<I, Self>,
190 impl_def_id: I::ImplId,
191 then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResult<I>,
192 ) -> Result<Candidate<I>, NoSolution>;
193
194 fn consider_error_guaranteed_candidate(
201 ecx: &mut EvalCtxt<'_, D>,
202 guar: I::ErrorGuaranteed,
203 ) -> Result<Candidate<I>, NoSolution>;
204
205 fn consider_auto_trait_candidate(
210 ecx: &mut EvalCtxt<'_, D>,
211 goal: Goal<I, Self>,
212 ) -> Result<Candidate<I>, NoSolution>;
213
214 fn consider_trait_alias_candidate(
216 ecx: &mut EvalCtxt<'_, D>,
217 goal: Goal<I, Self>,
218 ) -> Result<Candidate<I>, NoSolution>;
219
220 fn consider_builtin_sizedness_candidates(
226 ecx: &mut EvalCtxt<'_, D>,
227 goal: Goal<I, Self>,
228 sizedness: SizedTraitKind,
229 ) -> Result<Candidate<I>, NoSolution>;
230
231 fn consider_builtin_copy_clone_candidate(
236 ecx: &mut EvalCtxt<'_, D>,
237 goal: Goal<I, Self>,
238 ) -> Result<Candidate<I>, NoSolution>;
239
240 fn consider_builtin_fn_ptr_trait_candidate(
242 ecx: &mut EvalCtxt<'_, D>,
243 goal: Goal<I, Self>,
244 ) -> Result<Candidate<I>, NoSolution>;
245
246 fn consider_builtin_fn_trait_candidates(
249 ecx: &mut EvalCtxt<'_, D>,
250 goal: Goal<I, Self>,
251 kind: ty::ClosureKind,
252 ) -> Result<Candidate<I>, NoSolution>;
253
254 fn consider_builtin_async_fn_trait_candidates(
257 ecx: &mut EvalCtxt<'_, D>,
258 goal: Goal<I, Self>,
259 kind: ty::ClosureKind,
260 ) -> Result<Candidate<I>, NoSolution>;
261
262 fn consider_builtin_async_fn_kind_helper_candidate(
266 ecx: &mut EvalCtxt<'_, D>,
267 goal: Goal<I, Self>,
268 ) -> Result<Candidate<I>, NoSolution>;
269
270 fn consider_builtin_tuple_candidate(
272 ecx: &mut EvalCtxt<'_, D>,
273 goal: Goal<I, Self>,
274 ) -> Result<Candidate<I>, NoSolution>;
275
276 fn consider_builtin_pointee_candidate(
282 ecx: &mut EvalCtxt<'_, D>,
283 goal: Goal<I, Self>,
284 ) -> Result<Candidate<I>, NoSolution>;
285
286 fn consider_builtin_future_candidate(
290 ecx: &mut EvalCtxt<'_, D>,
291 goal: Goal<I, Self>,
292 ) -> Result<Candidate<I>, NoSolution>;
293
294 fn consider_builtin_iterator_candidate(
298 ecx: &mut EvalCtxt<'_, D>,
299 goal: Goal<I, Self>,
300 ) -> Result<Candidate<I>, NoSolution>;
301
302 fn consider_builtin_fused_iterator_candidate(
305 ecx: &mut EvalCtxt<'_, D>,
306 goal: Goal<I, Self>,
307 ) -> Result<Candidate<I>, NoSolution>;
308
309 fn consider_builtin_async_iterator_candidate(
310 ecx: &mut EvalCtxt<'_, D>,
311 goal: Goal<I, Self>,
312 ) -> Result<Candidate<I>, NoSolution>;
313
314 fn consider_builtin_coroutine_candidate(
318 ecx: &mut EvalCtxt<'_, D>,
319 goal: Goal<I, Self>,
320 ) -> Result<Candidate<I>, NoSolution>;
321
322 fn consider_builtin_discriminant_kind_candidate(
323 ecx: &mut EvalCtxt<'_, D>,
324 goal: Goal<I, Self>,
325 ) -> Result<Candidate<I>, NoSolution>;
326
327 fn consider_builtin_destruct_candidate(
328 ecx: &mut EvalCtxt<'_, D>,
329 goal: Goal<I, Self>,
330 ) -> Result<Candidate<I>, NoSolution>;
331
332 fn consider_builtin_transmute_candidate(
333 ecx: &mut EvalCtxt<'_, D>,
334 goal: Goal<I, Self>,
335 ) -> Result<Candidate<I>, NoSolution>;
336
337 fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
338 ecx: &mut EvalCtxt<'_, D>,
339 goal: Goal<I, Self>,
340 ) -> Result<Candidate<I>, NoSolution>;
341
342 fn consider_structural_builtin_unsize_candidates(
350 ecx: &mut EvalCtxt<'_, D>,
351 goal: Goal<I, Self>,
352 ) -> Vec<Candidate<I>>;
353
354 fn consider_builtin_field_candidate(
355 ecx: &mut EvalCtxt<'_, D>,
356 goal: Goal<I, Self>,
357 ) -> Result<Candidate<I>, NoSolution>;
358}
359
360pub(super) enum AssembleCandidatesFrom {
368 All,
369 EnvAndBounds,
373}
374
375impl AssembleCandidatesFrom {
376 fn should_assemble_impl_candidates(&self) -> bool {
377 match self {
378 AssembleCandidatesFrom::All => true,
379 AssembleCandidatesFrom::EnvAndBounds => false,
380 }
381 }
382}
383
384#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FailedCandidateInfo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"FailedCandidateInfo", "param_env_head_usages",
&&self.param_env_head_usages)
}
}Debug)]
393pub(super) struct FailedCandidateInfo {
394 pub param_env_head_usages: CandidateHeadUsages,
395}
396
397impl<D, I> EvalCtxt<'_, D>
398where
399 D: SolverDelegate<Interner = I>,
400 I: Interner,
401{
402 pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
403 &mut self,
404 goal: Goal<I, G>,
405 assemble_from: AssembleCandidatesFrom,
406 ) -> (Vec<Candidate<I>>, FailedCandidateInfo) {
407 let mut candidates = ::alloc::vec::Vec::new()vec![];
408 let mut failed_candidate_info =
409 FailedCandidateInfo { param_env_head_usages: CandidateHeadUsages::default() };
410 let Ok(normalized_self_ty) =
411 self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
412 else {
413 return (candidates, failed_candidate_info);
414 };
415
416 let goal: Goal<I, G> = goal
417 .with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
418
419 if normalized_self_ty.is_ty_var() {
420 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs:420",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(420u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("self type has been normalized to infer")
as &dyn Value))])
});
} else { ; }
};debug!("self type has been normalized to infer");
421 self.try_assemble_bounds_via_registered_opaques(goal, assemble_from, &mut candidates);
422 return (candidates, failed_candidate_info);
423 }
424
425 let goal = self.resolve_vars_if_possible(goal);
428
429 if let TypingMode::Coherence = self.typing_mode()
430 && let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
431 {
432 candidates.push(candidate);
433 return (candidates, failed_candidate_info);
434 }
435
436 self.assemble_alias_bound_candidates(goal, &mut candidates);
437 self.assemble_param_env_candidates(goal, &mut candidates, &mut failed_candidate_info);
438
439 match assemble_from {
440 AssembleCandidatesFrom::All => {
441 self.assemble_builtin_impl_candidates(goal, &mut candidates);
442 if TypingMode::Coherence == self.typing_mode()
454 || !candidates.iter().any(|c| {
455 #[allow(non_exhaustive_omitted_patterns)] match c.source {
CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) |
CandidateSource::AliasBound(_) => true,
_ => false,
}matches!(
456 c.source,
457 CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
458 | CandidateSource::AliasBound(_)
459 ) && has_no_inference_or_external_constraints(c.result)
460 })
461 {
462 self.assemble_impl_candidates(goal, &mut candidates);
463 self.assemble_object_bound_candidates(goal, &mut candidates);
464 }
465 }
466 AssembleCandidatesFrom::EnvAndBounds => {
467 if #[allow(non_exhaustive_omitted_patterns)] match normalized_self_ty.kind() {
ty::Dynamic(..) => true,
_ => false,
}matches!(normalized_self_ty.kind(), ty::Dynamic(..))
471 && !candidates.iter().any(|c| #[allow(non_exhaustive_omitted_patterns)] match c.source {
CandidateSource::ParamEnv(_) => true,
_ => false,
}matches!(c.source, CandidateSource::ParamEnv(_)))
472 {
473 self.assemble_object_bound_candidates(goal, &mut candidates);
474 }
475 }
476 }
477
478 (candidates, failed_candidate_info)
479 }
480
481 pub(super) fn forced_ambiguity(
482 &mut self,
483 cause: MaybeCause,
484 ) -> Result<Candidate<I>, NoSolution> {
485 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
494 let certainty = Certainty::Maybe { cause, opaque_types_jank: OpaqueTypesJank::AllGood };
495 self.probe_trait_candidate(source)
496 .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
497 }
498
499 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("assemble_impl_candidates",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(499u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let cx = self.cx();
cx.for_each_relevant_impl(goal.predicate.trait_def_id(cx),
goal.predicate.self_ty(),
|impl_def_id|
{
if cx.impl_is_default(impl_def_id) { return; }
match G::consider_impl_candidate(self, goal, impl_def_id,
|ecx, certainty|
{
ecx.evaluate_added_goals_and_make_canonical_response(certainty)
}) {
Ok(candidate) => candidates.push(candidate),
Err(NoSolution) => (),
}
});
}
}
}#[instrument(level = "trace", skip_all)]
500 fn assemble_impl_candidates<G: GoalKind<D>>(
501 &mut self,
502 goal: Goal<I, G>,
503 candidates: &mut Vec<Candidate<I>>,
504 ) {
505 let cx = self.cx();
506 cx.for_each_relevant_impl(
507 goal.predicate.trait_def_id(cx),
508 goal.predicate.self_ty(),
509 |impl_def_id| {
510 if cx.impl_is_default(impl_def_id) {
514 return;
515 }
516 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
517 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
518 }) {
519 Ok(candidate) => candidates.push(candidate),
520 Err(NoSolution) => (),
521 }
522 },
523 );
524 }
525
526 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("assemble_builtin_impl_candidates",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(526u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let cx = self.cx();
let trait_def_id = goal.predicate.trait_def_id(cx);
let result =
if let Err(guar) = goal.predicate.error_reported() {
G::consider_error_guaranteed_candidate(self, guar)
} else if cx.trait_is_auto(trait_def_id) {
G::consider_auto_trait_candidate(self, goal)
} else if cx.trait_is_alias(trait_def_id) {
G::consider_trait_alias_candidate(self, goal)
} else {
match cx.as_trait_lang_item(trait_def_id) {
Some(SolverTraitLangItem::Sized) => {
G::consider_builtin_sizedness_candidates(self, goal,
SizedTraitKind::Sized)
}
Some(SolverTraitLangItem::MetaSized) => {
G::consider_builtin_sizedness_candidates(self, goal,
SizedTraitKind::MetaSized)
}
Some(SolverTraitLangItem::PointeeSized) => {
{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`PointeeSized` is removed during lowering")));
};
}
Some(SolverTraitLangItem::Copy | SolverTraitLangItem::Clone
| SolverTraitLangItem::TrivialClone) =>
G::consider_builtin_copy_clone_candidate(self, goal),
Some(SolverTraitLangItem::Fn) => {
G::consider_builtin_fn_trait_candidates(self, goal,
ty::ClosureKind::Fn)
}
Some(SolverTraitLangItem::FnMut) => {
G::consider_builtin_fn_trait_candidates(self, goal,
ty::ClosureKind::FnMut)
}
Some(SolverTraitLangItem::FnOnce) => {
G::consider_builtin_fn_trait_candidates(self, goal,
ty::ClosureKind::FnOnce)
}
Some(SolverTraitLangItem::AsyncFn) => {
G::consider_builtin_async_fn_trait_candidates(self, goal,
ty::ClosureKind::Fn)
}
Some(SolverTraitLangItem::AsyncFnMut) => {
G::consider_builtin_async_fn_trait_candidates(self, goal,
ty::ClosureKind::FnMut)
}
Some(SolverTraitLangItem::AsyncFnOnce) => {
G::consider_builtin_async_fn_trait_candidates(self, goal,
ty::ClosureKind::FnOnce)
}
Some(SolverTraitLangItem::FnPtrTrait) => {
G::consider_builtin_fn_ptr_trait_candidate(self, goal)
}
Some(SolverTraitLangItem::AsyncFnKindHelper) => {
G::consider_builtin_async_fn_kind_helper_candidate(self,
goal)
}
Some(SolverTraitLangItem::Tuple) =>
G::consider_builtin_tuple_candidate(self, goal),
Some(SolverTraitLangItem::PointeeTrait) => {
G::consider_builtin_pointee_candidate(self, goal)
}
Some(SolverTraitLangItem::Future) => {
G::consider_builtin_future_candidate(self, goal)
}
Some(SolverTraitLangItem::Iterator) => {
G::consider_builtin_iterator_candidate(self, goal)
}
Some(SolverTraitLangItem::FusedIterator) => {
G::consider_builtin_fused_iterator_candidate(self, goal)
}
Some(SolverTraitLangItem::AsyncIterator) => {
G::consider_builtin_async_iterator_candidate(self, goal)
}
Some(SolverTraitLangItem::Coroutine) => {
G::consider_builtin_coroutine_candidate(self, goal)
}
Some(SolverTraitLangItem::DiscriminantKind) => {
G::consider_builtin_discriminant_kind_candidate(self, goal)
}
Some(SolverTraitLangItem::Destruct) => {
G::consider_builtin_destruct_candidate(self, goal)
}
Some(SolverTraitLangItem::TransmuteTrait) => {
G::consider_builtin_transmute_candidate(self, goal)
}
Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self,
goal)
}
Some(SolverTraitLangItem::Field) =>
G::consider_builtin_field_candidate(self, goal),
_ => Err(NoSolution),
}
};
candidates.extend(result);
if cx.is_trait_lang_item(trait_def_id,
SolverTraitLangItem::Unsize) {
candidates.extend(G::consider_structural_builtin_unsize_candidates(self,
goal));
}
}
}
}#[instrument(level = "trace", skip_all)]
527 fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
528 &mut self,
529 goal: Goal<I, G>,
530 candidates: &mut Vec<Candidate<I>>,
531 ) {
532 let cx = self.cx();
533 let trait_def_id = goal.predicate.trait_def_id(cx);
534
535 let result = if let Err(guar) = goal.predicate.error_reported() {
543 G::consider_error_guaranteed_candidate(self, guar)
544 } else if cx.trait_is_auto(trait_def_id) {
545 G::consider_auto_trait_candidate(self, goal)
546 } else if cx.trait_is_alias(trait_def_id) {
547 G::consider_trait_alias_candidate(self, goal)
548 } else {
549 match cx.as_trait_lang_item(trait_def_id) {
550 Some(SolverTraitLangItem::Sized) => {
551 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
552 }
553 Some(SolverTraitLangItem::MetaSized) => {
554 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
555 }
556 Some(SolverTraitLangItem::PointeeSized) => {
557 unreachable!("`PointeeSized` is removed during lowering");
558 }
559 Some(
560 SolverTraitLangItem::Copy
561 | SolverTraitLangItem::Clone
562 | SolverTraitLangItem::TrivialClone,
563 ) => G::consider_builtin_copy_clone_candidate(self, goal),
564 Some(SolverTraitLangItem::Fn) => {
565 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
566 }
567 Some(SolverTraitLangItem::FnMut) => {
568 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
569 }
570 Some(SolverTraitLangItem::FnOnce) => {
571 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
572 }
573 Some(SolverTraitLangItem::AsyncFn) => {
574 G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
575 }
576 Some(SolverTraitLangItem::AsyncFnMut) => {
577 G::consider_builtin_async_fn_trait_candidates(
578 self,
579 goal,
580 ty::ClosureKind::FnMut,
581 )
582 }
583 Some(SolverTraitLangItem::AsyncFnOnce) => {
584 G::consider_builtin_async_fn_trait_candidates(
585 self,
586 goal,
587 ty::ClosureKind::FnOnce,
588 )
589 }
590 Some(SolverTraitLangItem::FnPtrTrait) => {
591 G::consider_builtin_fn_ptr_trait_candidate(self, goal)
592 }
593 Some(SolverTraitLangItem::AsyncFnKindHelper) => {
594 G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
595 }
596 Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
597 Some(SolverTraitLangItem::PointeeTrait) => {
598 G::consider_builtin_pointee_candidate(self, goal)
599 }
600 Some(SolverTraitLangItem::Future) => {
601 G::consider_builtin_future_candidate(self, goal)
602 }
603 Some(SolverTraitLangItem::Iterator) => {
604 G::consider_builtin_iterator_candidate(self, goal)
605 }
606 Some(SolverTraitLangItem::FusedIterator) => {
607 G::consider_builtin_fused_iterator_candidate(self, goal)
608 }
609 Some(SolverTraitLangItem::AsyncIterator) => {
610 G::consider_builtin_async_iterator_candidate(self, goal)
611 }
612 Some(SolverTraitLangItem::Coroutine) => {
613 G::consider_builtin_coroutine_candidate(self, goal)
614 }
615 Some(SolverTraitLangItem::DiscriminantKind) => {
616 G::consider_builtin_discriminant_kind_candidate(self, goal)
617 }
618 Some(SolverTraitLangItem::Destruct) => {
619 G::consider_builtin_destruct_candidate(self, goal)
620 }
621 Some(SolverTraitLangItem::TransmuteTrait) => {
622 G::consider_builtin_transmute_candidate(self, goal)
623 }
624 Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
625 G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
626 }
627 Some(SolverTraitLangItem::Field) => G::consider_builtin_field_candidate(self, goal),
628 _ => Err(NoSolution),
629 }
630 };
631
632 candidates.extend(result);
633
634 if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
637 candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
638 }
639 }
640
641 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("assemble_param_env_candidates",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(641u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
for assumption in goal.param_env.caller_bounds().iter() {
match G::probe_and_consider_param_env_candidate(self, goal,
assumption) {
Ok(candidate) => candidates.push(candidate),
Err(head_usages) => {
failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
}
}
}
}
}
}#[instrument(level = "trace", skip_all)]
642 fn assemble_param_env_candidates<G: GoalKind<D>>(
643 &mut self,
644 goal: Goal<I, G>,
645 candidates: &mut Vec<Candidate<I>>,
646 failed_candidate_info: &mut FailedCandidateInfo,
647 ) {
648 for assumption in goal.param_env.caller_bounds().iter() {
649 match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
650 Ok(candidate) => candidates.push(candidate),
651 Err(head_usages) => {
652 failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
653 }
654 }
655 }
656 }
657
658 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("assemble_alias_bound_candidates",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(658u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let () =
self.probe(|_|
ProbeKind::NormalizedSelfTyAssembly).enter(|ecx|
{
ecx.assemble_alias_bound_candidates_recur(goal.predicate.self_ty(),
goal, candidates, AliasBoundKind::SelfBounds);
});
}
}
}#[instrument(level = "trace", skip_all)]
659 fn assemble_alias_bound_candidates<G: GoalKind<D>>(
660 &mut self,
661 goal: Goal<I, G>,
662 candidates: &mut Vec<Candidate<I>>,
663 ) {
664 let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
665 ecx.assemble_alias_bound_candidates_recur(
666 goal.predicate.self_ty(),
667 goal,
668 candidates,
669 AliasBoundKind::SelfBounds,
670 );
671 });
672 }
673
674 fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
684 &mut self,
685 self_ty: I::Ty,
686 goal: Goal<I, G>,
687 candidates: &mut Vec<Candidate<I>>,
688 consider_self_bounds: AliasBoundKind,
689 ) {
690 let (kind, alias_ty) = match self_ty.kind() {
691 ty::Bool
692 | ty::Char
693 | ty::Int(_)
694 | ty::Uint(_)
695 | ty::Float(_)
696 | ty::Adt(_, _)
697 | ty::Foreign(_)
698 | ty::Str
699 | ty::Array(_, _)
700 | ty::Pat(_, _)
701 | ty::Slice(_)
702 | ty::RawPtr(_, _)
703 | ty::Ref(_, _, _)
704 | ty::FnDef(_, _)
705 | ty::FnPtr(..)
706 | ty::UnsafeBinder(_)
707 | ty::Dynamic(..)
708 | ty::Closure(..)
709 | ty::CoroutineClosure(..)
710 | ty::Coroutine(..)
711 | ty::CoroutineWitness(..)
712 | ty::Never
713 | ty::Tuple(_)
714 | ty::Param(_)
715 | ty::Placeholder(..)
716 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
717 | ty::Error(_) => return,
718 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
719 {
::core::panicking::panic_fmt(format_args!("unexpected self type for `{0:?}`",
goal));
}panic!("unexpected self type for `{goal:?}`")
720 }
721
722 ty::Infer(ty::TyVar(_)) => {
723 if let Ok(result) =
727 self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
728 {
729 candidates.push(Candidate {
730 source: CandidateSource::AliasBound(consider_self_bounds),
731 result,
732 head_usages: CandidateHeadUsages::default(),
733 });
734 }
735 return;
736 }
737
738 ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
739 ty::Alias(ty::Inherent | ty::Free, _) => {
740 self.cx().delay_bug(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("could not normalize {0:?}, it is not WF",
self_ty))
})format!("could not normalize {self_ty:?}, it is not WF"));
741 return;
742 }
743 };
744
745 match consider_self_bounds {
746 AliasBoundKind::SelfBounds => {
747 for assumption in self
748 .cx()
749 .item_self_bounds(alias_ty.def_id)
750 .iter_instantiated(self.cx(), alias_ty.args)
751 {
752 candidates.extend(G::probe_and_consider_implied_clause(
753 self,
754 CandidateSource::AliasBound(consider_self_bounds),
755 goal,
756 assumption,
757 [],
758 ));
759 }
760 }
761 AliasBoundKind::NonSelfBounds => {
762 for assumption in self
763 .cx()
764 .item_non_self_bounds(alias_ty.def_id)
765 .iter_instantiated(self.cx(), alias_ty.args)
766 {
767 candidates.extend(G::probe_and_consider_implied_clause(
768 self,
769 CandidateSource::AliasBound(consider_self_bounds),
770 goal,
771 assumption,
772 [],
773 ));
774 }
775 }
776 }
777
778 candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
779
780 if kind != ty::Projection {
781 return;
782 }
783
784 match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
786 Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
787 next_self_ty,
788 goal,
789 candidates,
790 AliasBoundKind::NonSelfBounds,
791 ),
792 Err(NoSolution) => {}
793 }
794 }
795
796 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("assemble_object_bound_candidates",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(796u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let cx = self.cx();
if cx.is_sizedness_trait(goal.predicate.trait_def_id(cx)) {
return;
}
let self_ty = goal.predicate.self_ty();
let bounds =
match self_ty.kind() {
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) |
ty::Float(_) | ty::Adt(_, _) | ty::Foreign(_) | ty::Str |
ty::Array(_, _) | ty::Pat(_, _) | ty::Slice(_) |
ty::RawPtr(_, _) | ty::Ref(_, _, _) | ty::FnDef(_, _) |
ty::FnPtr(..) | ty::UnsafeBinder(_) | ty::Alias(..) |
ty::Closure(..) | ty::CoroutineClosure(..) |
ty::Coroutine(..) | ty::CoroutineWitness(..) | ty::Never |
ty::Tuple(_) | ty::Param(_) | ty::Placeholder(..) |
ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) | ty::Error(_) =>
return,
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_)
| ty::FreshFloatTy(_)) | ty::Bound(..) => {
::core::panicking::panic_fmt(format_args!("unexpected self type for `{0:?}`",
goal));
}
ty::Dynamic(bounds, ..) => bounds,
};
if bounds.principal_def_id().is_some_and(|def_id|
!cx.trait_is_dyn_compatible(def_id)) {
return;
}
for bound in bounds.iter() {
match bound.skip_binder() {
ty::ExistentialPredicate::Trait(_) => {}
ty::ExistentialPredicate::Projection(_) |
ty::ExistentialPredicate::AutoTrait(_) => {
candidates.extend(G::probe_and_consider_object_bound_candidate(self,
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc), goal,
bound.with_self_ty(cx, self_ty)));
}
}
}
if let Some(principal) = bounds.principal() {
let principal_trait_ref = principal.with_self_ty(cx, self_ty);
for (idx, assumption) in
elaborate::supertraits(cx, principal_trait_ref).enumerate()
{
candidates.extend(G::probe_and_consider_object_bound_candidate(self,
CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
goal, assumption.upcast(cx)));
}
}
}
}
}#[instrument(level = "trace", skip_all)]
797 fn assemble_object_bound_candidates<G: GoalKind<D>>(
798 &mut self,
799 goal: Goal<I, G>,
800 candidates: &mut Vec<Candidate<I>>,
801 ) {
802 let cx = self.cx();
803 if cx.is_sizedness_trait(goal.predicate.trait_def_id(cx)) {
804 return;
807 }
808
809 let self_ty = goal.predicate.self_ty();
810 let bounds = match self_ty.kind() {
811 ty::Bool
812 | ty::Char
813 | ty::Int(_)
814 | ty::Uint(_)
815 | ty::Float(_)
816 | ty::Adt(_, _)
817 | ty::Foreign(_)
818 | ty::Str
819 | ty::Array(_, _)
820 | ty::Pat(_, _)
821 | ty::Slice(_)
822 | ty::RawPtr(_, _)
823 | ty::Ref(_, _, _)
824 | ty::FnDef(_, _)
825 | ty::FnPtr(..)
826 | ty::UnsafeBinder(_)
827 | ty::Alias(..)
828 | ty::Closure(..)
829 | ty::CoroutineClosure(..)
830 | ty::Coroutine(..)
831 | ty::CoroutineWitness(..)
832 | ty::Never
833 | ty::Tuple(_)
834 | ty::Param(_)
835 | ty::Placeholder(..)
836 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
837 | ty::Error(_) => return,
838 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
839 | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
840 ty::Dynamic(bounds, ..) => bounds,
841 };
842
843 if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
845 return;
846 }
847
848 for bound in bounds.iter() {
852 match bound.skip_binder() {
853 ty::ExistentialPredicate::Trait(_) => {
854 }
856 ty::ExistentialPredicate::Projection(_)
857 | ty::ExistentialPredicate::AutoTrait(_) => {
858 candidates.extend(G::probe_and_consider_object_bound_candidate(
859 self,
860 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
861 goal,
862 bound.with_self_ty(cx, self_ty),
863 ));
864 }
865 }
866 }
867
868 if let Some(principal) = bounds.principal() {
872 let principal_trait_ref = principal.with_self_ty(cx, self_ty);
873 for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
874 candidates.extend(G::probe_and_consider_object_bound_candidate(
875 self,
876 CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
877 goal,
878 assumption.upcast(cx),
879 ));
880 }
881 }
882 }
883
884 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("consider_coherence_unknowable_candidate",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(890u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Result<Candidate<I>, NoSolution> =
loop {};
return __tracing_attr_fake_return;
}
{
self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx|
{
let cx = ecx.cx();
let trait_ref = goal.predicate.trait_ref(cx);
if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
Err(NoSolution)
} else {
let predicate: I::Predicate = trait_ref.upcast(cx);
ecx.add_goals(GoalSource::Misc,
elaborate::elaborate(cx,
[predicate]).skip(1).map(|predicate|
goal.with(cx, predicate)));
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}
})
}
}
}#[instrument(level = "trace", skip_all)]
891 fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
892 &mut self,
893 goal: Goal<I, G>,
894 ) -> Result<Candidate<I>, NoSolution> {
895 self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
896 let cx = ecx.cx();
897 let trait_ref = goal.predicate.trait_ref(cx);
898 if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
899 Err(NoSolution)
900 } else {
901 let predicate: I::Predicate = trait_ref.upcast(cx);
907 ecx.add_goals(
908 GoalSource::Misc,
909 elaborate::elaborate(cx, [predicate])
910 .skip(1)
911 .map(|predicate| goal.with(cx, predicate)),
912 );
913 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
914 }
915 })
916 }
917}
918
919pub(super) enum AllowInferenceConstraints {
920 Yes,
921 No,
922}
923
924impl<D, I> EvalCtxt<'_, D>
925where
926 D: SolverDelegate<Interner = I>,
927 I: Interner,
928{
929 pub(super) fn filter_specialized_impls(
933 &mut self,
934 allow_inference_constraints: AllowInferenceConstraints,
935 candidates: &mut Vec<Candidate<I>>,
936 ) {
937 match self.typing_mode() {
938 TypingMode::Coherence => return,
939 TypingMode::Analysis { .. }
940 | TypingMode::Borrowck { .. }
941 | TypingMode::PostBorrowckAnalysis { .. }
942 | TypingMode::PostAnalysis => {}
943 }
944
945 let mut i = 0;
946 'outer: while i < candidates.len() {
947 let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
948 i += 1;
949 continue;
950 };
951
952 for (j, c) in candidates.iter().enumerate() {
953 if i == j {
954 continue;
955 }
956
957 let CandidateSource::Impl(other_def_id) = c.source else {
958 continue;
959 };
960
961 if #[allow(non_exhaustive_omitted_patterns)] match allow_inference_constraints {
AllowInferenceConstraints::Yes => true,
_ => false,
}matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
968 || has_only_region_constraints(c.result)
969 {
970 if self.cx().impl_specializes(other_def_id, victim_def_id) {
971 candidates.remove(i);
972 continue 'outer;
973 }
974 }
975 }
976
977 i += 1;
978 }
979 }
980
981 fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
993 &mut self,
994 goal: Goal<I, G>,
995 assemble_from: AssembleCandidatesFrom,
996 candidates: &mut Vec<Candidate<I>>,
997 ) {
998 let self_ty = goal.predicate.self_ty();
999 let opaque_types = match self.typing_mode() {
1001 TypingMode::Analysis { .. } => self.opaques_with_sub_unified_hidden_type(self_ty),
1002 TypingMode::Coherence
1003 | TypingMode::Borrowck { .. }
1004 | TypingMode::PostBorrowckAnalysis { .. }
1005 | TypingMode::PostAnalysis => ::alloc::vec::Vec::new()vec![],
1006 };
1007
1008 if opaque_types.is_empty() {
1009 candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
1010 return;
1011 }
1012
1013 for &alias_ty in &opaque_types {
1014 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs:1014",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(1014u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("self ty is sub unified with {0:?}",
alias_ty) as &dyn Value))])
});
} else { ; }
};debug!("self ty is sub unified with {alias_ty:?}");
1015
1016 struct ReplaceOpaque<I: Interner> {
1017 cx: I,
1018 alias_ty: ty::AliasTy<I>,
1019 self_ty: I::Ty,
1020 }
1021 impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
1022 fn cx(&self) -> I {
1023 self.cx
1024 }
1025 fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1026 if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
1027 if alias_ty == self.alias_ty {
1028 return self.self_ty;
1029 }
1030 }
1031 ty.super_fold_with(self)
1032 }
1033 }
1034
1035 for item_bound in self
1043 .cx()
1044 .item_self_bounds(alias_ty.def_id)
1045 .iter_instantiated(self.cx(), alias_ty.args)
1046 {
1047 let assumption =
1048 item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1049 candidates.extend(G::probe_and_match_goal_against_assumption(
1050 self,
1051 CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
1052 goal,
1053 assumption,
1054 |ecx| {
1055 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1058 },
1059 ));
1060 }
1061 }
1062
1063 if assemble_from.should_assemble_impl_candidates() {
1068 let cx = self.cx();
1069 cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1070 if cx.impl_is_default(impl_def_id) {
1074 return;
1075 }
1076
1077 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1078 if ecx.shallow_resolve(self_ty).is_ty_var() {
1079 let certainty = certainty.and(Certainty::AMBIGUOUS);
1081 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1082 } else {
1083 Err(NoSolution)
1089 }
1090 }) {
1091 Ok(candidate) => candidates.push(candidate),
1092 Err(NoSolution) => (),
1093 }
1094 });
1095 }
1096
1097 if candidates.is_empty() {
1098 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
1099 let certainty = Certainty::Maybe {
1100 cause: MaybeCause::Ambiguity,
1101 opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
1102 };
1103 candidates
1104 .extend(self.probe_trait_candidate(source).enter(|this| {
1105 this.evaluate_added_goals_and_make_canonical_response(certainty)
1106 }));
1107 }
1108 }
1109
1110 x;#[instrument(level = "debug", skip_all, fields(proven_via, goal), ret)]
1141 pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1142 &mut self,
1143 proven_via: Option<TraitGoalProvenVia>,
1144 goal: Goal<I, G>,
1145 inject_forced_ambiguity_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> Option<QueryResult<I>>,
1146 inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1147 ) -> QueryResult<I> {
1148 let Some(proven_via) = proven_via else {
1149 return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1156 };
1157
1158 match proven_via {
1159 TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1160 let (mut candidates, _) = self
1164 .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1165 debug!(?candidates);
1166
1167 if candidates.is_empty() {
1170 return inject_normalize_to_rigid_candidate(self);
1171 }
1172
1173 if let Some(result) = inject_forced_ambiguity_candidate(self) {
1176 return result;
1177 }
1178
1179 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1182 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1183 }
1184
1185 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1186 Ok(response)
1187 } else {
1188 self.flounder(&candidates)
1189 }
1190 }
1191 TraitGoalProvenVia::Misc => {
1192 let (mut candidates, _) =
1193 self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1194
1195 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1198 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1199 }
1200
1201 self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1207 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1208 Ok(response)
1209 } else {
1210 self.flounder(&candidates)
1211 }
1212 }
1213 }
1214 }
1215
1216 fn characterize_param_env_assumption(
1230 &mut self,
1231 param_env: I::ParamEnv,
1232 assumption: I::Clause,
1233 ) -> Result<(CandidateSource<I>, Certainty), NoSolution> {
1234 if assumption.has_bound_vars() {
1237 return Ok((CandidateSource::ParamEnv(ParamEnvSource::NonGlobal), Certainty::Yes));
1238 }
1239
1240 match assumption.visit_with(&mut FindParamInClause {
1241 ecx: self,
1242 param_env,
1243 universes: ::alloc::vec::Vec::new()vec![],
1244 recursion_depth: 0,
1245 }) {
1246 ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1247 ControlFlow::Break(Ok(certainty)) => {
1248 Ok((CandidateSource::ParamEnv(ParamEnvSource::NonGlobal), certainty))
1249 }
1250 ControlFlow::Continue(()) => {
1251 Ok((CandidateSource::ParamEnv(ParamEnvSource::Global), Certainty::Yes))
1252 }
1253 }
1254 }
1255}
1256
1257struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1258 ecx: &'a mut EvalCtxt<'b, D>,
1259 param_env: I::ParamEnv,
1260 universes: Vec<Option<ty::UniverseIndex>>,
1261 recursion_depth: usize,
1262}
1263
1264impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1265where
1266 D: SolverDelegate<Interner = I>,
1267 I: Interner,
1268{
1269 type Result = ControlFlow<Result<Certainty, NoSolution>>;
1274
1275 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1276 self.universes.push(None);
1277 t.super_visit_with(self)?;
1278 self.universes.pop();
1279 ControlFlow::Continue(())
1280 }
1281
1282 fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1283 let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1284 let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1285 return ControlFlow::Break(Err(NoSolution));
1286 };
1287
1288 match ty.kind() {
1289 ty::Placeholder(p) => {
1290 if p.universe() == ty::UniverseIndex::ROOT {
1291 ControlFlow::Break(Ok(Certainty::Yes))
1292 } else {
1293 ControlFlow::Continue(())
1294 }
1295 }
1296 ty::Infer(_) => ControlFlow::Break(Ok(Certainty::AMBIGUOUS)),
1297 _ if ty.has_type_flags(
1298 TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_INFER | TypeFlags::HAS_ALIAS,
1299 ) =>
1300 {
1301 self.recursion_depth += 1;
1302 if self.recursion_depth > self.ecx.cx().recursion_limit() {
1303 return ControlFlow::Break(Ok(Certainty::Maybe {
1304 cause: MaybeCause::Overflow {
1305 suggest_increasing_limit: true,
1306 keep_constraints: false,
1307 },
1308 opaque_types_jank: OpaqueTypesJank::AllGood,
1309 }));
1310 }
1311 let result = ty.super_visit_with(self);
1312 self.recursion_depth -= 1;
1313 result
1314 }
1315 _ => ControlFlow::Continue(()),
1316 }
1317 }
1318
1319 fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1320 let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1321 let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1322 return ControlFlow::Break(Err(NoSolution));
1323 };
1324
1325 match ct.kind() {
1326 ty::ConstKind::Placeholder(p) => {
1327 if p.universe() == ty::UniverseIndex::ROOT {
1328 ControlFlow::Break(Ok(Certainty::Yes))
1329 } else {
1330 ControlFlow::Continue(())
1331 }
1332 }
1333 ty::ConstKind::Infer(_) => ControlFlow::Break(Ok(Certainty::AMBIGUOUS)),
1334 _ if ct.has_type_flags(
1335 TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_INFER | TypeFlags::HAS_ALIAS,
1336 ) =>
1337 {
1338 ct.super_visit_with(self)
1340 }
1341 _ => ControlFlow::Continue(()),
1342 }
1343 }
1344
1345 fn visit_region(&mut self, r: I::Region) -> Self::Result {
1346 match self.ecx.eager_resolve_region(r).kind() {
1347 ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1348 ty::RePlaceholder(p) => {
1349 if p.universe() == ty::UniverseIndex::ROOT {
1350 ControlFlow::Break(Ok(Certainty::Yes))
1351 } else {
1352 ControlFlow::Continue(())
1353 }
1354 }
1355 ty::ReVar(_) => ControlFlow::Break(Ok(Certainty::Yes)),
1356 ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1357 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected region in param-env clause")));
}unreachable!("unexpected region in param-env clause")
1358 }
1359 }
1360 }
1361}