Skip to content

Commit aa3fe20

Browse files
committed
Replace uses of Decorator and Modifier
1 parent 01172ee commit aa3fe20

File tree

5 files changed

+92
-32
lines changed

5 files changed

+92
-32
lines changed

src/librustc/plugin/registry.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ 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};
17+
use syntax::ext::base::{IdentTT, Decorator, MultiDecorator, Modifier, MultiModifier, MacroRulesTT};
1818
use syntax::ext::base::{MacroExpanderFn};
1919
use syntax::codemap::Span;
2020
use syntax::parse::token;
@@ -76,11 +76,13 @@ impl<'a> Registry<'a> {
7676
/// Register a syntax extension of any kind.
7777
///
7878
/// This is the most general hook into `libsyntax`'s expansion behavior.
79+
#[allow(deprecated)]
7980
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
8081
self.syntax_exts.push((name, match extension {
8182
NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
8283
IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
8384
Decorator(ext) => Decorator(ext),
85+
MultiDecorator(ext) => MultiDecorator(ext),
8486
Modifier(ext) => Modifier(ext),
8587
MultiModifier(ext) => MultiModifier(ext),
8688
MacroRulesTT => {

src/libsyntax/ext/base.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub trait ItemDecorator {
3939
push: Box<FnMut(P<ast::Item>)>);
4040
}
4141

42+
#[allow(deprecated)]
43+
#[deprecated="Replaced by MultiItemDecorator"]
4244
impl<F> ItemDecorator for F
4345
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, Box<FnMut(P<ast::Item>)>)
4446
{
@@ -62,6 +64,7 @@ pub trait ItemModifier {
6264
-> P<ast::Item>;
6365
}
6466

67+
#[allow(deprecated)]
6568
#[deprecated="Replaced by MultiItemModifier"]
6669
impl<F> ItemModifier for F
6770
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item>
@@ -157,18 +160,18 @@ pub trait MultiItemDecorator {
157160
sp: Span,
158161
meta_item: &ast::MetaItem,
159162
item: &Annotatable,
160-
push: Box<FnMut(P<Annotatable>)>);
163+
push: Box<FnMut(Annotatable)>);
161164
}
162165

163166
impl<F> MultiItemDecorator for F
164-
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &Annotatable, Box<FnMut(P<Annotatable>)>)
167+
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &Annotatable, Box<FnMut(Annotatable)>)
165168
{
166169
fn expand(&self,
167170
ecx: &mut ExtCtxt,
168171
sp: Span,
169172
meta_item: &ast::MetaItem,
170173
item: &Annotatable,
171-
push: Box<FnMut(P<Annotatable>)>) {
174+
push: Box<FnMut(Annotatable)>) {
172175
(*self)(ecx, sp, meta_item, item, push)
173176
}
174177
}
@@ -426,7 +429,7 @@ pub enum SyntaxExtension {
426429
/// A syntax extension that is attached to an item and creates new items
427430
/// based upon it.
428431
///
429-
/// `#[derive(...)]` is an `ItemDecorator`.
432+
/// `#[derive(...)]` is a `MultiItemDecorator`.
430433
MultiDecorator(Box<MultiItemDecorator + 'static>),
431434

432435
/// A syntax extension that is attached to an item and modifies it
@@ -499,7 +502,7 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
499502
builtin_normal_expander(
500503
ext::log_syntax::expand_syntax_ext));
501504
syntax_expanders.insert(intern("derive"),
502-
Decorator(box ext::deriving::expand_meta_derive));
505+
MultiDecorator(box ext::deriving::expand_meta_derive));
503506
syntax_expanders.insert(intern("deriving"),
504507
Decorator(box ext::deriving::expand_deprecated_deriving));
505508

@@ -562,7 +565,7 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
562565
builtin_normal_expander(
563566
ext::cfg::expand_cfg));
564567
syntax_expanders.insert(intern("cfg_attr"),
565-
Modifier(box ext::cfg_attr::expand));
568+
MultiModifier(box ext::cfg_attr::expand));
566569
syntax_expanders.insert(intern("trace_macros"),
567570
builtin_normal_expander(
568571
ext::trace_macros::expand_trace_macros));

src/libsyntax/ext/cfg_attr.rs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,70 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast;
11+
use ast::{self, TraitItem, ImplItem};
1212
use attr;
1313
use codemap::Span;
14-
use ext::base::ExtCtxt;
14+
use ext::base::{Annotatable, ExtCtxt};
1515
use ext::build::AstBuilder;
1616
use ptr::P;
1717

18-
pub fn expand(cx: &mut ExtCtxt, sp: Span, mi: &ast::MetaItem, it: P<ast::Item>) -> P<ast::Item> {
18+
macro_rules! fold_annotatable {
19+
($ann:expr, $item:ident => $oper:expr) => (
20+
match $ann {
21+
Annotatable::Item(it) => {
22+
let mut $item = (*it).clone();
23+
$oper;
24+
Annotatable::Item(P($item))
25+
}
26+
Annotatable::TraitItem(it) => {
27+
match it {
28+
TraitItem::RequiredMethod(mut $item) => {
29+
$oper;
30+
Annotatable::TraitItem(TraitItem::RequiredMethod($item))
31+
}
32+
TraitItem::ProvidedMethod(pm) => {
33+
let mut $item = (*pm).clone();
34+
$oper;
35+
Annotatable::TraitItem(TraitItem::ProvidedMethod(P($item)))
36+
}
37+
TraitItem::TypeTraitItem(at) => {
38+
let mut $item = (*at).clone();
39+
$oper;
40+
Annotatable::TraitItem(TraitItem::TypeTraitItem(P($item)))
41+
}
42+
}
43+
}
44+
Annotatable::ImplItem(it) => {
45+
match it {
46+
ImplItem::MethodImplItem(pm) => {
47+
let mut $item = (*pm).clone();
48+
$oper;
49+
Annotatable::ImplItem(ImplItem::MethodImplItem(P($item)))
50+
}
51+
ImplItem::TypeImplItem(at) => {
52+
let mut $item = (*at).clone();
53+
$oper;
54+
Annotatable::ImplItem(ImplItem::TypeImplItem(P($item)))
55+
}
56+
}
57+
}
58+
}
59+
);
60+
}
61+
62+
pub fn expand(cx: &mut ExtCtxt, sp: Span, mi: &ast::MetaItem, ann: Annotatable) -> Annotatable {
1963
let (cfg, attr) = match mi.node {
2064
ast::MetaList(_, ref mis) if mis.len() == 2 => (&mis[0], &mis[1]),
2165
_ => {
2266
cx.span_err(sp, "expected `#[cfg_attr(<cfg pattern>, <attr>)]`");
23-
return it;
67+
return ann;
2468
}
2569
};
2670

27-
let mut out = (*it).clone();
2871
if attr::cfg_matches(&cx.parse_sess.span_diagnostic, cx.cfg.as_slice(), &**cfg) {
29-
out.attrs.push(cx.attribute(attr.span, attr.clone()));
72+
let attr = cx.attribute(attr.span, attr.clone());
73+
fold_annotatable!(ann, item => item.attrs.push(attr))
74+
} else {
75+
ann
3076
}
31-
32-
P(out)
3377
}
34-

src/libsyntax/ext/deriving/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
//! FIXME (#2810): hygiene. Search for "__" strings (in other files too). We also assume "extra" is
1414
//! the standard library, and "std" is the core library.
1515
16-
use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord};
17-
use ext::base::ExtCtxt;
16+
use ast::{MetaItem, MetaList, MetaNameValue, MetaWord};
17+
use ext::base::{Annotatable, ExtCtxt};
1818
use codemap::Span;
19-
use ptr::P;
2019

2120
pub mod bounds;
2221
pub mod clone;
@@ -49,10 +48,20 @@ pub fn expand_deprecated_deriving(cx: &mut ExtCtxt,
4948
}
5049

5150
pub fn expand_meta_derive(cx: &mut ExtCtxt,
52-
_span: Span,
51+
span: Span,
5352
mitem: &MetaItem,
54-
item: &Item,
55-
mut push: Box<FnMut(P<Item>)>) {
53+
annotatable: &Annotatable,
54+
mut push: Box<FnMut(Annotatable)>)
55+
{
56+
// Derive can only be applied to items
57+
let item = match annotatable {
58+
&Annotatable::Item(ref it) => it.clone(),
59+
_ => {
60+
cx.span_err(span, "`derive` can only be applied to items");
61+
return;
62+
}
63+
};
64+
5665
match mitem.node {
5766
MetaNameValue(_, ref l) => {
5867
cx.span_err(l.span, "unexpected value in `derive`");
@@ -70,8 +79,8 @@ pub fn expand_meta_derive(cx: &mut ExtCtxt,
7079
MetaList(ref tname, _) |
7180
MetaWord(ref tname) => {
7281
macro_rules! expand {
73-
($func:path) => ($func(cx, titem.span, &**titem, item,
74-
|i| push(i)))
82+
($func:path) => ($func(cx, titem.span, &**titem,
83+
&*item, |i| push(Annotatable::Item(i))))
7584
}
7685

7786
match tname.get() {

src/libsyntax/ext/expand.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ pub fn expand_item(it: P<ast::Item>, fld: &mut MacroExpander)
488488
.into_iter().map(|i| i.expect_item()).collect()
489489
}
490490

491+
#[allow(deprecated)] // This is needed because the `ItemModifier` trait is used
491492
fn expand_item_modifiers(mut it: P<ast::Item>, fld: &mut MacroExpander)
492493
-> P<ast::Item> {
493494
// partition the attributes into ItemModifiers and others
@@ -1056,6 +1057,7 @@ impl<'a> Folder for PatIdentRenamer<'a> {
10561057
}
10571058
}
10581059

1060+
#[allow(deprecated)] // This is needed because the `Decorator` variant is used
10591061
fn expand_annotatable(a: Annotatable,
10601062
fld: &mut MacroExpander)
10611063
-> SmallVector<Annotatable> {
@@ -1092,7 +1094,8 @@ fn expand_annotatable(a: Annotatable,
10921094
dec.expand(fld.cx, attr.span, &*attr.node.value, &**it,
10931095
box |&mut: item| items.push(item));
10941096
decorator_items.extend(items.into_iter()
1095-
.flat_map(|item| expand_item(item, fld).into_iter()));
1097+
.flat_map(|item| expand_item(item, fld).into_iter()
1098+
.map(|i| Annotatable::Item(i))));
10961099

10971100
fld.cx.bt_pop();
10981101
}
@@ -1108,13 +1111,13 @@ fn expand_annotatable(a: Annotatable,
11081111
}
11091112
});
11101113

1111-
// we'd ideally decorator_items.push_all(expand_item(item, fld)),
1114+
// we'd ideally decorator_items.push_all(expand_annotatable(ann, fld)),
11121115
// but that double-mut-borrows fld
1113-
let mut items: SmallVector<P<ast::Item>> = SmallVector::zero();
1114-
dec.expand(fld.cx, attr.span, &*attr.node.value, a,
1115-
box |&mut: item| items.push(item));
1116-
decorator_items.extend(items.into_iter()
1117-
.flat_map(|item| expand_item(item, fld).into_iter()));
1116+
let mut anns: SmallVector<Annotatable> = SmallVector::zero();
1117+
dec.expand(fld.cx, attr.span, &*attr.node.value, &a,
1118+
box |&mut: ann| anns.push(ann));
1119+
decorator_items.extend(anns.into_iter()
1120+
.flat_map(|ann| expand_annotatable(ann, fld).into_iter()));
11181121

11191122
fld.cx.bt_pop();
11201123
}
@@ -1179,7 +1182,7 @@ fn expand_annotatable(a: Annotatable,
11791182
}
11801183
};
11811184

1182-
new_items.push_all(decorator_items.into_iter().map(|i| Annotatable::Item(i)).collect());
1185+
new_items.push_all(decorator_items);
11831186
new_items
11841187
}
11851188

0 commit comments

Comments
 (0)