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: Unexpected token when metavariable is "tt", works with "expr" #27231

Closed
jonas-schievink opened this issue Jul 23, 2015 · 2 comments
Closed
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@jonas-schievink
Copy link
Contributor

http://is.gd/GKzXeD

struct S(u8);

macro_rules! mac {
    ( $val:tt ) => ( S($val) );
    //( $val:expr ) => ( S($val) );
}

fn main() {
    let s: S = mac!(4);
    println!("{}", s.0);
}
<anon>:4:24: 4:28 error: unexpected token: `4`
<anon>:4     ( $val:tt ) => ( S($val) );
                                ^~~~

The token 4 should be perfectly valid there. Replacing tt with expr in the macro "fixes" this, but since expr is much stricter, this limits what can be expressed.

Is this intended behaviour?

Related: #24145

@jonas-schievink
Copy link
Contributor Author

Indirecting via another macro that interprets the tt as an expr works around the issue as well:

struct S(u8);

macro_rules! mkexpr {
    ( $e:expr ) => ( $e );
}

macro_rules! mac {
    //( $val:tt ) => ( S($val) );   // Doesn't work
    ( $val:tt ) => ( S(mkexpr!($val)) );
}

fn main() {
    let s: S = mac!(4);
    println!("{}", s.0);
}

@steveklabnik steveklabnik added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jul 23, 2015
@huonw
Copy link
Member

huonw commented Oct 27, 2015

Dupe of #5846. Thanks for filing!

@huonw huonw closed this as completed Oct 27, 2015
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, ..)
Projects
None yet
Development

No branches or pull requests

3 participants