Skip to content

quote_expr! causes compile error when invoked twice on an ident nested in a macro invocation #21283

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
lambda-fairy opened this issue Jan 17, 2015 · 1 comment

Comments

@lambda-fairy
Copy link
Contributor

Sorry about the long title. I couldn't find a better way to express the issue :)

Given the following syntax extension:

#![crate_type = "dylib"]
#![allow(unstable)]
#![feature(plugin_registrar, quote)]

extern crate syntax;
extern crate rustc;

use syntax::ast::TokenTree;
use syntax::codemap::Span;
use syntax::ext::base::{DummyResult, ExtCtxt, MacExpr, MacResult};
use rustc::plugin::Registry;

fn expand<'cx>(cx: &'cx mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box<MacResult + 'cx> {
    match args {
        [ref tt, ..] => {
            let e = quote_expr!(cx, println!("{}", $tt));
            MacExpr::new(quote_expr!(cx, $e))
        },
        _ => DummyResult::any(sp),
    }
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    reg.register_macro("macro_test", expand);
}

and this test file:

#![feature(plugin)]

#[plugin] #[no_link] extern crate quote_fail;

#[test]
fn it_works() {
    let a = "hello";
    macro_test!(a);
}

The latter fails to compile with the following error:

<quote expansion>:1:27: 1:28 error: unresolved name `a`
<quote expansion>:1 name_228,ctxt_0!("{}" , a)
                                              ^
note: in expansion of format_args!
<std macros>:2:42: 2:75 note: expansion site
<std macros>:1:1: 2:77 note: in expansion of println!
<quote expansion>:1:1: 1:29 note: expansion site
/home/chris/dev/quote_fail/tests/tests.rs:1:1: 9:1 note: in expansion of macro_test!
/home/chris/dev/quote_fail/tests/tests.rs:8:5: 8:20 note: expansion site
error: aborting due to previous error
Could not compile `quote_fail`.

This error goes away if I do either of these things:

  • Remove the println! call, i.e. replace println!("{}", $tt) with $tt
  • Call quote_expr! only once instead of twice, i.e. MacExpr::new(e)

I'm using rustc commit 8903c21 built 2015-01-16 on Ubuntu 14.10 x86_64.

@lambda-fairy
Copy link
Contributor Author

This looks like a duplicate of #16617, so I'm closing this in favor of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant