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 the problem with deriving Debug with other attributes #15

Merged
merged 3 commits into from
Nov 18, 2023

Conversation

jinankjain
Copy link
Contributor

This pull request tries to fix the problem pointed out in #14 and along with it also add a test to make sure that this does not happen in future.

@jinankjain
Copy link
Contributor Author

jinankjain commented Oct 23, 2023 via email

} else if derive.is_ident("Eq") {
our_derives.remove("Eq");
}
let ident_str = derive.get_ident().unwrap().to_string();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for derives that aren't a single identifier. Example: derive(zerocopy::AsBytes).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add zerocopy = { version = "0.7.11", features = ["derive"] } to dev-dependencies and ensure that derive(zerocopy::AsBytes, zerocopy::FromBytes) works in both the inner open enum and the outer struct? Without a use, so a full path derive can be tested.

The fact that derive(::Debug) or derive(core::fmt::Debug) does not work is a known issue, but it should work for cases like this. The order in which the derive is placed does matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a test for core::fmt::Debug. Is that good enough or should I add zerocopy as well?

Copy link
Owner

@kupiakos kupiakos Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also like a test with another derive that should not be intercepted

should I add zerocopy as well

yes, please

tests/basic.rs Outdated Show resolved Hide resolved
@jinankjain jinankjain force-pushed the main branch 2 times, most recently from 7b84ef1 to 5ce12d1 Compare November 12, 2023 12:02
... in case we are emitting custom Debug impl trait implementation.
Otherwise it would conflict with the default Debug implementation.

Signed-off-by: Jinank Jain <[email protected]>
There are two new things added:
1. OpenEnum which mimics the failing build scenario
2. Struct which embeds that enum inside it and also derive Debug on top
   of it.

By doing so we verify first of all the code compiles with multiple
derive attributes and secondly the embedded debug derives works as
expected.

Signed-off-by: Jinank Jain <[email protected]>
@jinankjain
Copy link
Contributor Author

@kupiakos Can you please take a look now? I have tried to address your concern around single identifier.

@jinankjain jinankjain requested a review from kupiakos November 15, 2023 08:25
for nested_meta in nested.iter() {
if let NestedMeta::Meta(inner_meta) = nested_meta {
let inner_meta_str = format!("{}", quote::quote!(#inner_meta));
if inner_meta_str.contains("Debug") && !allow_alias {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break a derive some_crate::DeriveThatHasDebugInTheName.

Can you instead have it only "capture" a Debug derive if its path is exactly one of these:

  • Debug
  • fmt::Debug
  • ::core::fmt::Debug
  • core::fmt::Debug
  • ::std::fmt::Debug
  • std::fmt::Debug

It's unlikely (albeit possible) that these would conflict with other derives

Copy link
Owner

@kupiakos kupiakos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely better though it still has problems with intercepting derives that it should not

@jinankjain
Copy link
Contributor Author

intercepting

I could not think of way of removing the intercept without parsing the derives actually... Is there something else that you could recommend?

It will now check against a (module, Name) pair to determine how to
intercept PartialEq, Eq, and Debug.

Fixes kupiakos#14.
@kupiakos
Copy link
Owner

I could not think of way of removing the intercept without parsing the derives actually... Is there something else that you could recommend?

I added a new function to check against prelude derive paths which should work in all desired cases.

@kupiakos kupiakos merged commit c9a5730 into kupiakos:main Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants