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

Update macros-by-example.md #1480

Closed
wants to merge 1 commit into from

Conversation

JoergBrueggmann
Copy link

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

correct syntax '_MacroRulesDef_' 
NOTE: I tried to use round and square brackets. It seems that only curly brackets are allowed.
@JoergBrueggmann
Copy link
Author

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

@JoergBrueggmann
Copy link
Author

JoergBrueggmann commented Mar 5, 2024

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

@ehuss
Copy link
Contributor

ehuss commented Mar 5, 2024

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
Copy link
Author

JoergBrueggmann commented Mar 8, 2024

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
Copy link
Contributor

ehuss commented Mar 8, 2024

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
Copy link
Author

JoergBrueggmann commented Mar 9, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants