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

#[derive(TransparentWrapper)] doesn't work with where clause generic bounds. #241

Open
JayWhite2357 opened this issue May 24, 2024 · 1 comment
Labels
proc-macros I don't do proc-macros, but I accepts PRs about them.

Comments

@JayWhite2357
Copy link
Contributor

JayWhite2357 commented May 24, 2024

Here is a MWE of code that I have:

trait MyTrait {}
#[derive(TransparentWrapper)]
#[repr(transparent)]
struct A<T>(T)
where
    T: MyTrait;

However, this gives the error

the trait bound `T: MyTrait` is not satisfied
the trait `MyTrait` is not implemented for `T`

because the macro generates

unsafe impl<T> ::bytemuck::TransparentWrapper<T> for A<T> {}

rather than

unsafe impl<T> ::bytemuck::TransparentWrapper<T> for A<T> where T: MyTrait {}

The solution is to do this instead:

trait MyTrait {}
#[derive(TransparentWrapper)]
#[repr(transparent)]
struct A<T: MyTrait>(T);

However, as of Rust 1.78, clippy now gives the following message: bound is defined in more than one place because #[warn(clippy::multiple_bound_locations)] is on by default, and it can only be turned off at the module level because #[allow(clippy::multiple_bound_locations)] needs to be applied to the macro generated code.

Suggestion 1: avoid using where clauses for generics in the macro generated code.
Suggestion 2: add #[allow(clippy::multiple_bound_locations)] to the macro generated code.
Suggestion 3: generate correct code when there are where clause bounds for generics.

@Lokathor
Copy link
Owner

Since #242 is merged, can we close this, or was that just a partial fix?

@Lokathor Lokathor added the proc-macros I don't do proc-macros, but I accepts PRs about them. label Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proc-macros I don't do proc-macros, but I accepts PRs about them.
Projects
None yet
Development

No branches or pull requests

2 participants