-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Provide a way for custom derives to know if they were invoked via #[derive_const]
#118580
Conversation
#[unstable(feature = "derive_const", issue = "none")] | ||
#[derive(Default, Clone)] | ||
#[non_exhaustive] | ||
pub struct DeriveExpansionOptions; |
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.
bikeshed name
#[unstable(feature = "derive_const", issue = "none")] | ||
impl !Send for DeriveExpansionOptions {} | ||
#[unstable(feature = "derive_const", issue = "none")] | ||
impl !Sync for DeriveExpansionOptions {} |
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.
for forward compatibility
This comment has been minimized.
This comment has been minimized.
87da6b8
to
1e4af5e
Compare
This comment has been minimized.
This comment has been minimized.
1e4af5e
to
25c6726
Compare
| incorrect number of function parameters | ||
| incorrect number of function parameters |
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.
This duplication makes me sad but it's annoying to fix. I'd need to modify note_type_err
just for this.
This comment has been minimized.
This comment has been minimized.
25c6726
to
541dd8c
Compare
This comment has been minimized.
This comment has been minimized.
Ah, I need to update r-a, too ( |
541dd8c
to
f4480cd
Compare
rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead. cc @rust-lang/rust-analyzer |
Pretty sure that this in-tree change is unavoidable. |
@rust-lang/libs-api, would you like to see an ACP for this first? TL;DR: Allow derive-proc-macros to have the signature |
(I should get to this on new year holidays.) |
☔ The latest upstream changes (presumably #119146) made this pull request unmergeable. Please resolve the merge conflicts. |
f4480cd
to
882ab9b
Compare
☔ The latest upstream changes (presumably #119542) made this pull request unmergeable. Please resolve the merge conflicts. |
882ab9b
to
3768c8f
Compare
&& tcx | ||
.features() | ||
.declared_lib_features | ||
.iter() | ||
.any(|&(feature, _)| feature == sym::derive_const) |
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 think I should actually be able to do:
&& tcx | |
.features() | |
.declared_lib_features | |
.iter() | |
.any(|&(feature, _)| feature == sym::derive_const) | |
&& tcx.features().declared(sym::derive_const) |
or is it
&& tcx | |
.features() | |
.declared_lib_features | |
.iter() | |
.any(|&(feature, _)| feature == sym::derive_const) | |
&& tcx.features().active(sym::derive_const) |
trait_name: &'static str, | ||
attributes: &'static [&'static str], | ||
expand: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy, | ||
expand: impl ~const ExpandCustomDerive<Discriminant>, |
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.
It's unfortunate that I had to enable const_trait_impl
and effects
to make this work. If I hadn't done it, I wouldn't have been able to call into_fn
in this const fn
.
@@ -506,3 +510,26 @@ impl ProcMacro { | |||
ProcMacro::Bang { name, client: Client::expand1(expand) } | |||
} | |||
} | |||
|
|||
#[const_trait] | |||
pub trait ExpandCustomDerive<Discriminant> { |
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 know that adding a “discriminant” to this trait is slightly ugly but it's necessary to make the two Fn
impls non-overlapping. Sadly, Fn(T)
and Fn(T, U)
are considered to be overlapping which obviously makes sense looking at their desugared form Fn<(T,)>
and Fn<(T, U)>
. Theoretically a single type can implement both of those trait refs (under #![feature(unboxed_closures)]
).
@@ -484,12 +484,16 @@ impl ProcMacro { | |||
} | |||
} | |||
|
|||
pub const fn custom_derive( | |||
pub const fn custom_derive<Discriminant>( |
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 really hope that this type parameter doesn't “leak” in the sense that Discriminant
doesn't lead to inference failures. See my other comment for why Discriminant
is necessary.
By the way, I can't replace this type parameter with impl-Trait since nested impl-Traits aren't supported at the moment (here: impl ~const ExpandCustomDerive<impl Discriminant>
). It would at least prevent users from being able to explicitly specify it. Of course, this function is not considered part of the public API since this entire module is #[doc(hidden)]
if I remember correctly (hence no stability attributes anywhere in this file).
F: Fn(crate::TokenStream, crate::DeriveExpansionOptions) -> crate::TokenStream + Copy, | ||
{ | ||
fn into_fn(self) -> impl Fn(crate::TokenStream) -> crate::TokenStream + Copy { | ||
move |input| self(input, Default::default()) |
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.
This feels a bit weird and it demonstrates that DeriveExpansionOptions
is truly a “sham”. I fear that the way it's wired right now, this impl isn't extensible. Consider the hypothetical in which we do want to store something meaningful in DeriveExpansionOptions
, we wouldn't be able to actually retrieve it, only receive it with an RPC, e.g. via FreeFunctions
. Maybe this is a non-issue, I'm just rambling at this point ^^'
Nominating for t-libs-api
|
We briefly discussed this in the libs-api meeting. As an unstable feature in proc_macro, this is fine. Feel free to go ahead, as long as there is a tracking issue attached to these unstable APIs and there will be an libs-api FCP before stabilization. |
Both How macros currently take their input - by a token stream, or two token streams if the macro has two inputs like This PR adds a third backdoor input tailored for a very specific use case. I would suggest keeping the current model and using already existing token-based methods for configuring macro behaviors.
Then the macros themselves can process the inputs as they want. If |
On a second thought, there's a third way to pass inputs - the "global data" in Proc macro methods without arguments like If "derive expansion options" are placed into |
What other similar configuration values we may need in the future? Will we need to pass them to derive macros only, or for attribute possibly for attribute or fn-like macros too? |
☔ The latest upstream changes (presumably #120486) made this pull request unmergeable. Please resolve the merge conflicts. |
@fmease any updates on this? thanks |
No updates yet, my plan is to discuss petrochenkov's very good feedback with the rest of project-const-traits as well as with wg-macros. |
I've updated #118304 to mention petrochenkov's valid criticism and I will close this PR to preserve history instead of keeping it open until we work out a redesign of this feature and implementing it here. |
Fixes #118304.
cc @rust-lang/wg-macros
r? compiler and libs-api