15
15
use hir:: def:: CtorKind ;
16
16
use hir:: def_id:: DefId ;
17
17
use hir:: { self , HirId , InlineAsm } ;
18
- use mir:: interpret:: { ConstValue , EvalErrorKind , Scalar } ;
18
+ use mir:: interpret:: { ConstValue , EvalErrorKind , Scalar , Pointer , EvalResult } ;
19
19
use mir:: visit:: MirVisitable ;
20
20
use rustc_apfloat:: ieee:: { Double , Single } ;
21
21
use rustc_apfloat:: Float ;
@@ -39,7 +39,7 @@ use syntax_pos::{Span, DUMMY_SP};
39
39
use ty:: fold:: { TypeFoldable , TypeFolder , TypeVisitor } ;
40
40
use ty:: subst:: { CanonicalUserSubsts , Subst , Substs } ;
41
41
use ty:: { self , AdtDef , CanonicalTy , ClosureSubsts , GeneratorSubsts , Region , Ty , TyCtxt } ;
42
- use ty:: layout:: VariantIdx ;
42
+ use ty:: layout:: { VariantIdx , Size } ;
43
43
use util:: ppaux;
44
44
45
45
pub use mir:: interpret:: AssertMessage ;
@@ -2623,24 +2623,29 @@ pub fn fmt_const_val(f: &mut impl Write, const_val: &ty::Const<'_>) -> fmt::Resu
2623
2623
return write ! ( f, "{}" , item_path_str( did) ) ;
2624
2624
}
2625
2625
// print string literals
2626
- if let ConstValue :: ScalarPair ( ptr, len) = value {
2627
- if let Scalar :: Ptr ( ptr) = ptr {
2628
- if let Scalar :: Bits { bits : len, .. } = len {
2629
- if let Ref ( _, & ty:: TyS { sty : Str , .. } , _) = ty. sty {
2630
- return ty:: tls:: with ( |tcx| {
2631
- let alloc = tcx. alloc_map . lock ( ) . get ( ptr. alloc_id ) ;
2632
- if let Some ( interpret:: AllocType :: Memory ( alloc) ) = alloc {
2633
- assert_eq ! ( len as usize as u128 , len) ;
2634
- let slice =
2635
- & alloc. bytes [ ( ptr. offset . bytes ( ) as usize ) ..] [ ..( len as usize ) ] ;
2636
- let s = :: std:: str:: from_utf8 ( slice) . expect ( "non utf8 str from miri" ) ;
2637
- write ! ( f, "{:?}" , s)
2638
- } else {
2639
- write ! ( f, "pointer to erroneous constant {:?}, {:?}" , ptr, len)
2640
- }
2641
- } ) ;
2626
+ if let ConstValue :: ByRef ( alloc_id, alloc, offset) = value {
2627
+ if let Ref ( _, & ty:: TyS { sty : Str , .. } , _) = ty. sty {
2628
+ return ty:: tls:: with ( |tcx| {
2629
+ let mut dummy = || -> EvalResult < ' _ , _ > {
2630
+ let ptr = Pointer :: new ( alloc_id, offset) ;
2631
+ let slice_ptr = alloc. read_ptr ( & tcx, ptr) ?;
2632
+ let ptr = ptr. offset ( tcx. data_layout . pointer_size , & tcx) ?;
2633
+ let len = alloc. read_usize ( & tcx, ptr) ?;
2634
+ let alloc = tcx. alloc_map . lock ( ) . get ( slice_ptr. alloc_id ) ;
2635
+ if let Some ( interpret:: AllocType :: Memory ( alloc) ) = alloc {
2636
+ let slice = alloc. get_bytes ( & tcx, slice_ptr, Size :: from_bytes ( len) ) ?;
2637
+ let s = :: std:: str:: from_utf8 ( slice)
2638
+ . map_err ( |err| EvalErrorKind :: ValidationFailure ( err. to_string ( ) ) ) ?;
2639
+ Ok ( write ! ( f, "{:?}" , s) )
2640
+ } else {
2641
+ Ok ( write ! ( f, "broken str slice: {:?}" , alloc) )
2642
+ }
2643
+ } ;
2644
+ match dummy ( ) {
2645
+ Ok ( res) => res,
2646
+ Err ( err) => write ! ( f, "broken str constant: {:?}" , err) ,
2642
2647
}
2643
- }
2648
+ } ) ;
2644
2649
}
2645
2650
}
2646
2651
// just raw dump everything else
0 commit comments