Skip to main content

rustc_type_ir/
interner.rs

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