# Macro expansion optionally in natural order

**URL:** <https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261>\
**Category:** language design\
**Created:** [February 4, 2024, 10:41pm UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261 "2024-02-04T22:41:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [February 4, 2024, 10:41pm UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/1 "2024-02-04T22:41:12Z")

</div>

Nested macros are known as being evaluated “the wrong way round.” This makes it impossible to have a helper macro, the output of which you can recursively match against. To remedy this, I would suggest an additional backwards compatible syntax: If the outermost macro is called with a double exclam, then any nested macro calls that also have a double exclam would be expanded first. So

```rust
macro_rules! sum { () => { 1 + 2 } }

// unchanged from today:
assert_ne!("1 + 2 = 3", stringify!(sum!() = 3));

// new, 1st might give a warning as double exclam serves no purpose:
assert_ne!!("1 + 2 = 3", stringify!(sum!() = 3));
assert_ne!!("1 + 2 = 3", stringify!!(sum!() = 3));

// new, sometimes useful:
assert_eq!!("1 + 2 = 3", stringify!(sum!!() = 3));
assert_eq!!("1 + 2 = 3", stringify!!(sum!!() = 3));

```

Without requiring a double exclam as an activator on the outer call, it might break some macro that matches `$id:ident!!(…)`. For such a hypothetical macro, this new syntax might not be useful. But that’s an edge case.

One place where this would be beneficial is for embedded languages, which you can’t easily keep in a separate file. For example, my upcoming RustDOT crate, which will be able to parse all Graphviz DOT (as long as it is lexically valid Rust, which it almost always is.) Instead of the first, you could directly do the second:

```rust
dot! {
    digraph "Hello World" {
        space [label = " "]
        World [fontcolor = blue]
        Hello -> "," -> space -> World -> "!"
    }
}
dot!!(include!!("hello-world.dot"))

```

NB: This is not the same as the [similar suggestion for proc macros](https://internals.rust-lang.org/t/pre-rfc-macro-expansion-of-input-for-proc-macros-derives-primarily/7210). That one, besides being for a different kind of macro, didn’t seem to offer finely granular choice, which inner macros to expand early.

---

<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:** [February 5, 2024, 8:58am UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/2 "2024-02-05T08:58:56Z")

</div>

Previous work: [RFC: Eager Macro Expansion by pierzchalski · Pull Request #2320 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/pull/2320) which has been postponed.

---

<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:** [February 6, 2024, 12:47am UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/3 "2024-02-06T00:47:35Z")

</div>

If we do add a mechanism for eager macro expansion, I personally expect we'll want to do something less subtle than one versus two exclamation marks.

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [February 6, 2024, 3:46am UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/4 "2024-02-06T03:46:44Z")

</div>

> [@josh](#):
>
> I personally expect we'll want to do something less subtle than one versus two exclamation marks.

Isn't there already a builtin macro which is eager in its arguments? IIRC, `concat!(include_str!("filename.txt"))` doesn't work. How about a `now!` or `eager!` macro that does this? Though maybe that is an instance of this problem as well (it's late, but I don't want to let this thought slip away overnight)…`concat!` not "waiting" for inner expansion to act. And I suppose any `now!`-like macro runs into the case of something trying to match that literally…

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [February 6, 2024, 5:25am UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/5 "2024-02-06T05:25:51Z")

</div>

The general thinking nowadays is to use [macro metavariable expressions](https://github.com/rust-lang/rust/issues/83527)-like syntax.

---

<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:** [February 6, 2024, 12:51pm UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/6 "2024-02-06T12:51:29Z")

</div>

We have the `proc-macro-expand` feature to allow proc-macros to do the same thing as builtins.

> <https://github.com/rust-lang/rust/issues/90765>
>
> Feature gate: \`#!\[feature(proc\_macro\_expand)\]\`
> 
> This is a tracking issue for t…he \`TokenStream::expand\_expr\` function for use in proc-macros. This function allows eagerly expanding TokenStreams containing expressions from within a proc-macro, such that they can be modified, giving proc-macros similar functionality to built-in macros like \`format\_args!\`, \`concat!\` and \`include!\`.
> 
> \### Public API
> 
> \`\`\`rust
> impl TokenStream {
> pub fn expand\_expr(&self) -\> Result\<TokenStream, ExpandError\>;
> }
> 
> \#\[non\_exhaustive\]
> pub struct ExpandError;
> 
> impl Debug for ExpandError { ... }
> impl Display for ExpandError { ... }
> impl Error for ExpandError {}
> impl !Send for ExpandError {}
> impl !Sync for ExpandError {}
> \`\`\`
> 
> \### Steps / History
> 
> \- \[x\] Implementation for literal expressions: #87264
> \- \[\] Generalization for other expression types: TODO
> \- \[\] (optional) Generalization to other AST types: TODO
> \- \[\] Final comment period (FCP)
> \- \[\] Stabilization PR
> 
> \### Unresolved Questions
> 
> \- What types of expressions beyond literals should be supported?
> \- Should expanding non-expression macros (e.g. types, items, etc.) be supported?
> \- Should certain macros (e.g. those with \`#\[allow\_internal\_unstable\]\`) be expandable or left unexpanded?
> \- How can we evolve which macros are expanded or not within the rustc stability model? Can downstream crates opt-in to additional macro expansion on nightly channels? Should an additional argument be provided to customize what should be expanded?
> \- Should compilation be allowed to proceed after an expand method errors?
> \- Are there concerns about stabilizing the span information generated by internal macros like \`include!\`?
> \- How can we properly preserve span information when expanding expression macros?

That's coming from the other direction though, the outer macro knows that this specific part of its input should be allowed to expand, rather than the caller of the macro forcing it to pre-expand before the outer macro runs.

---

<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:** [May 6, 2024, 12:51pm UTC](https://internals.rust-lang.org/t/macro-expansion-optionally-in-natural-order/20261/7 "2024-05-06T12:51:41Z")

</div>

Closed on May 6, 2024, 12:51pm UTC

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