Skip to content

Commit 20a9048

Browse files
committed
Add exmaple/test for quote! hygiene.
1 parent 7d493bd commit 20a9048

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(proc_macro)]
12+
13+
extern crate hygiene_example_codegen;
14+
15+
pub use hygiene_example_codegen::hello;
16+
17+
pub fn print(string: &str) {
18+
println!("{}", string);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// no-prefer-dynamic
12+
13+
#![feature(proc_macro)]
14+
#![crate_type = "proc-macro"]
15+
16+
extern crate proc_macro as proc_macro_renamed; // This does not break `quote!`
17+
18+
use proc_macro_renamed::{TokenStream, quote};
19+
20+
#[proc_macro]
21+
pub fn hello(input: TokenStream) -> TokenStream {
22+
quote!(hello_helper!($input))
23+
//^ `hello_helper!` always resolves to the following proc macro,
24+
//| no matter where `hello!` is used.
25+
}
26+
27+
#[proc_macro]
28+
pub fn hello_helper(input: TokenStream) -> TokenStream {
29+
quote! {
30+
extern crate hygiene_example; // This is never a conflict error
31+
let string = format!("hello {}", $input);
32+
//^ `format!` always resolves to the prelude macro,
33+
//| even if a different `format!` is in scope where `hello!` is used.
34+
hygiene_example::print(&string)
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:hygiene_example_codegen.rs
12+
// aux-build:hygiene_example.rs
13+
14+
#![feature(proc_macro)]
15+
16+
extern crate hygiene_example;
17+
use hygiene_example::hello;
18+
19+
fn main() {
20+
mod hygiene_example {} // no conflict with `extern crate hygiene_example;` from the proc macro
21+
macro_rules! format { () => {} } // does not interfere with `format!` from the proc macro
22+
macro_rules! hello_helper { () => {} } // similarly does not intefere with the proc macro
23+
24+
let string = "world"; // no conflict with `string` from the proc macro
25+
hello!(string);
26+
hello!(string);
27+
}

0 commit comments

Comments
 (0)