From c785802c0a6e609b2b1ad9a259de7ff947009d00 Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Tue, 5 Jan 2016 17:26:22 -0600 Subject: [PATCH 1/2] Boring whitespace cleanup. --- src/librustc/mir/repr.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/librustc/mir/repr.rs b/src/librustc/mir/repr.rs index 75a588d424e..6d126f8306f 100644 --- a/src/librustc/mir/repr.rs +++ b/src/librustc/mir/repr.rs @@ -496,13 +496,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)] @@ -546,11 +544,11 @@ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { 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 => From b2903d87c90709e5ae45e225718b23a91bfbd0eb Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Tue, 5 Jan 2016 23:06:33 -0600 Subject: [PATCH 2/2] Improve pretty-printing for ConstVals in MIR. --- src/librustc/mir/repr.rs | 38 +++++++++++++++++++++++++++----------- 1 file 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 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 @@ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { 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(fmt: &mut W, const_val: &ConstVal) -> fmt::Result { +fn fmt_const_val(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)) +}