Rollup merge of #94020 - tmiasko:pp, r=oli-obk
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. Closes #93688.
This commit is contained in:
commit
bc4f117acc
7 changed files with 117 additions and 29 deletions
|
@ -98,4 +98,12 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?;
|
||||
Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory())
|
||||
}
|
||||
|
||||
/// Destructure a constant ADT or array into its variant index and its field values.
|
||||
pub fn destructure_const(
|
||||
self,
|
||||
param_env_and_val: ty::ParamEnvAnd<'tcx, ty::Const<'tcx>>,
|
||||
) -> mir::DestructuredConst<'tcx> {
|
||||
self.try_destructure_const(param_env_and_val).unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -924,10 +924,12 @@ rustc_queries! {
|
|||
}
|
||||
|
||||
/// Destructure a constant ADT or array into its variant index and its
|
||||
/// field values.
|
||||
query destructure_const(
|
||||
/// field values or return `None` if constant is invalid.
|
||||
///
|
||||
/// Use infallible `TyCtxt::destructure_const` when you know that constant is valid.
|
||||
query try_destructure_const(
|
||||
key: ty::ParamEnvAnd<'tcx, ty::Const<'tcx>>
|
||||
) -> mir::DestructuredConst<'tcx> {
|
||||
) -> Option<mir::DestructuredConst<'tcx>> {
|
||||
desc { "destructure constant" }
|
||||
remap_env_constness
|
||||
}
|
||||
|
|
|
@ -1459,10 +1459,18 @@ pub trait PrettyPrinter<'tcx>:
|
|||
// FIXME(eddyb) for `--emit=mir`/`-Z dump-mir`, we should provide the
|
||||
// correct `ty::ParamEnv` to allow printing *all* constant values.
|
||||
(_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_param_types_or_consts() => {
|
||||
let contents =
|
||||
self.tcx().destructure_const(ty::ParamEnv::reveal_all().and(
|
||||
self.tcx().mk_const(ty::ConstS { val: ty::ConstKind::Value(ct), ty }),
|
||||
));
|
||||
let Some(contents) = self.tcx().try_destructure_const(
|
||||
ty::ParamEnv::reveal_all()
|
||||
.and(self.tcx().mk_const(ty::ConstS { val: ty::ConstKind::Value(ct), ty })),
|
||||
) else {
|
||||
// Fall back to debug pretty printing for invalid constants.
|
||||
p!(write("{:?}", ct));
|
||||
if print_ty {
|
||||
p!(": ", print(ty));
|
||||
}
|
||||
return Ok(self);
|
||||
};
|
||||
|
||||
let fields = contents.fields.iter().copied();
|
||||
|
||||
match *ty.kind() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue