Rollup merge of #97225 - cuviper:ref-display, r=scottmcm

Fix `Display` for `cell::{Ref,RefMut}`

These guards changed to pointers in #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, #97204, so hopefully that will be fixed.
This commit is contained in:
Yuki Okushi 2022-05-22 11:53:05 +09:00 committed by GitHub
commit d22ebf0d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View File

@ -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")] #[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for Ref<'_, T> { impl<T: ?Sized + fmt::Display> fmt::Display for Ref<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f) (**self).fmt(f)
} }
} }
@ -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")] #[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> { impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f) (**self).fmt(f)
} }
} }

View File

@ -73,11 +73,13 @@ fn ref_and_refmut_have_sensible_show() {
let refcell = RefCell::new("foo"); let refcell = RefCell::new("foo");
let refcell_refmut = refcell.borrow_mut(); 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); drop(refcell_refmut);
let refcell_ref = refcell.borrow(); 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); drop(refcell_ref);
} }