From 9b5896ade3ae279a2d91773b3ae0b097657b8b6e Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Thu, 11 Apr 2019 22:04:04 +0200 Subject: [PATCH] Render const byte slices in MIR --- src/librustc/ty/print/pretty.rs | 54 ++++++++++++++++++++------------- src/test/mir-opt/byte_slice.rs | 2 +- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 793d3d8ec6d..10038603f30 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -6,6 +6,7 @@ use crate::middle::cstore::{ExternCrate, ExternCrateSource}; use crate::middle::region; use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable}; use crate::ty::subst::{Kind, Subst, UnpackedKind}; +use crate::ty::layout::Size; use crate::mir::interpret::{ConstValue, sign_extend, Scalar}; use syntax::ast; use rustc_apfloat::ieee::{Double, Single}; @@ -1537,6 +1538,7 @@ define_print_and_forward_display! { } &'tcx ty::Const<'tcx> { + let u8 = cx.tcx().types.u8; match (self.val, &self.ty.sty) { | (ConstValue::Unevaluated(..), _) | (ConstValue::Infer(..), _) @@ -1566,28 +1568,38 @@ define_print_and_forward_display! { (ConstValue::Scalar(Scalar::Bits { bits, ..}), ty::Char) => p!(write("{:?}", ::std::char::from_u32(bits as u32).unwrap())), (_, ty::FnDef(did, _)) => p!(write("{}", cx.tcx().def_path_str(*did))), - ( - ConstValue::Slice { data, start, end }, - ty::Ref(_, slice_ty, _), - ) => { - let slice = &data.bytes[start..end]; - match slice_ty.sty { - ty::Str => { - let s = ::std::str::from_utf8(slice) - .expect("non utf8 str from miri"); - p!(write("{:?}", s)) - }, - ty::Slice(elem) if elem == cx.tcx().types.u8 => { - p!(write("b\"")); - for &c in slice { - for e in std::ascii::escape_default(c) { - p!(write("{}", e)); - } + (_, ty::Ref(_, ref_ty, _)) => match (self.val, &ref_ty.sty) { + (ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => { + let n = n.unwrap_usize(cx.tcx()); + let slice = cx.tcx() + .alloc_map.lock() + .unwrap_memory(ptr.alloc_id) + .get_bytes(&cx.tcx(), ptr, Size::from_bytes(n)).unwrap(); + p!(write("b\"")); + for &c in slice { + for e in std::ascii::escape_default(c) { + p!(write("{}", e)); } - p!(write("\"")); - }, - _ => bug!("invalid slice: {:#?}", self), - } + } + p!(write("\"")); + }, + (ConstValue::Slice { data, start, end }, ty::Str) => { + let slice = &data.bytes[start..end]; + let s = ::std::str::from_utf8(slice) + .expect("non utf8 str from miri"); + p!(write("{:?}", s)) + }, + (ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => { + let slice = &data.bytes[start..end]; + p!(write("b\"")); + for &c in slice { + for e in std::ascii::escape_default(c) { + p!(write("{}", e)); + } + } + p!(write("\"")); + }, + _ => p!(write("{:?} : ", self.val), print(self.ty)), }, _ => p!(write("{:?} : ", self.val), print(self.ty)), } diff --git a/src/test/mir-opt/byte_slice.rs b/src/test/mir-opt/byte_slice.rs index ae5d7c4895f..23faf7e16a7 100644 --- a/src/test/mir-opt/byte_slice.rs +++ b/src/test/mir-opt/byte_slice.rs @@ -8,7 +8,7 @@ fn main() { // END RUST SOURCE // START rustc.main.EraseRegions.after.mir // ... -// _1 = const Scalar(Ptr(Pointer { alloc_id: AllocId(0), offset: Size { raw: 0 }, tag: () })) : &[u8; 3]; +// _1 = const b"102111111"; // ... // _2 = [const 5u8, const 120u8]; // ...