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

recursive macro --- macro doesn't exist #14946

Closed
BurntSushi opened this issue Jun 16, 2014 · 6 comments · Fixed by #15834
Closed

recursive macro --- macro doesn't exist #14946

BurntSushi opened this issue Jun 16, 2014 · 6 comments · Fixed by #15834
Labels
A-syntaxext Area: Syntax extensions

Comments

@BurntSushi
Copy link
Member

I believe this should output the string abc:

#![feature(macro_rules)]

macro_rules! rec(
    () => ("");
    ($a:ident) => (stringify!($a));
    ($a:ident, $($rest:tt),*) => (
        concat!(stringify!($a), rec!($($rest),*))
    );
)

fn main() {
    println!("{}", rec!(a, b, c));
}

But I get this error:

reem.rs:7:33: 7:36 error: macro undefined: 'rec'
reem.rs:7         concat!(stringify!($a), rec!($($rest),*))

Rust version:

[andrew@Liger rust] rustc --version
rustc 0.11.0-pre (6d8342f 2014-06-14 17:51:49 +0000)
host: x86_64-unknown-linux-gnu
@BurntSushi
Copy link
Member Author

Note that standard recursive macros work fine, e.g.,

macro_rules! rec_works(
    () => (0);
    ($a:expr) => ($a);
    ($a:expr, $($rest:tt),*) => (
        $a + rec_works!($($rest),*)
    );
)

And rec_works!(1, 2, 3) outputs 6.

Maybe the bug here is that expanding macros within other macros is using an older environment that doesn't include the name rec?

@sfackler
Copy link
Member

It's even worse than that. expand_expr only uses the compiler built-in macros: https://github.com/rust-lang/rust/blob/master/src/libsyntax/ext/base.rs#L421

@huonw
Copy link
Member

huonw commented Jun 17, 2014

I think this might be a dupe of #12404.

@BurntSushi
Copy link
Member Author

@huonw Does fixing print!(format!(...)) mean that print!(some_recursive_call!(...)) also gets fixed? I wouldn't assume so, but it might be possible depending on the implementation.

(Sorry I missed that issue. I searched for 'recursive macro' instead of 'nested macro'!)

@sfackler
Copy link
Member

@BurntSushi both print! and concat_idents! use expand_expr, which doesn't work with non-builtin macros.

@BurntSushi
Copy link
Member Author

@sfackler Right. I'm referring to the recursive aspect. Wasn't sure if that would make a difference. (I just don't know enough about how macros are expanded to know. Pure guesswork on my part.)

bors added a commit that referenced this issue Jul 22, 2014
- Made custom syntax extensions capable of expanding custom macros by moving `SyntaxEnv` into `ExtCtx`
- Added convenience method on `ExtCtx` for getting a macro expander.
- Made a few things private to force only a single way to use them (through `ExtCtx`)
- Removed some ancient commented-out code

Closes #14946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants