1use rustc_middle::ty::Ty;
4use rustc_middle::{bug, mir, ty};
5use rustc_public_bridge::Tables;
6use rustc_public_bridge::context::SmirCtxt;
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>, _: &SmirCtxt<'_, 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: &SmirCtxt<'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: &SmirCtxt<'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>, _: &SmirCtxt<'_, 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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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>, _: &SmirCtxt<'_, 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>, _: &SmirCtxt<'_, 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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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: &SmirCtxt<'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>, _: &SmirCtxt<'_, 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>, _: &SmirCtxt<'_, 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>, _: &SmirCtxt<'_, 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: &SmirCtxt<'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: &SmirCtxt<'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 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 cx.coroutine_movability(*def_id).stable(tables, cx),
461 )),
462 ty::Never => TyKind::RigidTy(RigidTy::Never),
463 ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple(
464 fields.iter().map(|ty| ty.stable(tables, cx)).collect(),
465 )),
466 ty::Alias(alias_kind, alias_ty) => {
467 TyKind::Alias(alias_kind.stable(tables, cx), alias_ty.stable(tables, cx))
468 }
469 ty::Param(param_ty) => TyKind::Param(param_ty.stable(tables, cx)),
470 ty::Bound(debruijn_idx, bound_ty) => {
471 TyKind::Bound(debruijn_idx.as_usize(), bound_ty.stable(tables, cx))
472 }
473 ty::CoroutineWitness(def_id, args) => TyKind::RigidTy(RigidTy::CoroutineWitness(
474 tables.coroutine_witness_def(*def_id),
475 args.stable(tables, cx),
476 )),
477 ty::Placeholder(..) | ty::Infer(_) | ty::Error(_) => {
478 unreachable!();
479 }
480 }
481 }
482}
483
484impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
485 type T = crate::ty::Pattern;
486
487 fn stable<'cx>(
488 &self,
489 tables: &mut Tables<'cx, BridgeTys>,
490 cx: &SmirCtxt<'cx, BridgeTys>,
491 ) -> Self::T {
492 match **self {
493 ty::PatternKind::Range { start, end } => crate::ty::Pattern::Range {
494 start: Some(start.stable(tables, cx)),
496 end: Some(end.stable(tables, cx)),
497 include_end: true,
498 },
499 ty::PatternKind::Or(_) => todo!(),
500 }
501 }
502}
503
504impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
505 type T = crate::ty::TyConst;
506
507 fn stable<'cx>(
508 &self,
509 tables: &mut Tables<'cx, BridgeTys>,
510 cx: &SmirCtxt<'cx, BridgeTys>,
511 ) -> Self::T {
512 let ct = cx.lift(*self).unwrap();
513 let kind = match ct.kind() {
514 ty::ConstKind::Value(cv) => {
515 let const_val = cx.valtree_to_const_val(cv);
516 if matches!(const_val, mir::ConstValue::ZeroSized) {
517 crate::ty::TyConstKind::ZSTValue(cv.ty.stable(tables, cx))
518 } else {
519 crate::ty::TyConstKind::Value(
520 cv.ty.stable(tables, cx),
521 alloc::new_allocation(cv.ty, const_val, tables, cx),
522 )
523 }
524 }
525 ty::ConstKind::Param(param) => crate::ty::TyConstKind::Param(param.stable(tables, cx)),
526 ty::ConstKind::Unevaluated(uv) => crate::ty::TyConstKind::Unevaluated(
527 tables.const_def(uv.def),
528 uv.args.stable(tables, cx),
529 ),
530 ty::ConstKind::Error(_) => unreachable!(),
531 ty::ConstKind::Infer(_) => unreachable!(),
532 ty::ConstKind::Bound(_, _) => unimplemented!(),
533 ty::ConstKind::Placeholder(_) => unimplemented!(),
534 ty::ConstKind::Expr(_) => unimplemented!(),
535 };
536 let id = tables.intern_ty_const(ct);
537 crate::ty::TyConst::new(kind, id)
538 }
539}
540
541impl<'tcx> Stable<'tcx> for ty::ParamConst {
542 type T = crate::ty::ParamConst;
543 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
544 use crate::ty::ParamConst;
545 ParamConst { index: self.index, name: self.name.to_string() }
546 }
547}
548
549impl<'tcx> Stable<'tcx> for ty::ParamTy {
550 type T = crate::ty::ParamTy;
551 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
552 use crate::ty::ParamTy;
553 ParamTy { index: self.index, name: self.name.to_string() }
554 }
555}
556
557impl<'tcx> Stable<'tcx> for ty::BoundTy {
558 type T = crate::ty::BoundTy;
559 fn stable<'cx>(
560 &self,
561 tables: &mut Tables<'cx, BridgeTys>,
562 cx: &SmirCtxt<'cx, BridgeTys>,
563 ) -> Self::T {
564 use crate::ty::BoundTy;
565 BoundTy { var: self.var.as_usize(), kind: self.kind.stable(tables, cx) }
566 }
567}
568
569impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {
570 type T = crate::ty::TraitSpecializationKind;
571 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
572 use crate::ty::TraitSpecializationKind;
573
574 match self {
575 ty::trait_def::TraitSpecializationKind::None => TraitSpecializationKind::None,
576 ty::trait_def::TraitSpecializationKind::Marker => TraitSpecializationKind::Marker,
577 ty::trait_def::TraitSpecializationKind::AlwaysApplicable => {
578 TraitSpecializationKind::AlwaysApplicable
579 }
580 }
581 }
582}
583
584impl<'tcx> Stable<'tcx> for ty::TraitDef {
585 type T = crate::ty::TraitDecl;
586 fn stable<'cx>(
587 &self,
588 tables: &mut Tables<'cx, BridgeTys>,
589 cx: &SmirCtxt<'cx, BridgeTys>,
590 ) -> Self::T {
591 use crate::opaque;
592 use crate::ty::TraitDecl;
593
594 TraitDecl {
595 def_id: tables.trait_def(self.def_id),
596 safety: self.safety.stable(tables, cx),
597 paren_sugar: self.paren_sugar,
598 has_auto_impl: self.has_auto_impl,
599 is_marker: self.is_marker,
600 is_coinductive: self.is_coinductive,
601 skip_array_during_method_dispatch: self.skip_array_during_method_dispatch,
602 skip_boxed_slice_during_method_dispatch: self.skip_boxed_slice_during_method_dispatch,
603 specialization_kind: self.specialization_kind.stable(tables, cx),
604 must_implement_one_of: self
605 .must_implement_one_of
606 .as_ref()
607 .map(|idents| idents.iter().map(|ident| opaque(ident)).collect()),
608 implement_via_object: self.implement_via_object,
609 deny_explicit_impl: self.deny_explicit_impl,
610 }
611 }
612}
613
614impl<'tcx> Stable<'tcx> for ty::TraitRef<'tcx> {
615 type T = crate::ty::TraitRef;
616 fn stable<'cx>(
617 &self,
618 tables: &mut Tables<'cx, BridgeTys>,
619 cx: &SmirCtxt<'cx, BridgeTys>,
620 ) -> Self::T {
621 use crate::ty::TraitRef;
622
623 TraitRef::try_new(tables.trait_def(self.def_id), self.args.stable(tables, cx)).unwrap()
624 }
625}
626
627impl<'tcx> Stable<'tcx> for ty::Generics {
628 type T = crate::ty::Generics;
629
630 fn stable<'cx>(
631 &self,
632 tables: &mut Tables<'cx, BridgeTys>,
633 cx: &SmirCtxt<'cx, BridgeTys>,
634 ) -> Self::T {
635 use crate::ty::Generics;
636
637 let params: Vec<_> = self.own_params.iter().map(|param| param.stable(tables, cx)).collect();
638 let param_def_id_to_index =
639 params.iter().map(|param| (param.def_id, param.index)).collect();
640
641 Generics {
642 parent: self.parent.map(|did| tables.generic_def(did)),
643 parent_count: self.parent_count,
644 params,
645 param_def_id_to_index,
646 has_self: self.has_self,
647 has_late_bound_regions: self
648 .has_late_bound_regions
649 .as_ref()
650 .map(|late_bound_regions| late_bound_regions.stable(tables, cx)),
651 }
652 }
653}
654
655impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {
656 type T = crate::ty::GenericParamDefKind;
657
658 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
659 use crate::ty::GenericParamDefKind;
660 match self {
661 ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
662 ty::GenericParamDefKind::Type { has_default, synthetic } => {
663 GenericParamDefKind::Type { has_default: *has_default, synthetic: *synthetic }
664 }
665 ty::GenericParamDefKind::Const { has_default, synthetic: _ } => {
666 GenericParamDefKind::Const { has_default: *has_default }
667 }
668 }
669 }
670}
671
672impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDef {
673 type T = crate::ty::GenericParamDef;
674
675 fn stable<'cx>(
676 &self,
677 tables: &mut Tables<'cx, BridgeTys>,
678 cx: &SmirCtxt<'cx, BridgeTys>,
679 ) -> Self::T {
680 GenericParamDef {
681 name: self.name.to_string(),
682 def_id: tables.generic_def(self.def_id),
683 index: self.index,
684 pure_wrt_drop: self.pure_wrt_drop,
685 kind: self.kind.stable(tables, cx),
686 }
687 }
688}
689
690impl<'tcx> Stable<'tcx> for ty::PredicateKind<'tcx> {
691 type T = crate::ty::PredicateKind;
692
693 fn stable<'cx>(
694 &self,
695 tables: &mut Tables<'cx, BridgeTys>,
696 cx: &SmirCtxt<'cx, BridgeTys>,
697 ) -> Self::T {
698 use rustc_middle::ty::PredicateKind;
699 match self {
700 PredicateKind::Clause(clause_kind) => {
701 crate::ty::PredicateKind::Clause(clause_kind.stable(tables, cx))
702 }
703 PredicateKind::DynCompatible(did) => {
704 crate::ty::PredicateKind::DynCompatible(tables.trait_def(*did))
705 }
706 PredicateKind::Subtype(subtype_predicate) => {
707 crate::ty::PredicateKind::SubType(subtype_predicate.stable(tables, cx))
708 }
709 PredicateKind::Coerce(coerce_predicate) => {
710 crate::ty::PredicateKind::Coerce(coerce_predicate.stable(tables, cx))
711 }
712 PredicateKind::ConstEquate(a, b) => {
713 crate::ty::PredicateKind::ConstEquate(a.stable(tables, cx), b.stable(tables, cx))
714 }
715 PredicateKind::Ambiguous => crate::ty::PredicateKind::Ambiguous,
716 PredicateKind::NormalizesTo(_pred) => unimplemented!(),
717 PredicateKind::AliasRelate(a, b, alias_relation_direction) => {
718 crate::ty::PredicateKind::AliasRelate(
719 a.kind().stable(tables, cx),
720 b.kind().stable(tables, cx),
721 alias_relation_direction.stable(tables, cx),
722 )
723 }
724 }
725 }
726}
727
728impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
729 type T = crate::ty::ClauseKind;
730
731 fn stable<'cx>(
732 &self,
733 tables: &mut Tables<'cx, BridgeTys>,
734 cx: &SmirCtxt<'cx, BridgeTys>,
735 ) -> Self::T {
736 use rustc_middle::ty::ClauseKind;
737 match *self {
738 ClauseKind::Trait(trait_object) => {
739 crate::ty::ClauseKind::Trait(trait_object.stable(tables, cx))
740 }
741 ClauseKind::RegionOutlives(region_outlives) => {
742 crate::ty::ClauseKind::RegionOutlives(region_outlives.stable(tables, cx))
743 }
744 ClauseKind::TypeOutlives(type_outlives) => {
745 let ty::OutlivesPredicate::<_, _>(a, b) = type_outlives;
746 crate::ty::ClauseKind::TypeOutlives(crate::ty::OutlivesPredicate(
747 a.stable(tables, cx),
748 b.stable(tables, cx),
749 ))
750 }
751 ClauseKind::Projection(projection_predicate) => {
752 crate::ty::ClauseKind::Projection(projection_predicate.stable(tables, cx))
753 }
754 ClauseKind::ConstArgHasType(const_, ty) => crate::ty::ClauseKind::ConstArgHasType(
755 const_.stable(tables, cx),
756 ty.stable(tables, cx),
757 ),
758 ClauseKind::WellFormed(term) => {
759 crate::ty::ClauseKind::WellFormed(term.kind().stable(tables, cx))
760 }
761 ClauseKind::ConstEvaluatable(const_) => {
762 crate::ty::ClauseKind::ConstEvaluatable(const_.stable(tables, cx))
763 }
764 ClauseKind::HostEffect(..) => {
765 todo!()
766 }
767 ClauseKind::UnstableFeature(_) => {
768 todo!()
769 }
770 }
771 }
772}
773
774impl<'tcx> Stable<'tcx> for ty::ClosureKind {
775 type T = crate::ty::ClosureKind;
776
777 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
778 use rustc_middle::ty::ClosureKind::*;
779 match self {
780 Fn => crate::ty::ClosureKind::Fn,
781 FnMut => crate::ty::ClosureKind::FnMut,
782 FnOnce => crate::ty::ClosureKind::FnOnce,
783 }
784 }
785}
786
787impl<'tcx> Stable<'tcx> for ty::SubtypePredicate<'tcx> {
788 type T = crate::ty::SubtypePredicate;
789
790 fn stable<'cx>(
791 &self,
792 tables: &mut Tables<'cx, BridgeTys>,
793 cx: &SmirCtxt<'cx, BridgeTys>,
794 ) -> Self::T {
795 let ty::SubtypePredicate { a, b, a_is_expected: _ } = self;
796 crate::ty::SubtypePredicate { a: a.stable(tables, cx), b: b.stable(tables, cx) }
797 }
798}
799
800impl<'tcx> Stable<'tcx> for ty::CoercePredicate<'tcx> {
801 type T = crate::ty::CoercePredicate;
802
803 fn stable<'cx>(
804 &self,
805 tables: &mut Tables<'cx, BridgeTys>,
806 cx: &SmirCtxt<'cx, BridgeTys>,
807 ) -> Self::T {
808 let ty::CoercePredicate { a, b } = self;
809 crate::ty::CoercePredicate { a: a.stable(tables, cx), b: b.stable(tables, cx) }
810 }
811}
812
813impl<'tcx> Stable<'tcx> for ty::AliasRelationDirection {
814 type T = crate::ty::AliasRelationDirection;
815
816 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
817 use rustc_middle::ty::AliasRelationDirection::*;
818 match self {
819 Equate => crate::ty::AliasRelationDirection::Equate,
820 Subtype => crate::ty::AliasRelationDirection::Subtype,
821 }
822 }
823}
824
825impl<'tcx> Stable<'tcx> for ty::TraitPredicate<'tcx> {
826 type T = crate::ty::TraitPredicate;
827
828 fn stable<'cx>(
829 &self,
830 tables: &mut Tables<'cx, BridgeTys>,
831 cx: &SmirCtxt<'cx, BridgeTys>,
832 ) -> Self::T {
833 let ty::TraitPredicate { trait_ref, polarity } = self;
834 crate::ty::TraitPredicate {
835 trait_ref: trait_ref.stable(tables, cx),
836 polarity: polarity.stable(tables, cx),
837 }
838 }
839}
840
841impl<'tcx, T> Stable<'tcx> for ty::OutlivesPredicate<'tcx, T>
842where
843 T: Stable<'tcx>,
844{
845 type T = crate::ty::OutlivesPredicate<T::T, Region>;
846
847 fn stable<'cx>(
848 &self,
849 tables: &mut Tables<'cx, BridgeTys>,
850 cx: &SmirCtxt<'cx, BridgeTys>,
851 ) -> Self::T {
852 let ty::OutlivesPredicate(a, b) = self;
853 crate::ty::OutlivesPredicate(a.stable(tables, cx), b.stable(tables, cx))
854 }
855}
856
857impl<'tcx> Stable<'tcx> for ty::ProjectionPredicate<'tcx> {
858 type T = crate::ty::ProjectionPredicate;
859
860 fn stable<'cx>(
861 &self,
862 tables: &mut Tables<'cx, BridgeTys>,
863 cx: &SmirCtxt<'cx, BridgeTys>,
864 ) -> Self::T {
865 let ty::ProjectionPredicate { projection_term, term } = self;
866 crate::ty::ProjectionPredicate {
867 projection_term: projection_term.stable(tables, cx),
868 term: term.kind().stable(tables, cx),
869 }
870 }
871}
872
873impl<'tcx> Stable<'tcx> for ty::ImplPolarity {
874 type T = crate::ty::ImplPolarity;
875
876 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
877 use rustc_middle::ty::ImplPolarity::*;
878 match self {
879 Positive => crate::ty::ImplPolarity::Positive,
880 Negative => crate::ty::ImplPolarity::Negative,
881 Reservation => crate::ty::ImplPolarity::Reservation,
882 }
883 }
884}
885
886impl<'tcx> Stable<'tcx> for ty::PredicatePolarity {
887 type T = crate::ty::PredicatePolarity;
888
889 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
890 use rustc_middle::ty::PredicatePolarity::*;
891 match self {
892 Positive => crate::ty::PredicatePolarity::Positive,
893 Negative => crate::ty::PredicatePolarity::Negative,
894 }
895 }
896}
897
898impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
899 type T = crate::ty::Region;
900
901 fn stable<'cx>(
902 &self,
903 tables: &mut Tables<'cx, BridgeTys>,
904 cx: &SmirCtxt<'cx, BridgeTys>,
905 ) -> Self::T {
906 Region { kind: self.kind().stable(tables, cx) }
907 }
908}
909
910impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> {
911 type T = crate::ty::RegionKind;
912
913 fn stable<'cx>(
914 &self,
915 tables: &mut Tables<'cx, BridgeTys>,
916 cx: &SmirCtxt<'cx, BridgeTys>,
917 ) -> Self::T {
918 use crate::ty::{BoundRegion, EarlyParamRegion, RegionKind};
919 match self {
920 ty::ReEarlyParam(early_reg) => RegionKind::ReEarlyParam(EarlyParamRegion {
921 index: early_reg.index,
922 name: early_reg.name.to_string(),
923 }),
924 ty::ReBound(db_index, bound_reg) => RegionKind::ReBound(
925 db_index.as_u32(),
926 BoundRegion {
927 var: bound_reg.var.as_u32(),
928 kind: bound_reg.kind.stable(tables, cx),
929 },
930 ),
931 ty::ReStatic => RegionKind::ReStatic,
932 ty::RePlaceholder(place_holder) => RegionKind::RePlaceholder(crate::ty::Placeholder {
933 universe: place_holder.universe.as_u32(),
934 bound: BoundRegion {
935 var: place_holder.bound.var.as_u32(),
936 kind: place_holder.bound.kind.stable(tables, cx),
937 },
938 }),
939 ty::ReErased => RegionKind::ReErased,
940 _ => unreachable!("{self:?}"),
941 }
942 }
943}
944
945impl<'tcx> Stable<'tcx> for ty::Instance<'tcx> {
946 type T = crate::mir::mono::Instance;
947
948 fn stable<'cx>(
949 &self,
950 tables: &mut Tables<'cx, BridgeTys>,
951 cx: &SmirCtxt<'cx, BridgeTys>,
952 ) -> Self::T {
953 let def = tables.instance_def(cx.lift(*self).unwrap());
954 let kind = match self.def {
955 ty::InstanceKind::Item(..) => crate::mir::mono::InstanceKind::Item,
956 ty::InstanceKind::Intrinsic(..) => crate::mir::mono::InstanceKind::Intrinsic,
957 ty::InstanceKind::Virtual(_def_id, idx) => {
958 crate::mir::mono::InstanceKind::Virtual { idx }
959 }
960 ty::InstanceKind::VTableShim(..)
961 | ty::InstanceKind::ReifyShim(..)
962 | ty::InstanceKind::FnPtrAddrShim(..)
963 | ty::InstanceKind::ClosureOnceShim { .. }
964 | ty::InstanceKind::ConstructCoroutineInClosureShim { .. }
965 | ty::InstanceKind::ThreadLocalShim(..)
966 | ty::InstanceKind::DropGlue(..)
967 | ty::InstanceKind::CloneShim(..)
968 | ty::InstanceKind::FnPtrShim(..)
969 | ty::InstanceKind::FutureDropPollShim(..)
970 | ty::InstanceKind::AsyncDropGlue(..)
971 | ty::InstanceKind::AsyncDropGlueCtorShim(..) => crate::mir::mono::InstanceKind::Shim,
972 };
973 crate::mir::mono::Instance { def, kind }
974 }
975}
976
977impl<'tcx> Stable<'tcx> for ty::Variance {
978 type T = crate::mir::Variance;
979 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
980 match self {
981 ty::Bivariant => crate::mir::Variance::Bivariant,
982 ty::Contravariant => crate::mir::Variance::Contravariant,
983 ty::Covariant => crate::mir::Variance::Covariant,
984 ty::Invariant => crate::mir::Variance::Invariant,
985 }
986 }
987}
988
989impl<'tcx> Stable<'tcx> for ty::Movability {
990 type T = crate::ty::Movability;
991
992 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
993 match self {
994 ty::Movability::Static => crate::ty::Movability::Static,
995 ty::Movability::Movable => crate::ty::Movability::Movable,
996 }
997 }
998}
999
1000impl<'tcx> Stable<'tcx> for rustc_abi::ExternAbi {
1001 type T = crate::ty::Abi;
1002
1003 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
1004 use rustc_abi::ExternAbi;
1005
1006 use crate::ty::Abi;
1007 match *self {
1008 ExternAbi::Rust => Abi::Rust,
1009 ExternAbi::C { unwind } => Abi::C { unwind },
1010 ExternAbi::Cdecl { unwind } => Abi::Cdecl { unwind },
1011 ExternAbi::Stdcall { unwind } => Abi::Stdcall { unwind },
1012 ExternAbi::Fastcall { unwind } => Abi::Fastcall { unwind },
1013 ExternAbi::Vectorcall { unwind } => Abi::Vectorcall { unwind },
1014 ExternAbi::Thiscall { unwind } => Abi::Thiscall { unwind },
1015 ExternAbi::Aapcs { unwind } => Abi::Aapcs { unwind },
1016 ExternAbi::Win64 { unwind } => Abi::Win64 { unwind },
1017 ExternAbi::SysV64 { unwind } => Abi::SysV64 { unwind },
1018 ExternAbi::PtxKernel => Abi::PtxKernel,
1019 ExternAbi::GpuKernel => Abi::GpuKernel,
1020 ExternAbi::Msp430Interrupt => Abi::Msp430Interrupt,
1021 ExternAbi::X86Interrupt => Abi::X86Interrupt,
1022 ExternAbi::EfiApi => Abi::EfiApi,
1023 ExternAbi::AvrInterrupt => Abi::AvrInterrupt,
1024 ExternAbi::AvrNonBlockingInterrupt => Abi::AvrNonBlockingInterrupt,
1025 ExternAbi::CmseNonSecureCall => Abi::CCmseNonSecureCall,
1026 ExternAbi::CmseNonSecureEntry => Abi::CCmseNonSecureEntry,
1027 ExternAbi::System { unwind } => Abi::System { unwind },
1028 ExternAbi::RustCall => Abi::RustCall,
1029 ExternAbi::Unadjusted => Abi::Unadjusted,
1030 ExternAbi::RustCold => Abi::RustCold,
1031 ExternAbi::RustInvalid => Abi::RustInvalid,
1032 ExternAbi::RiscvInterruptM => Abi::RiscvInterruptM,
1033 ExternAbi::RiscvInterruptS => Abi::RiscvInterruptS,
1034 ExternAbi::Custom => Abi::Custom,
1035 }
1036 }
1037}
1038
1039impl<'tcx> Stable<'tcx> for rustc_session::cstore::ForeignModule {
1040 type T = crate::ty::ForeignModule;
1041
1042 fn stable<'cx>(
1043 &self,
1044 tables: &mut Tables<'cx, BridgeTys>,
1045 cx: &SmirCtxt<'cx, BridgeTys>,
1046 ) -> Self::T {
1047 crate::ty::ForeignModule {
1048 def_id: tables.foreign_module_def(self.def_id),
1049 abi: self.abi.stable(tables, cx),
1050 }
1051 }
1052}
1053
1054impl<'tcx> Stable<'tcx> for ty::AssocKind {
1055 type T = crate::ty::AssocKind;
1056
1057 fn stable<'cx>(
1058 &self,
1059 tables: &mut Tables<'cx, BridgeTys>,
1060 cx: &SmirCtxt<'cx, BridgeTys>,
1061 ) -> Self::T {
1062 use crate::ty::{AssocKind, AssocTypeData};
1063 match *self {
1064 ty::AssocKind::Const { name } => AssocKind::Const { name: name.to_string() },
1065 ty::AssocKind::Fn { name, has_self } => {
1066 AssocKind::Fn { name: name.to_string(), has_self }
1067 }
1068 ty::AssocKind::Type { data } => AssocKind::Type {
1069 data: match data {
1070 ty::AssocTypeData::Normal(name) => AssocTypeData::Normal(name.to_string()),
1071 ty::AssocTypeData::Rpitit(rpitit) => {
1072 AssocTypeData::Rpitit(rpitit.stable(tables, cx))
1073 }
1074 },
1075 },
1076 }
1077 }
1078}
1079
1080impl<'tcx> Stable<'tcx> for ty::AssocItemContainer {
1081 type T = crate::ty::AssocItemContainer;
1082
1083 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &SmirCtxt<'_, BridgeTys>) -> Self::T {
1084 use crate::ty::AssocItemContainer;
1085 match self {
1086 ty::AssocItemContainer::Trait => AssocItemContainer::Trait,
1087 ty::AssocItemContainer::Impl => AssocItemContainer::Impl,
1088 }
1089 }
1090}
1091
1092impl<'tcx> Stable<'tcx> for ty::AssocItem {
1093 type T = crate::ty::AssocItem;
1094
1095 fn stable<'cx>(
1096 &self,
1097 tables: &mut Tables<'cx, BridgeTys>,
1098 cx: &SmirCtxt<'cx, BridgeTys>,
1099 ) -> Self::T {
1100 crate::ty::AssocItem {
1101 def_id: tables.assoc_def(self.def_id),
1102 kind: self.kind.stable(tables, cx),
1103 container: self.container.stable(tables, cx),
1104 trait_item_def_id: self.trait_item_def_id.map(|did| tables.assoc_def(did)),
1105 }
1106 }
1107}
1108
1109impl<'tcx> Stable<'tcx> for ty::ImplTraitInTraitData {
1110 type T = crate::ty::ImplTraitInTraitData;
1111
1112 fn stable<'cx>(
1113 &self,
1114 tables: &mut Tables<'cx, BridgeTys>,
1115 _: &SmirCtxt<'cx, BridgeTys>,
1116 ) -> Self::T {
1117 use crate::ty::ImplTraitInTraitData;
1118 match self {
1119 ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id } => {
1120 ImplTraitInTraitData::Trait {
1121 fn_def_id: tables.fn_def(*fn_def_id),
1122 opaque_def_id: tables.opaque_def(*opaque_def_id),
1123 }
1124 }
1125 ty::ImplTraitInTraitData::Impl { fn_def_id } => {
1126 ImplTraitInTraitData::Impl { fn_def_id: tables.fn_def(*fn_def_id) }
1127 }
1128 }
1129 }
1130}
1131
1132impl<'tcx> Stable<'tcx> for rustc_middle::ty::util::Discr<'tcx> {
1133 type T = crate::ty::Discr;
1134
1135 fn stable<'cx>(
1136 &self,
1137 tables: &mut Tables<'cx, BridgeTys>,
1138 cx: &SmirCtxt<'cx, BridgeTys>,
1139 ) -> Self::T {
1140 crate::ty::Discr { val: self.val, ty: self.ty.stable(tables, cx) }
1141 }
1142}