From 473d0c551006f838f69dfadcc1965492849cfc79 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 1 Feb 2023 18:59:27 +0000 Subject: [PATCH] Introduce write_aggregate. --- .../rustc_const_eval/src/interpret/place.rs | 28 +++++++++++++++++++ .../rustc_const_eval/src/interpret/step.rs | 20 +------------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 5c00dc21d04..8d4d0420cda 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -846,6 +846,34 @@ pub fn write_discriminant( Ok(()) } + /// Writes the discriminant of the given variant. + #[instrument(skip(self), level = "debug")] + pub fn write_aggregate( + &mut self, + kind: &mir::AggregateKind<'tcx>, + operands: &[mir::Operand<'tcx>], + dest: &PlaceTy<'tcx, M::Provenance>, + ) -> InterpResult<'tcx> { + self.write_uninit(&dest)?; + let (variant_index, variant_dest, active_field_index) = match *kind { + mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => { + let variant_dest = self.place_downcast(&dest, variant_index)?; + (variant_index, variant_dest, active_field_index) + } + _ => (VariantIdx::from_u32(0), dest.clone(), None), + }; + if active_field_index.is_some() { + assert_eq!(operands.len(), 1); + } + for (field_index, operand) in operands.iter().enumerate() { + let field_index = active_field_index.unwrap_or(field_index); + let field_dest = self.place_field(&variant_dest, field_index)?; + let op = self.eval_operand(operand, Some(field_dest.layout))?; + self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?; + } + self.write_discriminant(variant_index, &dest) + } + pub fn raw_const_to_mplace( &self, raw: ConstAlloc<'tcx>, diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index 7e00d90342e..7d9a98da08a 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -7,7 +7,6 @@ use rustc_middle::mir; use rustc_middle::mir::interpret::{InterpResult, Scalar}; use rustc_middle::ty::layout::LayoutOf; -use rustc_target::abi::VariantIdx; use super::{ImmTy, InterpCx, Machine}; @@ -200,24 +199,7 @@ pub fn eval_rvalue_into_place( } Aggregate(box ref kind, ref operands) => { - self.write_uninit(&dest)?; - let (variant_index, variant_dest, active_field_index) = match *kind { - mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => { - let variant_dest = self.place_downcast(&dest, variant_index)?; - (variant_index, variant_dest, active_field_index) - } - _ => (VariantIdx::from_u32(0), dest.clone(), None), - }; - if active_field_index.is_some() { - assert_eq!(operands.len(), 1); - } - for (field_index, operand) in operands.iter().enumerate() { - let field_index = active_field_index.unwrap_or(field_index); - let field_dest = self.place_field(&variant_dest, field_index)?; - let op = self.eval_operand(operand, Some(field_dest.layout))?; - self.copy_op(&op, &field_dest, /*allow_transmute*/ false)?; - } - self.write_discriminant(variant_index, &dest)?; + self.write_aggregate(kind, operands, &dest)?; } Repeat(ref operand, _) => { -- 2.44.0