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

Add ability to apply custom derive to union types. #50383

Merged
merged 1 commit into from
Jun 19, 2018

Conversation

stevepentland
Copy link
Contributor

@stevepentland stevepentland commented May 2, 2018

The Union item type has been included in the allowed types for a custom derive.
fyi @abonander

Closes #50223

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2018
@abonander
Copy link
Contributor

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

extern crate derive_union;

#[repr(C)]
#[derive(Union)]
Copy link
Contributor

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.

_ => {
ecx.span_err(span, "proc-macro derives may only be \
applied to struct/enum items");
applied to a struct, enum, or union item");
Copy link
Contributor

@abonander abonander May 2, 2018

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.

@abonander
Copy link
Contributor

Looks all right to me! Now to see if it passes the build and the official review.

@abonander
Copy link
Contributor

@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 git rebase?

@stevepentland
Copy link
Contributor Author

you don't have to do it right now but the reviewer will probably want you to squash your second commit onto the first

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.

@abonander
Copy link
Contributor

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.

@alexcrichton alexcrichton added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label May 2, 2018
@alexcrichton
Copy link
Member

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?

@abonander
Copy link
Contributor

Builtin derives are already allowed on unions so I consider it a discrepancy that custom derives aren't allowed. This was discussed in #50223.

@cramertj
Copy link
Member

cramertj commented May 2, 2018

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented May 2, 2018

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.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 2, 2018
@scottmcm
Copy link
Member

scottmcm commented May 2, 2018

@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"?

@joshtriplett
Copy link
Member

joshtriplett commented May 2, 2018

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

@withoutboats
Copy link
Contributor

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.

@alexcrichton
Copy link
Member

@scottmcm I'm mostly just the man in the middle, I'd ask that question to #50223

@nikomatsakis
Copy link
Contributor

I think the reason we initially did not include union was that it can so easily go wrong. e.g., #[derive(Ord)] or something couldn't possible work. That said, I think that having the ability to derive things on unions makes sense.

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?

@nikomatsakis
Copy link
Contributor

But that seems more like a "libs" problem, honestly.

@withoutboats
Copy link
Contributor

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?

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

@shepmaster shepmaster added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 6, 2018
@pietroalbini
Copy link
Member

Ping from triage @aturon @nrc @pnkfelix @scottmcm! There are some checkboxes to check.

@rfcbot
Copy link

rfcbot commented Jun 3, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jun 3, 2018
@rfcbot
Copy link

rfcbot commented Jun 13, 2018

The final comment period, with a disposition to merge, as per the review above, is now complete.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jun 13, 2018
@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Jun 13, 2018
@pietroalbini
Copy link
Member

The reviewer is currently away, can someone else from @rust-lang/compiler check in on this?

@oli-obk
Copy link
Contributor

oli-obk commented Jun 18, 2018

@bors r+

@bors
Copy link
Contributor

bors commented Jun 18, 2018

📌 Commit 4be07af has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 18, 2018
@bors
Copy link
Contributor

bors commented Jun 18, 2018

🔒 Merge conflict

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 18, 2018
@pietroalbini
Copy link
Member

@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
@stevepentland
Copy link
Contributor Author

@pietroalbini done

@pietroalbini
Copy link
Member

@oli-obk and now you need to approve this again...

@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2018

@bors r+

@bors
Copy link
Contributor

bors commented Jun 19, 2018

📌 Commit 14abb55 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 19, 2018
@bors
Copy link
Contributor

bors commented Jun 19, 2018

⌛ Testing commit 14abb55 with merge 1080203...

bors added a commit that referenced this pull request Jun 19, 2018
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
@bors
Copy link
Contributor

bors commented Jun 19, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: oli-obk
Pushing 1080203 to master...

@bors bors merged commit 14abb55 into rust-lang:master Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.