rustc_traits/
normalize_projection_ty.rs1use rustc_infer::infer::TyCtxtInferExt;
2use rustc_infer::infer::canonical::{Canonical, QueryResponse};
3use rustc_infer::traits::PredicateObligations;
4use rustc_middle::query::Providers;
5use rustc_middle::ty::{ParamEnvAnd, TyCtxt};
6use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
7use rustc_trait_selection::infer::InferCtxtBuilderExt;
8use rustc_trait_selection::traits::query::normalize::NormalizationResult;
9use rustc_trait_selection::traits::query::{CanonicalAliasGoal, NoSolution};
10use rustc_trait_selection::traits::{self, ObligationCause, ScrubbedTraitError, SelectionContext};
11use tracing::debug;
12
13pub(crate) fn provide(p: &mut Providers) {
14 *p = Providers {
15 normalize_canonicalized_projection_ty,
16 normalize_canonicalized_free_alias,
17 normalize_canonicalized_inherent_projection_ty,
18 ..*p
19 };
20}
21
22fn normalize_canonicalized_projection_ty<'tcx>(
23 tcx: TyCtxt<'tcx>,
24 goal: CanonicalAliasGoal<'tcx>,
25) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> {
26 debug!("normalize_canonicalized_projection_ty(goal={:#?})", goal);
27
28 tcx.infer_ctxt().enter_canonical_trait_query(
29 &goal,
30 |ocx, ParamEnvAnd { param_env, value: goal }| {
31 debug_assert!(!ocx.infcx.next_trait_solver());
32 let selcx = &mut SelectionContext::new(ocx.infcx);
33 let cause = ObligationCause::dummy();
34 let mut obligations = PredicateObligations::new();
35 let answer = traits::normalize_projection_term(
36 selcx,
37 param_env,
38 goal.into(),
39 cause,
40 0,
41 &mut obligations,
42 );
43 ocx.register_obligations(obligations);
44 let errors = ocx.select_where_possible();
49 if !errors.is_empty() {
50 if !tcx.sess.opts.actually_rustdoc {
55 for error in &errors {
56 if let ScrubbedTraitError::Cycle(cycle) = &error {
57 ocx.infcx.err_ctxt().report_overflow_obligation_cycle(cycle);
58 }
59 }
60 }
61 return Err(NoSolution);
62 }
63
64 Ok(NormalizationResult { normalized_ty: answer.expect_type() })
68 },
69 )
70}
71
72fn normalize_canonicalized_free_alias<'tcx>(
73 tcx: TyCtxt<'tcx>,
74 goal: CanonicalAliasGoal<'tcx>,
75) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> {
76 debug!("normalize_canonicalized_free_alias(goal={:#?})", goal);
77
78 tcx.infer_ctxt().enter_canonical_trait_query(
79 &goal,
80 |ocx, ParamEnvAnd { param_env, value: goal }| {
81 let obligations = tcx.predicates_of(goal.def_id).instantiate_own(tcx, goal.args).map(
82 |(predicate, span)| {
83 traits::Obligation::new(
84 tcx,
85 ObligationCause::dummy_with_span(span),
86 param_env,
87 predicate,
88 )
89 },
90 );
91 ocx.register_obligations(obligations);
92 let normalized_ty = tcx.type_of(goal.def_id).instantiate(tcx, goal.args);
93 Ok(NormalizationResult { normalized_ty })
94 },
95 )
96}
97
98fn normalize_canonicalized_inherent_projection_ty<'tcx>(
99 tcx: TyCtxt<'tcx>,
100 goal: CanonicalAliasGoal<'tcx>,
101) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> {
102 debug!("normalize_canonicalized_inherent_projection_ty(goal={:#?})", goal);
103
104 tcx.infer_ctxt().enter_canonical_trait_query(
105 &goal,
106 |ocx, ParamEnvAnd { param_env, value: goal }| {
107 let selcx = &mut SelectionContext::new(ocx.infcx);
108 let cause = ObligationCause::dummy();
109 let mut obligations = PredicateObligations::new();
110 let answer = traits::normalize_inherent_projection(
111 selcx,
112 param_env,
113 goal.into(),
114 cause,
115 0,
116 &mut obligations,
117 );
118 ocx.register_obligations(obligations);
119
120 Ok(NormalizationResult { normalized_ty: answer.expect_type() })
121 },
122 )
123}