-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Warnings in code generated by proc_macro_derive should be on derive() attr #37563
Comments
This was caused by #36308, which intentionally changed custom derive error spans from the custom derive name's span to the item's span.
|
@SimonSapin the reasoning behind #36308 is that custom derives are used many more times than they are implemented. Sure, someone may implement a custom derive and their implementation contains 10 bugs. The first time each bug shows up, some early adopter will get a weird message, somehow track it down, file a ticket, and the bug is fixed. Hopefully another 10,000 people will use the custom derive without hitting bugs. The assumption is that a custom derive given a valid struct should not be generating invalid code. This assumption was violated in your case but I don't think it is the right thing to optimize for. If a custom derive generates invalid code, chances are it is because one of the 10,000 late adopters screwed something up in their struct, not one of the 10 early adopters hitting a bug in the implementation. There are so many things to screw up in a struct (using a type that is not imported, lifetimes snafu, wrong generic types, ...) and so many more late adopters that your situation feels like a rounding error. The error messages should reflect this balance. |
Ok, so maybe the span should not be reverted. Still, I think the current error message can be improved. At least it should mention that the warning is in code generated by a custom derive (and which one). Even if the root cause is in the type definition, that would help explain why the error message is talking about an (unreachable) expression when there is no expression at the designated span. |
macros 1.1: Allow proc_macro functions to declare attributes to be mark as used This PR allows proc macro functions to declare attribute names that should be marked as used when attached to the deriving item. There are a few questions for this PR. - Currently this uses a separate attribute named `#[proc_macro_attributes(..)]`, is this the best choice? - In order to make this work, the `check_attribute` function had to be modified to not error on attributes marked as used. This is a pretty large change in semantics, is there a better way to do this? - I've got a few clones where I don't know if I need them (like turning `item` into a `TokenStream`), can these be avoided? - Is switching to `MultiItemDecorator` the right thing here? Also fixes rust-lang#37563.
Now that proc_macro_derive functions no longer return the item, the span information of the derived items is no longer changed to that of the original item. |
… not on the type definition.
This warning started appearing in Servo recently:
It turns out that these types have
#[derive(Clone, Copy, HeapSizeOf)]
, with a dependency defining#[proc_macro_derive(HeapSizeOf)]
. The warning is in code generated by this macro.But at first I couldn’t figure it out. I had to ask on IRC and sfackler suggested it could be generated code.
It would have helped if the span of the error was not on the type definition, but on the attribute that caused code to be generated in the first place. Something like this:
The error message could also mention macros or generated code, like "unreachable expression in expansion of
derive_HeapSizeOf
".The text was updated successfully, but these errors were encountered: