mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Get tests passing
This commit is contained in:
parent
b2ddd937b2
commit
aa5ca282b2
@ -398,7 +398,8 @@ impl<'a> TraitDef<'a> {
|
|||||||
generics)
|
generics)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(mitem.span, "`derive` may only be applied to structs and enums");
|
cx.span_err(mitem.span,
|
||||||
|
"`derive` may only be applied to structs and enums");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -417,7 +418,7 @@ impl<'a> TraitDef<'a> {
|
|||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
cx.span_err(mitem.span, "`derive` may only be applied to structs and enums");
|
cx.span_err(mitem.span, "`derive` may only be applied to structs and enums");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ extern crate rustc;
|
|||||||
|
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::{Decorator, ExtCtxt};
|
use syntax::ext::base::{Decorator, ExtCtxt, Annotatable};
|
||||||
use syntax::ext::build::AstBuilder;
|
use syntax::ext::build::AstBuilder;
|
||||||
use syntax::ext::deriving::generic::{cs_fold, TraitDef, MethodDef, combine_substructure};
|
use syntax::ext::deriving::generic::{cs_fold, TraitDef, MethodDef, combine_substructure};
|
||||||
use syntax::ext::deriving::generic::ty::{Literal, LifetimeBounds, Path, borrowed_explicit_self};
|
use syntax::ext::deriving::generic::ty::{Literal, LifetimeBounds, Path, borrowed_explicit_self};
|
||||||
@ -70,5 +70,13 @@ fn expand(cx: &mut ExtCtxt,
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
trait_def.expand(cx, mitem, item, push)
|
trait_def.expand(cx,
|
||||||
|
mitem,
|
||||||
|
Annotatable::Item(P(item.clone())),
|
||||||
|
&mut |i| {
|
||||||
|
match i {
|
||||||
|
Annotatable::Item(i) => push(i),
|
||||||
|
_ => panic!("Not an item")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,12 @@
|
|||||||
|
|
||||||
// force-host
|
// force-host
|
||||||
|
|
||||||
#![feature(plugin_registrar, quote)]
|
#![feature(plugin_registrar, quote, rustc_private)]
|
||||||
#![feature(box_syntax, rustc_private)]
|
|
||||||
|
|
||||||
extern crate syntax;
|
extern crate syntax;
|
||||||
extern crate rustc;
|
extern crate rustc;
|
||||||
|
|
||||||
use syntax::ast::{self, TokenTree, Item, MetaItem, Method, ImplItem, TraitItem};
|
use syntax::ast::{self, TokenTree, Item, MetaItem, ImplItem, TraitItem};
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::base::*;
|
use syntax::ext::base::*;
|
||||||
use syntax::parse::{self, token};
|
use syntax::parse::{self, token};
|
||||||
@ -25,7 +24,6 @@ use rustc::plugin::Registry;
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! exported_macro { () => (2) }
|
macro_rules! exported_macro { () => (2) }
|
||||||
|
|
||||||
macro_rules! unexported_macro { () => (3) }
|
macro_rules! unexported_macro { () => (3) }
|
||||||
|
|
||||||
#[plugin_registrar]
|
#[plugin_registrar]
|
||||||
@ -42,7 +40,8 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||||||
MultiModifier(Box::new(expand_into_foo_multi)));
|
MultiModifier(Box::new(expand_into_foo_multi)));
|
||||||
reg.register_syntax_extension(
|
reg.register_syntax_extension(
|
||||||
token::intern("duplicate"),
|
token::intern("duplicate"),
|
||||||
MultiDecorator(box expand_duplicate));
|
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
|
||||||
|
MultiDecorator(Box::new(expand_duplicate)));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
|
fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
|
||||||
@ -109,13 +108,13 @@ fn expand_into_foo_multi(cx: &mut ExtCtxt,
|
|||||||
fn expand_duplicate(cx: &mut ExtCtxt,
|
fn expand_duplicate(cx: &mut ExtCtxt,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
mi: &MetaItem,
|
mi: &MetaItem,
|
||||||
it: &Annotatable,
|
it: Annotatable,
|
||||||
mut push: Box<FnMut(Annotatable)>)
|
push: &mut FnMut(Annotatable))
|
||||||
{
|
{
|
||||||
let copy_name = match mi.node {
|
let copy_name = match mi.node {
|
||||||
ast::MetaItem_::MetaList(_, ref xs) => {
|
ast::MetaItem_::MetaList(_, ref xs) => {
|
||||||
if let ast::MetaItem_::MetaWord(ref w) = xs[0].node {
|
if let ast::MetaItem_::MetaWord(ref w) = xs[0].node {
|
||||||
token::str_to_ident(w.get())
|
token::str_to_ident(&w)
|
||||||
} else {
|
} else {
|
||||||
cx.span_err(mi.span, "Expected word");
|
cx.span_err(mi.span, "Expected word");
|
||||||
return;
|
return;
|
||||||
@ -136,75 +135,18 @@ fn expand_duplicate(cx: &mut ExtCtxt,
|
|||||||
push(Annotatable::Item(P(new_it)));
|
push(Annotatable::Item(P(new_it)));
|
||||||
}
|
}
|
||||||
Annotatable::ImplItem(it) => {
|
Annotatable::ImplItem(it) => {
|
||||||
match it {
|
let mut new_it = (*it).clone();
|
||||||
ImplItem::MethodImplItem(m) => {
|
new_it.attrs.clear();
|
||||||
let mut new_m = (*m).clone();
|
new_it.ident = copy_name;
|
||||||
new_m.attrs.clear();
|
push(Annotatable::ImplItem(P(new_it)));
|
||||||
replace_method_name(&mut new_m.node, copy_name);
|
|
||||||
push(Annotatable::ImplItem(ImplItem::MethodImplItem(P(new_m))));
|
|
||||||
}
|
|
||||||
ImplItem::TypeImplItem(t) => {
|
|
||||||
let mut new_t = (*t).clone();
|
|
||||||
new_t.attrs.clear();
|
|
||||||
new_t.ident = copy_name;
|
|
||||||
push(Annotatable::ImplItem(ImplItem::TypeImplItem(P(new_t))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Annotatable::TraitItem(it) => {
|
Annotatable::TraitItem(tt) => {
|
||||||
match it {
|
let mut new_it = (*tt).clone();
|
||||||
TraitItem::RequiredMethod(rm) => {
|
new_it.attrs.clear();
|
||||||
let mut new_rm = rm.clone();
|
new_it.ident = copy_name;
|
||||||
new_rm.attrs.clear();
|
push(Annotatable::TraitItem(P(new_it)));
|
||||||
new_rm.ident = copy_name;
|
|
||||||
push(Annotatable::TraitItem(TraitItem::RequiredMethod(new_rm)));
|
|
||||||
}
|
|
||||||
TraitItem::ProvidedMethod(pm) => {
|
|
||||||
let mut new_pm = (*pm).clone();
|
|
||||||
new_pm.attrs.clear();
|
|
||||||
replace_method_name(&mut new_pm.node, copy_name);
|
|
||||||
push(Annotatable::TraitItem(TraitItem::ProvidedMethod(P(new_pm))));
|
|
||||||
}
|
|
||||||
TraitItem::TypeTraitItem(t) => {
|
|
||||||
let mut new_t = (*t).clone();
|
|
||||||
new_t.attrs.clear();
|
|
||||||
new_t.ty_param.ident = copy_name;
|
|
||||||
push(Annotatable::TraitItem(TraitItem::TypeTraitItem(P(new_t))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_method_name(m: &mut ast::Method_, i: ast::Ident) {
|
|
||||||
if let &mut ast::Method_::MethDecl(ref mut ident, _, _, _, _, _, _, _) = m {
|
|
||||||
*ident = i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expand_forged_ident(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult+'static> {
|
|
||||||
use syntax::ext::quote::rt::*;
|
|
||||||
|
|
||||||
if !tts.is_empty() {
|
|
||||||
cx.span_fatal(sp, "forged_ident takes no arguments");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Most of this is modelled after the expansion of the `quote_expr!`
|
|
||||||
// macro ...
|
|
||||||
let parse_sess = cx.parse_sess();
|
|
||||||
let cfg = cx.cfg();
|
|
||||||
|
|
||||||
// ... except this is where we inject a forged identifier,
|
|
||||||
// and deliberately do not call `cx.parse_tts_with_hygiene`
|
|
||||||
// (because we are testing that this will be *rejected*
|
|
||||||
// by the default parser).
|
|
||||||
|
|
||||||
let expr = {
|
|
||||||
let tt = cx.parse_tts("\x00name_2,ctxt_0\x00".to_string());
|
|
||||||
let mut parser = new_parser_from_tts(parse_sess, cfg, tt);
|
|
||||||
parser.parse_expr()
|
|
||||||
};
|
|
||||||
MacEager::expr(expr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn foo() {}
|
pub fn foo() {}
|
||||||
|
@ -11,12 +11,15 @@
|
|||||||
// aux-build:macro_crate_test.rs
|
// aux-build:macro_crate_test.rs
|
||||||
// ignore-stage1
|
// ignore-stage1
|
||||||
|
|
||||||
#![feature(plugin)]
|
#![feature(plugin, custom_attribute)]
|
||||||
|
#![plugin(macro_crate_test)]
|
||||||
|
|
||||||
#[plugin] #[no_link]
|
#[macro_use]
|
||||||
|
#[no_link]
|
||||||
extern crate macro_crate_test;
|
extern crate macro_crate_test;
|
||||||
|
|
||||||
// The duplicate macro will create a copy of the item with the given identifier
|
// The duplicate macro will create a copy of the item with the given identifier.
|
||||||
|
|
||||||
#[duplicate(MyCopy)]
|
#[duplicate(MyCopy)]
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
number: i32
|
number: i32
|
||||||
|
Loading…
Reference in New Issue
Block a user