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 ?Sized type parameters in Debug #289

Merged
merged 5 commits into from
Aug 14, 2023
Merged
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: 1 addition & 1 deletion impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn expand_struct(
.ident
.clone()
.map_or_else(|| syn::Member::Unnamed(i.into()), syn::Member::Named);
quote! { let #var = &self.#member; }
quote! { let #var = &&self.#member; }
});

let body = quote! {
Expand Down
64 changes: 61 additions & 3 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ mod enums {

mod generic {
#[cfg(not(feature = "std"))]
use alloc::format;
use alloc::{boxed::Box, format};
use core::fmt;

use derive_more::Debug;
Expand Down Expand Up @@ -666,6 +666,22 @@ mod generic {
);
}

#[derive(Debug)]
struct NamedGenericStructUnsized<T: ?Sized> {
field: T,
}
#[test]
fn named_generic_struct_unsized() {
assert_eq!(
format!("{:?}", NamedGenericStructUnsized { field: 1 }),
"NamedGenericStructUnsized { field: 1 }",
);
assert_eq!(
format!("{:#?}", NamedGenericStructUnsized { field: 1 }),
"NamedGenericStructUnsized {\n field: 1,\n}",
);
}

#[derive(Debug)]
struct NamedGenericStructIgnored<T> {
#[debug(ignore)]
Expand Down Expand Up @@ -826,6 +842,20 @@ mod generic {
);
}

#[derive(Debug)]
struct UnnamedGenericStructUnsized<T: ?Sized>(T);
#[test]
fn unnamed_generic_struct_unsized() {
assert_eq!(
format!("{:?}", UnnamedGenericStructUnsized(2)),
"UnnamedGenericStructUnsized(2)",
);
assert_eq!(
format!("{:#?}", UnnamedGenericStructUnsized(2)),
"UnnamedGenericStructUnsized(\n 2,\n)",
);
}

#[derive(Debug)]
struct UnnamedGenericStructIgnored<T>(#[debug(skip)] T);
#[test]
Expand Down Expand Up @@ -910,11 +940,11 @@ mod generic {
fn generic_enum() {
assert_eq!(
format!("{:?}", GenericEnum::A::<_, u8> { field: 1 }),
"A { field: 1 }"
"A { field: 1 }",
);
assert_eq!(
format!("{:#?}", GenericEnum::A::<_, u8> { field: 1 }),
"A {\n field: 1,\n}"
"A {\n field: 1,\n}",
);
assert_eq!(format!("{:?}", GenericEnum::B::<u8, _>(2)), "B(2)");
assert_eq!(
Expand All @@ -923,6 +953,34 @@ mod generic {
);
}

#[derive(derive_more::Debug)]
enum GenericEnumUnsized<A: ?Sized, B: ?Sized + 'static> {
A { field: Box<A> },
B(&'static B),
}
#[test]
fn generic_enum_unsized() {
assert_eq!(
format!("{:?}", GenericEnumUnsized::A::<i32, u8> { field: 1.into() }),
"A { field: 1 }",
);
assert_eq!(
format!(
"{:#?}",
GenericEnumUnsized::A::<i32, u8> { field: 1.into() },
),
"A {\n field: 1,\n}",
);
assert_eq!(
format!("{:?}", GenericEnumUnsized::B::<u8, i32>(&2)),
"B(2)",
);
assert_eq!(
format!("{:#?}", GenericEnumUnsized::B::<u8, i32>(&2)),
"B(\n 2,\n)",
);
}

#[derive(Debug)]
enum InterpolatedGenericEnum<A, B> {
A {
Expand Down