Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsized fields usage in derive(Display) (#432) #440

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Top-level `#[display("...")]` attribute on an enum not working transparently
for directly specified fields.
([#438](https://github.com/JelteF/derive_more/pull/438))
- Incorrect dereferencing of unsized fields in `Debug` and `Display` expansions.
([#440](https://github.com/JelteF/derive_more/pull/440))


## 1.0.0 - 2024-08-07
Expand Down
8 changes: 6 additions & 2 deletions impl/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,19 @@ impl FmtAttribute {

/// Returns an [`Iterator`] over the additional formatting arguments doing the dereferencing
/// replacement in this [`FmtAttribute`] for those [`Placeholder`] representing the provided
/// [`syn::Fields`] and requiring it
/// [`syn::Fields`] and requiring it ([`fmt::Pointer`] ones).
///
/// [`fmt::Pointer`]: std::fmt::Pointer
fn additional_deref_args<'fmt: 'ret, 'fields: 'ret, 'ret>(
&'fmt self,
fields: &'fields syn::Fields,
) -> impl Iterator<Item = TokenStream> + 'ret {
let used_args = Placeholder::parse_fmt_string(&self.lit.value())
.into_iter()
.filter_map(|placeholder| match placeholder.arg {
Parameter::Named(name) => Some(name),
Parameter::Named(name) if placeholder.trait_name == "Pointer" => {
Some(name)
}
_ => None,
})
.collect::<Vec<_>>();
Expand Down
238 changes: 238 additions & 0 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,84 @@ mod structs {
}
}

mod r#unsized {
#[cfg(not(feature = "std"))]
use alloc::format;
use core::ptr;

use derive_more::Debug;

#[derive(Debug)]
struct Tuple(str);

#[derive(Debug)]
struct Struct {
tail: str,
}

#[test]
fn assert() {
let dat = "14";

let t =
unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple) };
assert_eq!(format!("{t:?}"), r#"Tuple("14")"#);
let s =
unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Struct) };
assert_eq!(format!("{s:?}"), r#"Struct { tail: "14" }"#);
}

mod interpolated {
#[cfg(not(feature = "std"))]
use alloc::format;
use core::ptr;

use derive_more::Debug;

#[derive(Debug)]
#[debug("{}.", _0)]
struct Tuple1(str);

#[derive(Debug)]
#[debug("{_0}.")]
struct Tuple2(str);

#[derive(Debug)]
#[debug("{}.", tail)]
struct Struct1 {
tail: str,
}

#[derive(Debug)]
#[debug("{tail}.")]
struct Struct2 {
tail: str,
}

#[test]
fn assert() {
let dat = "14";

let t1 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple1)
};
assert_eq!(format!("{t1:?}"), "14.");
let t2 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple2)
};
assert_eq!(format!("{t2:?}"), "14.");
let s1 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Struct1)
};
assert_eq!(format!("{s1:?}"), "14.");
let s2 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Struct2)
};
assert_eq!(format!("{s2:?}"), "14.");
}
}
}

#[cfg(nightly)]
mod never {
use derive_more::Debug;
Expand Down Expand Up @@ -691,6 +769,87 @@ mod structs {
}
}

mod r#unsized {
#[cfg(not(feature = "std"))]
use alloc::format;
use core::ptr;

use derive_more::Debug;

#[derive(Debug)]
struct Tuple(char, str);

#[derive(Debug)]
struct Struct {
head: char,
tail: str,
}

#[test]
fn assert() {
let dat = [51i32, 3028017];

let t =
unsafe { &*(ptr::addr_of!(dat) as *const [i32] as *const Tuple) };
assert_eq!(format!("{t:?}"), r#"Tuple('3', "14")"#);
let s =
unsafe { &*(ptr::addr_of!(dat) as *const [i32] as *const Struct) };
assert_eq!(format!("{s:?}"), r#"Struct { head: '3', tail: "14" }"#);
}

mod interpolated {
#[cfg(not(feature = "std"))]
use alloc::format;
use core::ptr;

use derive_more::Debug;

#[derive(Debug)]
#[debug("{}.{}", _0, _1)]
struct Tuple1(char, str);

#[derive(Debug)]
#[debug("{_0}.{_1}")]
struct Tuple2(char, str);

#[derive(Debug)]
#[debug("{}.{}", head, tail)]
struct Struct1 {
head: char,
tail: str,
}

#[derive(Debug)]
#[debug("{head}.{tail}")]
struct Struct2 {
head: char,
tail: str,
}

#[test]
fn assert() {
let dat = [51i32, 3028017];

let t1 = unsafe {
&*(ptr::addr_of!(dat) as *const [i32] as *const Tuple1)
};
assert_eq!(format!("{t1:?}"), "3.14");
let t2 = unsafe {
&*(ptr::addr_of!(dat) as *const [i32] as *const Tuple2)
};
assert_eq!(format!("{t2:?}"), "3.14");
let s1 = unsafe {
&*(ptr::addr_of!(dat) as *const [i32] as *const Struct1)
};
assert_eq!(format!("{s1:?}"), "3.14");
let s2 = unsafe {
&*(ptr::addr_of!(dat) as *const [i32] as *const Struct2)
};
assert_eq!(format!("{s2:?}"), "3.14");
}
}
}

#[cfg(nightly)]
mod never {
use derive_more::Debug;
Expand Down Expand Up @@ -2178,6 +2337,85 @@ mod generic {
}
}
}

mod r#unsized {
#[cfg(not(feature = "std"))]
use alloc::format;
use core::ptr;

use derive_more::Debug;

#[derive(Debug)]
struct Tuple<T: ?Sized>(T);

#[derive(Debug)]
struct Struct<T: ?Sized> {
tail: T,
}

#[test]
fn assert() {
let dat = "14";

let t =
unsafe { &*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple<str>) };
assert_eq!(format!("{t:?}"), r#"Tuple("14")"#);
let s = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Struct<str>)
};
assert_eq!(format!("{s:?}"), r#"Struct { tail: "14" }"#);
}

mod interpolated {
#[cfg(not(feature = "std"))]
use alloc::format;
use core::ptr;

use derive_more::Debug;

#[derive(Debug)]
#[debug("{}.", _0)]
struct Tuple1<T: ?Sized>(T);

#[derive(Debug)]
#[debug("{_0}.")]
struct Tuple2<T: ?Sized>(T);

#[derive(Debug)]
#[debug("{}.", tail)]
struct Struct1<T: ?Sized> {
tail: T,
}

#[derive(Debug)]
#[debug("{tail}.")]
struct Struct2<T: ?Sized> {
tail: T,
}

#[test]
fn assert() {
let dat = "14";

let t1 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple1<str>)
};
assert_eq!(format!("{t1:?}"), "14.");
let t2 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Tuple2<str>)
};
assert_eq!(format!("{t2:?}"), "14.");
let s1 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Struct1<str>)
};
assert_eq!(format!("{s1:?}"), "14.");
let s2 = unsafe {
&*(ptr::addr_of!(*dat) as *const [i32] as *const Struct2<str>)
};
assert_eq!(format!("{s2:?}"), "14.");
}
}
}
}

// See: https://github.com/JelteF/derive_more/issues/301
Expand Down
Loading
Loading