Add example of making an unhygienic macro hygienic by wrapping it in a declarative macro.

This commit is contained in:
Jeffrey Seyfried 2017-12-06 20:30:40 -08:00
parent d052d28d70
commit b766fa887d
3 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,11 @@
// Copyright 2017 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
pub fn f() {}

View File

@ -0,0 +1,37 @@
// Copyright 2017 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![crate_type = "lib"]
extern crate my_crate;
pub fn g() {} // (a)
#[macro_export]
macro_rules! unhygienic_macro {
() => {
// (1) unhygienic: depends on `my_crate` in the crate root at the invocation site.
::my_crate::f();
// (2) unhygienic: defines `f` at the invocation site (in addition to the above point).
use my_crate::f;
f();
g(); // (3) unhygienic: `g` needs to be in scope at use site.
$crate::g(); // (4) hygienic: this always resolves to (a)
}
}
#[allow(unused)]
fn test_unhygienic() {
unhygienic_macro!();
f(); // `f` was defined at the use site
}

View File

@ -0,0 +1,43 @@
// Copyright 2017 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-pretty pretty-printing is unhygienic
// aux-build:my_crate.rs
// aux-build:unhygienic_example.rs
#![feature(decl_macro)]
extern crate unhygienic_example;
extern crate my_crate; // (b)
// Hygienic version of `unhygienic_macro`.
pub macro hygienic_macro() {
fn g() {} // (c)
::unhygienic_example::unhygienic_macro!();
// ^ Even though we invoke an unhygienic macro, `hygienic_macro` remains hygienic.
// In the above expansion:
// (1) `my_crate` always resolves to (b) regardless of invocation site.
// (2) The defined function `f` is only usable inside this macro definition.
// (3) `g` always resolves to (c) regardless of invocation site.
// (4) `$crate::g` remains hygienic and continues to resolve to (a).
f();
}
#[allow(unused)]
fn test_hygienic_macro() {
hygienic_macro!();
fn f() {} // (d) no conflict
f(); // resolves to (d)
}
fn main() {}