Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_transmute: fix big-endian discriminants #102076

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions compiler/rustc_transmute/src/layout/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub(crate) mod rustc {
.unwrap();
trace!(?discr_layout, "computed discriminant layout");
variant_layout = variant_layout.extend(discr_layout).unwrap().0;
tree = tree.then(Self::from_disr(discr, tcx, layout_summary.discriminant_size));
tree = tree.then(Self::from_discr(discr, tcx, layout_summary.discriminant_size));
}

// Next come fields.
Expand Down Expand Up @@ -444,11 +444,21 @@ pub(crate) mod rustc {
Ok(tree)
}

pub fn from_disr(discr: Discr<'tcx>, tcx: TyCtxt<'tcx>, size: usize) -> Self {
// FIXME(@jswrenn): I'm certain this is missing needed endian nuance.
let bytes = discr.val.to_ne_bytes();
let bytes = &bytes[..size];
Self::Seq(bytes.into_iter().copied().map(|b| Self::from_bits(b)).collect())
pub fn from_discr(discr: Discr<'tcx>, tcx: TyCtxt<'tcx>, size: usize) -> Self {
use rustc_target::abi::Endian;

let bytes: [u8; 16];
let bytes = match tcx.data_layout.endian {
Endian::Little => {
bytes = discr.val.to_le_bytes();
&bytes[..size]
}
Endian::Big => {
bytes = discr.val.to_be_bytes();
&bytes[bytes.len() - size..]
}
};
Self::Seq(bytes.iter().map(|&b| Self::from_bits(b)).collect())
}
}

Expand Down