Skip to content

Commit

Permalink
Export discriminant values in AdtDef
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed May 31, 2024
1 parent a483aff commit 44a41bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions frontend/exporter/src/index_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ impl<I: rustc_index::Idx, T: Sized> std::ops::DerefMut for IndexVec<I, T> {
}
}

impl<I: rustc_index::Idx, T: Sized> IndexVec<I, T> {
pub fn into_iter_enumerated(
self,
) -> impl DoubleEndedIterator<Item = (I, T)> + ExactSizeIterator {
rustc_index::IndexVec::from_raw(self.raw).into_iter_enumerated()
}
}

impl<
S,
J: rustc_index::Idx,
Expand Down
27 changes: 27 additions & 0 deletions frontend/exporter/src/types/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,14 @@ pub enum VariantDiscr {
Relative(u32),
}

/// Reflects [`rustc_middle::ty::util::Discr`]
#[derive(AdtInto, Clone, Debug, Serialize, Deserialize, JsonSchema)]
#[args(<'tcx, S: UnderOwnerState<'tcx>>, from: rustc_middle::ty::util::Discr<'tcx>, state: S as gstate)]
pub struct DiscriminantValue {
pub val: u128,
pub ty: Ty,
}

/// Reflects [`rustc_middle::ty::Visibility`]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub enum Visibility<Id = rustc_span::def_id::LocalDefId> {
Expand Down Expand Up @@ -1809,6 +1817,7 @@ pub struct AdtDef {
pub did: DefId,
pub adt_kind: AdtKind,
pub variants: IndexVec<VariantIdx, VariantDef>,
pub discriminants: IndexVec<VariantIdx, DiscriminantValue>,
pub flags: AdtFlags,
pub repr: ReprOptions,
}
Expand All @@ -1831,10 +1840,28 @@ pub struct ReprOptions {

impl<'tcx, S: UnderOwnerState<'tcx>> SInto<S, AdtDef> for rustc_middle::ty::AdtDef<'tcx> {
fn sinto(&self, s: &S) -> AdtDef {
let discriminants: Vec<_> = if self.is_enum() {
self.variants()
.iter_enumerated()
.map(|(variant_idx, _variant)| {
self.discriminant_for_variant(s.base().tcx, variant_idx)
})
.collect()
} else {
// Structs and unions have a single variant.
assert_eq!(self.variants().len(), 1);
vec![rustc_middle::ty::util::Discr {
val: 0,
ty: s.base().tcx.types.isize,
}]
};
let discriminants: rustc_index::IndexVec<VariantIdx, _> =
rustc_index::IndexVec::from_raw(discriminants);
AdtDef {
did: self.did().sinto(s),
adt_kind: self.adt_kind().sinto(s),
variants: self.variants().sinto(s),
discriminants: discriminants.sinto(s),
flags: self.flags().sinto(s),
repr: self.repr().sinto(s),
}
Expand Down

0 comments on commit 44a41bd

Please sign in to comment.