Skip to content

Commit c9f8119

Browse files
committed
Refactor ext::base::Resolver::add_ext to only define macros in the crate root.
1 parent 797eb57 commit c9f8119

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/librustc_resolve/macros.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,28 @@ impl<'a> base::Resolver for Resolver<'a> {
7474
self.session.span_err(def.span, "user-defined macros may not be named `macro_rules`");
7575
}
7676
if def.use_locally {
77+
self.macro_names.insert(def.ident.name);
7778
let ext = macro_rules::compile(&self.session.parse_sess, &def);
78-
self.add_ext(scope, def.ident, Rc::new(ext));
79+
80+
let mut module = self.expansion_data[&scope].module;
81+
while module.macros_escape {
82+
module = module.parent.unwrap();
83+
}
84+
module.macros.borrow_mut().insert(def.ident.name, NameBinding {
85+
ext: Rc::new(ext),
86+
});
7987
}
8088
if def.export {
8189
def.id = self.next_node_id();
8290
self.exported_macros.push(def);
8391
}
8492
}
8593

86-
fn add_ext(&mut self, scope: Mark, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
94+
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>) {
8795
if let NormalTT(..) = *ext {
8896
self.macro_names.insert(ident.name);
8997
}
90-
91-
let mut module = self.expansion_data[&scope].module;
92-
while module.macros_escape {
93-
module = module.parent.unwrap();
94-
}
95-
module.macros.borrow_mut().insert(ident.name, NameBinding {
98+
self.graph_root.macros.borrow_mut().insert(ident.name, NameBinding {
9699
ext: ext,
97100
});
98101
}

src/libsyntax/ext/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ pub trait Resolver {
519519

520520
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion);
521521
fn add_macro(&mut self, scope: Mark, def: ast::MacroDef);
522-
fn add_ext(&mut self, scope: Mark, ident: ast::Ident, ext: Rc<SyntaxExtension>);
522+
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
523523
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
524524

525525
fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
@@ -535,7 +535,7 @@ impl Resolver for DummyResolver {
535535

536536
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {}
537537
fn add_macro(&mut self, _scope: Mark, _def: ast::MacroDef) {}
538-
fn add_ext(&mut self, _scope: Mark, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
538+
fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
539539
fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
540540

541541
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
@@ -749,7 +749,7 @@ impl<'a> ExtCtxt<'a> {
749749

750750
for (name, extension) in user_exts {
751751
let ident = ast::Ident::with_empty_ctxt(name);
752-
self.resolver.add_ext(Mark::root(), ident, Rc::new(extension));
752+
self.resolver.add_ext(ident, Rc::new(extension));
753753
}
754754

755755
let mut module = ModuleData {

src/libsyntax_ext/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ pub mod deriving;
5151
use std::rc::Rc;
5252
use syntax::ast;
5353
use syntax::ext::base::{MacroExpanderFn, NormalTT, IdentTT, MultiModifier};
54-
use syntax::ext::hygiene::Mark;
5554
use syntax::ext::tt::macro_rules::MacroRulesExpander;
5655
use syntax::parse::token::intern;
5756

5857
pub fn register_builtins(resolver: &mut syntax::ext::base::Resolver, enable_quotes: bool) {
5958
let mut register = |name, ext| {
60-
resolver.add_ext(Mark::root(), ast::Ident::with_empty_ctxt(intern(name)), Rc::new(ext));
59+
resolver.add_ext(ast::Ident::with_empty_ctxt(intern(name)), Rc::new(ext));
6160
};
6261

6362
register("macro_rules", IdentTT(Box::new(MacroRulesExpander), None, false));

0 commit comments

Comments
 (0)