Is proc-macro-attribute application order defined?

Open
#692 2 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust

Research direction

Start by reading the related discussions in #565 and rust-lang/rust#63336, then inspect the Rust Reference sections covering attributes and procedural macros. The issue is done when the expansion order and attribute-resolution behavior are settled and documented; the payload names no specific file or test.

Written by the indexing model from the issue text.

Description

A-attributes A-proc-macro

In code, if I write

#[strip_attrs_on_item]
#[this_attr_always_panics]
struct Foo;

Is it guaranteed that strip_attrs_on_item will receive the token stream # [ this_attr_always_panics ] struct Foo ; and that if it does what it says on the tin and emits struct Foo ;, this_attr_always_panics is not called?

As an addendum, is this_attr_always_panics even resolved, if it's transformed before application?

#565 is probably related. https://github.com/rust-lang/rust/issues/63336 is this issue.

derive macros have a special path to say "I handle this attribute" so derive(serde::Serialize) implies that you can use #[serde(..)] afterwards, and the derive gives it meaning. If expansion order isn't guaranteed outer before inner, then replacing the derive with an attribute would remove this ability.

This is not #578, which is about attributes within the same crate but on different items.

Dominant language
Rust
Stars
1.6k
Forks
607
Avg merge
1d 1h
Merged PRs (30d)
12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rust-lang/reference

All issues in rust-lang/reference

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.