Skip to content

Commit 9b5896a

Browse files
committed
Render const byte slices in MIR
1 parent fa17654 commit 9b5896a

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::middle::cstore::{ExternCrate, ExternCrateSource};
66
use crate::middle::region;
77
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
88
use crate::ty::subst::{Kind, Subst, UnpackedKind};
9+
use crate::ty::layout::Size;
910
use crate::mir::interpret::{ConstValue, sign_extend, Scalar};
1011
use syntax::ast;
1112
use rustc_apfloat::ieee::{Double, Single};
@@ -1537,6 +1538,7 @@ define_print_and_forward_display! {
15371538
}
15381539

15391540
&'tcx ty::Const<'tcx> {
1541+
let u8 = cx.tcx().types.u8;
15401542
match (self.val, &self.ty.sty) {
15411543
| (ConstValue::Unevaluated(..), _)
15421544
| (ConstValue::Infer(..), _)
@@ -1566,28 +1568,38 @@ define_print_and_forward_display! {
15661568
(ConstValue::Scalar(Scalar::Bits { bits, ..}), ty::Char)
15671569
=> p!(write("{:?}", ::std::char::from_u32(bits as u32).unwrap())),
15681570
(_, ty::FnDef(did, _)) => p!(write("{}", cx.tcx().def_path_str(*did))),
1569-
(
1570-
ConstValue::Slice { data, start, end },
1571-
ty::Ref(_, slice_ty, _),
1572-
) => {
1573-
let slice = &data.bytes[start..end];
1574-
match slice_ty.sty {
1575-
ty::Str => {
1576-
let s = ::std::str::from_utf8(slice)
1577-
.expect("non utf8 str from miri");
1578-
p!(write("{:?}", s))
1579-
},
1580-
ty::Slice(elem) if elem == cx.tcx().types.u8 => {
1581-
p!(write("b\""));
1582-
for &c in slice {
1583-
for e in std::ascii::escape_default(c) {
1584-
p!(write("{}", e));
1585-
}
1571+
(_, ty::Ref(_, ref_ty, _)) => match (self.val, &ref_ty.sty) {
1572+
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => {
1573+
let n = n.unwrap_usize(cx.tcx());
1574+
let slice = cx.tcx()
1575+
.alloc_map.lock()
1576+
.unwrap_memory(ptr.alloc_id)
1577+
.get_bytes(&cx.tcx(), ptr, Size::from_bytes(n)).unwrap();
1578+
p!(write("b\""));
1579+
for &c in slice {
1580+
for e in std::ascii::escape_default(c) {
1581+
p!(write("{}", e));
15861582
}
1587-
p!(write("\""));
1588-
},
1589-
_ => bug!("invalid slice: {:#?}", self),
1590-
}
1583+
}
1584+
p!(write("\""));
1585+
},
1586+
(ConstValue::Slice { data, start, end }, ty::Str) => {
1587+
let slice = &data.bytes[start..end];
1588+
let s = ::std::str::from_utf8(slice)
1589+
.expect("non utf8 str from miri");
1590+
p!(write("{:?}", s))
1591+
},
1592+
(ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => {
1593+
let slice = &data.bytes[start..end];
1594+
p!(write("b\""));
1595+
for &c in slice {
1596+
for e in std::ascii::escape_default(c) {
1597+
p!(write("{}", e));
1598+
}
1599+
}
1600+
p!(write("\""));
1601+
},
1602+
_ => p!(write("{:?} : ", self.val), print(self.ty)),
15911603
},
15921604
_ => p!(write("{:?} : ", self.val), print(self.ty)),
15931605
}

src/test/mir-opt/byte_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
// END RUST SOURCE
99
// START rustc.main.EraseRegions.after.mir
1010
// ...
11-
// _1 = const Scalar(Ptr(Pointer { alloc_id: AllocId(0), offset: Size { raw: 0 }, tag: () })) : &[u8; 3];
11+
// _1 = const b"102111111";
1212
// ...
1313
// _2 = [const 5u8, const 120u8];
1414
// ...

0 commit comments

Comments
 (0)