Skip to content

Commit

Permalink
Allow selective macro import
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McAllister committed Jan 6, 2015
1 parent 0816255 commit aa69cbd
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/librustc/metadata/creader.rs
Expand Up @@ -552,7 +552,10 @@ impl<'a> PluginMetadata<'a> {
id: ast::DUMMY_NODE_ID,
span: span,
imported_from: imported_from,
export: false, // overridden in plugin/load.rs
// overridden in plugin/load.rs
export: false,
use_locally: false,

body: body,
});
true
Expand Down
33 changes: 28 additions & 5 deletions src/librustc/plugin/load.rs
Expand Up @@ -87,8 +87,8 @@ 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 macro_selection = Some(HashSet::new()); // None => load all
let mut reexport = HashSet::new();
for attr in vi.attrs.iter() {
let mut used = true;
Expand All @@ -98,7 +98,22 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
#[macro_use], #[plugin], and/or #[no_link]");
}
"plugin" => load_registrar = true,
"macro_use" => load_macros = true,
"macro_use" => {
let names = attr.meta_item_list();
if names.is_none() {
// no names => load all
macro_selection = None;
}
if let (Some(sel), Some(names)) = (macro_selection.as_mut(), names) {
for name in names.iter() {
if let ast::MetaWord(ref name) = name.node {
sel.insert(name.clone());
} else {
self.sess.span_err(name.span, "bad macro import");
}
}
}
}
"macro_reexport" => {
let names = match attr.meta_item_list() {
Some(names) => names,
Expand Down Expand Up @@ -126,6 +141,11 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
let mut macros = vec![];
let mut registrar = None;

let load_macros = match macro_selection.as_ref() {
Some(sel) => sel.len() != 0 || reexport.len() != 0,
None => true,
};

if load_macros || load_registrar {
let pmd = self.reader.read_plugin_metadata(vi);
if load_macros {
Expand All @@ -137,9 +157,12 @@ impl<'a, 'v> Visitor<'v> for PluginLoader<'a> {
}

for mut def in macros.into_iter() {
if reexport.contains(&token::get_ident(def.ident)) {
def.export = true;
}
let name = token::get_ident(def.ident);
def.use_locally = match macro_selection.as_ref() {
None => true,
Some(sel) => sel.contains(&name),
};
def.export = reexport.contains(&name);
self.plugins.macros.push(def);
}

Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ast.rs
Expand Up @@ -1709,6 +1709,7 @@ pub struct MacroDef {
pub span: Span,
pub imported_from: Option<Ident>,
pub export: bool,
pub use_locally: bool,
pub body: Vec<TokenTree>,
}

Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax/ext/base.rs
Expand Up @@ -574,8 +574,10 @@ impl<'a> ExtCtxt<'a> {
if def.export {
self.exported_macros.push(def.clone());
}
let ext = macro_rules::compile(self, &def);
self.syntax_env.insert(def.ident.name, ext);
if def.use_locally {
let ext = macro_rules::compile(self, &def);
self.syntax_env.insert(def.ident.name, ext);
}
}

/// Emit `msg` attached to `sp`, and stop compilation immediately.
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -636,6 +636,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
span: it.span,
imported_from: None,
export: attr::contains_name(it.attrs.as_slice(), "macro_export"),
use_locally: true,
body: tts,
};
fld.cx.insert_macro(def);
Expand Down
15 changes: 15 additions & 0 deletions src/test/auxiliary/macro_reexport_2_no_use.rs
@@ -0,0 +1,15 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type = "dylib"]

#[macro_reexport(reexported)]
#[no_link]
extern crate macro_reexport_1;
19 changes: 19 additions & 0 deletions src/test/auxiliary/two_macros.rs
@@ -0,0 +1,19 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// force-host

#![feature(macro_rules)]

#[macro_export]
macro_rules! macro_one { () => ("one") }

#[macro_export]
macro_rules! macro_two { () => ("two") }
19 changes: 19 additions & 0 deletions src/test/compile-fail/empty-macro-use.rs
@@ -0,0 +1,19 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use()]
extern crate two_macros;

pub fn main() {
macro_two!(); //~ ERROR macro undefined
}
20 changes: 20 additions & 0 deletions src/test/compile-fail/macro-reexport-not-locally-visible.rs
@@ -0,0 +1,20 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:macro_reexport_1.rs
// ignore-stage1

#[macro_reexport(reexported)]
#[no_link]
extern crate macro_reexport_1;

fn main() {
assert_eq!(reexported!(), 3u); //~ ERROR macro undefined
}
15 changes: 15 additions & 0 deletions src/test/compile-fail/macro-use-bad-args-1.rs
@@ -0,0 +1,15 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use(foo(bar))] //~ ERROR bad macro import
extern crate std;

fn main() {
}
15 changes: 15 additions & 0 deletions src/test/compile-fail/macro-use-bad-args-2.rs
@@ -0,0 +1,15 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[macro_use(foo="bar")] //~ ERROR bad macro import
extern crate std;

fn main() {
}
19 changes: 19 additions & 0 deletions src/test/compile-fail/macro-use-wrong-name.rs
@@ -0,0 +1,19 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use(macro_one)]
extern crate two_macros;

pub fn main() {
macro_two!(); //~ ERROR macro undefined
}
18 changes: 18 additions & 0 deletions src/test/compile-fail/missing-macro-use.rs
@@ -0,0 +1,18 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

extern crate two_macros;

pub fn main() {
macro_two!(); //~ ERROR macro undefined
}
20 changes: 20 additions & 0 deletions src/test/run-pass/macro-reexport-no-intermediate-use.rs
@@ -0,0 +1,20 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:macro_reexport_1.rs
// aux-build:macro_reexport_2_no_use.rs
// ignore-stage1

#[macro_use] #[no_link]
extern crate macro_reexport_2_no_use;

fn main() {
assert_eq!(reexported!(), 3u);
}
21 changes: 21 additions & 0 deletions src/test/run-pass/macro-use-all-and-none.rs
@@ -0,0 +1,21 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use]
#[macro_use()]
extern crate two_macros;

pub fn main() {
macro_one!();
macro_two!();
}
20 changes: 20 additions & 0 deletions src/test/run-pass/macro-use-all.rs
@@ -0,0 +1,20 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use]
extern crate two_macros;

pub fn main() {
macro_one!();
macro_two!();
}
20 changes: 20 additions & 0 deletions src/test/run-pass/macro-use-both.rs
@@ -0,0 +1,20 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use(macro_one, macro_two)]
extern crate two_macros;

pub fn main() {
macro_one!();
macro_two!();
}
19 changes: 19 additions & 0 deletions src/test/run-pass/macro-use-one.rs
@@ -0,0 +1,19 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use(macro_two)]
extern crate two_macros;

pub fn main() {
macro_two!();
}
21 changes: 21 additions & 0 deletions src/test/run-pass/two-macro-use.rs
@@ -0,0 +1,21 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:two_macros.rs
// ignore-stage1

#[macro_use(macro_one)]
#[macro_use(macro_two)]
extern crate two_macros;

pub fn main() {
macro_one!();
macro_two!();
}

0 comments on commit aa69cbd

Please sign in to comment.