Merge pull request #2284 from rust-lang-nursery/new-macro

Move mini-macro to proc macro
This commit is contained in:
Oliver Schneider 2017-12-20 21:56:00 +01:00 committed by GitHub
commit 2179f4d790
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 44 deletions

View file

@ -47,7 +47,7 @@ compiletest_rs = "0.3"
duct = "0.8.2"
lazy_static = "1.0"
serde_derive = "1.0"
clippy-mini-macro-test = { version = "0.1", path = "mini-macro" }
clippy-mini-macro-test = { version = "0.2", path = "mini-macro" }
serde = "1.0"
[features]

View file

@ -1,6 +1,6 @@
[package]
name = "clippy-mini-macro-test"
version = "0.1.0"
version = "0.2.0"
authors = [
"Manish Goregaokar <manishsmail@gmail.com>",
"Andre Bogus <bogusandre@gmail.com>",
@ -14,6 +14,6 @@ repository = "https://github.com/rust-lang-nursery/rust-clippy"
[lib]
name = "clippy_mini_macro_test"
plugin = true
proc-macro = true
[dependencies]

View file

@ -1,39 +1,11 @@
#![feature(plugin_registrar, rustc_private, quote)]
#![feature(proc_macro)]
extern crate proc_macro;
extern crate rustc_plugin;
extern crate syntax;
use proc_macro::{TokenStream, quote};
use rustc_plugin::Registry;
use syntax::ast::MetaItem;
use syntax::codemap::Span;
use syntax::ext::base::{Annotatable, ExtCtxt, MacEager, MacResult, SyntaxExtension};
use syntax::ext::build::AstBuilder; // trait for expr_usize
use syntax::symbol::Symbol;
use syntax::tokenstream::TokenTree;
fn expand_macro(cx: &mut ExtCtxt, sp: Span, _: &[TokenTree]) -> Box<MacResult + 'static> {
let e = cx.expr_usize(sp, 42);
let e = cx.expr_mut_addr_of(sp, e);
MacEager::expr(cx.expr_mut_addr_of(sp, e))
}
fn expand_attr_macro(cx: &mut ExtCtxt, _: Span, _: &MetaItem, annotated: Annotatable) -> Vec<Annotatable> {
vec![
Annotatable::Item(
quote_item!(
cx,
#[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
).unwrap()
),
annotated,
]
}
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_macro("mini_macro", expand_macro);
reg.register_syntax_extension(
Symbol::intern("mini_macro_attr"),
SyntaxExtension::MultiModifier(Box::new(expand_attr_macro)),
);
#[proc_macro_derive(ClippyMiniMacroTest)]
pub fn mini_macro(_: TokenStream) -> TokenStream {
quote!(
#[allow(unused)] fn needless_take_by_value(s: String) { println!("{}", s.len()); }
)
}

View file

@ -1,8 +1,12 @@
#![feature(plugin)]
#![plugin(clippy_mini_macro_test)]
#[macro_use]
extern crate clippy_mini_macro_test;
#[deny(warnings)]
#[mini_macro_attr]
fn main() {
let _ = mini_macro!();
let x = Foo;
println!("{:?}", x);
}
#[derive(ClippyMiniMacroTest, Debug)]
struct Foo;