use pretty_print_const_value from MIR constant 'extra' printing
This commit is contained in:
parent
10a822be38
commit
57444cf9f3
@ -47,6 +47,8 @@ extern crate cfg_if;
|
||||
#[macro_use]
|
||||
extern crate rustc_macros;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
pub use rustc_index::static_assert_size;
|
||||
|
||||
#[inline(never)]
|
||||
@ -126,6 +128,23 @@ impl<F: FnOnce()> Drop for OnDrop<F> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
|
||||
pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
|
||||
struct Printer<F> {
|
||||
f: F,
|
||||
}
|
||||
impl<F> fmt::Display for Printer<F>
|
||||
where
|
||||
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
|
||||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
(self.f)(fmt)
|
||||
}
|
||||
}
|
||||
|
||||
Printer { f }
|
||||
}
|
||||
|
||||
// See comments in src/librustc_middle/lib.rs
|
||||
#[doc(hidden)]
|
||||
pub fn __noop_fix_for_27438() {}
|
||||
|
@ -70,7 +70,7 @@ pub use self::pretty::{
|
||||
create_dump_file, display_allocation, dump_enabled, dump_mir, write_mir_pretty, PassWhere,
|
||||
};
|
||||
pub use consts::*;
|
||||
pub use pretty::pretty_print_const_value;
|
||||
use pretty::pretty_print_const_value;
|
||||
pub use syntax::*;
|
||||
pub use terminator::*;
|
||||
|
||||
|
@ -455,26 +455,27 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
|
||||
self.push(&format!("+ user_ty: {user_ty:?}"));
|
||||
}
|
||||
|
||||
// FIXME: this is a poor version of `pretty_print_const_value`.
|
||||
let fmt_val = |val: &ConstValue<'tcx>| match val {
|
||||
ConstValue::ZeroSized => "<ZST>".to_string(),
|
||||
ConstValue::Scalar(s) => format!("Scalar({s:?})"),
|
||||
ConstValue::Slice { .. } => "Slice(..)".to_string(),
|
||||
ConstValue::Indirect { .. } => "ByRef(..)".to_string(),
|
||||
let fmt_val = |val: ConstValue<'tcx>, ty: Ty<'tcx>| {
|
||||
let tcx = self.tcx;
|
||||
rustc_data_structures::make_display(move |fmt| {
|
||||
pretty_print_const_value_tcx(tcx, val, ty, fmt)
|
||||
})
|
||||
};
|
||||
|
||||
// FIXME: call pretty_print_const_valtree?
|
||||
let fmt_valtree = |valtree: &ty::ValTree<'tcx>| match valtree {
|
||||
ty::ValTree::Leaf(leaf) => format!("ValTree::Leaf({leaf:?})"),
|
||||
ty::ValTree::Branch(_) => "ValTree::Branch(..)".to_string(),
|
||||
ty::ValTree::Leaf(leaf) => format!("Leaf({leaf:?})"),
|
||||
ty::ValTree::Branch(_) => "Branch(..)".to_string(),
|
||||
};
|
||||
|
||||
let val = match literal {
|
||||
ConstantKind::Ty(ct) => match ct.kind() {
|
||||
ty::ConstKind::Param(p) => format!("Param({p})"),
|
||||
ty::ConstKind::Param(p) => format!("ty::Param({p})"),
|
||||
ty::ConstKind::Unevaluated(uv) => {
|
||||
format!("Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.args,)
|
||||
format!("ty::Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.args,)
|
||||
}
|
||||
ty::ConstKind::Value(val) => format!("Value({})", fmt_valtree(&val)),
|
||||
ty::ConstKind::Value(val) => format!("ty::Valtree({})", fmt_valtree(&val)),
|
||||
// No `ty::` prefix since we also use this to represent errors from `mir::Unevaluated`.
|
||||
ty::ConstKind::Error(_) => "Error".to_string(),
|
||||
// These variants shouldn't exist in the MIR.
|
||||
ty::ConstKind::Placeholder(_)
|
||||
@ -490,10 +491,7 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
|
||||
uv.promoted,
|
||||
)
|
||||
}
|
||||
// To keep the diffs small, we render this like we render `ty::Const::Value`.
|
||||
//
|
||||
// This changes once `ty::Const::Value` is represented using valtrees.
|
||||
ConstantKind::Val(val, _) => format!("Value({})", fmt_val(&val)),
|
||||
ConstantKind::Val(val, ty) => format!("Value({})", fmt_val(*val, *ty)),
|
||||
};
|
||||
|
||||
// This reflects what `Const` looked liked before `val` was renamed
|
||||
@ -1090,6 +1088,7 @@ fn pretty_print_byte_str(fmt: &mut Formatter<'_>, byte_str: &[u8]) -> fmt::Resul
|
||||
}
|
||||
|
||||
fn comma_sep<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
fmt: &mut Formatter<'_>,
|
||||
elems: Vec<(ConstValue<'tcx>, Ty<'tcx>)>,
|
||||
) -> fmt::Result {
|
||||
@ -1098,23 +1097,20 @@ fn comma_sep<'tcx>(
|
||||
if !first {
|
||||
fmt.write_str(", ")?;
|
||||
}
|
||||
pretty_print_const_value(ct, ty, fmt)?;
|
||||
pretty_print_const_value_tcx(tcx, ct, ty, fmt)?;
|
||||
first = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn pretty_print_const_value<'tcx>(
|
||||
fn pretty_print_const_value_tcx<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
ct: ConstValue<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
fmt: &mut Formatter<'_>,
|
||||
) -> fmt::Result {
|
||||
use crate::ty::print::PrettyPrinter;
|
||||
|
||||
ty::tls::with(|tcx| {
|
||||
let ct = tcx.lift(ct).unwrap();
|
||||
let ty = tcx.lift(ty).unwrap();
|
||||
|
||||
if tcx.sess.verbose() {
|
||||
fmt.write_str(&format!("ConstValue({ct:?}: {ty})"))?;
|
||||
return Ok(());
|
||||
@ -1160,12 +1156,12 @@ pub fn pretty_print_const_value<'tcx>(
|
||||
match *ty.kind() {
|
||||
ty::Array(..) => {
|
||||
fmt.write_str("[")?;
|
||||
comma_sep(fmt, fields)?;
|
||||
comma_sep(tcx, fmt, fields)?;
|
||||
fmt.write_str("]")?;
|
||||
}
|
||||
ty::Tuple(..) => {
|
||||
fmt.write_str("(")?;
|
||||
comma_sep(fmt, fields)?;
|
||||
comma_sep(tcx, fmt, fields)?;
|
||||
if contents.fields.len() == 1 {
|
||||
fmt.write_str(",")?;
|
||||
}
|
||||
@ -1189,20 +1185,19 @@ pub fn pretty_print_const_value<'tcx>(
|
||||
Some(CtorKind::Const) => {}
|
||||
Some(CtorKind::Fn) => {
|
||||
fmt.write_str("(")?;
|
||||
comma_sep(fmt, fields)?;
|
||||
comma_sep(tcx, fmt, fields)?;
|
||||
fmt.write_str(")")?;
|
||||
}
|
||||
None => {
|
||||
fmt.write_str(" {{ ")?;
|
||||
let mut first = true;
|
||||
for (field_def, (ct, ty)) in
|
||||
iter::zip(&variant_def.fields, fields)
|
||||
for (field_def, (ct, ty)) in iter::zip(&variant_def.fields, fields)
|
||||
{
|
||||
if !first {
|
||||
fmt.write_str(", ")?;
|
||||
}
|
||||
write!(fmt, "{}: ", field_def.name)?;
|
||||
pretty_print_const_value(ct, ty, fmt)?;
|
||||
pretty_print_const_value_tcx(tcx, ct, ty, fmt)?;
|
||||
first = false;
|
||||
}
|
||||
fmt.write_str(" }}")?;
|
||||
@ -1235,6 +1230,17 @@ pub fn pretty_print_const_value<'tcx>(
|
||||
}
|
||||
// Fall back to debug pretty printing for invalid constants.
|
||||
write!(fmt, "{ct:?}: {ty}")
|
||||
}
|
||||
|
||||
pub(crate) fn pretty_print_const_value<'tcx>(
|
||||
ct: ConstValue<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
fmt: &mut Formatter<'_>,
|
||||
) -> fmt::Result {
|
||||
ty::tls::with(|tcx| {
|
||||
let ct = tcx.lift(ct).unwrap();
|
||||
let ty = tcx.lift(ty).unwrap();
|
||||
pretty_print_const_value_tcx(tcx, ct, ty, fmt)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1713,7 +1713,7 @@ pub trait PrettyPrinter<'tcx>:
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pretty_print_const<'tcx>(
|
||||
pub(crate) fn pretty_print_const<'tcx>(
|
||||
c: ty::Const<'tcx>,
|
||||
fmt: &mut fmt::Formatter<'_>,
|
||||
print_types: bool,
|
||||
|
@ -11,7 +11,7 @@ fn outer(_1: u8) -> u8 {
|
||||
_0 = inner(move _2) -> [return: bb1, unwind unreachable]; // scope 0 at $DIR/spans.rs:10:5: 10:14
|
||||
// mir::Constant
|
||||
// + span: $DIR/spans.rs:10:5: 10:10
|
||||
// + literal: Const { ty: for<'a> fn(&'a u8) -> u8 {inner}, val: Value(<ZST>) }
|
||||
// + literal: Const { ty: for<'a> fn(&'a u8) -> u8 {inner}, val: Value(inner) }
|
||||
}
|
||||
|
||||
bb1: {
|
||||
|
@ -11,7 +11,7 @@ fn outer(_1: u8) -> u8 {
|
||||
_0 = inner(move _2) -> [return: bb1, unwind continue]; // scope 0 at $DIR/spans.rs:10:5: 10:14
|
||||
// mir::Constant
|
||||
// + span: $DIR/spans.rs:10:5: 10:10
|
||||
// + literal: Const { ty: for<'a> fn(&'a u8) -> u8 {inner}, val: Value(<ZST>) }
|
||||
// + literal: Const { ty: for<'a> fn(&'a u8) -> u8 {inner}, val: Value(inner) }
|
||||
}
|
||||
|
||||
bb1: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user