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::{
13 AliasBoundKind, MaybeInfo, NoSolutionOrRerunNonErased, QueryResultOrRerunNonErased,
14 RerunNonErased, RerunReason, RerunResultExt, SizedTraitKind, StalledOnCoroutines,
15};
16use rustc_type_ir::{
17 self as ty, AliasTy, Interner, MayBeErased, TypeFlags, TypeFoldable, TypeFolder,
18 TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
19 TypingMode, Unnormalized, Upcast, elaborate,
20};
21use tracing::{debug, instrument};
22
23use super::trait_goals::TraitGoalProvenVia;
24use super::{has_only_region_constraints, inspect};
25use crate::delegate::SolverDelegate;
26use crate::solve::inspect::ProbeKind;
27use crate::solve::{
28 BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
29 MaybeCause, NoSolution, OpaqueTypesJank, ParamEnvSource, QueryResult,
30 has_no_inference_or_external_constraints,
31};
32
33#[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)]
38pub(super) struct Candidate<I: Interner> {
39 pub(super) source: CandidateSource<I>,
40 pub(super) result: CanonicalResponse<I>,
41 pub(super) head_usages: CandidateHeadUsages,
42}
43
44pub(super) trait GoalKind<D, I = <D as SolverDelegate>::Interner>:
46 TypeFoldable<I> + Copy + Eq + std::fmt::Display
47where
48 D: SolverDelegate<Interner = I>,
49 I: Interner,
50{
51 fn self_ty(self) -> I::Ty;
52
53 fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
54
55 fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
56
57 fn trait_def_id(self, cx: I) -> I::TraitId;
58
59 fn probe_and_consider_implied_clause(
63 ecx: &mut EvalCtxt<'_, D>,
64 parent_source: CandidateSource<I>,
65 goal: Goal<I, Self>,
66 assumption: I::Clause,
67 requirements: impl IntoIterator<Item = (GoalSource, Goal<I, I::Predicate>)>,
68 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
69 Self::probe_and_match_goal_against_assumption(ecx, parent_source, goal, assumption, |ecx| {
70 for (nested_source, goal) in requirements {
71 ecx.add_goal(nested_source, goal);
72 }
73 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
74 })
75 }
76
77 fn probe_and_consider_object_bound_candidate(
83 ecx: &mut EvalCtxt<'_, D>,
84 source: CandidateSource<I>,
85 goal: Goal<I, Self>,
86 assumption: I::Clause,
87 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
88 Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
89 let cx = ecx.cx();
90 let ty::Dynamic(bounds, _) = goal.predicate.self_ty().kind() else {
91 {
::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`");
92 };
93
94 let trait_ref = assumption.kind().map_bound(|clause| match clause {
95 ty::ClauseKind::Trait(pred) => pred.trait_ref,
96 ty::ClauseKind::Projection(proj) => proj.projection_term.trait_ref(cx),
97
98 ty::ClauseKind::RegionOutlives(..)
99 | ty::ClauseKind::TypeOutlives(..)
100 | ty::ClauseKind::ConstArgHasType(..)
101 | ty::ClauseKind::WellFormed(..)
102 | ty::ClauseKind::ConstEvaluatable(..)
103 | ty::ClauseKind::HostEffect(..)
104 | ty::ClauseKind::UnstableFeature(..) => {
105 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("expected trait or projection predicate as an assumption")));
}unreachable!("expected trait or projection predicate as an assumption")
106 }
107 });
108
109 match structural_traits::predicates_for_object_candidate(
110 ecx,
111 goal.param_env,
112 trait_ref,
113 bounds,
114 ) {
115 Ok(requirements) => {
116 ecx.add_goals(GoalSource::ImplWhereBound, requirements);
117 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
118 }
119 Err(_) => {
120 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
121 }
122 }
123 })
124 }
125
126 fn consider_additional_alias_assumptions(
130 ecx: &mut EvalCtxt<'_, D>,
131 goal: Goal<I, Self>,
132 alias_ty: ty::AliasTy<I>,
133 ) -> Vec<Candidate<I>>;
134
135 fn probe_and_consider_param_env_candidate(
136 ecx: &mut EvalCtxt<'_, D>,
137 goal: Goal<I, Self>,
138 assumption: I::Clause,
139 ) -> Result<Result<Candidate<I>, CandidateHeadUsages>, RerunNonErased> {
140 match Self::fast_reject_assumption(ecx, goal, assumption) {
141 Ok(()) => {}
142 Err(NoSolution) => return Ok(Err(CandidateHeadUsages::default())),
143 }
144
145 let source = Cell::new(CandidateSource::ParamEnv(ParamEnvSource::Global));
152 let (result, head_usages) = ecx
153 .probe(|result: &QueryResult<I>| inspect::ProbeKind::TraitCandidate {
154 source: source.get(),
155 result: *result,
156 })
157 .enter_single_candidate(|ecx| {
158 Self::match_assumption(
159 ecx,
160 goal,
161 assumption,
162 |ecx| -> Result<_, NoSolutionOrRerunNonErased> {
163 ecx.try_evaluate_added_goals()?;
164 let (src, certainty) =
165 ecx.characterize_param_env_assumption(goal.param_env, assumption)?;
166 source.set(src);
167 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
168 },
169 )
170 .map_err(Into::into)
171 });
172
173 Ok(match result.map_err_to_rerun()? {
174 Ok(result) => Ok(Candidate { source: source.get(), result, head_usages }),
175 Err(NoSolution) => Err(head_usages),
176 })
177 }
178
179 fn probe_and_match_goal_against_assumption(
184 ecx: &mut EvalCtxt<'_, D>,
185 source: CandidateSource<I>,
186 goal: Goal<I, Self>,
187 assumption: I::Clause,
188 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResultOrRerunNonErased<I>,
189 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
190 Self::fast_reject_assumption(ecx, goal, assumption)?;
191
192 ecx.probe_trait_candidate(source)
193 .enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
194 }
195
196 fn fast_reject_assumption(
199 ecx: &mut EvalCtxt<'_, D>,
200 goal: Goal<I, Self>,
201 assumption: I::Clause,
202 ) -> Result<(), NoSolution>;
203
204 fn match_assumption(
206 ecx: &mut EvalCtxt<'_, D>,
207 goal: Goal<I, Self>,
208 assumption: I::Clause,
209 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResultOrRerunNonErased<I>,
210 ) -> QueryResultOrRerunNonErased<I>;
211
212 fn consider_impl_candidate(
213 ecx: &mut EvalCtxt<'_, D>,
214 goal: Goal<I, Self>,
215 impl_def_id: I::ImplId,
216 then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResultOrRerunNonErased<I>,
217 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
218
219 fn consider_error_guaranteed_candidate(
226 ecx: &mut EvalCtxt<'_, D>,
227 goal: Goal<I, Self>,
228 guar: I::ErrorGuaranteed,
229 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
230
231 fn consider_auto_trait_candidate(
236 ecx: &mut EvalCtxt<'_, D>,
237 goal: Goal<I, Self>,
238 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
239
240 fn consider_trait_alias_candidate(
242 ecx: &mut EvalCtxt<'_, D>,
243 goal: Goal<I, Self>,
244 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
245
246 fn consider_builtin_sizedness_candidates(
252 ecx: &mut EvalCtxt<'_, D>,
253 goal: Goal<I, Self>,
254 sizedness: SizedTraitKind,
255 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
256
257 fn consider_builtin_copy_clone_candidate(
262 ecx: &mut EvalCtxt<'_, D>,
263 goal: Goal<I, Self>,
264 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
265
266 fn consider_builtin_fn_ptr_trait_candidate(
268 ecx: &mut EvalCtxt<'_, D>,
269 goal: Goal<I, Self>,
270 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
271
272 fn consider_builtin_fn_trait_candidates(
275 ecx: &mut EvalCtxt<'_, D>,
276 goal: Goal<I, Self>,
277 kind: ty::ClosureKind,
278 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
279
280 fn consider_builtin_async_fn_trait_candidates(
283 ecx: &mut EvalCtxt<'_, D>,
284 goal: Goal<I, Self>,
285 kind: ty::ClosureKind,
286 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
287
288 fn consider_builtin_async_fn_kind_helper_candidate(
292 ecx: &mut EvalCtxt<'_, D>,
293 goal: Goal<I, Self>,
294 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
295
296 fn consider_builtin_tuple_candidate(
298 ecx: &mut EvalCtxt<'_, D>,
299 goal: Goal<I, Self>,
300 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
301
302 fn consider_builtin_pointee_candidate(
308 ecx: &mut EvalCtxt<'_, D>,
309 goal: Goal<I, Self>,
310 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
311
312 fn consider_builtin_future_candidate(
316 ecx: &mut EvalCtxt<'_, D>,
317 goal: Goal<I, Self>,
318 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
319
320 fn consider_builtin_iterator_candidate(
324 ecx: &mut EvalCtxt<'_, D>,
325 goal: Goal<I, Self>,
326 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
327
328 fn consider_builtin_fused_iterator_candidate(
331 ecx: &mut EvalCtxt<'_, D>,
332 goal: Goal<I, Self>,
333 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
334
335 fn consider_builtin_async_iterator_candidate(
336 ecx: &mut EvalCtxt<'_, D>,
337 goal: Goal<I, Self>,
338 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
339
340 fn consider_builtin_coroutine_candidate(
344 ecx: &mut EvalCtxt<'_, D>,
345 goal: Goal<I, Self>,
346 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
347
348 fn consider_builtin_discriminant_kind_candidate(
349 ecx: &mut EvalCtxt<'_, D>,
350 goal: Goal<I, Self>,
351 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
352
353 fn consider_builtin_destruct_candidate(
354 ecx: &mut EvalCtxt<'_, D>,
355 goal: Goal<I, Self>,
356 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
357
358 fn consider_builtin_transmute_candidate(
359 ecx: &mut EvalCtxt<'_, D>,
360 goal: Goal<I, Self>,
361 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
362
363 fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
364 ecx: &mut EvalCtxt<'_, D>,
365 goal: Goal<I, Self>,
366 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
367
368 fn consider_structural_builtin_unsize_candidates(
376 ecx: &mut EvalCtxt<'_, D>,
377 goal: Goal<I, Self>,
378 ) -> Result<Vec<Candidate<I>>, RerunNonErased>;
379
380 fn consider_builtin_field_candidate(
381 ecx: &mut EvalCtxt<'_, D>,
382 goal: Goal<I, Self>,
383 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased>;
384}
385
386pub(super) enum AssembleCandidatesFrom {
394 All,
395 EnvAndBounds,
399}
400
401impl AssembleCandidatesFrom {
402 fn should_assemble_impl_candidates(&self) -> bool {
403 match self {
404 AssembleCandidatesFrom::All => true,
405 AssembleCandidatesFrom::EnvAndBounds => false,
406 }
407 }
408}
409
410#[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)]
419pub(super) struct FailedCandidateInfo {
420 pub param_env_head_usages: CandidateHeadUsages,
421}
422
423impl<D, I> EvalCtxt<'_, D>
424where
425 D: SolverDelegate<Interner = I>,
426 I: Interner,
427{
428 pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
432 &mut self,
433 goal: Goal<I, G>,
434 assemble_from: AssembleCandidatesFrom,
435 ) -> Result<(Vec<Candidate<I>>, FailedCandidateInfo), RerunNonErased> {
436 let mut candidates = ::alloc::vec::Vec::new()vec![];
437 let mut failed_candidate_info =
438 FailedCandidateInfo { param_env_head_usages: CandidateHeadUsages::default() };
439 let Ok(normalized_self_ty) =
440 self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
441 else {
442 return Ok((candidates, failed_candidate_info));
443 };
444
445 let goal: Goal<I, G> = goal
446 .with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
447
448 if normalized_self_ty.is_ty_var() {
449 {
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:449",
"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(449u32),
::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");
450 self.try_assemble_bounds_via_registered_opaques(goal, assemble_from, &mut candidates)?;
451 return Ok((candidates, failed_candidate_info));
452 }
453
454 let goal = self.resolve_vars_if_possible(goal);
457
458 if self.typing_mode().is_coherence()
459 && let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
460 {
461 candidates.push(candidate);
462 return Ok((candidates, failed_candidate_info));
463 }
464
465 self.assemble_alias_bound_candidates(goal, &mut candidates)?;
466 self.assemble_param_env_candidates(goal, &mut candidates, &mut failed_candidate_info)?;
467
468 match assemble_from {
469 AssembleCandidatesFrom::All => {
470 self.assemble_builtin_impl_candidates(goal, &mut candidates)?;
471 let assemble_impls = match self.typing_mode() {
483 TypingMode::Coherence => true,
484 TypingMode::Typeck { .. }
485 | TypingMode::PostTypeckUntilBorrowck { .. }
486 | TypingMode::PostBorrowck { .. }
487 | TypingMode::PostAnalysis
488 | TypingMode::Codegen
489 | TypingMode::ErasedNotCoherence(MayBeErased) => !candidates.iter().any(|c| {
490 #[allow(non_exhaustive_omitted_patterns)] match c.source {
CandidateSource::ParamEnv(ParamEnvSource::NonGlobal) |
CandidateSource::AliasBound(_) => true,
_ => false,
}matches!(
491 c.source,
492 CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
493 | CandidateSource::AliasBound(_)
494 ) && has_no_inference_or_external_constraints(c.result)
495 }),
496 };
497 if assemble_impls {
498 self.assemble_impl_candidates(goal, &mut candidates)?;
499 self.assemble_object_bound_candidates(goal, &mut candidates);
500 }
501 }
502 AssembleCandidatesFrom::EnvAndBounds => {
503 if #[allow(non_exhaustive_omitted_patterns)] match normalized_self_ty.kind() {
ty::Dynamic(..) => true,
_ => false,
}matches!(normalized_self_ty.kind(), ty::Dynamic(..))
507 && !candidates.iter().any(|c| #[allow(non_exhaustive_omitted_patterns)] match c.source {
CandidateSource::ParamEnv(_) => true,
_ => false,
}matches!(c.source, CandidateSource::ParamEnv(_)))
508 {
509 self.assemble_object_bound_candidates(goal, &mut candidates);
510 }
511 }
512 }
513
514 Ok((candidates, failed_candidate_info))
515 }
516
517 pub(super) fn forced_ambiguity(
518 &mut self,
519 maybe: MaybeInfo,
520 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
521 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
530 let certainty = Certainty::Maybe(maybe);
531 self.probe_trait_candidate(source)
532 .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
533 }
534
535 #[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(535u32),
::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<(), RerunNonErased> =
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| -> Result<_, _>
{
if cx.impl_is_default(impl_def_id) { return Ok(()); }
match G::consider_impl_candidate(self, goal, impl_def_id,
|ecx, certainty|
{
ecx.evaluate_added_goals_and_make_canonical_response(certainty)
}).map_err_to_rerun()? {
Ok(candidate) => candidates.push(candidate),
Err(NoSolution) => {}
}
Ok(())
})
}
}
}#[instrument(level = "trace", skip_all)]
536 fn assemble_impl_candidates<G: GoalKind<D>>(
537 &mut self,
538 goal: Goal<I, G>,
539 candidates: &mut Vec<Candidate<I>>,
540 ) -> Result<(), RerunNonErased> {
541 let cx = self.cx();
542 cx.for_each_relevant_impl(
543 goal.predicate.trait_def_id(cx),
544 goal.predicate.self_ty(),
545 |impl_def_id| -> Result<_, _> {
546 if cx.impl_is_default(impl_def_id) {
550 return Ok(());
551 }
552 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
553 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
554 })
555 .map_err_to_rerun()?
556 {
557 Ok(candidate) => candidates.push(candidate),
558 Err(NoSolution) => {}
559 }
560
561 Ok(())
562 },
563 )
564 }
565
566 #[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(566u32),
::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<(), RerunNonErased> =
loop {};
return __tracing_attr_fake_return;
}
{
let cx = self.cx();
let trait_def_id = goal.predicate.trait_def_id(cx);
let result =
if let ty::Error(guar) = goal.predicate.self_ty().kind() {
G::consider_error_guaranteed_candidate(self, goal, 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.into()),
}
};
candidates.extend(result);
if cx.is_trait_lang_item(trait_def_id,
SolverTraitLangItem::Unsize) {
candidates.extend(G::consider_structural_builtin_unsize_candidates(self,
goal)?);
}
Ok(())
}
}
}#[instrument(level = "trace", skip_all)]
567 fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
568 &mut self,
569 goal: Goal<I, G>,
570 candidates: &mut Vec<Candidate<I>>,
571 ) -> Result<(), RerunNonErased> {
572 let cx = self.cx();
573 let trait_def_id = goal.predicate.trait_def_id(cx);
574
575 let result = if let ty::Error(guar) = goal.predicate.self_ty().kind() {
583 G::consider_error_guaranteed_candidate(self, goal, guar)
584 } else if cx.trait_is_auto(trait_def_id) {
585 G::consider_auto_trait_candidate(self, goal)
586 } else if cx.trait_is_alias(trait_def_id) {
587 G::consider_trait_alias_candidate(self, goal)
588 } else {
589 match cx.as_trait_lang_item(trait_def_id) {
590 Some(SolverTraitLangItem::Sized) => {
591 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
592 }
593 Some(SolverTraitLangItem::MetaSized) => {
594 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
595 }
596 Some(SolverTraitLangItem::PointeeSized) => {
597 unreachable!("`PointeeSized` is removed during lowering");
598 }
599 Some(
600 SolverTraitLangItem::Copy
601 | SolverTraitLangItem::Clone
602 | SolverTraitLangItem::TrivialClone,
603 ) => G::consider_builtin_copy_clone_candidate(self, goal),
604 Some(SolverTraitLangItem::Fn) => {
605 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
606 }
607 Some(SolverTraitLangItem::FnMut) => {
608 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
609 }
610 Some(SolverTraitLangItem::FnOnce) => {
611 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
612 }
613 Some(SolverTraitLangItem::AsyncFn) => {
614 G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
615 }
616 Some(SolverTraitLangItem::AsyncFnMut) => {
617 G::consider_builtin_async_fn_trait_candidates(
618 self,
619 goal,
620 ty::ClosureKind::FnMut,
621 )
622 }
623 Some(SolverTraitLangItem::AsyncFnOnce) => {
624 G::consider_builtin_async_fn_trait_candidates(
625 self,
626 goal,
627 ty::ClosureKind::FnOnce,
628 )
629 }
630 Some(SolverTraitLangItem::FnPtrTrait) => {
631 G::consider_builtin_fn_ptr_trait_candidate(self, goal)
632 }
633 Some(SolverTraitLangItem::AsyncFnKindHelper) => {
634 G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
635 }
636 Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
637 Some(SolverTraitLangItem::PointeeTrait) => {
638 G::consider_builtin_pointee_candidate(self, goal)
639 }
640 Some(SolverTraitLangItem::Future) => {
641 G::consider_builtin_future_candidate(self, goal)
642 }
643 Some(SolverTraitLangItem::Iterator) => {
644 G::consider_builtin_iterator_candidate(self, goal)
645 }
646 Some(SolverTraitLangItem::FusedIterator) => {
647 G::consider_builtin_fused_iterator_candidate(self, goal)
648 }
649 Some(SolverTraitLangItem::AsyncIterator) => {
650 G::consider_builtin_async_iterator_candidate(self, goal)
651 }
652 Some(SolverTraitLangItem::Coroutine) => {
653 G::consider_builtin_coroutine_candidate(self, goal)
654 }
655 Some(SolverTraitLangItem::DiscriminantKind) => {
656 G::consider_builtin_discriminant_kind_candidate(self, goal)
657 }
658 Some(SolverTraitLangItem::Destruct) => {
659 G::consider_builtin_destruct_candidate(self, goal)
660 }
661 Some(SolverTraitLangItem::TransmuteTrait) => {
662 G::consider_builtin_transmute_candidate(self, goal)
663 }
664 Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
665 G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
666 }
667 Some(SolverTraitLangItem::Field) => G::consider_builtin_field_candidate(self, goal),
668 _ => Err(NoSolution.into()),
669 }
670 };
671
672 candidates.extend(result);
673
674 if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
677 candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal)?);
678 }
679
680 Ok(())
681 }
682
683 #[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(683u32),
::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<(), RerunNonErased> =
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)
}
}
}
Ok(())
}
}
}#[instrument(level = "trace", skip_all)]
684 fn assemble_param_env_candidates<G: GoalKind<D>>(
685 &mut self,
686 goal: Goal<I, G>,
687 candidates: &mut Vec<Candidate<I>>,
688 failed_candidate_info: &mut FailedCandidateInfo,
689 ) -> Result<(), RerunNonErased> {
690 for assumption in goal.param_env.caller_bounds().iter() {
691 match G::probe_and_consider_param_env_candidate(self, goal, assumption)? {
692 Ok(candidate) => candidates.push(candidate),
693 Err(head_usages) => {
694 failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
695 }
696 }
697 }
698
699 Ok(())
700 }
701
702 #[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(702u32),
::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<(), RerunNonErased> =
loop {};
return __tracing_attr_fake_return;
}
{
let res =
self.probe(|_|
ProbeKind::NormalizedSelfTyAssembly).enter(|ecx|
{
ecx.assemble_alias_bound_candidates_recur(goal.predicate.self_ty(),
goal, candidates, AliasBoundKind::SelfBounds)?;
Ok(())
});
match res {
Ok(_) => Ok(()),
Err(NoSolutionOrRerunNonErased::RerunNonErased(e)) => Err(e),
Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => {
::core::panicking::panic("internal error: entered unreachable code")
}
}
}
}
}#[instrument(level = "trace", skip_all)]
703 fn assemble_alias_bound_candidates<G: GoalKind<D>>(
704 &mut self,
705 goal: Goal<I, G>,
706 candidates: &mut Vec<Candidate<I>>,
707 ) -> Result<(), RerunNonErased> {
708 let res = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
709 ecx.assemble_alias_bound_candidates_recur(
710 goal.predicate.self_ty(),
711 goal,
712 candidates,
713 AliasBoundKind::SelfBounds,
714 )?;
715 Ok(())
716 });
717
718 match res {
720 Ok(_) => Ok(()),
721 Err(NoSolutionOrRerunNonErased::RerunNonErased(e)) => Err(e),
722 Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => {
723 unreachable!()
724 }
725 }
726 }
727
728 fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
738 &mut self,
739 self_ty: I::Ty,
740 goal: Goal<I, G>,
741 candidates: &mut Vec<Candidate<I>>,
742 consider_self_bounds: AliasBoundKind,
743 ) -> Result<(), RerunNonErased> {
744 let alias_ty = match self_ty.kind() {
745 ty::Bool
746 | ty::Char
747 | ty::Int(_)
748 | ty::Uint(_)
749 | ty::Float(_)
750 | ty::Adt(_, _)
751 | ty::Foreign(_)
752 | ty::Str
753 | ty::Array(_, _)
754 | ty::Pat(_, _)
755 | ty::Slice(_)
756 | ty::RawPtr(_, _)
757 | ty::Ref(_, _, _)
758 | ty::FnDef(_, _)
759 | ty::FnPtr(..)
760 | ty::UnsafeBinder(_)
761 | ty::Dynamic(..)
762 | ty::Closure(..)
763 | ty::CoroutineClosure(..)
764 | ty::Coroutine(..)
765 | ty::CoroutineWitness(..)
766 | ty::Never
767 | ty::Tuple(_)
768 | ty::Param(_)
769 | ty::Placeholder(..)
770 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
771 | ty::Error(_) => return Ok(()),
772 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
773 {
::core::panicking::panic_fmt(format_args!("unexpected self type for `{0:?}`",
goal));
}panic!("unexpected self type for `{goal:?}`")
774 }
775
776 ty::Infer(ty::TyVar(_)) => {
777 if let Ok(result) =
781 self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
782 {
783 candidates.push(Candidate {
784 source: CandidateSource::AliasBound(consider_self_bounds),
785 result,
786 head_usages: CandidateHeadUsages::default(),
787 });
788 }
789 return Ok(());
790 }
791
792 ty::Alias(
793 alias_ty @ AliasTy { kind: ty::Projection { .. } | ty::Opaque { .. }, .. },
794 ) => alias_ty,
795 ty::Alias(AliasTy { kind: ty::Inherent { .. } | ty::Free { .. }, .. }) => {
796 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"));
797 return Ok(());
798 }
799 };
800
801 match consider_self_bounds {
802 AliasBoundKind::SelfBounds => {
803 for assumption in self
804 .cx()
805 .item_self_bounds(alias_ty.kind.def_id())
806 .iter_instantiated(self.cx(), alias_ty.args)
807 .map(Unnormalized::skip_norm_wip)
808 {
809 candidates.extend(G::probe_and_consider_implied_clause(
810 self,
811 CandidateSource::AliasBound(consider_self_bounds),
812 goal,
813 assumption,
814 [],
815 ));
816 }
817 }
818 AliasBoundKind::NonSelfBounds => {
819 for assumption in self
820 .cx()
821 .item_non_self_bounds(alias_ty.kind.def_id())
822 .iter_instantiated(self.cx(), alias_ty.args)
823 .map(Unnormalized::skip_norm_wip)
824 {
825 candidates.extend(G::probe_and_consider_implied_clause(
826 self,
827 CandidateSource::AliasBound(consider_self_bounds),
828 goal,
829 assumption,
830 [],
831 ));
832 }
833 }
834 }
835
836 candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
837
838 if !#[allow(non_exhaustive_omitted_patterns)] match alias_ty.kind {
ty::Projection { .. } => true,
_ => false,
}matches!(alias_ty.kind, ty::Projection { .. }) {
839 return Ok(());
840 }
841
842 match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
844 Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
845 next_self_ty,
846 goal,
847 candidates,
848 AliasBoundKind::NonSelfBounds,
849 ),
850 Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => Ok(()),
851 Err(NoSolutionOrRerunNonErased::RerunNonErased(e)) => Err(e),
852 }
853 }
854
855 #[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(855u32),
::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)]
856 fn assemble_object_bound_candidates<G: GoalKind<D>>(
857 &mut self,
858 goal: Goal<I, G>,
859 candidates: &mut Vec<Candidate<I>>,
860 ) {
861 let cx = self.cx();
862 if cx.is_sizedness_trait(goal.predicate.trait_def_id(cx)) {
863 return;
866 }
867
868 let self_ty = goal.predicate.self_ty();
869 let bounds = match self_ty.kind() {
870 ty::Bool
871 | ty::Char
872 | ty::Int(_)
873 | ty::Uint(_)
874 | ty::Float(_)
875 | ty::Adt(_, _)
876 | ty::Foreign(_)
877 | ty::Str
878 | ty::Array(_, _)
879 | ty::Pat(_, _)
880 | ty::Slice(_)
881 | ty::RawPtr(_, _)
882 | ty::Ref(_, _, _)
883 | ty::FnDef(_, _)
884 | ty::FnPtr(..)
885 | ty::UnsafeBinder(_)
886 | ty::Alias(..)
887 | ty::Closure(..)
888 | ty::CoroutineClosure(..)
889 | ty::Coroutine(..)
890 | ty::CoroutineWitness(..)
891 | ty::Never
892 | ty::Tuple(_)
893 | ty::Param(_)
894 | ty::Placeholder(..)
895 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
896 | ty::Error(_) => return,
897 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
898 | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
899 ty::Dynamic(bounds, ..) => bounds,
900 };
901
902 if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
904 return;
905 }
906
907 for bound in bounds.iter() {
911 match bound.skip_binder() {
912 ty::ExistentialPredicate::Trait(_) => {
913 }
915 ty::ExistentialPredicate::Projection(_)
916 | ty::ExistentialPredicate::AutoTrait(_) => {
917 candidates.extend(G::probe_and_consider_object_bound_candidate(
918 self,
919 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
920 goal,
921 bound.with_self_ty(cx, self_ty),
922 ));
923 }
924 }
925 }
926
927 if let Some(principal) = bounds.principal() {
931 let principal_trait_ref = principal.with_self_ty(cx, self_ty);
932 for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
933 candidates.extend(G::probe_and_consider_object_bound_candidate(
934 self,
935 CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
936 goal,
937 assumption.upcast(cx),
938 ));
939 }
940 }
941 }
942
943 #[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(949u32),
::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>, NoSolutionOrRerunNonErased> = 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.into())
} 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)]
950 fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
951 &mut self,
952 goal: Goal<I, G>,
953 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
954 self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
955 let cx = ecx.cx();
956 let trait_ref = goal.predicate.trait_ref(cx);
957 if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
958 Err(NoSolution.into())
959 } else {
960 let predicate: I::Predicate = trait_ref.upcast(cx);
966 ecx.add_goals(
967 GoalSource::Misc,
968 elaborate::elaborate(cx, [predicate])
969 .skip(1)
970 .map(|predicate| goal.with(cx, predicate)),
971 );
972 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
973 }
974 })
975 }
976}
977
978pub(super) enum AllowInferenceConstraints {
979 Yes,
980 No,
981}
982
983impl<D, I> EvalCtxt<'_, D>
984where
985 D: SolverDelegate<Interner = I>,
986 I: Interner,
987{
988 pub(super) fn filter_specialized_impls(
992 &mut self,
993 allow_inference_constraints: AllowInferenceConstraints,
994 candidates: &mut Vec<Candidate<I>>,
995 ) {
996 if self.typing_mode().is_coherence() {
997 return;
998 }
999
1000 let mut i = 0;
1001 'outer: while i < candidates.len() {
1002 let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
1003 i += 1;
1004 continue;
1005 };
1006
1007 for (j, c) in candidates.iter().enumerate() {
1008 if i == j {
1009 continue;
1010 }
1011
1012 let CandidateSource::Impl(other_def_id) = c.source else {
1013 continue;
1014 };
1015
1016 if #[allow(non_exhaustive_omitted_patterns)] match allow_inference_constraints {
AllowInferenceConstraints::Yes => true,
_ => false,
}matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
1023 || has_only_region_constraints(c.result)
1024 {
1025 if self.cx().impl_specializes(other_def_id, victim_def_id) {
1026 candidates.remove(i);
1027 continue 'outer;
1028 }
1029 }
1030 }
1031
1032 i += 1;
1033 }
1034 }
1035
1036 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::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("try_assemble_bounds_via_registered_opaques",
"rustc_next_trait_solver::solve::assembly",
::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs"),
::tracing_core::__macro_support::Option::Some(1047u32),
::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::assembly"),
::tracing_core::field::FieldSet::new(&["goal",
"candidates"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&candidates)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Result<(), RerunNonErased> =
loop {};
return __tracing_attr_fake_return;
}
{
let self_ty = goal.predicate.self_ty();
let opaque_types =
match self.typing_mode() {
TypingMode::Typeck { .. } =>
self.opaques_with_sub_unified_hidden_type(self_ty),
TypingMode::Coherence |
TypingMode::PostTypeckUntilBorrowck { .. } |
TypingMode::PostBorrowck { .. } | TypingMode::PostAnalysis |
TypingMode::Codegen => ::alloc::vec::Vec::new(),
TypingMode::ErasedNotCoherence(MayBeErased) => {
self.opaque_accesses.rerun_if_any_opaque_has_infer_as_hidden_type(RerunReason::SelfTyInfer)?;
Vec::new()
}
};
if opaque_types.is_empty() {
candidates.extend(self.forced_ambiguity(MaybeInfo::AMBIGUOUS));
return Ok(());
}
for &alias_ty in &opaque_types {
{
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:1076",
"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(1076u32),
::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 { ; }
};
struct ReplaceOpaque<I: Interner> {
cx: I,
alias_ty: ty::AliasTy<I>,
self_ty: I::Ty,
}
impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
fn cx(&self) -> I { self.cx }
fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
if let ty::Alias(alias_ty) = ty.kind() {
if alias_ty == self.alias_ty { return self.self_ty; }
}
ty.super_fold_with(self)
}
}
for item_bound in
self.cx().item_self_bounds(alias_ty.kind.def_id()).iter_instantiated(self.cx(),
alias_ty.args).map(Unnormalized::skip_norm_wip) {
let assumption =
item_bound.fold_with(&mut ReplaceOpaque {
cx: self.cx(),
alias_ty,
self_ty,
});
candidates.extend(G::probe_and_match_goal_against_assumption(self,
CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
goal, assumption,
|ecx|
{
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}));
}
}
if assemble_from.should_assemble_impl_candidates() {
let cx = self.cx();
cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx),
|impl_def_id|
{
if cx.impl_is_default(impl_def_id) { return Ok(()); }
match G::consider_impl_candidate(self, goal, impl_def_id,
|ecx, certainty|
{
if ecx.shallow_resolve(self_ty).is_ty_var() {
let certainty = certainty.and(Certainty::AMBIGUOUS);
ecx.evaluate_added_goals_and_make_canonical_response(certainty)
} else { Err(NoSolution.into()) }
}).map_err_to_rerun()? {
Ok(candidate) => candidates.push(candidate),
Err(NoSolution) => {}
}
Ok(())
})?;
}
if candidates.is_empty() {
let source =
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
let certainty =
Certainty::Maybe(MaybeInfo {
cause: MaybeCause::Ambiguity,
opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
stalled_on_coroutines: StalledOnCoroutines::No,
});
candidates.extend(self.probe_trait_candidate(source).enter(|this|
{
this.evaluate_added_goals_and_make_canonical_response(certainty)
}));
}
Ok(())
}
}
}#[tracing::instrument(skip(self, assemble_from))]
1048 fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
1049 &mut self,
1050 goal: Goal<I, G>,
1051 assemble_from: AssembleCandidatesFrom,
1052 candidates: &mut Vec<Candidate<I>>,
1053 ) -> Result<(), RerunNonErased> {
1054 let self_ty = goal.predicate.self_ty();
1055 let opaque_types = match self.typing_mode() {
1057 TypingMode::Typeck { .. } => self.opaques_with_sub_unified_hidden_type(self_ty),
1058 TypingMode::Coherence
1059 | TypingMode::PostTypeckUntilBorrowck { .. }
1060 | TypingMode::PostBorrowck { .. }
1061 | TypingMode::PostAnalysis
1062 | TypingMode::Codegen => vec![],
1063 TypingMode::ErasedNotCoherence(MayBeErased) => {
1064 self.opaque_accesses
1065 .rerun_if_any_opaque_has_infer_as_hidden_type(RerunReason::SelfTyInfer)?;
1066 Vec::new()
1067 }
1068 };
1069
1070 if opaque_types.is_empty() {
1071 candidates.extend(self.forced_ambiguity(MaybeInfo::AMBIGUOUS));
1072 return Ok(());
1073 }
1074
1075 for &alias_ty in &opaque_types {
1076 debug!("self ty is sub unified with {alias_ty:?}");
1077
1078 struct ReplaceOpaque<I: Interner> {
1079 cx: I,
1080 alias_ty: ty::AliasTy<I>,
1081 self_ty: I::Ty,
1082 }
1083 impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
1084 fn cx(&self) -> I {
1085 self.cx
1086 }
1087 fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1088 if let ty::Alias(alias_ty) = ty.kind() {
1089 if alias_ty == self.alias_ty {
1090 return self.self_ty;
1091 }
1092 }
1093 ty.super_fold_with(self)
1094 }
1095 }
1096
1097 for item_bound in self
1105 .cx()
1106 .item_self_bounds(alias_ty.kind.def_id())
1107 .iter_instantiated(self.cx(), alias_ty.args)
1108 .map(Unnormalized::skip_norm_wip)
1109 {
1110 let assumption =
1111 item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1112 candidates.extend(G::probe_and_match_goal_against_assumption(
1113 self,
1114 CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
1115 goal,
1116 assumption,
1117 |ecx| {
1118 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1121 },
1122 ));
1123 }
1124 }
1125
1126 if assemble_from.should_assemble_impl_candidates() {
1131 let cx = self.cx();
1132 cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1133 if cx.impl_is_default(impl_def_id) {
1137 return Ok(());
1138 }
1139
1140 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1141 if ecx.shallow_resolve(self_ty).is_ty_var() {
1142 let certainty = certainty.and(Certainty::AMBIGUOUS);
1144 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1145 } else {
1146 Err(NoSolution.into())
1152 }
1153 })
1154 .map_err_to_rerun()?
1155 {
1156 Ok(candidate) => candidates.push(candidate),
1157 Err(NoSolution) => {}
1158 }
1159
1160 Ok(())
1161 })?;
1162 }
1163
1164 if candidates.is_empty() {
1165 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
1166 let certainty = Certainty::Maybe(MaybeInfo {
1167 cause: MaybeCause::Ambiguity,
1168 opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
1169 stalled_on_coroutines: StalledOnCoroutines::No,
1170 });
1171 candidates
1172 .extend(self.probe_trait_candidate(source).enter(|this| {
1173 this.evaluate_added_goals_and_make_canonical_response(certainty)
1174 }));
1175 }
1176
1177 Ok(())
1178 }
1179
1180 x;#[instrument(level = "debug", skip_all, fields(proven_via, goal), ret)]
1211 pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1212 &mut self,
1213 proven_via: Option<TraitGoalProvenVia>,
1214 goal: Goal<I, G>,
1215 inject_forced_ambiguity_candidate: impl FnOnce(
1216 &mut EvalCtxt<'_, D>,
1217 ) -> Option<
1218 Result<CanonicalResponse<I>, NoSolutionOrRerunNonErased>,
1219 >,
1220 inject_normalize_to_rigid_candidate: impl FnOnce(
1221 &mut EvalCtxt<'_, D>,
1222 ) -> Result<
1223 CanonicalResponse<I>,
1224 NoSolutionOrRerunNonErased,
1225 >,
1226 ) -> QueryResultOrRerunNonErased<I> {
1227 let Some(proven_via) = proven_via else {
1228 return self.forced_ambiguity(MaybeInfo::AMBIGUOUS).map(|cand| cand.result);
1235 };
1236
1237 match proven_via {
1238 TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1239 let (mut candidates, _) = self
1243 .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds)?;
1244 debug!(?candidates);
1245
1246 if candidates.is_empty() {
1249 return inject_normalize_to_rigid_candidate(self);
1250 }
1251
1252 if let Some(result) = inject_forced_ambiguity_candidate(self) {
1255 return result;
1256 }
1257
1258 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1261 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1262 }
1263
1264 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1265 Ok(response)
1266 } else {
1267 self.flounder(&candidates).map_err(Into::into)
1268 }
1269 }
1270 TraitGoalProvenVia::Misc => {
1271 let (mut candidates, _) =
1272 self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All)?;
1273
1274 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1277 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1278 }
1279
1280 self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1286 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1287 Ok(response)
1288 } else {
1289 self.flounder(&candidates).map_err(Into::into)
1290 }
1291 }
1292 }
1293 }
1294
1295 fn characterize_param_env_assumption(
1309 &mut self,
1310 param_env: I::ParamEnv,
1311 assumption: I::Clause,
1312 ) -> Result<(CandidateSource<I>, Certainty), NoSolution> {
1313 if assumption.has_bound_vars() {
1316 return Ok((CandidateSource::ParamEnv(ParamEnvSource::NonGlobal), Certainty::Yes));
1317 }
1318
1319 match assumption.visit_with(&mut FindParamInClause {
1320 ecx: self,
1321 param_env,
1322 universes: ::alloc::vec::Vec::new()vec![],
1323 recursion_depth: 0,
1324 }) {
1325 ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1326 ControlFlow::Break(Ok(certainty)) => {
1327 Ok((CandidateSource::ParamEnv(ParamEnvSource::NonGlobal), certainty))
1328 }
1329 ControlFlow::Continue(()) => {
1330 Ok((CandidateSource::ParamEnv(ParamEnvSource::Global), Certainty::Yes))
1331 }
1332 }
1333 }
1334}
1335
1336struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1337 ecx: &'a mut EvalCtxt<'b, D>,
1338 param_env: I::ParamEnv,
1339 universes: Vec<Option<ty::UniverseIndex>>,
1340 recursion_depth: usize,
1341}
1342
1343impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1344where
1345 D: SolverDelegate<Interner = I>,
1346 I: Interner,
1347{
1348 type Result = ControlFlow<Result<Certainty, NoSolution>>;
1353
1354 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1355 self.universes.push(None);
1356 t.super_visit_with(self)?;
1357 self.universes.pop();
1358 ControlFlow::Continue(())
1359 }
1360
1361 fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1362 let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1363 let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1364 return ControlFlow::Break(Err(NoSolution));
1365 };
1366
1367 match ty.kind() {
1368 ty::Placeholder(p) => {
1369 if p.universe() == ty::UniverseIndex::ROOT {
1370 ControlFlow::Break(Ok(Certainty::Yes))
1371 } else {
1372 ControlFlow::Continue(())
1373 }
1374 }
1375 ty::Infer(_) => ControlFlow::Break(Ok(Certainty::AMBIGUOUS)),
1376 _ if ty.has_type_flags(
1377 TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_INFER | TypeFlags::HAS_ALIAS,
1378 ) =>
1379 {
1380 self.recursion_depth += 1;
1381 if self.recursion_depth > self.ecx.cx().recursion_limit() {
1382 return ControlFlow::Break(Ok(Certainty::Maybe(MaybeInfo {
1383 cause: MaybeCause::Overflow {
1384 suggest_increasing_limit: true,
1385 keep_constraints: false,
1386 },
1387 opaque_types_jank: OpaqueTypesJank::AllGood,
1388 stalled_on_coroutines: StalledOnCoroutines::No,
1389 })));
1390 }
1391 let result = ty.super_visit_with(self);
1392 self.recursion_depth -= 1;
1393 result
1394 }
1395 _ => ControlFlow::Continue(()),
1396 }
1397 }
1398
1399 fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1400 let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1401 let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1402 return ControlFlow::Break(Err(NoSolution));
1403 };
1404
1405 match ct.kind() {
1406 ty::ConstKind::Placeholder(p) => {
1407 if p.universe() == ty::UniverseIndex::ROOT {
1408 ControlFlow::Break(Ok(Certainty::Yes))
1409 } else {
1410 ControlFlow::Continue(())
1411 }
1412 }
1413 ty::ConstKind::Infer(_) => ControlFlow::Break(Ok(Certainty::AMBIGUOUS)),
1414 _ if ct.has_type_flags(
1415 TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_INFER | TypeFlags::HAS_ALIAS,
1416 ) =>
1417 {
1418 ct.super_visit_with(self)
1420 }
1421 _ => ControlFlow::Continue(()),
1422 }
1423 }
1424
1425 fn visit_region(&mut self, r: I::Region) -> Self::Result {
1426 match self.ecx.eager_resolve_region(r).kind() {
1427 ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1428 ty::RePlaceholder(p) => {
1429 if p.universe() == ty::UniverseIndex::ROOT {
1430 ControlFlow::Break(Ok(Certainty::Yes))
1431 } else {
1432 ControlFlow::Continue(())
1433 }
1434 }
1435 ty::ReVar(_) => ControlFlow::Break(Ok(Certainty::Yes)),
1436 ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1437 {
::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")
1438 }
1439 }
1440 }
1441}