Skip to content

Commit 2c88b4b

Browse files
committed
Load macros from extern crates in the InvocationCollector fold.
1 parent d986bbe commit 2c88b4b

File tree

1 file changed

+22
-62
lines changed

1 file changed

+22
-62
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ use parse::token::{intern, keywords};
2525
use ptr::P;
2626
use tokenstream::TokenTree;
2727
use util::small_vector::SmallVector;
28-
use visit;
29-
use visit::Visitor;
3028

3129
use std::collections::HashMap;
3230
use std::mem;
@@ -35,8 +33,7 @@ use std::rc::Rc;
3533

3634
macro_rules! expansions {
3735
($($kind:ident: $ty:ty [$($vec:ident, $ty_elt:ty)*], $kind_name:expr, .$make:ident,
38-
$(.$fold:ident)* $(lift .$fold_elt:ident)*,
39-
$(.$visit:ident)* $(lift .$visit_elt:ident)*;)*) => {
36+
$(.$fold:ident)* $(lift .$fold_elt:ident)*;)*) => {
4037
#[derive(Copy, Clone)]
4138
pub enum ExpansionKind { OptExpr, $( $kind, )* }
4239
pub enum Expansion { OptExpr(Option<P<ast::Expr>>), $( $kind($ty), )* }
@@ -81,17 +78,6 @@ macro_rules! expansions {
8178
}, )*)*
8279
}
8380
}
84-
85-
fn visit_with<V: Visitor>(&self, visitor: &mut V) {
86-
match *self {
87-
Expansion::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
88-
$($( Expansion::$kind(ref ast) => visitor.$visit(ast), )*)*
89-
$($( Expansion::$kind(ref ast) => for ast in ast.as_slice() {
90-
visitor.$visit_elt(ast);
91-
}, )*)*
92-
_ => {}
93-
}
94-
}
9581
}
9682

9783
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
@@ -109,17 +95,17 @@ macro_rules! expansions {
10995
}
11096

11197
expansions! {
112-
Expr: P<ast::Expr> [], "expression", .make_expr, .fold_expr, .visit_expr;
113-
Pat: P<ast::Pat> [], "pattern", .make_pat, .fold_pat, .visit_pat;
114-
Ty: P<ast::Ty> [], "type", .make_ty, .fold_ty, .visit_ty;
98+
Expr: P<ast::Expr> [], "expression", .make_expr, .fold_expr;
99+
Pat: P<ast::Pat> [], "pattern", .make_pat, .fold_pat;
100+
Ty: P<ast::Ty> [], "type", .make_ty, .fold_ty;
115101
Stmts: SmallVector<ast::Stmt> [SmallVector, ast::Stmt],
116-
"statement", .make_stmts, lift .fold_stmt, lift .visit_stmt;
102+
"statement", .make_stmts, lift .fold_stmt;
117103
Items: SmallVector<P<ast::Item>> [SmallVector, P<ast::Item>],
118-
"item", .make_items, lift .fold_item, lift .visit_item;
104+
"item", .make_items, lift .fold_item;
119105
TraitItems: SmallVector<ast::TraitItem> [SmallVector, ast::TraitItem],
120-
"trait item", .make_trait_items, lift .fold_trait_item, lift .visit_trait_item;
106+
"trait item", .make_trait_items, lift .fold_trait_item;
121107
ImplItems: SmallVector<ast::ImplItem> [SmallVector, ast::ImplItem],
122-
"impl item", .make_impl_items, lift .fold_impl_item, lift .visit_impl_item;
108+
"impl item", .make_impl_items, lift .fold_impl_item;
123109
}
124110

125111
impl ExpansionKind {
@@ -228,50 +214,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
228214
sess: self.cx.parse_sess,
229215
features: self.cx.ecfg.features,
230216
});
231-
self.load_macros(&expansion);
232217
let mut collector = InvocationCollector { cx: self.cx, invocations: Vec::new() };
233218
(expansion.fold_with(&mut collector), collector.invocations)
234219
}
235220

236-
fn load_macros(&mut self, node: &Expansion) {
237-
struct MacroLoadingVisitor<'a, 'b: 'a>{
238-
cx: &'a mut ExtCtxt<'b>,
239-
at_crate_root: bool,
240-
}
241-
242-
impl<'a, 'b> Visitor for MacroLoadingVisitor<'a, 'b> {
243-
fn visit_mac(&mut self, _: &ast::Mac) {}
244-
fn visit_item(&mut self, item: &ast::Item) {
245-
if let ast::ItemKind::ExternCrate(..) = item.node {
246-
// We need to error on `#[macro_use] extern crate` when it isn't at the
247-
// crate root, because `$crate` won't work properly.
248-
for def in self.cx.loader.load_crate(item, self.at_crate_root) {
249-
match def {
250-
LoadedMacro::Def(def) => self.cx.insert_macro(def),
251-
LoadedMacro::CustomDerive(name, ext) => {
252-
self.cx.insert_custom_derive(&name, ext, item.span);
253-
}
254-
}
255-
}
256-
} else {
257-
let at_crate_root = ::std::mem::replace(&mut self.at_crate_root, false);
258-
visit::walk_item(self, item);
259-
self.at_crate_root = at_crate_root;
260-
}
261-
}
262-
fn visit_block(&mut self, block: &ast::Block) {
263-
let at_crate_root = ::std::mem::replace(&mut self.at_crate_root, false);
264-
visit::walk_block(self, block);
265-
self.at_crate_root = at_crate_root;
266-
}
267-
}
268-
269-
node.visit_with(&mut MacroLoadingVisitor {
270-
at_crate_root: self.cx.syntax_env.is_crate_root(),
271-
cx: self.cx,
272-
});
273-
}
274-
275221
fn expand_invoc(&mut self, invoc: Invocation) -> Expansion {
276222
match invoc.kind {
277223
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc),
@@ -645,6 +591,20 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
645591
self.cx.syntax_env.current_module = module;
646592
result
647593
},
594+
ast::ItemKind::ExternCrate(..) => {
595+
// We need to error on `#[macro_use] extern crate` when it isn't at the
596+
// crate root, because `$crate` won't work properly.
597+
let is_crate_root = self.cx.syntax_env.is_crate_root();
598+
for def in self.cx.loader.load_crate(&*item, is_crate_root) {
599+
match def {
600+
LoadedMacro::Def(def) => self.cx.insert_macro(def),
601+
LoadedMacro::CustomDerive(name, ext) => {
602+
self.cx.insert_custom_derive(&name, ext, item.span);
603+
}
604+
}
605+
}
606+
SmallVector::one(item)
607+
},
648608
_ => noop_fold_item(item, self),
649609
}
650610
}

0 commit comments

Comments
 (0)