rustc_type_ir/
interner.rs

1use std::fmt::Debug;
2use std::hash::Hash;
3use std::ops::Deref;
4
5use rustc_ast_ir::Movability;
6use rustc_index::bit_set::DenseBitSet;
7
8use crate::fold::TypeFoldable;
9use crate::inherent::*;
10use crate::ir_print::IrPrint;
11use crate::lang_items::TraitSolverLangItem;
12use crate::relate::Relate;
13use crate::solve::{CanonicalInput, ExternalConstraintsData, PredefinedOpaquesData, QueryResult};
14use crate::visit::{Flags, TypeVisitable};
15use crate::{self as ty, CanonicalParamEnvCacheEntry, search_graph};
16
17#[cfg_attr(feature = "nightly", rustc_diagnostic_item = "type_ir_interner")]
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    + IrPrint<ty::PatternKind<Self>>
34{
35    fn next_trait_solver_globally(self) -> bool {
36        true
37    }
38
39    type DefId: DefId<Self>;
40    type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
41    type Span: Span<Self>;
42
43    type GenericArgs: GenericArgs<Self>;
44    type GenericArgsSlice: Copy + Debug + Hash + Eq + SliceLike<Item = Self::GenericArg>;
45    type GenericArg: GenericArg<Self>;
46    type Term: Term<Self>;
47
48    type BoundVarKinds: Copy + Debug + Hash + Eq + SliceLike<Item = Self::BoundVarKind> + Default;
49    type BoundVarKind: Copy + Debug + Hash + Eq;
50
51    type PredefinedOpaques: Copy
52        + Debug
53        + Hash
54        + Eq
55        + TypeFoldable<Self>
56        + Deref<Target = PredefinedOpaquesData<Self>>;
57    fn mk_predefined_opaques_in_body(
58        self,
59        data: PredefinedOpaquesData<Self>,
60    ) -> Self::PredefinedOpaques;
61
62    type LocalDefIds: Copy
63        + Debug
64        + Hash
65        + Default
66        + Eq
67        + TypeVisitable<Self>
68        + SliceLike<Item = Self::LocalDefId>;
69
70    type CanonicalVarKinds: Copy
71        + Debug
72        + Hash
73        + Eq
74        + SliceLike<Item = ty::CanonicalVarKind<Self>>
75        + Default;
76    fn mk_canonical_var_kinds(
77        self,
78        kinds: &[ty::CanonicalVarKind<Self>],
79    ) -> Self::CanonicalVarKinds;
80
81    type ExternalConstraints: Copy
82        + Debug
83        + Hash
84        + Eq
85        + TypeFoldable<Self>
86        + Deref<Target = ExternalConstraintsData<Self>>;
87    fn mk_external_constraints(
88        self,
89        data: ExternalConstraintsData<Self>,
90    ) -> Self::ExternalConstraints;
91
92    type DepNodeIndex;
93    type Tracked<T: Debug + Clone>: Debug;
94    fn mk_tracked<T: Debug + Clone>(
95        self,
96        data: T,
97        dep_node: Self::DepNodeIndex,
98    ) -> Self::Tracked<T>;
99    fn get_tracked<T: Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T;
100    fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, Self::DepNodeIndex);
101
102    // Kinds of tys
103    type Ty: Ty<Self>;
104    type Tys: Tys<Self>;
105    type FnInputTys: Copy + Debug + Hash + Eq + SliceLike<Item = Self::Ty> + TypeVisitable<Self>;
106    type ParamTy: ParamLike;
107    type BoundTy: BoundVarLike<Self>;
108    type PlaceholderTy: PlaceholderLike<Self, Bound = Self::BoundTy>;
109    type Symbol: Copy + Hash + PartialEq + Eq + Debug;
110
111    // Things stored inside of tys
112    type ErrorGuaranteed: Copy + Debug + Hash + Eq;
113    type BoundExistentialPredicates: BoundExistentialPredicates<Self>;
114    type AllocId: Copy + Debug + Hash + Eq;
115    type Pat: Copy
116        + Debug
117        + Hash
118        + Eq
119        + Debug
120        + Relate<Self>
121        + Flags
122        + IntoKind<Kind = ty::PatternKind<Self>>;
123    type PatList: Copy
124        + Debug
125        + Hash
126        + Default
127        + Eq
128        + TypeVisitable<Self>
129        + SliceLike<Item = Self::Pat>;
130    type Safety: Safety<Self>;
131    type Abi: Abi<Self>;
132
133    // Kinds of consts
134    type Const: Const<Self>;
135    type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
136    type BoundConst: BoundVarLike<Self>;
137    type PlaceholderConst: PlaceholderConst<Self>;
138    type ValueConst: ValueConst<Self>;
139    type ExprConst: ExprConst<Self>;
140    type ValTree: Copy + Debug + Hash + Eq;
141
142    // Kinds of regions
143    type Region: Region<Self>;
144    type EarlyParamRegion: ParamLike;
145    type LateParamRegion: Copy + Debug + Hash + Eq;
146    type BoundRegion: BoundVarLike<Self>;
147    type PlaceholderRegion: PlaceholderLike<Self, Bound = Self::BoundRegion>;
148
149    type RegionAssumptions: Copy
150        + Debug
151        + Hash
152        + Eq
153        + SliceLike<Item = ty::OutlivesPredicate<Self, Self::GenericArg>>
154        + TypeFoldable<Self>;
155
156    // Predicates
157    type ParamEnv: ParamEnv<Self>;
158    type Predicate: Predicate<Self>;
159    type Clause: Clause<Self>;
160    type Clauses: Clauses<Self>;
161
162    fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R;
163
164    fn canonical_param_env_cache_get_or_insert<R>(
165        self,
166        param_env: Self::ParamEnv,
167        f: impl FnOnce() -> CanonicalParamEnvCacheEntry<Self>,
168        from_entry: impl FnOnce(&CanonicalParamEnvCacheEntry<Self>) -> R,
169    ) -> R;
170
171    fn evaluation_is_concurrent(&self) -> bool;
172
173    fn expand_abstract_consts<T: TypeFoldable<Self>>(self, t: T) -> T;
174
175    type GenericsOf: GenericsOf<Self>;
176    fn generics_of(self, def_id: Self::DefId) -> Self::GenericsOf;
177
178    type VariancesOf: Copy + Debug + SliceLike<Item = ty::Variance>;
179    fn variances_of(self, def_id: Self::DefId) -> Self::VariancesOf;
180
181    fn opt_alias_variances(
182        self,
183        kind: impl Into<ty::AliasTermKind>,
184        def_id: Self::DefId,
185    ) -> Option<Self::VariancesOf>;
186
187    fn type_of(self, def_id: Self::DefId) -> ty::EarlyBinder<Self, Self::Ty>;
188    fn type_of_opaque_hir_typeck(self, def_id: Self::LocalDefId)
189    -> ty::EarlyBinder<Self, Self::Ty>;
190
191    type AdtDef: AdtDef<Self>;
192    fn adt_def(self, adt_def_id: Self::DefId) -> Self::AdtDef;
193
194    fn alias_ty_kind(self, alias: ty::AliasTy<Self>) -> ty::AliasTyKind;
195
196    fn alias_term_kind(self, alias: ty::AliasTerm<Self>) -> ty::AliasTermKind;
197
198    fn trait_ref_and_own_args_for_alias(
199        self,
200        def_id: Self::DefId,
201        args: Self::GenericArgs,
202    ) -> (ty::TraitRef<Self>, Self::GenericArgsSlice);
203
204    fn mk_args(self, args: &[Self::GenericArg]) -> Self::GenericArgs;
205
206    fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
207    where
208        I: Iterator<Item = T>,
209        T: CollectAndApply<Self::GenericArg, Self::GenericArgs>;
210
211    fn check_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs) -> bool;
212
213    fn debug_assert_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs);
214
215    /// Assert that the args from an `ExistentialTraitRef` or `ExistentialProjection`
216    /// are compatible with the `DefId`.
217    fn debug_assert_existential_args_compatible(self, def_id: Self::DefId, args: Self::GenericArgs);
218
219    fn mk_type_list_from_iter<I, T>(self, args: I) -> T::Output
220    where
221        I: Iterator<Item = T>,
222        T: CollectAndApply<Self::Ty, Self::Tys>;
223
224    fn parent(self, def_id: Self::DefId) -> Self::DefId;
225
226    fn recursion_limit(self) -> usize;
227
228    type Features: Features<Self>;
229    fn features(self) -> Self::Features;
230
231    fn coroutine_hidden_types(
232        self,
233        def_id: Self::DefId,
234    ) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::CoroutineWitnessTypes<Self>>>;
235
236    fn fn_sig(
237        self,
238        def_id: Self::DefId,
239    ) -> ty::EarlyBinder<Self, ty::Binder<Self, ty::FnSig<Self>>>;
240
241    fn coroutine_movability(self, def_id: Self::DefId) -> Movability;
242
243    fn coroutine_for_closure(self, def_id: Self::DefId) -> Self::DefId;
244
245    fn generics_require_sized_self(self, def_id: Self::DefId) -> bool;
246
247    fn item_bounds(
248        self,
249        def_id: Self::DefId,
250    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
251
252    fn item_self_bounds(
253        self,
254        def_id: Self::DefId,
255    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
256
257    fn item_non_self_bounds(
258        self,
259        def_id: Self::DefId,
260    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
261
262    fn predicates_of(
263        self,
264        def_id: Self::DefId,
265    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
266
267    fn own_predicates_of(
268        self,
269        def_id: Self::DefId,
270    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
271
272    fn explicit_super_predicates_of(
273        self,
274        def_id: Self::DefId,
275    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
276
277    fn explicit_implied_predicates_of(
278        self,
279        def_id: Self::DefId,
280    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
281
282    /// This is equivalent to computing the super-predicates of the trait for this impl
283    /// and filtering them to the outlives predicates. This is purely for performance.
284    fn impl_super_outlives(
285        self,
286        impl_def_id: Self::DefId,
287    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = Self::Clause>>;
288
289    fn impl_is_const(self, def_id: Self::DefId) -> bool;
290    fn fn_is_const(self, def_id: Self::DefId) -> bool;
291    fn alias_has_const_conditions(self, def_id: Self::DefId) -> bool;
292    fn const_conditions(
293        self,
294        def_id: Self::DefId,
295    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
296    fn explicit_implied_const_bounds(
297        self,
298        def_id: Self::DefId,
299    ) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
300
301    fn impl_self_is_guaranteed_unsized(self, def_id: Self::DefId) -> bool;
302
303    fn has_target_features(self, def_id: Self::DefId) -> bool;
304
305    fn require_lang_item(self, lang_item: TraitSolverLangItem) -> Self::DefId;
306
307    fn is_lang_item(self, def_id: Self::DefId, lang_item: TraitSolverLangItem) -> bool;
308
309    fn is_default_trait(self, def_id: Self::DefId) -> bool;
310
311    fn as_lang_item(self, def_id: Self::DefId) -> Option<TraitSolverLangItem>;
312
313    fn associated_type_def_ids(self, def_id: Self::DefId) -> impl IntoIterator<Item = Self::DefId>;
314
315    fn for_each_relevant_impl(
316        self,
317        trait_def_id: Self::DefId,
318        self_ty: Self::Ty,
319        f: impl FnMut(Self::DefId),
320    );
321
322    fn has_item_definition(self, def_id: Self::DefId) -> bool;
323
324    fn impl_specializes(self, impl_def_id: Self::DefId, victim_def_id: Self::DefId) -> bool;
325
326    fn impl_is_default(self, impl_def_id: Self::DefId) -> bool;
327
328    fn impl_trait_ref(self, impl_def_id: Self::DefId) -> ty::EarlyBinder<Self, ty::TraitRef<Self>>;
329
330    fn impl_polarity(self, impl_def_id: Self::DefId) -> ty::ImplPolarity;
331
332    fn trait_is_auto(self, trait_def_id: Self::DefId) -> bool;
333
334    fn trait_is_coinductive(self, trait_def_id: Self::DefId) -> bool;
335
336    fn trait_is_alias(self, trait_def_id: Self::DefId) -> bool;
337
338    fn trait_is_dyn_compatible(self, trait_def_id: Self::DefId) -> bool;
339
340    fn trait_is_fundamental(self, def_id: Self::DefId) -> bool;
341
342    fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool;
343
344    /// Returns `true` if this is an `unsafe trait`.
345    fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool;
346
347    fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool;
348
349    fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;
350
351    fn is_general_coroutine(self, coroutine_def_id: Self::DefId) -> bool;
352    fn coroutine_is_async(self, coroutine_def_id: Self::DefId) -> bool;
353    fn coroutine_is_gen(self, coroutine_def_id: Self::DefId) -> bool;
354    fn coroutine_is_async_gen(self, coroutine_def_id: Self::DefId) -> bool;
355
356    type UnsizingParams: Deref<Target = DenseBitSet<u32>>;
357    fn unsizing_params_for_adt(self, adt_def_id: Self::DefId) -> Self::UnsizingParams;
358
359    fn anonymize_bound_vars<T: TypeFoldable<Self>>(
360        self,
361        binder: ty::Binder<Self, T>,
362    ) -> ty::Binder<Self, T>;
363
364    fn opaque_types_defined_by(self, defining_anchor: Self::LocalDefId) -> Self::LocalDefIds;
365
366    fn opaque_types_and_coroutines_defined_by(
367        self,
368        defining_anchor: Self::LocalDefId,
369    ) -> Self::LocalDefIds;
370}
371
372/// Imagine you have a function `F: FnOnce(&[T]) -> R`, plus an iterator `iter`
373/// that produces `T` items. You could combine them with
374/// `f(&iter.collect::<Vec<_>>())`, but this requires allocating memory for the
375/// `Vec`.
376///
377/// This trait allows for faster implementations, intended for cases where the
378/// number of items produced by the iterator is small. There is a blanket impl
379/// for `T` items, but there is also a fallible impl for `Result<T, E>` items.
380pub trait CollectAndApply<T, R>: Sized {
381    type Output;
382
383    /// Produce a result of type `Self::Output` from `iter`. The result will
384    /// typically be produced by applying `f` on the elements produced by
385    /// `iter`, though this may not happen in some impls, e.g. if an error
386    /// occurred during iteration.
387    fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
388    where
389        I: Iterator<Item = Self>,
390        F: FnOnce(&[T]) -> R;
391}
392
393/// The blanket impl that always collects all elements and applies `f`.
394impl<T, R> CollectAndApply<T, R> for T {
395    type Output = R;
396
397    /// Equivalent to `f(&iter.collect::<Vec<_>>())`.
398    fn collect_and_apply<I, F>(mut iter: I, f: F) -> R
399    where
400        I: Iterator<Item = T>,
401        F: FnOnce(&[T]) -> R,
402    {
403        // This code is hot enough that it's worth specializing for the most
404        // common length lists, to avoid the overhead of `Vec` creation.
405
406        let Some(t0) = iter.next() else {
407            return f(&[]);
408        };
409
410        let Some(t1) = iter.next() else {
411            return f(&[t0]);
412        };
413
414        let Some(t2) = iter.next() else {
415            return f(&[t0, t1]);
416        };
417
418        let Some(t3) = iter.next() else {
419            return f(&[t0, t1, t2]);
420        };
421
422        let Some(t4) = iter.next() else {
423            return f(&[t0, t1, t2, t3]);
424        };
425
426        let Some(t5) = iter.next() else {
427            return f(&[t0, t1, t2, t3, t4]);
428        };
429
430        let Some(t6) = iter.next() else {
431            return f(&[t0, t1, t2, t3, t4, t5]);
432        };
433
434        let Some(t7) = iter.next() else {
435            return f(&[t0, t1, t2, t3, t4, t5, t6]);
436        };
437
438        let Some(t8) = iter.next() else {
439            return f(&[t0, t1, t2, t3, t4, t5, t6, t7]);
440        };
441
442        f(&[t0, t1, t2, t3, t4, t5, t6, t7, t8].into_iter().chain(iter).collect::<Vec<_>>())
443    }
444}
445
446/// A fallible impl that will fail, without calling `f`, if there are any
447/// errors during collection.
448impl<T, R, E> CollectAndApply<T, R> for Result<T, E> {
449    type Output = Result<R, E>;
450
451    /// Equivalent to `Ok(f(&iter.collect::<Result<Vec<_>>>()?))`.
452    fn collect_and_apply<I, F>(mut iter: I, f: F) -> Result<R, E>
453    where
454        I: Iterator<Item = Result<T, E>>,
455        F: FnOnce(&[T]) -> R,
456    {
457        // This code is hot enough that it's worth specializing for the most
458        // common length lists, to avoid the overhead of `Vec` creation.
459
460        let Some(t0) = iter.next() else {
461            return Ok(f(&[]));
462        };
463        let t0 = t0?;
464
465        let Some(t1) = iter.next() else {
466            return Ok(f(&[t0]));
467        };
468        let t1 = t1?;
469
470        let Some(t2) = iter.next() else {
471            return Ok(f(&[t0, t1]));
472        };
473        let t2 = t2?;
474
475        let Some(t3) = iter.next() else {
476            return Ok(f(&[t0, t1, t2]));
477        };
478        let t3 = t3?;
479
480        let Some(t4) = iter.next() else {
481            return Ok(f(&[t0, t1, t2, t3]));
482        };
483        let t4 = t4?;
484
485        let Some(t5) = iter.next() else {
486            return Ok(f(&[t0, t1, t2, t3, t4]));
487        };
488        let t5 = t5?;
489
490        let Some(t6) = iter.next() else {
491            return Ok(f(&[t0, t1, t2, t3, t4, t5]));
492        };
493        let t6 = t6?;
494
495        let Some(t7) = iter.next() else {
496            return Ok(f(&[t0, t1, t2, t3, t4, t5, t6]));
497        };
498        let t7 = t7?;
499
500        let Some(t8) = iter.next() else {
501            return Ok(f(&[t0, t1, t2, t3, t4, t5, t6, t7]));
502        };
503        let t8 = t8?;
504
505        Ok(f(&[Ok(t0), Ok(t1), Ok(t2), Ok(t3), Ok(t4), Ok(t5), Ok(t6), Ok(t7), Ok(t8)]
506            .into_iter()
507            .chain(iter)
508            .collect::<Result<Vec<_>, _>>()?))
509    }
510}
511
512impl<I: Interner> search_graph::Cx for I {
513    type Input = CanonicalInput<I>;
514    type Result = QueryResult<I>;
515
516    type DepNodeIndex = I::DepNodeIndex;
517    type Tracked<T: Debug + Clone> = I::Tracked<T>;
518    fn mk_tracked<T: Debug + Clone>(
519        self,
520        data: T,
521        dep_node_index: I::DepNodeIndex,
522    ) -> I::Tracked<T> {
523        I::mk_tracked(self, data, dep_node_index)
524    }
525    fn get_tracked<T: Debug + Clone>(self, tracked: &I::Tracked<T>) -> T {
526        I::get_tracked(self, tracked)
527    }
528    fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, I::DepNodeIndex) {
529        I::with_cached_task(self, task)
530    }
531    fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
532        I::with_global_cache(self, f)
533    }
534    fn evaluation_is_concurrent(&self) -> bool {
535        self.evaluation_is_concurrent()
536    }
537}