-
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
Add ability to apply custom derive to union types. #50383
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Excellent! If you would, edit your PR to say "closes #50223" at the end, that will automatically close that issue when this is merged. |
#[proc_macro_derive(Union)] | ||
pub fn derive(input: TokenStream) -> TokenStream { | ||
let input = input.to_string(); | ||
assert_eq!(EXPECTED, input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention is to put the actual value first (input
) and the expected value second. Then you can inline the EXPECTED
string without it being too ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was having problems with the formatting of the expected value when it was inline, I couldn't get the actual formatting of the incoming string (newlines, etc) to match up with the value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other derive tests appear to just assert that the input contains certain lines: https://github.com/rust-lang/rust/blob/master/src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-b.rs#L23
I don't think it affects the confidence interval of the test much since we're mostly just ensuring that derives are allowed to be applied to unions. I'll let @alexcrichton make the call on what he prefers to see.
d07cc5e
to
ee71c91
Compare
extern crate derive_union; | ||
|
||
#[repr(C)] | ||
#[derive(Union)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would maybe name the derive UnionTest
just to be perfectly clear. The filename is probably fine though.
src/libsyntax_ext/deriving/custom.rs
Outdated
_ => { | ||
ecx.span_err(span, "proc-macro derives may only be \ | ||
applied to struct/enum items"); | ||
applied to a struct, enum, or union item"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably drop the word "item" from this and the previous message as it doesn't really add anything.
Looks all right to me! Now to see if it passes the build and the official review. |
@stevepentland you don't have to do it right now but the reviewer will probably want you to squash your second commit onto the first. Do you know how to use |
Yeah, I've just didn't do it yet because of the stuff from highfive about adding them as extra commits to make tracking the changes easier. But since this is so small, I'll just do it right now. |
bcafaee
to
4be07af
Compare
It's normal to make a mess of the commit history during implementation/review and then squash at the end, that's why I said you didn't have to do it right away. |
The code changes and tests look good to me, thanks! This'll be an insta-stable addition to the language so I've tagged this as T-lang and now could I entice someone from @rust-lang/lang to propose a merge on this? |
Builtin derives are already allowed on unions so I consider it a discrepancy that custom derives aren't allowed. This was discussed in #50223. |
@rfcbot fcp merge |
Team member @cramertj has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@alexcrichton Can you summarize the why for this one? Is it mostly just "well, we allow other derives on unions so might as well allow custom ones too"? |
@scottmcm I can think of any number of good reasons to use a custom derive on a union type, either for a trait or synthetically as a marker to add other functionality to a type. Why would we allow it on a struct and not on a union? |
I think even the most minimal rational set of items that derives can be applied to (nominal type declarations) includes unions. I can't think of a name for the set that includes structs and enums but not unions. |
I think the reason we initially did not include I guess the question is: how do people write custom derives? Can we setup those libraries so that custom derives have to "opt in" to support union? |
But that seems more like a "libs" problem, honestly. |
Its already set up like that. Anyone using syn (which AFAIK is everyone) receives the type represented like this; they'd have to explicitly describe how to handle a union if they want to, or panic otherwise (its possible there are derives already set up to handle unions by authors who don't realize it doesn't work). |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
The reviewer is currently away, can someone else from @rust-lang/compiler check in on this? |
@bors r+ |
📌 Commit 4be07af has been approved by |
🔒 Merge conflict |
@stevepentland you need to rebase on top of the latest master, even if you don't see any conflicts. Sorry for the trouble! |
The Union item type has been included in the allowed types for a custom derive. Closes rust-lang#50223
4be07af
to
14abb55
Compare
@pietroalbini done |
@oli-obk and now you need to approve this again... |
@bors r+ |
📌 Commit 14abb55 has been approved by |
Add ability to apply custom derive to union types. The Union item type has been included in the allowed types for a custom derive. fyi @abonander Closes #50223
☀️ Test successful - status-appveyor, status-travis |
The Union item type has been included in the allowed types for a custom derive.
fyi @abonander
Closes #50223