Skip to content

Parse error on macro invocation expression at the start of a statement #7965

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

Closed
Deewiant opened this issue Jul 22, 2013 · 1 comment
Closed
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST

Comments

@Deewiant
Copy link
Contributor

The following results in a parse error:

$ rustc --no-trans - <<<'macro_rules! x(() => { "foo" }) fn main() { x!().len(); }' 
<anon>:1:48: 1:49 error: unexpected token: `.`
<anon>:1 macro_rules! x(() => { "foo" }) fn main() { x!().len(); }
                                                         ^

While e.g. the following, where the method call expression is wrapped in parentheses, does not:

$ rustc --no-trans - <<<'macro_rules! x(() => { "foo" }) fn main() { (x!().len()); }'

It seems as though the macro is assumed to expand to a statement if it is placed where a statement is allowed, causing an error even though it actually expands to an expression.

The Macros Tutorial says that macros are parsed as (paraphrased) "appropriate placeholders". Shouldn't this placeholder permit being treated as an expression even in a context where it isn't yet known whether it is an expression or not?

@huonw
Copy link
Member

huonw commented Aug 1, 2013

Thanks for the report; this is a dup of #5941, and is caused by exactly what you say: if a macro is syntactically allowed to expand as a statement, it is assumed to; so, the way to get it to expand as an expression is to put the macro invocation into an expression context, e.g. wrapped in parens, as you've done.

@huonw huonw closed this as completed Aug 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The lexing & parsing of Rust source code to an AST
Projects
None yet
Development

No branches or pull requests

2 participants