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