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