Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize $$ in Rust 1.63.0 #95860

Merged
merged 1 commit into from
Jun 9, 2022
Merged

Stabilize $$ in Rust 1.63.0 #95860

merged 1 commit into from
Jun 9, 2022

Conversation

c410-f3r
Copy link
Contributor

@c410-f3r c410-f3r commented Apr 9, 2022

Stabilization proposal

This PR proposes the stabilization of a subset of #![feature(macro_metavar_expr)] or more specifically, the stabilization of dollar-dollar ($$).

Tracking issue: #83527
Version: 1.63 (2022-06-28 => beta, 2022-08-11 => stable).

What is stabilized

macro_rules! foo {
    () => {
        macro_rules! bar {
            ( $$( $$any:tt )* ) => { $$( $$any )* };
        }
    };
}

fn main() {
    foo!();
}

Motivation

For more examples, see the RFC.

Users must currently resort to a tricky and not so well-known hack to declare nested macros with repetitions.

macro_rules! foo {
    ($dollar:tt) => {
        macro_rules! bar {
            ( $dollar ( $any:tt )* ) => { $dollar ( $any )* };
        }
    };
}
fn main() {
    foo!($);
}

As seen above, such hack is fragile and makes work with declarative macros much more unpleasant. Dollar-dollar ($$), on the other hand, makes nested macros more intuitive.

What isn't stabilized

count, ignore, index and length are not being stabilized due to the lack of consensus.

History

Non-stabilized expressions

#83527 lists several concerns about some characteristics of count, index and length that effectively make their stabilization unfeasible. $$ and ignore, however, are not part of any discussion and thus are suitable for stabilization.

It is not in the scope of this PR to detail each concern or suggest any possible converging solution. Such thing should be restrained in this tracking issue.

Tests

This list is a subset of https://github.com/rust-lang/rust/tree/master/src/test/ui/macros/rfc-3086-metavar-expr

Possible future work

Once consensus is achieved, other nightly expressions can be stabilized.

Thanks @markbt for creating the RFC and thanks to @petrochenkov and @mark-i-m for reviewing the implementations.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 9, 2022
@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2022
@dtolnay dtolnay added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-lang Relevant to the language team, which will review and decide on the PR/issue. F-macro-metavar-expr feature(macro_metavar_expr) and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 9, 2022
@joshtriplett
Copy link
Member

Looks reasonable to me!

The stabilization report seems to cover everything.

Shall we stabilize $$ and $ignore?

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Apr 9, 2022

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Apr 9, 2022
@michaelwoerister
Copy link
Member

r? @joshtriplett, unless compiler team input is required here (in which case someone with more knowledge about macros would be a better fit than me).

@m-ou-se
Copy link
Member

m-ou-se commented Apr 12, 2022

Nested macro declarations ($$)

Does this grow linearly or exponentially at deeper levels? In other words, are the next levels $$$ and $$$$, or $$$$ and $$$$$$$$?

@nikomatsakis
Copy link
Contributor

@rfcbot concern doc-pr

@nikomatsakis
Copy link
Contributor

Can we get a PR against the reference, @c410-f3r ?

Thanks for pursuing this!

@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@joshtriplett
Copy link
Member

@rfcbot concern multi-level-nesting-of-dollar

On behalf of @m-ou-se. We should document this. Consensus in the @rust-lang/lang meeting just now was that escaping each $ as $$ (so $$$$ two levels deep) would be simpler.

@c410-f3r
Copy link
Contributor Author

Nested macro declarations ($$)

Does this grow linearly or exponentially at deeper levels? In other words, are the next levels $$$ and $$$$, or $$$$ and $$$$$$$$?

$$ expands to $ regardless of the declared level as specified in the RFC. So "deeper" levels have to be $$$$, $$$$$$, $$$$$$$$ and so on.

Here is a test demonstrating such scenario -> https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/dollar-dollar-has-correct-behavior.rs

@c410-f3r
Copy link
Contributor Author

@rfcbot concern multi-level-nesting-of-dollar

On behalf of @m-ou-se. We should document this. Consensus in the @rust-lang/lang meeting just now was that escaping each $ as $$ (so $$$$ two levels deep) would be simpler.

I am assuming that this documentation should be restrained in https://github.com/rust-lang/reference. If not, then feel free to indicate other places

@markbt
Copy link

markbt commented Apr 12, 2022

Nested macro declarations ($$)

Does this grow linearly or exponentially at deeper levels? In other words, are the next levels $$$ and $$$$, or $$$$ and $$$$$$$$?

$$ expands to $ regardless of the declared level as specified in the RFC. So "deeper" levels have to be $$$$, $$$$$$, $$$$$$$$ and so on.

Here is a test demonstrating such scenario -> https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/dollar-dollar-has-correct-behavior.rs

Note that this is also necessary to be fully featured, so that you can specify names of metavariables using other metavariables at each nesting level:

$foo          => bar      => bar    // Evaluate foo at level 1
$$foo         => $foo     => bar    // Evaluate foo at level 2
$$$foo        => $bar     => baz    // Evaluate foo at level 1, and use that as a name at level 2
$$$$foo       => $$foo    => $foo   // Evaluate foo at level 3
$$$$$foo      => $$bar    => $bar   // Evaluate foo at level 1, and use that as a name at level 3
$$$$$$foo     => $$$foo   => $bar   // Evaluate foo at level 2, and use that as a name at level 3
$$$$$$$foo    => $$$bar   => $baz   // Evaluate foo at level 1, use that at level 2, and then use *that* at level 3

@c410-f3r
Copy link
Contributor Author

A PR addressing the concerns has been submitted > rust-lang/reference#1192

@c410-f3r c410-f3r changed the title Stabilize $$ and ${ignore} in Rust 1.62.0 Stabilize $$ in Rust 1.63.0 Jun 8, 2022
@c410-f3r
Copy link
Contributor Author

c410-f3r commented Jun 8, 2022

Done. Thank you for allowing the stabilization of $$.

@joshtriplett
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 8, 2022

📌 Commit 9edaa76 has been approved by joshtriplett

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 8, 2022
@joshtriplett joshtriplett added the relnotes Marks issues that should be documented in the release notes of the next release. label Jun 8, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 9, 2022
…lett

Stabilize `$$` in Rust 1.63.0

# Stabilization proposal

This PR proposes the stabilization of a subset of `#![feature(macro_metavar_expr)]` or more specifically, the stabilization of dollar-dollar (`$$`).

Tracking issue: rust-lang#83527
Version: 1.63 (2022-06-28 => beta, 2022-08-11 => stable).

## What is stabilized

```rust
macro_rules! foo {
    () => {
        macro_rules! bar {
            ( $$( $$any:tt )* ) => { $$( $$any )* };
        }
    };
}

fn main() {
    foo!();
}
```

## Motivation

For more examples, see the [RFC](https://github.com/markbt/rfcs/blob/macro_metavar_expr/text/0000-macro-metavar-expr.md).

Users must currently resort to a tricky and not so well-known hack to declare nested macros with repetitions.

```rust
macro_rules! foo {
    ($dollar:tt) => {
        macro_rules! bar {
            ( $dollar ( $any:tt )* ) => { $dollar ( $any )* };
        }
    };
}
fn main() {
    foo!($);
}
```

As seen above, such hack is fragile and makes work with declarative macros much more unpleasant. Dollar-dollar (`$$`), on the other hand, makes nested macros more intuitive.

## What isn't stabilized

`count`, `ignore`, `index` and `length` are not being stabilized due to the lack of consensus.

## History

* 2021-02-22, [RFC: Declarative macro metavariable expressions](rust-lang/rfcs#3086)
* 2021-03-26, [Tracking Issue for RFC 3086: macro metavariable expressions](rust-lang#83527)
* 2022-02-01, [Implement macro meta-variable expressions](rust-lang#93545)
* 2022-02-25, [[1/2] Implement macro meta-variable expressions](rust-lang#94368)
* 2022-03-11, [[2/2] Implement macro meta-variable expressions](rust-lang#94833)
* 2022-03-12, [Fix remaining meta-variable expression TODOs](rust-lang#94884)
* 2019-03-21, [[macro-metavar-expr] Fix generated tokens hygiene](rust-lang#95188)
* 2022-04-07, [Kickstart the inner usage of macro_metavar_expr](rust-lang#95761)
* 2022-04-07, [[macro_metavar_expr] Add tests to ensure the feature requirement](rust-lang#95764)

## Non-stabilized expressions

rust-lang#83527 lists several concerns about some characteristics of `count`, `index` and `length` that effectively make their stabilization unfeasible. `$$` and `ignore`, however, are not part of any discussion and thus are suitable for stabilization.

It is not in the scope of this PR to detail each concern or suggest any possible converging solution. Such thing should be restrained in this tracking issue.

## Tests

This list is a subset of https://github.com/rust-lang/rust/tree/master/src/test/ui/macros/rfc-3086-metavar-expr

* [Ensures that nested macros have correct behavior](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/dollar-dollar-has-correct-behavior.rs)

* [Compares produced tokens to assert expected outputs](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/feature-gate-macro_metavar_expr.rs)

* [Checks the declarations of the feature](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/required-feature.rs)

* [Verifies all possible errors that can occur due to incorrect user input](https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/syntax-errors.rs)

## Possible future work

Once consensus is achieved, other nightly expressions can be stabilized.

Thanks `@markbt` for creating the RFC and thanks to `@petrochenkov` and `@mark-i-m` for reviewing the implementations.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 9, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#95632 (impl Read and Write for VecDeque<u8>)
 - rust-lang#95860 (Stabilize `$$` in Rust 1.63.0)
 - rust-lang#97838 (hexagon: adapt test for upstream output changes)
 - rust-lang#97843 (Relax mipsel-sony-psp's linker script)
 - rust-lang#97874 (rewrite combine doc comment)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit afa2edb into rust-lang:master Jun 9, 2022
@rustbot rustbot added this to the 1.63.0 milestone Jun 9, 2022
@rfcbot rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 9, 2022
@CAD97
Copy link
Contributor

CAD97 commented Jun 9, 2022

Further discussion around metafunctions can take place on Zulip: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/macro.20metafunctions.20vs.20eager.20expansion

@blueglyph
Copy link

For future references, one of the links in the first post is broken:

Small typo in the URL (missing 's' on 'features'):

https://github.com/rust-lang/rust/blob/master/src/test/ui/macros/rfc-3086-metavar-expr/required-features.rs

=>

@CAD97
Copy link
Contributor

CAD97 commented Jul 8, 2022

What is the expected behavior of $$crate? Surely I would suppose that it should behave differently from $crate... #99035

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 19, 2022
…=Mark-Simulacrum

Revert "Stabilize $$ in Rust 1.63.0"

This mechanically reverts commit 9edaa76, the one commit from rust-lang#95860.

rust-lang#99035; the behavior of `$$crate` is potentially unexpected and not ready to be stabilized. rust-lang#99193 attempts to forbid `$$crate` without also destabilizing `$$` more generally.

`@rustbot` modify labels +T-compiler +T-lang +P-medium +beta-nominated +relnotes

(applying the labels I think are accurate from the issue and alternative partial revert)

cc `@Mark-Simulacrum`
ehuss pushed a commit to ehuss/rust that referenced this pull request Jul 22, 2022
…=Mark-Simulacrum

Revert "Stabilize $$ in Rust 1.63.0"

This mechanically reverts commit 9edaa76, the one commit from rust-lang#95860.

rust-lang#99035; the behavior of `$$crate` is potentially unexpected and not ready to be stabilized. rust-lang#99193 attempts to forbid `$$crate` without also destabilizing `$$` more generally.

`@rustbot` modify labels +T-compiler +T-lang +P-medium +beta-nominated +relnotes

(applying the labels I think are accurate from the issue and alternative partial revert)

cc `@Mark-Simulacrum`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) F-macro-metavar-expr feature(macro_metavar_expr) finished-final-comment-period The final comment period is finished for this PR / Issue. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.