-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Stabilize extended_key_value_attributes #83366
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
Conversation
|
(rust-highfive has picked a reviewer for you, use r? to override) |
|
r? @petrochenkov (forgot to put it in the PR description, sorry). I added T-lang and T-compiler since I think they may need to both sign off on the FCP? Feel free to remove one or the other if necessary. |
a18c9bc to
dc408e2
Compare
dc408e2 to
d9e7d25
Compare
|
Stabilizing this would not make #82768 irrelevant because of people trying to provide backward compatibility. Clap's MSRV is 1.46.0. We would like to keep it the same and use the |
@pksunkara see the workaround in #82768 (comment). |
This comment has been minimized.
This comment has been minimized.
This is not eager expansion, this is a limited version of the same |
|
There are two questions to answer/acknowledge before stabilizing this:
|
Do you mean |
|
@camelid |
|
I came here from the release notes for 1.54. Does this feature work for derive macros? It doesn't seem like it does. I'm trying something that looks like this: #[some_derive_macro(some_attribute = myfoo!())]
struct Whatever {
}Which does not work with What I actually want is this: #52393 And would look like this: const MY_FOO: &str = "bloop";
#[some_derive_macro(some_attribute = MY_FOO)]
struct Whatever {
}I thought I might be able to do a workaround using the new 1.54 feature: macro_rules identity! { ($x:expr) => { $x }; }
const MY_FOO: &str = "bloop";
#[some_derive_macro(some_attribute = identity!(MY_FOO))]
struct Whatever {
}Alas, I was naive. Is there any chance that derive macros can get the same love as proc macros so that we can use macros (well, actually constants) as values in derive macro attribute values? |
|
This is not specifically about proc-macros: #78835 (comment) |
So if I read that correctly, this is supported: #[some_derive_macro = something!()]
struct X {}Bot not this: #[some_derive_macro(attribute1 = something!())]
struct X {}Is that what #78835 (comment) is saying? Thanks. |
|
@webern Yes. |
|
@jyn514 but that doesn't: #[path = concat!(env!("OUT_DIR"), "/lib.rs")] |
Closes #44732. Closes #78835. Closes #82768 (by making it irrelevant).
Stabilization report
Summary
This stabilizes using macro expansion in key-value attributes, like so:
Eager macro expansion only happens in a limited number of built-in attributes, with
#[doc]being the primary example that will benefit from this. Notably, the attributespath,crate_type, andrecursion_limitdo not support this because they need their values before or during expansion. It is also not supported with proc-macros.See Petrochenkov's excellent blog post on internals
for alternatives that were considered and rejected ("why accept no more and no less?")
This has been available on nightly since 1.50 with no major issues.
Notes
Accepted syntax
The parser accepts arbitrary Rust expressions in this position, but any expression other than a macro invocation will ultimately lead to an error because it is not expected by the built-in expression forms (e.g.,
#[doc]). Note that decorators and the like may be able to observe other expression forms.Expansion ordering
Expansion of macro expressions in "inert" attributes occurs after decorators have executed, analogously to macro expressions appearing in the function body or other parts of decorator input.
There is currently no way for decorators to accept macros in key-value position if macro expansion must be performed before the decorator executes (if the macro can simply be copied into the output for later expansion, that can work).
Test cases
The feature has also been dogfooded extensively in the compiler and
standard library:
forward_inner_docshack #83230Implementation history
feature: Expand
NtExprtokens only in key-value attributes #77271Unresolved Questions
#83366 (comment) listed some concerns, but they have been resolved as of this final report.Additional Information
There are two workarounds that have a similar effect for
#[doc]attributes on nightly. One is to emulate this behavior by using a limited version of this feature that was stabilized for historical reasons:
The other is to use
doc(include):The first works, but is non-trivial for people to discover, and
difficult to read and maintain. The second is a strange special-case for
a particular use of the macro. This generalizes it to work for any use
case, not just including files.
I plan to remove
doc(include)when this is stabilized(#82539). The
forward_inner_docsworkaround will still compile without warnings, but I expect it to be
used less once it's no longer necessary.