chalk_solve/
lib.rs

1#![deny(rust_2018_idioms)]
2
3use crate::display::sanitize_debug_name;
4use crate::rust_ir::*;
5use chalk_ir::interner::Interner;
6
7use chalk_ir::*;
8use std::fmt::Debug;
9use std::sync::Arc;
10
11pub mod clauses;
12pub mod coherence;
13pub mod coinductive_goal;
14pub mod display;
15pub mod ext;
16pub mod goal_builder;
17pub mod infer;
18pub mod logging;
19pub mod logging_db;
20pub mod rust_ir;
21pub mod solve;
22pub mod split;
23pub mod wf;
24
25/// Trait representing access to a database of rust types.
26///
27/// # `*_name` methods
28///
29/// This trait has a number of `*_name` methods with default implementations.
30/// These are used in the implementation for [`LoggingRustIrDatabase`], so that
31/// when printing `.chalk` files equivalent to the data used, we can use real
32/// names.
33///
34/// The default implementations simply fall back to calling [`Interner`] debug
35/// methods, and printing `"UnknownN"` (where `N` is the demultiplexing integer)
36/// if those methods return `None`.
37///
38/// The [`display::sanitize_debug_name`] utility is used in the default
39/// implementations, and might be useful when providing custom implementations.
40///
41/// [`LoggingRustIrDatabase`]: crate::logging_db::LoggingRustIrDatabase
42/// [`display::sanitize_debug_name`]: crate::display::sanitize_debug_name
43/// [`Interner`]: Interner
44pub trait RustIrDatabase<I: Interner>: Debug {
45    /// Returns any "custom program clauses" that do not derive from
46    /// Rust IR. Used only in testing the underlying solver.
47    fn custom_clauses(&self) -> Vec<ProgramClause<I>>;
48
49    /// Returns the datum for the associated type with the given id.
50    fn associated_ty_data(&self, ty: AssocTypeId<I>) -> Arc<AssociatedTyDatum<I>>;
51
52    /// Returns the datum for the definition with the given id.
53    fn trait_datum(&self, trait_id: TraitId<I>) -> Arc<TraitDatum<I>>;
54
55    /// Returns the datum for the ADT with the given id.
56    fn adt_datum(&self, adt_id: AdtId<I>) -> Arc<AdtDatum<I>>;
57
58    /// Returns the coroutine datum for the coroutine with the given id.
59    fn coroutine_datum(&self, coroutine_id: CoroutineId<I>) -> Arc<CoroutineDatum<I>>;
60
61    /// Returns the coroutine witness datum for the coroutine with the given id.
62    fn coroutine_witness_datum(
63        &self,
64        coroutine_id: CoroutineId<I>,
65    ) -> Arc<CoroutineWitnessDatum<I>>;
66
67    /// Returns the representation for the ADT definition with the given id.
68    fn adt_repr(&self, id: AdtId<I>) -> Arc<AdtRepr<I>>;
69
70    /// Returns the siza and alignment of the ADT definition with the given id.
71    fn adt_size_align(&self, id: AdtId<I>) -> Arc<AdtSizeAlign>;
72
73    /// Returns the datum for the fn definition with the given id.
74    fn fn_def_datum(&self, fn_def_id: FnDefId<I>) -> Arc<FnDefDatum<I>>;
75
76    /// Returns the datum for the impl with the given id.
77    fn impl_datum(&self, impl_id: ImplId<I>) -> Arc<ImplDatum<I>>;
78
79    fn associated_ty_from_impl(
80        &self,
81        impl_id: ImplId<I>,
82        assoc_type_id: AssocTypeId<I>,
83    ) -> Option<AssociatedTyValueId<I>>;
84
85    /// Returns the `AssociatedTyValue` with the given id.
86    fn associated_ty_value(&self, id: AssociatedTyValueId<I>) -> Arc<AssociatedTyValue<I>>;
87
88    /// Returns the `OpaqueTyDatum` with the given id.
89    fn opaque_ty_data(&self, id: OpaqueTyId<I>) -> Arc<OpaqueTyDatum<I>>;
90
91    /// Returns the "hidden type" corresponding with the opaque type.
92    fn hidden_opaque_type(&self, id: OpaqueTyId<I>) -> Ty<I>;
93
94    /// Returns a list of potentially relevant impls for a given
95    /// trait-id; we also supply the type parameters that we are
96    /// trying to match (if known: these parameters may contain
97    /// inference variables, for example). The implementor is
98    /// permitted to return any superset of the applicable impls;
99    /// chalk will narrow down the list to only those that truly
100    /// apply. The parameters are provided as a "hint" to help the
101    /// implementor do less work, but can be completely ignored if
102    /// desired.
103    ///
104    /// The `binders` are for the `parameters`; if the recursive solver is used,
105    /// the parameters can contain bound variables referring to these binders.
106    fn impls_for_trait(
107        &self,
108        trait_id: TraitId<I>,
109        parameters: &[GenericArg<I>],
110        binders: &CanonicalVarKinds<I>,
111    ) -> Vec<ImplId<I>>;
112
113    /// Returns the impls that require coherence checking. This is not the
114    /// full set of impls that exist:
115    ///
116    /// - It can exclude impls not defined in the current crate.
117    /// - It can exclude "built-in" impls, like those for closures; only the
118    ///   impls actually written by users need to be checked.
119    fn local_impls_to_coherence_check(&self, trait_id: TraitId<I>) -> Vec<ImplId<I>>;
120
121    /// Returns true if there is an explicit impl of the auto trait
122    /// `auto_trait_id` for the type `ty`. This is part of
123    /// the auto trait handling -- if there is no explicit impl given
124    /// by the user for `ty`, then we provide default impls
125    /// (otherwise, we rely on the impls the user gave).
126    fn impl_provided_for(&self, auto_trait_id: TraitId<I>, ty: &TyKind<I>) -> bool;
127
128    /// Returns id of a trait lang item, if found
129    fn well_known_trait_id(&self, well_known_trait: WellKnownTrait) -> Option<TraitId<I>>;
130
131    /// Returns id of a associated type, if found.
132    fn well_known_assoc_type_id(&self, assoc_type: WellKnownAssocType) -> Option<AssocTypeId<I>>;
133
134    /// Calculates program clauses from an env. This is intended to call the
135    /// `program_clauses_for_env` function and then possibly cache the clauses.
136    fn program_clauses_for_env(&self, environment: &Environment<I>) -> ProgramClauses<I>;
137
138    fn interner(&self) -> I;
139
140    /// Check if a trait is object safe
141    fn is_object_safe(&self, trait_id: TraitId<I>) -> bool;
142
143    /// Gets the `ClosureKind` for a given closure and substitution.
144    fn closure_kind(&self, closure_id: ClosureId<I>, substs: &Substitution<I>) -> ClosureKind;
145
146    /// Gets the inputs and output for a given closure id and substitution. We
147    /// pass both the `ClosureId` and it's `Substituion` to give implementors
148    /// the freedom to store associated data in the substitution (like rustc) or
149    /// separately (like chalk-integration).
150    fn closure_inputs_and_output(
151        &self,
152        closure_id: ClosureId<I>,
153        substs: &Substitution<I>,
154    ) -> Binders<FnDefInputsAndOutputDatum<I>>;
155
156    /// Gets the upvars as a `Ty` for a given closure id and substitution. There
157    /// are no restrictions on the type of upvars.
158    fn closure_upvars(&self, closure_id: ClosureId<I>, substs: &Substitution<I>) -> Binders<Ty<I>>;
159
160    /// Gets the substitution for the closure when used as a function.
161    /// For example, for the following (not-quite-)rust code:
162    /// ```ignore
163    /// let foo = |a: &mut u32| { a += 1; };
164    /// let c: &'a u32 = &0;
165    /// foo(c);
166    /// ```
167    ///
168    /// This would return a `Substitution` of `[&'a]`. This could either be
169    /// substituted into the inputs and output, or into the upvars.
170    fn closure_fn_substitution(
171        &self,
172        closure_id: ClosureId<I>,
173        substs: &Substitution<I>,
174    ) -> Substitution<I>;
175
176    fn unification_database(&self) -> &dyn UnificationDatabase<I>;
177
178    /// Retrieves a trait's original name. No uniqueness guarantees, but must
179    /// a valid Rust identifier.
180    fn trait_name(&self, trait_id: TraitId<I>) -> String {
181        sanitize_debug_name(|f| I::debug_trait_id(trait_id, f))
182    }
183
184    /// Retrieves a struct's original name. No uniqueness guarantees, but must
185    /// a valid Rust identifier.
186    fn adt_name(&self, adt_id: AdtId<I>) -> String {
187        sanitize_debug_name(|f| I::debug_adt_id(adt_id, f))
188    }
189
190    /// Retrieves the name of an associated type. No uniqueness guarantees, but must
191    /// a valid Rust identifier.
192    fn assoc_type_name(&self, assoc_ty_id: AssocTypeId<I>) -> String {
193        sanitize_debug_name(|f| I::debug_assoc_type_id(assoc_ty_id, f))
194    }
195
196    /// Retrieves the name of an opaque type. No uniqueness guarantees, but must
197    /// a valid Rust identifier.
198    fn opaque_type_name(&self, opaque_ty_id: OpaqueTyId<I>) -> String {
199        sanitize_debug_name(|f| I::debug_opaque_ty_id(opaque_ty_id, f))
200    }
201
202    /// Retrieves the name of a function definition. No uniqueness guarantees, but must
203    /// a valid Rust identifier.
204    fn fn_def_name(&self, fn_def_id: FnDefId<I>) -> String {
205        sanitize_debug_name(|f| I::debug_fn_def_id(fn_def_id, f))
206    }
207
208    // Retrieves the discriminant type for a type (mirror of rustc `Ty::discriminant_ty`)
209    fn discriminant_type(&self, ty: Ty<I>) -> Ty<I>;
210}
211
212pub use clauses::program_clauses_for_env;
213
214pub use solve::Guidance;
215pub use solve::Solution;
216pub use solve::Solver;
217pub use solve::SubstitutionResult;
218
219#[macro_use]
220mod debug_macros {
221    #[macro_export]
222    macro_rules! debug_span {
223        ($($t: tt)*) => {
224            let __span = tracing::debug_span!($($t)*);
225            let __span = __span.enter();
226        };
227    }
228}