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;
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    // Kinds of tys
95    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    // Things stored inside of tys
103    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    // Kinds of consts
111    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    // Kinds of regions
120    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    // Predicates
127    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    /// Assert that the args from an `ExistentialTraitRef` or `ExistentialProjection`
177    /// are compatible with the `DefId`.
178    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 coroutine_hidden_types(
193        self,
194        def_id: Self::DefId,
195    ) -> ty::EarlyBinder<Self, ty::Binder<Self, Self::Tys>>;
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_coinductive(self, trait_def_id: Self::DefId) -> bool;
283
284    fn trait_is_alias(self, trait_def_id: Self::DefId) -> bool;
285
286    fn trait_is_dyn_compatible(self, trait_def_id: Self::DefId) -> bool;
287
288    fn trait_is_fundamental(self, def_id: Self::DefId) -> bool;
289
290    fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool;
291
292    /// Returns `true` if this is an `unsafe trait`.
293    fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool;
294
295    fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool;
296
297    fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;
298
299    fn is_general_coroutine(self, coroutine_def_id: Self::DefId) -> bool;
300    fn coroutine_is_async(self, coroutine_def_id: Self::DefId) -> bool;
301    fn coroutine_is_gen(self, coroutine_def_id: Self::DefId) -> bool;
302    fn coroutine_is_async_gen(self, coroutine_def_id: Self::DefId) -> bool;
303
304    type UnsizingParams: Deref<Target = DenseBitSet<u32>>;
305    fn unsizing_params_for_adt(self, adt_def_id: Self::DefId) -> Self::UnsizingParams;
306
307    fn find_const_ty_from_env(
308        self,
309        param_env: Self::ParamEnv,
310        placeholder: Self::PlaceholderConst,
311    ) -> Self::Ty;
312
313    fn anonymize_bound_vars<T: TypeFoldable<Self>>(
314        self,
315        binder: ty::Binder<Self, T>,
316    ) -> ty::Binder<Self, T>;
317
318    fn opaque_types_defined_by(
319        self,
320        defining_anchor: Self::LocalDefId,
321    ) -> Self::DefiningOpaqueTypes;
322}
323
324/// Imagine you have a function `F: FnOnce(&[T]) -> R`, plus an iterator `iter`
325/// that produces `T` items. You could combine them with
326/// `f(&iter.collect::<Vec<_>>())`, but this requires allocating memory for the
327/// `Vec`.
328///
329/// This trait allows for faster implementations, intended for cases where the
330/// number of items produced by the iterator is small. There is a blanket impl
331/// for `T` items, but there is also a fallible impl for `Result<T, E>` items.
332pub trait CollectAndApply<T, R>: Sized {
333    type Output;
334
335    /// Produce a result of type `Self::Output` from `iter`. The result will
336    /// typically be produced by applying `f` on the elements produced by
337    /// `iter`, though this may not happen in some impls, e.g. if an error
338    /// occurred during iteration.
339    fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
340    where
341        I: Iterator<Item = Self>,
342        F: FnOnce(&[T]) -> R;
343}
344
345/// The blanket impl that always collects all elements and applies `f`.
346impl<T, R> CollectAndApply<T, R> for T {
347    type Output = R;
348
349    /// Equivalent to `f(&iter.collect::<Vec<_>>())`.
350    fn collect_and_apply<I, F>(mut iter: I, f: F) -> R
351    where
352        I: Iterator<Item = T>,
353        F: FnOnce(&[T]) -> R,
354    {
355        // This code is hot enough that it's worth specializing for the most
356        // common length lists, to avoid the overhead of `SmallVec` creation.
357        // Lengths 0, 1, and 2 typically account for ~95% of cases. If
358        // `size_hint` is incorrect a panic will occur via an `unwrap` or an
359        // `assert`.
360        match iter.size_hint() {
361            (0, Some(0)) => {
362                assert!(iter.next().is_none());
363                f(&[])
364            }
365            (1, Some(1)) => {
366                let t0 = iter.next().unwrap();
367                assert!(iter.next().is_none());
368                f(&[t0])
369            }
370            (2, Some(2)) => {
371                let t0 = iter.next().unwrap();
372                let t1 = iter.next().unwrap();
373                assert!(iter.next().is_none());
374                f(&[t0, t1])
375            }
376            _ => f(&iter.collect::<SmallVec<[_; 8]>>()),
377        }
378    }
379}
380
381/// A fallible impl that will fail, without calling `f`, if there are any
382/// errors during collection.
383impl<T, R, E> CollectAndApply<T, R> for Result<T, E> {
384    type Output = Result<R, E>;
385
386    /// Equivalent to `Ok(f(&iter.collect::<Result<Vec<_>>>()?))`.
387    fn collect_and_apply<I, F>(mut iter: I, f: F) -> Result<R, E>
388    where
389        I: Iterator<Item = Result<T, E>>,
390        F: FnOnce(&[T]) -> R,
391    {
392        // This code is hot enough that it's worth specializing for the most
393        // common length lists, to avoid the overhead of `SmallVec` creation.
394        // Lengths 0, 1, and 2 typically account for ~95% of cases. If
395        // `size_hint` is incorrect a panic will occur via an `unwrap` or an
396        // `assert`, unless a failure happens first, in which case the result
397        // will be an error anyway.
398        Ok(match iter.size_hint() {
399            (0, Some(0)) => {
400                assert!(iter.next().is_none());
401                f(&[])
402            }
403            (1, Some(1)) => {
404                let t0 = iter.next().unwrap()?;
405                assert!(iter.next().is_none());
406                f(&[t0])
407            }
408            (2, Some(2)) => {
409                let t0 = iter.next().unwrap()?;
410                let t1 = iter.next().unwrap()?;
411                assert!(iter.next().is_none());
412                f(&[t0, t1])
413            }
414            _ => f(&iter.collect::<Result<SmallVec<[_; 8]>, _>>()?),
415        })
416    }
417}
418
419impl<I: Interner> search_graph::Cx for I {
420    type Input = CanonicalInput<I>;
421    type Result = QueryResult<I>;
422
423    type DepNodeIndex = I::DepNodeIndex;
424    type Tracked<T: Debug + Clone> = I::Tracked<T>;
425    fn mk_tracked<T: Debug + Clone>(
426        self,
427        data: T,
428        dep_node_index: I::DepNodeIndex,
429    ) -> I::Tracked<T> {
430        I::mk_tracked(self, data, dep_node_index)
431    }
432    fn get_tracked<T: Debug + Clone>(self, tracked: &I::Tracked<T>) -> T {
433        I::get_tracked(self, tracked)
434    }
435    fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, I::DepNodeIndex) {
436        I::with_cached_task(self, task)
437    }
438    fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
439        I::with_global_cache(self, f)
440    }
441    fn evaluation_is_concurrent(&self) -> bool {
442        self.evaluation_is_concurrent()
443    }
444}