rustc_next_trait_solver/solve/normalizes_to/
mod.rs

1mod anon_const;
2mod free_alias;
3mod inherent;
4mod opaque_types;
5
6use rustc_type_ir::fast_reject::DeepRejectCtxt;
7use rustc_type_ir::inherent::*;
8use rustc_type_ir::lang_items::TraitSolverLangItem;
9use rustc_type_ir::{self as ty, Interner, NormalizesTo, PredicateKind, Upcast as _};
10use tracing::instrument;
11
12use crate::delegate::SolverDelegate;
13use crate::solve::assembly::structural_traits::{self, AsyncCallableRelevantTypes};
14use crate::solve::assembly::{self, Candidate};
15use crate::solve::inspect::ProbeKind;
16use crate::solve::{
17    BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeCause,
18    NoSolution, QueryResult,
19};
20
21impl<D, I> EvalCtxt<'_, D>
22where
23    D: SolverDelegate<Interner = I>,
24    I: Interner,
25{
26    #[instrument(level = "trace", skip(self), ret)]
27    pub(super) fn compute_normalizes_to_goal(
28        &mut self,
29        goal: Goal<I, NormalizesTo<I>>,
30    ) -> QueryResult<I> {
31        debug_assert!(self.term_is_fully_unconstrained(goal));
32        let cx = self.cx();
33        match goal.predicate.alias.kind(cx) {
34            ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
35                let trait_ref = goal.predicate.alias.trait_ref(cx);
36                let (_, proven_via) =
37                    self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
38                        let trait_goal: Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
39                        ecx.compute_trait_goal(trait_goal)
40                    })?;
41                self.assemble_and_merge_candidates(proven_via, goal, |ecx| {
42                    ecx.probe(|&result| ProbeKind::RigidAlias { result }).enter(|this| {
43                        this.structurally_instantiate_normalizes_to_term(
44                            goal,
45                            goal.predicate.alias,
46                        );
47                        this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
48                    })
49                })
50            }
51            ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
52                self.normalize_inherent_associated_term(goal)
53            }
54            ty::AliasTermKind::OpaqueTy => self.normalize_opaque_type(goal),
55            ty::AliasTermKind::FreeTy | ty::AliasTermKind::FreeConst => {
56                self.normalize_free_alias(goal)
57            }
58            ty::AliasTermKind::UnevaluatedConst => self.normalize_anon_const(goal),
59        }
60    }
61
62    /// When normalizing an associated item, constrain the expected term to `term`.
63    ///
64    /// We know `term` to always be a fully unconstrained inference variable, so
65    /// `eq` should never fail here. However, in case `term` contains aliases, we
66    /// emit nested `AliasRelate` goals to structurally normalize the alias.
67    pub fn instantiate_normalizes_to_term(
68        &mut self,
69        goal: Goal<I, NormalizesTo<I>>,
70        term: I::Term,
71    ) {
72        self.eq(goal.param_env, goal.predicate.term, term)
73            .expect("expected goal term to be fully unconstrained");
74    }
75
76    /// Unlike `instantiate_normalizes_to_term` this instantiates the expected term
77    /// with a rigid alias. Using this is pretty much always wrong.
78    pub fn structurally_instantiate_normalizes_to_term(
79        &mut self,
80        goal: Goal<I, NormalizesTo<I>>,
81        term: ty::AliasTerm<I>,
82    ) {
83        self.relate_rigid_alias_non_alias(goal.param_env, term, ty::Invariant, goal.predicate.term)
84            .expect("expected goal term to be fully unconstrained");
85    }
86}
87
88impl<D, I> assembly::GoalKind<D> for NormalizesTo<I>
89where
90    D: SolverDelegate<Interner = I>,
91    I: Interner,
92{
93    fn self_ty(self) -> I::Ty {
94        self.self_ty()
95    }
96
97    fn trait_ref(self, cx: I) -> ty::TraitRef<I> {
98        self.alias.trait_ref(cx)
99    }
100
101    fn with_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
102        self.with_self_ty(cx, self_ty)
103    }
104
105    fn trait_def_id(self, cx: I) -> I::DefId {
106        self.trait_def_id(cx)
107    }
108
109    fn fast_reject_assumption(
110        ecx: &mut EvalCtxt<'_, D>,
111        goal: Goal<I, Self>,
112        assumption: I::Clause,
113    ) -> Result<(), NoSolution> {
114        if let Some(projection_pred) = assumption.as_projection_clause() {
115            if projection_pred.item_def_id() == goal.predicate.def_id() {
116                if DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
117                    goal.predicate.alias.args,
118                    projection_pred.skip_binder().projection_term.args,
119                ) {
120                    return Ok(());
121                }
122            }
123        }
124
125        Err(NoSolution)
126    }
127
128    fn match_assumption(
129        ecx: &mut EvalCtxt<'_, D>,
130        goal: Goal<I, Self>,
131        assumption: I::Clause,
132        then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
133    ) -> QueryResult<I> {
134        let cx = ecx.cx();
135        // FIXME(generic_associated_types): Addresses aggressive inference in #92917.
136        //
137        // If this type is a GAT with currently unconstrained arguments, we do not
138        // want to normalize it via a candidate which only applies for a specific
139        // instantiation. We could otherwise keep the GAT as rigid and succeed this way.
140        // See tests/ui/generic-associated-types/no-incomplete-gat-arg-inference.rs.
141        //
142        // This only avoids normalization if the GAT arguments are fully unconstrained.
143        // This is quite arbitrary but fixing it causes some ambiguity, see #125196.
144        match goal.predicate.alias.kind(cx) {
145            ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::ProjectionConst => {
146                for arg in goal.predicate.alias.own_args(cx).iter() {
147                    let Some(term) = arg.as_term() else {
148                        continue;
149                    };
150                    let term = ecx.structurally_normalize_term(goal.param_env, term)?;
151                    if term.is_infer() {
152                        return ecx.evaluate_added_goals_and_make_canonical_response(
153                            Certainty::AMBIGUOUS,
154                        );
155                    }
156                }
157            }
158            ty::AliasTermKind::OpaqueTy
159            | ty::AliasTermKind::InherentTy
160            | ty::AliasTermKind::InherentConst
161            | ty::AliasTermKind::FreeTy
162            | ty::AliasTermKind::FreeConst
163            | ty::AliasTermKind::UnevaluatedConst => {}
164        }
165
166        let projection_pred = assumption.as_projection_clause().unwrap();
167
168        let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
169        ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
170
171        ecx.instantiate_normalizes_to_term(goal, assumption_projection_pred.term);
172
173        // Add GAT where clauses from the trait's definition
174        // FIXME: We don't need these, since these are the type's own WF obligations.
175        ecx.add_goals(
176            GoalSource::AliasWellFormed,
177            cx.own_predicates_of(goal.predicate.def_id())
178                .iter_instantiated(cx, goal.predicate.alias.args)
179                .map(|pred| goal.with(cx, pred)),
180        );
181
182        then(ecx)
183    }
184
185    fn consider_additional_alias_assumptions(
186        _ecx: &mut EvalCtxt<'_, D>,
187        _goal: Goal<I, Self>,
188        _alias_ty: ty::AliasTy<I>,
189    ) -> Vec<Candidate<I>> {
190        vec![]
191    }
192
193    fn consider_impl_candidate(
194        ecx: &mut EvalCtxt<'_, D>,
195        goal: Goal<I, NormalizesTo<I>>,
196        impl_def_id: I::DefId,
197    ) -> Result<Candidate<I>, NoSolution> {
198        let cx = ecx.cx();
199
200        let goal_trait_ref = goal.predicate.alias.trait_ref(cx);
201        let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
202        if !DeepRejectCtxt::relate_rigid_infer(ecx.cx()).args_may_unify(
203            goal.predicate.alias.trait_ref(cx).args,
204            impl_trait_ref.skip_binder().args,
205        ) {
206            return Err(NoSolution);
207        }
208
209        // We have to ignore negative impls when projecting.
210        let impl_polarity = cx.impl_polarity(impl_def_id);
211        match impl_polarity {
212            ty::ImplPolarity::Negative => return Err(NoSolution),
213            ty::ImplPolarity::Reservation => {
214                unimplemented!("reservation impl for trait with assoc item: {:?}", goal)
215            }
216            ty::ImplPolarity::Positive => {}
217        };
218
219        ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
220            let impl_args = ecx.fresh_args_for_item(impl_def_id);
221            let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);
222
223            ecx.eq(goal.param_env, goal_trait_ref, impl_trait_ref)?;
224
225            let where_clause_bounds = cx
226                .predicates_of(impl_def_id)
227                .iter_instantiated(cx, impl_args)
228                .map(|pred| goal.with(cx, pred));
229            ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
230
231            // Bail if the nested goals don't hold here. This is to avoid unnecessarily
232            // computing the `type_of` query for associated types that never apply, as
233            // this may result in query cycles in the case of RPITITs.
234            // See <https://github.com/rust-lang/trait-system-refactor-initiative/issues/185>.
235            ecx.try_evaluate_added_goals()?;
236
237            // Add GAT where clauses from the trait's definition.
238            // FIXME: We don't need these, since these are the type's own WF obligations.
239            ecx.add_goals(
240                GoalSource::AliasWellFormed,
241                cx.own_predicates_of(goal.predicate.def_id())
242                    .iter_instantiated(cx, goal.predicate.alias.args)
243                    .map(|pred| goal.with(cx, pred)),
244            );
245
246            let error_response = |ecx: &mut EvalCtxt<'_, D>, guar| {
247                let error_term = match goal.predicate.alias.kind(cx) {
248                    ty::AliasTermKind::ProjectionTy => Ty::new_error(cx, guar).into(),
249                    ty::AliasTermKind::ProjectionConst => Const::new_error(cx, guar).into(),
250                    kind => panic!("expected projection, found {kind:?}"),
251                };
252                ecx.instantiate_normalizes_to_term(goal, error_term);
253                ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
254            };
255
256            let target_item_def_id = match ecx.fetch_eligible_assoc_item(
257                goal_trait_ref,
258                goal.predicate.def_id(),
259                impl_def_id,
260            ) {
261                Ok(Some(target_item_def_id)) => target_item_def_id,
262                Ok(None) => {
263                    match ecx.typing_mode() {
264                        // In case the associated item is hidden due to specialization,
265                        // normalizing this associated item is always ambiguous. Treating
266                        // the associated item as rigid would be incomplete and allow for
267                        // overlapping impls, see #105782.
268                        //
269                        // As this ambiguity is unavoidable we emit a nested ambiguous
270                        // goal instead of using `Certainty::AMBIGUOUS`. This allows us to
271                        // return the nested goals to the parent `AliasRelate` goal. This
272                        // would be relevant if any of the nested goals refer to the `term`.
273                        // This is not the case here and we only prefer adding an ambiguous
274                        // nested goal for consistency.
275                        ty::TypingMode::Coherence => {
276                            ecx.add_goal(GoalSource::Misc, goal.with(cx, PredicateKind::Ambiguous));
277                            return ecx
278                                .evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
279                        }
280                        // Outside of coherence, we treat the associated item as rigid instead.
281                        ty::TypingMode::Analysis { .. }
282                        | ty::TypingMode::Borrowck { .. }
283                        | ty::TypingMode::PostBorrowckAnalysis { .. }
284                        | ty::TypingMode::PostAnalysis => {
285                            ecx.structurally_instantiate_normalizes_to_term(
286                                goal,
287                                goal.predicate.alias,
288                            );
289                            return ecx
290                                .evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
291                        }
292                    };
293                }
294                Err(guar) => return error_response(ecx, guar),
295            };
296
297            if !cx.has_item_definition(target_item_def_id) {
298                // If the impl is missing an item, it's either because the user forgot to
299                // provide it, or the user is not *obligated* to provide it (because it
300                // has a trivially false `Sized` predicate). If it's the latter, we cannot
301                // delay a bug because we can have trivially false where clauses, so we
302                // treat it as rigid.
303                if cx.impl_self_is_guaranteed_unsized(impl_def_id) {
304                    match ecx.typing_mode() {
305                        // Trying to normalize such associated items is always ambiguous
306                        // during coherence to avoid cyclic reasoning. See the example in
307                        // tests/ui/traits/trivial-unsized-projection-in-coherence.rs.
308                        //
309                        // As this ambiguity is unavoidable we emit a nested ambiguous
310                        // goal instead of using `Certainty::AMBIGUOUS`. This allows us to
311                        // return the nested goals to the parent `AliasRelate` goal. This
312                        // would be relevant if any of the nested goals refer to the `term`.
313                        // This is not the case here and we only prefer adding an ambiguous
314                        // nested goal for consistency.
315                        ty::TypingMode::Coherence => {
316                            ecx.add_goal(GoalSource::Misc, goal.with(cx, PredicateKind::Ambiguous));
317                            return ecx
318                                .evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
319                        }
320                        ty::TypingMode::Analysis { .. }
321                        | ty::TypingMode::Borrowck { .. }
322                        | ty::TypingMode::PostBorrowckAnalysis { .. }
323                        | ty::TypingMode::PostAnalysis => {
324                            ecx.structurally_instantiate_normalizes_to_term(
325                                goal,
326                                goal.predicate.alias,
327                            );
328                            return ecx
329                                .evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
330                        }
331                    }
332                } else {
333                    return error_response(ecx, cx.delay_bug("missing item"));
334                }
335            }
336
337            let target_container_def_id = cx.parent(target_item_def_id);
338
339            // Getting the right args here is complex, e.g. given:
340            // - a goal `<Vec<u32> as Trait<i32>>::Assoc<u64>`
341            // - the applicable impl `impl<T> Trait<i32> for Vec<T>`
342            // - and the impl which defines `Assoc` being `impl<T, U> Trait<U> for Vec<T>`
343            //
344            // We first rebase the goal args onto the impl, going from `[Vec<u32>, i32, u64]`
345            // to `[u32, u64]`.
346            //
347            // And then map these args to the args of the defining impl of `Assoc`, going
348            // from `[u32, u64]` to `[u32, i32, u64]`.
349            let target_args = ecx.translate_args(
350                goal,
351                impl_def_id,
352                impl_args,
353                impl_trait_ref,
354                target_container_def_id,
355            )?;
356
357            if !cx.check_args_compatible(target_item_def_id, target_args) {
358                return error_response(
359                    ecx,
360                    cx.delay_bug("associated item has mismatched arguments"),
361                );
362            }
363
364            // Finally we construct the actual value of the associated type.
365            let term = match goal.predicate.alias.kind(cx) {
366                ty::AliasTermKind::ProjectionTy => {
367                    cx.type_of(target_item_def_id).map_bound(|ty| ty.into())
368                }
369                ty::AliasTermKind::ProjectionConst => {
370                    // FIXME(mgca): once const items are actual aliases defined as equal to type system consts
371                    // this should instead return that.
372                    if cx.features().associated_const_equality() {
373                        panic!("associated const projection is not supported yet")
374                    } else {
375                        ty::EarlyBinder::bind(
376                            Const::new_error_with_message(
377                                cx,
378                                "associated const projection is not supported yet",
379                            )
380                            .into(),
381                        )
382                    }
383                }
384                kind => panic!("expected projection, found {kind:?}"),
385            };
386
387            ecx.instantiate_normalizes_to_term(goal, term.instantiate(cx, target_args));
388            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
389        })
390    }
391
392    /// Fail to normalize if the predicate contains an error, alternatively, we could normalize to `ty::Error`
393    /// and succeed. Can experiment with this to figure out what results in better error messages.
394    fn consider_error_guaranteed_candidate(
395        _ecx: &mut EvalCtxt<'_, D>,
396        _guar: I::ErrorGuaranteed,
397    ) -> Result<Candidate<I>, NoSolution> {
398        Err(NoSolution)
399    }
400
401    fn consider_auto_trait_candidate(
402        ecx: &mut EvalCtxt<'_, D>,
403        _goal: Goal<I, Self>,
404    ) -> Result<Candidate<I>, NoSolution> {
405        ecx.cx().delay_bug("associated types not allowed on auto traits");
406        Err(NoSolution)
407    }
408
409    fn consider_trait_alias_candidate(
410        _ecx: &mut EvalCtxt<'_, D>,
411        goal: Goal<I, Self>,
412    ) -> Result<Candidate<I>, NoSolution> {
413        panic!("trait aliases do not have associated types: {:?}", goal);
414    }
415
416    fn consider_builtin_sized_candidate(
417        _ecx: &mut EvalCtxt<'_, D>,
418        goal: Goal<I, Self>,
419    ) -> Result<Candidate<I>, NoSolution> {
420        panic!("`Sized` does not have an associated type: {:?}", goal);
421    }
422
423    fn consider_builtin_copy_clone_candidate(
424        _ecx: &mut EvalCtxt<'_, D>,
425        goal: Goal<I, Self>,
426    ) -> Result<Candidate<I>, NoSolution> {
427        panic!("`Copy`/`Clone` does not have an associated type: {:?}", goal);
428    }
429
430    fn consider_builtin_fn_ptr_trait_candidate(
431        _ecx: &mut EvalCtxt<'_, D>,
432        goal: Goal<I, Self>,
433    ) -> Result<Candidate<I>, NoSolution> {
434        panic!("`FnPtr` does not have an associated type: {:?}", goal);
435    }
436
437    fn consider_builtin_fn_trait_candidates(
438        ecx: &mut EvalCtxt<'_, D>,
439        goal: Goal<I, Self>,
440        goal_kind: ty::ClosureKind,
441    ) -> Result<Candidate<I>, NoSolution> {
442        let cx = ecx.cx();
443        let tupled_inputs_and_output =
444            match structural_traits::extract_tupled_inputs_and_output_from_callable(
445                cx,
446                goal.predicate.self_ty(),
447                goal_kind,
448            )? {
449                Some(tupled_inputs_and_output) => tupled_inputs_and_output,
450                None => {
451                    return ecx.forced_ambiguity(MaybeCause::Ambiguity);
452                }
453            };
454
455        // A built-in `Fn` impl only holds if the output is sized.
456        // (FIXME: technically we only need to check this if the type is a fn ptr...)
457        let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| {
458            ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output])
459        });
460
461        let pred = tupled_inputs_and_output
462            .map_bound(|(inputs, output)| ty::ProjectionPredicate {
463                projection_term: ty::AliasTerm::new(
464                    cx,
465                    goal.predicate.def_id(),
466                    [goal.predicate.self_ty(), inputs],
467                ),
468                term: output.into(),
469            })
470            .upcast(cx);
471
472        Self::probe_and_consider_implied_clause(
473            ecx,
474            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
475            goal,
476            pred,
477            [(GoalSource::ImplWhereBound, goal.with(cx, output_is_sized_pred))],
478        )
479    }
480
481    fn consider_builtin_async_fn_trait_candidates(
482        ecx: &mut EvalCtxt<'_, D>,
483        goal: Goal<I, Self>,
484        goal_kind: ty::ClosureKind,
485    ) -> Result<Candidate<I>, NoSolution> {
486        let cx = ecx.cx();
487
488        let env_region = match goal_kind {
489            ty::ClosureKind::Fn | ty::ClosureKind::FnMut => goal.predicate.alias.args.region_at(2),
490            // Doesn't matter what this region is
491            ty::ClosureKind::FnOnce => Region::new_static(cx),
492        };
493        let (tupled_inputs_and_output_and_coroutine, nested_preds) =
494            structural_traits::extract_tupled_inputs_and_output_from_async_callable(
495                cx,
496                goal.predicate.self_ty(),
497                goal_kind,
498                env_region,
499            )?;
500
501        // A built-in `AsyncFn` impl only holds if the output is sized.
502        // (FIXME: technically we only need to check this if the type is a fn ptr...)
503        let output_is_sized_pred = tupled_inputs_and_output_and_coroutine.map_bound(
504            |AsyncCallableRelevantTypes { output_coroutine_ty: output_ty, .. }| {
505                ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output_ty])
506            },
507        );
508
509        let pred = tupled_inputs_and_output_and_coroutine
510            .map_bound(
511                |AsyncCallableRelevantTypes {
512                     tupled_inputs_ty,
513                     output_coroutine_ty,
514                     coroutine_return_ty,
515                 }| {
516                    let (projection_term, term) = if cx
517                        .is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::CallOnceFuture)
518                    {
519                        (
520                            ty::AliasTerm::new(
521                                cx,
522                                goal.predicate.def_id(),
523                                [goal.predicate.self_ty(), tupled_inputs_ty],
524                            ),
525                            output_coroutine_ty.into(),
526                        )
527                    } else if cx
528                        .is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::CallRefFuture)
529                    {
530                        (
531                            ty::AliasTerm::new(
532                                cx,
533                                goal.predicate.def_id(),
534                                [
535                                    I::GenericArg::from(goal.predicate.self_ty()),
536                                    tupled_inputs_ty.into(),
537                                    env_region.into(),
538                                ],
539                            ),
540                            output_coroutine_ty.into(),
541                        )
542                    } else if cx.is_lang_item(
543                        goal.predicate.def_id(),
544                        TraitSolverLangItem::AsyncFnOnceOutput,
545                    ) {
546                        (
547                            ty::AliasTerm::new(
548                                cx,
549                                goal.predicate.def_id(),
550                                [
551                                    I::GenericArg::from(goal.predicate.self_ty()),
552                                    tupled_inputs_ty.into(),
553                                ],
554                            ),
555                            coroutine_return_ty.into(),
556                        )
557                    } else {
558                        panic!(
559                            "no such associated type in `AsyncFn*`: {:?}",
560                            goal.predicate.def_id()
561                        )
562                    };
563                    ty::ProjectionPredicate { projection_term, term }
564                },
565            )
566            .upcast(cx);
567
568        Self::probe_and_consider_implied_clause(
569            ecx,
570            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
571            goal,
572            pred,
573            [goal.with(cx, output_is_sized_pred)]
574                .into_iter()
575                .chain(nested_preds.into_iter().map(|pred| goal.with(cx, pred)))
576                .map(|goal| (GoalSource::ImplWhereBound, goal)),
577        )
578    }
579
580    fn consider_builtin_async_fn_kind_helper_candidate(
581        ecx: &mut EvalCtxt<'_, D>,
582        goal: Goal<I, Self>,
583    ) -> Result<Candidate<I>, NoSolution> {
584        let [
585            closure_fn_kind_ty,
586            goal_kind_ty,
587            borrow_region,
588            tupled_inputs_ty,
589            tupled_upvars_ty,
590            coroutine_captures_by_ref_ty,
591        ] = *goal.predicate.alias.args.as_slice()
592        else {
593            panic!();
594        };
595
596        // Bail if the upvars haven't been constrained.
597        if tupled_upvars_ty.expect_ty().is_ty_var() {
598            return ecx.forced_ambiguity(MaybeCause::Ambiguity);
599        }
600
601        let Some(closure_kind) = closure_fn_kind_ty.expect_ty().to_opt_closure_kind() else {
602            // We don't need to worry about the self type being an infer var.
603            return Err(NoSolution);
604        };
605        let Some(goal_kind) = goal_kind_ty.expect_ty().to_opt_closure_kind() else {
606            return Err(NoSolution);
607        };
608        if !closure_kind.extends(goal_kind) {
609            return Err(NoSolution);
610        }
611
612        let upvars_ty = ty::CoroutineClosureSignature::tupled_upvars_by_closure_kind(
613            ecx.cx(),
614            goal_kind,
615            tupled_inputs_ty.expect_ty(),
616            tupled_upvars_ty.expect_ty(),
617            coroutine_captures_by_ref_ty.expect_ty(),
618            borrow_region.expect_region(),
619        );
620
621        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
622            ecx.instantiate_normalizes_to_term(goal, upvars_ty.into());
623            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
624        })
625    }
626
627    fn consider_builtin_tuple_candidate(
628        _ecx: &mut EvalCtxt<'_, D>,
629        goal: Goal<I, Self>,
630    ) -> Result<Candidate<I>, NoSolution> {
631        panic!("`Tuple` does not have an associated type: {:?}", goal);
632    }
633
634    fn consider_builtin_pointee_candidate(
635        ecx: &mut EvalCtxt<'_, D>,
636        goal: Goal<I, Self>,
637    ) -> Result<Candidate<I>, NoSolution> {
638        let cx = ecx.cx();
639        let metadata_def_id = cx.require_lang_item(TraitSolverLangItem::Metadata);
640        assert_eq!(metadata_def_id, goal.predicate.def_id());
641        let metadata_ty = match goal.predicate.self_ty().kind() {
642            ty::Bool
643            | ty::Char
644            | ty::Int(..)
645            | ty::Uint(..)
646            | ty::Float(..)
647            | ty::Array(..)
648            | ty::Pat(..)
649            | ty::RawPtr(..)
650            | ty::Ref(..)
651            | ty::FnDef(..)
652            | ty::FnPtr(..)
653            | ty::Closure(..)
654            | ty::CoroutineClosure(..)
655            | ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
656            | ty::Coroutine(..)
657            | ty::CoroutineWitness(..)
658            | ty::Never
659            | ty::Foreign(..)
660            | ty::Dynamic(_, _, ty::DynStar) => Ty::new_unit(cx),
661
662            ty::Error(e) => Ty::new_error(cx, e),
663
664            ty::Str | ty::Slice(_) => Ty::new_usize(cx),
665
666            ty::Dynamic(_, _, ty::Dyn) => {
667                let dyn_metadata = cx.require_lang_item(TraitSolverLangItem::DynMetadata);
668                cx.type_of(dyn_metadata)
669                    .instantiate(cx, &[I::GenericArg::from(goal.predicate.self_ty())])
670            }
671
672            ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
673                // This is the "fallback impl" for type parameters, unnormalizable projections
674                // and opaque types: If the `self_ty` is `Sized`, then the metadata is `()`.
675                // FIXME(ptr_metadata): This impl overlaps with the other impls and shouldn't
676                // exist. Instead, `Pointee<Metadata = ()>` should be a supertrait of `Sized`.
677                let alias_bound_result =
678                    ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
679                        let sized_predicate = ty::TraitRef::new(
680                            cx,
681                            cx.require_lang_item(TraitSolverLangItem::Sized),
682                            [I::GenericArg::from(goal.predicate.self_ty())],
683                        );
684                        ecx.add_goal(GoalSource::Misc, goal.with(cx, sized_predicate));
685                        ecx.instantiate_normalizes_to_term(goal, Ty::new_unit(cx).into());
686                        ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
687                    });
688                // In case the dummy alias-bound candidate does not apply, we instead treat this projection
689                // as rigid.
690                return alias_bound_result.or_else(|NoSolution| {
691                    ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|this| {
692                        this.structurally_instantiate_normalizes_to_term(
693                            goal,
694                            goal.predicate.alias,
695                        );
696                        this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
697                    })
698                });
699            }
700
701            ty::Adt(def, args) if def.is_struct() => match def.struct_tail_ty(cx) {
702                None => Ty::new_unit(cx),
703                Some(tail_ty) => {
704                    Ty::new_projection(cx, metadata_def_id, [tail_ty.instantiate(cx, args)])
705                }
706            },
707            ty::Adt(_, _) => Ty::new_unit(cx),
708
709            ty::Tuple(elements) => match elements.last() {
710                None => Ty::new_unit(cx),
711                Some(tail_ty) => Ty::new_projection(cx, metadata_def_id, [tail_ty]),
712            },
713
714            ty::UnsafeBinder(_) => {
715                // FIXME(unsafe_binder): Figure out how to handle pointee for unsafe binders.
716                todo!()
717            }
718
719            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
720            | ty::Bound(..) => panic!(
721                "unexpected self ty `{:?}` when normalizing `<T as Pointee>::Metadata`",
722                goal.predicate.self_ty()
723            ),
724        };
725
726        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
727            ecx.instantiate_normalizes_to_term(goal, metadata_ty.into());
728            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
729        })
730    }
731
732    fn consider_builtin_future_candidate(
733        ecx: &mut EvalCtxt<'_, D>,
734        goal: Goal<I, Self>,
735    ) -> Result<Candidate<I>, NoSolution> {
736        let self_ty = goal.predicate.self_ty();
737        let ty::Coroutine(def_id, args) = self_ty.kind() else {
738            return Err(NoSolution);
739        };
740
741        // Coroutines are not futures unless they come from `async` desugaring
742        let cx = ecx.cx();
743        if !cx.coroutine_is_async(def_id) {
744            return Err(NoSolution);
745        }
746
747        let term = args.as_coroutine().return_ty().into();
748
749        Self::probe_and_consider_implied_clause(
750            ecx,
751            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
752            goal,
753            ty::ProjectionPredicate {
754                projection_term: ty::AliasTerm::new(ecx.cx(), goal.predicate.def_id(), [self_ty]),
755                term,
756            }
757            .upcast(cx),
758            // Technically, we need to check that the future type is Sized,
759            // but that's already proven by the coroutine being WF.
760            [],
761        )
762    }
763
764    fn consider_builtin_iterator_candidate(
765        ecx: &mut EvalCtxt<'_, D>,
766        goal: Goal<I, Self>,
767    ) -> Result<Candidate<I>, NoSolution> {
768        let self_ty = goal.predicate.self_ty();
769        let ty::Coroutine(def_id, args) = self_ty.kind() else {
770            return Err(NoSolution);
771        };
772
773        // Coroutines are not Iterators unless they come from `gen` desugaring
774        let cx = ecx.cx();
775        if !cx.coroutine_is_gen(def_id) {
776            return Err(NoSolution);
777        }
778
779        let term = args.as_coroutine().yield_ty().into();
780
781        Self::probe_and_consider_implied_clause(
782            ecx,
783            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
784            goal,
785            ty::ProjectionPredicate {
786                projection_term: ty::AliasTerm::new(ecx.cx(), goal.predicate.def_id(), [self_ty]),
787                term,
788            }
789            .upcast(cx),
790            // Technically, we need to check that the iterator type is Sized,
791            // but that's already proven by the generator being WF.
792            [],
793        )
794    }
795
796    fn consider_builtin_fused_iterator_candidate(
797        _ecx: &mut EvalCtxt<'_, D>,
798        goal: Goal<I, Self>,
799    ) -> Result<Candidate<I>, NoSolution> {
800        panic!("`FusedIterator` does not have an associated type: {:?}", goal);
801    }
802
803    fn consider_builtin_async_iterator_candidate(
804        ecx: &mut EvalCtxt<'_, D>,
805        goal: Goal<I, Self>,
806    ) -> Result<Candidate<I>, NoSolution> {
807        let self_ty = goal.predicate.self_ty();
808        let ty::Coroutine(def_id, args) = self_ty.kind() else {
809            return Err(NoSolution);
810        };
811
812        // Coroutines are not AsyncIterators unless they come from `gen` desugaring
813        let cx = ecx.cx();
814        if !cx.coroutine_is_async_gen(def_id) {
815            return Err(NoSolution);
816        }
817
818        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
819            let expected_ty = ecx.next_ty_infer();
820            // Take `AsyncIterator<Item = I>` and turn it into the corresponding
821            // coroutine yield ty `Poll<Option<I>>`.
822            let wrapped_expected_ty = Ty::new_adt(
823                cx,
824                cx.adt_def(cx.require_lang_item(TraitSolverLangItem::Poll)),
825                cx.mk_args(&[Ty::new_adt(
826                    cx,
827                    cx.adt_def(cx.require_lang_item(TraitSolverLangItem::Option)),
828                    cx.mk_args(&[expected_ty.into()]),
829                )
830                .into()]),
831            );
832            let yield_ty = args.as_coroutine().yield_ty();
833            ecx.eq(goal.param_env, wrapped_expected_ty, yield_ty)?;
834            ecx.instantiate_normalizes_to_term(goal, expected_ty.into());
835            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
836        })
837    }
838
839    fn consider_builtin_coroutine_candidate(
840        ecx: &mut EvalCtxt<'_, D>,
841        goal: Goal<I, Self>,
842    ) -> Result<Candidate<I>, NoSolution> {
843        let self_ty = goal.predicate.self_ty();
844        let ty::Coroutine(def_id, args) = self_ty.kind() else {
845            return Err(NoSolution);
846        };
847
848        // `async`-desugared coroutines do not implement the coroutine trait
849        let cx = ecx.cx();
850        if !cx.is_general_coroutine(def_id) {
851            return Err(NoSolution);
852        }
853
854        let coroutine = args.as_coroutine();
855
856        let term = if cx.is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::CoroutineReturn)
857        {
858            coroutine.return_ty().into()
859        } else if cx.is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::CoroutineYield) {
860            coroutine.yield_ty().into()
861        } else {
862            panic!("unexpected associated item `{:?}` for `{self_ty:?}`", goal.predicate.def_id())
863        };
864
865        Self::probe_and_consider_implied_clause(
866            ecx,
867            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
868            goal,
869            ty::ProjectionPredicate {
870                projection_term: ty::AliasTerm::new(
871                    ecx.cx(),
872                    goal.predicate.def_id(),
873                    [self_ty, coroutine.resume_ty()],
874                ),
875                term,
876            }
877            .upcast(cx),
878            // Technically, we need to check that the coroutine type is Sized,
879            // but that's already proven by the coroutine being WF.
880            [],
881        )
882    }
883
884    fn consider_structural_builtin_unsize_candidates(
885        _ecx: &mut EvalCtxt<'_, D>,
886        goal: Goal<I, Self>,
887    ) -> Vec<Candidate<I>> {
888        panic!("`Unsize` does not have an associated type: {:?}", goal);
889    }
890
891    fn consider_builtin_discriminant_kind_candidate(
892        ecx: &mut EvalCtxt<'_, D>,
893        goal: Goal<I, Self>,
894    ) -> Result<Candidate<I>, NoSolution> {
895        let self_ty = goal.predicate.self_ty();
896        let discriminant_ty = match self_ty.kind() {
897            ty::Bool
898            | ty::Char
899            | ty::Int(..)
900            | ty::Uint(..)
901            | ty::Float(..)
902            | ty::Array(..)
903            | ty::Pat(..)
904            | ty::RawPtr(..)
905            | ty::Ref(..)
906            | ty::FnDef(..)
907            | ty::FnPtr(..)
908            | ty::Closure(..)
909            | ty::CoroutineClosure(..)
910            | ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
911            | ty::Coroutine(..)
912            | ty::CoroutineWitness(..)
913            | ty::Never
914            | ty::Foreign(..)
915            | ty::Adt(_, _)
916            | ty::Str
917            | ty::Slice(_)
918            | ty::Dynamic(_, _, _)
919            | ty::Tuple(_)
920            | ty::Error(_) => self_ty.discriminant_ty(ecx.cx()),
921
922            ty::UnsafeBinder(_) => {
923                // FIXME(unsafe_binders): instantiate this with placeholders?? i guess??
924                todo!("discr subgoal...")
925            }
926
927            // Given an alias, parameter, or placeholder we add an impl candidate normalizing to a rigid
928            // alias. In case there's a where-bound further constraining this alias it is preferred over
929            // this impl candidate anyways. It's still a bit scuffed.
930            ty::Alias(_, _) | ty::Param(_) | ty::Placeholder(..) => {
931                return ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
932                    ecx.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
933                    ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
934                });
935            }
936
937            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
938            | ty::Bound(..) => panic!(
939                "unexpected self ty `{:?}` when normalizing `<T as DiscriminantKind>::Discriminant`",
940                goal.predicate.self_ty()
941            ),
942        };
943
944        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
945            ecx.instantiate_normalizes_to_term(goal, discriminant_ty.into());
946            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
947        })
948    }
949
950    fn consider_builtin_destruct_candidate(
951        _ecx: &mut EvalCtxt<'_, D>,
952        goal: Goal<I, Self>,
953    ) -> Result<Candidate<I>, NoSolution> {
954        panic!("`Destruct` does not have an associated type: {:?}", goal);
955    }
956
957    fn consider_builtin_transmute_candidate(
958        _ecx: &mut EvalCtxt<'_, D>,
959        goal: Goal<I, Self>,
960    ) -> Result<Candidate<I>, NoSolution> {
961        panic!("`TransmuteFrom` does not have an associated type: {:?}", goal)
962    }
963
964    fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
965        _ecx: &mut EvalCtxt<'_, D>,
966        goal: Goal<I, Self>,
967    ) -> Result<Candidate<I>, NoSolution> {
968        unreachable!("`BikeshedGuaranteedNoDrop` does not have an associated type: {:?}", goal)
969    }
970}
971
972impl<D, I> EvalCtxt<'_, D>
973where
974    D: SolverDelegate<Interner = I>,
975    I: Interner,
976{
977    fn translate_args(
978        &mut self,
979        goal: Goal<I, ty::NormalizesTo<I>>,
980        impl_def_id: I::DefId,
981        impl_args: I::GenericArgs,
982        impl_trait_ref: rustc_type_ir::TraitRef<I>,
983        target_container_def_id: I::DefId,
984    ) -> Result<I::GenericArgs, NoSolution> {
985        let cx = self.cx();
986        Ok(if target_container_def_id == impl_trait_ref.def_id {
987            // Default value from the trait definition. No need to rebase.
988            goal.predicate.alias.args
989        } else if target_container_def_id == impl_def_id {
990            // Same impl, no need to fully translate, just a rebase from
991            // the trait is sufficient.
992            goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id, impl_args)
993        } else {
994            let target_args = self.fresh_args_for_item(target_container_def_id);
995            let target_trait_ref =
996                cx.impl_trait_ref(target_container_def_id).instantiate(cx, target_args);
997            // Relate source impl to target impl by equating trait refs.
998            self.eq(goal.param_env, impl_trait_ref, target_trait_ref)?;
999            // Also add predicates since they may be needed to constrain the
1000            // target impl's params.
1001            self.add_goals(
1002                GoalSource::Misc,
1003                cx.predicates_of(target_container_def_id)
1004                    .iter_instantiated(cx, target_args)
1005                    .map(|pred| goal.with(cx, pred)),
1006            );
1007            goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id, target_args)
1008        })
1009    }
1010}