-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Use declarative macro for #[derive(TryFromU32)]
#145495
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in coverage instrumentation. cc @Zalathar |
This comment has been minimized.
This comment has been minimized.
a1f545b
to
0bb8a0e
Compare
r? @Zalathar |
Is there some reason to do this? I don’t understand the motivation for wanting to make these enums considerably harder to maintain. |
procedural macros hurt build times because it feeds tokens to a parser for each procedural macro called for each type. There's no point in using a procedural macro for something this trivial. There are alternatives to this, if you don't like maintaining two lists (is that really hard? just two lists, and you get compiler errors if one doesn't match the other). One is to wrap the whole thing in the declarative macro, doable but I don't like how it looks. The other is to use the upcoming On the other hand, one additional procedural macro in |
I've written and used macros like this before and as I've kept working on and around the types they're applied to, I've noticed various small papercuts that make it mildly more annoying to work with than I have less hands-on experience with attribute macros but they also have some papercuts (e.g., rust-analyzer problems like rust-lang/rust-analyzer#19944). They're also conceptually the wrong tool here: derive is exactly for adding impls, attr macros are the bigger gun for when you need to transform and re-emit the input tokens. I'd suggest waiting for #145208 to be merged and reach beta, then it can stay a derive with the same use-site ergonomics as always. This is a perfect use case for it, and this change doesn't seem particularly time critical. There shouldn't be any build time benefits since cg_llvm still depends on |
Normally I'm very much in favour of removing needlessly-complex macros from rustc; we have far too many of them already. The fact that I was willing to add a proc-macro (with tests!) in this case should indicate how strongly I believe that it really is justified. Not having to redundantly list all variants of an FFI enum is a really big deal, as is being able to easily add When #145208 is available in stage0, I think it might we worth trying to use that instead. But I don't see any benefit in removing the existing derive at this time. It meaningfully increases the difficulty of maintaining FFI bindings to LLVM, in exchange for shuffling macro complexity around without actually decreasing it. |
cool, #145208 is a thing. Marking this as blocked until that one lands. |
0bb8a0e
to
06498d7
Compare
This comment has been minimized.
This comment has been minimized.
#[derive(TryFromU32)]
#[derive(TryFromU32)]
06498d7
to
d429ed2
Compare
@rustbot ready |
macro_rules! TryFromU32 { | ||
derive() ($(#[$meta:meta])* $vis:vis enum $Type:ident {$( | ||
$(#[$varmeta:meta])* | ||
$Variant:ident $(= $discr:expr)? | ||
),*$(,)?}) => { | ||
impl ::core::convert::TryFrom<u32> for $Type { | ||
type Error = u32; | ||
#[allow(deprecated)] // Don't warn about deprecated variants. | ||
fn try_from(value: u32) -> ::core::result::Result<$Type, Self::Error> { | ||
$( if value == const { $Type::$Variant as u32 } { return Ok($Type::$Variant) } )* | ||
Err(value) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub(crate) use TryFromU32; | ||
|
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.
Nit: I would put the macro in its own submodule file (perhaps macros.rs
) so that we aren't adding more non-trivial code to lib.rs
.
derive() ($(#[$meta:meta])* $vis:vis enum $Type:ident {$( | ||
$(#[$varmeta:meta])* | ||
$Variant:ident $(= $discr:expr)? | ||
),*$(,)?}) => { |
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.
Nit: I find this compact style quite hard to read.
derive() ($(#[$meta:meta])* $vis:vis enum $Type:ident {$( | |
$(#[$varmeta:meta])* | |
$Variant:ident $(= $discr:expr)? | |
),*$(,)?}) => { | |
derive() ( | |
$(#[$meta:meta])* | |
$vis:vis enum $Type:ident { | |
$( | |
$(#[$varmeta:meta])* | |
$Variant:ident $(= $discr:expr)? | |
),* $(,)? | |
} | |
) => { |
I have some style nits, but otherwise LGTM. |
☔ The latest upstream changes (presumably #147384) made this pull request unmergeable. Please resolve the merge conflicts. |
d429ed2
to
696b6ac
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
@rustbot ready |
cc @Zalathar