rust-lang / reference

The Rust Reference
https://doc.rust-lang.org/nightly/reference/
Apache License 2.0
1.16k stars 451 forks source link

Update macros-by-example.md #1480

Closed JoergBrueggmann closed 2 months ago

JoergBrueggmann commented 2 months ago

correct syntax 'MacroRulesDef' NOTE: I tried to use round and square brackets. It seems that only curly brackets are allowed.

JoergBrueggmann commented 2 months ago

Also NOTE: Later on, the MacroMatcher, can have round, square and curly brackets.

JoergBrueggmann commented 2 months ago

% rustc --version
rustc 1.76.0 (07dca489a 2024-02-04)

ehuss commented 2 months ago

Thanks for the PR! However, I don't quite understand the change. Parentheses and square brackets are valid: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4cb725835420bff6d2cd198e8c847581

Perhaps you are missing the semicolon?

JoergBrueggmann commented 2 months ago

Thanks for the PR! However, I don't quite understand the change. Parentheses and square brackets are valid: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4cb725835420bff6d2cd198e8c847581

Perhaps you are missing the semicolon?

Thank your for clarifying. Yes, inside you can use all three, but directly after the IDENTFIER the marco rules (symbol "MacroRules") can not be put into square brackets or round brackets. The marco rules can only be put into curly braces.

Look at the compiler error and it's suggestion:

error: macros that expand to items must be delimited with braces or followed by a semicolon
  --> src/lib_example/sub_example.rs:32:25
   |
32 |   macro_rules! test_macro [
   |  _________________________^
33 | |     { $( $x:literal ).* } => {
34 | |         {
35 | |             $(
...  |
39 | |     };
40 | | ]
   | |_^
   |
help: change the delimiters to curly braces
   |
32 ~ macro_rules! test_macro {
33 |     { $( $x:literal ).* } => {
 ...
39 |     };
40 + }
ehuss commented 2 months ago

In the given play link, it uses square or round brackets directly after the IDENTIFIER. The given error message explains the options:

must be delimited with braces or followed by a semicolon

It looks like from your error message, it is missing a semicolon.

JoergBrueggmann commented 2 months ago

must be delimited with braces or followed by a semicolon

OK, you are right. According to the syntax it has to end with semicolon when round or square brackets are used. When you use curly brackets then it must NOT use a semicolon at the end.

...% cargo run Compiling macros v0.1.0 (/.../macros) error: expected item, found ; --> src/lib_example/sub_example.rs:40:2 | 40 | }; | ^ help: remove this semicolon

I overlooked that. May be because such syntax looks wierd to me. In particular, why the exception for round or square brackets? May be there is a reason that I just don't understand.

However, the syntax is documented correctly.

I withdraw my PR. Sorry, for the inconvinence.

Best regards

Jörg

JoergBrueggmann commented 2 months ago

I hope it was the right action, to close the PR, was it? I am not used to GitHub PRs, so far.