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