Skip to content

Improve pretty-printing for ConstVal in MIR. #30734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/librustc/mir/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use middle::ty::{self, AdtDef, ClosureSubsts, FnOutput, Region, Ty};
use rustc_back::slice;
use rustc_data_structures::tuple_slice::TupleSlice;
use rustc_front::hir::InlineAsm;
use syntax::ast::Name;
use syntax::ast::{self, Name};
use syntax::codemap::Span;
use std::ascii;
use std::borrow::{Cow, IntoCow};
use std::fmt::{self, Debug, Formatter, Write};
use std::{iter, u32};
Expand Down Expand Up @@ -496,13 +497,11 @@ pub enum ProjectionElem<'tcx, V> {

/// Alias for projections as they appear in lvalues, where the base is an lvalue
/// and the index is an operand.
pub type LvalueProjection<'tcx> =
Projection<'tcx,Lvalue<'tcx>,Operand<'tcx>>;
pub type LvalueProjection<'tcx> = Projection<'tcx, Lvalue<'tcx>, Operand<'tcx>>;

/// Alias for projections as they appear in lvalues, where the base is an lvalue
/// and the index is an operand.
pub type LvalueElem<'tcx> =
ProjectionElem<'tcx,Operand<'tcx>>;
pub type LvalueElem<'tcx> = ProjectionElem<'tcx, Operand<'tcx>>;

/// Index into the list of fields found in a `VariantDef`
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
Expand Down Expand Up @@ -546,11 +545,11 @@ impl<'tcx> Debug for Lvalue<'tcx> {

match *self {
Var(id) =>
write!(fmt,"var{:?}", id),
write!(fmt, "var{:?}", id),
Arg(id) =>
write!(fmt,"arg{:?}", id),
write!(fmt, "arg{:?}", id),
Temp(id) =>
write!(fmt,"tmp{:?}", id),
write!(fmt, "tmp{:?}", id),
Static(def_id) =>
write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))),
ReturnPointer =>
Expand Down Expand Up @@ -846,26 +845,41 @@ impl<'tcx> Debug for Literal<'tcx> {
use self::Literal::*;
match *self {
Item { def_id, .. } =>
write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))),
Value { ref value } => fmt_const_val(fmt, value),
write!(fmt, "{}", item_path_str(def_id)),
Value { ref value } => {
try!(write!(fmt, "const "));
fmt_const_val(fmt, value)
}
}
}
}

/// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
pub fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
use middle::const_eval::ConstVal::*;
match *const_val {
Float(f) => write!(fmt, "{:?}", f),
Int(n) => write!(fmt, "{:?}", n),
Uint(n) => write!(fmt, "{:?}", n),
Str(ref s) => write!(fmt, "Str({:?})", s),
ByteStr(ref bytes) => write!(fmt, "ByteStr{:?}", bytes),
Str(ref s) => write!(fmt, "{:?}", s),
ByteStr(ref bytes) => {
let escaped: String = bytes
.iter()
.flat_map(|&ch| ascii::escape_default(ch).map(|c| c as char))
.collect();
write!(fmt, "b\"{}\"", escaped)
}
Bool(b) => write!(fmt, "{:?}", b),
Struct(id) => write!(fmt, "Struct({:?})", id),
Tuple(id) => write!(fmt, "Tuple({:?})", id),
Function(def_id) => write!(fmt, "Function({:?})", def_id),
Array(id, n) => write!(fmt, "Array({:?}, {:?})", id, n),
Repeat(id, n) => write!(fmt, "Repeat({:?}, {:?})", id, n),
Function(def_id) => write!(fmt, "{}", item_path_str(def_id)),
Struct(node_id) | Tuple(node_id) | Array(node_id, _) | Repeat(node_id, _) =>
write!(fmt, "{}", node_to_string(node_id)),
}
}

fn node_to_string(node_id: ast::NodeId) -> String {
ty::tls::with(|tcx| tcx.map.node_to_user_string(node_id))
}

fn item_path_str(def_id: DefId) -> String {
ty::tls::with(|tcx| tcx.item_path_str(def_id))
}