rust/src/test/run-pass-fulldeps/proc-macro/auxiliary/hygiene_example_codegen.rs

37 lines
1.3 KiB
Rust
Raw Normal View History

2017-03-30 23:06:28 -05:00
// 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.
// no-prefer-dynamic
rustc: Stabilize much of the `proc_macro` feature This commit stabilizes some of the `proc_macro` language feature as well as a number of APIs in the `proc_macro` crate as [previously discussed][1]. This means that on stable Rust you can now define custom procedural macros which operate as attributes attached to items or `macro_rules!`-like bang-style invocations. This extends the suite of currently stable procedural macros, custom derives, with custom attributes and custom bang macros. Note though that despite the stabilization in this commit procedural macros are still not usable on stable Rust. To stabilize that we'll need to stabilize at least part of the `use_extern_macros` feature. Currently you can define a procedural macro attribute but you can't import it to call it! A summary of the changes made in this PR (as well as the various consequences) is: * The `proc_macro` language and library features are now stable. * Other APIs not stabilized in the `proc_macro` crate are now named under a different feature, such as `proc_macro_diagnostic` or `proc_macro_span`. * A few checks in resolution for `proc_macro` being enabled have switched over to `use_extern_macros` being enabled. This means that code using `#![feature(proc_macro)]` today will likely need to move to `#![feature(use_extern_macros)]`. It's intended that this PR, once landed, will be followed up with an attempt to stabilize a small slice of `use_extern_macros` just for procedural macros to make this feature 100% usable on stable. [1]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
2018-07-03 17:36:31 -05:00
#![feature(use_extern_macros, proc_macro_quote, proc_macro_non_items)]
2017-03-30 23:06:28 -05:00
#![crate_type = "proc-macro"]
extern crate proc_macro as proc_macro_renamed; // This does not break `quote!`
use proc_macro_renamed::{TokenStream, quote};
#[proc_macro]
pub fn hello(input: TokenStream) -> TokenStream {
quote!(hello_helper!($input))
//^ `hello_helper!` always resolves to the following proc macro,
//| no matter where `hello!` is used.
}
#[proc_macro]
pub fn hello_helper(input: TokenStream) -> TokenStream {
quote! {
extern crate hygiene_example; // This is never a conflict error
let string = format!("hello {}", $input);
//^ `format!` always resolves to the prelude macro,
//| even if a different `format!` is in scope where `hello!` is used.
hygiene_example::print(&string)
}
}