Skip to content

Commit

Permalink
Shrink some internal enums
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Nov 12, 2018
1 parent 6fc8779 commit 19ae2b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/librustc/mir/mod.rs
Expand Up @@ -1939,7 +1939,7 @@ pub enum ProjectionElem<'tcx, V, T> {
/// "Downcast" to a variant of an ADT. Currently, we only introduce
/// this for ADTs with more than one variant. It may be better to
/// just introduce it always, or always for enums.
Downcast(&'tcx AdtDef, usize),
Downcast(&'tcx AdtDef, u32),
}

/// Alias for projections as they appear in places, where the base is a place
Expand All @@ -1950,6 +1950,11 @@ pub type PlaceProjection<'tcx> = Projection<'tcx, Place<'tcx>, Local, Ty<'tcx>>;
/// and the index is a local.
pub type PlaceElem<'tcx> = ProjectionElem<'tcx, Local, Ty<'tcx>>;

// at least on 64 bit systems, `PlaceElem` should not be larger than two pointers
static_assert!(PROJECTION_ELEM_IS_2_PTRS_LARGE:
mem::size_of::<PlaceElem<'_>>() <= 16
);

/// Alias for projections as they appear in `UserTypeProjection`, where we
/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
pub type ProjectionKind<'tcx> = ProjectionElem<'tcx, (), ()>;
Expand All @@ -1970,7 +1975,7 @@ impl<'tcx> Place<'tcx> {
}

pub fn downcast(self, adt_def: &'tcx AdtDef, variant_index: usize) -> Place<'tcx> {
self.elem(ProjectionElem::Downcast(adt_def, variant_index))
self.elem(ProjectionElem::Downcast(adt_def, variant_index as u32))
}

pub fn index(self, index: Local) -> Place<'tcx> {
Expand Down Expand Up @@ -2021,7 +2026,7 @@ impl<'tcx> Debug for Place<'tcx> {
Promoted(ref promoted) => write!(fmt, "({:?}: {:?})", promoted.0, promoted.1),
Projection(ref data) => match data.elem {
ProjectionElem::Downcast(ref adt_def, index) => {
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].name)
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index as usize].name)
}
ProjectionElem::Deref => write!(fmt, "(*{:?})", data.base),
ProjectionElem::Field(field, ty) => {
Expand Down
10 changes: 7 additions & 3 deletions src/librustc/mir/tcx.rs
Expand Up @@ -27,9 +27,13 @@ pub enum PlaceTy<'tcx> {
/// Downcast to a particular variant of an enum.
Downcast { adt_def: &'tcx AdtDef,
substs: &'tcx Substs<'tcx>,
variant_index: usize },
variant_index: u32 },
}

static_assert!(PLACE_TY_IS_3_PTRS_LARGE:
mem::size_of::<PlaceTy<'_>>() <= 24
);

impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
pub fn from_ty(ty: Ty<'tcx>) -> PlaceTy<'tcx> {
PlaceTy::Ty { ty }
Expand Down Expand Up @@ -58,7 +62,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
(PlaceTy::Ty {
ty: &ty::TyS { sty: ty::TyKind::Adt(adt_def, substs), .. } }, variant_index) |
(PlaceTy::Downcast { adt_def, substs, variant_index }, _) => {
let variant_def = &adt_def.variants[variant_index];
let variant_def = &adt_def.variants[variant_index as usize];
let field_def = &variant_def.fields[f.index()];
field_def.ty(tcx, substs)
}
Expand Down Expand Up @@ -134,7 +138,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
match self.to_ty(tcx).sty {
ty::Adt(adt_def, substs) => {
assert!(adt_def.is_enum());
assert!(index < adt_def.variants.len());
assert!(index < adt_def.variants.len() as u32);
assert_eq!(adt_def, adt_def1);
PlaceTy::Downcast { adt_def,
substs,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/mod.rs
Expand Up @@ -1801,6 +1801,7 @@ pub struct FieldDef {
/// table.
pub struct AdtDef {
pub did: DefId,
// TODO: make this an IndexVec
pub variants: Vec<VariantDef>,
flags: AdtFlags,
pub repr: ReprOptions,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -33,6 +33,7 @@ CloneTypeFoldableAndLiftImpls! {
(),
bool,
usize,
u32,
u64,
::middle::region::Scope,
::syntax::ast::FloatTy,
Expand Down

0 comments on commit 19ae2b9

Please sign in to comment.