diff options
| author | Lukas Markeffsky <@> | 2025-02-09 18:16:17 +0100 |
|---|---|---|
| committer | Lukas Markeffsky <@> | 2025-02-13 00:39:03 +0100 |
| commit | b722d5da1d9b9eb86b1bdb5d9e7a820d035b0b23 (patch) | |
| tree | 7f0460a3c2d16f9e787486daba0da2e23094d349 | |
| parent | 885e0f1b96112aae9fbdd97d08b69ddc8eb01b1e (diff) | |
| download | rust-b722d5da1d9b9eb86b1bdb5d9e7a820d035b0b23.tar.gz rust-b722d5da1d9b9eb86b1bdb5d9e7a820d035b0b23.zip | |
simplify valtree branches construction
| -rw-r--r-- | compiler/rustc_const_eval/src/const_eval/valtrees.rs | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/valtrees.rs b/compiler/rustc_const_eval/src/const_eval/valtrees.rs index 27599bdcd15..3776fb55c2e 100644 --- a/compiler/rustc_const_eval/src/const_eval/valtrees.rs +++ b/compiler/rustc_const_eval/src/const_eval/valtrees.rs @@ -21,7 +21,7 @@ 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> { @@ -29,24 +29,21 @@ fn branches<'tcx>( 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 { |
