avoid having both Debug and Display for a type and using the wrong one

This commit is contained in:
Ralf Jung 2019-05-24 10:09:18 +02:00
parent ff6f101074
commit 5569fffe7d

View File

@ -18,16 +18,16 @@ pub type PtrId = NonZeroU64;
pub type CallId = NonZeroU64; pub type CallId = NonZeroU64;
/// Tracking pointer provenance /// Tracking pointer provenance
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum Tag { pub enum Tag {
Tagged(PtrId), Tagged(PtrId),
Untagged, Untagged,
} }
impl fmt::Display for Tag { impl fmt::Debug for Tag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Tag::Tagged(id) => write!(f, "{}", id), Tag::Tagged(id) => write!(f, "<{}>", id),
Tag::Untagged => write!(f, "<untagged>"), Tag::Untagged => write!(f, "<untagged>"),
} }
} }
@ -48,7 +48,7 @@ pub enum Permission {
} }
/// An item in the per-location borrow stack. /// An item in the per-location borrow stack.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Item { pub struct Item {
/// The permission this item grants. /// The permission this item grants.
perm: Permission, perm: Permission,
@ -58,9 +58,9 @@ pub struct Item {
protector: Option<CallId>, protector: Option<CallId>,
} }
impl fmt::Display for Item { impl fmt::Debug for Item {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{:?} for {}", self.perm, self.tag)?; write!(f, "[{:?} for {:?}", self.perm, self.tag)?;
if let Some(call) = self.protector { if let Some(call) = self.protector {
write!(f, " (call {})", call)?; write!(f, " (call {})", call)?;
} }
@ -99,7 +99,7 @@ pub struct GlobalState {
pub type MemoryState = Rc<RefCell<GlobalState>>; pub type MemoryState = Rc<RefCell<GlobalState>>;
/// Indicates which kind of access is being performed. /// Indicates which kind of access is being performed.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum AccessKind { pub enum AccessKind {
Read, Read,
Write, Write,
@ -108,8 +108,8 @@ pub enum AccessKind {
impl fmt::Display for AccessKind { impl fmt::Display for AccessKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
AccessKind::Read => write!(f, "read"), AccessKind::Read => write!(f, "read access"),
AccessKind::Write => write!(f, "write"), AccessKind::Write => write!(f, "write access"),
} }
} }
} }
@ -117,7 +117,7 @@ impl fmt::Display for AccessKind {
/// Indicates which kind of reference is being created. /// Indicates which kind of reference is being created.
/// Used by high-level `reborrow` to compute which permissions to grant to the /// Used by high-level `reborrow` to compute which permissions to grant to the
/// new pointer. /// new pointer.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub enum RefKind { pub enum RefKind {
/// `&mut` and `Box`. /// `&mut` and `Box`.
Unique { two_phase: bool }, Unique { two_phase: bool },
@ -261,12 +261,12 @@ impl<'tcx> Stack {
if global.is_active(call) { if global.is_active(call) {
if let Some(tag) = tag { if let Some(tag) = tag {
return err!(MachineError(format!( return err!(MachineError(format!(
"not granting access to tag {} because incompatible item is protected: {}", "not granting access to tag {:?} because incompatible item is protected: {:?}",
tag, item tag, item
))); )));
} else { } else {
return err!(MachineError(format!( return err!(MachineError(format!(
"deallocating while item is protected: {}", item "deallocating while item is protected: {:?}", item
))); )));
} }
} }
@ -287,7 +287,7 @@ impl<'tcx> Stack {
// Step 1: Find granting item. // Step 1: Find granting item.
let granting_idx = self.find_granting(access, tag) let granting_idx = self.find_granting(access, tag)
.ok_or_else(|| InterpError::MachineError(format!( .ok_or_else(|| InterpError::MachineError(format!(
"no item granting {} access to tag {} found in borrow stack", "no item granting {} to tag {:?} found in borrow stack",
access, tag, access, tag,
)))?; )))?;
@ -298,7 +298,7 @@ impl<'tcx> Stack {
// pointers become invalid on write accesses (ensures F2a, and ensures U2 for write accesses). // pointers become invalid on write accesses (ensures F2a, and ensures U2 for write accesses).
let first_incompatible_idx = self.find_first_write_incompaible(granting_idx); let first_incompatible_idx = self.find_first_write_incompaible(granting_idx);
for item in self.borrows.drain(first_incompatible_idx..).rev() { for item in self.borrows.drain(first_incompatible_idx..).rev() {
trace!("access: popping item {}", item); trace!("access: popping item {:?}", item);
Stack::check_protector(&item, Some(tag), global)?; Stack::check_protector(&item, Some(tag), global)?;
} }
} else { } else {
@ -313,7 +313,7 @@ impl<'tcx> Stack {
for idx in (granting_idx+1 .. self.borrows.len()).rev() { for idx in (granting_idx+1 .. self.borrows.len()).rev() {
let item = &mut self.borrows[idx]; let item = &mut self.borrows[idx];
if item.perm == Permission::Unique { if item.perm == Permission::Unique {
trace!("access: disabling item {}", item); trace!("access: disabling item {:?}", item);
Stack::check_protector(item, Some(tag), global)?; Stack::check_protector(item, Some(tag), global)?;
item.perm = Permission::Disabled; item.perm = Permission::Disabled;
} }
@ -334,7 +334,7 @@ impl<'tcx> Stack {
// Step 1: Find granting item. // Step 1: Find granting item.
self.find_granting(AccessKind::Write, tag) self.find_granting(AccessKind::Write, tag)
.ok_or_else(|| InterpError::MachineError(format!( .ok_or_else(|| InterpError::MachineError(format!(
"no item granting write access for deallocation to tag {} found in borrow stack", "no item granting write access for deallocation to tag {:?} found in borrow stack",
tag, tag,
)))?; )))?;
@ -383,7 +383,7 @@ impl<'tcx> Stack {
// We use that to determine where to put the new item. // We use that to determine where to put the new item.
let granting_idx = self.find_granting(access, derived_from) let granting_idx = self.find_granting(access, derived_from)
.ok_or_else(|| InterpError::MachineError(format!( .ok_or_else(|| InterpError::MachineError(format!(
"trying to reborrow for {:?}, but parent tag {} does not have an appropriate item in the borrow stack", new.perm, derived_from, "trying to reborrow for {:?}, but parent tag {:?} does not have an appropriate item in the borrow stack", new.perm, derived_from,
)))?; )))?;
// Compute where to put the new item. // Compute where to put the new item.
@ -412,9 +412,9 @@ impl<'tcx> Stack {
// Put the new item there. As an optimization, deduplicate if it is equal to one of its new neighbors. // Put the new item there. As an optimization, deduplicate if it is equal to one of its new neighbors.
if self.borrows[new_idx-1] == new || self.borrows.get(new_idx) == Some(&new) { if self.borrows[new_idx-1] == new || self.borrows.get(new_idx) == Some(&new) {
// Optimization applies, done. // Optimization applies, done.
trace!("reborrow: avoiding adding redundant item {}", new); trace!("reborrow: avoiding adding redundant item {:?}", new);
} else { } else {
trace!("reborrow: adding item {}", new); trace!("reborrow: adding item {:?}", new);
self.borrows.insert(new_idx, new); self.borrows.insert(new_idx, new);
} }
@ -497,7 +497,7 @@ impl AllocationExtra<Tag> for Stacks {
ptr: Pointer<Tag>, ptr: Pointer<Tag>,
size: Size, size: Size,
) -> EvalResult<'tcx> { ) -> EvalResult<'tcx> {
trace!("read access with tag {}: {:?}, size {}", ptr.tag, ptr, size.bytes()); trace!("read access with tag {:?}: {:?}, size {}", ptr.tag, ptr, size.bytes());
alloc.extra.for_each(ptr, size, |stack, global| { alloc.extra.for_each(ptr, size, |stack, global| {
stack.access(AccessKind::Read, ptr.tag, global)?; stack.access(AccessKind::Read, ptr.tag, global)?;
Ok(()) Ok(())
@ -510,7 +510,7 @@ impl AllocationExtra<Tag> for Stacks {
ptr: Pointer<Tag>, ptr: Pointer<Tag>,
size: Size, size: Size,
) -> EvalResult<'tcx> { ) -> EvalResult<'tcx> {
trace!("write access with tag {}: {:?}, size {}", ptr.tag, ptr, size.bytes()); trace!("write access with tag {:?}: {:?}, size {}", ptr.tag, ptr, size.bytes());
alloc.extra.for_each(ptr, size, |stack, global| { alloc.extra.for_each(ptr, size, |stack, global| {
stack.access(AccessKind::Write, ptr.tag, global)?; stack.access(AccessKind::Write, ptr.tag, global)?;
Ok(()) Ok(())
@ -523,7 +523,7 @@ impl AllocationExtra<Tag> for Stacks {
ptr: Pointer<Tag>, ptr: Pointer<Tag>,
size: Size, size: Size,
) -> EvalResult<'tcx> { ) -> EvalResult<'tcx> {
trace!("deallocation with tag {}: {:?}, size {}", ptr.tag, ptr, size.bytes()); trace!("deallocation with tag {:?}: {:?}, size {}", ptr.tag, ptr, size.bytes());
alloc.extra.for_each(ptr, size, |stack, global| { alloc.extra.for_each(ptr, size, |stack, global| {
stack.dealloc(ptr.tag, global) stack.dealloc(ptr.tag, global)
}) })
@ -545,7 +545,7 @@ trait EvalContextPrivExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
let this = self.eval_context_mut(); let this = self.eval_context_mut();
let protector = if protect { Some(this.frame().extra) } else { None }; let protector = if protect { Some(this.frame().extra) } else { None };
let ptr = place.ptr.to_ptr()?; let ptr = place.ptr.to_ptr()?;
trace!("reborrow: {} reference {} derived from {} (pointee {}): {:?}, size {}", trace!("reborrow: {} reference {:?} derived from {:?} (pointee {}): {:?}, size {}",
kind, new_tag, ptr.tag, place.layout.ty, ptr, size.bytes()); kind, new_tag, ptr.tag, place.layout.ty, ptr, size.bytes());
// Get the allocation. It might not be mutable, so we cannot use `get_mut`. // Get the allocation. It might not be mutable, so we cannot use `get_mut`.