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 multi-statement macros #10681

Closed
sfackler opened this issue Nov 27, 2013 · 13 comments
Closed

Allow multi-statement macros #10681

sfackler opened this issue Nov 27, 2013 · 13 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-syntaxext Area: Syntax extensions

Comments

@sfackler
Copy link
Member

Macros can now expand to multiple items, but not multiple statements. This is a bit sad, especially since a macro that can expand in a module can't expand in a function. This will probably require a bit of restructuring in the parser, since there doesn't seem to be a good way to parse a series of expressions. parser::parse_block_tail is almost right, but would need to be pulled apart a bit.

@sfackler
Copy link
Member Author

Macros expanding to statements seem kind of broken right now even without this:

#[feature(macro_rules)]

macro_rules! foo (
    () => (
        struct Foo;
    )
)

fn main() {
    foo!()
}
test.rs:5:2: 5:8 error: found `struct` in ident position
test.rs:5       struct Foo;
                ^~~~~~
test.rs:5:9: 5:12 error: macro expansion ignores token `Foo` and any following
test.rs:5       struct Foo;
                       ^~~
error: aborting due to 2 previous errors

@emberian
Copy link
Member

cc @jbclements @paulstansifer

@thehydroimpulse
Copy link
Contributor

@cmr @sfackler Any news on this issue?

@sfackler
Copy link
Member Author

sfackler commented Feb 1, 2014

I haven't been working on it.

@treeman
Copy link
Contributor

treeman commented Aug 23, 2014

Still an issue

$ rustc -v
rustc 0.12.0-pre (2e5aea65c 2014-08-23 01:40:48 +0000)
#![feature(macro_rules)]

macro_rules! foo (
    () => (
        struct Foo;
    )
)

fn main() {
    foo!()
}
issue-10681.rs:5:9: 5:15 error: found `struct` in ident position
issue-10681.rs:5         struct Foo;
                         ^~~~~~
issue-10681.rs:5:16: 5:19 error: macro expansion ignores token `Foo` and any following
issue-10681.rs:5         struct Foo;
                                ^~~
error: aborting due to 2 previous errors

Would this require an RFC?

@emberian
Copy link
Member

@treeman no, I don't believe so.

@alexcrichton
Copy link
Member

@sfackler I feel like this issue is still open, but the original example you have compiles today. Do you have an updated example for this?

@sfackler
Copy link
Member Author

@alexcrichton

macro_rules! foo (
    () => (
        struct Foo;
        struct Bar;
    )
);

fn main() {
    foo!();
}

@arielb1 arielb1 added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) and removed I-wrong labels Jun 29, 2015
@durka
Copy link
Contributor

durka commented Sep 9, 2015

The situation is improved by #24155 but there are still hygiene issues (reported). Here is an example: http://is.gd/sGjll7

@marjakm
Copy link

marjakm commented Sep 9, 2015

Another example: http://is.gd/oZTig7
Also broken if I give in an ident for vector: http://is.gd/rg1Hh1
The workaround in the durkas example doesn't work in this case because of the lifetime. I couldn't find a way to work around this issue.

@durka
Copy link
Contributor

durka commented Dec 31, 2015

triage: multi statement macros are still allowed, but broken, because hygiene runs separately on each statement, so any bindings introduced are not accessible, even within the macro expansion.

cc @chris-chambers

@jseyfried
Copy link
Contributor

This was fixed in #32923.

@alexcrichton
Copy link
Member

Yay!

flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 23, 2023
make [`len_zero`] lint not spanning over parenthesis

sorry it should be a quick fix but I was caught up by other stuffs last couple weeks 🤦‍♂️

---

fixes: rust-lang#10529

changelog: make [`len_zero`] lint not spanning over parenthesis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

9 participants