rollup merge of #21052: nick29581/methods-ext

Allows modifiers to be used on methods, associated types, etc.

r? @sfackler
This commit is contained in:
Alex Crichton 2015-01-15 14:11:39 -08:00
commit 98d4d4997e
7 changed files with 419 additions and 121 deletions

View file

@ -1,4 +1,4 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@ -16,7 +16,7 @@
extern crate syntax;
extern crate rustc;
use syntax::ast::{TokenTree, Item, MetaItem};
use syntax::ast::{TokenTree, Item, MetaItem, ImplItem, TraitItem, Method};
use syntax::codemap::Span;
use syntax::ext::base::*;
use syntax::parse::token;
@ -37,6 +37,9 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(
token::intern("into_foo"),
Modifier(box expand_into_foo));
reg.register_syntax_extension(
token::intern("into_multi_foo"),
MultiModifier(box expand_into_foo_multi));
}
fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
@ -65,6 +68,30 @@ fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
})
}
fn expand_into_foo_multi(cx: &mut ExtCtxt,
sp: Span,
attr: &MetaItem,
it: Annotatable) -> Annotatable {
match it {
Annotatable::Item(it) => {
Annotatable::Item(P(Item {
attrs: it.attrs.clone(),
..(*quote_item!(cx, enum Foo2 { Bar2, Baz2 }).unwrap()).clone()
}))
}
Annotatable::ImplItem(it) => {
Annotatable::ImplItem(ImplItem::MethodImplItem(
quote_method!(cx, fn foo(&self) -> i32 { 42 })
))
}
Annotatable::TraitItem(it) => {
Annotatable::TraitItem(TraitItem::ProvidedMethod(
quote_method!(cx, fn foo(&self) -> i32 { 0 })
))
}
}
}
fn expand_forged_ident(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult+'static> {
use syntax::ext::quote::rt::*;

View file

@ -1,4 +1,4 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@ -20,12 +20,36 @@ extern crate macro_crate_test;
#[derive(PartialEq, Clone, Show)]
fn foo() -> AFakeTypeThatHadBetterGoAway {}
#[into_multi_foo]
#[derive(PartialEq, Clone, Show)]
fn foo() -> AnotherFakeTypeThatHadBetterGoAway {}
trait Qux {
#[into_multi_foo]
fn bar();
}
impl Qux for i32 {
#[into_multi_foo]
fn bar() {}
}
impl Qux for u8 {}
pub fn main() {
assert_eq!(1, make_a_1!());
assert_eq!(2, exported_macro!());
assert_eq!(Foo::Bar, Foo::Bar);
test(None::<Foo>);
assert_eq!(Foo2::Bar2, Foo2::Bar2);
test(None::<Foo2>);
let x = 10i32;
assert_eq!(x.foo(), 42);
let x = 10u8;
assert_eq!(x.foo(), 0);
}
fn test<T: PartialEq+Clone>(_: Option<T>) {}