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 source.set(ecx.characterize_param_env_assumption(goal.param_env, assumption)?);
142 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
143 })
144 });
145
146 match result {
147 Ok(result) => Ok(Candidate { source: source.get(), result, head_usages }),
148 Err(NoSolution) => Err(head_usages),
149 }
150 }
151
152 fn probe_and_match_goal_against_assumption(
157 ecx: &mut EvalCtxt<'_, D>,
158 source: CandidateSource<I>,
159 goal: Goal<I, Self>,
160 assumption: I::Clause,
161 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
162 ) -> Result<Candidate<I>, NoSolution> {
163 Self::fast_reject_assumption(ecx, goal, assumption)?;
164
165 ecx.probe_trait_candidate(source)
166 .enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
167 }
168
169 fn fast_reject_assumption(
172 ecx: &mut EvalCtxt<'_, D>,
173 goal: Goal<I, Self>,
174 assumption: I::Clause,
175 ) -> Result<(), NoSolution>;
176
177 fn match_assumption(
179 ecx: &mut EvalCtxt<'_, D>,
180 goal: Goal<I, Self>,
181 assumption: I::Clause,
182 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
183 ) -> QueryResult<I>;
184
185 fn consider_impl_candidate(
186 ecx: &mut EvalCtxt<'_, D>,
187 goal: Goal<I, Self>,
188 impl_def_id: I::ImplId,
189 then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResult<I>,
190 ) -> Result<Candidate<I>, NoSolution>;
191
192 fn consider_error_guaranteed_candidate(
199 ecx: &mut EvalCtxt<'_, D>,
200 guar: I::ErrorGuaranteed,
201 ) -> Result<Candidate<I>, NoSolution>;
202
203 fn consider_auto_trait_candidate(
208 ecx: &mut EvalCtxt<'_, D>,
209 goal: Goal<I, Self>,
210 ) -> Result<Candidate<I>, NoSolution>;
211
212 fn consider_trait_alias_candidate(
214 ecx: &mut EvalCtxt<'_, D>,
215 goal: Goal<I, Self>,
216 ) -> Result<Candidate<I>, NoSolution>;
217
218 fn consider_builtin_sizedness_candidates(
224 ecx: &mut EvalCtxt<'_, D>,
225 goal: Goal<I, Self>,
226 sizedness: SizedTraitKind,
227 ) -> Result<Candidate<I>, NoSolution>;
228
229 fn consider_builtin_copy_clone_candidate(
234 ecx: &mut EvalCtxt<'_, D>,
235 goal: Goal<I, Self>,
236 ) -> Result<Candidate<I>, NoSolution>;
237
238 fn consider_builtin_fn_ptr_trait_candidate(
240 ecx: &mut EvalCtxt<'_, D>,
241 goal: Goal<I, Self>,
242 ) -> Result<Candidate<I>, NoSolution>;
243
244 fn consider_builtin_fn_trait_candidates(
247 ecx: &mut EvalCtxt<'_, D>,
248 goal: Goal<I, Self>,
249 kind: ty::ClosureKind,
250 ) -> Result<Candidate<I>, NoSolution>;
251
252 fn consider_builtin_async_fn_trait_candidates(
255 ecx: &mut EvalCtxt<'_, D>,
256 goal: Goal<I, Self>,
257 kind: ty::ClosureKind,
258 ) -> Result<Candidate<I>, NoSolution>;
259
260 fn consider_builtin_async_fn_kind_helper_candidate(
264 ecx: &mut EvalCtxt<'_, D>,
265 goal: Goal<I, Self>,
266 ) -> Result<Candidate<I>, NoSolution>;
267
268 fn consider_builtin_tuple_candidate(
270 ecx: &mut EvalCtxt<'_, D>,
271 goal: Goal<I, Self>,
272 ) -> Result<Candidate<I>, NoSolution>;
273
274 fn consider_builtin_pointee_candidate(
280 ecx: &mut EvalCtxt<'_, D>,
281 goal: Goal<I, Self>,
282 ) -> Result<Candidate<I>, NoSolution>;
283
284 fn consider_builtin_future_candidate(
288 ecx: &mut EvalCtxt<'_, D>,
289 goal: Goal<I, Self>,
290 ) -> Result<Candidate<I>, NoSolution>;
291
292 fn consider_builtin_iterator_candidate(
296 ecx: &mut EvalCtxt<'_, D>,
297 goal: Goal<I, Self>,
298 ) -> Result<Candidate<I>, NoSolution>;
299
300 fn consider_builtin_fused_iterator_candidate(
303 ecx: &mut EvalCtxt<'_, D>,
304 goal: Goal<I, Self>,
305 ) -> Result<Candidate<I>, NoSolution>;
306
307 fn consider_builtin_async_iterator_candidate(
308 ecx: &mut EvalCtxt<'_, D>,
309 goal: Goal<I, Self>,
310 ) -> Result<Candidate<I>, NoSolution>;
311
312 fn consider_builtin_coroutine_candidate(
316 ecx: &mut EvalCtxt<'_, D>,
317 goal: Goal<I, Self>,
318 ) -> Result<Candidate<I>, NoSolution>;
319
320 fn consider_builtin_discriminant_kind_candidate(
321 ecx: &mut EvalCtxt<'_, D>,
322 goal: Goal<I, Self>,
323 ) -> Result<Candidate<I>, NoSolution>;
324
325 fn consider_builtin_destruct_candidate(
326 ecx: &mut EvalCtxt<'_, D>,
327 goal: Goal<I, Self>,
328 ) -> Result<Candidate<I>, NoSolution>;
329
330 fn consider_builtin_transmute_candidate(
331 ecx: &mut EvalCtxt<'_, D>,
332 goal: Goal<I, Self>,
333 ) -> Result<Candidate<I>, NoSolution>;
334
335 fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
336 ecx: &mut EvalCtxt<'_, D>,
337 goal: Goal<I, Self>,
338 ) -> Result<Candidate<I>, NoSolution>;
339
340 fn consider_structural_builtin_unsize_candidates(
348 ecx: &mut EvalCtxt<'_, D>,
349 goal: Goal<I, Self>,
350 ) -> Vec<Candidate<I>>;
351}
352
353pub(super) enum AssembleCandidatesFrom {
361 All,
362 EnvAndBounds,
366}
367
368impl AssembleCandidatesFrom {
369 fn should_assemble_impl_candidates(&self) -> bool {
370 match self {
371 AssembleCandidatesFrom::All => true,
372 AssembleCandidatesFrom::EnvAndBounds => false,
373 }
374 }
375}
376
377#[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)]
386pub(super) struct FailedCandidateInfo {
387 pub param_env_head_usages: CandidateHeadUsages,
388}
389
390impl<D, I> EvalCtxt<'_, D>
391where
392 D: SolverDelegate<Interner = I>,
393 I: Interner,
394{
395 pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
396 &mut self,
397 goal: Goal<I, G>,
398 assemble_from: AssembleCandidatesFrom,
399 ) -> (Vec<Candidate<I>>, FailedCandidateInfo) {
400 let mut candidates = ::alloc::vec::Vec::new()vec![];
401 let mut failed_candidate_info =
402 FailedCandidateInfo { param_env_head_usages: CandidateHeadUsages::default() };
403 let Ok(normalized_self_ty) =
404 self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
405 else {
406 return (candidates, failed_candidate_info);
407 };
408
409 let goal: Goal<I, G> = goal
410 .with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
411
412 if normalized_self_ty.is_ty_var() {
413 {
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:413",
"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(413u32),
::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");
414 self.try_assemble_bounds_via_registered_opaques(goal, assemble_from, &mut candidates);
415 return (candidates, failed_candidate_info);
416 }
417
418 let goal = self.resolve_vars_if_possible(goal);
421
422 if let TypingMode::Coherence = self.typing_mode()
423 && let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
424 {
425 candidates.push(candidate);
426 return (candidates, failed_candidate_info);
427 }
428
429 self.assemble_alias_bound_candidates(goal, &mut candidates);
430 self.assemble_param_env_candidates(goal, &mut candidates, &mut failed_candidate_info);
431
432 match assemble_from {
433 AssembleCandidatesFrom::All => {
434 self.assemble_builtin_impl_candidates(goal, &mut candidates);
435 if TypingMode::Coherence == self.typing_mode()
447 || !candidates.iter().any(|c| {
448 #[allow(non_exhaustive_omitted_patterns)] match c.source {
CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) |
CandidateSource::AliasBound(_) => true,
_ => false,
}matches!(
449 c.source,
450 CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
451 | CandidateSource::AliasBound(_)
452 ) && has_no_inference_or_external_constraints(c.result)
453 })
454 {
455 self.assemble_impl_candidates(goal, &mut candidates);
456 self.assemble_object_bound_candidates(goal, &mut candidates);
457 }
458 }
459 AssembleCandidatesFrom::EnvAndBounds => {
460 if #[allow(non_exhaustive_omitted_patterns)] match normalized_self_ty.kind() {
ty::Dynamic(..) => true,
_ => false,
}matches!(normalized_self_ty.kind(), ty::Dynamic(..))
464 && !candidates.iter().any(|c| #[allow(non_exhaustive_omitted_patterns)] match c.source {
CandidateSource::ParamEnv(_) => true,
_ => false,
}matches!(c.source, CandidateSource::ParamEnv(_)))
465 {
466 self.assemble_object_bound_candidates(goal, &mut candidates);
467 }
468 }
469 }
470
471 (candidates, failed_candidate_info)
472 }
473
474 pub(super) fn forced_ambiguity(
475 &mut self,
476 cause: MaybeCause,
477 ) -> Result<Candidate<I>, NoSolution> {
478 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
487 let certainty = Certainty::Maybe { cause, opaque_types_jank: OpaqueTypesJank::AllGood };
488 self.probe_trait_candidate(source)
489 .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
490 }
491
492 #[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(492u32),
::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)]
493 fn assemble_impl_candidates<G: GoalKind<D>>(
494 &mut self,
495 goal: Goal<I, G>,
496 candidates: &mut Vec<Candidate<I>>,
497 ) {
498 let cx = self.cx();
499 cx.for_each_relevant_impl(
500 goal.predicate.trait_def_id(cx),
501 goal.predicate.self_ty(),
502 |impl_def_id| {
503 if cx.impl_is_default(impl_def_id) {
507 return;
508 }
509 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
510 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
511 }) {
512 Ok(candidate) => candidates.push(candidate),
513 Err(NoSolution) => (),
514 }
515 },
516 );
517 }
518
519 #[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(519u32),
::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)
}
_ => 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)]
520 fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
521 &mut self,
522 goal: Goal<I, G>,
523 candidates: &mut Vec<Candidate<I>>,
524 ) {
525 let cx = self.cx();
526 let trait_def_id = goal.predicate.trait_def_id(cx);
527
528 let result = if let Err(guar) = goal.predicate.error_reported() {
536 G::consider_error_guaranteed_candidate(self, guar)
537 } else if cx.trait_is_auto(trait_def_id) {
538 G::consider_auto_trait_candidate(self, goal)
539 } else if cx.trait_is_alias(trait_def_id) {
540 G::consider_trait_alias_candidate(self, goal)
541 } else {
542 match cx.as_trait_lang_item(trait_def_id) {
543 Some(SolverTraitLangItem::Sized) => {
544 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
545 }
546 Some(SolverTraitLangItem::MetaSized) => {
547 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
548 }
549 Some(SolverTraitLangItem::PointeeSized) => {
550 unreachable!("`PointeeSized` is removed during lowering");
551 }
552 Some(
553 SolverTraitLangItem::Copy
554 | SolverTraitLangItem::Clone
555 | SolverTraitLangItem::TrivialClone,
556 ) => G::consider_builtin_copy_clone_candidate(self, goal),
557 Some(SolverTraitLangItem::Fn) => {
558 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
559 }
560 Some(SolverTraitLangItem::FnMut) => {
561 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
562 }
563 Some(SolverTraitLangItem::FnOnce) => {
564 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
565 }
566 Some(SolverTraitLangItem::AsyncFn) => {
567 G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
568 }
569 Some(SolverTraitLangItem::AsyncFnMut) => {
570 G::consider_builtin_async_fn_trait_candidates(
571 self,
572 goal,
573 ty::ClosureKind::FnMut,
574 )
575 }
576 Some(SolverTraitLangItem::AsyncFnOnce) => {
577 G::consider_builtin_async_fn_trait_candidates(
578 self,
579 goal,
580 ty::ClosureKind::FnOnce,
581 )
582 }
583 Some(SolverTraitLangItem::FnPtrTrait) => {
584 G::consider_builtin_fn_ptr_trait_candidate(self, goal)
585 }
586 Some(SolverTraitLangItem::AsyncFnKindHelper) => {
587 G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
588 }
589 Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
590 Some(SolverTraitLangItem::PointeeTrait) => {
591 G::consider_builtin_pointee_candidate(self, goal)
592 }
593 Some(SolverTraitLangItem::Future) => {
594 G::consider_builtin_future_candidate(self, goal)
595 }
596 Some(SolverTraitLangItem::Iterator) => {
597 G::consider_builtin_iterator_candidate(self, goal)
598 }
599 Some(SolverTraitLangItem::FusedIterator) => {
600 G::consider_builtin_fused_iterator_candidate(self, goal)
601 }
602 Some(SolverTraitLangItem::AsyncIterator) => {
603 G::consider_builtin_async_iterator_candidate(self, goal)
604 }
605 Some(SolverTraitLangItem::Coroutine) => {
606 G::consider_builtin_coroutine_candidate(self, goal)
607 }
608 Some(SolverTraitLangItem::DiscriminantKind) => {
609 G::consider_builtin_discriminant_kind_candidate(self, goal)
610 }
611 Some(SolverTraitLangItem::Destruct) => {
612 G::consider_builtin_destruct_candidate(self, goal)
613 }
614 Some(SolverTraitLangItem::TransmuteTrait) => {
615 G::consider_builtin_transmute_candidate(self, goal)
616 }
617 Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
618 G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
619 }
620 _ => Err(NoSolution),
621 }
622 };
623
624 candidates.extend(result);
625
626 if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
629 candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
630 }
631 }
632
633 #[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(633u32),
::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)]
634 fn assemble_param_env_candidates<G: GoalKind<D>>(
635 &mut self,
636 goal: Goal<I, G>,
637 candidates: &mut Vec<Candidate<I>>,
638 failed_candidate_info: &mut FailedCandidateInfo,
639 ) {
640 for assumption in goal.param_env.caller_bounds().iter() {
641 match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
642 Ok(candidate) => candidates.push(candidate),
643 Err(head_usages) => {
644 failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
645 }
646 }
647 }
648 }
649
650 #[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(650u32),
::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)]
651 fn assemble_alias_bound_candidates<G: GoalKind<D>>(
652 &mut self,
653 goal: Goal<I, G>,
654 candidates: &mut Vec<Candidate<I>>,
655 ) {
656 let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
657 ecx.assemble_alias_bound_candidates_recur(
658 goal.predicate.self_ty(),
659 goal,
660 candidates,
661 AliasBoundKind::SelfBounds,
662 );
663 });
664 }
665
666 fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
676 &mut self,
677 self_ty: I::Ty,
678 goal: Goal<I, G>,
679 candidates: &mut Vec<Candidate<I>>,
680 consider_self_bounds: AliasBoundKind,
681 ) {
682 let (kind, alias_ty) = match self_ty.kind() {
683 ty::Bool
684 | ty::Char
685 | ty::Int(_)
686 | ty::Uint(_)
687 | ty::Float(_)
688 | ty::Adt(_, _)
689 | ty::Foreign(_)
690 | ty::Str
691 | ty::Array(_, _)
692 | ty::Pat(_, _)
693 | ty::Slice(_)
694 | ty::RawPtr(_, _)
695 | ty::Ref(_, _, _)
696 | ty::FnDef(_, _)
697 | ty::FnPtr(..)
698 | ty::UnsafeBinder(_)
699 | ty::Dynamic(..)
700 | ty::Closure(..)
701 | ty::CoroutineClosure(..)
702 | ty::Coroutine(..)
703 | ty::CoroutineWitness(..)
704 | ty::Never
705 | ty::Tuple(_)
706 | ty::Param(_)
707 | ty::Placeholder(..)
708 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
709 | ty::Error(_) => return,
710 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
711 {
::core::panicking::panic_fmt(format_args!("unexpected self type for `{0:?}`",
goal));
}panic!("unexpected self type for `{goal:?}`")
712 }
713
714 ty::Infer(ty::TyVar(_)) => {
715 if let Ok(result) =
719 self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
720 {
721 candidates.push(Candidate {
722 source: CandidateSource::AliasBound(consider_self_bounds),
723 result,
724 head_usages: CandidateHeadUsages::default(),
725 });
726 }
727 return;
728 }
729
730 ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
731 ty::Alias(ty::Inherent | ty::Free, _) => {
732 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"));
733 return;
734 }
735 };
736
737 match consider_self_bounds {
738 AliasBoundKind::SelfBounds => {
739 for assumption in self
740 .cx()
741 .item_self_bounds(alias_ty.def_id)
742 .iter_instantiated(self.cx(), alias_ty.args)
743 {
744 candidates.extend(G::probe_and_consider_implied_clause(
745 self,
746 CandidateSource::AliasBound(consider_self_bounds),
747 goal,
748 assumption,
749 [],
750 ));
751 }
752 }
753 AliasBoundKind::NonSelfBounds => {
754 for assumption in self
755 .cx()
756 .item_non_self_bounds(alias_ty.def_id)
757 .iter_instantiated(self.cx(), alias_ty.args)
758 {
759 candidates.extend(G::probe_and_consider_implied_clause(
760 self,
761 CandidateSource::AliasBound(consider_self_bounds),
762 goal,
763 assumption,
764 [],
765 ));
766 }
767 }
768 }
769
770 candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
771
772 if kind != ty::Projection {
773 return;
774 }
775
776 match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
778 Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
779 next_self_ty,
780 goal,
781 candidates,
782 AliasBoundKind::NonSelfBounds,
783 ),
784 Err(NoSolution) => {}
785 }
786 }
787
788 #[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(788u32),
::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)]
789 fn assemble_object_bound_candidates<G: GoalKind<D>>(
790 &mut self,
791 goal: Goal<I, G>,
792 candidates: &mut Vec<Candidate<I>>,
793 ) {
794 let cx = self.cx();
795 if cx.is_sizedness_trait(goal.predicate.trait_def_id(cx)) {
796 return;
799 }
800
801 let self_ty = goal.predicate.self_ty();
802 let bounds = match self_ty.kind() {
803 ty::Bool
804 | ty::Char
805 | ty::Int(_)
806 | ty::Uint(_)
807 | ty::Float(_)
808 | ty::Adt(_, _)
809 | ty::Foreign(_)
810 | ty::Str
811 | ty::Array(_, _)
812 | ty::Pat(_, _)
813 | ty::Slice(_)
814 | ty::RawPtr(_, _)
815 | ty::Ref(_, _, _)
816 | ty::FnDef(_, _)
817 | ty::FnPtr(..)
818 | ty::UnsafeBinder(_)
819 | ty::Alias(..)
820 | ty::Closure(..)
821 | ty::CoroutineClosure(..)
822 | ty::Coroutine(..)
823 | ty::CoroutineWitness(..)
824 | ty::Never
825 | ty::Tuple(_)
826 | ty::Param(_)
827 | ty::Placeholder(..)
828 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
829 | ty::Error(_) => return,
830 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
831 | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
832 ty::Dynamic(bounds, ..) => bounds,
833 };
834
835 if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
837 return;
838 }
839
840 for bound in bounds.iter() {
844 match bound.skip_binder() {
845 ty::ExistentialPredicate::Trait(_) => {
846 }
848 ty::ExistentialPredicate::Projection(_)
849 | ty::ExistentialPredicate::AutoTrait(_) => {
850 candidates.extend(G::probe_and_consider_object_bound_candidate(
851 self,
852 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
853 goal,
854 bound.with_self_ty(cx, self_ty),
855 ));
856 }
857 }
858 }
859
860 if let Some(principal) = bounds.principal() {
864 let principal_trait_ref = principal.with_self_ty(cx, self_ty);
865 for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
866 candidates.extend(G::probe_and_consider_object_bound_candidate(
867 self,
868 CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
869 goal,
870 assumption.upcast(cx),
871 ));
872 }
873 }
874 }
875
876 #[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(882u32),
::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)]
883 fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
884 &mut self,
885 goal: Goal<I, G>,
886 ) -> Result<Candidate<I>, NoSolution> {
887 self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
888 let cx = ecx.cx();
889 let trait_ref = goal.predicate.trait_ref(cx);
890 if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
891 Err(NoSolution)
892 } else {
893 let predicate: I::Predicate = trait_ref.upcast(cx);
899 ecx.add_goals(
900 GoalSource::Misc,
901 elaborate::elaborate(cx, [predicate])
902 .skip(1)
903 .map(|predicate| goal.with(cx, predicate)),
904 );
905 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
906 }
907 })
908 }
909}
910
911pub(super) enum AllowInferenceConstraints {
912 Yes,
913 No,
914}
915
916impl<D, I> EvalCtxt<'_, D>
917where
918 D: SolverDelegate<Interner = I>,
919 I: Interner,
920{
921 pub(super) fn filter_specialized_impls(
925 &mut self,
926 allow_inference_constraints: AllowInferenceConstraints,
927 candidates: &mut Vec<Candidate<I>>,
928 ) {
929 match self.typing_mode() {
930 TypingMode::Coherence => return,
931 TypingMode::Analysis { .. }
932 | TypingMode::Borrowck { .. }
933 | TypingMode::PostBorrowckAnalysis { .. }
934 | TypingMode::PostAnalysis => {}
935 }
936
937 let mut i = 0;
938 'outer: while i < candidates.len() {
939 let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
940 i += 1;
941 continue;
942 };
943
944 for (j, c) in candidates.iter().enumerate() {
945 if i == j {
946 continue;
947 }
948
949 let CandidateSource::Impl(other_def_id) = c.source else {
950 continue;
951 };
952
953 if #[allow(non_exhaustive_omitted_patterns)] match allow_inference_constraints {
AllowInferenceConstraints::Yes => true,
_ => false,
}matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
960 || has_only_region_constraints(c.result)
961 {
962 if self.cx().impl_specializes(other_def_id, victim_def_id) {
963 candidates.remove(i);
964 continue 'outer;
965 }
966 }
967 }
968
969 i += 1;
970 }
971 }
972
973 fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
985 &mut self,
986 goal: Goal<I, G>,
987 assemble_from: AssembleCandidatesFrom,
988 candidates: &mut Vec<Candidate<I>>,
989 ) {
990 let self_ty = goal.predicate.self_ty();
991 let opaque_types = match self.typing_mode() {
993 TypingMode::Analysis { .. } => self.opaques_with_sub_unified_hidden_type(self_ty),
994 TypingMode::Coherence
995 | TypingMode::Borrowck { .. }
996 | TypingMode::PostBorrowckAnalysis { .. }
997 | TypingMode::PostAnalysis => ::alloc::vec::Vec::new()vec![],
998 };
999
1000 if opaque_types.is_empty() {
1001 candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
1002 return;
1003 }
1004
1005 for &alias_ty in &opaque_types {
1006 {
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:1006",
"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(1006u32),
::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:?}");
1007
1008 struct ReplaceOpaque<I: Interner> {
1009 cx: I,
1010 alias_ty: ty::AliasTy<I>,
1011 self_ty: I::Ty,
1012 }
1013 impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
1014 fn cx(&self) -> I {
1015 self.cx
1016 }
1017 fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1018 if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
1019 if alias_ty == self.alias_ty {
1020 return self.self_ty;
1021 }
1022 }
1023 ty.super_fold_with(self)
1024 }
1025 }
1026
1027 for item_bound in self
1035 .cx()
1036 .item_self_bounds(alias_ty.def_id)
1037 .iter_instantiated(self.cx(), alias_ty.args)
1038 {
1039 let assumption =
1040 item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1041 candidates.extend(G::probe_and_match_goal_against_assumption(
1042 self,
1043 CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
1044 goal,
1045 assumption,
1046 |ecx| {
1047 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1050 },
1051 ));
1052 }
1053 }
1054
1055 if assemble_from.should_assemble_impl_candidates() {
1060 let cx = self.cx();
1061 cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1062 if cx.impl_is_default(impl_def_id) {
1066 return;
1067 }
1068
1069 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1070 if ecx.shallow_resolve(self_ty).is_ty_var() {
1071 let certainty = certainty.and(Certainty::AMBIGUOUS);
1073 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1074 } else {
1075 Err(NoSolution)
1081 }
1082 }) {
1083 Ok(candidate) => candidates.push(candidate),
1084 Err(NoSolution) => (),
1085 }
1086 });
1087 }
1088
1089 if candidates.is_empty() {
1090 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
1091 let certainty = Certainty::Maybe {
1092 cause: MaybeCause::Ambiguity,
1093 opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
1094 };
1095 candidates
1096 .extend(self.probe_trait_candidate(source).enter(|this| {
1097 this.evaluate_added_goals_and_make_canonical_response(certainty)
1098 }));
1099 }
1100 }
1101
1102 x;#[instrument(level = "debug", skip_all, fields(proven_via, goal), ret)]
1133 pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1134 &mut self,
1135 proven_via: Option<TraitGoalProvenVia>,
1136 goal: Goal<I, G>,
1137 inject_forced_ambiguity_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> Option<QueryResult<I>>,
1138 inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1139 ) -> QueryResult<I> {
1140 let Some(proven_via) = proven_via else {
1141 return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1148 };
1149
1150 match proven_via {
1151 TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1152 let (mut candidates, _) = self
1156 .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1157 debug!(?candidates);
1158
1159 if candidates.is_empty() {
1162 return inject_normalize_to_rigid_candidate(self);
1163 }
1164
1165 if let Some(result) = inject_forced_ambiguity_candidate(self) {
1168 return result;
1169 }
1170
1171 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1174 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1175 }
1176
1177 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1178 Ok(response)
1179 } else {
1180 self.flounder(&candidates)
1181 }
1182 }
1183 TraitGoalProvenVia::Misc => {
1184 let (mut candidates, _) =
1185 self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1186
1187 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1190 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1191 }
1192
1193 self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1199 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1200 Ok(response)
1201 } else {
1202 self.flounder(&candidates)
1203 }
1204 }
1205 }
1206 }
1207
1208 fn characterize_param_env_assumption(
1222 &mut self,
1223 param_env: I::ParamEnv,
1224 assumption: I::Clause,
1225 ) -> Result<CandidateSource<I>, NoSolution> {
1226 if assumption.has_bound_vars() {
1229 return Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal));
1230 }
1231
1232 match assumption.visit_with(&mut FindParamInClause {
1233 ecx: self,
1234 param_env,
1235 universes: ::alloc::vec::Vec::new()vec![],
1236 }) {
1237 ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1238 ControlFlow::Break(Ok(())) => Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)),
1239 ControlFlow::Continue(()) => Ok(CandidateSource::ParamEnv(ParamEnvSource::Global)),
1240 }
1241 }
1242}
1243
1244struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1245 ecx: &'a mut EvalCtxt<'b, D>,
1246 param_env: I::ParamEnv,
1247 universes: Vec<Option<ty::UniverseIndex>>,
1248}
1249
1250impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1251where
1252 D: SolverDelegate<Interner = I>,
1253 I: Interner,
1254{
1255 type Result = ControlFlow<Result<(), NoSolution>>;
1256
1257 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1258 self.universes.push(None);
1259 t.super_visit_with(self)?;
1260 self.universes.pop();
1261 ControlFlow::Continue(())
1262 }
1263
1264 fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1265 let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1266 let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1267 return ControlFlow::Break(Err(NoSolution));
1268 };
1269
1270 if let ty::Placeholder(p) = ty.kind() {
1271 if p.universe() == ty::UniverseIndex::ROOT {
1272 ControlFlow::Break(Ok(()))
1273 } else {
1274 ControlFlow::Continue(())
1275 }
1276 } else if ty.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1277 ty.super_visit_with(self)
1278 } else {
1279 ControlFlow::Continue(())
1280 }
1281 }
1282
1283 fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1284 let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1285 let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1286 return ControlFlow::Break(Err(NoSolution));
1287 };
1288
1289 if let ty::ConstKind::Placeholder(p) = ct.kind() {
1290 if p.universe() == ty::UniverseIndex::ROOT {
1291 ControlFlow::Break(Ok(()))
1292 } else {
1293 ControlFlow::Continue(())
1294 }
1295 } else if ct.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1296 ct.super_visit_with(self)
1297 } else {
1298 ControlFlow::Continue(())
1299 }
1300 }
1301
1302 fn visit_region(&mut self, r: I::Region) -> Self::Result {
1303 match self.ecx.eager_resolve_region(r).kind() {
1304 ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1305 ty::RePlaceholder(p) => {
1306 if p.universe() == ty::UniverseIndex::ROOT {
1307 ControlFlow::Break(Ok(()))
1308 } else {
1309 ControlFlow::Continue(())
1310 }
1311 }
1312 ty::ReVar(_) => ControlFlow::Break(Ok(())),
1313 ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1314 {
::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")
1315 }
1316 }
1317 }
1318}