-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Can't use #[derive] and macro on a generic type at the same time #50676
Comments
It is intentional (here is the historical record), but there is a possibility the situation could be improved in the future, and at least the error message should be rewritten to explain why it refuses to compile. The underlying issue is that #[derive(Debug)]
struct MyStruct<T: FromStr> {
field: T
} We need to generate #[derive(Debug)]
struct MyStruct<T> {
field: T::Err
} Here the field is an associated type, so the generated code actually needs to be The derive macros actually scan the field types to see whether they need to bound When this was discovered we couldn't decide whether to let the type macro be expanded eagerly (seems like you could get into a loop or ordering issues), just copy the macro into the |
As a concrete solution, use a crate like derivative where you can fix the generated bounds: #[derive(Derivative)]
#[derivative(Debug)]
struct MyStruct<T> {
#[derivative(Debug(bound=""))] // or "typ!(): ::std::fmt::Debug"
field: typ!(),
} |
Triage: the error is a bit better now:
|
Huh? That's exactly the same error. The only difference is that since rustc 1.33, there's now enough error recovery in order to also print the unused type parameter error. But that is orthogonal and doesn't get any closer to pointing out what causes the derive error. |
That was all that I meant. |
I just ran into this while adding a generic parameter to an ast enum I had in a parser. I found the error message somewhat confusing because it says " Perhaps it's worth tweaking the error message to say " That would have saved me a bit of head scratching 😅 |
I have this code:
The compiler gives this error:
But if I change MyStruct into non-generic type
It works without any error.
Why can't use #[derive] and macro on a generic type at the same time?
Is it a compiler bug?
-> Mirror issue on StackOverflow
The text was updated successfully, but these errors were encountered: