Skip to content

Commit

Permalink
Rollup merge of rust-lang#97225 - cuviper:ref-display, r=scottmcm
Browse files Browse the repository at this point in the history
Fix `Display` for `cell::{Ref,RefMut}`

These guards changed to pointers in rust-lang#97027, but their `Display` was
formatting that field directly, which made it show the raw pointer
value. Now we go through `Deref` to display the real value again.

Miri noticed this change, rust-lang#97204, so hopefully that will be fixed.
  • Loading branch information
JohnTitor authored May 22, 2022
2 parents e1340f2 + 83abb7c commit d22ebf0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Ref<'b, U>> for Ref<'b,
#[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for Ref<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
(**self).fmt(f)
}
}

Expand Down Expand Up @@ -1735,7 +1735,7 @@ impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<RefMut<'b, U>> for RefM
#[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
(**self).fmt(f)
}
}

Expand Down
6 changes: 4 additions & 2 deletions library/core/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ fn ref_and_refmut_have_sensible_show() {
let refcell = RefCell::new("foo");

let refcell_refmut = refcell.borrow_mut();
assert!(format!("{refcell_refmut:?}").contains("foo"));
assert_eq!(format!("{refcell_refmut}"), "foo"); // Display
assert!(format!("{refcell_refmut:?}").contains("foo")); // Debug
drop(refcell_refmut);

let refcell_ref = refcell.borrow();
assert!(format!("{refcell_ref:?}").contains("foo"));
assert_eq!(format!("{refcell_ref}"), "foo"); // Display
assert!(format!("{refcell_ref:?}").contains("foo")); // Debug
drop(refcell_ref);
}

Expand Down

0 comments on commit d22ebf0

Please sign in to comment.