Skip to content

Commit 5382881

Browse files
author
Keegan McAllister
committed
Implement macro re-export
Fixes #17103.
1 parent e2a9c04 commit 5382881

File tree

10 files changed

+147
-1
lines changed

10 files changed

+147
-1
lines changed

src/librustc_driver/driver.rs

+2
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
275275
deriving_hash_type_parameter: sess.features.borrow().default_type_params,
276276
enable_quotes: sess.features.borrow().quote,
277277
recursion_limit: sess.recursion_limit.get(),
278+
reexported_macros: syntax::ext::tt::reexport::gather(sess.diagnostic(),
279+
&krate),
278280
};
279281
let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
280282
cfg,

src/libsyntax/ext/expand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,12 @@ pub fn expand_item_mac(it: P<ast::Item>,
616616
imported_from, tts);
617617

618618
fld.cx.syntax_env.insert(intern(name.as_slice()), ext);
619-
if attr::contains_name(it.attrs.as_slice(), "macro_export") {
619+
620+
if match imported_from {
621+
None => attr::contains_name(it.attrs.as_slice(), "macro_export"),
622+
Some(_) => fld.cx.ecfg.reexported_macros.iter()
623+
.any(|e| e.as_slice() == name.as_slice()),
624+
} {
620625
fld.cx.exported_macros.push(it);
621626
}
622627

@@ -1156,6 +1161,7 @@ pub struct ExpansionConfig {
11561161
pub deriving_hash_type_parameter: bool,
11571162
pub enable_quotes: bool,
11581163
pub recursion_limit: uint,
1164+
pub reexported_macros: Vec<String>,
11591165
}
11601166

11611167
impl ExpansionConfig {
@@ -1165,6 +1171,7 @@ impl ExpansionConfig {
11651171
deriving_hash_type_parameter: false,
11661172
enable_quotes: false,
11671173
recursion_limit: 64,
1174+
reexported_macros: vec![],
11681175
}
11691176
}
11701177
}

src/libsyntax/ext/tt/reexport.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Defines the crate attribute syntax for macro re-export.
12+
13+
use ast;
14+
use attr::AttrMetaMethods;
15+
use diagnostic::SpanHandler;
16+
17+
/// Return a vector of the names of all macros re-exported from the crate.
18+
pub fn gather(diag: &SpanHandler, krate: &ast::Crate) -> Vec<String> {
19+
let usage = "malformed macro_reexport attribute, expected \
20+
#![macro_reexport(ident, ident, ...)]";
21+
22+
let mut reexported: Vec<String> = vec!();
23+
for attr in krate.attrs.iter() {
24+
if !attr.check_name("macro_reexport") {
25+
continue;
26+
}
27+
28+
match attr.meta_item_list() {
29+
None => diag.span_err(attr.span, usage),
30+
Some(list) => for mi in list.iter() {
31+
match mi.node {
32+
ast::MetaWord(ref word)
33+
=> reexported.push(word.to_string()),
34+
_ => diag.span_err(mi.span, usage),
35+
}
36+
}
37+
}
38+
}
39+
40+
reexported
41+
}

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@ pub mod ext {
104104
pub mod transcribe;
105105
pub mod macro_parser;
106106
pub mod macro_rules;
107+
pub mod reexport;
107108
}
108109
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "dylib"]
12+
#![feature(macro_rules)]
13+
14+
#[macro_export]
15+
macro_rules! reexported {
16+
() => ( 3u )
17+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "dylib"]
12+
#![feature(phase)]
13+
14+
#![macro_reexport(reexported)]
15+
16+
#[phase(plugin)]
17+
extern crate macro_reexport_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![macro_reexport] //~ ERROR malformed macro_reexport attribute
12+
13+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![macro_reexport="foo"] //~ ERROR malformed macro_reexport attribute
12+
13+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![macro_reexport(foo="bar")] //~ ERROR malformed macro_reexport attribute
12+
13+
fn main() { }

src/test/run-pass/macro-reexport.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:macro_reexport_1.rs
12+
// aux-build:macro_reexport_2.rs
13+
// ignore-stage1
14+
15+
#![feature(phase)]
16+
17+
#[phase(plugin)]
18+
extern crate macro_reexport_2;
19+
20+
fn main() {
21+
assert_eq!(reexported!(), 3u);
22+
}

0 commit comments

Comments
 (0)