# Question: Does rust as a language \*require\* derive macros be kept in order?

**URL:** <https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947>\
**Category:** Uncategorized\
**Created:** [January 11, 2022, 8:08pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947 "2022-01-11T20:08:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 11, 2022, 8:08pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/1 "2022-01-11T20:08:35Z")

</div>

I have a situation (see [Sort derives · Issue #4112 · rust-lang/rustfmt · GitHub](https://github.com/rust-lang/rustfmt/issues/4112#issuecomment-999753023)) where I want `rustfmt` to sort derive macros into an order other than the order that they are specified in within the code. Right now, derive macros are required to be in a particular order because each macro only sees the output of macros that follow it (see [enhancement request - derive sorting · Issue #1867 · rust-lang/rustfmt · GitHub](https://github.com/rust-lang/rustfmt/issues/1867#issuecomment-321412488)). But when I read through the [derive-macro docs](https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros) I couldn't find anywhere where it specifically stated that rust as a language required this to be true. That is, it feels like the requirement that derive macros be in a particular order is an accident and not something that was intentionally planned.

So, with that background out of the way -

- Is there a place in the documentation that explicitly states that it is a requirement of rust as a language that the derive macros be order sensitive? If so, can someone please link to where I can find it?
- If there is no such requirement, and this behavior is an accident of the design of the compiler, can we choose to lift this restriction and not have it be considered a breaking change?1
- What would be involved within the compiler to support this change? That is, how would we specify that derive macros can be put in any order and their output needs to be semantically the same regardless of the order the derive macros were declared?

1I expect that doing this would require lots of testing, crater runs, etc. before everyone is comfortable with supporting the change, especially if it isn't on an edition boundary. That's not what I'm concerned with here, I'm concerned with the language definition itself.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [January 11, 2022, 8:33pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/2 "2022-01-11T20:33:58Z")

</div>

It's not a written requirement somewhere, but it's observable behavior and in principle changing it could break stable code. We've avoided changing it in the past precisely _because_ it could break stable code.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [January 11, 2022, 9:57pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/3 "2022-01-11T21:57:24Z")

</div>

[Is proc-macro-attribute application order defined? · Issue #692 · rust-lang/reference · GitHub](https://github.com/rust-lang/reference/issues/692) seems related

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [January 12, 2022, 8:02am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/4 "2022-01-12T08:02:29Z")

</div>

How can derives observe other derives? They only get the type input afaik, the impls from the other derives are not given to them.

---

<div class="post-metadata">

**Author:** ![Luro02](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/luro02/32/8947_2.png) [@Luro02](https://internals.rust-lang.org/u/Luro02)\
**Post date:** [January 12, 2022, 8:11am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/5 "2022-01-12T08:11:00Z")

</div>

For example a macro could write to a file that the other one reads or maybe make some http requests.

* * *

To be honest this seems like a hack that abuses implementation details of the rust compiler and I don't see any legitimate use for having derive macros that depend on the order they are derived in.

I can not really imagine that anyone would abuse the ordering of derives in real code.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 12, 2022, 3:43pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/6 "2022-01-12T15:43:04Z")

</div>

IIUC, what it is is that

```rust
#[derive(A, B)]
struct S;

```

is equivalent to

```rust
#[derive(A)]
#[derive(B)]
struct S;

```

so A gets (the equivalent of)

```rust
#[derive(B)]
struct S;

```

and B gets

```rust
struct S;

```

due to the normal attribute expansion rules.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [January 12, 2022, 4:05pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/7 "2022-01-12T16:05:52Z")

</div>

For separate `#[derive]` attributes that's true, for composite ones it's not, a little testing gave:

```rust
#[derive(bar::Foo, bar::Bar)]
struct Baz;

#[derive(bar::Foo)]
#[derive(bar::Bar)]
struct Quux;

```

```rust
Foo sees: struct Baz ;
Bar sees: struct Baz ;
Foo sees: #[derive(bar :: Bar)] struct Quux ;
Bar sees: struct Quux ;

```

(EDIT: And, I'm not sure how much of this Rust actually guarantees anyway, it feels like something that should not be guaranteed to keep working even if it does work currently).

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 13, 2022, 12:30am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/8 "2022-01-13T00:30:50Z")

</div>

> [@josh](#):
>
> It's not a written requirement somewhere, but it's observable behavior and in principle changing it could break stable code. We've avoided changing it in the past precisely _because_ it could break stable code.

How hard would it be to tweak the compiler to randomly reorder the derive attributes, and then do a crater run? I'd love to see how much code depends on the current behavior that's out in the wild.

> [@Nemo157](#):
>
> For separate `#[derive]` attributes that's true, for composite ones it's not, a little testing gave:

That does not bring me joy. The following _also_ doesn't bring me joy:

```rust
#[derive(Debug, Debug)]
struct Foo;

```

Compilation output from [the playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c9d96c9bac5aac01efcbfa39604ed0e0):

```bash
  |
1 | #[derive(Debug, Debug)]
  | ----- ^^^^^ conflicting implementation for `Foo`
  | |
  | first implementation here
  |
  = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

```

But at least the compiler lets me know what's going on so I can fix it. **BUT** is there a legitimate reason to try to derive something multiple times in a row? I know that it's possible to write a macro that will generate code that will compile, but it feels like a mistake that should be discouraged.

## My take on this issue

Given that everyone that's answered on this topic so far is (AFAIK) an expert in the language, the fact that there is ambiguity of interpretation makes me... concerned. So, can we resolve the ambiguity in a sane manner?

My vote is for the following rules:

1. Attributes in a composite derive are treated as an unordered set. The compiler is free to reorder the attributes as it sees fit, and is required to turn the attributes into a set. Attributes within a composite set are not permitted to see the output of attributes in the same composite derive. All permutations must result in the same output.
2. Separate derives are effectively within nested scopes. Outer scopes can see the results of execution of inner scopes, but not the other way around.
3. The innermost scope is 'executed' first, replacing the scope with the execution results. This continues until all derives are executed.

So, given that, we can look at the following example:

```rust
#[derive(Clone, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Quux)]
struct Foo;

```

is semantically similar to the following (if braces could be used this way)

```rust
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] // Scope 2
{
    #[derive(Quux)] // Scope 1
    {
        struct Foo;
    }
}

```

- By rule 2, each derive line defines a separate scope.
- By rule 3, scope 1 will be executed first, producing the output of `Quux` macro.
- Once the `Quux` macro is done, its output becomes available to the derive macros in scope 2.
- By rule 1, the duplicate `Clone` derives are reduced to a single instance. Also by rule 1, we can reorder the derive macros in whatever manner is convenient to the compiler. Finally, note that since the macros within a composite derive can't see each other's output, it is entirely possible to spawn futures for each derive macro, and construct their output concurrently. I'm not suggesting that is necessary or a good idea, just that the rules permit it.

The one issue that I see with this is how deriving the same macro multiple times in a row should be handled. E.g.:

```rust
#[derive(Quux)]
#[derive(Quux)]
#[derive(Quux)]
#[derive(Quux)]
struct Foo;

```

My vote is to not try to do anything clever, and just expand the macros using the rules above. Once the macros are all expanded, if there are any conflicts that the compiler will emit an error as normal.

Given all of that, are the rules unambiguous? Are there any real-world issues that anyone can think of by implementing these rules? More importantly does anyone disagree with the rules, or should I start writing an RFC along these lines?

FWIW, since this _is_ a change to the current behavior, even though that behavior appears to be an accidental quirk of the compiler as it currently is, my personal feeling is that settling these rules will require waiting for the 2024 edition before they could be stabilized (assuming that everyone here is interested in doing so, the RFC is accepted, etc., etc., etc.).

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [January 13, 2022, 12:49am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/9 "2022-01-13T00:49:28Z")

</div>

The current behavior is certainly not accidental, and is not a quirk.   
IIRC, some popular crate (`proc-macro-hack`?) generating `macro_rules` items from derives depended on the current behavior.

I see that two issues are mixed in this thread - the expansion order of derives in a single list (which is indeed unspecified), and the order of output items (which is well defined due to `macro_rules`).   
I'll try to look through the thread and answer in more detail today.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 13, 2022, 1:18am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/10 "2022-01-13T01:18:49Z")

</div>

> [@ckaran](#):
>
> 1. The innermost scope is 'executed' first, replacing the scope with the execution results. This continues until all derives are executed.

This is exactly backwards from the way that all other attribute macros work, which is outside-in. This is guaranteed by the language, and in fact a necessary property.

(E.g. `#[cfg(FALSE)] #[derive(Invalid) struct Foo;` compiles fine, because `#[cfg(FALSE)]` is (effectively) an attribute which takes its input and discards it, passing along an empty token stream for further processing.)

The outer attributes _should_ see any inner attributes. This is consistent with the existing and plain outside-in processing order.

What's not immediately clear is the interaction of multiple derives within a single `#[derive()]` container. We've shown that derive macros within a container don't see each other, so if that were the only potential way to observe ordering, it'd be valid to reörder them. However, derive order is observable one other way: `macro_rules!` macros using legacy scoping rules (i.e. all of them, currently). This means that reördering derives is potentially behavior changing. (However, our research here also indicates that rustfmt's default setting of merging derives is _also_ behavior changing.)

It's also worth noting, @ckaran, that `#[derive]` macros are special in that they're "inert;" they do not (and can not) impact the definition of the type they decorate. Instead, they emit some extra items which are inserted after the decorated item.

> [@ckaran](#):
>
> The following _also_ doesn't bring me joy:
> 
> ```rust
> #[derive(Debug, Debug)]
> struct Foo;
> 
> ```
> 
> Compilation output from [the playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c9d96c9bac5aac01efcbfa39604ed0e0):
> 
> ```rust
> |
> 1 | #[derive(Debug, Debug)]
> | ----- ^^^^^ conflicting implementation for `Foo`
> | |
> | first implementation here
> |
> = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
> 
> ```
> 
> But at least the compiler lets me know what's going on so I can fix it. **BUT** is there a legitimate reason to try to derive something multiple times in a row? I know that it's possible to write a macro that will generate code that will compile, but it feels like a mistake that should be discouraged.

This is exactly the output I would expect. Deriving a trait twice is no different to implementing it twice.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [January 13, 2022, 9:25am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/11 "2022-01-13T09:25:20Z")

</div>

> [@petrochenkov](#):
>
> I'll try to look through the thread and answer in more detail today.

@CAD97 basically already did this.

Multiple derive attributes (`#[derive(A)] #[derive(B)] struct S;`) follow the same rules as multiple of _any_ macro attributes.   
The first macro attribute, `derive` or not, is expanded first and takes the rest of the item as input tokens.   
So if rustfmt is merging multiple `#[derive]`s into one, or splitting one derive into multiple, it's indeed behavior changing in theory, but not much in practice.   
(With one exception - it's better to not split `Copy` from other built-in derives, it will lead to pessimization, or even build failures in case of `#[packed]` structs, because `Copy` modifies behavior of other derive macros in the same `#[derive]` container).

Multiple derive macros in a single derive attribute (`#[derive(A, B, C)]`) are expanded in unspecified order (as soon as their resolutions become ready in practice), so the tricks with writing a file in one derive and reading it in another is not guaranteed to work.   
What is guaranteed is that the code generated by `A` will be followed by code generated by `B` and then code generated by `C`.   
If you change that, you'll have issues like [Macro-generated macro can no longer be called · Issue #63651 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/63651) (apparently extracted from a version of futures-rs relevant at that time).   
I'm pretty sure that it's not a common issue in practice, and rustfmt can implement the reordering at least under an option, if some `#[rustfmt::skip]`-like opt-out is provided.

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 13, 2022, 2:12pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/12 "2022-01-13T14:12:38Z")

</div>

> [@CAD97](#):
>
> This is exactly backwards from the way that all other attribute macros work, which is outside-in. This is guaranteed by the language, and in fact a necessary property.

> [@petrochenkov](#):
>
> @CAD97 basically already did this.
> 
> Multiple derive attributes ( `#[derive(A)] #[derive(B)] struct S;` ) follow the same rules as multiple of _any_ macro attributes.

Yup, you're right, my mistake, thank you for catching it! Does this also guarantee that outer scope derive macros can't see the output of inner scope derive macros? Otherwise the whole system needs some kind of 'up-and-down' behavior, and possibly be executed multiple times before the macros are fully expanded (if they are fully expanded at all).

> [@petrochenkov](#):
>
> (With one exception - it's better to not split `Copy` from other built-in derives, it will lead to pessimization, or even build failures in case of `#[packed]` structs, because `Copy` modifies behavior of other derive macros in the same `#[derive]` container).

So, does this imply that `Copy` should be the first attribute in the list? Is that documented anywhere? Are there any other order dependent derive macros that we need to be concerned with in core/std?

> [@petrochenkov](#):
>
> What is guaranteed is that the code generated by `A` will be followed by code generated by `B` and then code generated by `C` .

OK, but this part is invisible to the end user, correct?

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [January 14, 2022, 2:09am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/13 "2022-01-14T02:09:27Z")

</div>

> Does this also guarantee that outer scope derive macros can't see the output of inner scope derive macros?

Yes, the outermost macro attribute sees everything below it as unexpanded tokens, so it cannot see the outputs, that would be eager expansion.

> So, does this imply that `Copy` should be the first attribute in the list?

`Copy` should either inside the same `#[derive(...)]` with the other built-in derive macro (e.g. `Clone`) or in one of the `#[derive(...)]`s immediately above it.

```rust
#[derive(Clone, Copy)] // OK
#[derive(Debug)] // OK too
#[derive(PartialEq)] // Still OK
#[repr(packed)]
struct S { ... }

```

See `fn resolve_derives` in the compiler for the exact implementation.

This property of `Copy` was originally introduced to optimize behavior of other derives like `Clone`, then reused again to keep supporting built-in derives on `#[repr(packed)]` structures, and now we need to continue support it for backward compatibility and to not lose performance.

> Is that documented anywhere?

Maybe, but likely not.

> OK, but this part is invisible to the end user, correct?

It's visible if the derive macro generates `macro_rules` items, like in the issue linked above.

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 14, 2022, 3:31pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/14 "2022-01-14T15:31:10Z")

</div>

> [@petrochenkov](#):
>
> `Copy` should either inside the same `#[derive(...)]` with the other built-in derive macro (e.g. `Clone` ) or in one of the `#[derive(...)]` s immediately above it.
> 
> ```rust
> #[derive(Clone, Copy)] // OK
> #[derive(Debug)] // OK too
> #[derive(PartialEq)] // Still OK
> #[repr(packed)]
> struct S { ... }
> 
> ```

OK, but what about:

```rust
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
#[repr(packed)]
struct S { ... }

```

versus

```rust
#[derive(Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
#[repr(packed)]
struct S { ... }

```

Are there performance implications here?

> [@petrochenkov](#):
>
> See `fn resolve_derives` in the compiler for the exact implementation.

Thank you, I will.

> [@petrochenkov](#):
>
> > Is that documented anywhere?
> 
> Maybe, but likely not.
> 
> > OK, but this part is invisible to the end user, correct?
> 
> It's visible if the derive macro generates `macro_rules` items, like in the issue linked above.

This is probably a problem. I use [LaTeX](https://tug.org/) which has a real problem with packages being required to be imported in a particular order in order to work correctly. The issue is that the documentation for different packages can be unclear, and may lead to cycles in the dependency graph (if you can call it that). Learning this sharp edge of LaTeX takes a fair amount of effort, and is in general a waste of time and brain cells. The fact that rust's macro system may be heading down that path is **extremely** concerning to me, and is one of the reasons why I wanted to change the behavior using the rules that I originally described in my [earlier post](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/8) (modulo the fixes that @CAD97 and @petrochenkov described). I acknowledge that trying to do so would be a breaking change, and therefore it could only be done on an edition boundary, if it is done at all. However, I think it's something that should be considered before we end up having weird dependencies in the macro system that aren't obvious to users. I mean, the fact that the ordering of `Copy` could make a difference in packed structures is something I would _never_ have thought about.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [January 14, 2022, 4:08pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/15 "2022-01-14T16:08:02Z")

</div>

I believe the `macro_rules!` cases from the issue are all from `#[derive]` attributes generated from other related macros, so those macros are in charge of ordering the derives in the correct order not the user. For general purpose user-written derives there should never be any ordering requirement between them.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [January 16, 2022, 2:17am UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/16 "2022-01-16T02:17:06Z")

</div>

> [@ckaran](#):
>
> OK, but what about:
> 
> ```rust
> #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
> #[repr(packed)]
> struct S { ... }
> 
> ```
> 
> versus
> 
> ```rust
> #[derive(Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
> #[repr(packed)]
> struct S { ... }
> 
> ```
> 
> Are there performance implications here?

No, position of `Copy` inside a single list is not important.

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 18, 2022, 2:59pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/17 "2022-01-18T14:59:24Z")

</div>

OK, to summarize what I understand at the moment about how derive macros currently work:

1. Macros are order-dependent. Macros that are earlier in a list are aware of macros that are later in a list.
2. Macros are expanded from outer scopes to inner scopes. Thus, an outer-scope derive macro will only see a token stream of the inner contents, not the results of expansion of the inner contents.
3. `#[derive(A, B)]` may yield different results than `#[derive(A)] #[derive(B)]`.
4. `Copy` is special-cased and needs to be in the same list as the rest of the built-in derive macros to work correctly, but it doesn't matter where it is in the list provided that it is in the outer-most scope.

Have I missed anything here? Are there any other special cased derive macros?

Looking the list over, the first two points seem to have relatively low cognitive complexity. The last two points feel like they can _easily_ lead to overwhelming cognitive complexity. Is it possible to fix those points?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [January 18, 2022, 9:56pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/18 "2022-01-18T21:56:09Z")

</div>

> [@ckaran](#):
>
> Is it possible to fix

> [@ckaran](#):
>
> `#[derive(A, B)]` may yield different results than `#[derive(A)] #[derive(B)]` .

Given the proc macro APIs are stable, not for the currently implemented proc macro APIs. But then again, proc macros can already make arbitrary syntactical differences semantic, so I don't see that as _that_ much different from proc macros just being poorly written and reliant on syntax specifics they shouldn't be.

Personally, I'd like to have proc macros receive the derive attribute that their derive is in (like how attribute proc macros receive their attribute), but starting with their derive name, keeping the look-forward-only property. (Yes, this would make it more ordering sensitive.) A really nice (imho) effect of this is that a derive macro now can know the path to the proc macro (thus likely, via reëxports, the path to the trait it's implementing) without having to hardcode crate names. (This unfortunately doesn't help getting to helper modules, unless we add the (very interesting) feature of `$ty::super` paths.)

(Please give me some way to know the path to the runtime crate from my attribute proc macros, so they don't break when the runtime crate is renamed 🥺 (NB: workaround for function-like macros: take a `#![crate($path)]` at the beginning, then expose it in the impl crate via a wrapper `macro_rules!` which adds `#![crate($crate)]`.))

> [@ckaran](#):
>
> Is it possible to fix

> [@ckaran](#):
>
> `Copy` is special-cased and needs to be in the same list as the rest of the built-in derive macros to work correctly,

With a bit more special casing, yes: in addition to `Copy` looking for other built in derives and changing their semantics, have the other built in derives look for `Copy` and update their semantics. Then, using the extra built-in power of looking within the same derive container, it doesn't matter which is first; the one that comes first will see the latter one, and adjust semantics as required.

Doing so in the type system rather than compiler built-in special powers would require a very big new metaprogramming feature to have `#[cfg(T: Copy)]` and I think such metaprogramming would _easily_ become unsolvable at the item level. (And as block statements can contain items...)

---

<div class="post-metadata">

**Author:** ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)\
**Post date:** [January 18, 2022, 10:08pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/19 "2022-01-18T22:08:50Z")

</div>

> [@CAD97](#):
>
> Given the proc macro APIs are stable, not for the currently implemented proc macro APIs.

2024 Edition! (I hope...)

Is there a current collection point for enumerating all of the 'broken' things with the various bits of the current macro system? Or a working group? I'd like to see this part of the language really cleaned up and ironed out before we end up with C++'s template metaprogramming craziness (anyone that has dealt with an error because of Boost knows what I'm talking about, you make one tiny mistake, and have 10,000+ errors in the compile, fix that error and they **all** go away).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 18, 2022, 10:09pm UTC](https://internals.rust-lang.org/t/question-does-rust-as-a-language-require-derive-macros-be-kept-in-order/15947/20 "2022-04-18T22:09:27Z")

</div>

Closed on April 18, 2022, 10:09pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
