Skip to main content

rustc_middle/traits/
solve.rs

1use rustc_data_structures::intern::Interned;
2use rustc_macros::StableHash;
3use rustc_type_ir as ir;
4pub use rustc_type_ir::solve::*;
5
6use crate::ty::{
7    self, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor,
8    try_visit,
9};
10
11pub type Goal<'tcx, P> = ir::solve::Goal<TyCtxt<'tcx>, P>;
12pub type QueryInput<'tcx, P> = ir::solve::QueryInput<TyCtxt<'tcx>, P>;
13pub type QueryResult<'tcx> = ir::solve::QueryResult<TyCtxt<'tcx>>;
14pub type CandidateSource<'tcx> = ir::solve::CandidateSource<TyCtxt<'tcx>>;
15pub type CanonicalInput<'tcx, P = ty::Predicate<'tcx>> = ir::solve::CanonicalInput<TyCtxt<'tcx>, P>;
16pub type CanonicalResponse<'tcx> = ir::solve::CanonicalResponse<TyCtxt<'tcx>>;
17pub type FetchEligibleAssocItemResponse<'tcx> =
18    ir::solve::FetchEligibleAssocItemResponse<TyCtxt<'tcx>>;
19
20pub type PredefinedOpaques<'tcx> = &'tcx ty::List<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>;
21
22#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ExternalConstraints<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "ExternalConstraints", &&self.0)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for ExternalConstraints<'tcx> {
    #[inline]
    fn eq(&self, other: &ExternalConstraints<'tcx>) -> bool {
        self.0 == other.0
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for ExternalConstraints<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _:
                ::core::cmp::AssertParamIsEq<Interned<'tcx,
                ExternalConstraintsData<TyCtxt<'tcx>>>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ExternalConstraints<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for ExternalConstraints<'tcx> {
    #[inline]
    fn clone(&self) -> ExternalConstraints<'tcx> {
        let _:
                ::core::clone::AssertParamIsClone<Interned<'tcx,
                ExternalConstraintsData<TyCtxt<'tcx>>>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for ExternalConstraints<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash, const _: () =
    {
        impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
            ExternalConstraints<'tcx> {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    ExternalConstraints(ref __binding_0) => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
23pub struct ExternalConstraints<'tcx>(
24    pub(crate) Interned<'tcx, ExternalConstraintsData<TyCtxt<'tcx>>>,
25);
26
27impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
28    type Target = ExternalConstraintsData<TyCtxt<'tcx>>;
29
30    fn deref(&self) -> &Self::Target {
31        &self.0
32    }
33}
34
35// FIXME: Having to clone `region_constraints` for folding feels bad and
36// probably isn't great wrt performance.
37//
38// Not sure how to fix this, maybe we should also intern `opaque_types` and
39// `region_constraints` here or something.
40impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
41    fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
42        self,
43        folder: &mut F,
44    ) -> Result<Self, F::Error> {
45        // Perf testing has found that this check is slightly faster than
46        // folding and re-interning an empty `ExternalConstraintsData`.
47        // See: <https://github.com/rust-lang/rust/pull/142430>.
48        if self.is_empty() {
49            return Ok(self);
50        }
51
52        Ok(FallibleTypeFolder::cx(folder).mk_external_constraints(ExternalConstraintsData {
53            region_constraints: self.region_constraints.clone().try_fold_with(folder)?,
54            opaque_types: self
55                .opaque_types
56                .iter()
57                .map(|opaque| opaque.try_fold_with(folder))
58                .collect::<Result<_, F::Error>>()?,
59            normalization_nested_goals: self
60                .normalization_nested_goals
61                .clone()
62                .try_fold_with(folder)?,
63        }))
64    }
65
66    fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
67        // Perf testing has found that this check is slightly faster than
68        // folding and re-interning an empty `ExternalConstraintsData`.
69        // See: <https://github.com/rust-lang/rust/pull/142430>.
70        if self.is_empty() {
71            return self;
72        }
73
74        TypeFolder::cx(folder).mk_external_constraints(ExternalConstraintsData {
75            region_constraints: self.region_constraints.clone().fold_with(folder),
76            opaque_types: self.opaque_types.iter().map(|opaque| opaque.fold_with(folder)).collect(),
77            normalization_nested_goals: self.normalization_nested_goals.clone().fold_with(folder),
78        })
79    }
80}
81
82impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
83    fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
84        let ExternalConstraintsData {
85            region_constraints,
86            opaque_types,
87            normalization_nested_goals,
88        } = &**self;
89
90        match ::rustc_ast_ir::visit::VisitorResult::branch(region_constraints.visit_with(visitor))
    {
    core::ops::ControlFlow::Continue(()) =>
        (),
        #[allow(unreachable_code)]
        core::ops::ControlFlow::Break(r) => {
        return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
    }
};try_visit!(region_constraints.visit_with(visitor));
91        match ::rustc_ast_ir::visit::VisitorResult::branch(opaque_types.visit_with(visitor))
    {
    core::ops::ControlFlow::Continue(()) =>
        (),
        #[allow(unreachable_code)]
        core::ops::ControlFlow::Break(r) => {
        return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
    }
};try_visit!(opaque_types.visit_with(visitor));
92        normalization_nested_goals.visit_with(visitor)
93    }
94}