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

Allow macro_rules! to also define item and stmt macros #4021

Closed
wants to merge 17 commits into from

Conversation

paulstansifer
Copy link
Contributor

Now it's possible to invoke macros in statement and item position. The extra argument that syntax extensions in item position take is now optional (things defined with macro_rules! cannot see it).

At expansion time, the expansion of a macro defined with macro_rules! is parsed as an expr, an item, or a stmt, depending on where it is.

The delimiters around the RHS of a macro are no longer transcribed (this interfered with the parsing of items). This means that macro_rules! mytuple( () => (1,2,3) ) doesn't work; an extra set of delimiters must be added around the (1,2,3).

Closes #3086.

Outstanding issues:

  • Expanding to multiple items is still not done.
  • { my_expr_mac!() + 16 } is a parse error because the macro is interpreted as a statement. Workaround: wrap in parentheses.

@paulstansifer
Copy link
Contributor Author

One problem with this design is that item/expr/stmt macros are not distinguished except by where they are used, so there cannot be a section in the macro tutorial titled "Invocation location notation" ( :

@graydon
Copy link
Contributor

graydon commented Nov 29, 2012

r+, landing along with #4045. Though I'd appreciate a bit of explanation about why the delimiters can't be part of the RHS expansion anymore. I can sorta guess, but if you can say it in clear words I'd appreciate it.

(also: thanks! this is excellent)

@paulstansifer
Copy link
Contributor Author

With expressions, you can always take a valid expression and wrap it in () or {} and get a valid expression that means the same thing, so the old way of doing things was fine for them. However, items and statements can't be wrapped in delimiters at all. For token-tree reasons, we need to write the RHS with delimiters, but, to make it possible to express items and statements, the delimiters are now ignored.

@graydon
Copy link
Contributor

graydon commented Dec 4, 2012

That's what I figured, ok. Thanks. This landed as a88f9ac.

@graydon graydon closed this Dec 4, 2012
RalfJung added a commit to RalfJung/rust that referenced this pull request Nov 10, 2024
mati865 pushed a commit to mati865/rust that referenced this pull request Nov 12, 2024
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.

3 participants