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

Using macro with literal patterns in other macro fails #25875

Closed
waucka opened this issue May 29, 2015 · 4 comments
Closed

Using macro with literal patterns in other macro fails #25875

waucka opened this issue May 29, 2015 · 4 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.

Comments

@waucka
Copy link

waucka commented May 29, 2015

Code

fn foo1(x: &str) {
        println!("foo1({})", x);
}

fn foo2(x: &str) {
        println!("foo2({})", x);
}

macro_rules! wtf {
        (1) => (foo1);
        (2) => (foo2);
}

macro_rules! wtf_foo {
        ($x:expr, $num:expr) => {
                wtf!($num)($x);
        }
}

fn main() {
        //Fails
        wtf_foo!("one", 1);
        //Works
        wtf!(1)("one");
}

Output

$ rustc foo.rs 
foo.rs:16:8: 17:12 error: no rules expected the token `1`
foo.rs:16               wtf!($num)($x);
                             ^~~~

Expected Output

$ rustc foo.rs 
foo.rs:5:1: 7:2 warning: function is never used: `foo2`, #[warn(dead_code)] on by default
foo.rs:5 fn foo2(x: &str) {
foo.rs:6        println!("foo2({})", x);
foo.rs:7 }
$ ./foo
foo1(one)
foo1(one)

Other

This works fine, so the problem appears to be limited to literal patterns (not sure if that's the right term):

macro_rules! print_stuff_for_real {
        ($x:expr) => (println!("Printing stuff: {}", $x));
}

macro_rules! print_stuff {
        ($x:expr) => (print_stuff_for_real!($x));
}

fn main() {
        print_stuff!("stuff");
}

Meta

I used two versions of rustc; they behaved identically.

rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
binary: rustc
commit-hash: a59de37e99060162a2674e3ff45409ac73595c0e
commit-date: 2015-05-13
build-date: 2015-05-14
host: x86_64-unknown-linux-gnu
release: 1.0.0
rustc 1.2.0-nightly (f76d9bcfc 2015-05-28) (built 2015-05-28)
binary: rustc
commit-hash: f76d9bcfc2c269452522fbbe19f66fe653325646
commit-date: 2015-05-28
build-date: 2015-05-28
host: x86_64-unknown-linux-gnu
release: 1.2.0-nightly
@sfackler sfackler added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label May 29, 2015
@oli-obk
Copy link
Contributor

oli-obk commented May 29, 2015

so basically we need a lit bound for macro arguments...

@steveklabnik
Copy link
Member

Triage: yes, it seems like we need a new feature for macros for this, though I'm not good at macros, so I very much could be wrong.

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@nayato
Copy link

nayato commented Aug 23, 2017

is there a workaround for this?..

@dtolnay
Copy link
Member

dtolnay commented Dec 27, 2018

Closing as a duplicate of #41472 which is newer but has a more accurate and general title.

@dtolnay dtolnay closed this as completed Dec 27, 2018
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, ..) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

7 participants