-
-
Notifications
You must be signed in to change notification settings - Fork 76
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
Derive macro for Format does not set generic bound correctly #799
Comments
OK, interesting! We're going to need to think about this one, and we're also just about to head into Christmas break. We'll get back to this in 2024. |
My colleague offers this expansion of some built-in derives: trait Foo {
type Bar;
}
#[derive(Clone, Debug)]
struct Baz<T: Foo> {
field: T::Bar,
} expands to #[automatically_derived]
impl<T: ::core::clone::Clone + Foo> ::core::clone::Clone for Baz<T>
where
T::Bar: ::core::clone::Clone,
{
#[inline]
fn clone(&self) -> Baz<T> {
Baz {
field: ::core::clone::Clone::clone(&self.field),
}
}
}
#[automatically_derived]
impl<T: ::core::fmt::Debug + Foo> ::core::fmt::Debug for Baz<T>
where
T::Bar: ::core::fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "Baz", "field", &&self.field)
}
} It probably makes sense to emit something similar. I don't fully understand why |
More details on this behaviour: https://smallcultfollowing.com/babysteps/blog/2022/04/12/implied-bounds-and-perfect-derive/ |
Fix trait bounds issue #799
I see 2 non-exclusive options going forward:
See jamesmunns/postcard#153 for a similar issue in another crate. I think the first option is pretty easy to implement and would provide a solid workaround. |
Didn't PR #800 already fix this? I think this issue can be closed now. |
Is #800 part of 0.3.8? If yes, then I'll extract a minimal example from my concrete problem. |
While trying to create a minimal example, I found the issue. The |
The next release will bump the minimum version. |
The currently implementation of the derive macro does set
T: defmt::Format
bounds for all generic parametersT
. This is not allways correct. Consider the following example:This will generate the trait bound
T: defmt::Format
instead ofT::Bar: defmt::Format
.I am not quite sure what the general rule is in this case, but the easiest thing to do would be to require
: defmt::Format
for all field types whether they are generic or not.I could try to work on this, but I never wrote procedural macros before, so I am not quite sure how to solve this.
Currently this code is responsible for the where clause:
defmt/macros/src/derives/format/codegen.rs
Lines 47 to 55 in 06650b9
I think
where_clause
should be moved toEncodeData
and constructed inencode_struct_data
from the fields.The text was updated successfully, but these errors were encountered: