Skip to main content

rustc_middle/ty/context/
impl_interner.rs

1//! Implementation of [`rustc_type_ir::Interner`] for [`TyCtxt`].
2
3use std::fmt;
4
5use rustc_abi::ExternAbi;
6use rustc_data_structures::debug_assert_matches;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir::def::{CtorKind, CtorOf, DefKind};
9use rustc_hir::def_id::{DefId, LocalDefId};
10use rustc_hir::lang_items::LangItem;
11use rustc_hir::{self as hir};
12use rustc_span::{DUMMY_SP, Span, Symbol};
13use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
14use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph};
15
16use crate::dep_graph::DepNodeIndex;
17use crate::infer::canonical::CanonicalVarKinds;
18use crate::query::IntoQueryParam;
19use crate::traits::cache::WithDepNode;
20use crate::traits::solve::{
21    self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
22};
23use crate::ty::{
24    self, Clause, Const, List, ParamTy, Pattern, PolyExistentialPredicate, Predicate, Region, Ty,
25    TyCtxt,
26};
27
28#[allow(rustc::usage_of_ty_tykind)]
29impl<'tcx> Interner for TyCtxt<'tcx> {
30    fn next_trait_solver_globally(self) -> bool {
31        self.next_trait_solver_globally()
32    }
33
34    type DefId = DefId;
35    type LocalDefId = LocalDefId;
36    type TraitId = DefId;
37    type ForeignId = DefId;
38    type FunctionId = DefId;
39    type ClosureId = DefId;
40    type CoroutineClosureId = DefId;
41    type CoroutineId = DefId;
42    type AdtId = DefId;
43    type ImplId = DefId;
44    type UnevaluatedConstId = DefId;
45    type Span = Span;
46
47    type GenericArgs = ty::GenericArgsRef<'tcx>;
48
49    type GenericArgsSlice = &'tcx [ty::GenericArg<'tcx>];
50    type GenericArg = ty::GenericArg<'tcx>;
51    type Term = ty::Term<'tcx>;
52    type BoundVarKinds = &'tcx List<ty::BoundVariableKind<'tcx>>;
53
54    type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
55
56    fn mk_predefined_opaques_in_body(
57        self,
58        data: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)],
59    ) -> Self::PredefinedOpaques {
60        self.mk_predefined_opaques_in_body(data)
61    }
62    type LocalDefIds = &'tcx ty::List<LocalDefId>;
63    type CanonicalVarKinds = CanonicalVarKinds<'tcx>;
64    fn mk_canonical_var_kinds(
65        self,
66        kinds: &[ty::CanonicalVarKind<Self>],
67    ) -> Self::CanonicalVarKinds {
68        self.mk_canonical_var_kinds(kinds)
69    }
70
71    type ExternalConstraints = ExternalConstraints<'tcx>;
72    fn mk_external_constraints(
73        self,
74        data: ExternalConstraintsData<Self>,
75    ) -> ExternalConstraints<'tcx> {
76        self.mk_external_constraints(data)
77    }
78    type DepNodeIndex = DepNodeIndex;
79    fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, DepNodeIndex) {
80        self.dep_graph.with_anon_task(self, crate::dep_graph::dep_kinds::TraitSelect, task)
81    }
82    type Ty = Ty<'tcx>;
83    type Tys = &'tcx List<Ty<'tcx>>;
84
85    type FnInputTys = &'tcx [Ty<'tcx>];
86    type ParamTy = ParamTy;
87    type Symbol = Symbol;
88
89    type ErrorGuaranteed = ErrorGuaranteed;
90    type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
91
92    type AllocId = crate::mir::interpret::AllocId;
93    type Pat = Pattern<'tcx>;
94    type PatList = &'tcx List<Pattern<'tcx>>;
95    type Safety = hir::Safety;
96    type Abi = ExternAbi;
97    type Const = ty::Const<'tcx>;
98
99    type ParamConst = ty::ParamConst;
100    type ValueConst = ty::Value<'tcx>;
101    type ExprConst = ty::Expr<'tcx>;
102    type ValTree = ty::ValTree<'tcx>;
103    type ScalarInt = ty::ScalarInt;
104
105    type Region = Region<'tcx>;
106    type EarlyParamRegion = ty::EarlyParamRegion;
107    type LateParamRegion = ty::LateParamRegion;
108
109    type RegionAssumptions = &'tcx ty::List<ty::ArgOutlivesPredicate<'tcx>>;
110
111    type ParamEnv = ty::ParamEnv<'tcx>;
112    type Predicate = Predicate<'tcx>;
113
114    type Clause = Clause<'tcx>;
115    type Clauses = ty::Clauses<'tcx>;
116
117    type Tracked<T: fmt::Debug + Clone> = WithDepNode<T>;
118    fn mk_tracked<T: fmt::Debug + Clone>(
119        self,
120        data: T,
121        dep_node: DepNodeIndex,
122    ) -> Self::Tracked<T> {
123        WithDepNode::new(dep_node, data)
124    }
125    fn get_tracked<T: fmt::Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T {
126        tracked.get(self)
127    }
128
129    fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
130        f(&mut *self.new_solver_evaluation_cache.lock())
131    }
132
133    fn canonical_param_env_cache_get_or_insert<R>(
134        self,
135        param_env: ty::ParamEnv<'tcx>,
136        f: impl FnOnce() -> ty::CanonicalParamEnvCacheEntry<Self>,
137        from_entry: impl FnOnce(&ty::CanonicalParamEnvCacheEntry<Self>) -> R,
138    ) -> R {
139        let mut cache = self.new_solver_canonical_param_env_cache.lock();
140        let entry = cache.entry(param_env).or_insert_with(f);
141        from_entry(entry)
142    }
143
144    fn assert_evaluation_is_concurrent(&self) {
145        // Turns out, the assumption for this function isn't perfect.
146        // See trait-system-refactor-initiative#234.
147    }
148
149    fn expand_abstract_consts<T: TypeFoldable<TyCtxt<'tcx>>>(self, t: T) -> T {
150        self.expand_abstract_consts(t)
151    }
152
153    type GenericsOf = &'tcx ty::Generics;
154
155    fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
156        self.generics_of(def_id)
157    }
158
159    type VariancesOf = &'tcx [ty::Variance];
160
161    fn variances_of(self, def_id: DefId) -> Self::VariancesOf {
162        self.variances_of(def_id)
163    }
164
165    fn opt_alias_variances(
166        self,
167        kind: impl Into<ty::AliasTermKind>,
168        def_id: DefId,
169    ) -> Option<&'tcx [ty::Variance]> {
170        self.opt_alias_variances(kind, def_id)
171    }
172
173    fn type_of(self, def_id: DefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
174        self.type_of(def_id)
175    }
176    fn type_of_opaque_hir_typeck(self, def_id: LocalDefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
177        self.type_of_opaque_hir_typeck(def_id)
178    }
179    fn const_of_item(self, def_id: DefId) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
180        self.const_of_item(def_id)
181    }
182    fn anon_const_kind(self, def_id: DefId) -> ty::AnonConstKind {
183        self.anon_const_kind(def_id)
184    }
185
186    type AdtDef = ty::AdtDef<'tcx>;
187    fn adt_def(self, adt_def_id: DefId) -> Self::AdtDef {
188        self.adt_def(adt_def_id)
189    }
190
191    fn alias_ty_kind(self, alias: ty::AliasTy<'tcx>) -> ty::AliasTyKind {
192        match self.def_kind(alias.def_id) {
193            DefKind::AssocTy => {
194                if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
195                {
196                    ty::Inherent
197                } else {
198                    ty::Projection
199                }
200            }
201            DefKind::OpaqueTy => ty::Opaque,
202            DefKind::TyAlias => ty::Free,
203            kind => crate::util::bug::bug_fmt(format_args!("unexpected DefKind in AliasTy: {0:?}",
        kind))bug!("unexpected DefKind in AliasTy: {kind:?}"),
204        }
205    }
206
207    fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind {
208        match self.def_kind(alias.def_id) {
209            DefKind::AssocTy => {
210                if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
211                {
212                    ty::AliasTermKind::InherentTy
213                } else {
214                    ty::AliasTermKind::ProjectionTy
215                }
216            }
217            DefKind::AssocConst => {
218                if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
219                {
220                    ty::AliasTermKind::InherentConst
221                } else {
222                    ty::AliasTermKind::ProjectionConst
223                }
224            }
225            DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
226            DefKind::TyAlias => ty::AliasTermKind::FreeTy,
227            DefKind::Const => ty::AliasTermKind::FreeConst,
228            DefKind::AnonConst | DefKind::Ctor(_, CtorKind::Const) => {
229                ty::AliasTermKind::UnevaluatedConst
230            }
231            kind => crate::util::bug::bug_fmt(format_args!("unexpected DefKind in AliasTy: {0:?}",
        kind))bug!("unexpected DefKind in AliasTy: {kind:?}"),
232        }
233    }
234
235    fn trait_ref_and_own_args_for_alias(
236        self,
237        def_id: DefId,
238        args: ty::GenericArgsRef<'tcx>,
239    ) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
240        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);
241        let trait_def_id = self.parent(def_id);
242        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);
243        let trait_ref = ty::TraitRef::from_assoc(self, trait_def_id, args);
244        (trait_ref, &args[trait_ref.args.len()..])
245    }
246
247    fn mk_args(self, args: &[Self::GenericArg]) -> ty::GenericArgsRef<'tcx> {
248        self.mk_args(args)
249    }
250
251    fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
252    where
253        I: Iterator<Item = T>,
254        T: CollectAndApply<Self::GenericArg, ty::GenericArgsRef<'tcx>>,
255    {
256        self.mk_args_from_iter(args)
257    }
258
259    fn check_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) -> bool {
260        self.check_args_compatible(def_id, args)
261    }
262
263    fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) {
264        self.debug_assert_args_compatible(def_id, 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 parent(self, def_id: DefId) -> DefId {
296        self.parent(def_id)
297    }
298
299    fn recursion_limit(self) -> usize {
300        self.recursion_limit().0
301    }
302
303    type Features = &'tcx rustc_feature::Features;
304
305    fn features(self) -> Self::Features {
306        self.features()
307    }
308
309    fn coroutine_hidden_types(
310        self,
311        def_id: DefId,
312    ) -> ty::EarlyBinder<'tcx, ty::Binder<'tcx, ty::CoroutineWitnessTypes<TyCtxt<'tcx>>>> {
313        self.coroutine_hidden_types(def_id)
314    }
315
316    fn fn_sig(self, def_id: DefId) -> ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>> {
317        self.fn_sig(def_id)
318    }
319
320    fn coroutine_movability(self, def_id: DefId) -> rustc_ast::Movability {
321        self.coroutine_movability(def_id)
322    }
323
324    fn coroutine_for_closure(self, def_id: DefId) -> DefId {
325        self.coroutine_for_closure(def_id)
326    }
327
328    fn generics_require_sized_self(self, def_id: DefId) -> bool {
329        self.generics_require_sized_self(def_id)
330    }
331
332    fn item_bounds(
333        self,
334        def_id: DefId,
335    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
336        self.item_bounds(def_id).map_bound(IntoIterator::into_iter)
337    }
338
339    fn item_self_bounds(
340        self,
341        def_id: DefId,
342    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
343        self.item_self_bounds(def_id).map_bound(IntoIterator::into_iter)
344    }
345
346    fn item_non_self_bounds(
347        self,
348        def_id: DefId,
349    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
350        self.item_non_self_bounds(def_id).map_bound(IntoIterator::into_iter)
351    }
352
353    fn predicates_of(
354        self,
355        def_id: DefId,
356    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
357        ty::EarlyBinder::bind(
358            self.predicates_of(def_id).instantiate_identity(self).predicates.into_iter(),
359        )
360    }
361
362    fn own_predicates_of(
363        self,
364        def_id: DefId,
365    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
366        ty::EarlyBinder::bind(
367            self.predicates_of(def_id).instantiate_own_identity().map(|(clause, _)| clause),
368        )
369    }
370
371    fn explicit_super_predicates_of(
372        self,
373        def_id: DefId,
374    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
375        self.explicit_super_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
376    }
377
378    fn explicit_implied_predicates_of(
379        self,
380        def_id: DefId,
381    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
382        self.explicit_implied_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
383    }
384
385    fn impl_super_outlives(
386        self,
387        impl_def_id: DefId,
388    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
389        self.impl_super_outlives(impl_def_id)
390    }
391
392    fn impl_is_const(self, def_id: DefId) -> bool {
393        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 });
394        self.is_conditionally_const(def_id)
395    }
396
397    fn fn_is_const(self, def_id: DefId) -> bool {
398        if true {
    match self.def_kind(def_id) {
        DefKind::Fn | DefKind::AssocFn |
            DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(CtorOf::Struct, CtorKind::Fn)",
                ::core::option::Option::None);
        }
    };
};debug_assert_matches!(
399            self.def_kind(def_id),
400            DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(CtorOf::Struct, CtorKind::Fn)
401        );
402        self.is_conditionally_const(def_id)
403    }
404
405    fn alias_has_const_conditions(self, def_id: DefId) -> bool {
406        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);
407        self.is_conditionally_const(def_id)
408    }
409
410    fn const_conditions(
411        self,
412        def_id: DefId,
413    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
414        ty::EarlyBinder::bind(
415            self.const_conditions(def_id).instantiate_identity(self).into_iter().map(|(c, _)| c),
416        )
417    }
418
419    fn explicit_implied_const_bounds(
420        self,
421        def_id: DefId,
422    ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
423        ty::EarlyBinder::bind(
424            self.explicit_implied_const_bounds(def_id).iter_identity_copied().map(|(c, _)| c),
425        )
426    }
427
428    fn impl_self_is_guaranteed_unsized(self, impl_def_id: DefId) -> bool {
429        self.impl_self_is_guaranteed_unsized(impl_def_id)
430    }
431
432    fn has_target_features(self, def_id: DefId) -> bool {
433        !self.codegen_fn_attrs(def_id).target_features.is_empty()
434    }
435
436    fn require_lang_item(self, lang_item: SolverLangItem) -> DefId {
437        self.require_lang_item(solver_lang_item_to_lang_item(lang_item), DUMMY_SP)
438    }
439
440    fn require_trait_lang_item(self, lang_item: SolverTraitLangItem) -> DefId {
441        self.require_lang_item(solver_trait_lang_item_to_lang_item(lang_item), DUMMY_SP)
442    }
443
444    fn require_adt_lang_item(self, lang_item: SolverAdtLangItem) -> DefId {
445        self.require_lang_item(solver_adt_lang_item_to_lang_item(lang_item), DUMMY_SP)
446    }
447
448    fn is_lang_item(self, def_id: DefId, lang_item: SolverLangItem) -> bool {
449        self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
450    }
451
452    fn is_trait_lang_item(self, def_id: DefId, lang_item: SolverTraitLangItem) -> bool {
453        self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
454    }
455
456    fn is_adt_lang_item(self, def_id: DefId, lang_item: SolverAdtLangItem) -> bool {
457        self.is_lang_item(def_id, solver_adt_lang_item_to_lang_item(lang_item))
458    }
459
460    fn is_default_trait(self, def_id: DefId) -> bool {
461        self.is_default_trait(def_id)
462    }
463
464    fn is_sizedness_trait(self, def_id: DefId) -> bool {
465        self.is_sizedness_trait(def_id)
466    }
467
468    fn as_lang_item(self, def_id: DefId) -> Option<SolverLangItem> {
469        lang_item_to_solver_lang_item(self.lang_items().from_def_id(def_id)?)
470    }
471
472    fn as_trait_lang_item(self, def_id: DefId) -> Option<SolverTraitLangItem> {
473        lang_item_to_solver_trait_lang_item(self.lang_items().from_def_id(def_id)?)
474    }
475
476    fn as_adt_lang_item(self, def_id: DefId) -> Option<SolverAdtLangItem> {
477        lang_item_to_solver_adt_lang_item(self.lang_items().from_def_id(def_id)?)
478    }
479
480    fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
481        self.associated_items(def_id)
482            .in_definition_order()
483            .filter(|assoc_item| assoc_item.is_type())
484            .map(|assoc_item| assoc_item.def_id)
485    }
486
487    // This implementation is a bit different from `TyCtxt::for_each_relevant_impl`,
488    // since we want to skip over blanket impls for non-rigid aliases, and also we
489    // only want to consider types that *actually* unify with float/int vars.
490    fn for_each_relevant_impl(
491        self,
492        trait_def_id: DefId,
493        self_ty: Ty<'tcx>,
494        mut f: impl FnMut(DefId),
495    ) {
496        let tcx = self;
497        let trait_impls = tcx.trait_impls_of(trait_def_id);
498        let mut consider_impls_for_simplified_type = |simp| {
499            if let Some(impls_for_type) = trait_impls.non_blanket_impls().get(&simp) {
500                for &impl_def_id in impls_for_type {
501                    f(impl_def_id);
502                }
503            }
504        };
505
506        match self_ty.kind() {
507            ty::Bool
508            | ty::Char
509            | ty::Int(_)
510            | ty::Uint(_)
511            | ty::Float(_)
512            | ty::Adt(_, _)
513            | ty::Foreign(_)
514            | ty::Str
515            | ty::Array(_, _)
516            | ty::Pat(_, _)
517            | ty::Slice(_)
518            | ty::RawPtr(_, _)
519            | ty::Ref(_, _, _)
520            | ty::FnDef(_, _)
521            | ty::FnPtr(..)
522            | ty::Dynamic(_, _)
523            | ty::Closure(..)
524            | ty::CoroutineClosure(..)
525            | ty::Coroutine(_, _)
526            | ty::Never
527            | ty::Tuple(_)
528            | ty::UnsafeBinder(_) => {
529                if let Some(simp) = ty::fast_reject::simplify_type(
530                    tcx,
531                    self_ty,
532                    ty::fast_reject::TreatParams::AsRigid,
533                ) {
534                    consider_impls_for_simplified_type(simp);
535                }
536            }
537
538            // HACK: For integer and float variables we have to manually look at all impls
539            // which have some integer or float as a self type.
540            ty::Infer(ty::IntVar(_)) => {
541                use ty::IntTy::*;
542                use ty::UintTy::*;
543                // This causes a compiler error if any new integer kinds are added.
544                let (I8 | I16 | I32 | I64 | I128 | Isize): ty::IntTy;
545                let (U8 | U16 | U32 | U64 | U128 | Usize): ty::UintTy;
546                let possible_integers = [
547                    // signed integers
548                    ty::SimplifiedType::Int(I8),
549                    ty::SimplifiedType::Int(I16),
550                    ty::SimplifiedType::Int(I32),
551                    ty::SimplifiedType::Int(I64),
552                    ty::SimplifiedType::Int(I128),
553                    ty::SimplifiedType::Int(Isize),
554                    // unsigned integers
555                    ty::SimplifiedType::Uint(U8),
556                    ty::SimplifiedType::Uint(U16),
557                    ty::SimplifiedType::Uint(U32),
558                    ty::SimplifiedType::Uint(U64),
559                    ty::SimplifiedType::Uint(U128),
560                    ty::SimplifiedType::Uint(Usize),
561                ];
562                for simp in possible_integers {
563                    consider_impls_for_simplified_type(simp);
564                }
565            }
566
567            ty::Infer(ty::FloatVar(_)) => {
568                // This causes a compiler error if any new float kinds are added.
569                let (ty::FloatTy::F16 | ty::FloatTy::F32 | ty::FloatTy::F64 | ty::FloatTy::F128);
570                let possible_floats = [
571                    ty::SimplifiedType::Float(ty::FloatTy::F16),
572                    ty::SimplifiedType::Float(ty::FloatTy::F32),
573                    ty::SimplifiedType::Float(ty::FloatTy::F64),
574                    ty::SimplifiedType::Float(ty::FloatTy::F128),
575                ];
576
577                for simp in possible_floats {
578                    consider_impls_for_simplified_type(simp);
579                }
580            }
581
582            // The only traits applying to aliases and placeholders are blanket impls.
583            //
584            // Impls which apply to an alias after normalization are handled by
585            // `assemble_candidates_after_normalizing_self_ty`.
586            ty::Alias(_, _) | ty::Placeholder(..) | ty::Error(_) => (),
587
588            // FIXME: These should ideally not exist as a self type. It would be nice for
589            // the builtin auto trait impls of coroutines to instead directly recurse
590            // into the witness.
591            ty::CoroutineWitness(..) => (),
592
593            // These variants should not exist as a self type.
594            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
595            | ty::Param(_)
596            | ty::Bound(_, _) => crate::util::bug::bug_fmt(format_args!("unexpected self type: {0}", self_ty))bug!("unexpected self type: {self_ty}"),
597        }
598
599        #[allow(rustc::usage_of_type_ir_traits)]
600        self.for_each_blanket_impl(trait_def_id, f)
601    }
602    fn for_each_blanket_impl(self, trait_def_id: DefId, mut f: impl FnMut(DefId)) {
603        let trait_impls = self.trait_impls_of(trait_def_id);
604        for &impl_def_id in trait_impls.blanket_impls() {
605            f(impl_def_id);
606        }
607    }
608
609    fn has_item_definition(self, def_id: DefId) -> bool {
610        self.defaultness(def_id).has_value()
611    }
612
613    fn impl_specializes(self, impl_def_id: Self::DefId, victim_def_id: Self::DefId) -> bool {
614        self.specializes((impl_def_id, victim_def_id))
615    }
616
617    fn impl_is_default(self, impl_def_id: DefId) -> bool {
618        self.defaultness(impl_def_id).is_default()
619    }
620
621    fn impl_trait_ref(self, impl_def_id: DefId) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> {
622        self.impl_trait_ref(impl_def_id)
623    }
624
625    fn impl_polarity(self, impl_def_id: DefId) -> ty::ImplPolarity {
626        self.impl_polarity(impl_def_id)
627    }
628
629    fn trait_is_auto(self, trait_def_id: DefId) -> bool {
630        self.trait_is_auto(trait_def_id)
631    }
632
633    fn trait_is_coinductive(self, trait_def_id: DefId) -> bool {
634        self.trait_is_coinductive(trait_def_id)
635    }
636
637    fn trait_is_alias(self, trait_def_id: DefId) -> bool {
638        self.trait_is_alias(trait_def_id)
639    }
640
641    fn trait_is_dyn_compatible(self, trait_def_id: DefId) -> bool {
642        self.is_dyn_compatible(trait_def_id)
643    }
644
645    fn trait_is_fundamental(self, def_id: DefId) -> bool {
646        self.trait_def(def_id).is_fundamental
647    }
648
649    fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool {
650        self.trait_def(trait_def_id).safety.is_unsafe()
651    }
652
653    fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
654        self.is_impl_trait_in_trait(def_id)
655    }
656
657    fn delay_bug(self, msg: impl ToString) -> ErrorGuaranteed {
658        self.dcx().span_delayed_bug(DUMMY_SP, msg.to_string())
659    }
660
661    fn is_general_coroutine(self, coroutine_def_id: DefId) -> bool {
662        self.is_general_coroutine(coroutine_def_id)
663    }
664
665    fn coroutine_is_async(self, coroutine_def_id: DefId) -> bool {
666        self.coroutine_is_async(coroutine_def_id)
667    }
668
669    fn coroutine_is_gen(self, coroutine_def_id: DefId) -> bool {
670        self.coroutine_is_gen(coroutine_def_id)
671    }
672
673    fn coroutine_is_async_gen(self, coroutine_def_id: DefId) -> bool {
674        self.coroutine_is_async_gen(coroutine_def_id)
675    }
676
677    type UnsizingParams = &'tcx rustc_index::bit_set::DenseBitSet<u32>;
678    fn unsizing_params_for_adt(self, adt_def_id: DefId) -> Self::UnsizingParams {
679        self.unsizing_params_for_adt(adt_def_id)
680    }
681
682    fn anonymize_bound_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
683        self,
684        binder: ty::Binder<'tcx, T>,
685    ) -> ty::Binder<'tcx, T> {
686        self.anonymize_bound_vars(binder)
687    }
688
689    fn opaque_types_defined_by(self, defining_anchor: LocalDefId) -> Self::LocalDefIds {
690        self.opaque_types_defined_by(defining_anchor)
691    }
692
693    fn opaque_types_and_coroutines_defined_by(
694        self,
695        defining_anchor: Self::LocalDefId,
696    ) -> Self::LocalDefIds {
697        let coroutines_defined_by = self
698            .nested_bodies_within(defining_anchor)
699            .iter()
700            .filter(|def_id| self.is_coroutine(def_id.to_def_id()));
701        self.mk_local_def_ids_from_iter(
702            self.opaque_types_defined_by(defining_anchor).iter().chain(coroutines_defined_by),
703        )
704    }
705
706    type Probe = &'tcx inspect::Probe<TyCtxt<'tcx>>;
707    fn mk_probe(self, probe: inspect::Probe<Self>) -> &'tcx inspect::Probe<TyCtxt<'tcx>> {
708        self.arena.alloc(probe)
709    }
710    fn evaluate_root_goal_for_proof_tree_raw(
711        self,
712        canonical_goal: CanonicalInput<'tcx>,
713    ) -> (QueryResult<'tcx>, &'tcx inspect::Probe<TyCtxt<'tcx>>) {
714        self.evaluate_root_goal_for_proof_tree_raw(canonical_goal)
715    }
716
717    fn item_name(self, id: DefId) -> Symbol {
718        let id = id.into_query_param();
719        self.opt_item_name(id).unwrap_or_else(|| {
720            crate::util::bug::bug_fmt(format_args!("item_name: no name for {0:?}",
        self.def_path(id)));bug!("item_name: no name for {:?}", self.def_path(id));
721        })
722    }
723}
724
725/// Defines trivial conversion functions between the main [`LangItem`] enum,
726/// and some other lang-item enum that is a subset of it.
727macro_rules! bidirectional_lang_item_map {
728    (
729        $solver_ty:ident, fn $to_solver:ident, fn $from_solver:ident;
730        $($name:ident),+ $(,)?
731    ) => {
732        fn $from_solver(lang_item: $solver_ty) -> LangItem {
733            match lang_item {
734                $($solver_ty::$name => LangItem::$name,)+
735            }
736        }
737
738        fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> {
739            Some(match lang_item {
740                $(LangItem::$name => $solver_ty::$name,)+
741                _ => return None,
742            })
743        }
744    }
745}
746
747fn solver_lang_item_to_lang_item(lang_item: SolverLangItem) -> LangItem {
    match lang_item {
        SolverLangItem::AsyncFnKindUpvars => LangItem::AsyncFnKindUpvars,
        SolverLangItem::AsyncFnOnceOutput => LangItem::AsyncFnOnceOutput,
        SolverLangItem::CallOnceFuture => LangItem::CallOnceFuture,
        SolverLangItem::CallRefFuture => LangItem::CallRefFuture,
        SolverLangItem::CoroutineReturn => LangItem::CoroutineReturn,
        SolverLangItem::CoroutineYield => LangItem::CoroutineYield,
        SolverLangItem::DynMetadata => LangItem::DynMetadata,
        SolverLangItem::FutureOutput => LangItem::FutureOutput,
        SolverLangItem::Metadata => LangItem::Metadata,
    }
}
fn lang_item_to_solver_lang_item(lang_item: LangItem)
    -> Option<SolverLangItem> {
    Some(match lang_item {
            LangItem::AsyncFnKindUpvars => SolverLangItem::AsyncFnKindUpvars,
            LangItem::AsyncFnOnceOutput => SolverLangItem::AsyncFnOnceOutput,
            LangItem::CallOnceFuture => SolverLangItem::CallOnceFuture,
            LangItem::CallRefFuture => SolverLangItem::CallRefFuture,
            LangItem::CoroutineReturn => SolverLangItem::CoroutineReturn,
            LangItem::CoroutineYield => SolverLangItem::CoroutineYield,
            LangItem::DynMetadata => SolverLangItem::DynMetadata,
            LangItem::FutureOutput => SolverLangItem::FutureOutput,
            LangItem::Metadata => SolverLangItem::Metadata,
            _ => return None,
        })
}bidirectional_lang_item_map! {
748    SolverLangItem, fn lang_item_to_solver_lang_item, fn solver_lang_item_to_lang_item;
749
750// tidy-alphabetical-start
751    AsyncFnKindUpvars,
752    AsyncFnOnceOutput,
753    CallOnceFuture,
754    CallRefFuture,
755    CoroutineReturn,
756    CoroutineYield,
757    DynMetadata,
758    FutureOutput,
759    Metadata,
760// tidy-alphabetical-end
761}
762
763fn solver_adt_lang_item_to_lang_item(lang_item: SolverAdtLangItem)
    -> LangItem {
    match lang_item {
        SolverAdtLangItem::Option => LangItem::Option,
        SolverAdtLangItem::Poll => LangItem::Poll,
    }
}
fn lang_item_to_solver_adt_lang_item(lang_item: LangItem)
    -> Option<SolverAdtLangItem> {
    Some(match lang_item {
            LangItem::Option => SolverAdtLangItem::Option,
            LangItem::Poll => SolverAdtLangItem::Poll,
            _ => return None,
        })
}bidirectional_lang_item_map! {
764    SolverAdtLangItem, fn lang_item_to_solver_adt_lang_item, fn solver_adt_lang_item_to_lang_item;
765
766// tidy-alphabetical-start
767    Option,
768    Poll,
769// tidy-alphabetical-end
770}
771
772fn 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::AsyncFnOnceOutput => LangItem::AsyncFnOnceOutput,
        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::Fn => LangItem::Fn,
        SolverTraitLangItem::FnMut => LangItem::FnMut,
        SolverTraitLangItem::FnOnce => LangItem::FnOnce,
        SolverTraitLangItem::FnPtrTrait => LangItem::FnPtrTrait,
        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::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::AsyncFnOnceOutput =>
                SolverTraitLangItem::AsyncFnOnceOutput,
            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::Fn => SolverTraitLangItem::Fn,
            LangItem::FnMut => SolverTraitLangItem::FnMut,
            LangItem::FnOnce => SolverTraitLangItem::FnOnce,
            LangItem::FnPtrTrait => SolverTraitLangItem::FnPtrTrait,
            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::Tuple => SolverTraitLangItem::Tuple,
            LangItem::Unpin => SolverTraitLangItem::Unpin,
            LangItem::Unsize => SolverTraitLangItem::Unsize,
            _ => return None,
        })
}bidirectional_lang_item_map! {
773    SolverTraitLangItem, fn lang_item_to_solver_trait_lang_item, fn solver_trait_lang_item_to_lang_item;
774
775// tidy-alphabetical-start
776    AsyncFn,
777    AsyncFnKindHelper,
778    AsyncFnMut,
779    AsyncFnOnce,
780    AsyncFnOnceOutput,
781    AsyncIterator,
782    BikeshedGuaranteedNoDrop,
783    Clone,
784    Copy,
785    Coroutine,
786    Destruct,
787    DiscriminantKind,
788    Drop,
789    Fn,
790    FnMut,
791    FnOnce,
792    FnPtrTrait,
793    FusedIterator,
794    Future,
795    Iterator,
796    MetaSized,
797    PointeeSized,
798    PointeeTrait,
799    Sized,
800    TransmuteTrait,
801    TrivialClone,
802    Tuple,
803    Unpin,
804    Unsize,
805// tidy-alphabetical-end
806}