Skip to content

Commit d4037fc

Browse files
authored
Auto merge of rust-lang#36444 - jseyfried:macro_rules_ext, r=nrc
Remove variant `MacroRulesTT` of `SyntaxExtension` r? @nrc
2 parents 55a61a1 + f9a08cc commit d4037fc

File tree

9 files changed

+63
-114
lines changed

9 files changed

+63
-114
lines changed

src/librustc_driver/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,11 +674,11 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
674674
}
675675
let features = sess.features.borrow();
676676
let cfg = syntax::ext::expand::ExpansionConfig {
677-
crate_name: crate_name.to_string(),
678677
features: Some(&features),
679678
recursion_limit: sess.recursion_limit.get(),
680679
trace_mac: sess.opts.debugging_opts.trace_macros,
681680
should_test: sess.opts.test,
681+
..syntax::ext::expand::ExpansionConfig::default(crate_name.to_string())
682682
};
683683
let mut ecx = ExtCtxt::new(&sess.parse_sess, krate.config.clone(), cfg, &mut resolver);
684684
let ret = syntax::ext::expand::expand_crate(&mut ecx, syntax_exts, krate);

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ pub trait IdentMacroExpander {
177177
cx: &'cx mut ExtCtxt,
178178
sp: Span,
179179
ident: ast::Ident,
180-
token_tree: Vec<tokenstream::TokenTree> )
180+
token_tree: Vec<tokenstream::TokenTree>,
181+
attrs: Vec<ast::Attribute>)
181182
-> Box<MacResult+'cx>;
182183
}
183184

@@ -193,7 +194,8 @@ impl<F> IdentMacroExpander for F
193194
cx: &'cx mut ExtCtxt,
194195
sp: Span,
195196
ident: ast::Ident,
196-
token_tree: Vec<tokenstream::TokenTree> )
197+
token_tree: Vec<tokenstream::TokenTree>,
198+
_attrs: Vec<ast::Attribute>)
197199
-> Box<MacResult+'cx>
198200
{
199201
(*self)(cx, sp, ident, token_tree)
@@ -455,9 +457,6 @@ pub enum SyntaxExtension {
455457
/// the block.
456458
///
457459
IdentTT(Box<IdentMacroExpander + 'static>, Option<Span>, bool),
458-
459-
/// Represents `macro_rules!` itself.
460-
MacroRulesTT,
461460
}
462461

463462
pub type NamedSyntaxExtension = (Name, SyntaxExtension);

src/libsyntax/ext/expand.rs

+8-51
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};
@@ -173,19 +173,12 @@ impl Invocation {
173173

174174
pub struct MacroExpander<'a, 'b:'a> {
175175
pub cx: &'a mut ExtCtxt<'b>,
176-
pub single_step: bool,
177-
pub keep_macs: bool,
178176
monotonic: bool, // c.f. `cx.monotonic_expander()`
179177
}
180178

181179
impl<'a, 'b> MacroExpander<'a, 'b> {
182180
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
183-
MacroExpander {
184-
cx: cx,
185-
monotonic: monotonic,
186-
single_step: false,
187-
keep_macs: false,
188-
}
181+
MacroExpander { cx: cx, monotonic: monotonic }
189182
}
190183

191184
fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
@@ -238,7 +231,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
238231
expansions.push(Vec::new());
239232
}
240233
expansions[depth].push((mark.as_u32(), expansion));
241-
if !self.single_step {
234+
if !self.cx.ecfg.single_step {
242235
invocations.extend(new_invocations.into_iter().rev());
243236
}
244237
}
@@ -381,47 +374,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
381374
}
382375
});
383376

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

427380
MultiDecorator(..) | MultiModifier(..) => {
@@ -726,6 +679,8 @@ pub struct ExpansionConfig<'feat> {
726679
pub recursion_limit: usize,
727680
pub trace_mac: bool,
728681
pub should_test: bool, // If false, strip `#[test]` nodes
682+
pub single_step: bool,
683+
pub keep_macs: bool,
729684
}
730685

731686
macro_rules! feature_tests {
@@ -749,6 +704,8 @@ impl<'feat> ExpansionConfig<'feat> {
749704
recursion_limit: 64,
750705
trace_mac: false,
751706
should_test: false,
707+
single_step: false,
708+
keep_macs: false,
752709
}
753710
}
754711

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, )*) => { $(

src/test/compile-fail-fulldeps/auxiliary/macro_crate_MacroRulesTT.rs

-26
This file was deleted.

src/test/compile-fail-fulldeps/plugin-MacroRulesTT.rs

-18
This file was deleted.

0 commit comments

Comments
 (0)