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

Warnings in code generated by proc_macro_derive should be on derive() attr #37563

Closed
SimonSapin opened this issue Nov 3, 2016 · 4 comments · Fixed by #37614
Closed

Warnings in code generated by proc_macro_derive should be on derive() attr #37563

SimonSapin opened this issue Nov 3, 2016 · 4 comments · Fixed by #37614
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@SimonSapin
Copy link
Contributor

… not on the type definition.

This warning started appearing in Servo recently:

warning: unreachable expression, #[warn(unreachable_code)] on by default
   --> /home/simon/projects/servo1/components/script_traits/lib.rs:403:1
    |
403 | pub enum Milliseconds {}
    | ^^^^^^^^^^^^^^^^^^^^^^^^

warning: unreachable expression, #[warn(unreachable_code)] on by default
   --> /home/simon/projects/servo1/components/script_traits/lib.rs:406:1
    |
406 | pub enum Nanoseconds {}
    | ^^^^^^^^^^^^^^^^^^^^^^^

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:

405 | #[derive(Clone, Copy, HeapSizeOf)]
    |                       ^^^^^^^^^^

The error message could also mention macros or generated code, like "unreachable expression in expansion of derive_HeapSizeOf".

@bluss bluss added A-syntaxext Area: Syntax extensions A-diagnostics Area: Messages for errors, warnings, and lints labels Nov 3, 2016
@jseyfried jseyfried self-assigned this Nov 3, 2016
@jseyfried jseyfried added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) and removed A-syntaxext Area: Syntax extensions labels Nov 3, 2016
@jseyfried
Copy link
Contributor

jseyfried commented Nov 4, 2016

This was caused by #36308, which intentionally changed custom derive error spans from the custom derive name's span to the item's span.

For the record, I think the custom derive name's span is a better choice, but neither is optimal. Hopefully we'll change custom derives so that they cannot modify the underlying item (cc #35900) so that we can get the best of both worlds.

@dtolnay
Copy link
Member

dtolnay commented Nov 4, 2016

@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.

@SimonSapin
Copy link
Contributor Author

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.

eddyb added a commit to eddyb/rust that referenced this issue Nov 9, 2016
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.
@keeperofdakeys
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants