Support pretty printing of invalid constants
Make it possible to pretty print invalid constants by introducing a fallible variant of `destructure_const` and falling back to debug formatting when it fails.
This commit is contained in:
parent
bfb2856f27
commit
92d20c4aad
7 changed files with 117 additions and 29 deletions
|
@ -11,7 +11,8 @@ use rustc_middle::{
|
|||
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
|
||||
|
||||
use crate::interpret::{
|
||||
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, MPlaceTy, MemPlaceMeta, Scalar,
|
||||
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MPlaceTy,
|
||||
MemPlaceMeta, Scalar,
|
||||
};
|
||||
|
||||
mod error;
|
||||
|
@ -132,42 +133,39 @@ fn const_to_valtree_inner<'tcx>(
|
|||
}
|
||||
}
|
||||
|
||||
/// This function uses `unwrap` copiously, because an already validated constant
|
||||
/// must have valid fields and can thus never fail outside of compiler bugs. However, it is
|
||||
/// invoked from the pretty printer, where it can receive enums with no variants and e.g.
|
||||
/// `read_discriminant` needs to be able to handle that.
|
||||
pub(crate) fn destructure_const<'tcx>(
|
||||
/// This function should never fail for validated constants. However, it is also invoked from the
|
||||
/// pretty printer which might attempt to format invalid constants and in that case it might fail.
|
||||
pub(crate) fn try_destructure_const<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
val: ty::Const<'tcx>,
|
||||
) -> mir::DestructuredConst<'tcx> {
|
||||
) -> InterpResult<'tcx, mir::DestructuredConst<'tcx>> {
|
||||
trace!("destructure_const: {:?}", val);
|
||||
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
|
||||
let op = ecx.const_to_op(val, None).unwrap();
|
||||
let op = ecx.const_to_op(val, None)?;
|
||||
|
||||
// We go to `usize` as we cannot allocate anything bigger anyway.
|
||||
let (field_count, variant, down) = match val.ty().kind() {
|
||||
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
|
||||
ty::Adt(def, _) if def.variants.is_empty() => {
|
||||
return mir::DestructuredConst { variant: None, fields: &[] };
|
||||
}
|
||||
ty::Adt(def, _) => {
|
||||
let variant = ecx.read_discriminant(&op).unwrap().1;
|
||||
let down = ecx.operand_downcast(&op, variant).unwrap();
|
||||
let variant = ecx.read_discriminant(&op)?.1;
|
||||
let down = ecx.operand_downcast(&op, variant)?;
|
||||
(def.variants[variant].fields.len(), Some(variant), down)
|
||||
}
|
||||
ty::Tuple(substs) => (substs.len(), None, op),
|
||||
_ => bug!("cannot destructure constant {:?}", val),
|
||||
};
|
||||
|
||||
let fields_iter = (0..field_count).map(|i| {
|
||||
let field_op = ecx.operand_field(&down, i).unwrap();
|
||||
let val = op_to_const(&ecx, &field_op);
|
||||
ty::Const::from_value(tcx, val, field_op.layout.ty)
|
||||
});
|
||||
let fields = tcx.arena.alloc_from_iter(fields_iter);
|
||||
let fields = (0..field_count)
|
||||
.map(|i| {
|
||||
let field_op = ecx.operand_field(&down, i)?;
|
||||
let val = op_to_const(&ecx, &field_op);
|
||||
Ok(ty::Const::from_value(tcx, val, field_op.layout.ty))
|
||||
})
|
||||
.collect::<InterpResult<'tcx, Vec<_>>>()?;
|
||||
let fields = tcx.arena.alloc_from_iter(fields);
|
||||
|
||||
mir::DestructuredConst { variant, fields }
|
||||
Ok(mir::DestructuredConst { variant, fields })
|
||||
}
|
||||
|
||||
pub(crate) fn deref_const<'tcx>(
|
||||
|
|
|
@ -41,9 +41,9 @@ pub fn provide(providers: &mut Providers) {
|
|||
providers.eval_to_const_value_raw = const_eval::eval_to_const_value_raw_provider;
|
||||
providers.eval_to_allocation_raw = const_eval::eval_to_allocation_raw_provider;
|
||||
providers.const_caller_location = const_eval::const_caller_location;
|
||||
providers.destructure_const = |tcx, param_env_and_value| {
|
||||
providers.try_destructure_const = |tcx, param_env_and_value| {
|
||||
let (param_env, value) = param_env_and_value.into_parts();
|
||||
const_eval::destructure_const(tcx, param_env, value)
|
||||
const_eval::try_destructure_const(tcx, param_env, value).ok()
|
||||
};
|
||||
providers.const_to_valtree = |tcx, param_env_and_value| {
|
||||
let (param_env, raw) = param_env_and_value.into_parts();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue