From 5bf385be6a5ce267ac7cd9d1725178488e33131c Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Thu, 18 Dec 2014 20:48:26 -0800 Subject: [PATCH] Rename macro_escape to macro_use In the future we want to support #[macro_use(foo, bar)] mod macros; but it's not an essential part of macro reform. Reserve the syntax for now. --- src/libcollections/lib.rs | 3 +- src/libcore/lib.rs | 12 +++-- src/libcoretest/num/mod.rs | 6 ++- src/liblog/lib.rs | 3 +- src/librustc_driver/driver.rs | 2 +- src/librustc_trans/trans/mod.rs | 3 +- src/librustdoc/lib.rs | 3 +- src/libstd/io/mod.rs | 3 +- src/libstd/lib.rs | 21 ++++++--- src/libstd/rt/mod.rs | 2 + src/libstd/thread_local/mod.rs | 3 +- src/libsyntax/ext/expand.rs | 45 +++++++++++-------- src/libsyntax/parse/mod.rs | 3 +- .../module-macro_use-arguments.rs | 16 +++++++ src/test/run-pass/cfg-macros-foo.rs | 4 +- src/test/run-pass/cfg-macros-notfoo.rs | 4 +- .../run-pass/deprecated-macro_escape-inner.rs | 19 ++++++++ src/test/run-pass/deprecated-macro_escape.rs | 18 ++++++++ 18 files changed, 127 insertions(+), 43 deletions(-) create mode 100644 src/test/compile-fail/module-macro_use-arguments.rs create mode 100644 src/test/run-pass/deprecated-macro_escape-inner.rs create mode 100644 src/test/run-pass/deprecated-macro_escape.rs diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 9214ec7e65b..142ac6f34e0 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -54,7 +54,8 @@ pub use vec_map::VecMap; // Needed for the vec! macro pub use alloc::boxed; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod macros; pub mod binary_heap; diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 588421dfa10..aff0065c527 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -62,19 +62,23 @@ #![feature(default_type_params, unboxed_closures, associated_types)] #![deny(missing_docs)] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod macros; #[path = "num/float_macros.rs"] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod float_macros; #[path = "num/int_macros.rs"] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod int_macros; #[path = "num/uint_macros.rs"] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod uint_macros; #[path = "num/int.rs"] pub mod int; diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index 01868675c76..f86c85f8216 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -14,7 +14,8 @@ use core::num::{NumCast, cast}; use core::ops::{Add, Sub, Mul, Div, Rem}; use core::kinds::Copy; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod int_macros; mod i8; @@ -23,7 +24,8 @@ mod i32; mod i64; mod int; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod uint_macros; mod u8; diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 61523c6fd0f..c210873563c 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -183,7 +183,8 @@ use regex::Regex; use directive::LOG_LEVEL_NAMES; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod macros; mod directive; diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 5056ada6a8c..027af6619ab 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -182,7 +182,7 @@ pub fn phase_2_configure_and_expand(sess: &Session, // strip before expansion to allow macros to depend on // configuration variables e.g/ in // - // #[macro_escape] #[cfg(foo)] + // #[macro_use] #[cfg(foo)] // mod bar { macro_rules! baz!(() => {{}}) } // // baz! should not use this definition unless foo is enabled. diff --git a/src/librustc_trans/trans/mod.rs b/src/librustc_trans/trans/mod.rs index 9b7f282f8bb..fa9cd5a698b 100644 --- a/src/librustc_trans/trans/mod.rs +++ b/src/librustc_trans/trans/mod.rs @@ -16,7 +16,8 @@ pub use self::base::trans_crate; pub use self::context::CrateContext; pub use self::common::gensym_name; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod macros; mod doc; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index a454760c8b5..319eee87317 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -49,7 +49,8 @@ use rustc::session::search_paths::SearchPaths; // reexported from `clean` so it can be easily updated with the mod itself pub use clean::SCHEMA_VERSION; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod externalfiles; pub mod clean; diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index bf373a145e4..e9386c30a6d 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -285,7 +285,8 @@ pub mod stdio; pub mod timer; pub mod util; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod test; /// The default buffer size for various I/O operations diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 5ffd3ebc7ad..abe968849c2 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -173,14 +173,17 @@ pub use unicode::char; /* Exported macros */ #[cfg(stage0)] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod macros_stage0; #[cfg(not(stage0))] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod macros; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod bitflags; mod rtdeps; @@ -193,15 +196,18 @@ pub mod prelude; /* Primitive types */ #[path = "num/float_macros.rs"] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod float_macros; #[path = "num/int_macros.rs"] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod int_macros; #[path = "num/uint_macros.rs"] -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod uint_macros; #[path = "num/int.rs"] pub mod int; @@ -229,7 +235,8 @@ pub mod num; /* Runtime and platform support */ -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod thread_local; pub mod c_str; diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs index 2b0639c5705..e556888a470 100644 --- a/src/libstd/rt/mod.rs +++ b/src/libstd/rt/mod.rs @@ -39,6 +39,8 @@ pub use alloc::heap; pub mod backtrace; // Internals +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] mod macros; // These should be refactored/moved/made private over time diff --git a/src/libstd/thread_local/mod.rs b/src/libstd/thread_local/mod.rs index 1ed01c034b5..e0cbaa8ca50 100644 --- a/src/libstd/thread_local/mod.rs +++ b/src/libstd/thread_local/mod.rs @@ -40,7 +40,8 @@ use prelude::v1::*; use cell::UnsafeCell; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod scoped; // Sure wish we had macro hygiene, no? diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index d4be46d025e..13cbc83f730 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -440,9 +440,9 @@ pub fn expand_item(it: P, fld: &mut MacroExpander) if valid_ident { fld.cx.mod_push(it.ident); } - let macro_escape = contains_macro_escape(new_attrs[]); + let macro_use = contains_macro_use(fld, new_attrs[]); let result = with_exts_frame!(fld.cx.syntax_env, - macro_escape, + macro_use, noop_fold_item(it, fld)); if valid_ident { fld.cx.mod_pop(); @@ -522,9 +522,28 @@ fn expand_item_underscore(item: ast::Item_, fld: &mut MacroExpander) -> ast::Ite } } -// does this attribute list contain "macro_escape" ? -fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool { - attr::contains_name(attrs, "macro_escape") +// does this attribute list contain "macro_use" ? +fn contains_macro_use(fld: &mut MacroExpander, attrs: &[ast::Attribute]) -> bool { + for attr in attrs.iter() { + let mut is_use = attr.check_name("macro_use"); + if attr.check_name("macro_escape") { + fld.cx.span_warn(attr.span, "macro_escape is a deprecated synonym for macro_use"); + is_use = true; + if let ast::AttrInner = attr.node.style { + fld.cx.span_help(attr.span, "consider an outer attribute, \ + #[macro_use] mod ..."); + } + }; + + if is_use { + match attr.node.value.node { + ast::MetaWord(..) => (), + _ => fld.cx.span_err(attr.span, "arguments to macro_use are not allowed here"), + } + return true; + } + } + false } // Support for item-position macro invocations, exactly the same @@ -1299,7 +1318,7 @@ impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> { #[cfg(test)] mod test { - use super::{pattern_bindings, expand_crate, contains_macro_escape}; + use super::{pattern_bindings, expand_crate, contains_macro_use}; use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer, ExpansionConfig}; use ast; use ast::{Attribute_, AttrOuter, MetaWord, Name}; @@ -1396,9 +1415,9 @@ mod test { expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast); } - // macro_escape modules should allow macros to escape + // macro_use modules should allow macros to escape #[test] fn macros_can_escape_flattened_mods_test () { - let src = "#[macro_escape] mod foo {macro_rules! z (() => (3+4));}\ + let src = "#[macro_use] mod foo {macro_rules! z (() => (3+4));}\ fn inty() -> int { z!() }".to_string(); let sess = parse::new_parse_sess(); let crate_ast = parse::parse_crate_from_source_str( @@ -1408,16 +1427,6 @@ mod test { expand_crate(&sess, test_ecfg(), vec!(), vec!(), crate_ast); } - #[test] fn test_contains_flatten (){ - let attr1 = make_dummy_attr ("foo"); - let attr2 = make_dummy_attr ("bar"); - let escape_attr = make_dummy_attr ("macro_escape"); - let attrs1 = vec!(attr1.clone(), escape_attr, attr2.clone()); - assert_eq!(contains_macro_escape(attrs1[]),true); - let attrs2 = vec!(attr1,attr2); - assert_eq!(contains_macro_escape(attrs2[]),false); - } - // make a MetaWord outer attribute with the given name fn make_dummy_attr(s: &str) -> ast::Attribute { Spanned { diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 05ed535ee36..ee7edceaf69 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -24,7 +24,8 @@ use std::num::Int; use std::str; use std::iter; -#[macro_escape] +#[cfg_attr(stage0, macro_escape)] +#[cfg_attr(not(stage0), macro_use)] pub mod parser; pub mod lexer; diff --git a/src/test/compile-fail/module-macro_use-arguments.rs b/src/test/compile-fail/module-macro_use-arguments.rs new file mode 100644 index 00000000000..6d3038b4820 --- /dev/null +++ b/src/test/compile-fail/module-macro_use-arguments.rs @@ -0,0 +1,16 @@ +// Copyright 2013-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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[macro_use(foo, bar)] //~ ERROR arguments to macro_use are not allowed here +mod foo { +} + +fn main() { +} diff --git a/src/test/run-pass/cfg-macros-foo.rs b/src/test/run-pass/cfg-macros-foo.rs index ec9ef381501..548057e9e60 100644 --- a/src/test/run-pass/cfg-macros-foo.rs +++ b/src/test/run-pass/cfg-macros-foo.rs @@ -16,7 +16,7 @@ #![feature(macro_rules)] #[cfg(foo)] -#[macro_escape] +#[macro_use] mod foo { macro_rules! bar { () => { true } @@ -24,7 +24,7 @@ mod foo { } #[cfg(not(foo))] -#[macro_escape] +#[macro_use] mod foo { macro_rules! bar { () => { false } diff --git a/src/test/run-pass/cfg-macros-notfoo.rs b/src/test/run-pass/cfg-macros-notfoo.rs index fb44176ec22..bf4f7e6bc40 100644 --- a/src/test/run-pass/cfg-macros-notfoo.rs +++ b/src/test/run-pass/cfg-macros-notfoo.rs @@ -16,7 +16,7 @@ #![feature(macro_rules)] #[cfg(foo)] -#[macro_escape] +#[macro_use] mod foo { macro_rules! bar { () => { true } @@ -24,7 +24,7 @@ mod foo { } #[cfg(not(foo))] -#[macro_escape] +#[macro_use] mod foo { macro_rules! bar { () => { false } diff --git a/src/test/run-pass/deprecated-macro_escape-inner.rs b/src/test/run-pass/deprecated-macro_escape-inner.rs new file mode 100644 index 00000000000..7960a91bdc4 --- /dev/null +++ b/src/test/run-pass/deprecated-macro_escape-inner.rs @@ -0,0 +1,19 @@ +// Copyright 2013-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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// ignore-pretty + +mod foo { + #![macro_escape] //~ WARNING macro_escape is a deprecated synonym for macro_use + //~^ HELP consider an outer attribute +} + +fn main() { +} diff --git a/src/test/run-pass/deprecated-macro_escape.rs b/src/test/run-pass/deprecated-macro_escape.rs new file mode 100644 index 00000000000..b03905e1a0d --- /dev/null +++ b/src/test/run-pass/deprecated-macro_escape.rs @@ -0,0 +1,18 @@ +// Copyright 2013-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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// ignore-pretty + +#[macro_escape] //~ WARNING macro_escape is a deprecated synonym for macro_use +mod foo { +} + +fn main() { +}