1use std::fmt::Debug;
2use std::hash::Hash;
3use std::ops::Deref;
4
5use rustc_ast_ir::Movability;
6use rustc_index::bit_set::DenseBitSet;
7use smallvec::SmallVec;
8
9use crate::fold::TypeFoldable;
10use crate::inherent::*;
11use crate::ir_print::IrPrint;
12use crate::lang_items::TraitSolverLangItem;
13use crate::relate::Relate;
14use crate::solve::{CanonicalInput, ExternalConstraintsData, PredefinedOpaquesData, QueryResult};
15use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
16use crate::{self as ty, search_graph};
17
18pub trait Interner:
19 Sized
20 + Copy
21 + IrPrint<ty::AliasTy<Self>>
22 + IrPrint<ty::AliasTerm<Self>>
23 + IrPrint<ty::TraitRef<Self>>
24 + IrPrint<ty::TraitPredicate<Self>>
25 + IrPrint<ty::HostEffectPredicate<Self>>
26 + IrPrint<ty::ExistentialTraitRef<Self>>
27 + IrPrint<ty::ExistentialProjection<Self>>
28 + IrPrint<ty::ProjectionPredicate<Self>>
29 + IrPrint<ty::NormalizesTo<Self>>
30 + IrPrint<ty::SubtypePredicate<Self>>
31 + IrPrint<ty::CoercePredicate<Self>>
32 + IrPrint<ty::FnSig<Self>>
33{
34 type DefId: DefId<Self>;
35 type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
36 type Span: Span<Self>;
37
38 type GenericArgs: GenericArgs<Self>;
39 type GenericArgsSlice: Copy + Debug + Hash + Eq + SliceLike<Item = Self::GenericArg>;
40 type GenericArg: GenericArg<Self>;
41 type Term: Term<Self>;
42
43 type BoundVarKinds: Copy + Debug + Hash + Eq + SliceLike<Item = Self::BoundVarKind> + Default;
44 type BoundVarKind: Copy + Debug + Hash + Eq;
45
46 type PredefinedOpaques: Copy
47 + Debug
48 + Hash
49 + Eq
50 + TypeFoldable<Self>
51 + Deref<Target = PredefinedOpaquesData<Self>>;
52 fn mk_predefined_opaques_in_body(
53 self,
54 data: PredefinedOpaquesData<Self>,
55 ) -> Self::PredefinedOpaques;
56
57 type DefiningOpaqueTypes: Copy
58 + Debug
59 + Hash
60 + Default
61 + Eq
62 + TypeVisitable<Self>
63 + SliceLike<Item = Self::LocalDefId>;
64
65 type CanonicalVars: Copy
66 + Debug
67 + Hash
68 + Eq
69 + SliceLike<Item = ty::CanonicalVarInfo<Self>>
70 + Default;
71 fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars;
72
73 type ExternalConstraints: Copy
74 + Debug
75 + Hash
76 + Eq
77 + TypeFoldable<Self>
78 + Deref<Target = ExternalConstraintsData<Self>>;
79 fn mk_external_constraints(
80 self,
81 data: ExternalConstraintsData<Self>,
82 ) -> Self::ExternalConstraints;
83
84 type DepNodeIndex;
85 type Tracked<T: Debug + Clone>: Debug;
86 fn mk_tracked<T: Debug + Clone>(
87 self,
88 data: T,
89 dep_node: Self::DepNodeIndex,
90 ) -> Self::Tracked<T>;
91 fn get_tracked<T: Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T;
92 fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, Self::DepNodeIndex);
93
94 type Ty: Ty<Self>;
96 type Tys: Tys<Self>;
97 type FnInputTys: Copy + Debug + Hash + Eq + SliceLike<Item = Self::Ty> + TypeVisitable<Self>;
98 type ParamTy: Copy + Debug + Hash + Eq + ParamLike;
99 type BoundTy: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
100 type PlaceholderTy: PlaceholderLike;
101
102 type ErrorGuaranteed: Copy + Debug + Hash + Eq;
104 type BoundExistentialPredicates: BoundExistentialPredicates<Self>;
105 type AllocId: Copy + Debug + Hash + Eq;
106 type Pat: Copy + Debug + Hash + Eq + Debug + Relate<Self>;
107 type Safety: Safety<Self>;
108 type Abi: Abi<Self>;
109
110 type Const: Const<Self>;
112 type PlaceholderConst: PlaceholderLike;
113 type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
114 type BoundConst: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
115 type ValueConst: ValueConst<Self>;
116 type ExprConst: ExprConst<Self>;
117 type ValTree: Copy + Debug + Hash + Eq;
118
119 type Region: Region<Self>;
121 type EarlyParamRegion: Copy + Debug + Hash + Eq + ParamLike;
122 type LateParamRegion: Copy + Debug + Hash + Eq;
123 type BoundRegion: Copy + Debug + Hash + Eq + BoundVarLike<Self>;
124 type PlaceholderRegion: PlaceholderLike;
125
126 type ParamEnv: ParamEnv<Self>;
128 type Predicate: Predicate<Self>;
129 type Clause: Clause<Self>;
130 type Clauses: Copy + Debug + Hash + Eq + TypeSuperVisitable<Self> + Flags;
131
132 fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R;
133
134 fn evaluation_is_concurrent(&self) -> bool;
135
136 fn expand_abstract_consts<T: TypeFoldable<Self>>(self, t: T) -> T;
137
138 type GenericsOf: GenericsOf<Self>;
139 fn generics_of(self, def_id: Self::DefId) -> Self::GenericsOf;
140
141 type VariancesOf: Copy + Debug + SliceLike<Item = ty::Variance>;
142 fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf;
143
144 fn opt_alias_variances(
145 self,
146 kind: impl Into<ty::AliasTermKind>,
147 def_id: Self::DefId,
148 ) -> Option<Self::VariancesOf>;
149
150 fn type_of(self, def_id: Self::DefId) -> ty::EarlyBinder<Self, Self::Ty>;
151
152 type AdtDef: AdtDef<Self>;
153 fn adt_def(self, adt_def_id: Self::DefId) -> Self::AdtDef;
154
155 fn alias_ty_kind(self, alias: ty::AliasTy<Self>) -> ty::AliasTyKind;
156
157 fn alias_term_kind(self, alias: ty::AliasTerm<Self>) -> ty::AliasTermKind;
158
159 fn trait_ref_and_own_args_for_alias(
160 self,
161 def_id: Self::DefId,
162 args: Self::GenericArgs,
163 ) -> (ty::TraitRef<Self>, Self::GenericArgsSlice);
164
165 fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs;
166
167 fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
168 where
169 I: Iterator<Item = T>,
170 T: CollectAndApply<Self::GenericArg, Self::GenericArgs>;
171
172 fn check_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs) -> bool;
173
174 fn debug_assert_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs);
175
176 fn debug_assert_existential_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs);
179
180 fn mk_type_list_from_iter<I, T>(self, args: I) -> T::Output
181 where
182 I: Iterator<Item = T>,
183 T: CollectAndApply<Self::Ty, Self::Tys>;
184
185 fn parent(self, def_id: Self::DefId) -> Self::DefId;
186
187 fn recursion_limit(self) -> usize;
188
189 type Features: Features<Self>;
190 fn features(self) -> Self::Features;
191
192 fn bound_coroutine_hidden_types(
193 self,
194 def_id: Self::DefId,
195 ) -> impl IntoIterator<Item = ty::EarlyBinder<Self, ty::Binder<Self, Self::Ty>>>;
196
197 fn fn_sig(
198 self,
199 def_id: Self::DefId,
200 ) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::FnSig<Self>>>;
201
202 fn coroutine_movability(self, def_id: Self::DefId) -> Movability;
203
204 fn coroutine_for_closure(self, def_id: Self::DefId) -> Self::DefId;
205
206 fn generics_require_sized_self(self, def_id: Self::DefId) -> bool;
207
208 fn item_bounds(
209 self,
210 def_id: Self::DefId,
211 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
212
213 fn item_self_bounds(
214 self,
215 def_id: Self::DefId,
216 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
217
218 fn item_non_self_bounds(
219 self,
220 def_id: Self::DefId,
221 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
222
223 fn predicates_of(
224 self,
225 def_id: Self::DefId,
226 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
227
228 fn own_predicates_of(
229 self,
230 def_id: Self::DefId,
231 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
232
233 fn explicit_super_predicates_of(
234 self,
235 def_id: Self::DefId,
236 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
237
238 fn explicit_implied_predicates_of(
239 self,
240 def_id: Self::DefId,
241 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
242
243 fn impl_is_const(self, def_id: Self::DefId) -> bool;
244 fn fn_is_const(self, def_id: Self::DefId) -> bool;
245 fn alias_has_const_conditions(self, def_id: Self::DefId) -> bool;
246 fn const_conditions(
247 self,
248 def_id: Self::DefId,
249 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
250 fn explicit_implied_const_bounds(
251 self,
252 def_id: Self::DefId,
253 ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
254
255 fn has_target_features(self, def_id: Self::DefId) -> bool;
256
257 fn require_lang_item(self, lang_item: TraitSolverLangItem) -> Self::DefId;
258
259 fn is_lang_item(self, def_id: Self::DefId, lang_item: TraitSolverLangItem) -> bool;
260
261 fn as_lang_item(self, def_id: Self::DefId) -> Option<TraitSolverLangItem>;
262
263 fn associated_type_def_ids(self, def_id: Self::DefId) -> impl IntoIterator<Item = Self::DefId>;
264
265 fn for_each_relevant_impl(
266 self,
267 trait_def_id: Self::DefId,
268 self_ty: Self::Ty,
269 f: impl FnMut(Self::DefId),
270 );
271
272 fn has_item_definition(self, def_id: Self::DefId) -> bool;
273
274 fn impl_is_default(self, impl_def_id: Self::DefId) -> bool;
275
276 fn impl_trait_ref(self, impl_def_id: Self::DefId) -> ty::EarlyBinder<Self, ty::TraitRef<Self>>;
277
278 fn impl_polarity(self, impl_def_id: Self::DefId) -> ty::ImplPolarity;
279
280 fn trait_is_auto(self, trait_def_id: Self::DefId) -> bool;
281
282 fn trait_is_alias(self, trait_def_id: Self::DefId) -> bool;
283
284 fn trait_is_dyn_compatible(self, trait_def_id: Self::DefId) -> bool;
285
286 fn trait_is_fundamental(self, def_id: Self::DefId) -> bool;
287
288 fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool;
289
290 fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool;
292
293 fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool;
294
295 fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;
296
297 fn is_general_coroutine(self, coroutine_def_id: Self::DefId) -> bool;
298 fn coroutine_is_async(self, coroutine_def_id: Self::DefId) -> bool;
299 fn coroutine_is_gen(self, coroutine_def_id: Self::DefId) -> bool;
300 fn coroutine_is_async_gen(self, coroutine_def_id: Self::DefId) -> bool;
301
302 type UnsizingParams: Deref<Target = DenseBitSet<u32>>;
303 fn unsizing_params_for_adt(self, adt_def_id: Self::DefId) -> Self::UnsizingParams;
304
305 fn find_const_ty_from_env(
306 self,
307 param_env: Self::ParamEnv,
308 placeholder: Self::PlaceholderConst,
309 ) -> Self::Ty;
310
311 fn anonymize_bound_vars<T: TypeFoldable<Self>>(
312 self,
313 binder: ty::Binder<Self, T>,
314 ) -> ty::Binder<Self, T>;
315
316 fn opaque_types_defined_by(
317 self,
318 defining_anchor: Self::LocalDefId,
319 ) -> Self::DefiningOpaqueTypes;
320}
321
322pub trait CollectAndApply<T, R>: Sized {
331 type Output;
332
333 fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
338 where
339 I: Iterator<Item = Self>,
340 F: FnOnce(&[T]) -> R;
341}
342
343impl<T, R> CollectAndApply<T, R> for T {
345 type Output = R;
346
347 fn collect_and_apply<I, F>(mut iter: I, f: F) -> R
349 where
350 I: Iterator<Item = T>,
351 F: FnOnce(&[T]) -> R,
352 {
353 match iter.size_hint() {
359 (0, Some(0)) => {
360 assert!(iter.next().is_none());
361 f(&[])
362 }
363 (1, Some(1)) => {
364 let t0 = iter.next().unwrap();
365 assert!(iter.next().is_none());
366 f(&[t0])
367 }
368 (2, Some(2)) => {
369 let t0 = iter.next().unwrap();
370 let t1 = iter.next().unwrap();
371 assert!(iter.next().is_none());
372 f(&[t0, t1])
373 }
374 _ => f(&iter.collect::<SmallVec<[_; 8]>>()),
375 }
376 }
377}
378
379impl<T, R, E> CollectAndApply<T, R> for Result<T, E> {
382 type Output = Result<R, E>;
383
384 fn collect_and_apply<I, F>(mut iter: I, f: F) -> Result<R, E>
386 where
387 I: Iterator<Item = Result<T, E>>,
388 F: FnOnce(&[T]) -> R,
389 {
390 Ok(match iter.size_hint() {
397 (0, Some(0)) => {
398 assert!(iter.next().is_none());
399 f(&[])
400 }
401 (1, Some(1)) => {
402 let t0 = iter.next().unwrap()?;
403 assert!(iter.next().is_none());
404 f(&[t0])
405 }
406 (2, Some(2)) => {
407 let t0 = iter.next().unwrap()?;
408 let t1 = iter.next().unwrap()?;
409 assert!(iter.next().is_none());
410 f(&[t0, t1])
411 }
412 _ => f(&iter.collect::<Result<SmallVec<[_; 8]>, _>>()?),
413 })
414 }
415}
416
417impl<I: Interner> search_graph::Cx for I {
418 type Input = CanonicalInput<I>;
419 type Result = QueryResult<I>;
420
421 type DepNodeIndex = I::DepNodeIndex;
422 type Tracked<T: Debug + Clone> = I::Tracked<T>;
423 fn mk_tracked<T: Debug + Clone>(
424 self,
425 data: T,
426 dep_node_index: I::DepNodeIndex,
427 ) -> I::Tracked<T> {
428 I::mk_tracked(self, data, dep_node_index)
429 }
430 fn get_tracked<T: Debug + Clone>(self, tracked: &I::Tracked<T>) -> T {
431 I::get_tracked(self, tracked)
432 }
433 fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, I::DepNodeIndex) {
434 I::with_cached_task(self, task)
435 }
436 fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
437 I::with_global_cache(self, f)
438 }
439 fn evaluation_is_concurrent(&self) -> bool {
440 self.evaluation_is_concurrent()
441 }
442}