Skip to content

Commit

Permalink
Implement macro re-export
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcallister committed Sep 16, 2014
1 parent ef36808 commit 99cc9cf
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
let cfg = syntax::ext::expand::ExpansionConfig {
deriving_hash_type_parameter: sess.features.default_type_params.get(),
crate_name: crate_name.to_string(),
reexported_macros: syntax::ext::tt::reexport::gather(sess.diagnostic(),
&krate),
};
let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
cfg,
Expand Down
1 change: 1 addition & 0 deletions src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ fn generate_test_harness(sess: &Session,
ExpansionConfig {
deriving_hash_type_parameter: false,
crate_name: "test".to_string(),
reexported_macros: vec!(),
}),
path: Vec::new(),
testfns: Vec::new(),
Expand Down
9 changes: 8 additions & 1 deletion src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,12 @@ pub fn expand_item_mac(it: P<ast::Item>,
imported_from, tts);

fld.cx.syntax_env.insert(intern(name.as_slice()), ext);
if attr::contains_name(it.attrs.as_slice(), "macro_export") {

if match imported_from {
None => attr::contains_name(it.attrs.as_slice(), "macro_export"),
Some(_) => fld.cx.ecfg.reexported_macros.iter()
.any(|e| e.as_slice() == name.as_slice()),
} {
fld.cx.exported_macros.push(it);
}

Expand Down Expand Up @@ -968,6 +973,7 @@ fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
pub struct ExpansionConfig {
pub deriving_hash_type_parameter: bool,
pub crate_name: String,
pub reexported_macros: Vec<String>,
}

pub struct ExportedMacros {
Expand Down Expand Up @@ -1181,6 +1187,7 @@ mod test {
ExpansionConfig {
deriving_hash_type_parameter: false,
crate_name: "test".to_string(),
reexported_macros: vec!(),
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/libsyntax/ext/tt/reexport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2014 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.

//! Defines the crate attribute syntax for macro re-export.
use ast;
use attr::AttrMetaMethods;
use diagnostic::SpanHandler;

/// Return a vector of the names of all macros re-exported from the crate.
pub fn gather(diag: &SpanHandler, krate: &ast::Crate) -> Vec<String> {
let usage = "malformed macro_reexport attribute, expected \
#![macro_export(ident, ident, ...)]";

let mut reexported: Vec<String> = vec!();
for attr in krate.attrs.iter() {
if !attr.check_name("macro_reexport") {
continue;
}

match attr.meta_item_list() {
None => diag.span_err(attr.span, usage),
Some(list) => for mi in list.iter() {
match mi.node {
ast::MetaWord(ref word)
=> reexported.push(word.to_string()),
_ => diag.span_err(mi.span, usage),
}
}
}
}

reexported
}
1 change: 1 addition & 0 deletions src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ pub mod ext {
pub mod transcribe;
pub mod macro_parser;
pub mod macro_rules;
pub mod reexport;
}
}
15 changes: 15 additions & 0 deletions src/test/auxiliary/macro_reexport_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2014 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"]
#![feature(macro_rules)]

#[macro_export]
macro_rules! reexported ( () => ( 3u ) )
17 changes: 17 additions & 0 deletions src/test/auxiliary/macro_reexport_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 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"]
#![feature(phase)]

#![macro_reexport(reexported)]

#[phase(plugin)]
extern crate macro_reexport_1;
13 changes: 13 additions & 0 deletions src/test/compile-fail/macro-reexport-malformed-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2014 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_reexport] //~ ERROR malformed macro_reexport attribute

fn main() { }
13 changes: 13 additions & 0 deletions src/test/compile-fail/macro-reexport-malformed-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2014 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_reexport="foo"] //~ ERROR malformed macro_reexport attribute

fn main() { }
13 changes: 13 additions & 0 deletions src/test/compile-fail/macro-reexport-malformed-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2014 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_reexport(foo="bar")] //~ ERROR malformed macro_reexport attribute

fn main() { }
22 changes: 22 additions & 0 deletions src/test/run-pass/macro-reexport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2014 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.rs
// ignore-stage1

#![feature(phase)]

#[phase(plugin)]
extern crate macro_reexport_2;

fn main() {
assert_eq!(reexported!(), 3u);
}

0 comments on commit 99cc9cf

Please sign in to comment.