about summary refs log tree commit diff
diff options
context:
space:
mode:
authorScott Olson <scott@solson.me>2016-01-05 23:06:33 -0600
committerScott Olson <scott@solson.me>2016-01-05 23:08:16 -0600
commitb2903d87c90709e5ae45e225718b23a91bfbd0eb (patch)
tree1a7f8544a446bfde417f58697aa16e5e5ba8b2fc
parentc785802c0a6e609b2b1ad9a259de7ff947009d00 (diff)
downloadrust-b2903d87c90709e5ae45e225718b23a91bfbd0eb.tar.gz
rust-b2903d87c90709e5ae45e225718b23a91bfbd0eb.zip
Improve pretty-printing for ConstVals in MIR.
-rw-r--r--src/librustc/mir/repr.rs38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/librustc/mir/repr.rs b/src/librustc/mir/repr.rs
index 6d126f8306f..4226b600f68 100644
--- a/src/librustc/mir/repr.rs
+++ b/src/librustc/mir/repr.rs
@@ -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};
@@ -844,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))
+}