Skip to content

Commit 1f3e247

Browse files
committed
indicate better which kind of memory got leaked
1 parent aecaeab commit 1f3e247

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/librustc_mir/interpret/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub trait AllocMap<K: Hash + Eq, V> {
7979
/// and some use case dependent behaviour can instead be applied.
8080
pub trait Machine<'mir, 'tcx>: Sized {
8181
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
82-
type MemoryKind: ::std::fmt::Debug + MayLeak + Eq + 'static;
82+
type MemoryKind: ::std::fmt::Debug + ::std::fmt::Display + MayLeak + Eq + 'static;
8383

8484
/// Tag tracked alongside every pointer. This is used to implement "Stacked Borrows"
8585
/// <https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html>.

src/librustc_mir/interpret/memory.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use std::borrow::Cow;
1010
use std::collections::VecDeque;
1111
use std::convert::TryFrom;
12+
use std::fmt;
1213
use std::ptr;
1314

1415
use rustc_ast::ast::Mutability;
@@ -46,6 +47,17 @@ impl<T: MayLeak> MayLeak for MemoryKind<T> {
4647
}
4748
}
4849

50+
impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52+
match self {
53+
MemoryKind::Stack => write!(f, "stack variable"),
54+
MemoryKind::Vtable => write!(f, "vtable"),
55+
MemoryKind::CallerLocation => write!(f, "caller location"),
56+
MemoryKind::Machine(m) => write!(f, "{}", m),
57+
}
58+
}
59+
}
60+
4961
/// Used by `get_size_and_align` to indicate whether the allocation needs to be live.
5062
#[derive(Debug, Copy, Clone)]
5163
pub enum AllocCheck {
@@ -259,7 +271,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
259271

260272
if alloc_kind != kind {
261273
throw_ub_format!(
262-
"deallocating `{:?}` memory using `{:?}` deallocation operation",
274+
"deallocating {} memory using {} deallocation operation",
263275
alloc_kind,
264276
kind
265277
);
@@ -677,22 +689,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
677689
match self.alloc_map.get(id) {
678690
Some(&(kind, ref alloc)) => {
679691
// normal alloc
680-
match kind {
681-
MemoryKind::Stack => eprint!(" (stack variable, "),
682-
MemoryKind::Vtable => eprint!(" (vtable, "),
683-
MemoryKind::CallerLocation => eprint!(" (caller_location, "),
684-
MemoryKind::Machine(m) if Some(m) == M::GLOBAL_KIND => {
685-
eprint!(" (global, ")
686-
}
687-
MemoryKind::Machine(m) => eprint!(" ({:?}, ", m),
688-
};
692+
eprint!(" ({}, ", kind);
689693
write_allocation_track_relocs(self.tcx, &mut allocs_to_print, alloc);
690694
}
691695
None => {
692696
// global alloc
693697
match self.tcx.alloc_map.lock().get(id) {
694698
Some(GlobalAlloc::Memory(alloc)) => {
695-
eprint!(" (global, ");
699+
eprint!(" (unchanged global, ");
696700
write_allocation_track_relocs(self.tcx, &mut allocs_to_print, alloc);
697701
}
698702
Some(GlobalAlloc::Function(func)) => {

0 commit comments

Comments
 (0)