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