Skip to content

Commit b2903d8

Browse files
committed
Improve pretty-printing for ConstVals in MIR.
1 parent c785802 commit b2903d8

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

Diff for: src/librustc/mir/repr.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use middle::ty::{self, AdtDef, ClosureSubsts, FnOutput, Region, Ty};
1515
use rustc_back::slice;
1616
use rustc_data_structures::tuple_slice::TupleSlice;
1717
use rustc_front::hir::InlineAsm;
18-
use syntax::ast::Name;
18+
use syntax::ast::{self, Name};
1919
use syntax::codemap::Span;
20+
use std::ascii;
2021
use std::borrow::{Cow, IntoCow};
2122
use std::fmt::{self, Debug, Formatter, Write};
2223
use std::{iter, u32};
@@ -844,26 +845,41 @@ impl<'tcx> Debug for Literal<'tcx> {
844845
use self::Literal::*;
845846
match *self {
846847
Item { def_id, .. } =>
847-
write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))),
848-
Value { ref value } => fmt_const_val(fmt, value),
848+
write!(fmt, "{}", item_path_str(def_id)),
849+
Value { ref value } => {
850+
try!(write!(fmt, "const "));
851+
fmt_const_val(fmt, value)
852+
}
849853
}
850854
}
851855
}
852856

853857
/// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
854-
pub fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
858+
fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ConstVal) -> fmt::Result {
855859
use middle::const_eval::ConstVal::*;
856860
match *const_val {
857861
Float(f) => write!(fmt, "{:?}", f),
858862
Int(n) => write!(fmt, "{:?}", n),
859863
Uint(n) => write!(fmt, "{:?}", n),
860-
Str(ref s) => write!(fmt, "Str({:?})", s),
861-
ByteStr(ref bytes) => write!(fmt, "ByteStr{:?}", bytes),
864+
Str(ref s) => write!(fmt, "{:?}", s),
865+
ByteStr(ref bytes) => {
866+
let escaped: String = bytes
867+
.iter()
868+
.flat_map(|&ch| ascii::escape_default(ch).map(|c| c as char))
869+
.collect();
870+
write!(fmt, "b\"{}\"", escaped)
871+
}
862872
Bool(b) => write!(fmt, "{:?}", b),
863-
Struct(id) => write!(fmt, "Struct({:?})", id),
864-
Tuple(id) => write!(fmt, "Tuple({:?})", id),
865-
Function(def_id) => write!(fmt, "Function({:?})", def_id),
866-
Array(id, n) => write!(fmt, "Array({:?}, {:?})", id, n),
867-
Repeat(id, n) => write!(fmt, "Repeat({:?}, {:?})", id, n),
873+
Function(def_id) => write!(fmt, "{}", item_path_str(def_id)),
874+
Struct(node_id) | Tuple(node_id) | Array(node_id, _) | Repeat(node_id, _) =>
875+
write!(fmt, "{}", node_to_string(node_id)),
868876
}
869877
}
878+
879+
fn node_to_string(node_id: ast::NodeId) -> String {
880+
ty::tls::with(|tcx| tcx.map.node_to_user_string(node_id))
881+
}
882+
883+
fn item_path_str(def_id: DefId) -> String {
884+
ty::tls::with(|tcx| tcx.item_path_str(def_id))
885+
}

0 commit comments

Comments
 (0)