Skip to content

Commit 20e1ea2

Browse files
committed
Remove the Modifier and Decorator kinds of syntax extensions.
This is a [breaking-change] for syntax extension authors. The fix is to use MultiModifier or MultiDecorator, which have the same functionality but are more flexible. Users of syntax extensions are unaffected.
1 parent 8f28c9b commit 20e1ea2

File tree

5 files changed

+3
-173
lines changed

5 files changed

+3
-173
lines changed

src/librustc/plugin/registry.rs

Lines changed: 1 addition & 3 deletions
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, MultiDecorator};
17+
use syntax::ext::base::{IdentTT, MultiModifier, MultiDecorator};
1818
use syntax::ext::base::{MacroExpanderFn, MacroRulesTT};
1919
use syntax::codemap::Span;
2020
use syntax::parse::token;
@@ -98,9 +98,7 @@ impl<'a> Registry<'a> {
9898
IdentTT(ext, _, allow_internal_unstable) => {
9999
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
100100
}
101-
Decorator(ext) => Decorator(ext),
102101
MultiDecorator(ext) => MultiDecorator(ext),
103-
Modifier(ext) => Modifier(ext),
104102
MultiModifier(ext) => MultiModifier(ext),
105103
MacroRulesTT => {
106104
self.sess.err("plugin tried to register a new MacroRulesTT");

src/libsyntax/ext/base.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,6 @@ use std::collections::HashMap;
3131
use std::rc::Rc;
3232
use std::default::Default;
3333

34-
#[unstable(feature = "rustc_private")]
35-
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemDecorator")]
36-
pub trait ItemDecorator {
37-
fn expand(&self,
38-
ecx: &mut ExtCtxt,
39-
sp: Span,
40-
meta_item: &ast::MetaItem,
41-
item: &ast::Item,
42-
push: &mut FnMut(P<ast::Item>));
43-
}
44-
45-
#[allow(deprecated)]
46-
#[unstable(feature = "rustc_private")]
47-
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemDecorator")]
48-
impl<F> ItemDecorator for F
49-
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, &mut FnMut(P<ast::Item>))
50-
{
51-
fn expand(&self,
52-
ecx: &mut ExtCtxt,
53-
sp: Span,
54-
meta_item: &ast::MetaItem,
55-
item: &ast::Item,
56-
push: &mut FnMut(P<ast::Item>)) {
57-
(*self)(ecx, sp, meta_item, item, push)
58-
}
59-
}
60-
61-
#[unstable(feature = "rustc_private")]
62-
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemModifier")]
63-
pub trait ItemModifier {
64-
fn expand(&self,
65-
ecx: &mut ExtCtxt,
66-
span: Span,
67-
meta_item: &ast::MetaItem,
68-
item: P<ast::Item>)
69-
-> P<ast::Item>;
70-
}
71-
72-
#[allow(deprecated)]
73-
#[unstable(feature = "rustc_private")]
74-
#[deprecated(since = "1.0.0", reason = "replaced by MultiItemModifier")]
75-
impl<F> ItemModifier for F
76-
where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P<ast::Item>) -> P<ast::Item>
77-
{
78-
79-
fn expand(&self,
80-
ecx: &mut ExtCtxt,
81-
span: Span,
82-
meta_item: &ast::MetaItem,
83-
item: P<ast::Item>)
84-
-> P<ast::Item> {
85-
(*self)(ecx, span, meta_item, item)
86-
}
87-
}
8834

8935
#[derive(Debug,Clone)]
9036
pub enum Annotatable {
@@ -460,26 +406,12 @@ impl MacResult for DummyResult {
460406

461407
/// An enum representing the different kinds of syntax extensions.
462408
pub enum SyntaxExtension {
463-
/// A syntax extension that is attached to an item and creates new items
464-
/// based upon it.
465-
#[unstable(feature = "rustc_private")]
466-
#[deprecated(since = "1.0.0", reason = "replaced by MultiDecorator")]
467-
#[allow(deprecated)]
468-
Decorator(Box<ItemDecorator + 'static>),
469-
470409
/// A syntax extension that is attached to an item and creates new items
471410
/// based upon it.
472411
///
473412
/// `#[derive(...)]` is a `MultiItemDecorator`.
474413
MultiDecorator(Box<MultiItemDecorator + 'static>),
475414

476-
/// A syntax extension that is attached to an item and modifies it
477-
/// in-place.
478-
#[unstable(feature = "rustc_private")]
479-
#[deprecated(since = "1.0.0", reason = "replaced by MultiModifier")]
480-
#[allow(deprecated)]
481-
Modifier(Box<ItemModifier + 'static>),
482-
483415
/// A syntax extension that is attached to an item and modifies it
484416
/// in-place. More flexible version than Modifier.
485417
MultiModifier(Box<MultiItemModifier + 'static>),

src/libsyntax/ext/expand.rs

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,9 @@ macro_rules! with_exts_frame {
639639
// When we enter a module, record it, for the sake of `module!`
640640
pub fn expand_item(it: P<ast::Item>, fld: &mut MacroExpander)
641641
-> SmallVector<P<ast::Item>> {
642-
let it = expand_item_modifiers(it, fld);
642+
let it = expand_item_multi_modifier(Annotatable::Item(it), fld);
643643

644-
expand_annotatable(Annotatable::Item(it), fld)
644+
expand_annotatable(it, fld)
645645
.into_iter().map(|i| i.expect_item()).collect()
646646
}
647647

@@ -1278,11 +1278,9 @@ macro_rules! partition {
12781278
}
12791279
}
12801280

1281-
partition!(modifiers, Modifier);
12821281
partition!(multi_modifiers, MultiModifier);
12831282

12841283

1285-
#[allow(deprecated)] // The `allow` is needed because the `Decorator` variant is used.
12861284
fn expand_decorators(a: Annotatable,
12871285
fld: &mut MacroExpander,
12881286
decorator_items: &mut SmallVector<Annotatable>,
@@ -1292,33 +1290,6 @@ fn expand_decorators(a: Annotatable,
12921290
let mname = intern(&attr.name());
12931291
match fld.cx.syntax_env.find(&mname) {
12941292
Some(rc) => match *rc {
1295-
Decorator(ref dec) => {
1296-
attr::mark_used(&attr);
1297-
1298-
fld.cx.bt_push(ExpnInfo {
1299-
call_site: attr.span,
1300-
callee: NameAndSpan {
1301-
format: MacroAttribute(mname),
1302-
span: Some(attr.span),
1303-
// attributes can do whatever they like,
1304-
// for now.
1305-
allow_internal_unstable: true,
1306-
}
1307-
});
1308-
1309-
// we'd ideally decorator_items.push_all(expand_item(item, fld)),
1310-
// but that double-mut-borrows fld
1311-
let mut items: SmallVector<Annotatable> = SmallVector::zero();
1312-
dec.expand(fld.cx,
1313-
attr.span,
1314-
&attr.node.value,
1315-
&a.clone().expect_item(),
1316-
&mut |item| items.push(Annotatable::Item(item)));
1317-
decorator_items.extend(items.into_iter()
1318-
.flat_map(|ann| expand_annotatable(ann, fld).into_iter()));
1319-
1320-
fld.cx.bt_pop();
1321-
}
13221293
MultiDecorator(ref dec) => {
13231294
attr::mark_used(&attr);
13241295

@@ -1395,58 +1366,6 @@ fn expand_item_multi_modifier(mut it: Annotatable,
13951366
expand_item_multi_modifier(it, fld)
13961367
}
13971368

1398-
#[allow(deprecated)] // This is needed because the `ItemModifier` trait is used
1399-
fn expand_item_modifiers(mut it: P<ast::Item>,
1400-
fld: &mut MacroExpander)
1401-
-> P<ast::Item> {
1402-
// partition the attributes into ItemModifiers and others
1403-
let (modifiers, other_attrs) = modifiers(&it.attrs, fld);
1404-
1405-
// update the attrs, leave everything else alone. Is this mutation really a good idea?
1406-
it = P(ast::Item {
1407-
attrs: other_attrs,
1408-
..(*it).clone()
1409-
});
1410-
1411-
if modifiers.is_empty() {
1412-
let it = expand_item_multi_modifier(Annotatable::Item(it), fld);
1413-
return it.expect_item();
1414-
}
1415-
1416-
for attr in &modifiers {
1417-
let mname = intern(&attr.name());
1418-
1419-
match fld.cx.syntax_env.find(&mname) {
1420-
Some(rc) => match *rc {
1421-
Modifier(ref mac) => {
1422-
attr::mark_used(attr);
1423-
fld.cx.bt_push(ExpnInfo {
1424-
call_site: attr.span,
1425-
callee: NameAndSpan {
1426-
format: MacroAttribute(mname),
1427-
span: Some(attr.span),
1428-
// attributes can do whatever they like,
1429-
// for now
1430-
allow_internal_unstable: true,
1431-
}
1432-
});
1433-
it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
1434-
fld.cx.bt_pop();
1435-
}
1436-
_ => unreachable!()
1437-
},
1438-
_ => unreachable!()
1439-
}
1440-
}
1441-
1442-
// Expansion may have added new ItemModifiers.
1443-
// It is possible, that an item modifier could expand to a multi-modifier or
1444-
// vice versa. In this case we will expand all modifiers before multi-modifiers,
1445-
// which might give an odd ordering. However, I think it is unlikely that the
1446-
// two kinds will be mixed, and old-style multi-modifiers are deprecated.
1447-
expand_item_modifiers(it, fld)
1448-
}
1449-
14501369
fn expand_impl_item(ii: P<ast::ImplItem>, fld: &mut MacroExpander)
14511370
-> SmallVector<P<ast::ImplItem>> {
14521371
match ii.node {

src/test/auxiliary/macro_crate_test.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ macro_rules! unexported_macro { () => (3) }
3030
pub fn plugin_registrar(reg: &mut Registry) {
3131
reg.register_macro("make_a_1", expand_make_a_1);
3232
reg.register_macro("identity", expand_identity);
33-
reg.register_syntax_extension(
34-
token::intern("into_foo"),
35-
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
36-
Modifier(Box::new(expand_into_foo)));
3733
reg.register_syntax_extension(
3834
token::intern("into_multi_foo"),
3935
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
@@ -62,14 +58,6 @@ fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
6258
MacEager::expr(quote_expr!(&mut *cx, $expr))
6359
}
6460

65-
fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
66-
-> P<Item> {
67-
P(Item {
68-
attrs: it.attrs.clone(),
69-
..(*quote_item!(cx, enum Foo { Bar, Baz }).unwrap()).clone()
70-
})
71-
}
72-
7361
fn expand_into_foo_multi(cx: &mut ExtCtxt,
7462
sp: Span,
7563
attr: &MetaItem,

src/test/run-pass-fulldeps/macro-crate.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#[macro_use] #[no_link]
1818
extern crate macro_crate_test;
1919

20-
#[into_foo]
21-
#[derive(PartialEq, Clone, Debug)]
22-
fn foo() -> AFakeTypeThatHadBetterGoAway {}
23-
2420
#[into_multi_foo]
2521
#[derive(PartialEq, Clone, Debug)]
2622
fn foo() -> AnotherFakeTypeThatHadBetterGoAway {}
@@ -41,9 +37,6 @@ pub fn main() {
4137
assert_eq!(1, make_a_1!());
4238
assert_eq!(2, exported_macro!());
4339

44-
assert_eq!(Foo::Bar, Foo::Bar);
45-
test(None::<Foo>);
46-
4740
assert_eq!(Foo2::Bar2, Foo2::Bar2);
4841
test(None::<Foo2>);
4942

0 commit comments

Comments
 (0)