Skip to content

Commit

Permalink
simplify valtree branches construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Feb 11, 2025
1 parent 9ace513 commit 88beb48
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions compiler/rustc_const_eval/src/const_eval/valtrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@ use crate::interpret::{
fn branches<'tcx>(
ecx: &CompileTimeInterpCx<'tcx>,
place: &MPlaceTy<'tcx>,
n: usize,
field_count: usize,
variant: Option<VariantIdx>,
num_nodes: &mut usize,
) -> ValTreeCreationResult<'tcx> {
let place = match variant {
Some(variant) => ecx.project_downcast(place, variant).unwrap(),
None => place.clone(),
};
let variant =
variant.map(|variant| Some(ty::ValTree::from_scalar_int(*ecx.tcx, variant.as_u32().into())));
debug!(?place, ?variant);
debug!(?place);

let mut fields = Vec::with_capacity(n);
for i in 0..n {
let field = ecx.project_field(&place, i).unwrap();
let valtree = const_to_valtree_inner(ecx, &field, num_nodes)?;
fields.push(Some(valtree));
}
let mut branches = Vec::with_capacity(field_count + variant.is_some() as usize);

// For enums, we prepend their variant index before the variant's fields so we can figure out
// the variant again when just seeing a valtree.
let branches = variant
.into_iter()
.chain(fields.into_iter())
.collect::<Option<Vec<_>>>()
.expect("should have already checked for errors in ValTree creation");
if let Some(variant) = variant {
branches.push(ty::ValTree::from_scalar_int(*ecx.tcx, variant.as_u32().into()));
}

for i in 0..field_count {
let field = ecx.project_field(&place, i).unwrap();
let valtree = const_to_valtree_inner(ecx, &field, num_nodes)?;
branches.push(valtree);
}

// Have to account for ZSTs here
if branches.len() == 0 {
Expand Down

0 comments on commit 88beb48

Please sign in to comment.