@@ -18,7 +18,7 @@ use rustc_front::hir::InlineAsm;
1818use syntax:: ast:: Name ;
1919use syntax:: codemap:: Span ;
2020use std:: borrow:: { Cow , IntoCow } ;
21- use std:: fmt:: { Debug , Formatter , Error , Write } ;
21+ use std:: fmt:: { self , Debug , Formatter , Write } ;
2222use std:: { iter, u32} ;
2323
2424/// Lowered representation of a single function.
@@ -183,8 +183,8 @@ impl BasicBlock {
183183}
184184
185185impl Debug for BasicBlock {
186- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
187- write ! ( fmt, "BB({}) " , self . 0 )
186+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
187+ write ! ( fmt, "bb{} " , self . 0 )
188188 }
189189}
190190
@@ -317,7 +317,7 @@ impl<'tcx> BasicBlockData<'tcx> {
317317}
318318
319319impl < ' tcx > Debug for Terminator < ' tcx > {
320- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
320+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
321321 try!( self . fmt_head ( fmt) ) ;
322322 let successors = self . successors ( ) ;
323323 let labels = self . fmt_successor_labels ( ) ;
@@ -347,7 +347,7 @@ impl<'tcx> Terminator<'tcx> {
347347 /// Write the "head" part of the terminator; that is, its name and the data it uses to pick the
348348 /// successor basic block, if any. The only information not inlcuded is the list of possible
349349 /// successors, which may be rendered differently between the text and the graphviz format.
350- pub fn fmt_head < W : Write > ( & self , fmt : & mut W ) -> Result < ( ) , Error > {
350+ pub fn fmt_head < W : Write > ( & self , fmt : & mut W ) -> fmt :: Result {
351351 use self :: Terminator :: * ;
352352 match * self {
353353 Goto { .. } => write ! ( fmt, "goto" ) ,
@@ -421,7 +421,7 @@ pub enum DropKind {
421421}
422422
423423impl < ' tcx > Debug for Statement < ' tcx > {
424- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
424+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
425425 use self :: StatementKind :: * ;
426426 match self . kind {
427427 Assign ( ref lv, ref rv) => write ! ( fmt, "{:?} = {:?}" , lv, rv) ,
@@ -541,7 +541,7 @@ impl<'tcx> Lvalue<'tcx> {
541541}
542542
543543impl < ' tcx > Debug for Lvalue < ' tcx > {
544- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
544+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
545545 use self :: Lvalue :: * ;
546546
547547 match * self {
@@ -551,24 +551,24 @@ impl<'tcx> Debug for Lvalue<'tcx> {
551551 write ! ( fmt, "arg{:?}" , id) ,
552552 Temp ( id) =>
553553 write ! ( fmt, "tmp{:?}" , id) ,
554- Static ( id ) =>
555- write ! ( fmt, "Static({:?}) ", id ) ,
554+ Static ( def_id ) =>
555+ write ! ( fmt, "{} ", ty :: tls :: with ( |tcx| tcx . item_path_str ( def_id ) ) ) ,
556556 ReturnPointer =>
557- write ! ( fmt, "ReturnPointer ") ,
557+ write ! ( fmt, "return ") ,
558558 Projection ( ref data) =>
559559 match data. elem {
560560 ProjectionElem :: Downcast ( ref adt_def, index) =>
561- write ! ( fmt, "({:?} as {})" , data. base, adt_def. variants[ index] . name) ,
561+ write ! ( fmt, "({:?} as {})" , data. base, adt_def. variants[ index] . name) ,
562562 ProjectionElem :: Deref =>
563- write ! ( fmt, "(*{:?})" , data. base) ,
563+ write ! ( fmt, "(*{:?})" , data. base) ,
564564 ProjectionElem :: Field ( field) =>
565- write ! ( fmt, "{:?}.{:?}" , data. base, field. index( ) ) ,
565+ write ! ( fmt, "{:?}.{:?}" , data. base, field. index( ) ) ,
566566 ProjectionElem :: Index ( ref index) =>
567- write ! ( fmt, "{:?}[{:?}]" , data. base, index) ,
567+ write ! ( fmt, "{:?}[{:?}]" , data. base, index) ,
568568 ProjectionElem :: ConstantIndex { offset, min_length, from_end : false } =>
569- write ! ( fmt, "{:?}[{:?} of {:?}]" , data. base, offset, min_length) ,
569+ write ! ( fmt, "{:?}[{:?} of {:?}]" , data. base, offset, min_length) ,
570570 ProjectionElem :: ConstantIndex { offset, min_length, from_end : true } =>
571- write ! ( fmt, "{:?}[-{:?} of {:?}]" , data. base, offset, min_length) ,
571+ write ! ( fmt, "{:?}[-{:?} of {:?}]" , data. base, offset, min_length) ,
572572 } ,
573573 }
574574 }
@@ -588,7 +588,7 @@ pub enum Operand<'tcx> {
588588}
589589
590590impl < ' tcx > Debug for Operand < ' tcx > {
591- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
591+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
592592 use self :: Operand :: * ;
593593 match * self {
594594 Constant ( ref a) => write ! ( fmt, "{:?}" , a) ,
@@ -715,22 +715,87 @@ pub enum UnOp {
715715}
716716
717717impl < ' tcx > Debug for Rvalue < ' tcx > {
718- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
718+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
719719 use self :: Rvalue :: * ;
720720
721721 match * self {
722722 Use ( ref lvalue) => write ! ( fmt, "{:?}" , lvalue) ,
723723 Repeat ( ref a, ref b) => write ! ( fmt, "[{:?}; {:?}]" , a, b) ,
724- Ref ( ref a, bk, ref b) => write ! ( fmt, "&{:?} {:?} {:?}" , a, bk, b) ,
725724 Len ( ref a) => write ! ( fmt, "Len({:?})" , a) ,
726725 Cast ( ref kind, ref lv, ref ty) => write ! ( fmt, "{:?} as {:?} ({:?})" , lv, ty, kind) ,
727726 BinaryOp ( ref op, ref a, ref b) => write ! ( fmt, "{:?}({:?}, {:?})" , op, a, b) ,
728727 UnaryOp ( ref op, ref a) => write ! ( fmt, "{:?}({:?})" , op, a) ,
729728 Box ( ref t) => write ! ( fmt, "Box({:?})" , t) ,
730- Aggregate ( ref kind, ref lvs) => write ! ( fmt, "Aggregate<{:?}>{:?}" , kind, lvs) ,
731729 InlineAsm ( ref asm) => write ! ( fmt, "InlineAsm({:?})" , asm) ,
732730 Slice { ref input, from_start, from_end } =>
733731 write ! ( fmt, "{:?}[{:?}..-{:?}]" , input, from_start, from_end) ,
732+
733+ Ref ( _, borrow_kind, ref lv) => {
734+ let kind_str = match borrow_kind {
735+ BorrowKind :: Shared => "" ,
736+ BorrowKind :: Mut | BorrowKind :: Unique => "mut " ,
737+ } ;
738+ write ! ( fmt, "&{}{:?}" , kind_str, lv)
739+ }
740+
741+ Aggregate ( ref kind, ref lvs) => {
742+ use self :: AggregateKind :: * ;
743+
744+ fn fmt_tuple ( fmt : & mut Formatter , name : & str , lvs : & [ Operand ] ) -> fmt:: Result {
745+ let mut tuple_fmt = fmt. debug_tuple ( name) ;
746+ for lv in lvs {
747+ tuple_fmt. field ( lv) ;
748+ }
749+ tuple_fmt. finish ( )
750+ }
751+
752+ match * kind {
753+ Vec => write ! ( fmt, "{:?}" , lvs) ,
754+
755+ Tuple => {
756+ if lvs. len ( ) == 1 {
757+ write ! ( fmt, "({:?},)" , lvs[ 0 ] )
758+ } else {
759+ fmt_tuple ( fmt, "" , lvs)
760+ }
761+ }
762+
763+ Adt ( adt_def, variant, _) => {
764+ let variant_def = & adt_def. variants [ variant] ;
765+ let name = ty:: tls:: with ( |tcx| tcx. item_path_str ( variant_def. did ) ) ;
766+
767+ match variant_def. kind ( ) {
768+ ty:: VariantKind :: Unit => write ! ( fmt, "{}" , name) ,
769+ ty:: VariantKind :: Tuple => fmt_tuple ( fmt, & name, lvs) ,
770+ ty:: VariantKind :: Struct => {
771+ let mut struct_fmt = fmt. debug_struct ( & name) ;
772+ for ( field, lv) in variant_def. fields . iter ( ) . zip ( lvs) {
773+ struct_fmt. field ( & field. name . as_str ( ) , lv) ;
774+ }
775+ struct_fmt. finish ( )
776+ }
777+ }
778+ }
779+
780+ Closure ( def_id, _) => ty:: tls:: with ( |tcx| {
781+ if let Some ( node_id) = tcx. map . as_local_node_id ( def_id) {
782+ let name = format ! ( "[closure@{:?}]" , tcx. map. span( node_id) ) ;
783+ let mut struct_fmt = fmt. debug_struct ( & name) ;
784+
785+ tcx. with_freevars ( node_id, |freevars| {
786+ for ( freevar, lv) in freevars. iter ( ) . zip ( lvs) {
787+ let var_name = tcx. local_var_name_str ( freevar. def . var_id ( ) ) ;
788+ struct_fmt. field ( & var_name, lv) ;
789+ }
790+ } ) ;
791+
792+ struct_fmt. finish ( )
793+ } else {
794+ write ! ( fmt, "[closure]" )
795+ }
796+ } ) ,
797+ }
798+ }
734799 }
735800 }
736801}
@@ -771,13 +836,13 @@ pub enum Literal<'tcx> {
771836}
772837
773838impl < ' tcx > Debug for Constant < ' tcx > {
774- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
839+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
775840 write ! ( fmt, "{:?}" , self . literal)
776841 }
777842}
778843
779844impl < ' tcx > Debug for Literal < ' tcx > {
780- fn fmt ( & self , fmt : & mut Formatter ) -> Result < ( ) , Error > {
845+ fn fmt ( & self , fmt : & mut Formatter ) -> fmt :: Result {
781846 use self :: Literal :: * ;
782847 match * self {
783848 Item { def_id, .. } =>
@@ -788,7 +853,7 @@ impl<'tcx> Debug for Literal<'tcx> {
788853}
789854
790855/// Write a `ConstVal` in a way closer to the original source code than the `Debug` output.
791- pub fn fmt_const_val < W : Write > ( fmt : & mut W , const_val : & ConstVal ) -> Result < ( ) , Error > {
856+ pub fn fmt_const_val < W : Write > ( fmt : & mut W , const_val : & ConstVal ) -> fmt :: Result {
792857 use middle:: const_eval:: ConstVal :: * ;
793858 match * const_val {
794859 Float ( f) => write ! ( fmt, "{:?}" , f) ,
0 commit comments