Address code review comments

This commit is contained in:
Dominik Stolz 2022-06-28 23:26:54 +02:00
parent 080525229b
commit 053f48d91f
2 changed files with 65 additions and 72 deletions

View file

@ -1,13 +1,18 @@
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_target::abi::VariantIdx; use rustc_target::abi::VariantIdx;
/// Tries to destructure constants of type Array or Adt into the constants use std::iter;
/// Tries to destructure array, ADT or tuple constants into the constants
/// of its fields. /// of its fields.
pub(crate) fn destructure_const<'tcx>( pub(crate) fn destructure_const<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
const_: ty::Const<'tcx>, const_: ty::Const<'tcx>,
) -> ty::DestructuredConst<'tcx> { ) -> ty::DestructuredConst<'tcx> {
if let ty::ConstKind::Value(valtree) = const_.kind() { let ty::ConstKind::Value(valtree) = const_.kind() else {
bug!("cannot destructure constant {:?}", const_)
};
let branches = match valtree { let branches = match valtree {
ty::ValTree::Branch(b) => b, ty::ValTree::Branch(b) => b,
_ => bug!("cannot destructure constant {:?}", const_), _ => bug!("cannot destructure constant {:?}", const_),
@ -18,9 +23,7 @@ pub(crate) fn destructure_const<'tcx>(
// construct the consts for the elements of the array/slice // construct the consts for the elements of the array/slice
let field_consts = branches let field_consts = branches
.iter() .iter()
.map(|b| { .map(|b| tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Value(*b), ty: *inner_ty }))
tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Value(*b), ty: *inner_ty })
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
debug!(?field_consts); debug!(?field_consts);
@ -28,38 +31,32 @@ pub(crate) fn destructure_const<'tcx>(
} }
ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"), ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
ty::Adt(def, substs) => { ty::Adt(def, substs) => {
let variant_idx = if def.is_enum() { let (variant_idx, branches) = if def.is_enum() {
VariantIdx::from_u32(branches[0].unwrap_leaf().try_to_u32().unwrap()) let (head, rest) = branches.split_first().unwrap();
(VariantIdx::from_u32(head.unwrap_leaf().try_to_u32().unwrap()), rest)
} else { } else {
VariantIdx::from_u32(0) (VariantIdx::from_u32(0), branches)
}; };
let fields = &def.variant(variant_idx).fields; let fields = &def.variant(variant_idx).fields;
let mut field_consts = Vec::with_capacity(fields.len()); let mut field_consts = Vec::with_capacity(fields.len());
// Note: First element inValTree corresponds to variant of enum for (field, field_valtree) in iter::zip(fields, branches) {
let mut valtree_idx = if def.is_enum() { 1 } else { 0 };
for field in fields {
let field_ty = field.ty(tcx, substs); let field_ty = field.ty(tcx, substs);
let field_valtree = branches[valtree_idx]; // first element of branches is variant
let field_const = tcx.mk_const(ty::ConstS { let field_const = tcx.mk_const(ty::ConstS {
kind: ty::ConstKind::Value(field_valtree), kind: ty::ConstKind::Value(*field_valtree),
ty: field_ty, ty: field_ty,
}); });
field_consts.push(field_const); field_consts.push(field_const);
valtree_idx += 1;
} }
debug!(?field_consts); debug!(?field_consts);
(field_consts, Some(variant_idx)) (field_consts, Some(variant_idx))
} }
ty::Tuple(elem_tys) => { ty::Tuple(elem_tys) => {
let fields = elem_tys let fields = iter::zip(*elem_tys, branches)
.iter() .map(|(elem_ty, elem_valtree)| {
.enumerate()
.map(|(i, elem_ty)| {
let elem_valtree = branches[i];
tcx.mk_const(ty::ConstS { tcx.mk_const(ty::ConstS {
kind: ty::ConstKind::Value(elem_valtree), kind: ty::ConstKind::Value(*elem_valtree),
ty: elem_ty, ty: elem_ty,
}) })
}) })
@ -73,12 +70,8 @@ pub(crate) fn destructure_const<'tcx>(
let fields = tcx.arena.alloc_from_iter(fields.into_iter()); let fields = tcx.arena.alloc_from_iter(fields.into_iter());
ty::DestructuredConst { variant, fields } ty::DestructuredConst { variant, fields }
} else {
bug!("cannot destructure constant {:?}", const_)
}
} }
pub fn provide(providers: &mut ty::query::Providers) { pub fn provide(providers: &mut ty::query::Providers) {
*providers = *providers = ty::query::Providers { destructure_const, ..*providers };
ty::query::Providers { destructure_const, ..*providers };
} }