Skip to content

Commit f9b8e6b

Browse files
authored
Rollup merge of #114044 - ericmarkmartin:more-stable-impls, r=spastorino
factor out more stable impls I saw some logic must resembling stable impls and thought it might be nice to factor it out r? `@spastorino`
2 parents 0b13deb + 0a0ce49 commit f9b8e6b

File tree

1 file changed

+110
-55
lines changed
  • compiler/rustc_smir/src/rustc_smir

1 file changed

+110
-55
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+110-55
Original file line numberDiff line numberDiff line change
@@ -575,19 +575,22 @@ impl<'tcx> Stable<'tcx> for mir::Terminator<'tcx> {
575575
impl<'tcx> Stable<'tcx> for ty::GenericArgs<'tcx> {
576576
type T = stable_mir::ty::GenericArgs;
577577
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
578-
use stable_mir::ty::{GenericArgKind, GenericArgs};
579-
580-
GenericArgs(
581-
self.iter()
582-
.map(|arg| match arg.unpack() {
583-
ty::GenericArgKind::Lifetime(region) => {
584-
GenericArgKind::Lifetime(opaque(&region))
585-
}
586-
ty::GenericArgKind::Type(ty) => GenericArgKind::Type(tables.intern_ty(ty)),
587-
ty::GenericArgKind::Const(const_) => GenericArgKind::Const(opaque(&const_)),
588-
})
589-
.collect(),
590-
)
578+
use stable_mir::ty::GenericArgs;
579+
580+
GenericArgs(self.iter().map(|arg| arg.unpack().stable(tables)).collect())
581+
}
582+
}
583+
584+
impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> {
585+
type T = stable_mir::ty::GenericArgKind;
586+
587+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
588+
use stable_mir::ty::GenericArgKind;
589+
match self {
590+
ty::GenericArgKind::Lifetime(region) => GenericArgKind::Lifetime(opaque(region)),
591+
ty::GenericArgKind::Type(ty) => GenericArgKind::Type(tables.intern_ty(*ty)),
592+
ty::GenericArgKind::Const(const_) => GenericArgKind::Const(opaque(&const_)),
593+
}
591594
}
592595
}
593596

@@ -659,63 +662,118 @@ impl<'tcx> Stable<'tcx> for ty::FnSig<'tcx> {
659662
}
660663
}
661664

665+
impl<'tcx> Stable<'tcx> for ty::BoundTyKind {
666+
type T = stable_mir::ty::BoundTyKind;
667+
668+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
669+
use stable_mir::ty::BoundTyKind;
670+
671+
match self {
672+
ty::BoundTyKind::Anon => BoundTyKind::Anon,
673+
ty::BoundTyKind::Param(def_id, symbol) => {
674+
BoundTyKind::Param(rustc_internal::param_def(*def_id), symbol.to_string())
675+
}
676+
}
677+
}
678+
}
679+
680+
impl<'tcx> Stable<'tcx> for ty::BoundRegionKind {
681+
type T = stable_mir::ty::BoundRegionKind;
682+
683+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
684+
use stable_mir::ty::BoundRegionKind;
685+
686+
match self {
687+
ty::BoundRegionKind::BrAnon(option_span) => {
688+
BoundRegionKind::BrAnon(option_span.map(|span| opaque(&span)))
689+
}
690+
ty::BoundRegionKind::BrNamed(def_id, symbol) => {
691+
BoundRegionKind::BrNamed(rustc_internal::br_named_def(*def_id), symbol.to_string())
692+
}
693+
ty::BoundRegionKind::BrEnv => BoundRegionKind::BrEnv,
694+
}
695+
}
696+
}
697+
662698
impl<'tcx> Stable<'tcx> for ty::BoundVariableKind {
663699
type T = stable_mir::ty::BoundVariableKind;
664-
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
665-
use stable_mir::ty::{BoundRegionKind, BoundTyKind, BoundVariableKind};
700+
701+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
702+
use stable_mir::ty::BoundVariableKind;
666703

667704
match self {
668705
ty::BoundVariableKind::Ty(bound_ty_kind) => {
669-
BoundVariableKind::Ty(match bound_ty_kind {
670-
ty::BoundTyKind::Anon => BoundTyKind::Anon,
671-
ty::BoundTyKind::Param(def_id, symbol) => {
672-
BoundTyKind::Param(rustc_internal::param_def(*def_id), symbol.to_string())
673-
}
674-
})
706+
BoundVariableKind::Ty(bound_ty_kind.stable(tables))
675707
}
676708
ty::BoundVariableKind::Region(bound_region_kind) => {
677-
BoundVariableKind::Region(match bound_region_kind {
678-
ty::BoundRegionKind::BrAnon(option_span) => {
679-
BoundRegionKind::BrAnon(option_span.map(|span| opaque(&span)))
680-
}
681-
ty::BoundRegionKind::BrNamed(def_id, symbol) => BoundRegionKind::BrNamed(
682-
rustc_internal::br_named_def(*def_id),
683-
symbol.to_string(),
684-
),
685-
ty::BoundRegionKind::BrEnv => BoundRegionKind::BrEnv,
686-
})
709+
BoundVariableKind::Region(bound_region_kind.stable(tables))
687710
}
688711
ty::BoundVariableKind::Const => BoundVariableKind::Const,
689712
}
690713
}
691714
}
692715

716+
impl<'tcx> Stable<'tcx> for ty::IntTy {
717+
type T = IntTy;
718+
719+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
720+
match self {
721+
ty::IntTy::Isize => IntTy::Isize,
722+
ty::IntTy::I8 => IntTy::I8,
723+
ty::IntTy::I16 => IntTy::I16,
724+
ty::IntTy::I32 => IntTy::I32,
725+
ty::IntTy::I64 => IntTy::I64,
726+
ty::IntTy::I128 => IntTy::I128,
727+
}
728+
}
729+
}
730+
731+
impl<'tcx> Stable<'tcx> for ty::UintTy {
732+
type T = UintTy;
733+
734+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
735+
match self {
736+
ty::UintTy::Usize => UintTy::Usize,
737+
ty::UintTy::U8 => UintTy::U8,
738+
ty::UintTy::U16 => UintTy::U16,
739+
ty::UintTy::U32 => UintTy::U32,
740+
ty::UintTy::U64 => UintTy::U64,
741+
ty::UintTy::U128 => UintTy::U128,
742+
}
743+
}
744+
}
745+
746+
impl<'tcx> Stable<'tcx> for ty::FloatTy {
747+
type T = FloatTy;
748+
749+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
750+
match self {
751+
ty::FloatTy::F32 => FloatTy::F32,
752+
ty::FloatTy::F64 => FloatTy::F64,
753+
}
754+
}
755+
}
756+
757+
impl<'tcx> Stable<'tcx> for hir::Movability {
758+
type T = Movability;
759+
760+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
761+
match self {
762+
hir::Movability::Static => Movability::Static,
763+
hir::Movability::Movable => Movability::Movable,
764+
}
765+
}
766+
}
767+
693768
impl<'tcx> Stable<'tcx> for Ty<'tcx> {
694769
type T = stable_mir::ty::TyKind;
695770
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
696771
match self.kind() {
697772
ty::Bool => TyKind::RigidTy(RigidTy::Bool),
698773
ty::Char => TyKind::RigidTy(RigidTy::Char),
699-
ty::Int(int_ty) => match int_ty {
700-
ty::IntTy::Isize => TyKind::RigidTy(RigidTy::Int(IntTy::Isize)),
701-
ty::IntTy::I8 => TyKind::RigidTy(RigidTy::Int(IntTy::I8)),
702-
ty::IntTy::I16 => TyKind::RigidTy(RigidTy::Int(IntTy::I16)),
703-
ty::IntTy::I32 => TyKind::RigidTy(RigidTy::Int(IntTy::I32)),
704-
ty::IntTy::I64 => TyKind::RigidTy(RigidTy::Int(IntTy::I64)),
705-
ty::IntTy::I128 => TyKind::RigidTy(RigidTy::Int(IntTy::I128)),
706-
},
707-
ty::Uint(uint_ty) => match uint_ty {
708-
ty::UintTy::Usize => TyKind::RigidTy(RigidTy::Uint(UintTy::Usize)),
709-
ty::UintTy::U8 => TyKind::RigidTy(RigidTy::Uint(UintTy::U8)),
710-
ty::UintTy::U16 => TyKind::RigidTy(RigidTy::Uint(UintTy::U16)),
711-
ty::UintTy::U32 => TyKind::RigidTy(RigidTy::Uint(UintTy::U32)),
712-
ty::UintTy::U64 => TyKind::RigidTy(RigidTy::Uint(UintTy::U64)),
713-
ty::UintTy::U128 => TyKind::RigidTy(RigidTy::Uint(UintTy::U128)),
714-
},
715-
ty::Float(float_ty) => match float_ty {
716-
ty::FloatTy::F32 => TyKind::RigidTy(RigidTy::Float(FloatTy::F32)),
717-
ty::FloatTy::F64 => TyKind::RigidTy(RigidTy::Float(FloatTy::F64)),
718-
},
774+
ty::Int(int_ty) => TyKind::RigidTy(RigidTy::Int(int_ty.stable(tables))),
775+
ty::Uint(uint_ty) => TyKind::RigidTy(RigidTy::Uint(uint_ty.stable(tables))),
776+
ty::Float(float_ty) => TyKind::RigidTy(RigidTy::Float(float_ty.stable(tables))),
719777
ty::Adt(adt_def, generic_args) => TyKind::RigidTy(RigidTy::Adt(
720778
rustc_internal::adt_def(adt_def.did()),
721779
generic_args.stable(tables),
@@ -758,10 +816,7 @@ impl<'tcx> Stable<'tcx> for Ty<'tcx> {
758816
ty::Generator(def_id, generic_args, movability) => TyKind::RigidTy(RigidTy::Generator(
759817
rustc_internal::generator_def(*def_id),
760818
generic_args.stable(tables),
761-
match movability {
762-
hir::Movability::Static => Movability::Static,
763-
hir::Movability::Movable => Movability::Movable,
764-
},
819+
movability.stable(tables),
765820
)),
766821
ty::Never => TyKind::RigidTy(RigidTy::Never),
767822
ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple(

0 commit comments

Comments
 (0)