7

Github Stabilize extended_key_value_attributes by jyn514 · Pull Request #83366 ·...

 3 years ago
source link: https://github.com/rust-lang/rust/pull/83366
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Member

jyn514 commented on Mar 22

edited

Closes #44732. Closes #78835. Closes #82768 (by making it irrelevant).

Stabilization report

Summary

This stabilizes using macro expansion in key-value attributes, like so:

#[doc = include_str!("my_doc.md")]
struct S;

#[path = concat!(env!("OUT_DIR"), "/generated.rs")]
mod m;

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:

Implementation history

  • Initial proposal: #55414 (comment)
  • Experiment to see how much code it would break: #67121
  • Preliminary work to restrict expansion that would conflict with this
    feature: #77271
  • Initial implementation: #78837
  • Fix for an ICE: #80563

Unresolved 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:

macro_rules! forward_inner_docs {
    ($e:expr => $i:item) => {
        #[doc = $e]
        $i
    };
}

forward_inner_docs!(include_str!("lib.rs") => struct S {});

This also works for other attributes (like #[path = concat!(...)]).
The other is to use doc(include):

 #![feature(external_doc)]
 #[doc(include = "lib.rs")]
 struct S {}

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_docs
workaround will still compile without warnings, but I expect it to be
used less once it's no longer necessary.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK