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 CanonicalResponse<'tcx> = ir::solve::CanonicalResponse<TyCtxt<'tcx>>;
16pub type FetchEligibleAssocItemResponse<'tcx> =
17    ir::solve::FetchEligibleAssocItemResponse<TyCtxt<'tcx>>;
18pub type ComputeGoalFastPathOutcome<'tcx> = ir::solve::ComputeGoalFastPathOutcome<TyCtxt<'tcx>>;
19pub type GoalStalledOn<'tcx> = ir::solve::GoalStalledOn<TyCtxt<'tcx>>;
20pub type GoalStalledOnOpaques<'tcx> = ir::solve::GoalStalledOnOpaques<TyCtxt<'tcx>>;
21pub type SucceededInErased<'tcx> = ir::solve::SucceededInErased<TyCtxt<'tcx>>;
22
23pub type PredefinedOpaques<'tcx> = &'tcx ty::List<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)>;
24
25// Interning CanonicalInput drastically reduces max memory usage when compiling a crate that has
26// trait solver recursion depth overflows with next-solver deduplicating individual inputs.
27// This mostly fixes #161748 where it reduced the memory usage for compiling bevy_render from
28// ~14GiB to ~4GiB
29// Main improved types:
30//   - rustc_type_ir::search_graph::GlobalCache
31//   - rustc_type_ir::search_graph::NestedGoals
32#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for CanonicalInput<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "CanonicalInput",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::marker::StructuralPartialEq for CanonicalInput<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for CanonicalInput<'tcx> {
    #[inline]
    fn eq(&self, other: &CanonicalInput<'tcx>) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for CanonicalInput<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _:
                ::core::cmp::AssertParamIsEq<Interned<'tcx,
                CanonicalInputData<TyCtxt<'tcx>>>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for CanonicalInput<'tcx> { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl<'tcx> ::core::clone::TrivialClone for CanonicalInput<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::clone::Clone for CanonicalInput<'tcx> {
    #[inline]
    fn clone(&self) -> CanonicalInput<'tcx> {
        let _:
                ::core::clone::AssertParamIsClone<Interned<'tcx,
                CanonicalInputData<TyCtxt<'tcx>>>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for CanonicalInput<'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
            CanonicalInput<'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 {
                    CanonicalInput(ref __binding_0) => {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash)]
33pub struct CanonicalInput<'tcx>(pub(crate) Interned<'tcx, CanonicalInputData<TyCtxt<'tcx>>>);
34
35impl<'tcx> std::ops::Deref for CanonicalInput<'tcx> {
36    type Target = CanonicalInputData<TyCtxt<'tcx>>;
37
38    fn deref(&self) -> &Self::Target {
39        &self.0
40    }
41}
42
43#[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::marker::StructuralPartialEq for ExternalConstraints<'tcx> {
}
#[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]
#[doc(hidden)]
unsafe impl<'tcx> ::core::clone::TrivialClone for ExternalConstraints<'tcx> {
}
#[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)]
44pub struct ExternalConstraints<'tcx>(
45    pub(crate) Interned<'tcx, ExternalConstraintsData<TyCtxt<'tcx>>>,
46);
47
48impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> {
49    type Target = ExternalConstraintsData<TyCtxt<'tcx>>;
50
51    fn deref(&self) -> &Self::Target {
52        &self.0
53    }
54}
55
56// FIXME: Having to clone `region_constraints` for folding feels bad and
57// probably isn't great wrt performance.
58//
59// Not sure how to fix this, maybe we should also intern `opaque_types` and
60// `region_constraints` here or something.
61impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
62    fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
63        self,
64        folder: &mut F,
65    ) -> Result<Self, F::Error> {
66        // Perf testing has found that this check is slightly faster than
67        // folding and re-interning an empty `ExternalConstraintsData`.
68        // See: <https://github.com/rust-lang/rust/pull/142430>.
69        if self.is_empty() {
70            return Ok(self);
71        }
72
73        Ok(FallibleTypeFolder::cx(folder).mk_external_constraints(ExternalConstraintsData {
74            region_constraints: self.region_constraints.clone().try_fold_with(folder)?,
75            opaque_types: self
76                .opaque_types
77                .iter()
78                .map(|opaque| opaque.try_fold_with(folder))
79                .collect::<Result<_, F::Error>>()?,
80            normalization_nested_goals: self
81                .normalization_nested_goals
82                .clone()
83                .try_fold_with(folder)?,
84        }))
85    }
86
87    fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
88        // Perf testing has found that this check is slightly faster than
89        // folding and re-interning an empty `ExternalConstraintsData`.
90        // See: <https://github.com/rust-lang/rust/pull/142430>.
91        if self.is_empty() {
92            return self;
93        }
94
95        TypeFolder::cx(folder).mk_external_constraints(ExternalConstraintsData {
96            region_constraints: self.region_constraints.clone().fold_with(folder),
97            opaque_types: self.opaque_types.iter().map(|opaque| opaque.fold_with(folder)).collect(),
98            normalization_nested_goals: self.normalization_nested_goals.clone().fold_with(folder),
99        })
100    }
101}
102
103impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for ExternalConstraints<'tcx> {
104    fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
105        let ExternalConstraintsData {
106            region_constraints,
107            opaque_types,
108            normalization_nested_goals,
109        } = &**self;
110
111        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));
112        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));
113        normalization_nested_goals.visit_with(visitor)
114    }
115}
116
117// Some types are used a lot. Make sure they don't unintentionally get bigger.
118#[cfg(target_pointer_width = "64")]
119mod size_asserts {
120    use rustc_data_structures::static_assert_size;
121
122    use super::*;
123    // tidy-alphabetical-start
124    const _: [(); 56] = [(); ::std::mem::size_of::<GoalStalledOn<'_>>()];static_assert_size!(GoalStalledOn<'_>, 56);
125    // tidy-alphabetical-end
126}