@@ -9,8 +9,6 @@ use crate::hir::def_id::DefId;
9
9
use crate :: hir:: { self , InlineAsm as HirInlineAsm } ;
10
10
use crate :: mir:: interpret:: { ConstValue , InterpError , Scalar } ;
11
11
use crate :: mir:: visit:: MirVisitable ;
12
- use rustc_apfloat:: ieee:: { Double , Single } ;
13
- use rustc_apfloat:: Float ;
14
12
use rustc_data_structures:: fx:: FxHashSet ;
15
13
use rustc_data_structures:: graph:: dominators:: { dominators, Dominators } ;
16
14
use rustc_data_structures:: graph:: { self , GraphPredecessors , GraphSuccessors } ;
@@ -21,13 +19,13 @@ use rustc_macros::HashStable;
21
19
use crate :: rustc_serialize:: { self as serialize} ;
22
20
use smallvec:: SmallVec ;
23
21
use std:: borrow:: Cow ;
24
- use std:: fmt:: { self , Debug , Formatter , Write } ;
22
+ use std:: fmt:: { self , Debug , Formatter , Write , Display } ;
25
23
use std:: iter:: FusedIterator ;
26
24
use std:: ops:: { Index , IndexMut } ;
27
25
use std:: slice;
28
26
use std:: vec:: IntoIter ;
29
27
use std:: { iter, mem, option, u32} ;
30
- use syntax:: ast:: { self , Name } ;
28
+ use syntax:: ast:: Name ;
31
29
use syntax:: symbol:: { InternedString , Symbol } ;
32
30
use syntax_pos:: { Span , DUMMY_SP } ;
33
31
use crate :: ty:: fold:: { TypeFoldable , TypeFolder , TypeVisitor } ;
@@ -1662,28 +1660,25 @@ impl<'tcx> TerminatorKind<'tcx> {
1662
1660
switch_ty,
1663
1661
..
1664
1662
} => {
1665
- let size = ty:: tls:: with ( |tcx| {
1663
+ ty:: tls:: with ( |tcx| {
1666
1664
let param_env = ty:: ParamEnv :: empty ( ) ;
1667
1665
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
+ } )
1687
1682
}
1688
1683
Call {
1689
1684
destination : Some ( _) ,
@@ -2331,9 +2326,7 @@ impl<'tcx> Operand<'tcx> {
2331
2326
span,
2332
2327
ty,
2333
2328
user_ty : None ,
2334
- literal : tcx. mk_const (
2335
- ty:: Const :: zero_sized ( ty) ,
2336
- ) ,
2329
+ literal : ty:: Const :: zero_sized ( tcx, ty) ,
2337
2330
} )
2338
2331
}
2339
2332
@@ -2827,67 +2820,15 @@ newtype_index! {
2827
2820
2828
2821
impl < ' tcx > Debug for Constant < ' tcx > {
2829
2822
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 )
2861
2824
}
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)
2887
2825
}
2888
2826
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
+ }
2891
2832
}
2892
2833
2893
2834
impl < ' tcx > graph:: DirectedGraph for Mir < ' tcx > {
0 commit comments