Uh oh!
There was an error while loading. Please reload this page.
Allow path as value in name-value attribute - #76734
Conversation
rust-highfive
commented
Sep 15, 2020
(rust_highfive has picked a reviewer for you, use r? to override) |
cc https://internals.rust-lang.org/t/macro-expansion-points-in-attributes/11455 with which this extension overlaps. |
dtolnay
commented
Sep 15, 2020
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.
petrochenkov
commented
Sep 26, 2020
Yes. In the meantime |
petrochenkov
commented
Sep 27, 2020
I started working on this in #77271. |
dtolnay
commented
Nov 3, 2020
Reopening with rebase on #77271. |
petrochenkov
commented
Nov 7, 2020
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. Closesrust-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.
bors
commented
Dec 10, 2020
☔ 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: |
petrochenkov
commented
Dec 10, 2020
#78837 has landed, unblocking. |
petrochenkov
commented
Feb 23, 2021
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 casestrueandfalse. 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.