1//! Conversion of internal Rust compiler `ty` items to stable ones.23use rustc_middle::ty::Ty;4use rustc_middle::{bug, mir, ty};5use rustc_public_bridge::Tables;6use rustc_public_bridge::context::CompilerCtxt;78use 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;1415impl<'tcx> Stable<'tcx> for ty::AliasTyKind<'tcx> {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}2627impl<'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, kind, .. } = self;35 crate::ty::AliasTy {36 def_id: tables.alias_def(kind.def_id()),37 args: args.stable(tables, cx),38 }39 }40}4142impl<'tcx> Stable<'tcx> for ty::AliasTerm<'tcx> {43 type T = crate::ty::AliasTerm;44 fn stable<'cx>(45 &self,46 tables: &mut Tables<'cx, BridgeTys>,47 cx: &CompilerCtxt<'cx, BridgeTys>,48 ) -> Self::T {49 let ty::AliasTerm { args, kind, .. } = self;50 crate::ty::AliasTerm {51 def_id: tables.alias_def(kind.def_id()),52 args: args.stable(tables, cx),53 }54 }55}5657impl<'tcx> Stable<'tcx> for ty::ExistentialPredicate<'tcx> {58 type T = crate::ty::ExistentialPredicate;5960 fn stable<'cx>(61 &self,62 tables: &mut Tables<'cx, BridgeTys>,63 cx: &CompilerCtxt<'cx, BridgeTys>,64 ) -> Self::T {65 use crate::ty::ExistentialPredicate::*;66 match self {67 ty::ExistentialPredicate::Trait(existential_trait_ref) => {68 Trait(existential_trait_ref.stable(tables, cx))69 }70 ty::ExistentialPredicate::Projection(existential_projection) => {71 Projection(existential_projection.stable(tables, cx))72 }73 ty::ExistentialPredicate::AutoTrait(def_id) => AutoTrait(tables.trait_def(*def_id)),74 }75 }76}7778impl<'tcx> Stable<'tcx> for ty::ExistentialTraitRef<'tcx> {79 type T = crate::ty::ExistentialTraitRef;8081 fn stable<'cx>(82 &self,83 tables: &mut Tables<'cx, BridgeTys>,84 cx: &CompilerCtxt<'cx, BridgeTys>,85 ) -> Self::T {86 let ty::ExistentialTraitRef { def_id, args, .. } = self;87 crate::ty::ExistentialTraitRef {88 def_id: tables.trait_def(*def_id),89 generic_args: args.stable(tables, cx),90 }91 }92}9394impl<'tcx> Stable<'tcx> for ty::TermKind<'tcx> {95 type T = crate::ty::TermKind;9697 fn stable<'cx>(98 &self,99 tables: &mut Tables<'cx, BridgeTys>,100 cx: &CompilerCtxt<'cx, BridgeTys>,101 ) -> Self::T {102 use crate::ty::TermKind;103 match self {104 ty::TermKind::Ty(ty) => TermKind::Type(ty.stable(tables, cx)),105 ty::TermKind::Const(cnst) => {106 let cnst = cnst.stable(tables, cx);107 TermKind::Const(cnst)108 }109 }110 }111}112113impl<'tcx> Stable<'tcx> for ty::ExistentialProjection<'tcx> {114 type T = crate::ty::ExistentialProjection;115116 fn stable<'cx>(117 &self,118 tables: &mut Tables<'cx, BridgeTys>,119 cx: &CompilerCtxt<'cx, BridgeTys>,120 ) -> Self::T {121 let ty::ExistentialProjection { def_id, args, term, .. } = self;122 crate::ty::ExistentialProjection {123 def_id: tables.trait_def(*def_id),124 generic_args: args.stable(tables, cx),125 term: term.kind().stable(tables, cx),126 }127 }128}129130impl<'tcx> Stable<'tcx> for ty::adjustment::PointerCoercion {131 type T = crate::mir::PointerCoercion;132 fn stable<'cx>(133 &self,134 tables: &mut Tables<'cx, BridgeTys>,135 cx: &CompilerCtxt<'cx, BridgeTys>,136 ) -> Self::T {137 use rustc_middle::ty::adjustment::PointerCoercion;138 match self {139 PointerCoercion::ReifyFnPointer(safety) => {140 crate::mir::PointerCoercion::ReifyFnPointer(safety.stable(tables, cx))141 }142 PointerCoercion::UnsafeFnPointer => crate::mir::PointerCoercion::UnsafeFnPointer,143 PointerCoercion::ClosureFnPointer(safety) => {144 crate::mir::PointerCoercion::ClosureFnPointer(safety.stable(tables, cx))145 }146 PointerCoercion::MutToConstPointer => crate::mir::PointerCoercion::MutToConstPointer,147 PointerCoercion::ArrayToPointer => crate::mir::PointerCoercion::ArrayToPointer,148 PointerCoercion::Unsize => crate::mir::PointerCoercion::Unsize,149 }150 }151}152153impl<'tcx> Stable<'tcx> for ty::UserTypeAnnotationIndex {154 type T = usize;155 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {156 self.as_usize()157 }158}159160impl<'tcx> Stable<'tcx> for ty::AdtKind {161 type T = AdtKind;162163 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {164 match self {165 ty::AdtKind::Struct => AdtKind::Struct,166 ty::AdtKind::Union => AdtKind::Union,167 ty::AdtKind::Enum => AdtKind::Enum,168 }169 }170}171172impl<'tcx> Stable<'tcx> for ty::FieldDef {173 type T = crate::ty::FieldDef;174175 fn stable<'cx>(176 &self,177 tables: &mut Tables<'cx, BridgeTys>,178 cx: &CompilerCtxt<'cx, BridgeTys>,179 ) -> Self::T {180 crate::ty::FieldDef {181 def: tables.create_def_id(self.did),182 name: self.name.stable(tables, cx),183 }184 }185}186187impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {188 type T = crate::ty::GenericArgs;189 fn stable<'cx>(190 &self,191 tables: &mut Tables<'cx, BridgeTys>,192 cx: &CompilerCtxt<'cx, BridgeTys>,193 ) -> Self::T {194 GenericArgs(self.iter().map(|arg| arg.kind().stable(tables, cx)).collect())195 }196}197198impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> {199 type T = crate::ty::GenericArgKind;200201 fn stable<'cx>(202 &self,203 tables: &mut Tables<'cx, BridgeTys>,204 cx: &CompilerCtxt<'cx, BridgeTys>,205 ) -> Self::T {206 use crate::ty::GenericArgKind;207 match self {208 ty::GenericArgKind::Lifetime(region) => {209 GenericArgKind::Lifetime(region.stable(tables, cx))210 }211 ty::GenericArgKind::Type(ty) => GenericArgKind::Type(ty.stable(tables, cx)),212 ty::GenericArgKind::Const(cnst) => GenericArgKind::Const(cnst.stable(tables, cx)),213 }214 }215}216217impl<'tcx, S, V> Stable<'tcx> for ty::Binder<'tcx, S>218where219 S: Stable<'tcx, T = V>,220{221 type T = crate::ty::Binder<V>;222223 fn stable<'cx>(224 &self,225 tables: &mut Tables<'cx, BridgeTys>,226 cx: &CompilerCtxt<'cx, BridgeTys>,227 ) -> Self::T {228 use crate::ty::Binder;229230 Binder {231 value: self.as_ref().skip_binder().stable(tables, cx),232 bound_vars: self233 .bound_vars()234 .iter()235 .map(|bound_var| bound_var.stable(tables, cx))236 .collect(),237 }238 }239}240241impl<'tcx, S, V> Stable<'tcx> for ty::EarlyBinder<'tcx, S>242where243 S: Stable<'tcx, T = V>,244{245 type T = crate::ty::EarlyBinder<V>;246247 fn stable<'cx>(248 &self,249 tables: &mut Tables<'cx, BridgeTys>,250 cx: &CompilerCtxt<'cx, BridgeTys>,251 ) -> Self::T {252 use crate::ty::EarlyBinder;253254 EarlyBinder { value: self.as_ref().skip_binder().stable(tables, cx) }255 }256}257258// This internal type isn't publicly exposed, because it is an implementation detail.259// But it's a public field of FnSig (which has a public mirror type), so allow conversions.260impl<'tcx> Stable<'tcx> for ty::FnSigKind<'tcx> {261 type T = (bool /*c_variadic*/, crate::mir::Safety, crate::ty::Abi);262 fn stable<'cx>(263 &self,264 tables: &mut Tables<'cx, BridgeTys>,265 cx: &CompilerCtxt<'cx, BridgeTys>,266 ) -> Self::T {267 (268 self.c_variadic(),269 if self.is_safe() { crate::mir::Safety::Safe } else { crate::mir::Safety::Unsafe },270 self.abi().stable(tables, cx),271 )272 }273}274275impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {276 type T = crate::ty::FnSig;277 fn stable<'cx>(278 &self,279 tables: &mut Tables<'cx, BridgeTys>,280 cx: &CompilerCtxt<'cx, BridgeTys>,281 ) -> Self::T {282 use crate::ty::FnSig;283 let (c_variadic, safety, abi) = self.fn_sig_kind.stable(tables, cx);284285 FnSig {286 inputs_and_output: self287 .inputs_and_output288 .iter()289 .map(|ty| ty.stable(tables, cx))290 .collect(),291 c_variadic,292 safety,293 abi,294 }295 }296}297298impl<'tcx> Stable<'tcx> for ty::BoundTyKind<'tcx> {299 type T = crate::ty::BoundTyKind;300301 fn stable<'cx>(302 &self,303 tables: &mut Tables<'cx, BridgeTys>,304 cx: &CompilerCtxt<'cx, BridgeTys>,305 ) -> Self::T {306 use crate::ty::BoundTyKind;307308 match self {309 ty::BoundTyKind::Anon => BoundTyKind::Anon,310 ty::BoundTyKind::Param(def_id) => {311 BoundTyKind::Param(tables.param_def(*def_id), cx.tcx.item_name(*def_id).to_string())312 }313 }314 }315}316317impl<'tcx> Stable<'tcx> for ty::BoundRegionKind<'tcx> {318 type T = crate::ty::BoundRegionKind;319320 fn stable<'cx>(321 &self,322 tables: &mut Tables<'cx, BridgeTys>,323 cx: &CompilerCtxt<'cx, BridgeTys>,324 ) -> Self::T {325 use crate::ty::BoundRegionKind;326327 match self {328 ty::BoundRegionKind::Anon => BoundRegionKind::BrAnon,329 ty::BoundRegionKind::Named(def_id) => BoundRegionKind::BrNamed(330 tables.br_named_def(*def_id),331 cx.tcx.item_name(*def_id).to_string(),332 ),333 ty::BoundRegionKind::ClosureEnv => BoundRegionKind::BrEnv,334 ty::BoundRegionKind::NamedForPrinting(_) => bug!("only used for pretty printing"),335 }336 }337}338339impl<'tcx> Stable<'tcx> for ty::BoundVariableKind<'tcx> {340 type T = crate::ty::BoundVariableKind;341342 fn stable<'cx>(343 &self,344 tables: &mut Tables<'cx, BridgeTys>,345 cx: &CompilerCtxt<'cx, BridgeTys>,346 ) -> Self::T {347 use crate::ty::BoundVariableKind;348349 match self {350 ty::BoundVariableKind::Ty(bound_ty_kind) => {351 BoundVariableKind::Ty(bound_ty_kind.stable(tables, cx))352 }353 ty::BoundVariableKind::Region(bound_region_kind) => {354 BoundVariableKind::Region(bound_region_kind.stable(tables, cx))355 }356 ty::BoundVariableKind::Const => BoundVariableKind::Const,357 }358 }359}360361impl<'tcx> Stable<'tcx> for ty::IntTy {362 type T = IntTy;363364 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {365 match self {366 ty::IntTy::Isize => IntTy::Isize,367 ty::IntTy::I8 => IntTy::I8,368 ty::IntTy::I16 => IntTy::I16,369 ty::IntTy::I32 => IntTy::I32,370 ty::IntTy::I64 => IntTy::I64,371 ty::IntTy::I128 => IntTy::I128,372 }373 }374}375376impl<'tcx> Stable<'tcx> for ty::UintTy {377 type T = UintTy;378379 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {380 match self {381 ty::UintTy::Usize => UintTy::Usize,382 ty::UintTy::U8 => UintTy::U8,383 ty::UintTy::U16 => UintTy::U16,384 ty::UintTy::U32 => UintTy::U32,385 ty::UintTy::U64 => UintTy::U64,386 ty::UintTy::U128 => UintTy::U128,387 }388 }389}390391impl<'tcx> Stable<'tcx> for ty::FloatTy {392 type T = FloatTy;393394 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {395 match self {396 ty::FloatTy::F16 => FloatTy::F16,397 ty::FloatTy::F32 => FloatTy::F32,398 ty::FloatTy::F64 => FloatTy::F64,399 ty::FloatTy::F128 => FloatTy::F128,400 }401 }402}403404impl<'tcx> Stable<'tcx> for Ty<'tcx> {405 type T = crate::ty::Ty;406 fn stable<'cx>(407 &self,408 tables: &mut Tables<'cx, BridgeTys>,409 cx: &CompilerCtxt<'cx, BridgeTys>,410 ) -> Self::T {411 tables.intern_ty(cx.lift(*self))412 }413}414415impl<'tcx> Stable<'tcx> for ty::TyKind<'tcx> {416 type T = crate::ty::TyKind;417 fn stable<'cx>(418 &self,419 tables: &mut Tables<'cx, BridgeTys>,420 cx: &CompilerCtxt<'cx, BridgeTys>,421 ) -> Self::T {422 match self {423 ty::Bool => TyKind::RigidTy(RigidTy::Bool),424 ty::Char => TyKind::RigidTy(RigidTy::Char),425 ty::Int(int_ty) => TyKind::RigidTy(RigidTy::Int(int_ty.stable(tables, cx))),426 ty::Uint(uint_ty) => TyKind::RigidTy(RigidTy::Uint(uint_ty.stable(tables, cx))),427 ty::Float(float_ty) => TyKind::RigidTy(RigidTy::Float(float_ty.stable(tables, cx))),428 ty::Adt(adt_def, generic_args) => TyKind::RigidTy(RigidTy::Adt(429 tables.adt_def(adt_def.did()),430 generic_args.stable(tables, cx),431 )),432 ty::Foreign(def_id) => TyKind::RigidTy(RigidTy::Foreign(tables.foreign_def(*def_id))),433 ty::Str => TyKind::RigidTy(RigidTy::Str),434 ty::Array(ty, constant) => {435 TyKind::RigidTy(RigidTy::Array(ty.stable(tables, cx), constant.stable(tables, cx)))436 }437 ty::Pat(ty, pat) => {438 TyKind::RigidTy(RigidTy::Pat(ty.stable(tables, cx), pat.stable(tables, cx)))439 }440 ty::Slice(ty) => TyKind::RigidTy(RigidTy::Slice(ty.stable(tables, cx))),441 ty::RawPtr(ty, mutbl) => {442 TyKind::RigidTy(RigidTy::RawPtr(ty.stable(tables, cx), mutbl.stable(tables, cx)))443 }444 ty::Ref(region, ty, mutbl) => TyKind::RigidTy(RigidTy::Ref(445 region.stable(tables, cx),446 ty.stable(tables, cx),447 mutbl.stable(tables, cx),448 )),449 ty::FnDef(def_id, generic_args) => TyKind::RigidTy(RigidTy::FnDef(450 tables.fn_def(*def_id),451 generic_args.stable(tables, cx),452 )),453 ty::FnPtr(sig_tys, hdr) => {454 TyKind::RigidTy(RigidTy::FnPtr(sig_tys.with(*hdr).stable(tables, cx)))455 }456 // FIXME(unsafe_binders):457 ty::UnsafeBinder(_) => todo!(),458 ty::Dynamic(existential_predicates, region) => TyKind::RigidTy(RigidTy::Dynamic(459 existential_predicates460 .iter()461 .map(|existential_predicate| existential_predicate.stable(tables, cx))462 .collect(),463 region.stable(tables, cx),464 )),465 ty::Closure(def_id, generic_args) => TyKind::RigidTy(RigidTy::Closure(466 tables.closure_def(*def_id),467 generic_args.stable(tables, cx),468 )),469 ty::CoroutineClosure(..) => todo!("FIXME(async_closures): Lower these to SMIR"),470 ty::Coroutine(def_id, generic_args) => TyKind::RigidTy(RigidTy::Coroutine(471 tables.coroutine_def(*def_id),472 generic_args.stable(tables, cx),473 )),474 ty::Never => TyKind::RigidTy(RigidTy::Never),475 ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple(476 fields.iter().map(|ty| ty.stable(tables, cx)).collect(),477 )),478 ty::Alias(_alias_ty) => {479 todo!()480 }481 ty::Param(param_ty) => TyKind::Param(param_ty.stable(tables, cx)),482 ty::Bound(ty::BoundVarIndexKind::Canonical, _) => {483 unreachable!()484 }485 ty::Bound(ty::BoundVarIndexKind::Bound(debruijn_idx), bound_ty) => {486 TyKind::Bound(debruijn_idx.as_usize(), bound_ty.stable(tables, cx))487 }488 ty::CoroutineWitness(def_id, args) => TyKind::RigidTy(RigidTy::CoroutineWitness(489 tables.coroutine_witness_def(*def_id),490 args.stable(tables, cx),491 )),492 ty::Placeholder(..) | ty::Infer(_) | ty::Error(_) => {493 unreachable!();494 }495 }496 }497}498499impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {500 type T = crate::ty::Pattern;501502 fn stable<'cx>(503 &self,504 tables: &mut Tables<'cx, BridgeTys>,505 cx: &CompilerCtxt<'cx, BridgeTys>,506 ) -> Self::T {507 match **self {508 ty::PatternKind::Range { start, end } => crate::ty::Pattern::Range {509 start: start.stable(tables, cx),510 end: end.stable(tables, cx),511 include_end: true,512 },513 ty::PatternKind::NotNull => crate::ty::Pattern::NotNull,514 ty::PatternKind::Or(pats) => {515 crate::ty::Pattern::Or(pats.iter().map(|pat| pat.stable(tables, cx)).collect())516 }517 }518 }519}520521impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {522 type T = crate::ty::TyConst;523524 fn stable<'cx>(525 &self,526 tables: &mut Tables<'cx, BridgeTys>,527 cx: &CompilerCtxt<'cx, BridgeTys>,528 ) -> Self::T {529 let ct = cx.lift(*self);530 let kind = match ct.kind() {531 ty::ConstKind::Value(cv) => {532 let const_val = cx.valtree_to_const_val(cv);533 if matches!(const_val, mir::ConstValue::ZeroSized) {534 crate::ty::TyConstKind::ZSTValue(cv.ty.stable(tables, cx))535 } else {536 crate::ty::TyConstKind::Value(537 cv.ty.stable(tables, cx),538 alloc::new_allocation(cv.ty, const_val, tables, cx),539 )540 }541 }542 ty::ConstKind::Param(param) => crate::ty::TyConstKind::Param(param.stable(tables, cx)),543 ty::ConstKind::Unevaluated(uv) => crate::ty::TyConstKind::Unevaluated(544 tables.const_def(uv.def),545 uv.args.stable(tables, cx),546 ),547 ty::ConstKind::Error(_) => unreachable!(),548 ty::ConstKind::Infer(_) => unreachable!(),549 ty::ConstKind::Bound(_, _) => unimplemented!(),550 ty::ConstKind::Placeholder(_) => unimplemented!(),551 ty::ConstKind::Expr(_) => unimplemented!(),552 };553 let id = tables.intern_ty_const(ct);554 crate::ty::TyConst::new(kind, id)555 }556}557558impl<'tcx> Stable<'tcx> for ty::ParamConst {559 type T = crate::ty::ParamConst;560 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {561 use crate::ty::ParamConst;562 ParamConst { index: self.index, name: self.name.to_string() }563 }564}565566impl<'tcx> Stable<'tcx> for ty::ParamTy {567 type T = crate::ty::ParamTy;568 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {569 use crate::ty::ParamTy;570 ParamTy { index: self.index, name: self.name.to_string() }571 }572}573574impl<'tcx> Stable<'tcx> for ty::BoundTy<'tcx> {575 type T = crate::ty::BoundTy;576 fn stable<'cx>(577 &self,578 tables: &mut Tables<'cx, BridgeTys>,579 cx: &CompilerCtxt<'cx, BridgeTys>,580 ) -> Self::T {581 use crate::ty::BoundTy;582 BoundTy { var: self.var.as_usize(), kind: self.kind.stable(tables, cx) }583 }584}585586impl<'tcx> Stable<'tcx> for ty::trait_def::TraitSpecializationKind {587 type T = crate::ty::TraitSpecializationKind;588 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {589 use crate::ty::TraitSpecializationKind;590591 match self {592 ty::trait_def::TraitSpecializationKind::None => TraitSpecializationKind::None,593 ty::trait_def::TraitSpecializationKind::Marker => TraitSpecializationKind::Marker,594 ty::trait_def::TraitSpecializationKind::AlwaysApplicable => {595 TraitSpecializationKind::AlwaysApplicable596 }597 }598 }599}600601impl<'tcx> Stable<'tcx> for ty::TraitDef {602 type T = crate::ty::TraitDecl;603 fn stable<'cx>(604 &self,605 tables: &mut Tables<'cx, BridgeTys>,606 cx: &CompilerCtxt<'cx, BridgeTys>,607 ) -> Self::T {608 use crate::opaque;609 use crate::ty::TraitDecl;610611 TraitDecl {612 def_id: tables.trait_def(self.def_id),613 safety: self.safety.stable(tables, cx),614 paren_sugar: self.paren_sugar,615 has_auto_impl: self.has_auto_impl,616 is_marker: self.is_marker,617 is_coinductive: self.is_coinductive,618 skip_array_during_method_dispatch: self.skip_array_during_method_dispatch,619 skip_boxed_slice_during_method_dispatch: self.skip_boxed_slice_during_method_dispatch,620 specialization_kind: self.specialization_kind.stable(tables, cx),621 must_implement_one_of: self622 .must_implement_one_of623 .as_ref()624 .map(|idents| idents.iter().map(|ident| opaque(ident)).collect()),625 force_dyn_incompatible: self.force_dyn_incompatible.stable(tables, cx),626 deny_explicit_impl: self.deny_explicit_impl,627 }628 }629}630631impl<'tcx> Stable<'tcx> for ty::TraitRef<'tcx> {632 type T = crate::ty::TraitRef;633 fn stable<'cx>(634 &self,635 tables: &mut Tables<'cx, BridgeTys>,636 cx: &CompilerCtxt<'cx, BridgeTys>,637 ) -> Self::T {638 use crate::ty::TraitRef;639640 TraitRef::try_new(tables.trait_def(self.def_id), self.args.stable(tables, cx)).unwrap()641 }642}643644impl<'tcx> Stable<'tcx> for ty::Generics {645 type T = crate::ty::Generics;646647 fn stable<'cx>(648 &self,649 tables: &mut Tables<'cx, BridgeTys>,650 cx: &CompilerCtxt<'cx, BridgeTys>,651 ) -> Self::T {652 use crate::ty::Generics;653654 let params: Vec<_> = self.own_params.iter().map(|param| param.stable(tables, cx)).collect();655 let param_def_id_to_index =656 params.iter().map(|param| (param.def_id, param.index)).collect();657658 Generics {659 parent: self.parent.map(|did| tables.generic_def(did)),660 parent_count: self.parent_count,661 params,662 param_def_id_to_index,663 has_self: self.has_self,664 has_late_bound_regions: self665 .has_late_bound_regions666 .as_ref()667 .map(|late_bound_regions| late_bound_regions.stable(tables, cx)),668 }669 }670}671672impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDefKind {673 type T = crate::ty::GenericParamDefKind;674675 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {676 use crate::ty::GenericParamDefKind;677 match *self {678 ty::GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,679 ty::GenericParamDefKind::Type { has_default, synthetic } => {680 GenericParamDefKind::Type { has_default, synthetic }681 }682 ty::GenericParamDefKind::Const { has_default } => {683 GenericParamDefKind::Const { has_default }684 }685 }686 }687}688689impl<'tcx> Stable<'tcx> for rustc_middle::ty::GenericParamDef {690 type T = crate::ty::GenericParamDef;691692 fn stable<'cx>(693 &self,694 tables: &mut Tables<'cx, BridgeTys>,695 cx: &CompilerCtxt<'cx, BridgeTys>,696 ) -> Self::T {697 GenericParamDef {698 name: self.name.to_string(),699 def_id: tables.generic_def(self.def_id),700 index: self.index,701 pure_wrt_drop: self.pure_wrt_drop,702 kind: self.kind.stable(tables, cx),703 }704 }705}706707impl<'tcx> Stable<'tcx> for ty::PredicateKind<'tcx> {708 type T = crate::ty::PredicateKind;709710 fn stable<'cx>(711 &self,712 tables: &mut Tables<'cx, BridgeTys>,713 cx: &CompilerCtxt<'cx, BridgeTys>,714 ) -> Self::T {715 use rustc_middle::ty::PredicateKind;716 match self {717 PredicateKind::Clause(clause_kind) => {718 crate::ty::PredicateKind::Clause(clause_kind.stable(tables, cx))719 }720 PredicateKind::DynCompatible(did) => {721 crate::ty::PredicateKind::DynCompatible(tables.trait_def(*did))722 }723 PredicateKind::Subtype(subtype_predicate) => {724 crate::ty::PredicateKind::SubType(subtype_predicate.stable(tables, cx))725 }726 PredicateKind::Coerce(coerce_predicate) => {727 crate::ty::PredicateKind::Coerce(coerce_predicate.stable(tables, cx))728 }729 PredicateKind::ConstEquate(a, b) => {730 crate::ty::PredicateKind::ConstEquate(a.stable(tables, cx), b.stable(tables, cx))731 }732 PredicateKind::Ambiguous => crate::ty::PredicateKind::Ambiguous,733 PredicateKind::NormalizesTo(_pred) => unimplemented!(),734 PredicateKind::AliasRelate(a, b, alias_relation_direction) => {735 crate::ty::PredicateKind::AliasRelate(736 a.kind().stable(tables, cx),737 b.kind().stable(tables, cx),738 alias_relation_direction.stable(tables, cx),739 )740 }741 }742 }743}744745impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {746 type T = crate::ty::ClauseKind;747748 fn stable<'cx>(749 &self,750 tables: &mut Tables<'cx, BridgeTys>,751 cx: &CompilerCtxt<'cx, BridgeTys>,752 ) -> Self::T {753 use rustc_middle::ty::ClauseKind;754 match *self {755 ClauseKind::Trait(trait_object) => {756 crate::ty::ClauseKind::Trait(trait_object.stable(tables, cx))757 }758 ClauseKind::RegionOutlives(region_outlives) => {759 crate::ty::ClauseKind::RegionOutlives(region_outlives.stable(tables, cx))760 }761 ClauseKind::TypeOutlives(type_outlives) => {762 let ty::OutlivesPredicate::<_, _>(a, b) = type_outlives;763 crate::ty::ClauseKind::TypeOutlives(crate::ty::OutlivesPredicate(764 a.stable(tables, cx),765 b.stable(tables, cx),766 ))767 }768 ClauseKind::Projection(projection_predicate) => {769 crate::ty::ClauseKind::Projection(projection_predicate.stable(tables, cx))770 }771 ClauseKind::ConstArgHasType(const_, ty) => crate::ty::ClauseKind::ConstArgHasType(772 const_.stable(tables, cx),773 ty.stable(tables, cx),774 ),775 ClauseKind::WellFormed(term) => {776 crate::ty::ClauseKind::WellFormed(term.kind().stable(tables, cx))777 }778 ClauseKind::ConstEvaluatable(const_) => {779 crate::ty::ClauseKind::ConstEvaluatable(const_.stable(tables, cx))780 }781 ClauseKind::HostEffect(..) => {782 todo!()783 }784 ClauseKind::UnstableFeature(_) => {785 todo!()786 }787 }788 }789}790791impl<'tcx> Stable<'tcx> for ty::ClosureKind {792 type T = crate::ty::ClosureKind;793794 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {795 use rustc_middle::ty::ClosureKind::*;796 match self {797 Fn => crate::ty::ClosureKind::Fn,798 FnMut => crate::ty::ClosureKind::FnMut,799 FnOnce => crate::ty::ClosureKind::FnOnce,800 }801 }802}803804impl<'tcx> Stable<'tcx> for ty::SubtypePredicate<'tcx> {805 type T = crate::ty::SubtypePredicate;806807 fn stable<'cx>(808 &self,809 tables: &mut Tables<'cx, BridgeTys>,810 cx: &CompilerCtxt<'cx, BridgeTys>,811 ) -> Self::T {812 let ty::SubtypePredicate { a, b, a_is_expected: _ } = self;813 crate::ty::SubtypePredicate { a: a.stable(tables, cx), b: b.stable(tables, cx) }814 }815}816817impl<'tcx> Stable<'tcx> for ty::CoercePredicate<'tcx> {818 type T = crate::ty::CoercePredicate;819820 fn stable<'cx>(821 &self,822 tables: &mut Tables<'cx, BridgeTys>,823 cx: &CompilerCtxt<'cx, BridgeTys>,824 ) -> Self::T {825 let ty::CoercePredicate { a, b } = self;826 crate::ty::CoercePredicate { a: a.stable(tables, cx), b: b.stable(tables, cx) }827 }828}829830impl<'tcx> Stable<'tcx> for ty::AliasRelationDirection {831 type T = crate::ty::AliasRelationDirection;832833 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {834 use rustc_middle::ty::AliasRelationDirection::*;835 match self {836 Equate => crate::ty::AliasRelationDirection::Equate,837 Subtype => crate::ty::AliasRelationDirection::Subtype,838 }839 }840}841842impl<'tcx> Stable<'tcx> for ty::TraitPredicate<'tcx> {843 type T = crate::ty::TraitPredicate;844845 fn stable<'cx>(846 &self,847 tables: &mut Tables<'cx, BridgeTys>,848 cx: &CompilerCtxt<'cx, BridgeTys>,849 ) -> Self::T {850 let ty::TraitPredicate { trait_ref, polarity } = self;851 crate::ty::TraitPredicate {852 trait_ref: trait_ref.stable(tables, cx),853 polarity: polarity.stable(tables, cx),854 }855 }856}857858impl<'tcx, T> Stable<'tcx> for ty::OutlivesPredicate<'tcx, T>859where860 T: Stable<'tcx>,861{862 type T = crate::ty::OutlivesPredicate<T::T, Region>;863864 fn stable<'cx>(865 &self,866 tables: &mut Tables<'cx, BridgeTys>,867 cx: &CompilerCtxt<'cx, BridgeTys>,868 ) -> Self::T {869 let ty::OutlivesPredicate(a, b) = self;870 crate::ty::OutlivesPredicate(a.stable(tables, cx), b.stable(tables, cx))871 }872}873874impl<'tcx> Stable<'tcx> for ty::ProjectionPredicate<'tcx> {875 type T = crate::ty::ProjectionPredicate;876877 fn stable<'cx>(878 &self,879 tables: &mut Tables<'cx, BridgeTys>,880 cx: &CompilerCtxt<'cx, BridgeTys>,881 ) -> Self::T {882 let ty::ProjectionPredicate { projection_term, term } = self;883 crate::ty::ProjectionPredicate {884 projection_term: projection_term.stable(tables, cx),885 term: term.kind().stable(tables, cx),886 }887 }888}889890impl<'tcx> Stable<'tcx> for ty::ImplPolarity {891 type T = crate::ty::ImplPolarity;892893 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {894 use rustc_middle::ty::ImplPolarity::*;895 match self {896 Positive => crate::ty::ImplPolarity::Positive,897 Negative => crate::ty::ImplPolarity::Negative,898 Reservation => crate::ty::ImplPolarity::Reservation,899 }900 }901}902903impl<'tcx> Stable<'tcx> for ty::PredicatePolarity {904 type T = crate::ty::PredicatePolarity;905906 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {907 use rustc_middle::ty::PredicatePolarity::*;908 match self {909 Positive => crate::ty::PredicatePolarity::Positive,910 Negative => crate::ty::PredicatePolarity::Negative,911 }912 }913}914915impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {916 type T = crate::ty::Region;917918 fn stable<'cx>(919 &self,920 tables: &mut Tables<'cx, BridgeTys>,921 cx: &CompilerCtxt<'cx, BridgeTys>,922 ) -> Self::T {923 Region { kind: self.kind().stable(tables, cx) }924 }925}926927impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> {928 type T = crate::ty::RegionKind;929930 fn stable<'cx>(931 &self,932 tables: &mut Tables<'cx, BridgeTys>,933 cx: &CompilerCtxt<'cx, BridgeTys>,934 ) -> Self::T {935 use crate::ty::{BoundRegion, EarlyParamRegion, RegionKind};936 match self {937 ty::ReEarlyParam(early_reg) => RegionKind::ReEarlyParam(EarlyParamRegion {938 index: early_reg.index,939 name: early_reg.name.to_string(),940 }),941 ty::ReBound(ty::BoundVarIndexKind::Bound(db_index), bound_reg) => RegionKind::ReBound(942 db_index.as_u32(),943 BoundRegion {944 var: bound_reg.var.as_u32(),945 kind: bound_reg.kind.stable(tables, cx),946 },947 ),948 ty::ReStatic => RegionKind::ReStatic,949 ty::RePlaceholder(place_holder) => RegionKind::RePlaceholder(crate::ty::Placeholder {950 universe: place_holder.universe.as_u32(),951 bound: BoundRegion {952 var: place_holder.bound.var.as_u32(),953 kind: place_holder.bound.kind.stable(tables, cx),954 },955 }),956 ty::ReErased => RegionKind::ReErased,957 _ => unreachable!("{self:?}"),958 }959 }960}961962impl<'tcx> Stable<'tcx> for ty::Instance<'tcx> {963 type T = crate::mir::mono::Instance;964965 fn stable<'cx>(966 &self,967 tables: &mut Tables<'cx, BridgeTys>,968 cx: &CompilerCtxt<'cx, BridgeTys>,969 ) -> Self::T {970 let def = tables.instance_def(cx.lift(*self));971 let kind = match self.def {972 ty::InstanceKind::Item(..) => crate::mir::mono::InstanceKind::Item,973 ty::InstanceKind::Intrinsic(..) => crate::mir::mono::InstanceKind::Intrinsic,974 ty::InstanceKind::Virtual(_def_id, idx) => {975 crate::mir::mono::InstanceKind::Virtual { idx }976 }977 ty::InstanceKind::VTableShim(..)978 | ty::InstanceKind::ReifyShim(..)979 | ty::InstanceKind::FnPtrAddrShim(..)980 | ty::InstanceKind::ClosureOnceShim { .. }981 | ty::InstanceKind::ConstructCoroutineInClosureShim { .. }982 | ty::InstanceKind::ThreadLocalShim(..)983 | ty::InstanceKind::DropGlue(..)984 | ty::InstanceKind::CloneShim(..)985 | ty::InstanceKind::FnPtrShim(..)986 | ty::InstanceKind::FutureDropPollShim(..)987 | ty::InstanceKind::AsyncDropGlue(..)988 | ty::InstanceKind::AsyncDropGlueCtorShim(..) => crate::mir::mono::InstanceKind::Shim,989 };990 crate::mir::mono::Instance { def, kind }991 }992}993994impl<'tcx> Stable<'tcx> for ty::Variance {995 type T = crate::mir::Variance;996 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {997 match self {998 ty::Bivariant => crate::mir::Variance::Bivariant,999 ty::Contravariant => crate::mir::Variance::Contravariant,1000 ty::Covariant => crate::mir::Variance::Covariant,1001 ty::Invariant => crate::mir::Variance::Invariant,1002 }1003 }1004}10051006impl<'tcx> Stable<'tcx> for ty::Movability {1007 type T = crate::ty::Movability;10081009 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {1010 match self {1011 ty::Movability::Static => crate::ty::Movability::Static,1012 ty::Movability::Movable => crate::ty::Movability::Movable,1013 }1014 }1015}10161017impl<'tcx> Stable<'tcx> for rustc_abi::ExternAbi {1018 type T = crate::ty::Abi;10191020 fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {1021 use rustc_abi::ExternAbi;10221023 use crate::ty::Abi;1024 match *self {1025 ExternAbi::Rust => Abi::Rust,1026 ExternAbi::C { unwind } => Abi::C { unwind },1027 ExternAbi::Cdecl { unwind } => Abi::Cdecl { unwind },1028 ExternAbi::Stdcall { unwind } => Abi::Stdcall { unwind },1029 ExternAbi::Fastcall { unwind } => Abi::Fastcall { unwind },1030 ExternAbi::Vectorcall { unwind } => Abi::Vectorcall { unwind },1031 ExternAbi::Thiscall { unwind } => Abi::Thiscall { unwind },1032 ExternAbi::Aapcs { unwind } => Abi::Aapcs { unwind },1033 ExternAbi::Win64 { unwind } => Abi::Win64 { unwind },1034 ExternAbi::SysV64 { unwind } => Abi::SysV64 { unwind },1035 ExternAbi::PtxKernel => Abi::PtxKernel,1036 ExternAbi::GpuKernel => Abi::GpuKernel,1037 ExternAbi::Msp430Interrupt => Abi::Msp430Interrupt,1038 ExternAbi::X86Interrupt => Abi::X86Interrupt,1039 ExternAbi::EfiApi => Abi::EfiApi,1040 ExternAbi::AvrInterrupt => Abi::AvrInterrupt,1041 ExternAbi::AvrNonBlockingInterrupt => Abi::AvrNonBlockingInterrupt,1042 ExternAbi::CmseNonSecureCall => Abi::CCmseNonSecureCall,1043 ExternAbi::CmseNonSecureEntry => Abi::CCmseNonSecureEntry,1044 ExternAbi::System { unwind } => Abi::System { unwind },1045 ExternAbi::RustCall => Abi::RustCall,1046 ExternAbi::Unadjusted => Abi::Unadjusted,1047 ExternAbi::RustCold => Abi::RustCold,1048 ExternAbi::RustPreserveNone => Abi::RustPreserveNone,1049 ExternAbi::RustInvalid => Abi::RustInvalid,1050 ExternAbi::RiscvInterruptM => Abi::RiscvInterruptM,1051 ExternAbi::RiscvInterruptS => Abi::RiscvInterruptS,1052 ExternAbi::Custom => Abi::Custom,1053 }1054 }1055}10561057impl<'tcx> Stable<'tcx> for rustc_session::cstore::ForeignModule {1058 type T = crate::ty::ForeignModule;10591060 fn stable<'cx>(1061 &self,1062 tables: &mut Tables<'cx, BridgeTys>,1063 cx: &CompilerCtxt<'cx, BridgeTys>,1064 ) -> Self::T {1065 crate::ty::ForeignModule {1066 def_id: tables.foreign_module_def(self.def_id),1067 abi: self.abi.stable(tables, cx),1068 }1069 }1070}10711072impl<'tcx> Stable<'tcx> for ty::AssocKind {1073 type T = crate::ty::AssocKind;10741075 fn stable<'cx>(1076 &self,1077 tables: &mut Tables<'cx, BridgeTys>,1078 cx: &CompilerCtxt<'cx, BridgeTys>,1079 ) -> Self::T {1080 use crate::ty::{AssocKind, AssocTypeData};1081 match *self {1082 ty::AssocKind::Const { name, .. } => AssocKind::Const { name: name.to_string() },1083 ty::AssocKind::Fn { name, has_self } => {1084 AssocKind::Fn { name: name.to_string(), has_self }1085 }1086 ty::AssocKind::Type { data } => AssocKind::Type {1087 data: match data {1088 ty::AssocTypeData::Normal(name) => AssocTypeData::Normal(name.to_string()),1089 ty::AssocTypeData::Rpitit(rpitit) => {1090 AssocTypeData::Rpitit(rpitit.stable(tables, cx))1091 }1092 },1093 },1094 }1095 }1096}10971098impl<'tcx> Stable<'tcx> for ty::AssocContainer {1099 type T = crate::ty::AssocContainer;11001101 fn stable(1102 &self,1103 tables: &mut Tables<'_, BridgeTys>,1104 _: &CompilerCtxt<'_, BridgeTys>,1105 ) -> Self::T {1106 use crate::ty::AssocContainer;1107 match self {1108 ty::AssocContainer::Trait => AssocContainer::Trait,1109 ty::AssocContainer::InherentImpl => AssocContainer::InherentImpl,1110 ty::AssocContainer::TraitImpl(trait_item_id) => {1111 AssocContainer::TraitImpl(tables.assoc_def(trait_item_id.unwrap()))1112 }1113 }1114 }1115}11161117impl<'tcx> Stable<'tcx> for ty::AssocItem {1118 type T = crate::ty::AssocItem;11191120 fn stable<'cx>(1121 &self,1122 tables: &mut Tables<'cx, BridgeTys>,1123 cx: &CompilerCtxt<'cx, BridgeTys>,1124 ) -> Self::T {1125 crate::ty::AssocItem {1126 def_id: tables.assoc_def(self.def_id),1127 kind: self.kind.stable(tables, cx),1128 container: self.container.stable(tables, cx),1129 }1130 }1131}11321133impl<'tcx> Stable<'tcx> for ty::ImplTraitInTraitData {1134 type T = crate::ty::ImplTraitInTraitData;11351136 fn stable<'cx>(1137 &self,1138 tables: &mut Tables<'cx, BridgeTys>,1139 _: &CompilerCtxt<'cx, BridgeTys>,1140 ) -> Self::T {1141 use crate::ty::ImplTraitInTraitData;1142 match self {1143 ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id } => {1144 ImplTraitInTraitData::Trait {1145 fn_def_id: tables.fn_def(*fn_def_id),1146 opaque_def_id: tables.opaque_def(*opaque_def_id),1147 }1148 }1149 ty::ImplTraitInTraitData::Impl { fn_def_id } => {1150 ImplTraitInTraitData::Impl { fn_def_id: tables.fn_def(*fn_def_id) }1151 }1152 }1153 }1154}11551156impl<'tcx> Stable<'tcx> for rustc_middle::ty::util::Discr<'tcx> {1157 type T = crate::ty::Discr;11581159 fn stable<'cx>(1160 &self,1161 tables: &mut Tables<'cx, BridgeTys>,1162 cx: &CompilerCtxt<'cx, BridgeTys>,1163 ) -> Self::T {1164 crate::ty::Discr { val: self.val, ty: self.ty.stable(tables, cx) }1165 }1166}11671168impl<'tcx> Stable<'tcx> for rustc_middle::ty::VtblEntry<'tcx> {1169 type T = crate::ty::VtblEntry;11701171 fn stable<'cx>(1172 &self,1173 tables: &mut Tables<'cx, BridgeTys>,1174 cx: &CompilerCtxt<'cx, BridgeTys>,1175 ) -> Self::T {1176 use crate::ty::VtblEntry;1177 match self {1178 ty::VtblEntry::MetadataDropInPlace => VtblEntry::MetadataDropInPlace,1179 ty::VtblEntry::MetadataSize => VtblEntry::MetadataSize,1180 ty::VtblEntry::MetadataAlign => VtblEntry::MetadataAlign,1181 ty::VtblEntry::Vacant => VtblEntry::Vacant,1182 ty::VtblEntry::Method(instance) => VtblEntry::Method(instance.stable(tables, cx)),1183 ty::VtblEntry::TraitVPtr(trait_ref) => {1184 VtblEntry::TraitVPtr(trait_ref.stable(tables, cx))1185 }1186 }1187 }1188}