From 43c326ef6b93d12cc7355067b4770f668792e344 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Thu, 14 Aug 2014 11:55:47 +0200 Subject: [PATCH] Followup to PR #16477: a run-pass regression test for Issue #15750. --- src/test/auxiliary/macro_crate_test.rs | 12 ++++++++ .../macro-crate-does-hygiene-work.rs | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/test/run-pass-fulldeps/macro-crate-does-hygiene-work.rs diff --git a/src/test/auxiliary/macro_crate_test.rs b/src/test/auxiliary/macro_crate_test.rs index f6e96cca5c19c..b628320667666 100644 --- a/src/test/auxiliary/macro_crate_test.rs +++ b/src/test/auxiliary/macro_crate_test.rs @@ -19,6 +19,7 @@ use syntax::ast::{TokenTree, Item, MetaItem}; use syntax::codemap::Span; use syntax::ext::base::*; use syntax::parse::token; +use syntax::parse; use rustc::plugin::Registry; use std::gc::{Gc, GC}; @@ -32,6 +33,7 @@ macro_rules! unexported_macro (() => (3i)) pub fn plugin_registrar(reg: &mut Registry) { reg.register_macro("make_a_1", expand_make_a_1); reg.register_macro("forged_ident", expand_forged_ident); + reg.register_macro("identity", expand_identity); reg.register_syntax_extension( token::intern("into_foo"), ItemModifier(expand_into_foo)); @@ -45,6 +47,16 @@ fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) MacExpr::new(quote_expr!(cx, 1i)) } +// See Issue #15750 +fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree]) + -> Box { + // 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)) +} + fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: Gc, it: Gc) -> Gc { box(GC) Item { diff --git a/src/test/run-pass-fulldeps/macro-crate-does-hygiene-work.rs b/src/test/run-pass-fulldeps/macro-crate-does-hygiene-work.rs new file mode 100644 index 0000000000000..0afd76e1659c4 --- /dev/null +++ b/src/test/run-pass-fulldeps/macro-crate-does-hygiene-work.rs @@ -0,0 +1,28 @@ +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:macro_crate_test.rs +// ignore-stage1 + +// Issue #15750: a macro that internally parses its input and then +// uses `quote_expr!` to rearrange it should be hygiene-preserving. + +#![feature(phase)] + +#[phase(plugin)] +extern crate macro_crate_test; + +fn main() { + let x = 3i; + assert_eq!(3, identity!(x)); + assert_eq!(6, identity!(x+x)); + let x = 4i; + assert_eq!(4, identity!(x)); +}