-
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
Allow path as value in name-value attribute #76734
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
31f13e4
to
a7149c5
Compare
cc https://internals.rust-lang.org/t/macro-expansion-points-in-attributes/11455 with which this extension overlaps. |
Expressions seems fine and is still compatible with potentially allowing attributes to be written combined like Do you think we shouldn't take this PR without the bigger change in #55414 (comment)? I think they're largely orthogonal because values which are just mod-style paths would not participate in any kind of expansion described there. |
This comment has been minimized.
This comment has been minimized.
Yes. In the meantime |
I started working on this in #77271. |
Reopening with rebase on #77271. |
Accept arbitrary expressions in key-value attributes at parse time Continuation of rust-lang#77271. We now support arbitrary expressions in values of key-value attributes at parse time. ``` #[my_attr = EXPR] ``` Previously only unsuffixed literals and interpolated expressions (`$expr`) were accepted. There are two immediate motivational cases for this: - External doc strings (`#[doc = include_str!("my_doc.md")]`, eliminating the need in rust-lang#44732) and expanding macros in this position in general. Currently such macro expansions are supported in this position in interpolated `$expr`s (the `#[doc = $doc]` idiom). - Paths (`#[namespace = foo::bar] extern "C++" { ... }`) like proposed in rust-lang#76734. If the attribute in question survives expansion, then the value is still restricted to unsuffixed literals by a semantic check. This restriction doesn't prevent the use cases listed above, so this PR keeps it in place for now. Closes rust-lang#52607. Previous attempt - rust-lang#67121. Some more detailed write up on internals - https://internals.rust-lang.org/t/macro-expansion-points-in-attributes/11455. Tracking issue - rust-lang#78835.
☔ The latest upstream changes (presumably #78837) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
#78837 has landed, unblocking. |
Closing due to inactivity. |
This commit allows proc macros to take inert attributes that look like
#[path = ident]
or#[path = p::p::path]
. Previously the rhs was only permitted to be an unsuffixed numeric literal or the identifier special casestrue
andfalse
. However, parenthesized#[path(key = ident)]
and#[path(key = p::p::path)]
were already allowed; see https://docs.rs/cxx/0.4.4/cxx/attr.bridge.html for one attribute currently using that style.My use case for this is related to C++ namespace support in https://github.com/dtolnay/cxx.
Chesterton's fence: why was the rhs syntax restricted originally? I believe this was to leave open the possibility of
#[m1 = "...", m2 = "..."]
syntax which would be equivalent to#[m1 = "..."] #[m2 = "..."]
but more compact (this might still happen). Allowing an "arbitrary tokenstream" on the rhs would rule that out. In this PR I've still kept the rhs syntax quite restricted, adding only mod-style paths and remaining compatible with compact syntax being introduced later.