Skip to main content

rustc_middle/ty/context/
impl_interner.rs

1//! Implementation of [`rustc_type_ir::Interner`] for [`TyCtxt`].
2
3use std::{debug_assert_matches, fmt};
4
5use rustc_attr_ir::lang_items::LangItem;
6use rustc_data_structures::intern::Interned;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir as hir;
9use rustc_hir::def::{CtorKind, DefKind};
10use rustc_hir::def_id::{DefId, LocalDefId};
11use rustc_span::{DUMMY_SP, Span, Symbol, bug};
12use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverProjectionLangItem, SolverTraitLangItem};
13use rustc_type_ir::solve::CanonicalInputData;
14use rustc_type_ir::{
15    BoundVar, CollectAndApply, DebruijnIndex, Interner, RegionVid, TypeFoldable, Unnormalized,
16    VisitorResult, WithCachedTypeInfo, search_graph, try_visit,
17};
18
19use crate::dep_graph::{DepKind, DepNodeIndex};
20use crate::infer::canonical::CanonicalVarKinds;
21use crate::mir::interpret::Scalar;
22use crate::traits::cache::WithDepNode;
23use crate::traits::solve::{
24    self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
25};
26use crate::ty::{
27    self, BoundRegion, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate,
28    Region, RegionKind, RequiredDepth, Ty, TyCtxt,
29};
30
31#[allow(rustc::usage_of_ty_tykind)]
32impl<'tcx> Interner for TyCtxt<'tcx> {
33    fn next_trait_solver_globally(self) -> bool {
34        self.next_trait_solver_globally()
35    }
36
37    type DefId = DefId;
38    type LocalDefId = LocalDefId;
39    type TraitId = DefId;
40    type ForeignId = DefId;
41    type FunctionId = DefId;
42    type ClosureId = DefId;
43    type CoroutineClosureId = DefId;
44    type CoroutineId = DefId;
45    type AdtId = DefId;
46    type ImplId = DefId;
47    type AnonConstId = DefId;
48    type TraitAssocTyId = DefId;
49    type TraitAssocConstId = DefId;
50    type TraitAssocTermId = DefId;
51    type OpaqueTyId = DefId;
52    type LocalOpaqueTyId = LocalDefId;
53    type FreeTyAliasId = DefId;
54    type FreeConstAliasId = DefId;
55    type FreeTermAliasId = DefId;
56    type ImplOrTraitAssocTyId = DefId;
57    type ImplOrTraitAssocConstId = DefId;
58    type ImplOrTraitAssocTermId = DefId;
59    type InherentAssocTyId = DefId;
60    type InherentAssocConstId = DefId;
61    type InherentAssocTermId = DefId;
62    type Span = Span;
63
64    type GenericArgs = ty::GenericArgsRef<'tcx>;
65
66    type GenericArgsSlice = &'tcx [ty::GenericArg<'tcx>];
67    type GenericArg = ty::GenericArg<'tcx>;
68    type Term = ty::Term<'tcx>;
69    type BoundVarKinds = &'tcx List<ty::BoundVariableKind<'tcx>>;
70    type TypingEnv = ty::TypingEnv<'tcx>;
71
72    type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
73
74    fn mk_predefined_opaques_in_body(
75        self,
76        data: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)],
77    ) -> Self::PredefinedOpaques {
78        self.mk_predefined_opaques_in_body(data)
79    }
80    type LocalDefIds = &'tcx ty::List<LocalDefId>;
81    type CanonicalVarKinds = CanonicalVarKinds<'tcx>;
82    fn mk_canonical_var_kinds(
83        self,
84        kinds: &[ty::CanonicalVarKind<Self>],
85    ) -> Self::CanonicalVarKinds {
86        self.mk_canonical_var_kinds(kinds)
87    }
88
89    type ExternalConstraints = ExternalConstraints<'tcx>;
90    fn mk_external_constraints(
91        self,
92        data: ExternalConstraintsData<Self>,
93    ) -> ExternalConstraints<'tcx> {
94        self.mk_external_constraints(data)
95    }
96    type DepNodeIndex = DepNodeIndex;
97    fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, DepNodeIndex) {
98        self.dep_graph.with_anon_task(self, DepKind::TraitSelect, task)
99    }
100    type Ty = Ty<'tcx>;
101    type Tys = &'tcx List<Ty<'tcx>>;
102
103    type FnInputTys = &'tcx [Ty<'tcx>];
104    type ParamTy = ParamTy;
105    type Symbol = Symbol;
106
107    type ErrorGuaranteed = ErrorGuaranteed;
108    type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
109
110    type AllocId = crate::mir::interpret::AllocId;
111    type Pat = Pattern<'tcx>;
112    type PatList = &'tcx List<Pattern<'tcx>>;
113    type Safety = hir::Safety;
114    type Consts = &'tcx List<ty::Const<'tcx>>;
115
116    type ParamConst = ty::ParamConst;
117    type ValueConst = ty::Value<'tcx>;
118    type ExprConst = ty::Expr<'tcx>;
119    type ValTree = ty::ValTree<'tcx>;
120    type ScalarInt = ty::ScalarInt;
121    type Scalar = Scalar;
122    type InternedRegionKind = Interned<'tcx, ty::RegionKind<'tcx>>;
123    type InternedConstKind = Interned<'tcx, WithCachedTypeInfo<ty::ConstKind<'tcx>>>;
124    type EarlyParamRegion = ty::EarlyParamRegion;
125    type LateParamRegionKind = ty::LateParamRegionKind;
126
127    type RegionAssumptions = &'tcx ty::List<ty::ArgOutlivesClause<'tcx>>;
128
129    type ParamEnv = ty::ParamEnv<'tcx>;
130    type Predicate = Predicate<'tcx>;
131
132    type Clause = Clause<'tcx>;
133    type Clauses = ty::Clauses<'tcx>;
134
135    type Tracked<T: fmt::Debug + Clone> = WithDepNode<T>;
136    fn mk_tracked<T: fmt::Debug + Clone>(
137        self,
138        data: T,
139        dep_node: DepNodeIndex,
140    ) -> Self::Tracked<T> {
141        WithDepNode::new(dep_node, data)
142    }
143    fn get_tracked<T: fmt::Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T {
144        tracked.get(self)
145    }
146
147    fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
148        f(&mut *self.caches.new_solver_evaluation_cache.lock())
149    }
150
151    fn with_canonical_param_env_cache<R>(
152        self,
153        f: impl FnOnce(&mut ty::CanonicalParamEnvCache<Self>) -> R,
154    ) -> R {
155        f(&mut *self.caches.new_solver_canonical_param_env_cache.lock())
156    }
157
158    fn assert_evaluation_is_concurrent(&self) {
159        // Turns out, the assumption for this function isn't perfect.
160        // See trait-system-refactor-initiative#234.
161    }
162
163    fn expand_abstract_consts<T: TypeFoldable<TyCtxt<'tcx>>>(self, t: T) -> T {
164        self.expand_abstract_consts(t)
165    }
166
167    type GenericsOf = &'tcx ty::Generics;
168
169    fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
170        self.generics_of(def_id)
171    }
172
173    type VariancesOf = &'tcx [ty::Variance];
174
175    fn variances_of(self, def_id: DefId) -> Self::VariancesOf {
176        self.variances_of(def_id)
177    }
178
179    fn opt_alias_variances(
180        self,
181        kind: impl Into<ty::AliasTermKind<'tcx>>,
182    ) -> Option<&'tcx [ty::Variance]> {
183        self.opt_alias_variances(kind)
184    }
185
186    fn type_of(self, def_id: DefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
187        self.type_of(def_id)
188    }
189    fn type_of_opaque_hir_typeck(self, def_id: LocalDefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
190        self.type_of_opaque_hir_typeck(def_id)
191    }
192    fn is_direct_const(self, alias: ty::AliasConstKind<'tcx>) -> bool {
193        match alias {
194            ty::AliasConstKind::Projection { def_id }
195            | ty::AliasConstKind::InherentSelf { def_id }
196            | ty::AliasConstKind::InherentImpl { def_id }
197            | ty::AliasConstKind::Free { def_id } => self.is_direct_const(def_id),
198            ty::AliasConstKind::Anon { .. } => false,
199        }
200    }
201    fn const_of_item(
202        self,
203        alias: ty::AliasConstKind<'tcx>,
204    ) -> Option<ty::EarlyBinder<'tcx, Const<'tcx>>> {
205        match alias {
206            ty::AliasConstKind::Projection { def_id }
207            | ty::AliasConstKind::InherentSelf { def_id }
208            | ty::AliasConstKind::InherentImpl { def_id }
209            | ty::AliasConstKind::Free { def_id } => self.const_of_item(def_id),
210            ty::AliasConstKind::Anon { .. } => None,
211        }
212    }
213
214    fn def_span(self, def_id: DefId) -> Span {
215        self.def_span(def_id)
216    }
217
218    type AdtDef = ty::AdtDef<'tcx>;
219    fn adt_def(self, adt_def_id: DefId) -> Self::AdtDef {
220        self.adt_def(adt_def_id)
221    }
222
223    fn trait_ref_and_own_args_for_alias(
224        self,
225        def_id: DefId,
226        args: ty::GenericArgsRef<'tcx>,
227    ) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
228        if true {
    {
        match self.def_kind(def_id) {
            DefKind::AssocTy | DefKind::AssocConst => {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::AssocTy | DefKind::AssocConst",
                    ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
229        let trait_def_id = self.parent(def_id);
230        if true {
    {
        match self.def_kind(trait_def_id) {
            DefKind::Trait => {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::Trait", ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
231        let trait_ref = ty::TraitRef::from_assoc(self, trait_def_id, args);
232        (trait_ref, &args[trait_ref.args.len()..])
233    }
234
235    fn mk_args(self, args: &[Self::GenericArg]) -> ty::GenericArgsRef<'tcx> {
236        self.mk_args(args)
237    }
238
239    fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
240    where
241        I: Iterator<Item = T>,
242        T: CollectAndApply<Self::GenericArg, ty::GenericArgsRef<'tcx>>,
243    {
244        self.mk_args_from_iter(args)
245    }
246
247    fn check_alias_term_args_compatible(
248        self,
249        kind: ty::AliasTermKind<'tcx>,
250        args: ty::GenericArgsRef<'tcx>,
251    ) -> bool {
252        self.check_alias_term_args_compatible(kind, args)
253    }
254
255    fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) {
256        self.debug_assert_args_compatible(def_id, args);
257    }
258
259    fn debug_assert_alias_term_args_compatible(
260        self,
261        kind: ty::AliasTermKind<'tcx>,
262        args: ty::GenericArgsRef<'tcx>,
263    ) {
264        self.debug_assert_alias_term_args_compatible(kind, args);
265    }
266
267    /// Assert that the args from an `ExistentialTraitRef` or `ExistentialProjection`
268    /// are compatible with the `DefId`. Since we're missing a `Self` type, stick on
269    /// a dummy self type and forward to `debug_assert_args_compatible`.
270    fn debug_assert_existential_args_compatible(
271        self,
272        def_id: Self::DefId,
273        args: Self::GenericArgs,
274    ) {
275        // FIXME: We could perhaps add a `skip: usize` to `debug_assert_args_compatible`
276        // to avoid needing to reintern the set of args...
277        if truecfg!(debug_assertions) {
278            self.debug_assert_args_compatible(
279                def_id,
280                self.mk_args_from_iter(
281                    [self.types.trait_object_dummy_self.into()].into_iter().chain(args.iter()),
282                ),
283            );
284        }
285    }
286
287    fn mk_type_list_from_iter<I, T>(self, args: I) -> T::Output
288    where
289        I: Iterator<Item = T>,
290        T: CollectAndApply<Ty<'tcx>, &'tcx List<Ty<'tcx>>>,
291    {
292        self.mk_type_list_from_iter(args)
293    }
294
295    fn mk_ct_from_kind(self, kind: ty::ConstKind<'tcx>) -> ty::Const<'tcx> {
296        self.mk_ct_from_kind(kind)
297    }
298
299    fn projection_parent(self, def_id: Self::TraitAssocTermId) -> Self::TraitId {
300        self.parent(def_id)
301    }
302
303    fn impl_or_trait_assoc_term_parent(self, def_id: Self::ImplOrTraitAssocTyId) -> DefId {
304        self.parent(def_id)
305    }
306
307    fn inherent_alias_term_parent(self, def_id: Self::InherentAssocTermId) -> Self::ImplId {
308        self.parent(def_id)
309    }
310
311    fn recursion_limit(self) -> usize {
312        self.recursion_limit().0
313    }
314
315    type Features = &'tcx rustc_feature::Features;
316
317    fn features(self) -> Self::Features {
318        self.features()
319    }
320
321    fn assumptions_on_binders(self) -> bool {
322        self.assumptions_on_binders()
323    }
324
325    fn renormalize_rigid_aliases(self) -> bool {
326        self.renormalize_rigid_aliases()
327    }
328
329    fn coroutine_hidden_types(
330        self,
331        def_id: DefId,
332    ) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
333        self.coroutine_hidden_types(def_id)
334    }
335
336    fn fn_sig(self, def_id: DefId) -> ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>> {
337        self.fn_sig(def_id)
338    }
339
340    fn coroutine_movability(self, def_id: DefId) -> rustc_ast::Movability {
341        self.coroutine_movability(def_id)
342    }
343
344    fn coroutine_for_closure(self, def_id: DefId) -> DefId {
345        self.coroutine_for_closure(def_id)
346    }
347
348    fn generics_require_sized_self(self, def_id: DefId) -> bool {
349        self.generics_require_sized_self(def_id)
350    }
351
352    fn item_bounds(
353        self,
354        def_id: DefId,
355    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
356        self.item_bounds(def_id).map_bound(IntoIterator::into_iter)
357    }
358
359    fn item_self_bounds(
360        self,
361        def_id: DefId,
362    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
363        self.item_self_bounds(def_id).map_bound(IntoIterator::into_iter)
364    }
365
366    fn item_non_self_bounds(
367        self,
368        def_id: DefId,
369    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
370        self.item_non_self_bounds(def_id).map_bound(IntoIterator::into_iter)
371    }
372
373    fn clauses_of(
374        self,
375        def_id: DefId,
376    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
377        ty::EarlyBinder::bind_iter(
378            self.clauses_of(def_id)
379                .instantiate_identity(self)
380                .clauses
381                .into_iter()
382                .map(Unnormalized::skip_normalization),
383        )
384    }
385
386    fn own_clauses_of(
387        self,
388        def_id: DefId,
389    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
390        ty::EarlyBinder::bind_iter(
391            self.clauses_of(def_id)
392                .instantiate_own_identity()
393                .map(|(clause, _)| clause.skip_normalization()),
394        )
395    }
396
397    fn explicit_super_clauses_of(
398        self,
399        def_id: DefId,
400    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
401        self.explicit_super_clauses_of(def_id).map_bound(|preds| preds.into_iter().copied())
402    }
403
404    fn explicit_implied_clauses_of(
405        self,
406        def_id: DefId,
407    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
408        self.explicit_implied_clauses_of(def_id).map_bound(|preds| preds.into_iter().copied())
409    }
410
411    fn impl_super_outlives(
412        self,
413        impl_def_id: DefId,
414    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
415        self.impl_super_outlives(impl_def_id)
416    }
417
418    fn supertrait_def_ids(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> {
419        rustc_type_ir::elaborate::supertrait_def_ids(self, trait_def_id)
420    }
421
422    fn impl_is_const(self, def_id: DefId) -> bool {
423        if true {
    {
        match self.def_kind(def_id) {
            DefKind::Impl { of_trait: true } => {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::Impl { of_trait: true }",
                    ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(self.def_kind(def_id), DefKind::Impl { of_trait: true });
424        self.is_conditionally_const(def_id)
425    }
426
427    fn fn_is_const(self, def_id: DefId) -> bool {
428        if true {
    {
        match self.def_kind(def_id) {
            DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) =>
                {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn)",
                    ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(
429            self.def_kind(def_id),
430            DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn)
431        );
432        self.is_conditionally_const(def_id)
433    }
434
435    fn closure_is_const(self, def_id: DefId) -> bool {
436        if true {
    {
        match self.def_kind(def_id) {
            DefKind::Closure => {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::Closure", ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(self.def_kind(def_id), DefKind::Closure);
437        #[allow(non_exhaustive_omitted_patterns)] match self.constness(def_id) {
    hir::Constness::Const { always: false } => true,
    _ => false,
}matches!(self.constness(def_id), hir::Constness::Const { always: false })
438    }
439
440    fn alias_has_const_conditions(self, def_id: DefId) -> bool {
441        if true {
    {
        match self.def_kind(def_id) {
            DefKind::AssocTy | DefKind::OpaqueTy => {}
            ref left_val => {
                ::core::panicking::assert_matches_failed(left_val,
                    "DefKind::AssocTy | DefKind::OpaqueTy",
                    ::core::option::Option::None);
            }
        }
    };
};debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::OpaqueTy);
442        self.is_conditionally_const(def_id)
443    }
444
445    fn const_conditions(
446        self,
447        def_id: DefId,
448    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
449        ty::EarlyBinder::bind_iter(
450            self.const_conditions(def_id)
451                .instantiate_identity(self)
452                .into_iter()
453                .map(|(c, _)| c.skip_normalization()),
454        )
455    }
456
457    fn explicit_implied_const_bounds(
458        self,
459        def_id: DefId,
460    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
461        ty::EarlyBinder::bind_iter(
462            self.explicit_implied_const_bounds(def_id)
463                .iter_identity_copied()
464                .map(Unnormalized::skip_normalization)
465                .map(|(c, _)| c),
466        )
467    }
468
469    fn impl_self_is_guaranteed_unsized(self, impl_def_id: DefId) -> bool {
470        self.impl_self_is_guaranteed_unsized(impl_def_id)
471    }
472
473    fn has_target_features(self, def_id: DefId) -> bool {
474        !self.codegen_fn_attrs(def_id).target_features.is_empty()
475    }
476
477    fn require_projection_lang_item(self, lang_item: SolverProjectionLangItem) -> DefId {
478        self.require_lang_item(solver_lang_item_to_lang_item(lang_item), DUMMY_SP)
479    }
480
481    fn require_trait_lang_item(self, lang_item: SolverTraitLangItem) -> DefId {
482        self.require_lang_item(solver_trait_lang_item_to_lang_item(lang_item), DUMMY_SP)
483    }
484
485    fn require_adt_lang_item(self, lang_item: SolverAdtLangItem) -> DefId {
486        self.require_lang_item(solver_adt_lang_item_to_lang_item(lang_item), DUMMY_SP)
487    }
488
489    fn is_projection_lang_item(self, def_id: DefId, lang_item: SolverProjectionLangItem) -> bool {
490        self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
491    }
492
493    fn is_trait_lang_item(self, def_id: DefId, lang_item: SolverTraitLangItem) -> bool {
494        self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
495    }
496
497    fn is_default_trait(self, def_id: DefId) -> bool {
498        self.is_default_trait(def_id)
499    }
500
501    fn is_sizedness_trait(self, def_id: DefId) -> bool {
502        self.is_sizedness_trait(def_id)
503    }
504
505    fn as_projection_lang_item(self, def_id: DefId) -> Option<SolverProjectionLangItem> {
506        lang_item_to_solver_lang_item(self.lang_items().from_def_id(def_id)?)
507    }
508
509    fn as_trait_lang_item(self, def_id: DefId) -> Option<SolverTraitLangItem> {
510        lang_item_to_solver_trait_lang_item(self.lang_items().from_def_id(def_id)?)
511    }
512
513    fn as_adt_lang_item(self, def_id: DefId) -> Option<SolverAdtLangItem> {
514        lang_item_to_solver_adt_lang_item(self.lang_items().from_def_id(def_id)?)
515    }
516
517    fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
518        self.associated_items(def_id)
519            .in_definition_order()
520            .filter(|assoc_item| assoc_item.is_type())
521            .map(|assoc_item| assoc_item.def_id)
522    }
523
524    // This signature is a bit different from `TyCtxt::for_each_relevant_impl`.
525    // While rustc only needs self_ty, rust-analyzer's impl needs to use all the args.
526    fn for_each_relevant_impl<R: VisitorResult>(
527        self,
528        trait_ref: ty::TraitRef<'tcx>,
529        f: impl FnMut(DefId) -> R,
530    ) -> R {
531        let self_ty = trait_ref.args.type_at(0);
532        if true {
    if !!#[allow(non_exhaustive_omitted_patterns)] match self_ty.kind() {
                    ty::Infer(ty::TyVar(_)) | ty::Param(_) | ty::Bound(_, _) =>
                        true,
                    _ => false,
                } {
        {
            ::core::panicking::panic_fmt(format_args!("we should not have them as self ty in the next solver"));
        }
    };
};debug_assert!(
533            !matches!(self_ty.kind(), ty::Infer(ty::TyVar(_)) | ty::Param(_) | ty::Bound(_, _)),
534            "we should not have them as self ty in the next solver"
535        );
536        TyCtxt::for_each_relevant_impl(self, trait_ref.def_id, self_ty, f)
537    }
538    fn for_each_blanket_impl<R: VisitorResult>(
539        self,
540        trait_def_id: DefId,
541        mut f: impl FnMut(DefId) -> R,
542    ) -> R {
543        let trait_impls = self.trait_impls_of(trait_def_id);
544        for &impl_def_id in trait_impls.blanket_impls() {
545            match ::rustc_ast_ir::visit::VisitorResult::branch(f(impl_def_id)) {
    core::ops::ControlFlow::Continue(()) =>
        (),
        #[allow(unreachable_code)]
        core::ops::ControlFlow::Break(r) => {
        return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
    }
};try_visit!(f(impl_def_id));
546        }
547
548        R::output()
549    }
550
551    fn has_item_definition(self, def_id: DefId) -> bool {
552        self.defaultness(def_id).has_value()
553    }
554
555    fn impl_specializes(self, impl_def_id: Self::DefId, victim_def_id: Self::DefId) -> bool {
556        self.specializes((impl_def_id, victim_def_id))
557    }
558
559    fn impl_is_default(self, impl_def_id: DefId) -> bool {
560        self.defaultness(impl_def_id).is_default()
561    }
562
563    fn impl_trait_ref(self, impl_def_id: DefId) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> {
564        self.impl_trait_ref(impl_def_id)
565    }
566
567    fn impl_polarity(self, impl_def_id: DefId) -> ty::ImplPolarity {
568        self.impl_polarity(impl_def_id)
569    }
570
571    fn is_fully_generic_for_reflection(self, impl_def_id: Self::ImplId) -> bool {
572        self.impl_is_fully_generic_for_reflection(impl_def_id)
573    }
574
575    fn trait_is_auto(self, trait_def_id: DefId) -> bool {
576        self.trait_is_auto(trait_def_id)
577    }
578
579    fn trait_is_marker(self, trait_def_id: DefId) -> bool {
580        self.trait_def(trait_def_id).is_marker
581    }
582
583    fn trait_is_coinductive(self, trait_def_id: DefId) -> bool {
584        self.trait_is_coinductive(trait_def_id)
585    }
586
587    fn trait_is_alias(self, trait_def_id: DefId) -> bool {
588        self.trait_is_alias(trait_def_id)
589    }
590
591    fn trait_is_dyn_compatible(self, trait_def_id: DefId) -> bool {
592        self.is_dyn_compatible(trait_def_id)
593    }
594
595    fn trait_is_fundamental(self, def_id: DefId) -> bool {
596        self.trait_def(def_id).is_fundamental
597    }
598
599    fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
600        self.trait_def(trait_def_id).safety.is_unsafe()
601    }
602
603    fn delay_bug(self, msg: impl ToString) -> ErrorGuaranteed {
604        self.dcx().span_delayed_bug(DUMMY_SP, msg.to_string())
605    }
606
607    fn span_delayed_bug(self, span: Self::Span, msg: impl ToString) -> ErrorGuaranteed {
608        self.dcx().span_delayed_bug(span, msg.to_string())
609    }
610
611    fn is_general_coroutine(self, coroutine_def_id: DefId) -> bool {
612        self.is_general_coroutine(coroutine_def_id)
613    }
614
615    fn coroutine_is_async(self, coroutine_def_id: DefId) -> bool {
616        self.coroutine_is_async(coroutine_def_id)
617    }
618
619    fn coroutine_is_gen(self, coroutine_def_id: DefId) -> bool {
620        self.coroutine_is_gen(coroutine_def_id)
621    }
622
623    fn coroutine_is_async_gen(self, coroutine_def_id: DefId) -> bool {
624        self.coroutine_is_async_gen(coroutine_def_id)
625    }
626
627    type UnsizingParams = &'tcx rustc_index::bit_set::DenseBitSet<u32>;
628    fn unsizing_params_for_adt(self, adt_def_id: DefId) -> Self::UnsizingParams {
629        self.unsizing_params_for_adt(adt_def_id)
630    }
631
632    fn anonymize_bound_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
633        self,
634        binder: ty::Binder<'tcx, T>,
635    ) -> ty::Binder<'tcx, T> {
636        self.anonymize_bound_vars(binder)
637    }
638
639    fn opaque_types_defined_by(self, defining_anchor: LocalDefId) -> Self::LocalDefIds {
640        self.opaque_types_defined_by(defining_anchor)
641    }
642
643    fn opaque_types_and_coroutines_defined_by(
644        self,
645        defining_anchor: Self::LocalDefId,
646    ) -> Self::LocalDefIds {
647        let coroutines_defined_by = self
648            .nested_bodies_within(defining_anchor)
649            .iter()
650            .filter(|def_id| self.is_coroutine(def_id.to_def_id()));
651        self.mk_local_def_ids_from_iter(
652            self.opaque_types_defined_by(defining_anchor).iter().chain(coroutines_defined_by),
653        )
654    }
655
656    type Probe = &'tcx inspect::Probe<TyCtxt<'tcx>>;
657    fn mk_probe(self, probe: inspect::Probe<Self>) -> &'tcx inspect::Probe<TyCtxt<'tcx>> {
658        self.arena.alloc(probe)
659    }
660    type CanonicalInput = CanonicalInput<'tcx>;
661    fn mk_canonical_input(self, data: CanonicalInputData<Self>) -> CanonicalInput<'tcx> {
662        self.intern_canonical_input(data)
663    }
664    fn evaluate_root_goal_for_proof_tree_raw(
665        self,
666        canonical_goal: CanonicalInput<'tcx>,
667        root_depth: usize,
668    ) -> (QueryResult<'tcx>, &'tcx inspect::Probe<TyCtxt<'tcx>>, RequiredDepth) {
669        self.evaluate_root_goal_for_proof_tree_raw((canonical_goal, root_depth))
670    }
671
672    fn item_name(self, id: DefId) -> Symbol {
673        self.opt_item_name(id).unwrap_or_else(|| {
674            ::rustc_span::macros::bug_impl(None,
    format_args!("item_name: no name for {0:?}", self.def_path(id)),
    Location::caller());bug!("item_name: no name for {:?}", self.def_path(id));
675        })
676    }
677
678    fn get_re_static_lifetime(self) -> Region<'tcx> {
679        self.lifetimes.re_static
680    }
681
682    fn intern_re_var(self, rv: RegionVid) -> Region<'tcx> {
683        // Use a pre-interned one when possible.
684        self.lifetimes
685            .re_vars
686            .get(rv.as_usize())
687            .copied()
688            .unwrap_or_else(|| self.intern_region(ty::ReVar(rv)))
689    }
690
691    fn intern_region(self, region_kind: RegionKind<'tcx>) -> Region<'tcx> {
692        self.intern_region(region_kind)
693    }
694
695    fn intern_bound_region(
696        self,
697        debruijn: DebruijnIndex,
698        bound_region: BoundRegion<'tcx>,
699    ) -> Region<'tcx> {
700        // Use a pre-interned one when possible.
701        if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region
702            && let Some(inner) = self.lifetimes.re_bound_bound_anons.get(debruijn.as_usize())
703            && let Some(re) = inner.get(var.as_usize()).copied()
704        {
705            re
706        } else {
707            self.intern_region(ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), bound_region))
708        }
709    }
710
711    fn intern_canonical_bound(self, var: BoundVar) -> Region<'tcx> {
712        // Use a pre-interned one when possible.
713        if let Some(re) = self.lifetimes.re_bound_canonical_anons.get(var.as_usize()).copied() {
714            re
715        } else {
716            self.intern_region(ty::ReBound(
717                ty::BoundVarIndexKind::Canonical,
718                BoundRegion { var, kind: ty::BoundRegionKind::Anon },
719            ))
720        }
721    }
722
723    fn intern_valtree(self, valtree_kind: ty::ValTreeKind<TyCtxt<'tcx>>) -> ty::ValTree<'tcx> {
724        self.intern_valtree(valtree_kind)
725    }
726
727    fn const_zst(self) -> ty::ValTree<'tcx> {
728        self.consts.valtree_zst
729    }
730}
731
732impl<'tcx, T: Clone + Copy> rustc_type_ir::intern::Interned<TyCtxt<'tcx>> for Interned<'tcx, T> {
733    type Value = T;
734    fn get(self) -> T {
735        *self.0
736    }
737}
738
739/// Defines trivial conversion functions between the main [`LangItem`] enum,
740/// and some other lang-item enum that is a subset of it.
741macro_rules! bidirectional_lang_item_map {
742    (
743        $solver_ty:ident, fn $to_solver:ident, fn $from_solver:ident;
744        $($name:ident),+ $(,)?
745    ) => {
746        fn $from_solver(lang_item: $solver_ty) -> LangItem {
747            match lang_item {
748                $($solver_ty::$name => LangItem::$name,)+
749            }
750        }
751
752        fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> {
753            Some(match lang_item {
754                $(LangItem::$name => $solver_ty::$name,)+
755                _ => return None,
756            })
757        }
758    }
759}
760
761fn solver_lang_item_to_lang_item(lang_item: SolverProjectionLangItem)
    -> LangItem {
    match lang_item {
        SolverProjectionLangItem::AsyncFnKindUpvars =>
            LangItem::AsyncFnKindUpvars,
        SolverProjectionLangItem::AsyncFnOnceOutput =>
            LangItem::AsyncFnOnceOutput,
        SolverProjectionLangItem::CallOnceFuture => LangItem::CallOnceFuture,
        SolverProjectionLangItem::CallRefFuture => LangItem::CallRefFuture,
        SolverProjectionLangItem::CoroutineReturn =>
            LangItem::CoroutineReturn,
        SolverProjectionLangItem::CoroutineYield => LangItem::CoroutineYield,
        SolverProjectionLangItem::FieldBase => LangItem::FieldBase,
        SolverProjectionLangItem::FieldType => LangItem::FieldType,
        SolverProjectionLangItem::FutureOutput => LangItem::FutureOutput,
        SolverProjectionLangItem::Metadata => LangItem::Metadata,
    }
}
fn lang_item_to_solver_lang_item(lang_item: LangItem)
    -> Option<SolverProjectionLangItem> {
    Some(match lang_item {
            LangItem::AsyncFnKindUpvars =>
                SolverProjectionLangItem::AsyncFnKindUpvars,
            LangItem::AsyncFnOnceOutput =>
                SolverProjectionLangItem::AsyncFnOnceOutput,
            LangItem::CallOnceFuture =>
                SolverProjectionLangItem::CallOnceFuture,
            LangItem::CallRefFuture =>
                SolverProjectionLangItem::CallRefFuture,
            LangItem::CoroutineReturn =>
                SolverProjectionLangItem::CoroutineReturn,
            LangItem::CoroutineYield =>
                SolverProjectionLangItem::CoroutineYield,
            LangItem::FieldBase => SolverProjectionLangItem::FieldBase,
            LangItem::FieldType => SolverProjectionLangItem::FieldType,
            LangItem::FutureOutput => SolverProjectionLangItem::FutureOutput,
            LangItem::Metadata => SolverProjectionLangItem::Metadata,
            _ => return None,
        })
}bidirectional_lang_item_map! {
762    SolverProjectionLangItem, fn lang_item_to_solver_lang_item, fn solver_lang_item_to_lang_item;
763
764// tidy-alphabetical-start
765    AsyncFnKindUpvars,
766    AsyncFnOnceOutput,
767    CallOnceFuture,
768    CallRefFuture,
769    CoroutineReturn,
770    CoroutineYield,
771    FieldBase,
772    FieldType,
773    FutureOutput,
774    Metadata,
775// tidy-alphabetical-end
776}
777
778fn solver_adt_lang_item_to_lang_item(lang_item: SolverAdtLangItem)
    -> LangItem {
    match lang_item {
        SolverAdtLangItem::DynMetadata => LangItem::DynMetadata,
        SolverAdtLangItem::Option => LangItem::Option,
        SolverAdtLangItem::OwnedBox => LangItem::OwnedBox,
        SolverAdtLangItem::Poll => LangItem::Poll,
    }
}
fn lang_item_to_solver_adt_lang_item(lang_item: LangItem)
    -> Option<SolverAdtLangItem> {
    Some(match lang_item {
            LangItem::DynMetadata => SolverAdtLangItem::DynMetadata,
            LangItem::Option => SolverAdtLangItem::Option,
            LangItem::OwnedBox => SolverAdtLangItem::OwnedBox,
            LangItem::Poll => SolverAdtLangItem::Poll,
            _ => return None,
        })
}bidirectional_lang_item_map! {
779    SolverAdtLangItem, fn lang_item_to_solver_adt_lang_item, fn solver_adt_lang_item_to_lang_item;
780
781// tidy-alphabetical-start
782    DynMetadata,
783    Option,
784    OwnedBox,
785    Poll,
786// tidy-alphabetical-end
787}
788
789fn solver_trait_lang_item_to_lang_item(lang_item: SolverTraitLangItem)
    -> LangItem {
    match lang_item {
        SolverTraitLangItem::AsyncFn => LangItem::AsyncFn,
        SolverTraitLangItem::AsyncFnKindHelper => LangItem::AsyncFnKindHelper,
        SolverTraitLangItem::AsyncFnMut => LangItem::AsyncFnMut,
        SolverTraitLangItem::AsyncFnOnce => LangItem::AsyncFnOnce,
        SolverTraitLangItem::AsyncIterator => LangItem::AsyncIterator,
        SolverTraitLangItem::BikeshedGuaranteedNoDrop =>
            LangItem::BikeshedGuaranteedNoDrop,
        SolverTraitLangItem::Clone => LangItem::Clone,
        SolverTraitLangItem::Copy => LangItem::Copy,
        SolverTraitLangItem::Coroutine => LangItem::Coroutine,
        SolverTraitLangItem::Destruct => LangItem::Destruct,
        SolverTraitLangItem::DiscriminantKind => LangItem::DiscriminantKind,
        SolverTraitLangItem::Drop => LangItem::Drop,
        SolverTraitLangItem::Field => LangItem::Field,
        SolverTraitLangItem::Fn => LangItem::Fn,
        SolverTraitLangItem::FnMut => LangItem::FnMut,
        SolverTraitLangItem::FnOnce => LangItem::FnOnce,
        SolverTraitLangItem::FnPtrTrait => LangItem::FnPtrTrait,
        SolverTraitLangItem::Freeze => LangItem::Freeze,
        SolverTraitLangItem::FusedIterator => LangItem::FusedIterator,
        SolverTraitLangItem::Future => LangItem::Future,
        SolverTraitLangItem::Iterator => LangItem::Iterator,
        SolverTraitLangItem::MetaSized => LangItem::MetaSized,
        SolverTraitLangItem::PointeeSized => LangItem::PointeeSized,
        SolverTraitLangItem::PointeeTrait => LangItem::PointeeTrait,
        SolverTraitLangItem::Sized => LangItem::Sized,
        SolverTraitLangItem::TransmuteTrait => LangItem::TransmuteTrait,
        SolverTraitLangItem::TrivialClone => LangItem::TrivialClone,
        SolverTraitLangItem::TryAsDyn => LangItem::TryAsDyn,
        SolverTraitLangItem::Tuple => LangItem::Tuple,
        SolverTraitLangItem::Unpin => LangItem::Unpin,
        SolverTraitLangItem::Unsize => LangItem::Unsize,
    }
}
fn lang_item_to_solver_trait_lang_item(lang_item: LangItem)
    -> Option<SolverTraitLangItem> {
    Some(match lang_item {
            LangItem::AsyncFn => SolverTraitLangItem::AsyncFn,
            LangItem::AsyncFnKindHelper =>
                SolverTraitLangItem::AsyncFnKindHelper,
            LangItem::AsyncFnMut => SolverTraitLangItem::AsyncFnMut,
            LangItem::AsyncFnOnce => SolverTraitLangItem::AsyncFnOnce,
            LangItem::AsyncIterator => SolverTraitLangItem::AsyncIterator,
            LangItem::BikeshedGuaranteedNoDrop =>
                SolverTraitLangItem::BikeshedGuaranteedNoDrop,
            LangItem::Clone => SolverTraitLangItem::Clone,
            LangItem::Copy => SolverTraitLangItem::Copy,
            LangItem::Coroutine => SolverTraitLangItem::Coroutine,
            LangItem::Destruct => SolverTraitLangItem::Destruct,
            LangItem::DiscriminantKind =>
                SolverTraitLangItem::DiscriminantKind,
            LangItem::Drop => SolverTraitLangItem::Drop,
            LangItem::Field => SolverTraitLangItem::Field,
            LangItem::Fn => SolverTraitLangItem::Fn,
            LangItem::FnMut => SolverTraitLangItem::FnMut,
            LangItem::FnOnce => SolverTraitLangItem::FnOnce,
            LangItem::FnPtrTrait => SolverTraitLangItem::FnPtrTrait,
            LangItem::Freeze => SolverTraitLangItem::Freeze,
            LangItem::FusedIterator => SolverTraitLangItem::FusedIterator,
            LangItem::Future => SolverTraitLangItem::Future,
            LangItem::Iterator => SolverTraitLangItem::Iterator,
            LangItem::MetaSized => SolverTraitLangItem::MetaSized,
            LangItem::PointeeSized => SolverTraitLangItem::PointeeSized,
            LangItem::PointeeTrait => SolverTraitLangItem::PointeeTrait,
            LangItem::Sized => SolverTraitLangItem::Sized,
            LangItem::TransmuteTrait => SolverTraitLangItem::TransmuteTrait,
            LangItem::TrivialClone => SolverTraitLangItem::TrivialClone,
            LangItem::TryAsDyn => SolverTraitLangItem::TryAsDyn,
            LangItem::Tuple => SolverTraitLangItem::Tuple,
            LangItem::Unpin => SolverTraitLangItem::Unpin,
            LangItem::Unsize => SolverTraitLangItem::Unsize,
            _ => return None,
        })
}bidirectional_lang_item_map! {
790    SolverTraitLangItem, fn lang_item_to_solver_trait_lang_item, fn solver_trait_lang_item_to_lang_item;
791
792// tidy-alphabetical-start
793    AsyncFn,
794    AsyncFnKindHelper,
795    AsyncFnMut,
796    AsyncFnOnce,
797    AsyncIterator,
798    BikeshedGuaranteedNoDrop,
799    Clone,
800    Copy,
801    Coroutine,
802    Destruct,
803    DiscriminantKind,
804    Drop,
805    Field,
806    Fn,
807    FnMut,
808    FnOnce,
809    FnPtrTrait,
810    Freeze,
811    FusedIterator,
812    Future,
813    Iterator,
814    MetaSized,
815    PointeeSized,
816    PointeeTrait,
817    Sized,
818    TransmuteTrait,
819    TrivialClone,
820    TryAsDyn,
821    Tuple,
822    Unpin,
823    Unsize,
824// tidy-alphabetical-end
825}