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

Macros: limitation in the expression parser for <$:path>::<ident> #48067

Open
behnam opened this Issue Feb 8, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@behnam
Copy link
Contributor

behnam commented Feb 8, 2018

Background: https://users.rust-lang.org/t/macros-using-path-tokens-with-format-args/15480

When passing in a path token to a macro, then trying to suffix the metavariable with ::<ident> (or more), the parser cannot recognize the whole thing as an :expr, which causes failures on calls to macros like format_args!.

Repro:

macro_rules! something {
    ( $path:path ) => (
        //println!("Say: {}", $path::say);
        format_args!("Say: {}", $path::say);
    );
}

mod foo {
    const say: &str = "Hello";
}

mod bar {
    const say: &str = "World";

    mod baz {
        const say: &str = "Universe";
    }
}

fn main() {
    something!(foo);
    something!(bar);
    something!(bar::baz);
}

It fails with three instances of this error (with RUSTFLAGS='-Z external-macro-backtrace'):

error: expected token: `,`
  --> src/main.rs:4:9
   |
1  | / macro_rules! talk {
2  | |     ( $mod:path ) => (
3  | |         // print!("Say: {}", $mod::say);
4  | |         format_args!("Say: {}", $mod::say);
   | |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5  | |     );
6  | | }
   | |_- in this expansion of `talk!`
...
21 |       talk!(foo);
   |       ----------- in this macro invocation

A workaround is to use:

{ use $path as base; base::say }

but would be great if we could just use:

$path::say

I couldn't find an existing report. I'm guessing it falls under RFE.

@behnam behnam changed the title Macros: limitation in the expression parser for <$:path>::<tt> Macros: limitation in the expression parser for <$:path>::<ident> Feb 8, 2018

@durka

This comment has been minimized.

Copy link
Contributor

durka commented Feb 9, 2018

cc @jseyfried

This is known issue, but I don't know how hard it would be to make it "just work".

@petrochenkov

This comment has been minimized.

Copy link
Contributor

petrochenkov commented Feb 9, 2018

We can even do $path1::$path2 in general, this is very similar to what nested groups in imports already have to deal with.
I'll assign to myself, because this is already in my queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.