Skip to content

Quote/splice discard syntactic context #15750

Closed
@kmcallister

Description

@kmcallister

This breaks even hygienic references to the environment at the macro use site. This has broken my match_token macro now that hygiene for self is enforced.

I think the right solution would involve a general overhaul of quote/splice, which currently works by pretty-printing and re-parsing the code :/

Example to reproduce the bug:

plugin.rs

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

extern crate syntax;
extern crate rustc;

use rustc::plugin::Registry;

use syntax::codemap::Span;
use syntax::ext::base::{ExtCtxt, MacResult, MacExpr};
use syntax::parse;
use syntax::ast;

fn expand(cx: &mut ExtCtxt, _span: Span, tts: &[ast::TokenTree])
        -> Box<MacResult> {
    // Parse an expression and emit it unchanged.
    let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
        cx.cfg(), Vec::from_slice(tts));
    let expr = parser.parse_expr();
    MacExpr::new(quote_expr!(&mut *cx, $expr))
}

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

test.rs

#![feature(phase)]

#[phase(plugin)]
extern crate plugin;

fn main() {
    let x = 3;
    println!("{}", mymacro!(x));
}

Result:

$ rustc -v
rustc 0.12.0-pre (459ffc2adc74f5e8b64a76f5670edb419b9f65da 2014-07-17 01:16:19 +0000)

$ rustc plugin.rs 

$ rustc -L. test.rs
test.rs:8:20: 8:31 error: unresolved name `x`.
test.rs:8     println!("{}", mymacro!(x));
                             ^~~~~~~~~~~
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions