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