@@ -9,8 +9,6 @@ use crate::hir::def_id::DefId;
99use crate :: hir:: { self , InlineAsm as HirInlineAsm } ;
1010use crate :: mir:: interpret:: { ConstValue , InterpError , Scalar } ;
1111use crate :: mir:: visit:: MirVisitable ;
12- use rustc_apfloat:: ieee:: { Double , Single } ;
13- use rustc_apfloat:: Float ;
1412use rustc_data_structures:: fx:: FxHashSet ;
1513use rustc_data_structures:: graph:: dominators:: { dominators, Dominators } ;
1614use rustc_data_structures:: graph:: { self , GraphPredecessors , GraphSuccessors } ;
@@ -21,13 +19,13 @@ use rustc_macros::HashStable;
2119use crate :: rustc_serialize:: { self as serialize} ;
2220use smallvec:: SmallVec ;
2321use std:: borrow:: Cow ;
24- use std:: fmt:: { self , Debug , Formatter , Write } ;
22+ use std:: fmt:: { self , Debug , Formatter , Write , Display } ;
2523use std:: iter:: FusedIterator ;
2624use std:: ops:: { Index , IndexMut } ;
2725use std:: slice;
2826use std:: vec:: IntoIter ;
2927use std:: { iter, mem, option, u32} ;
30- use syntax:: ast:: { self , Name } ;
28+ use syntax:: ast:: Name ;
3129use syntax:: symbol:: { InternedString , Symbol } ;
3230use syntax_pos:: { Span , DUMMY_SP } ;
3331use crate :: ty:: fold:: { TypeFoldable , TypeFolder , TypeVisitor } ;
@@ -1662,28 +1660,25 @@ impl<'tcx> TerminatorKind<'tcx> {
16621660 switch_ty,
16631661 ..
16641662 } => {
1665- let size = ty:: tls:: with ( |tcx| {
1663+ ty:: tls:: with ( |tcx| {
16661664 let param_env = ty:: ParamEnv :: empty ( ) ;
16671665 let switch_ty = tcx. lift_to_global ( & switch_ty) . unwrap ( ) ;
1668- tcx. layout_of ( param_env. and ( switch_ty) ) . unwrap ( ) . size
1669- } ) ;
1670- values
1671- . iter ( )
1672- . map ( |& u| {
1673- let mut s = String :: new ( ) ;
1674- let c = ty:: Const {
1675- val : ConstValue :: Scalar (
1676- Scalar :: Bits {
1677- bits : u,
1678- size : size. bytes ( ) as u8 ,
1679- } . into ( ) ,
1680- ) ,
1681- ty : switch_ty,
1682- } ;
1683- fmt_const_val ( & mut s, c) . unwrap ( ) ;
1684- s. into ( )
1685- } ) . chain ( iter:: once ( "otherwise" . into ( ) ) )
1686- . collect ( )
1666+ let size = tcx. layout_of ( param_env. and ( switch_ty) ) . unwrap ( ) . size ;
1667+ values
1668+ . iter ( )
1669+ . map ( |& u| {
1670+ tcx. mk_const ( ty:: Const {
1671+ val : ConstValue :: Scalar (
1672+ Scalar :: Bits {
1673+ bits : u,
1674+ size : size. bytes ( ) as u8 ,
1675+ } . into ( ) ,
1676+ ) ,
1677+ ty : switch_ty,
1678+ } ) . to_string ( ) . into ( )
1679+ } ) . chain ( iter:: once ( "otherwise" . into ( ) ) )
1680+ . collect ( )
1681+ } )
16871682 }
16881683 Call {
16891684 destination : Some ( _) ,
@@ -2331,9 +2326,7 @@ impl<'tcx> Operand<'tcx> {
23312326 span,
23322327 ty,
23332328 user_ty : None ,
2334- literal : tcx. mk_const (
2335- ty:: Const :: zero_sized ( ty) ,
2336- ) ,
2329+ literal : ty:: Const :: zero_sized ( tcx, ty) ,
23372330 } )
23382331 }
23392332
@@ -2827,67 +2820,15 @@ newtype_index! {
28272820
28282821impl < ' tcx > Debug for Constant < ' tcx > {
28292822 fn fmt ( & self , fmt : & mut Formatter < ' _ > ) -> fmt:: Result {
2830- write ! ( fmt, "const " ) ?;
2831- fmt_const_val ( fmt, * self . literal )
2832- }
2833- }
2834- /// Write a `ConstValue` in a way closer to the original source code than the `Debug` output.
2835- pub fn fmt_const_val ( f : & mut impl Write , const_val : ty:: Const < ' _ > ) -> fmt:: Result {
2836- use crate :: ty:: TyKind :: * ;
2837- let value = const_val. val ;
2838- let ty = const_val. ty ;
2839- // print some primitives
2840- if let ConstValue :: Scalar ( Scalar :: Bits { bits, .. } ) = value {
2841- match ty. sty {
2842- Bool if bits == 0 => return write ! ( f, "false" ) ,
2843- Bool if bits == 1 => return write ! ( f, "true" ) ,
2844- Float ( ast:: FloatTy :: F32 ) => return write ! ( f, "{}f32" , Single :: from_bits( bits) ) ,
2845- Float ( ast:: FloatTy :: F64 ) => return write ! ( f, "{}f64" , Double :: from_bits( bits) ) ,
2846- Uint ( ui) => return write ! ( f, "{:?}{}" , bits, ui) ,
2847- Int ( i) => {
2848- let bit_width = ty:: tls:: with ( |tcx| {
2849- let ty = tcx. lift_to_global ( & ty) . unwrap ( ) ;
2850- tcx. layout_of ( ty:: ParamEnv :: empty ( ) . and ( ty) )
2851- . unwrap ( )
2852- . size
2853- . bits ( )
2854- } ) ;
2855- let shift = 128 - bit_width;
2856- return write ! ( f, "{:?}{}" , ( ( bits as i128 ) << shift) >> shift, i) ;
2857- }
2858- Char => return write ! ( f, "{:?}" , :: std:: char :: from_u32( bits as u32 ) . unwrap( ) ) ,
2859- _ => { }
2860- }
2823+ write ! ( fmt, "{}" , self )
28612824 }
2862- // print function definitions
2863- if let FnDef ( did, _) = ty. sty {
2864- return write ! ( f, "{}" , def_path_str( did) ) ;
2865- }
2866- // print string literals
2867- if let ConstValue :: Slice ( ptr, len) = value {
2868- if let Scalar :: Ptr ( ptr) = ptr {
2869- if let Ref ( _, & ty:: TyS { sty : Str , .. } , _) = ty. sty {
2870- return ty:: tls:: with ( |tcx| {
2871- let alloc = tcx. alloc_map . lock ( ) . get ( ptr. alloc_id ) ;
2872- if let Some ( interpret:: AllocKind :: Memory ( alloc) ) = alloc {
2873- assert_eq ! ( len as usize as u64 , len) ;
2874- let slice =
2875- & alloc. bytes [ ( ptr. offset . bytes ( ) as usize ) ..] [ ..( len as usize ) ] ;
2876- let s = :: std:: str:: from_utf8 ( slice) . expect ( "non utf8 str from miri" ) ;
2877- write ! ( f, "{:?}" , s)
2878- } else {
2879- write ! ( f, "pointer to erroneous constant {:?}, {:?}" , ptr, len)
2880- }
2881- } ) ;
2882- }
2883- }
2884- }
2885- // just raw dump everything else
2886- write ! ( f, "{:?} : {}" , value, ty)
28872825}
28882826
2889- fn def_path_str ( def_id : DefId ) -> String {
2890- ty:: tls:: with ( |tcx| tcx. def_path_str ( def_id) )
2827+ impl < ' tcx > Display for Constant < ' tcx > {
2828+ fn fmt ( & self , fmt : & mut Formatter < ' _ > ) -> fmt:: Result {
2829+ write ! ( fmt, "const " ) ?;
2830+ write ! ( fmt, "{}" , self . literal)
2831+ }
28912832}
28922833
28932834impl < ' tcx > graph:: DirectedGraph for Mir < ' tcx > {
0 commit comments