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

implement item macros #3086

Closed
paulstansifer opened this issue Aug 2, 2012 · 7 comments
Closed

implement item macros #3086

paulstansifer opened this issue Aug 2, 2012 · 7 comments
Labels
A-syntaxext Area: Syntax extensions E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@paulstansifer
Copy link
Contributor

It's already possible to invoke macros in item position, but macro_rules currently only ever produces macros that mr_expr(...). It should be pretty easy to make it produce both kinds of macros. Perhaps (...) => (...) defines expr macros and (...) => {...} defines item macros?

Ideally, item macros should be able to produce multiple items. This would require changing fold.rs to handle ~[@item] instead of option<@item>, as it does now. This would require a bit more hacking; perhaps changing the AST to allow sequences of items in more places is an option (it should always be semantically valid, I believe).

@eholk
Copy link
Contributor

eholk commented Aug 2, 2012

What about having different macro defining forms for expression macros and item macros? I don't know that want the behavior to be different based on which delimiter I use. I could also see something like macro_rules! foo:expr { } and macro_rules! foo:items { }, although allowing those makes the macro syntax more flexible/subtle...

@eholk
Copy link
Contributor

eholk commented Aug 15, 2012

It'd be nice if an item macro could expand into multiple (including zero) items.

@nikomatsakis
Copy link
Contributor

In particular, #[auto_serialize] as currently implemented requires
this. However, auto_serialize is likely being converted to use the
visitor when possible, so maybe it's not an important case. Also,
auto_serialize is this sort of weird decorator form that may never
become something more standard.

@graydon
Copy link
Contributor

graydon commented Aug 17, 2012

I agree, expanding to >1 item is probably useful; at the moment you can probably fake it by expanding to a module-full-of-items.

@nikomatsakis
Copy link
Contributor

Two interesting use cases are ignore! and include!; both of which seem equally applicable to item or expr pos.

@paulstansifer
Copy link
Contributor Author

I've been alerted to the fact that X macro-like techniques (see http://stackoverflow.com/questions/264269/what-is-a-good-reference-documenting-patterns-of-use-of-x-macros-in-c-or-possib) may require that we allow macros that can expand either to exprs or to items depending on where they are invoked. This should be possible. I previously didn't think there was any value to it, but I guess I was wrong.

@catamorphism
Copy link
Contributor

This is done; open more specific issues if there are bugs.

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
RalfJung pushed a commit to RalfJung/rust that referenced this issue Sep 30, 2023
RalfJung pushed a commit to RalfJung/rust that referenced this issue Oct 1, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Dependency upgrade resulting from `cargo update`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

5 participants