use rustc::ty::{self, TyCtxt};
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
-use crate::interpret::{intern_const_alloc_recursive, ConstValue, InterpCx};
+use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx};
mod error;
mod eval_queries;
let loc_ty = tcx.caller_location_ty();
let loc_place = ecx.alloc_caller_location(file, line, col);
- intern_const_alloc_recursive(&mut ecx, None, loc_place, false).unwrap();
+ intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place, false).unwrap();
let loc_const = ty::Const {
ty: loc_ty,
val: ty::ConstKind::Value(ConstValue::Scalar(loc_place.ptr.into())),
tcx.mk_const(loc_const)
}
-// this function uses `unwrap` copiously, because an already validated constant must have valid
-// fields and can thus never fail outside of compiler bugs
-pub(crate) fn const_variant_index<'tcx>(
+// this function uses `unwrap` copiously, because an already validated constant
+// must have valid fields and can thus never fail outside of compiler bugs
+pub(crate) fn destructure_const<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
val: &'tcx ty::Const<'tcx>,
-) -> VariantIdx {
- trace!("const_variant_index: {:?}", val);
+) -> mir::DestructuredConst<'tcx> {
+ trace!("destructure_const: {:?}", val);
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
let op = ecx.eval_const_to_op(val, None).unwrap();
- ecx.read_discriminant(op).unwrap().1
+
+ let variant = ecx.read_discriminant(op).unwrap().1;
+
+ let field_count = match val.ty.kind {
+ ty::Array(_, len) => len.eval_usize(tcx, param_env),
+ ty::Adt(def, _) => def.variants[variant].fields.len() as u64,
+ ty::Tuple(substs) => substs.len() as u64,
+ _ => bug!("cannot destructure constant {:?}", val),
+ };
+
+ let down = ecx.operand_downcast(op, variant).unwrap();
+ let fields_iter = (0..field_count).map(|i| {
+ let field_op = ecx.operand_field(down, i).unwrap();
+ op_to_const(&ecx, field_op)
+ });
+ let fields = tcx.arena.alloc_from_iter(fields_iter);
+
+ mir::DestructuredConst { variant, fields }
}