-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
base: master
Are you sure you want to change the base?
Conversation
@@ -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). |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 theconcat
metavariable expression:// example
The above expands to:
// expansion
There was a problem hiding this comment.
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.
src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
r? @tgross35 |
Happy to review this one. One general note: |
|
||
### Overview | ||
|
||
`macro_rules!` macros cannot create new identifiers and use them in ident positions. |
There was a problem hiding this comment.
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 theconcat
metavariable expression:// example
The above expands to:
// expansion
src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
Outdated
Show resolved
Hide resolved
src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
Outdated
Show resolved
Hide resolved
### 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. |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 🤷
src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
Outdated
Show resolved
Hide resolved
@rustbot author for the above |
Reminder, once the PR becomes ready for a review, use |
@rustbot ready |
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); | ||
``` |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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`]. |
There was a problem hiding this comment.
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 ready |
Rendered:
cc @c410-f3r