Skip to main content

rustc_next_trait_solver/solve/project_goals/
opaque_types.rs

1//! Computes a 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::project_goals::opaque_types",
                                    ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/project_goals/opaque_types.rs"),
                                    ::tracing_core::__macro_support::Option::Some(16u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::project_goals::opaque_types"),
                                    ::tracing_core::field::FieldSet::new(&["goal"],
                                        ::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))])
                            })
                } 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.projection_term;
            let expected =
                goal.predicate.term.as_type().expect("no such thing as an opaque const");
            let def_id = opaque_ty.expect_opaque_ty_def_id();
            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::Typeck {
                    defining_opaque_types_and_generators: defining_opaque_types
                    } | TypingMode::PostTypeckUntilBorrowck {
                    defining_opaque_types } => {
                    let Some(def_id) =
                        def_id.as_local().filter(|&def_id|
                                defining_opaque_types.contains(&def_id.into())) else {
                            self.relate_rigid_alias_non_alias(goal.param_env, opaque_ty,
                                    ty::Invariant, goal.predicate.term)?;
                            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::Typeck { .. } => {}
                            TypingMode::PostTypeckUntilBorrowck { .. } => {
                                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::PostBorrowck { .. } |
                                TypingMode::PostAnalysis | TypingMode::Codegen =>
                                ::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::PostBorrowck { defined_opaque_types } => {
                    let Some(def_id) =
                        def_id.as_local().filter(|&def_id|
                                defined_opaque_types.contains(&def_id.into())) else {
                            self.relate_rigid_alias_non_alias(goal.param_env, opaque_ty,
                                    ty::Invariant, goal.predicate.term)?;
                            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 | TypingMode::Codegen => {
                    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) => {
                    if let Some(def_id) = def_id.as_local() {
                        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.relate_rigid_alias_non_alias(goal.param_env, opaque_ty,
                            ty::Invariant, goal.predicate.term)?;
                    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::ProjectionPredicate<I>>,
20    ) -> QueryResultOrRerunNonErased<I> {
21        let cx = self.cx();
22        let opaque_ty = goal.predicate.projection_term;
23        let expected = goal.predicate.term.as_type().expect("no such thing as an opaque const");
24        let def_id = opaque_ty.expect_opaque_ty_def_id();
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::Typeck { defining_opaque_types_and_generators: defining_opaque_types }
45            | TypingMode::PostTypeckUntilBorrowck { defining_opaque_types } => {
46                let Some(def_id) = def_id
47                    .as_local()
48                    .filter(|&def_id| defining_opaque_types.contains(&def_id.into()))
49                else {
50                    // If we're not in the defining scope, treat the alias as rigid.
51                    self.relate_rigid_alias_non_alias(
52                        goal.param_env,
53                        opaque_ty,
54                        ty::Invariant,
55                        goal.predicate.term,
56                    )?;
57                    return self
58                        .evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
59                        .map_err(Into::into);
60                };
61
62                // We structurally normalize the args so that we're able to detect defining uses
63                // later on.
64                //
65                // This reduces the amount of duplicate definitions in the `opaque_type_storage` and
66                // strengthens inference. This causes us to subtly depend on the normalization behavior
67                // when inferring the hidden type of opaques.
68                //
69                // E.g. it's observable that we don't normalize nested aliases with bound vars in
70                // `structurally_normalize` and because we use structural lookup, we also don't
71                // reuse an entry for `Tait<for<'a> fn(&'a ())>` for `Tait<for<'b> fn(&'b ())>`.
72                let normalized_args =
73                    cx.mk_args_from_iter(opaque_ty.args.iter().map(|arg| match arg.kind() {
74                        ty::GenericArgKind::Lifetime(lt) => Ok(lt.into()),
75                        ty::GenericArgKind::Type(ty) => {
76                            self.structurally_normalize_ty(goal.param_env, ty).map(Into::into)
77                        }
78                        ty::GenericArgKind::Const(ct) => {
79                            self.structurally_normalize_const(goal.param_env, ct).map(Into::into)
80                        }
81                    }))?;
82
83                let opaque_type_key = ty::OpaqueTypeKey { def_id, args: normalized_args };
84                if let Some(prev) = self.register_hidden_type_in_storage(opaque_type_key, expected)
85                {
86                    self.eq(goal.param_env, expected, prev)?;
87                } else {
88                    // During HIR typeck, opaque types start out as unconstrained
89                    // inference variables. In borrowck we instead use the type
90                    // computed in HIR typeck as the initial value.
91                    match self.typing_mode().assert_not_erased() {
92                        TypingMode::Typeck { .. } => {}
93                        TypingMode::PostTypeckUntilBorrowck { .. } => {
94                            let actual = cx
95                                .type_of_opaque_hir_typeck(def_id)
96                                .instantiate(cx, opaque_ty.args)
97                                .skip_norm_wip();
98                            let actual = fold_regions(cx, actual, |re, _dbi| match re.kind() {
99                                ty::ReErased => self.next_region_var(),
100                                _ => re,
101                            });
102                            self.eq(goal.param_env, expected, actual)?;
103                        }
104                        TypingMode::Coherence
105                        | TypingMode::PostBorrowck { .. }
106                        | TypingMode::PostAnalysis
107                        | TypingMode::Codegen => unreachable!(),
108                    }
109                }
110
111                self.add_item_bounds_for_hidden_type(
112                    def_id.into(),
113                    normalized_args,
114                    goal.param_env,
115                    expected,
116                );
117                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
118                    .map_err(Into::into)
119            }
120            TypingMode::PostBorrowck { defined_opaque_types } => {
121                let Some(def_id) = def_id
122                    .as_local()
123                    .filter(|&def_id| defined_opaque_types.contains(&def_id.into()))
124                else {
125                    self.relate_rigid_alias_non_alias(
126                        goal.param_env,
127                        opaque_ty,
128                        ty::Invariant,
129                        goal.predicate.term,
130                    )?;
131                    return self
132                        .evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
133                        .map_err(Into::into);
134                };
135
136                let actual =
137                    cx.type_of(def_id.into()).instantiate(cx, opaque_ty.args).skip_norm_wip();
138                // FIXME: Actually use a proper binder here instead of relying on `ReErased`.
139                //
140                // This is also probably unsound or sth :shrug:
141                let actual = fold_regions(cx, actual, |re, _dbi| match re.kind() {
142                    ty::ReErased => self.next_region_var(),
143                    _ => re,
144                });
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::PostAnalysis | TypingMode::Codegen => {
150                // FIXME: Add an assertion that opaque type storage is empty.
151                let actual =
152                    cx.type_of(def_id.into()).instantiate(cx, opaque_ty.args).skip_norm_wip();
153                self.eq(goal.param_env, expected, actual)?;
154                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
155                    .map_err(Into::into)
156            }
157            TypingMode::ErasedNotCoherence(MayBeErased) => {
158                // If we have a local defid, in other typing modes we check whether
159                // this is the defining scope, and otherwise treat it as rigid.
160                // However, in `ErasedNotcoherence` we *always* treat it as rigid.
161                // This is the same as other modes if def_id is None, but wrong if we do have a DefId.
162                // So, if we have one, we register in the EvalCtxt that we may need that defid.
163                // We might then decide to rerun in the correct typing mode.
164                if let Some(def_id) = def_id.as_local() {
165                    self.opaque_accesses.rerun_if_opaque_in_opaque_type_storage(
166                        RerunReason::NormalizeOpaqueType,
167                        def_id,
168                    )?;
169                } else {
170                    self.opaque_accesses
171                        .rerun_if_in_post_analysis(RerunReason::NormalizeOpaqueTypeRemoteCrate)?;
172                }
173
174                // Always treat the opaque type as rigid.
175                self.relate_rigid_alias_non_alias(
176                    goal.param_env,
177                    opaque_ty,
178                    ty::Invariant,
179                    goal.predicate.term,
180                )?;
181                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
182                    .map_err(Into::into)
183            }
184        }
185    }
186}