Skip to content

Commit

Permalink
Move #[macro_reexport] to extern crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McAllister committed Jan 6, 2015
1 parent 60be2f5 commit 0816255
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 71 deletions.
1 change: 1 addition & 0 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl<'a> PluginMetadata<'a> {
id: ast::DUMMY_NODE_ID,
span: span,
imported_from: imported_from,
export: false, // overridden in plugin/load.rs
body: body,
});
true
Expand Down
28 changes: 27 additions & 1 deletion src/librustc/plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ use plugin::registry::Registry;
use std::mem;
use std::os;
use std::dynamic_lib::DynamicLibrary;
use std::collections::HashSet;
use syntax::ast;
use syntax::attr;
use syntax::parse::token;
use syntax::visit;
use syntax::visit::Visitor;
use syntax::attr::AttrMetaMethods;
Expand Down Expand Up @@ -87,6 +89,7 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
// Parse the attributes relating to macro / plugin loading.
let mut load_macros = false;
let mut load_registrar = false;
let mut reexport = HashSet::new();
for attr in vi.attrs.iter() {
let mut used = true;
match attr.name().get() {
Expand All @@ -96,6 +99,23 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
}
"plugin" => load_registrar = true,
"macro_use" => load_macros = true,
"macro_reexport" => {
let names = match attr.meta_item_list() {
Some(names) => names,
None => {
self.sess.span_err(attr.span, "bad macro reexport");
continue;
}
};

for name in names.iter() {
if let ast::MetaWord(ref name) = name.node {
reexport.insert(name.clone());
} else {
self.sess.span_err(name.span, "bad macro reexport");
}
}
}
_ => used = false,
}
if used {
Expand All @@ -116,7 +136,13 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
}
}

self.plugins.macros.extend(macros.into_iter());
for mut def in macros.into_iter() {
if reexport.contains(&token::get_ident(def.ident)) {
def.export = true;
}
self.plugins.macros.push(def);
}

if let Some((lib, symbol)) = registrar {
self.dylink_registrar(vi, lib, symbol);
}
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ pub fn phase_2_configure_and_expand(sess: &Session,
deriving_hash_type_parameter: sess.features.borrow().default_type_params,
enable_quotes: sess.features.borrow().quote,
recursion_limit: sess.recursion_limit.get(),
reexported_macros: syntax::ext::tt::reexport::gather(sess.diagnostic(),
&krate),
};
let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
cfg,
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@

#![reexport_test_harness_main = "test_main"]

#![macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
unreachable, unimplemented, write, writeln, vec)]

#[cfg(all(test, stage0))]
#[phase(plugin, link)]
extern crate log;
Expand All @@ -134,6 +131,8 @@ extern crate core;

#[cfg(not(stage0))]
#[macro_use]
#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
unreachable, unimplemented, write, writeln)]
extern crate core;

#[cfg(stage0)]
Expand All @@ -142,6 +141,7 @@ extern crate "collections" as core_collections;

#[cfg(not(stage0))]
#[macro_use]
#[macro_reexport(vec)]
extern crate "collections" as core_collections;

extern crate "rand" as core_rand;
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,7 @@ pub struct MacroDef {
pub id: NodeId,
pub span: Span,
pub imported_from: Option<Ident>,
pub export: bool,
pub body: Vec<TokenTree>,
}

Expand Down
10 changes: 10 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use codemap;
use codemap::{CodeMap, Span, ExpnId, ExpnInfo, NO_EXPANSION};
use ext;
use ext::expand;
use ext::tt::macro_rules;
use parse;
use parse::parser;
use parse::token;
Expand Down Expand Up @@ -568,6 +569,15 @@ impl<'a> ExtCtxt<'a> {
}
}
}

pub fn insert_macro(&mut self, def: ast::MacroDef) {
if def.export {
self.exported_macros.push(def.clone());
}
let ext = macro_rules::compile(self, &def);
self.syntax_env.insert(def.ident.name, ext);
}

/// Emit `msg` attached to `sp`, and stop compilation immediately.
///
/// `span_err` should be strongly preferred where-ever possible:
Expand Down
22 changes: 4 additions & 18 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use ast;
use ast_util::path_to_ident;
use ext::mtwt;
use ext::build::AstBuilder;
use ext::tt::macro_rules;
use attr;
use attr::AttrMetaMethods;
use codemap;
Expand Down Expand Up @@ -636,14 +635,10 @@ pub fn expand_item_mac(it: P<ast::Item>,
id: ast::DUMMY_NODE_ID,
span: it.span,
imported_from: None,
export: attr::contains_name(it.attrs.as_slice(), "macro_export"),
body: tts,
};
let ext = macro_rules::compile(fld.cx, &def);
fld.cx.syntax_env.insert(def.ident.name, ext);

if attr::contains_name(def.attrs.as_slice(), "macro_export") {
fld.cx.exported_macros.push(def);
}
fld.cx.insert_macro(def);

// macro_rules! has a side effect but expands to nothing.
fld.cx.bt_pop();
Expand Down Expand Up @@ -1178,7 +1173,6 @@ pub struct ExpansionConfig {
pub deriving_hash_type_parameter: bool,
pub enable_quotes: bool,
pub recursion_limit: uint,
pub reexported_macros: Vec<String>,
}

impl ExpansionConfig {
Expand All @@ -1188,7 +1182,6 @@ impl ExpansionConfig {
deriving_hash_type_parameter: false,
enable_quotes: false,
recursion_limit: 64,
reexported_macros: vec![],
}
}
}
Expand All @@ -1202,15 +1195,8 @@ pub fn expand_crate(parse_sess: &parse::ParseSess,
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
let mut expander = MacroExpander::new(&mut cx);

for def in imported_macros.iter() {
let ext = macro_rules::compile(expander.cx, def);
expander.cx.syntax_env.insert(def.ident.name, ext);

if expander.cx.ecfg.reexported_macros.iter()
.any(|e| e[] == token::get_ident(def.ident).get()) {

expander.cx.exported_macros.push(def.clone());
}
for def in imported_macros.into_iter() {
expander.cx.insert_macro(def);
}

for (name, extension) in user_exts.into_iter() {
Expand Down
41 changes: 0 additions & 41 deletions src/libsyntax/ext/tt/reexport.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,5 @@ pub mod ext {
pub mod transcribe;
pub mod macro_parser;
pub mod macro_rules;
pub mod reexport;
}
}
3 changes: 1 addition & 2 deletions src/test/auxiliary/macro_reexport_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#![crate_type = "dylib"]

#![macro_reexport(reexported)]

#[macro_reexport(reexported)]
#[macro_use] #[no_link]
extern crate macro_reexport_1;
3 changes: 2 additions & 1 deletion src/test/compile-fail/macro-reexport-malformed-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![macro_reexport] //~ ERROR malformed macro_reexport attribute
#[macro_reexport] //~ ERROR bad macro reexport
extern crate std;

fn main() { }
3 changes: 2 additions & 1 deletion src/test/compile-fail/macro-reexport-malformed-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![macro_reexport="foo"] //~ ERROR malformed macro_reexport attribute
#[macro_reexport="foo"] //~ ERROR bad macro reexport
extern crate std;

fn main() { }
3 changes: 2 additions & 1 deletion src/test/compile-fail/macro-reexport-malformed-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![macro_reexport(foo="bar")] //~ ERROR malformed macro_reexport attribute
#[macro_reexport(foo="bar")] //~ ERROR bad macro reexport
extern crate std;

fn main() { }

0 comments on commit 0816255

Please sign in to comment.