Skip to content

Commit 7d67a1b

Browse files
committed
Replace write-to-vec hack by introducing a display renderer for allocations
1 parent 5e96bb4 commit 7d67a1b

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

src/librustc_mir/interpret/memory.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -741,13 +741,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
741741
for &(_, target_id) in alloc.relocations().values() {
742742
allocs_to_print.push_back(target_id);
743743
}
744-
// This vec dance is necessary, because there is no trait
745-
// that supports writing to files and to `std::fmt::Formatter` at the
746-
// same time.
747-
let mut v = Vec::new();
748-
pretty::write_allocation(tcx, alloc, &mut v).unwrap();
749-
let s = String::from_utf8(v).unwrap();
750-
fmt.write_str(&s)
744+
write!(fmt, "{}", pretty::display_allocation(tcx, alloc))
751745
}
752746

753747
let mut allocs_to_print: VecDeque<_> = self.allocs.iter().copied().collect();

src/librustc_mir/util/pretty.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub fn write_allocations<'tcx>(
588588
todo.push(id);
589589
}
590590
}
591-
write_allocation(tcx, alloc, w)
591+
write!(w, "{}", display_allocation(tcx, alloc))
592592
};
593593
write!(w, "\n{}", id)?;
594594
match tcx.get_global_alloc(id) {
@@ -640,24 +640,36 @@ pub fn write_allocations<'tcx>(
640640
/// After the hex dump, an ascii dump follows, replacing all unprintable characters (control
641641
/// characters or characters whose value is larger than 127) with a `.`
642642
/// This also prints relocations adequately.
643-
pub fn write_allocation<Tag: Copy + Debug, Extra>(
643+
pub fn display_allocation<Tag: Copy + Debug, Extra>(
644644
tcx: TyCtxt<'tcx>,
645-
alloc: &Allocation<Tag, Extra>,
646-
w: &mut dyn Write,
647-
) -> io::Result<()> {
648-
write!(w, "size: {}, align: {})", alloc.size.bytes(), alloc.align.bytes())?;
649-
if alloc.size == Size::ZERO {
650-
// We are done.
651-
return write!(w, " {{}}");
645+
alloc: &'a Allocation<Tag, Extra>,
646+
) -> RenderAllocation<'a, 'tcx, Tag, Extra> {
647+
RenderAllocation { tcx, alloc }
648+
}
649+
650+
#[doc(hidden)]
651+
pub struct RenderAllocation<'a, 'tcx, Tag, Extra> {
652+
tcx: TyCtxt<'tcx>,
653+
alloc: &'a Allocation<Tag, Extra>,
654+
}
655+
656+
impl<Tag: Copy + Debug, Extra> std::fmt::Display for RenderAllocation<'a, 'tcx, Tag, Extra> {
657+
fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
658+
let RenderAllocation { tcx, alloc } = *self;
659+
write!(w, "size: {}, align: {})", alloc.size.bytes(), alloc.align.bytes())?;
660+
if alloc.size == Size::ZERO {
661+
// We are done.
662+
return write!(w, " {{}}");
663+
}
664+
// Write allocation bytes.
665+
writeln!(w, " {{")?;
666+
write_allocation_bytes(tcx, alloc, w, " ")?;
667+
write!(w, "}}")?;
668+
Ok(())
652669
}
653-
// Write allocation bytes.
654-
writeln!(w, " {{")?;
655-
write_allocation_bytes(tcx, alloc, w, " ")?;
656-
write!(w, "}}")?;
657-
Ok(())
658670
}
659671

660-
fn write_allocation_endline(w: &mut dyn Write, ascii: &str) -> io::Result<()> {
672+
fn write_allocation_endline(w: &mut dyn std::fmt::Write, ascii: &str) -> std::fmt::Result {
661673
for _ in 0..(BYTES_PER_LINE - ascii.chars().count()) {
662674
write!(w, " ")?;
663675
}
@@ -669,12 +681,12 @@ const BYTES_PER_LINE: usize = 16;
669681

670682
/// Prints the line start address and returns the new line start address.
671683
fn write_allocation_newline(
672-
w: &mut dyn Write,
684+
w: &mut dyn std::fmt::Write,
673685
mut line_start: Size,
674686
ascii: &str,
675687
pos_width: usize,
676688
prefix: &str,
677-
) -> io::Result<Size> {
689+
) -> Result<Size, std::fmt::Error> {
678690
write_allocation_endline(w, ascii)?;
679691
line_start += Size::from_bytes(BYTES_PER_LINE);
680692
write!(w, "{}0x{:02$x} │ ", prefix, line_start.bytes(), pos_width)?;
@@ -687,9 +699,9 @@ fn write_allocation_newline(
687699
fn write_allocation_bytes<Tag: Copy + Debug, Extra>(
688700
tcx: TyCtxt<'tcx>,
689701
alloc: &Allocation<Tag, Extra>,
690-
w: &mut dyn Write,
702+
w: &mut dyn std::fmt::Write,
691703
prefix: &str,
692-
) -> io::Result<()> {
704+
) -> std::fmt::Result {
693705
let num_lines = alloc.size.bytes_usize().saturating_sub(BYTES_PER_LINE);
694706
// Number of chars needed to represent all line numbers.
695707
let pos_width = format!("{:x}", alloc.size.bytes()).len();

0 commit comments

Comments
 (0)