rustc_public/unstable/convert/stable/
ty.rs

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