Skip to content

Commit dd9dcc1

Browse files
committed
Auto merge of #25024 - nrc:mulit-decor, r=sfackler
2 parents fa43387 + c544e83 commit dd9dcc1

22 files changed

+474
-279
lines changed

src/librustc/plugin/registry.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use lint::{LintPassObject, LintId, Lint};
1414
use session::Session;
1515

1616
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
17-
use syntax::ext::base::{IdentTT, Decorator, Modifier, MultiModifier, MacroRulesTT};
18-
use syntax::ext::base::MacroExpanderFn;
17+
use syntax::ext::base::{IdentTT, Decorator, Modifier, MultiModifier, MultiDecorator};
18+
use syntax::ext::base::{MacroExpanderFn, MacroRulesTT};
1919
use syntax::codemap::Span;
2020
use syntax::parse::token;
2121
use syntax::ptr::P;
@@ -84,6 +84,7 @@ impl<'a> Registry<'a> {
8484
/// Register a syntax extension of any kind.
8585
///
8686
/// This is the most general hook into `libsyntax`'s expansion behavior.
87+
#[allow(deprecated)]
8788
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
8889
self.syntax_exts.push((name, match extension {
8990
NormalTT(ext, _, allow_internal_unstable) => {
@@ -93,6 +94,7 @@ impl<'a> Registry<'a> {
9394
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
9495
}
9596
Decorator(ext) => Decorator(ext),
97+
MultiDecorator(ext) => MultiDecorator(ext),
9698
Modifier(ext) => Modifier(ext),
9799
MultiModifier(ext) => MultiModifier(ext),
98100
MacroRulesTT => {

src/libsyntax/ext/base.rs

+56-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use std::collections::HashMap;
3030
use std::rc::Rc;
3131
use std::default::Default;
3232

33+
#[unstable(feature = "rustc_private")]
34+
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemDecorator")]
3335
pub trait ItemDecorator {
3436
fn expand(&self,
3537
ecx: &mut ExtCtxt,
@@ -39,6 +41,9 @@ pub trait ItemDecorator {
3941
push: &mut FnMut(P<ast::Item>));
4042
}
4143

44+
#[allow(deprecated)]
45+
#[unstable(feature = "rustc_private")]
46+
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemDecorator")]
4247
impl<F> ItemDecorator for F
4348
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, &mut FnMut(P<ast::Item>))
4449
{
@@ -52,6 +57,8 @@ impl<F> ItemDecorator for F
5257
}
5358
}
5459

60+
#[unstable(feature = "rustc_private")]
61+
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemModifier")]
5562
pub trait ItemModifier {
5663
fn expand(&self,
5764
ecx: &mut ExtCtxt,
@@ -61,9 +68,13 @@ pub trait ItemModifier {
6168
-> P<ast::Item>;
6269
}
6370

71+
#[allow(deprecated)]
72+
#[unstable(feature = "rustc_private")]
73+
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemModifier")]
6474
impl<F> ItemModifier for F
6575
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item>
6676
{
77+
6778
fn expand(&self,
6879
ecx: &mut ExtCtxt,
6980
span: Span,
@@ -112,6 +123,16 @@ impl Annotatable {
112123
}
113124
}
114125

126+
pub fn map_item_or<F, G>(self, mut f: F, mut or: G) -> Annotatable
127+
where F: FnMut(P<ast::Item>) -> P<ast::Item>,
128+
G: FnMut(Annotatable) -> Annotatable
129+
{
130+
match self {
131+
Annotatable::Item(i) => Annotatable::Item(f(i)),
132+
_ => or(self)
133+
}
134+
}
135+
115136
pub fn expect_trait_item(self) -> P<ast::TraitItem> {
116137
match self {
117138
Annotatable::TraitItem(i) => i,
@@ -127,6 +148,29 @@ impl Annotatable {
127148
}
128149
}
129150

151+
// A more flexible ItemDecorator.
152+
pub trait MultiItemDecorator {
153+
fn expand(&self,
154+
ecx: &mut ExtCtxt,
155+
sp: Span,
156+
meta_item: &ast::MetaItem,
157+
item: Annotatable,
158+
push: &mut FnMut(Annotatable));
159+
}
160+
161+
impl<F> MultiItemDecorator for F
162+
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, Annotatable, &mut FnMut(Annotatable))
163+
{
164+
fn expand(&self,
165+
ecx: &mut ExtCtxt,
166+
sp: Span,
167+
meta_item: &ast::MetaItem,
168+
item: Annotatable,
169+
push: &mut FnMut(Annotatable)) {
170+
(*self)(ecx, sp, meta_item, item, push)
171+
}
172+
}
173+
130174
// A more flexible ItemModifier (ItemModifier should go away, eventually, FIXME).
131175
// meta_item is the annotation, item is the item being modified, parent_item
132176
// is the impl or trait item is declared in if item is part of such a thing.
@@ -397,12 +441,22 @@ impl MacResult for DummyResult {
397441
pub enum SyntaxExtension {
398442
/// A syntax extension that is attached to an item and creates new items
399443
/// based upon it.
400-
///
401-
/// `#[derive(...)]` is an `ItemDecorator`.
444+
#[unstable(feature = "rustc_private")]
445+
#[deprecated(since = "1.0.0", reason = "replaced by MultiDecorator")]
446+
#[allow(deprecated)]
402447
Decorator(Box<ItemDecorator + 'static>),
403448

449+
/// A syntax extension that is attached to an item and creates new items
450+
/// based upon it.
451+
///
452+
/// `#[derive(...)]` is a `MultiItemDecorator`.
453+
MultiDecorator(Box<MultiItemDecorator + 'static>),
454+
404455
/// A syntax extension that is attached to an item and modifies it
405456
/// in-place.
457+
#[unstable(feature = "rustc_private")]
458+
#[deprecated(since = "1.0.0", reason = "replaced by MultiModifier")]
459+
#[allow(deprecated)]
406460
Modifier(Box<ItemModifier + 'static>),
407461

408462
/// A syntax extension that is attached to an item and modifies it

src/libsyntax/ext/deriving/bounds.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Item};
11+
use ast::MetaItem;
1212
use codemap::Span;
13-
use ext::base::ExtCtxt;
13+
use ext::base::{ExtCtxt, Annotatable};
1414
use ext::deriving::generic::*;
1515
use ext::deriving::generic::ty::*;
16-
use ptr::P;
1716

1817
pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt,
1918
span: Span,
2019
_: &MetaItem,
21-
_: &Item,
22-
_: &mut FnMut(P<Item>))
20+
_: Annotatable,
21+
_: &mut FnMut(Annotatable))
2322
{
2423
cx.span_err(span, "this unsafe trait should be implemented explicitly");
2524
}
2625

2726
pub fn expand_deriving_copy(cx: &mut ExtCtxt,
2827
span: Span,
2928
mitem: &MetaItem,
30-
item: &Item,
31-
push: &mut FnMut(P<Item>))
29+
item: Annotatable,
30+
push: &mut FnMut(Annotatable))
3231
{
3332
let path = Path::new(vec![
3433
if cx.use_std { "std" } else { "core" },
@@ -46,5 +45,5 @@ pub fn expand_deriving_copy(cx: &mut ExtCtxt,
4645
associated_types: Vec::new(),
4746
};
4847

49-
trait_def.expand(cx, mitem, item, push);
48+
trait_def.expand(cx, mitem, &item, push);
5049
}

src/libsyntax/ext/deriving/clone.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Item, Expr};
11+
use ast::{MetaItem, Expr};
1212
use codemap::Span;
13-
use ext::base::ExtCtxt;
13+
use ext::base::{ExtCtxt, Annotatable};
1414
use ext::build::AstBuilder;
1515
use ext::deriving::generic::*;
1616
use ext::deriving::generic::ty::*;
@@ -20,8 +20,8 @@ use ptr::P;
2020
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
2121
span: Span,
2222
mitem: &MetaItem,
23-
item: &Item,
24-
push: &mut FnMut(P<Item>))
23+
item: Annotatable,
24+
push: &mut FnMut(Annotatable))
2525
{
2626
let inline = cx.meta_word(span, InternedString::new("inline"));
2727
let attrs = vec!(cx.attribute(span, inline));
@@ -47,7 +47,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
4747
associated_types: Vec::new(),
4848
};
4949

50-
trait_def.expand(cx, mitem, item, push)
50+
trait_def.expand(cx, mitem, &item, push)
5151
}
5252

5353
fn cs_clone(

src/libsyntax/ext/deriving/cmp/eq.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Item, Expr};
11+
use ast::{MetaItem, Expr};
1212
use codemap::Span;
13-
use ext::base::ExtCtxt;
13+
use ext::base::{ExtCtxt, Annotatable};
1414
use ext::build::AstBuilder;
1515
use ext::deriving::generic::*;
1616
use ext::deriving::generic::ty::*;
@@ -20,8 +20,8 @@ use ptr::P;
2020
pub fn expand_deriving_eq(cx: &mut ExtCtxt,
2121
span: Span,
2222
mitem: &MetaItem,
23-
item: &Item,
24-
push: &mut FnMut(P<Item>))
23+
item: Annotatable,
24+
push: &mut FnMut(Annotatable))
2525
{
2626
fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
2727
cs_same_method(
@@ -66,5 +66,5 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt,
6666
),
6767
associated_types: Vec::new(),
6868
};
69-
trait_def.expand(cx, mitem, item, push)
69+
trait_def.expand(cx, mitem, &item, push)
7070
}

src/libsyntax/ext/deriving/cmp/ord.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// except according to those terms.
1010

1111
use ast;
12-
use ast::{MetaItem, Item, Expr};
12+
use ast::{MetaItem, Expr};
1313
use codemap::Span;
14-
use ext::base::ExtCtxt;
14+
use ext::base::{ExtCtxt, Annotatable};
1515
use ext::build::AstBuilder;
1616
use ext::deriving::generic::*;
1717
use ext::deriving::generic::ty::*;
@@ -21,8 +21,8 @@ use ptr::P;
2121
pub fn expand_deriving_ord(cx: &mut ExtCtxt,
2222
span: Span,
2323
mitem: &MetaItem,
24-
item: &Item,
25-
push: &mut FnMut(P<Item>))
24+
item: Annotatable,
25+
push: &mut FnMut(Annotatable))
2626
{
2727
let inline = cx.meta_word(span, InternedString::new("inline"));
2828
let attrs = vec!(cx.attribute(span, inline));
@@ -48,7 +48,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt,
4848
associated_types: Vec::new(),
4949
};
5050

51-
trait_def.expand(cx, mitem, item, push)
51+
trait_def.expand(cx, mitem, &item, push)
5252
}
5353

5454

src/libsyntax/ext/deriving/cmp/partial_eq.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Item, Expr, self};
11+
use ast::{MetaItem, Expr, self};
1212
use codemap::Span;
13-
use ext::base::ExtCtxt;
13+
use ext::base::{ExtCtxt, Annotatable};
1414
use ext::build::AstBuilder;
1515
use ext::deriving::generic::*;
1616
use ext::deriving::generic::ty::*;
@@ -20,8 +20,8 @@ use ptr::P;
2020
pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
2121
span: Span,
2222
mitem: &MetaItem,
23-
item: &Item,
24-
push: &mut FnMut(P<Item>))
23+
item: Annotatable,
24+
push: &mut FnMut(Annotatable))
2525
{
2626
// structures are equal if all fields are equal, and non equal, if
2727
// any fields are not equal or if the enum variants are different
@@ -90,5 +90,5 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt,
9090
),
9191
associated_types: Vec::new(),
9292
};
93-
trait_def.expand(cx, mitem, item, push)
93+
trait_def.expand(cx, mitem, &item, push)
9494
}

src/libsyntax/ext/deriving/cmp/partial_ord.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
pub use self::OrderingOp::*;
1212

1313
use ast;
14-
use ast::{MetaItem, Item, Expr};
14+
use ast::{MetaItem, Expr};
1515
use codemap::Span;
16-
use ext::base::ExtCtxt;
16+
use ext::base::{ExtCtxt, Annotatable};
1717
use ext::build::AstBuilder;
1818
use ext::deriving::generic::*;
1919
use ext::deriving::generic::ty::*;
@@ -23,8 +23,8 @@ use ptr::P;
2323
pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
2424
span: Span,
2525
mitem: &MetaItem,
26-
item: &Item,
27-
push: &mut FnMut(P<Item>))
26+
item: Annotatable,
27+
push: &mut FnMut(Annotatable))
2828
{
2929
macro_rules! md {
3030
($name:expr, $op:expr, $equal:expr) => { {
@@ -80,7 +80,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt,
8080
],
8181
associated_types: Vec::new(),
8282
};
83-
trait_def.expand(cx, mitem, item, push)
83+
trait_def.expand(cx, mitem, &item, push)
8484
}
8585

8686
#[derive(Copy, Clone)]

src/libsyntax/ext/deriving/decodable.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//! The compiler code necessary for `#[derive(Decodable)]`. See encodable.rs for more.
1212
1313
use ast;
14-
use ast::{MetaItem, Item, Expr, MutMutable};
14+
use ast::{MetaItem, Expr, MutMutable};
1515
use codemap::Span;
16-
use ext::base::ExtCtxt;
16+
use ext::base::{ExtCtxt, Annotatable};
1717
use ext::build::AstBuilder;
1818
use ext::deriving::generic::*;
1919
use ext::deriving::generic::ty::*;
@@ -24,26 +24,26 @@ use ptr::P;
2424
pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt,
2525
span: Span,
2626
mitem: &MetaItem,
27-
item: &Item,
28-
push: &mut FnMut(P<Item>))
27+
item: Annotatable,
28+
push: &mut FnMut(Annotatable))
2929
{
3030
expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize")
3131
}
3232

3333
pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
3434
span: Span,
3535
mitem: &MetaItem,
36-
item: &Item,
37-
push: &mut FnMut(P<Item>))
36+
item: Annotatable,
37+
push: &mut FnMut(Annotatable))
3838
{
3939
expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize")
4040
}
4141

4242
fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
4343
span: Span,
4444
mitem: &MetaItem,
45-
item: &Item,
46-
push: &mut FnMut(P<Item>),
45+
item: Annotatable,
46+
push: &mut FnMut(Annotatable),
4747
krate: &'static str)
4848
{
4949
if !cx.use_std {
@@ -87,7 +87,7 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
8787
associated_types: Vec::new(),
8888
};
8989

90-
trait_def.expand(cx, mitem, item, push)
90+
trait_def.expand(cx, mitem, &item, push)
9191
}
9292

9393
fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,

0 commit comments

Comments
 (0)