Skip to content

Commit 2abdc88

Browse files
committed
Remove MacroRulesTT.
1 parent 0ddb66c commit 2abdc88

File tree

6 files changed

+51
-57
lines changed

6 files changed

+51
-57
lines changed

src/librustc_plugin/registry.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::mir::transform::MirMapPass;
1717

1818
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
1919
use syntax::ext::base::{IdentTT, MultiModifier, MultiDecorator};
20-
use syntax::ext::base::{MacroExpanderFn, MacroRulesTT};
20+
use syntax::ext::base::MacroExpanderFn;
2121
use syntax::parse::token;
2222
use syntax::ast;
2323
use syntax::feature_gate::AttributeType;
@@ -111,10 +111,6 @@ impl<'a> Registry<'a> {
111111
}
112112
MultiDecorator(ext) => MultiDecorator(ext),
113113
MultiModifier(ext) => MultiModifier(ext),
114-
MacroRulesTT => {
115-
self.sess.err("plugin tried to register a new MacroRulesTT");
116-
return;
117-
}
118114
}));
119115
}
120116

src/libsyntax/ext/base.rs

-3
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,6 @@ pub enum SyntaxExtension {
457457
/// the block.
458458
///
459459
IdentTT(Box<IdentMacroExpander + 'static>, Option<Span>, bool),
460-
461-
/// Represents `macro_rules!` itself.
462-
MacroRulesTT,
463460
}
464461

465462
pub type NamedSyntaxExtension = (Name, SyntaxExtension);

src/libsyntax/ext/expand.rs

+1-41
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ast::{Block, Crate, Ident, Mac_, PatKind};
1212
use ast::{MacStmtStyle, StmtKind, ItemKind};
1313
use ast;
1414
use ext::hygiene::Mark;
15-
use ext::placeholders::{self, placeholder, PlaceholderExpander};
15+
use ext::placeholders::{placeholder, PlaceholderExpander};
1616
use attr::{self, HasAttrs};
1717
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
1818
use syntax_pos::{self, Span, ExpnId};
@@ -377,46 +377,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
377377
kind.make_from(expander.expand(self.cx, span, ident, marked_tts, attrs))
378378
}
379379

380-
MacroRulesTT => {
381-
if ident.name == keywords::Invalid.name() {
382-
self.cx.span_err(path.span,
383-
&format!("macro {}! expects an ident argument", extname));
384-
return kind.dummy(span);
385-
};
386-
387-
self.cx.bt_push(ExpnInfo {
388-
call_site: span,
389-
callee: NameAndSpan {
390-
format: MacroBang(extname),
391-
span: None,
392-
// `macro_rules!` doesn't directly allow unstable
393-
// (this is orthogonal to whether the macro it creates allows it)
394-
allow_internal_unstable: false,
395-
}
396-
});
397-
398-
let def = ast::MacroDef {
399-
ident: ident,
400-
id: ast::DUMMY_NODE_ID,
401-
span: span,
402-
imported_from: None,
403-
use_locally: true,
404-
body: marked_tts,
405-
export: attr::contains_name(&attrs, "macro_export"),
406-
allow_internal_unstable: attr::contains_name(&attrs, "allow_internal_unstable"),
407-
attrs: attrs,
408-
};
409-
410-
self.cx.insert_macro(def.clone());
411-
412-
// If keep_macs is true, expands to a MacEager::items instead.
413-
if self.cx.ecfg.keep_macs {
414-
Some(placeholders::reconstructed_macro_rules(&def, &path))
415-
} else {
416-
Some(placeholders::macro_scope_placeholder())
417-
}
418-
}
419-
420380
MultiDecorator(..) | MultiModifier(..) => {
421381
self.cx.span_err(path.span,
422382
&format!("`{}` can only be used in attributes", extname));

src/libsyntax/ext/placeholders.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use codemap::{DUMMY_SP, dummy_spanned};
1313
use ext::base::ExtCtxt;
1414
use ext::expand::{Expansion, ExpansionKind};
1515
use fold::*;
16-
use parse::token::keywords;
16+
use parse::token::{intern, keywords};
1717
use ptr::P;
1818
use util::move_map::MoveMap;
1919
use util::small_vector::SmallVector;
@@ -214,15 +214,22 @@ impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> {
214214
}
215215
}
216216

217-
pub fn reconstructed_macro_rules(def: &ast::MacroDef, path: &ast::Path) -> Expansion {
217+
pub fn reconstructed_macro_rules(def: &ast::MacroDef) -> Expansion {
218218
Expansion::Items(SmallVector::one(P(ast::Item {
219219
ident: def.ident,
220220
attrs: def.attrs.clone(),
221221
id: ast::DUMMY_NODE_ID,
222222
node: ast::ItemKind::Mac(ast::Mac {
223223
span: def.span,
224224
node: ast::Mac_ {
225-
path: path.clone(),
225+
path: ast::Path {
226+
span: DUMMY_SP,
227+
global: false,
228+
segments: vec![ast::PathSegment {
229+
identifier: ast::Ident::with_empty_ctxt(intern("macro_rules")),
230+
parameters: ast::PathParameters::none(),
231+
}],
232+
},
226233
tts: def.body.clone(),
227234
}
228235
}),

src/libsyntax/ext/tt/macro_rules.rs

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

11-
use ast;
11+
use {ast, attr};
1212
use syntax_pos::{Span, DUMMY_SP};
13-
use ext::base::{DummyResult, ExtCtxt, MacResult, SyntaxExtension};
14-
use ext::base::{NormalTT, TTMacroExpander};
13+
use ext::base::{DummyResult, ExtCtxt, MacEager, MacResult, SyntaxExtension};
14+
use ext::base::{IdentMacroExpander, NormalTT, TTMacroExpander};
15+
use ext::placeholders;
1516
use ext::tt::macro_parser::{Success, Error, Failure};
1617
use ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
1718
use ext::tt::macro_parser::parse;
@@ -242,6 +243,38 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
242243
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]);
243244
}
244245

246+
pub struct MacroRulesExpander;
247+
impl IdentMacroExpander for MacroRulesExpander {
248+
fn expand(&self,
249+
cx: &mut ExtCtxt,
250+
span: Span,
251+
ident: ast::Ident,
252+
tts: Vec<tokenstream::TokenTree>,
253+
attrs: Vec<ast::Attribute>)
254+
-> Box<MacResult> {
255+
let def = ast::MacroDef {
256+
ident: ident,
257+
id: ast::DUMMY_NODE_ID,
258+
span: span,
259+
imported_from: None,
260+
use_locally: true,
261+
body: tts,
262+
export: attr::contains_name(&attrs, "macro_export"),
263+
allow_internal_unstable: attr::contains_name(&attrs, "allow_internal_unstable"),
264+
attrs: attrs,
265+
};
266+
267+
cx.insert_macro(def.clone());
268+
269+
// If keep_macs is true, expands to a MacEager::items instead.
270+
if cx.ecfg.keep_macs {
271+
MacEager::items(placeholders::reconstructed_macro_rules(&def).make_items())
272+
} else {
273+
MacEager::items(placeholders::macro_scope_placeholder().make_items())
274+
}
275+
}
276+
}
277+
245278
// Note that macro-by-example's input is also matched against a token tree:
246279
// $( $lhs:tt => $rhs:tt );+
247280
//

src/libsyntax_ext/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ pub mod deriving;
5050

5151
use std::rc::Rc;
5252
use syntax::ast;
53-
use syntax::ext::base::{MacroExpanderFn, MacroRulesTT, NormalTT, MultiModifier};
53+
use syntax::ext::base::{MacroExpanderFn, NormalTT, IdentTT, MultiModifier};
5454
use syntax::ext::hygiene::Mark;
55+
use syntax::ext::tt::macro_rules::MacroRulesExpander;
5556
use syntax::parse::token::intern;
5657

5758
pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, enable_quotes: bool) {
5859
let mut register = |name, ext| {
5960
resolver.add_macro(Mark::root(), ast::Ident::with_empty_ctxt(intern(name)), Rc::new(ext));
6061
};
6162

62-
register("macro_rules", MacroRulesTT);
63+
register("macro_rules", IdentTT(Box::new(MacroRulesExpander), None, false));
6364

6465
macro_rules! register {
6566
($( $name:ident: $f:expr, )*) => { $(

0 commit comments

Comments
 (0)