Skip to main content

rustc_next_trait_solver/
delegate.rs

1use std::fmt::Debug;
2use std::ops::Deref;
3
4use rustc_type_ir::solve::{
5    Certainty, ComputeGoalFastPathOutcome, FetchEligibleAssocItemResponse, Goal, NoSolution,
6    VisibleForLeakCheck,
7};
8use rustc_type_ir::{self as ty, CanonicalizerState, InferCtxtLike, Interner, TypeFoldable};
9
10pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
11    type Infcx: InferCtxtLike<Interner = Self::Interner>;
12    type Interner: Interner;
13    fn cx(&self) -> Self::Interner {
14        (**self).cx()
15    }
16
17    fn build_with_canonical<V>(
18        cx: Self::Interner,
19        canonical: &ty::CanonicalQueryInput<Self::Interner, V>,
20    ) -> (Self, V, ty::CanonicalVarValues<Self::Interner>)
21    where
22        V: TypeFoldable<Self::Interner>;
23
24    fn compute_goal_fast_path(
25        &self,
26        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
27        span: <Self::Interner as Interner>::Span,
28    ) -> ComputeGoalFastPathOutcome<Self::Interner>;
29
30    fn fresh_var_for_kind(
31        &self,
32        arg: <Self::Interner as Interner>::GenericArg,
33        span: <Self::Interner as Interner>::Span,
34        universe: ty::UniverseIndex,
35    ) -> <Self::Interner as Interner>::GenericArg;
36
37    // FIXME: Uplift the leak check into this crate.
38    fn leak_check(&self, max_input_universe: ty::UniverseIndex) -> Result<(), NoSolution>;
39
40    /// Evaluate a const, normalizing the type of the resulting value with `normalize_ty`.
41    /// Returns `Ok(None)` if the const is too generic, and `Err(_)` only if `normalize_ty`
42    /// failed.
43    fn evaluate_const<E: Debug>(
44        &self,
45        param_env: <Self::Interner as Interner>::ParamEnv,
46        alias_const: ty::AliasConst<Self::Interner>,
47        normalize_ty: impl FnOnce(
48            ty::Unnormalized<Self::Interner, <Self::Interner as Interner>::Ty>,
49        ) -> Result<<Self::Interner as Interner>::Ty, E>,
50    ) -> Result<Option<<Self::Interner as Interner>::Const>, E>;
51
52    // FIXME: This only is here because `wf::obligations` is in `rustc_trait_selection`!
53    fn well_formed_goals(
54        &self,
55        param_env: <Self::Interner as Interner>::ParamEnv,
56        term: <Self::Interner as Interner>::Term,
57    ) -> Option<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>>;
58
59    fn make_deduplicated_region_constraints(
60        &self,
61    ) -> Vec<(ty::RegionConstraint<Self::Interner>, VisibleForLeakCheck)>;
62
63    fn instantiate_canonical<V>(
64        &self,
65        canonical: ty::Canonical<Self::Interner, V>,
66        values: ty::CanonicalVarValues<Self::Interner>,
67    ) -> V
68    where
69        V: TypeFoldable<Self::Interner>;
70
71    fn instantiate_canonical_var(
72        &self,
73        kind: ty::CanonicalVarKind<Self::Interner>,
74        span: <Self::Interner as Interner>::Span,
75        var_values: &[<Self::Interner as Interner>::GenericArg],
76        universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
77    ) -> <Self::Interner as Interner>::GenericArg;
78
79    fn add_item_bounds_for_hidden_type(
80        &self,
81        def_id: <Self::Interner as Interner>::OpaqueTyId,
82        args: <Self::Interner as Interner>::GenericArgs,
83        param_env: <Self::Interner as Interner>::ParamEnv,
84        hidden_ty: <Self::Interner as Interner>::Ty,
85        goals: &mut Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>,
86    );
87
88    fn fetch_eligible_assoc_item(
89        &self,
90        goal_trait_ref: ty::TraitRef<Self::Interner>,
91        trait_assoc_def_id: <Self::Interner as Interner>::TraitAssocTermId,
92        impl_def_id: <Self::Interner as Interner>::ImplId,
93    ) -> FetchEligibleAssocItemResponse<Self::Interner>;
94
95    fn is_transmutable(
96        &self,
97        src: <Self::Interner as Interner>::Ty,
98        dst: <Self::Interner as Interner>::Ty,
99        assume: <Self::Interner as Interner>::Const,
100    ) -> Result<Certainty, NoSolution>;
101
102    /// Obtain canonicalizer state, either by allocating it afresh (the default) or by reusing
103    /// previously allocated state.
104    fn obtain_canonicalizer_state(&self) -> CanonicalizerState<Self::Interner> {
105        Default::default()
106    }
107
108    /// Release canonicalizer state, either by deallocating it (the default) or by clearing it and
109    /// stashing it for later reuse.
110    fn release_canonicalizer_state(&self, _: CanonicalizerState<Self::Interner>) {}
111}