Skip to main content

rustc_next_trait_solver/solve/normalizes_to/
opaque_types.rs

1//! Computes a normalizes-to (projection) goal for opaque types. This goal
2//! behaves differently depending on the current `TypingMode`.
3
4use rustc_type_ir::inherent::*;
5use rustc_type_ir::solve::{GoalSource, QueryResultOrRerunNonErased, RerunReason};
6use rustc_type_ir::{self as ty, Interner, MayBeErased, TypingMode, fold_regions};
7
8use crate::delegate::SolverDelegate;
9use crate::solve::{Certainty, EvalCtxt, Goal};
10
11impl<D, I> EvalCtxt<'_, D>
12where
13    D: SolverDelegate<Interner = I>,
14    I: Interner,
15{
16    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("normalize_opaque_type",
                                    "rustc_next_trait_solver::solve::normalizes_to::opaque_types",
                                    ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs"),
                                    ::tracing_core::__macro_support::Option::Some(16u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::normalizes_to::opaque_types"),
                                    ::tracing_core::field::FieldSet::new(&["goal", "def_id"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::INFO <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::INFO <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: QueryResultOrRerunNonErased<I> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let cx = self.cx();
            let opaque_ty = goal.predicate.alias;
            let expected =
                goal.predicate.term.as_type().expect("no such thing as an opaque const");
            match self.typing_mode() {
                TypingMode::Coherence => {
                    self.add_item_bounds_for_hidden_type(def_id, opaque_ty.args,
                        goal.param_env, expected);
                    self.add_goal(GoalSource::Misc,
                        goal.with(cx, ty::PredicateKind::Ambiguous));
                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into)
                }
                TypingMode::Analysis {
                    defining_opaque_types_and_generators: defining_opaque_types
                    } | TypingMode::Borrowck { defining_opaque_types } => {
                    let Some(def_id) =
                        def_id.as_local().filter(|&def_id|
                                defining_opaque_types.contains(&def_id.into())) else {
                            self.structurally_instantiate_normalizes_to_term(goal,
                                goal.predicate.alias);
                            return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into);
                        };
                    let normalized_args =
                        cx.mk_args_from_iter(opaque_ty.args.iter().map(|arg|
                                        match arg.kind() {
                                            ty::GenericArgKind::Lifetime(lt) => Ok(lt.into()),
                                            ty::GenericArgKind::Type(ty) => {
                                                self.structurally_normalize_ty(goal.param_env,
                                                        ty).map(Into::into)
                                            }
                                            ty::GenericArgKind::Const(ct) => {
                                                self.structurally_normalize_const(goal.param_env,
                                                        ct).map(Into::into)
                                            }
                                        }))?;
                    let opaque_type_key =
                        ty::OpaqueTypeKey { def_id, args: normalized_args };
                    if let Some(prev) =
                            self.register_hidden_type_in_storage(opaque_type_key,
                                expected) {
                        self.eq(goal.param_env, expected, prev)?;
                    } else {
                        match self.typing_mode().assert_not_erased() {
                            TypingMode::Analysis { .. } => {}
                            TypingMode::Borrowck { .. } => {
                                let actual =
                                    cx.type_of_opaque_hir_typeck(def_id).instantiate(cx,
                                            opaque_ty.args).skip_norm_wip();
                                let actual =
                                    fold_regions(cx, actual,
                                        |re, _dbi|
                                            match re.kind() {
                                                ty::ReErased => self.next_region_var(),
                                                _ => re,
                                            });
                                self.eq(goal.param_env, expected, actual)?;
                            }
                            TypingMode::Coherence | TypingMode::PostBorrowckAnalysis {
                                .. } | TypingMode::PostAnalysis =>
                                ::core::panicking::panic("internal error: entered unreachable code"),
                        }
                    }
                    self.add_item_bounds_for_hidden_type(def_id.into(),
                        normalized_args, goal.param_env, expected);
                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into)
                }
                TypingMode::PostBorrowckAnalysis { defined_opaque_types } => {
                    let Some(def_id) =
                        opaque_ty.def_id().as_local().filter(|&def_id|
                                defined_opaque_types.contains(&def_id)) else {
                            self.structurally_instantiate_normalizes_to_term(goal,
                                goal.predicate.alias);
                            return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into);
                        };
                    let actual =
                        cx.type_of(def_id.into()).instantiate(cx,
                                opaque_ty.args).skip_norm_wip();
                    let actual =
                        fold_regions(cx, actual,
                            |re, _dbi|
                                match re.kind() {
                                    ty::ReErased => self.next_region_var(),
                                    _ => re,
                                });
                    self.eq(goal.param_env, expected, actual)?;
                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into)
                }
                TypingMode::PostAnalysis => {
                    let actual =
                        cx.type_of(def_id.into()).instantiate(cx,
                                opaque_ty.args).skip_norm_wip();
                    self.eq(goal.param_env, expected, actual)?;
                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into)
                }
                TypingMode::ErasedNotCoherence(MayBeErased) => {
                    let def_id = opaque_ty.def_id().as_local();
                    if let Some(def_id) = def_id {
                        self.opaque_accesses.rerun_if_opaque_in_opaque_type_storage(RerunReason::NormalizeOpaqueType,
                                def_id)?;
                    } else {
                        self.opaque_accesses.rerun_if_in_post_analysis(RerunReason::NormalizeOpaqueTypeRemoteCrate)?;
                    }
                    self.structurally_instantiate_normalizes_to_term(goal,
                        goal.predicate.alias);
                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into)
                }
            }
        }
    }
}#[tracing::instrument(skip(self))]
17    pub(super) fn normalize_opaque_type(
18        &mut self,
19        goal: Goal<I, ty::NormalizesTo<I>>,
20        def_id: I::OpaqueTyId,
21    ) -> QueryResultOrRerunNonErased<I> {
22        let cx = self.cx();
23        let opaque_ty = goal.predicate.alias;
24        let expected = goal.predicate.term.as_type().expect("no such thing as an opaque const");
25
26        match self.typing_mode() {
27            TypingMode::Coherence => {
28                // An impossible opaque type bound is the only way this goal will fail
29                // e.g. assigning `impl Copy := NotCopy`
30                self.add_item_bounds_for_hidden_type(
31                    def_id,
32                    opaque_ty.args,
33                    goal.param_env,
34                    expected,
35                );
36                // Trying to normalize an opaque type during coherence is always ambiguous.
37                // We add a nested ambiguous goal here instead of using `Certainty::AMBIGUOUS`.
38                // This allows us to return the nested goals to the parent `AliasRelate` goal.
39                // This can then allow nested goals to fail after we've constrained the `term`.
40                self.add_goal(GoalSource::Misc, goal.with(cx, ty::PredicateKind::Ambiguous));
41                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
42                    .map_err(Into::into)
43            }
44            TypingMode::Analysis {
45                defining_opaque_types_and_generators: defining_opaque_types,
46            }
47            | TypingMode::Borrowck { defining_opaque_types } => {
48                let Some(def_id) = def_id
49                    .as_local()
50                    .filter(|&def_id| defining_opaque_types.contains(&def_id.into()))
51                else {
52                    // If we're not in the defining scope, treat the alias as rigid.
53                    self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
54                    return self
55                        .evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
56                        .map_err(Into::into);
57                };
58
59                // We structurally normalize the args so that we're able to detect defining uses
60                // later on.
61                //
62                // This reduces the amount of duplicate definitions in the `opaque_type_storage` and
63                // strengthens inference. This causes us to subtly depend on the normalization behavior
64                // when inferring the hidden type of opaques.
65                //
66                // E.g. it's observable that we don't normalize nested aliases with bound vars in
67                // `structurally_normalize` and because we use structural lookup, we also don't
68                // reuse an entry for `Tait<for<'a> fn(&'a ())>` for `Tait<for<'b> fn(&'b ())>`.
69                let normalized_args =
70                    cx.mk_args_from_iter(opaque_ty.args.iter().map(|arg| match arg.kind() {
71                        ty::GenericArgKind::Lifetime(lt) => Ok(lt.into()),
72                        ty::GenericArgKind::Type(ty) => {
73                            self.structurally_normalize_ty(goal.param_env, ty).map(Into::into)
74                        }
75                        ty::GenericArgKind::Const(ct) => {
76                            self.structurally_normalize_const(goal.param_env, ct).map(Into::into)
77                        }
78                    }))?;
79
80                let opaque_type_key = ty::OpaqueTypeKey { def_id, args: normalized_args };
81                if let Some(prev) = self.register_hidden_type_in_storage(opaque_type_key, expected)
82                {
83                    self.eq(goal.param_env, expected, prev)?;
84                } else {
85                    // During HIR typeck, opaque types start out as unconstrained
86                    // inference variables. In borrowck we instead use the type
87                    // computed in HIR typeck as the initial value.
88                    match self.typing_mode().assert_not_erased() {
89                        TypingMode::Analysis { .. } => {}
90                        TypingMode::Borrowck { .. } => {
91                            let actual = cx
92                                .type_of_opaque_hir_typeck(def_id)
93                                .instantiate(cx, opaque_ty.args)
94                                .skip_norm_wip();
95                            let actual = fold_regions(cx, actual, |re, _dbi| match re.kind() {
96                                ty::ReErased => self.next_region_var(),
97                                _ => re,
98                            });
99                            self.eq(goal.param_env, expected, actual)?;
100                        }
101                        TypingMode::Coherence
102                        | TypingMode::PostBorrowckAnalysis { .. }
103                        | TypingMode::PostAnalysis => unreachable!(),
104                    }
105                }
106
107                self.add_item_bounds_for_hidden_type(
108                    def_id.into(),
109                    normalized_args,
110                    goal.param_env,
111                    expected,
112                );
113                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
114                    .map_err(Into::into)
115            }
116            TypingMode::PostBorrowckAnalysis { defined_opaque_types } => {
117                let Some(def_id) = opaque_ty
118                    .def_id()
119                    .as_local()
120                    .filter(|&def_id| defined_opaque_types.contains(&def_id))
121                else {
122                    self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
123                    return self
124                        .evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
125                        .map_err(Into::into);
126                };
127
128                let actual =
129                    cx.type_of(def_id.into()).instantiate(cx, opaque_ty.args).skip_norm_wip();
130                // FIXME: Actually use a proper binder here instead of relying on `ReErased`.
131                //
132                // This is also probably unsound or sth :shrug:
133                let actual = fold_regions(cx, actual, |re, _dbi| match re.kind() {
134                    ty::ReErased => self.next_region_var(),
135                    _ => re,
136                });
137                self.eq(goal.param_env, expected, actual)?;
138                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
139                    .map_err(Into::into)
140            }
141            TypingMode::PostAnalysis => {
142                // FIXME: Add an assertion that opaque type storage is empty.
143                let actual =
144                    cx.type_of(def_id.into()).instantiate(cx, opaque_ty.args).skip_norm_wip();
145                self.eq(goal.param_env, expected, actual)?;
146                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
147                    .map_err(Into::into)
148            }
149            TypingMode::ErasedNotCoherence(MayBeErased) => {
150                let def_id = opaque_ty.def_id().as_local();
151
152                // If we have a local defid, in other typing modes we check whether
153                // this is the defining scope, and otherwise treat it as rigid.
154                // However, in `ErasedNotcoherence` we *always* treat it as rigid.
155                // This is the same as other modes if def_id is None, but wrong if we do have a DefId.
156                // So, if we have one, we register in the EvalCtxt that we may need that defid.
157                // We might then decide to rerun in the correct typing mode.
158                if let Some(def_id) = def_id {
159                    self.opaque_accesses.rerun_if_opaque_in_opaque_type_storage(
160                        RerunReason::NormalizeOpaqueType,
161                        def_id,
162                    )?;
163                } else {
164                    self.opaque_accesses
165                        .rerun_if_in_post_analysis(RerunReason::NormalizeOpaqueTypeRemoteCrate)?;
166                }
167
168                // Always treat the opaque type as rigid.
169                self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
170                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
171                    .map_err(Into::into)
172            }
173        }
174    }
175}