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