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

unstable book; document macro_metavar_expr_concat #139416

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mejrs
Copy link
Contributor

@mejrs mejrs commented Apr 5, 2025

Rendered:

image
image
image
image

cc @c410-f3r

@rustbot
Copy link
Collaborator

rustbot commented Apr 5, 2025

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 5, 2025
@@ -6,6 +6,8 @@ The tracking issue for this feature is: [#29599]

------------------------

> This feature is expected to be removed in favor of [`macro_metavar_expr_concat`](./macro-metavar-expr-concat.md).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


### Overview

`macro_rules!` macros cannot create new identifiers and use them in ident positions.
Copy link
Contributor Author

@mejrs mejrs Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the "in ident position" phrasing. Is there a better (or maybe stronger/weaker) way of describing where macros canot be used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to say that we can't create identifiers at all, since this isn't possible on stable. This section could be something like:

In stable Rust, there is no way to create new identifiers by joining identifiers to literals or other identifiers without using procedural macros such as paste. #![feature(macro_metavar_expr_concat)] introduces a way to do this, using the concat metavariable expression:

// example

The above expands to:

// expansion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used this wording at the top but kept this section as is, I prefer a "show a locked door before you show a key" approach.

@rust-log-analyzer

This comment has been minimized.

@ehuss
Copy link
Contributor

ehuss commented Apr 7, 2025

r? @tgross35
maybe?
or
@joshtriplett

@rustbot rustbot assigned tgross35 and unassigned ehuss Apr 7, 2025
@tgross35
Copy link
Contributor

tgross35 commented Apr 8, 2025

Happy to review this one. One general note: concat_idents is in the process of being deprecated/removed within a few months, so I don’t think it makes sense to introduce macro_metavar_expr_concat with a comparison to concat_idents. If anything just a short note that it supersedes that feature.


### Overview

`macro_rules!` macros cannot create new identifiers and use them in ident positions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to say that we can't create identifiers at all, since this isn't possible on stable. This section could be something like:

In stable Rust, there is no way to create new identifiers by joining identifiers to literals or other identifiers without using procedural macros such as paste. #![feature(macro_metavar_expr_concat)] introduces a way to do this, using the concat metavariable expression:

// example

The above expands to:

// expansion

### Syntax

This feature builds upon the metavariable expression syntax `${ .. }` as specified in [RFC 3086] ([`macro_metavar_expr`]).
`concat` is available like `${ concat(items) }`, where `items` is a comma separated sequence of idents and/or string literals.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/string literals/literals, I think we want to support numeric literals as well (or maybe already do?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numerics are not supported: https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=db1dbedf669a4061ff6ebceccf9cd7ad

#![feature(macro_metavar_expr_concat)]
macro_rules! cursed_add {
  ($a:literal, $b:literal) => {
      ${ concat($a, $b) }
  }
}

fn main() {
    let x = cursed_add!(42, 73);
    dbg!(x);
}
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
 --> src/main.rs:4:18
  |
4 |       ${ concat($a, $b) }
  |                  ^
  |
  = note: currently only string literals are supported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. It would probably still be fine to leave it as open ended "literals" so docs don't need to update if this changes, but 🤷

@tgross35
Copy link
Contributor

tgross35 commented Apr 8, 2025

@rustbot author for the above

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 8, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@mejrs
Copy link
Contributor Author

mejrs commented Apr 8, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 8, 2025
Comment on lines 18 to 33
A common use case is the need to create names for structs or functions. The following cannot be done on stable Rust[^1]:

```rust,compile_fail
macro_rules! create_some_structs {
($name:ident) => {
// Invalid syntax
pub struct First$name;
// Also invalid syntax
pub struct Second($name);
// Macros are not allowed in this position
// (This restriction is what makes `concat_idents!` useless)
pub struct concat_idents!(Third, $name);
}
}
# create_some_structs!(Thing);
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit can be eliminated, macro_metavar_expr_concat shouldn't be discussed in terms of concat_idents's limitations.

# create_some_structs!(Thing);
```

`#![feature(macro_metavar_expr_concat)]` provides the `concat` metavariable to concatenate idents:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`#![feature(macro_metavar_expr_concat)]` provides the `concat` metavariable expression for creating new identifiers:

### Syntax

This feature builds upon the metavariable expression syntax `${ .. }` as specified in [RFC 3086] ([`macro_metavar_expr`]).
`concat` is available like `${ concat(items) }`, where `items` is a comma separated sequence of idents and/or string literals.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. It would probably still be fine to leave it as open ended "literals" so docs don't need to update if this changes, but 🤷

In stable Rust, there is no way to create new identifiers by joining identifiers to literals or other identifiers without using procedural macros such as [`paste`].
`#![feature(macro_metavar_expr_concat)]` introduces a way to do this, using the concat metavariable expression.

> This feature is not to be confused with [`macro_metavar_expr`] or [`concat_idents`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> This feature uses the syntax from [`macro_metavar_expr`] but is otherwise
> independent. It replaces the old unstable feature [`concat_idents`]. 

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 8, 2025
@mejrs
Copy link
Contributor Author

mejrs commented Apr 9, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants