@@ -15,8 +15,9 @@ use middle::ty::{self, AdtDef, ClosureSubsts, FnOutput, Region, Ty};
15
15
use rustc_back:: slice;
16
16
use rustc_data_structures:: tuple_slice:: TupleSlice ;
17
17
use rustc_front:: hir:: InlineAsm ;
18
- use syntax:: ast:: Name ;
18
+ use syntax:: ast:: { self , Name } ;
19
19
use syntax:: codemap:: Span ;
20
+ use std:: ascii;
20
21
use std:: borrow:: { Cow , IntoCow } ;
21
22
use std:: fmt:: { self , Debug , Formatter , Write } ;
22
23
use std:: { iter, u32} ;
@@ -844,26 +845,41 @@ impl<'tcx> Debug for Literal<'tcx> {
844
845
use self :: Literal :: * ;
845
846
match * self {
846
847
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
+ }
849
853
}
850
854
}
851
855
}
852
856
853
857
/// 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 {
855
859
use middle:: const_eval:: ConstVal :: * ;
856
860
match * const_val {
857
861
Float ( f) => write ! ( fmt, "{:?}" , f) ,
858
862
Int ( n) => write ! ( fmt, "{:?}" , n) ,
859
863
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
+ }
862
872
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) ) ,
868
876
}
869
877
}
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