Skip to main content

rustc_traits/
normalize_projection_ty.rs

1use 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::{self, 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,
16        normalize_canonicalized_free_alias,
17        normalize_canonicalized_inherent_projection,
18        ..*p
19    };
20}
21
22/// If `normalized_term` is a const, returns a `ConstArgHasType` obligation
23/// to verify that the const value's type matches the alias's declared type.
24/// Returns `None` if the term is a type rather than a const.
25fn const_arg_has_type_obligation<'tcx>(
26    tcx: TyCtxt<'tcx>,
27    param_env: ty::ParamEnv<'tcx>,
28    normalized_term: ty::Term<'tcx>,
29    goal: ty::AliasTerm<'tcx>,
30) -> Option<traits::PredicateObligation<'tcx>> {
31    let ct = normalized_term.as_const()?;
32    let expected_ty = goal.expect_ct().type_of(tcx).skip_norm_wip();
33    Some(traits::Obligation::new(
34        tcx,
35        ObligationCause::dummy(),
36        param_env,
37        ty::ClauseKind::ConstArgHasType(ct, expected_ty),
38    ))
39}
40
41fn normalize_canonicalized_projection<'tcx>(
42    tcx: TyCtxt<'tcx>,
43    goal: CanonicalAliasGoal<'tcx>,
44) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> {
45    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/5a2be9f5f075d31e3ca5526b5b029881ce441253/compiler/rustc_traits/src/normalize_projection_ty.rs:45",
                        "rustc_traits::normalize_projection_ty",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/5a2be9f5f075d31e3ca5526b5b029881ce441253/compiler/rustc_traits/src/normalize_projection_ty.rs"),
                        ::tracing_core::__macro_support::Option::Some(45u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::normalize_projection_ty"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("normalize_canonicalized_projection(goal={0:#?})",
                                                    goal) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("normalize_canonicalized_projection(goal={:#?})", goal);
46
47    tcx.infer_ctxt().enter_canonical_trait_query(
48        &goal,
49        |ocx, ParamEnvAnd { param_env, value: goal }| {
50            if true {
    if !!ocx.infcx.next_trait_solver() {
        ::core::panicking::panic("assertion failed: !ocx.infcx.next_trait_solver()")
    };
};debug_assert!(!ocx.infcx.next_trait_solver());
51            let selcx = &mut SelectionContext::new(ocx.infcx);
52            let cause = ObligationCause::dummy();
53            let mut obligations = PredicateObligations::new();
54            let normalized_term = traits::normalize_projection_term(
55                selcx,
56                param_env,
57                goal,
58                cause,
59                0,
60                &mut obligations,
61            );
62            ocx.register_obligations(obligations);
63            // #112047: With projections and opaques, we are able to create opaques that
64            // are recursive (given some generic parameters of the opaque's type variables).
65            // In that case, we may only realize a cycle error when calling
66            // `normalize_erasing_regions` in mono.
67            let errors = ocx.try_evaluate_obligations();
68            if !errors.no_errors() {
69                // Rustdoc may attempt to normalize type alias types which are not
70                // well-formed. Rustdoc also normalizes types that are just not
71                // well-formed, since we don't do as much HIR analysis (checking
72                // that impl vars are constrained by the signature, for example).
73                if !tcx.sess.opts.actually_rustdoc {
74                    for error in &errors {
75                        if let ScrubbedTraitError::Cycle(cycle) = &error {
76                            ocx.infcx.err_ctxt().report_overflow_obligation_cycle(cycle);
77                        }
78                    }
79                }
80                return Err(NoSolution);
81            }
82
83            Ok(NormalizationResult { normalized_term })
84        },
85    )
86}
87
88fn normalize_canonicalized_free_alias<'tcx>(
89    tcx: TyCtxt<'tcx>,
90    goal: CanonicalAliasGoal<'tcx>,
91) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> {
92    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/5a2be9f5f075d31e3ca5526b5b029881ce441253/compiler/rustc_traits/src/normalize_projection_ty.rs:92",
                        "rustc_traits::normalize_projection_ty",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/5a2be9f5f075d31e3ca5526b5b029881ce441253/compiler/rustc_traits/src/normalize_projection_ty.rs"),
                        ::tracing_core::__macro_support::Option::Some(92u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::normalize_projection_ty"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("normalize_canonicalized_free_alias(goal={0:#?})",
                                                    goal) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("normalize_canonicalized_free_alias(goal={:#?})", goal);
93
94    tcx.infer_ctxt().enter_canonical_trait_query(
95        &goal,
96        |ocx, ParamEnvAnd { param_env, value: goal }| {
97            let def_id = goal.expect_free_def_id();
98            let obligations =
99                tcx.clauses_of(def_id).instantiate_own(tcx, goal.args).map(|(clause, span)| {
100                    traits::Obligation::new(
101                        tcx,
102                        ObligationCause::dummy_with_span(span),
103                        param_env,
104                        clause.skip_norm_wip(),
105                    )
106                });
107            ocx.register_obligations(obligations);
108            let normalized_term: ty::Term<'tcx> = if goal.kind.is_type() {
109                tcx.type_of(def_id).instantiate(tcx, goal.args).skip_norm_wip().into()
110            } else {
111                traits::project::const_of_item_or_delayed_bug(tcx, def_id)
112                    .instantiate(tcx, goal.args)
113                    .skip_norm_wip()
114                    .into()
115            };
116            ocx.register_obligations(const_arg_has_type_obligation(
117                tcx,
118                param_env,
119                normalized_term,
120                goal,
121            ));
122            Ok(NormalizationResult { normalized_term })
123        },
124    )
125}
126
127fn normalize_canonicalized_inherent_projection<'tcx>(
128    tcx: TyCtxt<'tcx>,
129    goal: CanonicalAliasGoal<'tcx>,
130) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> {
131    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/5a2be9f5f075d31e3ca5526b5b029881ce441253/compiler/rustc_traits/src/normalize_projection_ty.rs:131",
                        "rustc_traits::normalize_projection_ty",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/5a2be9f5f075d31e3ca5526b5b029881ce441253/compiler/rustc_traits/src/normalize_projection_ty.rs"),
                        ::tracing_core::__macro_support::Option::Some(131u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_traits::normalize_projection_ty"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("normalize_canonicalized_inherent_projection(goal={0:#?})",
                                                    goal) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("normalize_canonicalized_inherent_projection(goal={:#?})", goal);
132
133    tcx.infer_ctxt().enter_canonical_trait_query(
134        &goal,
135        |ocx, ParamEnvAnd { param_env, value: goal }| {
136            let selcx = &mut SelectionContext::new(ocx.infcx);
137            let cause = ObligationCause::dummy();
138            let mut obligations = PredicateObligations::new();
139            let normalized_term = traits::normalize_inherent_projection(
140                selcx,
141                param_env,
142                goal.into(),
143                cause,
144                0,
145                &mut obligations,
146            );
147            ocx.register_obligations(obligations);
148
149            Ok(NormalizationResult { normalized_term })
150        },
151    )
152}