Skip to content

Commit 209c701

Browse files
committed
std: Stablize the macros module
This commit performs a pass over the `std::macros` module, applying stability attributes where necessary. In particular, this audits macros for patterns such as: * Standard use of forward-to-format-args via `$($arg:tt)*` (or `+`) * Prevent macro-defined identifiers from leaking into expression arguments as hygiene is not perfectly implemented. * Wherever possible, `$crate` is used now. Specifically, the following actions were taken: * The `std::macros` module itself is no longer public. * The `panic!` macro is stable * The `assert!` macro is stable * The `assert_eq!` macro is stable * The `debug_assert!` macro is stable * The `debug_assert_eq!` macro is stable * The `unreachable!` macro is stable after removing the extra forms to bring the definition in line with the `unimplemented!` macro. * The `try!` macro is stable * The `vec!` macro is stable [breaking-change]
1 parent 9e4e524 commit 209c701

File tree

9 files changed

+33
-224
lines changed

9 files changed

+33
-224
lines changed

src/libcollections/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/// Creates a `Vec` containing the arguments.
1212
#[macro_export]
13+
#[stable]
1314
macro_rules! vec {
1415
($($x:expr),*) => ({
1516
let xs: $crate::boxed::Box<[_]> = box [$($x),*];

src/libstd/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@
120120
extern crate log;
121121

122122
#[macro_use]
123-
#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
124-
unreachable, unimplemented, write, writeln)]
123+
#[macro_reexport(write, writeln)]
125124
extern crate core;
126125

127126
#[macro_use]
@@ -176,7 +175,7 @@ pub use unicode::char;
176175
/* Exported macros */
177176

178177
#[macro_use]
179-
pub mod macros;
178+
mod macros;
180179

181180
#[macro_use]
182181
pub mod bitflags;

src/libstd/macros.rs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,27 @@
3636
/// panic!("this is a {} {message}", "fancy", message = "message");
3737
/// ```
3838
#[macro_export]
39+
#[stable]
3940
macro_rules! panic {
4041
() => ({
4142
panic!("explicit panic")
4243
});
4344
($msg:expr) => ({
44-
// static requires less code at runtime, more constant data
45-
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
46-
::std::rt::begin_unwind($msg, &_FILE_LINE)
45+
$crate::rt::begin_unwind($msg, {
46+
// static requires less code at runtime, more constant data
47+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
48+
&_FILE_LINE
49+
})
4750
});
48-
($fmt:expr, $($arg:tt)*) => ({
49-
// The leading _'s are to avoid dead code warnings if this is
50-
// used inside a dead function. Just `#[allow(dead_code)]` is
51-
// insufficient, since the user may have
52-
// `#[forbid(dead_code)]` and which cannot be overridden.
53-
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
54-
::std::rt::begin_unwind_fmt(format_args!($fmt, $($arg)*), &_FILE_LINE)
55-
51+
($fmt:expr, $($arg:tt)+) => ({
52+
$crate::rt::begin_unwind_fmt(format_args!($fmt, $($arg)+), {
53+
// The leading _'s are to avoid dead code warnings if this is
54+
// used inside a dead function. Just `#[allow(dead_code)]` is
55+
// insufficient, since the user may have
56+
// `#[forbid(dead_code)]` and which cannot be overridden.
57+
static _FILE_LINE: (&'static str, uint) = (file!(), line!());
58+
&_FILE_LINE
59+
})
5660
});
5761
}
5862

@@ -77,15 +81,16 @@ macro_rules! panic {
7781
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
7882
/// ```
7983
#[macro_export]
84+
#[stable]
8085
macro_rules! assert {
8186
($cond:expr) => (
8287
if !$cond {
8388
panic!(concat!("assertion failed: ", stringify!($cond)))
8489
}
8590
);
86-
($cond:expr, $($arg:expr),+) => (
91+
($cond:expr, $($arg:tt)+) => (
8792
if !$cond {
88-
panic!($($arg),+)
93+
panic!($($arg)+)
8994
}
9095
);
9196
}
@@ -103,6 +108,7 @@ macro_rules! assert {
103108
/// assert_eq!(a, b);
104109
/// ```
105110
#[macro_export]
111+
#[stable]
106112
macro_rules! assert_eq {
107113
($left:expr , $right:expr) => ({
108114
match (&($left), &($right)) {
@@ -144,6 +150,7 @@ macro_rules! assert_eq {
144150
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
145151
/// ```
146152
#[macro_export]
153+
#[stable]
147154
macro_rules! debug_assert {
148155
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
149156
}
@@ -210,6 +217,7 @@ macro_rules! debug_assert_eq {
210217
/// }
211218
/// ```
212219
#[macro_export]
220+
#[unstable = "relationship with panic is unclear"]
213221
macro_rules! unreachable {
214222
() => ({
215223
panic!("internal error: entered unreachable code")
@@ -225,6 +233,7 @@ macro_rules! unreachable {
225233
/// A standardised placeholder for marking unfinished code. It panics with the
226234
/// message `"not yet implemented"` when executed.
227235
#[macro_export]
236+
#[unstable = "relationship with panic is unclear"]
228237
macro_rules! unimplemented {
229238
() => (panic!("not yet implemented"))
230239
}
@@ -242,15 +251,15 @@ macro_rules! unimplemented {
242251
#[macro_export]
243252
#[stable]
244253
macro_rules! format {
245-
($($arg:tt)*) => (::std::fmt::format(format_args!($($arg)*)))
254+
($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
246255
}
247256

248257
/// Equivalent to the `println!` macro except that a newline is not printed at
249258
/// the end of the message.
250259
#[macro_export]
251260
#[stable]
252261
macro_rules! print {
253-
($($arg:tt)*) => (::std::io::stdio::print_args(format_args!($($arg)*)))
262+
($($arg:tt)*) => ($crate::io::stdio::print_args(format_args!($($arg)*)))
254263
}
255264

256265
/// Macro for printing to a task's stdout handle.
@@ -268,20 +277,19 @@ macro_rules! print {
268277
#[macro_export]
269278
#[stable]
270279
macro_rules! println {
271-
($($arg:tt)*) => (::std::io::stdio::println_args(format_args!($($arg)*)))
280+
($($arg:tt)*) => ($crate::io::stdio::println_args(format_args!($($arg)*)))
272281
}
273282

274283
/// Helper macro for unwrapping `Result` values while returning early with an
275284
/// error if the value of the expression is `Err`. For more information, see
276285
/// `std::io`.
277286
#[macro_export]
287+
#[stable]
278288
macro_rules! try {
279-
($expr:expr) => ({
280-
use $crate::result::Result::{Ok, Err};
281-
282-
match $expr {
283-
Ok(val) => val,
284-
Err(err) => return Err($crate::error::FromError::from_error(err)),
289+
($expr:expr) => (match $expr {
290+
$crate::result::Result::Ok(val) => val,
291+
$crate::result::Result::Err(err) => {
292+
return $crate::result::Result::Err($crate::error::FromError::from_error(err))
285293
}
286294
})
287295
}
@@ -412,26 +420,6 @@ pub mod builtin {
412420
#[macro_export]
413421
macro_rules! option_env { ($name:expr) => ({ /* compiler built-in */ }) }
414422

415-
/// Concatenate literals into a static byte slice.
416-
///
417-
/// This macro takes any number of comma-separated literal expressions,
418-
/// yielding an expression of type `&'static [u8]` which is the
419-
/// concatenation (left to right) of all the literals in their byte format.
420-
///
421-
/// This extension currently only supports string literals, character
422-
/// literals, and integers less than 256. The byte slice returned is the
423-
/// utf8-encoding of strings and characters.
424-
///
425-
/// # Example
426-
///
427-
/// ```
428-
/// let rust = bytes!("r", 'u', "st", 255);
429-
/// assert_eq!(rust[1], b'u');
430-
/// assert_eq!(rust[4], 255);
431-
/// ```
432-
#[macro_export]
433-
macro_rules! bytes { ($($e:expr),*) => ({ /* compiler built-in */ }) }
434-
435423
/// Concatenate identifiers into one identifier.
436424
///
437425
/// This macro takes any number of comma-separated identifiers, and
@@ -565,10 +553,6 @@ pub mod builtin {
565553
#[macro_export]
566554
macro_rules! include_bytes { ($file:expr) => ({ /* compiler built-in */ }) }
567555

568-
/// Deprecated alias for `include_bytes!()`.
569-
#[macro_export]
570-
macro_rules! include_bin { ($file:expr) => ({ /* compiler built-in */}) }
571-
572556
/// Expands to a string that represents the current module path.
573557
///
574558
/// The current module path can be thought of as the hierarchy of modules

src/libsyntax/ext/base.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,6 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
341341

342342
let mut syntax_expanders = SyntaxEnv::new();
343343
syntax_expanders.insert(intern("macro_rules"), MacroRulesTT);
344-
syntax_expanders.insert(intern("fmt"),
345-
builtin_normal_expander(
346-
ext::fmt::expand_syntax_ext));
347344
syntax_expanders.insert(intern("format_args"),
348345
builtin_normal_expander(
349346
ext::format::expand_format_args));
@@ -353,9 +350,6 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
353350
syntax_expanders.insert(intern("option_env"),
354351
builtin_normal_expander(
355352
ext::env::expand_option_env));
356-
syntax_expanders.insert(intern("bytes"),
357-
builtin_normal_expander(
358-
ext::bytes::expand_syntax_ext));
359353
syntax_expanders.insert(intern("concat_idents"),
360354
builtin_normal_expander(
361355
ext::concat_idents::expand_syntax_ext));
@@ -367,8 +361,6 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
367361
ext::log_syntax::expand_syntax_ext));
368362
syntax_expanders.insert(intern("derive"),
369363
Decorator(box ext::deriving::expand_meta_derive));
370-
syntax_expanders.insert(intern("deriving"),
371-
Decorator(box ext::deriving::expand_meta_deriving));
372364

373365
if ecfg.enable_quotes {
374366
// Quasi-quoting expanders
@@ -416,9 +408,6 @@ fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
416408
syntax_expanders.insert(intern("include_str"),
417409
builtin_normal_expander(
418410
ext::source_util::expand_include_str));
419-
syntax_expanders.insert(intern("include_bin"),
420-
builtin_normal_expander(
421-
ext::source_util::expand_include_bin));
422411
syntax_expanders.insert(intern("include_bytes"),
423412
builtin_normal_expander(
424413
ext::source_util::expand_include_bytes));

src/libsyntax/ext/bytes.rs

Lines changed: 0 additions & 117 deletions
This file was deleted.

src/libsyntax/ext/deriving/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ pub mod totalord;
4040

4141
pub mod generic;
4242

43-
pub fn expand_meta_deriving(cx: &mut ExtCtxt,
44-
_span: Span,
45-
mitem: &MetaItem,
46-
item: &Item,
47-
push: Box<FnMut(P<Item>)>) {
48-
cx.span_warn(mitem.span, "`deriving` is deprecated; use `derive`");
49-
50-
expand_meta_derive(cx, _span, mitem, item, push)
51-
}
52-
5343
pub fn expand_meta_derive(cx: &mut ExtCtxt,
5444
_span: Span,
5545
mitem: &MetaItem,

0 commit comments

Comments
 (0)