rustc_smir/rustc_smir/convert/
ty.rs

1//! Conversion of internal Rust compiler `ty` items to stable ones.
2
3use rustc_middle::ty::Ty;
4use rustc_middle::{mir, ty};
5use stable_mir::ty::{
6    AdtKind, FloatTy, GenericArgs, GenericParamDef, IntTy, Region, RigidTy, TyKind, UintTy,
7};
8
9use crate::rustc_smir::{Stable, Tables, alloc};
10
11impl<'tcx> Stable<'tcx> for ty::AliasTyKind {
12    type T = stable_mir::ty::AliasKind;
13    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
14        match self {
15            ty::Projection => stable_mir::ty::AliasKind::Projection,
16            ty::Inherent => stable_mir::ty::AliasKind::Inherent,
17            ty::Opaque => stable_mir::ty::AliasKind::Opaque,
18            ty::Weak => stable_mir::ty::AliasKind::Weak,
19        }
20    }
21}
22
23impl<'tcx> Stable<'tcx> for ty::AliasTy<'tcx> {
24    type T = stable_mir::ty::AliasTy;
25    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
26        let ty::AliasTy { args, def_id, .. } = self;
27        stable_mir::ty::AliasTy { def_id: tables.alias_def(*def_id), args: args.stable(tables) }
28    }
29}
30
31impl<'tcx> Stable<'tcx> for ty::AliasTerm<'tcx> {
32    type T = stable_mir::ty::AliasTerm;
33    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
34        let ty::AliasTerm { args, def_id, .. } = self;
35        stable_mir::ty::AliasTerm { def_id: tables.alias_def(*def_id), args: args.stable(tables) }
36    }
37}
38
39impl<'tcx> Stable<'tcx> for ty::DynKind {
40    type T = stable_mir::ty::DynKind;
41
42    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
43        match self {
44            ty::Dyn => stable_mir::ty::DynKind::Dyn,
45            ty::DynStar => stable_mir::ty::DynKind::DynStar,
46        }
47    }
48}
49
50impl<'tcx> Stable<'tcx> for ty::ExistentialPredicate<'tcx> {
51    type T = stable_mir::ty::ExistentialPredicate;
52
53    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
54        use stable_mir::ty::ExistentialPredicate::*;
55        match self {
56            ty::ExistentialPredicate::Trait(existential_trait_ref) => {
57                Trait(existential_trait_ref.stable(tables))
58            }
59            ty::ExistentialPredicate::Projection(existential_projection) => {
60                Projection(existential_projection.stable(tables))
61            }
62            ty::ExistentialPredicate::AutoTrait(def_id) => AutoTrait(tables.trait_def(*def_id)),
63        }
64    }
65}
66
67impl<'tcx> Stable<'tcx> for ty::ExistentialTraitRef<'tcx> {
68    type T = stable_mir::ty::ExistentialTraitRef;
69
70    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
71        let ty::ExistentialTraitRef { def_id, args, .. } = self;
72        stable_mir::ty::ExistentialTraitRef {
73            def_id: tables.trait_def(*def_id),
74            generic_args: args.stable(tables),
75        }
76    }
77}
78
79impl<'tcx> Stable<'tcx> for ty::TermKind<'tcx> {
80    type T = stable_mir::ty::TermKind;
81
82    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
83        use stable_mir::ty::TermKind;
84        match self {
85            ty::TermKind::Ty(ty) => TermKind::Type(ty.stable(tables)),
86            ty::TermKind::Const(cnst) => {
87                let cnst = cnst.stable(tables);
88                TermKind::Const(cnst)
89            }
90        }
91    }
92}
93
94impl<'tcx> Stable<'tcx> for ty::ExistentialProjection<'tcx> {
95    type T = stable_mir::ty::ExistentialProjection;
96
97    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
98        let ty::ExistentialProjection { def_id, args, term, .. } = self;
99        stable_mir::ty::ExistentialProjection {
100            def_id: tables.trait_def(*def_id),
101            generic_args: args.stable(tables),
102            term: term.unpack().stable(tables),
103        }
104    }
105}
106
107impl<'tcx> Stable<'tcx> for ty::adjustment::PointerCoercion {
108    type T = stable_mir::mir::PointerCoercion;
109    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
110        use rustc_middle::ty::adjustment::PointerCoercion;
111        match self {
112            PointerCoercion::ReifyFnPointer => stable_mir::mir::PointerCoercion::ReifyFnPointer,
113            PointerCoercion::UnsafeFnPointer => stable_mir::mir::PointerCoercion::UnsafeFnPointer,
114            PointerCoercion::ClosureFnPointer(safety) => {
115                stable_mir::mir::PointerCoercion::ClosureFnPointer(safety.stable(tables))
116            }
117            PointerCoercion::MutToConstPointer => {
118                stable_mir::mir::PointerCoercion::MutToConstPointer
119            }
120            PointerCoercion::ArrayToPointer => stable_mir::mir::PointerCoercion::ArrayToPointer,
121            PointerCoercion::Unsize => stable_mir::mir::PointerCoercion::Unsize,
122            PointerCoercion::DynStar => unreachable!("represented as `CastKind::DynStar` in smir"),
123        }
124    }
125}
126
127impl<'tcx> Stable<'tcx> for ty::UserTypeAnnotationIndex {
128    type T = usize;
129    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
130        self.as_usize()
131    }
132}
133
134impl<'tcx> Stable<'tcx> for ty::AdtKind {
135    type T = AdtKind;
136
137    fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
138        match self {
139            ty::AdtKind::Struct => AdtKind::Struct,
140            ty::AdtKind::Union => AdtKind::Union,
141            ty::AdtKind::Enum => AdtKind::Enum,
142        }
143    }
144}
145
146impl<'tcx> Stable<'tcx> for ty::FieldDef {
147    type T = stable_mir::ty::FieldDef;
148
149    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
150        stable_mir::ty::FieldDef {
151            def: tables.create_def_id(self.did),
152            name: self.name.stable(tables),
153        }
154    }
155}
156
157impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {
158    type T = stable_mir::ty::GenericArgs;
159    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
160        GenericArgs(self.iter().map(|arg| arg.unpack().stable(tables)).collect())
161    }
162}
163
164impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> {
165    type T = stable_mir::ty::GenericArgKind;
166
167    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
168        use stable_mir::ty::GenericArgKind;
169        match self {
170            ty::GenericArgKind::Lifetime(region) => GenericArgKind::Lifetime(region.stable(tables)),
171            ty::GenericArgKind::Type(ty) => GenericArgKind::Type(ty.stable(tables)),
172            ty::GenericArgKind::Const(cnst) => GenericArgKind::Const(cnst.stable(tables)),
173        }
174    }
175}
176
177impl<'tcx, S, V> Stable<'tcx> for ty::Binder<'tcx, S>
178where
179    S: Stable<'tcx, T = V>,
180{
181    type T = stable_mir::ty::Binder<V>;
182
183    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
184        use stable_mir::ty::Binder;
185
186        Binder {
187            value: self.as_ref().skip_binder().stable(tables),
188            bound_vars: self
189                .bound_vars()
190                .iter()
191                .map(|bound_var| bound_var.stable(tables))
192                .collect(),
193        }
194    }
195}
196
197impl<'tcx, S, V> Stable<'tcx> for ty::EarlyBinder<'tcx, S>
198where
199    S: Stable<'tcx, T = V>,
200{
201    type T = stable_mir::ty::EarlyBinder<V>;
202
203    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
204        use stable_mir::ty::EarlyBinder;
205
206        EarlyBinder { value: self.as_ref().skip_binder().stable(tables) }
207    }
208}
209
210impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
211    type T = stable_mir::ty::FnSig;
212    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
213        use stable_mir::ty::FnSig;
214
215        FnSig {
216            inputs_and_output: self.inputs_and_output.iter().map(|ty| ty.stable(tables)).collect(),
217            c_variadic: self.c_variadic,
218            safety: self.safety.stable(tables),
219            abi: self.abi.stable(tables),
220        }
221    }
222}
223
224impl<'tcx> Stable<'tcx> for ty::BoundTyKind {
225    type T = stable_mir::ty::BoundTyKind;
226
227    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
228        use stable_mir::ty::BoundTyKind;
229
230        match self {
231            ty::BoundTyKind::Anon => BoundTyKind::Anon,
232            ty::BoundTyKind::Param(def_id, symbol) => {
233                BoundTyKind::Param(tables.param_def(*def_id), symbol.to_string())
234            }
235        }
236    }
237}
238
239impl<'tcx> Stable<'tcx> for ty::BoundRegionKind {
240    type T = stable_mir::ty::BoundRegionKind;
241
242    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
243        use stable_mir::ty::BoundRegionKind;
244
245        match self {
246            ty::BoundRegionKind::Anon => BoundRegionKind::BrAnon,
247            ty::BoundRegionKind::Named(def_id, symbol) => {
248                BoundRegionKind::BrNamed(tables.br_named_def(*def_id), symbol.to_string())
249            }
250            ty::BoundRegionKind::ClosureEnv => BoundRegionKind::BrEnv,
251        }
252    }
253}
254
255impl<'tcx> Stable<'tcx> for ty::BoundVariableKind {
256    type T = stable_mir::ty::BoundVariableKind;
257
258    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
259        use stable_mir::ty::BoundVariableKind;
260
261        match self {
262            ty::BoundVariableKind::Ty(bound_ty_kind) => {
263                BoundVariableKind::Ty(bound_ty_kind.stable(tables))
264            }
265            ty::BoundVariableKind::Region(bound_region_kind) => {
266                BoundVariableKind::Region(bound_region_kind.stable(tables))
267            }
268            ty::BoundVariableKind::Const => BoundVariableKind::Const,
269        }
270    }
271}
272
273impl<'tcx> Stable<'tcx> for ty::IntTy {
274    type T = IntTy;
275
276    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
277        match self {
278            ty::IntTy::Isize => IntTy::Isize,
279            ty::IntTy::I8 => IntTy::I8,
280            ty::IntTy::I16 => IntTy::I16,
281            ty::IntTy::I32 => IntTy::I32,
282            ty::IntTy::I64 => IntTy::I64,
283            ty::IntTy::I128 => IntTy::I128,
284        }
285    }
286}
287
288impl<'tcx> Stable<'tcx> for ty::UintTy {
289    type T = UintTy;
290
291    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
292        match self {
293            ty::UintTy::Usize => UintTy::Usize,
294            ty::UintTy::U8 => UintTy::U8,
295            ty::UintTy::U16 => UintTy::U16,
296            ty::UintTy::U32 => UintTy::U32,
297            ty::UintTy::U64 => UintTy::U64,
298            ty::UintTy::U128 => UintTy::U128,
299        }
300    }
301}
302
303impl<'tcx> Stable<'tcx> for ty::FloatTy {
304    type T = FloatTy;
305
306    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
307        match self {
308            ty::FloatTy::F16 => FloatTy::F16,
309            ty::FloatTy::F32 => FloatTy::F32,
310            ty::FloatTy::F64 => FloatTy::F64,
311            ty::FloatTy::F128 => FloatTy::F128,
312        }
313    }
314}
315
316impl<'tcx> Stable<'tcx> for Ty<'tcx> {
317    type T = stable_mir::ty::Ty;
318    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
319        tables.intern_ty(tables.tcx.lift(*self).unwrap())
320    }
321}
322
323impl<'tcx> Stable<'tcx> for ty::TyKind<'tcx> {
324    type T = stable_mir::ty::TyKind;
325    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
326        match self {
327            ty::Bool => TyKind::RigidTy(RigidTy::Bool),
328            ty::Char => TyKind::RigidTy(RigidTy::Char),
329            ty::Int(int_ty) => TyKind::RigidTy(RigidTy::Int(int_ty.stable(tables))),
330            ty::Uint(uint_ty) => TyKind::RigidTy(RigidTy::Uint(uint_ty.stable(tables))),
331            ty::Float(float_ty) => TyKind::RigidTy(RigidTy::Float(float_ty.stable(tables))),
332            ty::Adt(adt_def, generic_args) => TyKind::RigidTy(RigidTy::Adt(
333                tables.adt_def(adt_def.did()),
334                generic_args.stable(tables),
335            )),
336            ty::Foreign(def_id) => TyKind::RigidTy(RigidTy::Foreign(tables.foreign_def(*def_id))),
337            ty::Str => TyKind::RigidTy(RigidTy::Str),
338            ty::Array(ty, constant) => {
339                TyKind::RigidTy(RigidTy::Array(ty.stable(tables), constant.stable(tables)))
340            }
341            ty::Pat(ty, pat) => {
342                TyKind::RigidTy(RigidTy::Pat(ty.stable(tables), pat.stable(tables)))
343            }
344            ty::Slice(ty) => TyKind::RigidTy(RigidTy::Slice(ty.stable(tables))),
345            ty::RawPtr(ty, mutbl) => {
346                TyKind::RigidTy(RigidTy::RawPtr(ty.stable(tables), mutbl.stable(tables)))
347            }
348            ty::Ref(region, ty, mutbl) => TyKind::RigidTy(RigidTy::Ref(
349                region.stable(tables),
350                ty.stable(tables),
351                mutbl.stable(tables),
352            )),
353            ty::FnDef(def_id, generic_args) => {
354                TyKind::RigidTy(RigidTy::FnDef(tables.fn_def(*def_id), generic_args.stable(tables)))
355            }
356            ty::FnPtr(sig_tys, hdr) => {
357                TyKind::RigidTy(RigidTy::FnPtr(sig_tys.with(*hdr).stable(tables)))
358            }
359            // FIXME(unsafe_binders):
360            ty::UnsafeBinder(_) => todo!(),
361            ty::Dynamic(existential_predicates, region, dyn_kind) => {
362                TyKind::RigidTy(RigidTy::Dynamic(
363                    existential_predicates
364                        .iter()
365                        .map(|existential_predicate| existential_predicate.stable(tables))
366                        .collect(),
367                    region.stable(tables),
368                    dyn_kind.stable(tables),
369                ))
370            }
371            ty::Closure(def_id, generic_args) => TyKind::RigidTy(RigidTy::Closure(
372                tables.closure_def(*def_id),
373                generic_args.stable(tables),
374            )),
375            ty::CoroutineClosure(..) => todo!("FIXME(async_closures): Lower these to SMIR"),
376            ty::Coroutine(def_id, generic_args) => TyKind::RigidTy(RigidTy::Coroutine(
377                tables.coroutine_def(*def_id),
378                generic_args.stable(tables),
379                tables.tcx.coroutine_movability(*def_id).stable(tables),
380            )),
381            ty::Never => TyKind::RigidTy(RigidTy::Never),
382            ty::Tuple(fields) => {
383                TyKind::RigidTy(RigidTy::Tuple(fields.iter().map(|ty| ty.stable(tables)).collect()))
384            }
385            ty::Alias(alias_kind, alias_ty) => {
386                TyKind::Alias(alias_kind.stable(tables), alias_ty.stable(tables))
387            }
388            ty::Param(param_ty) => TyKind::Param(param_ty.stable(tables)),
389            ty::Bound(debruijn_idx, bound_ty) => {
390                TyKind::Bound(debruijn_idx.as_usize(), bound_ty.stable(tables))
391            }
392            ty::CoroutineWitness(def_id, args) => TyKind::RigidTy(RigidTy::CoroutineWitness(
393                tables.coroutine_witness_def(*def_id),
394                args.stable(tables),
395            )),
396            ty::Placeholder(..) | ty::Infer(_) | ty::Error(_) => {
397                unreachable!();
398            }
399        }
400    }
401}
402
403impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
404    type T = stable_mir::ty::Pattern;
405
406    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
407        match **self {
408            ty::PatternKind::Range { start, end, include_end } => stable_mir::ty::Pattern::Range {
409                start: start.stable(tables),
410                end: end.stable(tables),
411                include_end,
412            },
413        }
414    }
415}
416
417impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
418    type T = stable_mir::ty::TyConst;
419
420    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
421        let ct = tables.tcx.lift(*self).unwrap();
422        let kind = match ct.kind() {
423            ty::ConstKind::Value(cv) => {
424                let const_val = tables.tcx.valtree_to_const_val(cv);
425                if matches!(const_val, mir::ConstValue::ZeroSized) {
426                    stable_mir::ty::TyConstKind::ZSTValue(cv.ty.stable(tables))
427                } else {
428                    stable_mir::ty::TyConstKind::Value(
429                        cv.ty.stable(tables),
430                        alloc::new_allocation(cv.ty, const_val, tables),
431                    )
432                }
433            }
434            ty::ConstKind::Param(param) => stable_mir::ty::TyConstKind::Param(param.stable(tables)),
435            ty::ConstKind::Unevaluated(uv) => stable_mir::ty::TyConstKind::Unevaluated(
436                tables.const_def(uv.def),
437                uv.args.stable(tables),
438            ),
439            ty::ConstKind::Error(_) => unreachable!(),
440            ty::ConstKind::Infer(_) => unreachable!(),
441            ty::ConstKind::Bound(_, _) => unimplemented!(),
442            ty::ConstKind::Placeholder(_) => unimplemented!(),
443            ty::ConstKind::Expr(_) => unimplemented!(),
444        };
445        let id = tables.intern_ty_const(ct);
446        stable_mir::ty::TyConst::new(kind, id)
447    }
448}
449
450impl<'tcx> Stable<'tcx> for ty::ParamConst {
451    type T = stable_mir::ty::ParamConst;
452    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
453        use stable_mir::ty::ParamConst;
454        ParamConst { index: self.index, name: self.name.to_string() }
455    }
456}
457
458impl<'tcx> Stable<'tcx> for ty::ParamTy {
459    type T = stable_mir::ty::ParamTy;
460    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
461        use stable_mir::ty::ParamTy;
462        ParamTy { index: self.index, name: self.name.to_string() }
463    }
464}
465
466impl<'tcx> Stable<'tcx> for ty::BoundTy {
467    type T = stable_mir::ty::BoundTy;
468    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
469        use stable_mir::ty::BoundTy;
470        BoundTy { var: self.var.as_usize(), kind: self.kind.stable(tables) }
471    }
472}
473
474impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {
475    type T = stable_mir::ty::TraitSpecializationKind;
476    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
477        use stable_mir::ty::TraitSpecializationKind;
478
479        match self {
480            ty::trait_def::TraitSpecializationKind::None => TraitSpecializationKind::None,
481            ty::trait_def::TraitSpecializationKind::Marker => TraitSpecializationKind::Marker,
482            ty::trait_def::TraitSpecializationKind::AlwaysApplicable => {
483                TraitSpecializationKind::AlwaysApplicable
484            }
485        }
486    }
487}
488
489impl<'tcx> Stable<'tcx> for ty::TraitDef {
490    type T = stable_mir::ty::TraitDecl;
491    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
492        use stable_mir::opaque;
493        use stable_mir::ty::TraitDecl;
494
495        TraitDecl {
496            def_id: tables.trait_def(self.def_id),
497            safety: self.safety.stable(tables),
498            paren_sugar: self.paren_sugar,
499            has_auto_impl: self.has_auto_impl,
500            is_marker: self.is_marker,
501            is_coinductive: self.is_coinductive,
502            skip_array_during_method_dispatch: self.skip_array_during_method_dispatch,
503            skip_boxed_slice_during_method_dispatch: self.skip_boxed_slice_during_method_dispatch,
504            specialization_kind: self.specialization_kind.stable(tables),
505            must_implement_one_of: self
506                .must_implement_one_of
507                .as_ref()
508                .map(|idents| idents.iter().map(|ident| opaque(ident)).collect()),
509            implement_via_object: self.implement_via_object,
510            deny_explicit_impl: self.deny_explicit_impl,
511        }
512    }
513}
514
515impl<'tcx> Stable<'tcx> for ty::TraitRef<'tcx> {
516    type T = stable_mir::ty::TraitRef;
517    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
518        use stable_mir::ty::TraitRef;
519
520        TraitRef::try_new(tables.trait_def(self.def_id), self.args.stable(tables)).unwrap()
521    }
522}
523
524impl<'tcx> Stable<'tcx> for ty::Generics {
525    type T = stable_mir::ty::Generics;
526
527    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
528        use stable_mir::ty::Generics;
529
530        let params: Vec<_> = self.own_params.iter().map(|param| param.stable(tables)).collect();
531        let param_def_id_to_index =
532            params.iter().map(|param| (param.def_id, param.index)).collect();
533
534        Generics {
535            parent: self.parent.map(|did| tables.generic_def(did)),
536            parent_count: self.parent_count,
537            params,
538            param_def_id_to_index,
539            has_self: self.has_self,
540            has_late_bound_regions: self
541                .has_late_bound_regions
542                .as_ref()
543                .map(|late_bound_regions| late_bound_regions.stable(tables)),
544        }
545    }
546}
547
548impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {
549    type T = stable_mir::ty::GenericParamDefKind;
550
551    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
552        use stable_mir::ty::GenericParamDefKind;
553        match self {
554            ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
555            ty::GenericParamDefKind::Type { has_default, synthetic } => {
556                GenericParamDefKind::Type { has_default: *has_default, synthetic: *synthetic }
557            }
558            ty::GenericParamDefKind::Const { has_default, synthetic: _ } => {
559                GenericParamDefKind::Const { has_default: *has_default }
560            }
561        }
562    }
563}
564
565impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDef {
566    type T = stable_mir::ty::GenericParamDef;
567
568    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
569        GenericParamDef {
570            name: self.name.to_string(),
571            def_id: tables.generic_def(self.def_id),
572            index: self.index,
573            pure_wrt_drop: self.pure_wrt_drop,
574            kind: self.kind.stable(tables),
575        }
576    }
577}
578
579impl<'tcx> Stable<'tcx> for ty::PredicateKind<'tcx> {
580    type T = stable_mir::ty::PredicateKind;
581
582    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
583        use rustc_middle::ty::PredicateKind;
584        match self {
585            PredicateKind::Clause(clause_kind) => {
586                stable_mir::ty::PredicateKind::Clause(clause_kind.stable(tables))
587            }
588            PredicateKind::DynCompatible(did) => {
589                stable_mir::ty::PredicateKind::DynCompatible(tables.trait_def(*did))
590            }
591            PredicateKind::Subtype(subtype_predicate) => {
592                stable_mir::ty::PredicateKind::SubType(subtype_predicate.stable(tables))
593            }
594            PredicateKind::Coerce(coerce_predicate) => {
595                stable_mir::ty::PredicateKind::Coerce(coerce_predicate.stable(tables))
596            }
597            PredicateKind::ConstEquate(a, b) => {
598                stable_mir::ty::PredicateKind::ConstEquate(a.stable(tables), b.stable(tables))
599            }
600            PredicateKind::Ambiguous => stable_mir::ty::PredicateKind::Ambiguous,
601            PredicateKind::NormalizesTo(_pred) => unimplemented!(),
602            PredicateKind::AliasRelate(a, b, alias_relation_direction) => {
603                stable_mir::ty::PredicateKind::AliasRelate(
604                    a.unpack().stable(tables),
605                    b.unpack().stable(tables),
606                    alias_relation_direction.stable(tables),
607                )
608            }
609        }
610    }
611}
612
613impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
614    type T = stable_mir::ty::ClauseKind;
615
616    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
617        use rustc_middle::ty::ClauseKind;
618        match *self {
619            ClauseKind::Trait(trait_object) => {
620                stable_mir::ty::ClauseKind::Trait(trait_object.stable(tables))
621            }
622            ClauseKind::RegionOutlives(region_outlives) => {
623                stable_mir::ty::ClauseKind::RegionOutlives(region_outlives.stable(tables))
624            }
625            ClauseKind::TypeOutlives(type_outlives) => {
626                let ty::OutlivesPredicate::<_, _>(a, b) = type_outlives;
627                stable_mir::ty::ClauseKind::TypeOutlives(stable_mir::ty::OutlivesPredicate(
628                    a.stable(tables),
629                    b.stable(tables),
630                ))
631            }
632            ClauseKind::Projection(projection_predicate) => {
633                stable_mir::ty::ClauseKind::Projection(projection_predicate.stable(tables))
634            }
635            ClauseKind::ConstArgHasType(const_, ty) => stable_mir::ty::ClauseKind::ConstArgHasType(
636                const_.stable(tables),
637                ty.stable(tables),
638            ),
639            ClauseKind::WellFormed(generic_arg) => {
640                stable_mir::ty::ClauseKind::WellFormed(generic_arg.unpack().stable(tables))
641            }
642            ClauseKind::ConstEvaluatable(const_) => {
643                stable_mir::ty::ClauseKind::ConstEvaluatable(const_.stable(tables))
644            }
645            ClauseKind::HostEffect(..) => {
646                todo!()
647            }
648        }
649    }
650}
651
652impl<'tcx> Stable<'tcx> for ty::ClosureKind {
653    type T = stable_mir::ty::ClosureKind;
654
655    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
656        use rustc_middle::ty::ClosureKind::*;
657        match self {
658            Fn => stable_mir::ty::ClosureKind::Fn,
659            FnMut => stable_mir::ty::ClosureKind::FnMut,
660            FnOnce => stable_mir::ty::ClosureKind::FnOnce,
661        }
662    }
663}
664
665impl<'tcx> Stable<'tcx> for ty::SubtypePredicate<'tcx> {
666    type T = stable_mir::ty::SubtypePredicate;
667
668    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
669        let ty::SubtypePredicate { a, b, a_is_expected: _ } = self;
670        stable_mir::ty::SubtypePredicate { a: a.stable(tables), b: b.stable(tables) }
671    }
672}
673
674impl<'tcx> Stable<'tcx> for ty::CoercePredicate<'tcx> {
675    type T = stable_mir::ty::CoercePredicate;
676
677    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
678        let ty::CoercePredicate { a, b } = self;
679        stable_mir::ty::CoercePredicate { a: a.stable(tables), b: b.stable(tables) }
680    }
681}
682
683impl<'tcx> Stable<'tcx> for ty::AliasRelationDirection {
684    type T = stable_mir::ty::AliasRelationDirection;
685
686    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
687        use rustc_middle::ty::AliasRelationDirection::*;
688        match self {
689            Equate => stable_mir::ty::AliasRelationDirection::Equate,
690            Subtype => stable_mir::ty::AliasRelationDirection::Subtype,
691        }
692    }
693}
694
695impl<'tcx> Stable<'tcx> for ty::TraitPredicate<'tcx> {
696    type T = stable_mir::ty::TraitPredicate;
697
698    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
699        let ty::TraitPredicate { trait_ref, polarity } = self;
700        stable_mir::ty::TraitPredicate {
701            trait_ref: trait_ref.stable(tables),
702            polarity: polarity.stable(tables),
703        }
704    }
705}
706
707impl<'tcx, T> Stable<'tcx> for ty::OutlivesPredicate<'tcx, T>
708where
709    T: Stable<'tcx>,
710{
711    type T = stable_mir::ty::OutlivesPredicate<T::T, Region>;
712
713    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
714        let ty::OutlivesPredicate(a, b) = self;
715        stable_mir::ty::OutlivesPredicate(a.stable(tables), b.stable(tables))
716    }
717}
718
719impl<'tcx> Stable<'tcx> for ty::ProjectionPredicate<'tcx> {
720    type T = stable_mir::ty::ProjectionPredicate;
721
722    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
723        let ty::ProjectionPredicate { projection_term, term } = self;
724        stable_mir::ty::ProjectionPredicate {
725            projection_term: projection_term.stable(tables),
726            term: term.unpack().stable(tables),
727        }
728    }
729}
730
731impl<'tcx> Stable<'tcx> for ty::ImplPolarity {
732    type T = stable_mir::ty::ImplPolarity;
733
734    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
735        use rustc_middle::ty::ImplPolarity::*;
736        match self {
737            Positive => stable_mir::ty::ImplPolarity::Positive,
738            Negative => stable_mir::ty::ImplPolarity::Negative,
739            Reservation => stable_mir::ty::ImplPolarity::Reservation,
740        }
741    }
742}
743
744impl<'tcx> Stable<'tcx> for ty::PredicatePolarity {
745    type T = stable_mir::ty::PredicatePolarity;
746
747    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
748        use rustc_middle::ty::PredicatePolarity::*;
749        match self {
750            Positive => stable_mir::ty::PredicatePolarity::Positive,
751            Negative => stable_mir::ty::PredicatePolarity::Negative,
752        }
753    }
754}
755
756impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
757    type T = stable_mir::ty::Region;
758
759    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
760        Region { kind: self.kind().stable(tables) }
761    }
762}
763
764impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> {
765    type T = stable_mir::ty::RegionKind;
766
767    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
768        use stable_mir::ty::{BoundRegion, EarlyParamRegion, RegionKind};
769        match self {
770            ty::ReEarlyParam(early_reg) => RegionKind::ReEarlyParam(EarlyParamRegion {
771                index: early_reg.index,
772                name: early_reg.name.to_string(),
773            }),
774            ty::ReBound(db_index, bound_reg) => RegionKind::ReBound(
775                db_index.as_u32(),
776                BoundRegion { var: bound_reg.var.as_u32(), kind: bound_reg.kind.stable(tables) },
777            ),
778            ty::ReStatic => RegionKind::ReStatic,
779            ty::RePlaceholder(place_holder) => {
780                RegionKind::RePlaceholder(stable_mir::ty::Placeholder {
781                    universe: place_holder.universe.as_u32(),
782                    bound: BoundRegion {
783                        var: place_holder.bound.var.as_u32(),
784                        kind: place_holder.bound.kind.stable(tables),
785                    },
786                })
787            }
788            ty::ReErased => RegionKind::ReErased,
789            _ => unreachable!("{self:?}"),
790        }
791    }
792}
793
794impl<'tcx> Stable<'tcx> for ty::Instance<'tcx> {
795    type T = stable_mir::mir::mono::Instance;
796
797    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
798        let def = tables.instance_def(tables.tcx.lift(*self).unwrap());
799        let kind = match self.def {
800            ty::InstanceKind::Item(..) => stable_mir::mir::mono::InstanceKind::Item,
801            ty::InstanceKind::Intrinsic(..) => stable_mir::mir::mono::InstanceKind::Intrinsic,
802            ty::InstanceKind::Virtual(_def_id, idx) => {
803                stable_mir::mir::mono::InstanceKind::Virtual { idx }
804            }
805            ty::InstanceKind::VTableShim(..)
806            | ty::InstanceKind::ReifyShim(..)
807            | ty::InstanceKind::FnPtrAddrShim(..)
808            | ty::InstanceKind::ClosureOnceShim { .. }
809            | ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
810            | ty::InstanceKind::ThreadLocalShim(..)
811            | ty::InstanceKind::DropGlue(..)
812            | ty::InstanceKind::CloneShim(..)
813            | ty::InstanceKind::FnPtrShim(..)
814            | ty::InstanceKind::AsyncDropGlueCtorShim(..) => {
815                stable_mir::mir::mono::InstanceKind::Shim
816            }
817        };
818        stable_mir::mir::mono::Instance { def, kind }
819    }
820}
821
822impl<'tcx> Stable<'tcx> for ty::Variance {
823    type T = stable_mir::mir::Variance;
824    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
825        match self {
826            ty::Bivariant => stable_mir::mir::Variance::Bivariant,
827            ty::Contravariant => stable_mir::mir::Variance::Contravariant,
828            ty::Covariant => stable_mir::mir::Variance::Covariant,
829            ty::Invariant => stable_mir::mir::Variance::Invariant,
830        }
831    }
832}
833
834impl<'tcx> Stable<'tcx> for ty::Movability {
835    type T = stable_mir::ty::Movability;
836
837    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
838        match self {
839            ty::Movability::Static => stable_mir::ty::Movability::Static,
840            ty::Movability::Movable => stable_mir::ty::Movability::Movable,
841        }
842    }
843}
844
845impl<'tcx> Stable<'tcx> for rustc_abi::ExternAbi {
846    type T = stable_mir::ty::Abi;
847
848    fn stable(&self, _: &mut Tables<'_>) -> Self::T {
849        use rustc_abi::ExternAbi;
850        use stable_mir::ty::Abi;
851        match *self {
852            ExternAbi::Rust => Abi::Rust,
853            ExternAbi::C { unwind } => Abi::C { unwind },
854            ExternAbi::Cdecl { unwind } => Abi::Cdecl { unwind },
855            ExternAbi::Stdcall { unwind } => Abi::Stdcall { unwind },
856            ExternAbi::Fastcall { unwind } => Abi::Fastcall { unwind },
857            ExternAbi::Vectorcall { unwind } => Abi::Vectorcall { unwind },
858            ExternAbi::Thiscall { unwind } => Abi::Thiscall { unwind },
859            ExternAbi::Aapcs { unwind } => Abi::Aapcs { unwind },
860            ExternAbi::Win64 { unwind } => Abi::Win64 { unwind },
861            ExternAbi::SysV64 { unwind } => Abi::SysV64 { unwind },
862            ExternAbi::PtxKernel => Abi::PtxKernel,
863            ExternAbi::GpuKernel => Abi::GpuKernel,
864            ExternAbi::Msp430Interrupt => Abi::Msp430Interrupt,
865            ExternAbi::X86Interrupt => Abi::X86Interrupt,
866            ExternAbi::EfiApi => Abi::EfiApi,
867            ExternAbi::AvrInterrupt => Abi::AvrInterrupt,
868            ExternAbi::AvrNonBlockingInterrupt => Abi::AvrNonBlockingInterrupt,
869            ExternAbi::CCmseNonSecureCall => Abi::CCmseNonSecureCall,
870            ExternAbi::CCmseNonSecureEntry => Abi::CCmseNonSecureEntry,
871            ExternAbi::System { unwind } => Abi::System { unwind },
872            ExternAbi::RustIntrinsic => Abi::RustIntrinsic,
873            ExternAbi::RustCall => Abi::RustCall,
874            ExternAbi::Unadjusted => Abi::Unadjusted,
875            ExternAbi::RustCold => Abi::RustCold,
876            ExternAbi::RiscvInterruptM => Abi::RiscvInterruptM,
877            ExternAbi::RiscvInterruptS => Abi::RiscvInterruptS,
878        }
879    }
880}
881
882impl<'tcx> Stable<'tcx> for rustc_session::cstore::ForeignModule {
883    type T = stable_mir::ty::ForeignModule;
884
885    fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
886        stable_mir::ty::ForeignModule {
887            def_id: tables.foreign_module_def(self.def_id),
888            abi: self.abi.stable(tables),
889        }
890    }
891}