]> git.lizzy.rs Git - rust.git/commitdiff
Implement macro re-export
authorKeegan McAllister <kmcallister@mozilla.com>
Tue, 16 Sep 2014 02:02:14 +0000 (19:02 -0700)
committerKeegan McAllister <kmcallister@mozilla.com>
Mon, 5 Jan 2015 19:38:12 +0000 (11:38 -0800)
Fixes #17103.

src/librustc_driver/driver.rs
src/libsyntax/ext/expand.rs
src/libsyntax/ext/tt/reexport.rs [new file with mode: 0644]
src/libsyntax/lib.rs
src/test/auxiliary/macro_reexport_1.rs [new file with mode: 0644]
src/test/auxiliary/macro_reexport_2.rs [new file with mode: 0644]
src/test/compile-fail/macro-reexport-malformed-1.rs [new file with mode: 0644]
src/test/compile-fail/macro-reexport-malformed-2.rs [new file with mode: 0644]
src/test/compile-fail/macro-reexport-malformed-3.rs [new file with mode: 0644]
src/test/run-pass/macro-reexport.rs [new file with mode: 0644]

index 0d9736a82736fb206477fc655c18d0bf1e6647ed..5056ada6a8c117d0fcd2e2b3f1905baec1f6e4c8 100644 (file)
@@ -275,6 +275,8 @@ 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,
index 325d8aa594a32409eca15ff124599954eaac746c..d4be46d025ecf8494605d78fcb7d812d7de46258 100644 (file)
@@ -616,7 +616,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);
                     }
 
@@ -1156,6 +1161,7 @@ pub struct ExpansionConfig {
     pub deriving_hash_type_parameter: bool,
     pub enable_quotes: bool,
     pub recursion_limit: uint,
+    pub reexported_macros: Vec<String>,
 }
 
 impl ExpansionConfig {
@@ -1165,6 +1171,7 @@ pub fn default(crate_name: String) -> ExpansionConfig {
             deriving_hash_type_parameter: false,
             enable_quotes: false,
             recursion_limit: 64,
+            reexported_macros: vec![],
         }
     }
 }
diff --git a/src/libsyntax/ext/tt/reexport.rs b/src/libsyntax/ext/tt/reexport.rs
new file mode 100644 (file)
index 0000000..104f378
--- /dev/null
@@ -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_reexport(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
+}
index 18cdb3fc6478949180903e8481d04506c0f1a269..0503d88cca201a8a2d79cc003cc8e187360f5b41 100644 (file)
@@ -104,5 +104,6 @@ pub mod tt {
         pub mod transcribe;
         pub mod macro_parser;
         pub mod macro_rules;
+        pub mod reexport;
     }
 }
diff --git a/src/test/auxiliary/macro_reexport_1.rs b/src/test/auxiliary/macro_reexport_1.rs
new file mode 100644 (file)
index 0000000..bd00b33
--- /dev/null
@@ -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(macro_rules)]
+
+#[macro_export]
+macro_rules! reexported {
+    () => ( 3u )
+}
diff --git a/src/test/auxiliary/macro_reexport_2.rs b/src/test/auxiliary/macro_reexport_2.rs
new file mode 100644 (file)
index 0000000..3b68d47
--- /dev/null
@@ -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;
diff --git a/src/test/compile-fail/macro-reexport-malformed-1.rs b/src/test/compile-fail/macro-reexport-malformed-1.rs
new file mode 100644 (file)
index 0000000..ea3074d
--- /dev/null
@@ -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() { }
diff --git a/src/test/compile-fail/macro-reexport-malformed-2.rs b/src/test/compile-fail/macro-reexport-malformed-2.rs
new file mode 100644 (file)
index 0000000..3daa089
--- /dev/null
@@ -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() { }
diff --git a/src/test/compile-fail/macro-reexport-malformed-3.rs b/src/test/compile-fail/macro-reexport-malformed-3.rs
new file mode 100644 (file)
index 0000000..b3c0bf9
--- /dev/null
@@ -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() { }
diff --git a/src/test/run-pass/macro-reexport.rs b/src/test/run-pass/macro-reexport.rs
new file mode 100644 (file)
index 0000000..bc3632e
--- /dev/null
@@ -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);
+}