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