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