Skip to content

Commit c133b21

Browse files
committed
auto merge of #20776 : kmcallister/rust/macro-cleanup, r=alexcrichton
r? @alexcrichton. This passes tests for me.
2 parents 73a25f5 + a96a8b2 commit c133b21

File tree

4 files changed

+15
-183
lines changed

4 files changed

+15
-183
lines changed

src/doc/trpl/macros.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,18 @@ to print "I am never printed" and to run forever.
440440

441441
# Scoping and macro import/export
442442

443-
Macros occupy a single global namespace. The interaction with Rust's system of
444-
modules and crates is somewhat complex.
443+
Macros are expanded at an early stage in compilation, before name resolution.
444+
One downside is that scoping works differently for macros, compared to other
445+
constructs in the language.
445446

446447
Definition and expansion of macros both happen in a single depth-first,
447448
lexical-order traversal of a crate's source. So a macro defined at module scope
448449
is visible to any subsequent code in the same module, which includes the body
449450
of any subsequent child `mod` items.
450451

452+
A macro defined within the body of a single `fn`, or anywhere else not at
453+
module scope, is visible only within that item.
454+
451455
If a module has the `macro_use` attribute, its macros are also visible in its
452456
parent module after the child's `mod` item. If the parent also has `macro_use`
453457
then the macros will be visible in the grandparent after the parent's `mod`

src/libcore/macros.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ macro_rules! panic {
4949
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
5050
/// ```
5151
#[macro_export]
52+
#[stable]
5253
macro_rules! assert {
5354
($cond:expr) => (
5455
if !$cond {
5556
panic!(concat!("assertion failed: ", stringify!($cond)))
5657
}
5758
);
58-
($cond:expr, $($arg:expr),+) => (
59+
($cond:expr, $($arg:tt)+) => (
5960
if !$cond {
60-
panic!($($arg),+)
61+
panic!($($arg)+)
6162
}
6263
);
6364
}
@@ -75,6 +76,7 @@ macro_rules! assert {
7576
/// assert_eq!(a, b);
7677
/// ```
7778
#[macro_export]
79+
#[stable]
7880
macro_rules! assert_eq {
7981
($left:expr , $right:expr) => ({
8082
match (&($left), &($right)) {
@@ -116,6 +118,7 @@ macro_rules! assert_eq {
116118
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
117119
/// ```
118120
#[macro_export]
121+
#[stable]
119122
macro_rules! debug_assert {
120123
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
121124
}
@@ -227,6 +230,7 @@ macro_rules! writeln {
227230
/// }
228231
/// ```
229232
#[macro_export]
233+
#[unstable = "relationship with panic is unclear"]
230234
macro_rules! unreachable {
231235
() => ({
232236
panic!("internal error: entered unreachable code")
@@ -242,6 +246,7 @@ macro_rules! unreachable {
242246
/// A standardised placeholder for marking unfinished code. It panics with the
243247
/// message `"not yet implemented"` when executed.
244248
#[macro_export]
249+
#[unstable = "relationship with panic is unclear"]
245250
macro_rules! unimplemented {
246251
() => (panic!("not yet implemented"))
247252
}

src/libstd/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
extern crate log;
123123

124124
#[macro_use]
125-
#[macro_reexport(write, writeln)]
125+
#[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq,
126+
unreachable, unimplemented, write, writeln)]
126127
extern crate core;
127128

128129
#[macro_use]

src/libstd/macros.rs

Lines changed: 0 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -60,184 +60,6 @@ macro_rules! panic {
6060
});
6161
}
6262

63-
/// Ensure that a boolean expression is `true` at runtime.
64-
///
65-
/// This will invoke the `panic!` macro if the provided expression cannot be
66-
/// evaluated to `true` at runtime.
67-
///
68-
/// # Example
69-
///
70-
/// ```
71-
/// // the panic message for these assertions is the stringified value of the
72-
/// // expression given.
73-
/// assert!(true);
74-
/// # fn some_computation() -> bool { true }
75-
/// assert!(some_computation());
76-
///
77-
/// // assert with a custom message
78-
/// # let x = true;
79-
/// assert!(x, "x wasn't true!");
80-
/// # let a = 3i; let b = 27i;
81-
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
82-
/// ```
83-
#[macro_export]
84-
#[stable]
85-
macro_rules! assert {
86-
($cond:expr) => (
87-
if !$cond {
88-
panic!(concat!("assertion failed: ", stringify!($cond)))
89-
}
90-
);
91-
($cond:expr, $($arg:tt)+) => (
92-
if !$cond {
93-
panic!($($arg)+)
94-
}
95-
);
96-
}
97-
98-
/// Asserts that two expressions are equal to each other, testing equality in
99-
/// both directions.
100-
///
101-
/// On panic, this macro will print the values of the expressions.
102-
///
103-
/// # Example
104-
///
105-
/// ```
106-
/// let a = 3i;
107-
/// let b = 1i + 2i;
108-
/// assert_eq!(a, b);
109-
/// ```
110-
#[macro_export]
111-
#[stable]
112-
macro_rules! assert_eq {
113-
($left:expr , $right:expr) => ({
114-
match (&($left), &($right)) {
115-
(left_val, right_val) => {
116-
// check both directions of equality....
117-
if !((*left_val == *right_val) &&
118-
(*right_val == *left_val)) {
119-
panic!("assertion failed: `(left == right) && (right == left)` \
120-
(left: `{:?}`, right: `{:?}`)", *left_val, *right_val)
121-
}
122-
}
123-
}
124-
})
125-
}
126-
127-
/// Ensure that a boolean expression is `true` at runtime.
128-
///
129-
/// This will invoke the `panic!` macro if the provided expression cannot be
130-
/// evaluated to `true` at runtime.
131-
///
132-
/// Unlike `assert!`, `debug_assert!` statements can be disabled by passing
133-
/// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for
134-
/// checks that are too expensive to be present in a release build but may be
135-
/// helpful during development.
136-
///
137-
/// # Example
138-
///
139-
/// ```
140-
/// // the panic message for these assertions is the stringified value of the
141-
/// // expression given.
142-
/// debug_assert!(true);
143-
/// # fn some_expensive_computation() -> bool { true }
144-
/// debug_assert!(some_expensive_computation());
145-
///
146-
/// // assert with a custom message
147-
/// # let x = true;
148-
/// debug_assert!(x, "x wasn't true!");
149-
/// # let a = 3i; let b = 27i;
150-
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
151-
/// ```
152-
#[macro_export]
153-
#[stable]
154-
macro_rules! debug_assert {
155-
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
156-
}
157-
158-
/// Asserts that two expressions are equal to each other, testing equality in
159-
/// both directions.
160-
///
161-
/// On panic, this macro will print the values of the expressions.
162-
///
163-
/// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by
164-
/// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!`
165-
/// useful for checks that are too expensive to be present in a release build
166-
/// but may be helpful during development.
167-
///
168-
/// # Example
169-
///
170-
/// ```
171-
/// let a = 3i;
172-
/// let b = 1i + 2i;
173-
/// debug_assert_eq!(a, b);
174-
/// ```
175-
#[macro_export]
176-
macro_rules! debug_assert_eq {
177-
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); })
178-
}
179-
180-
/// A utility macro for indicating unreachable code.
181-
///
182-
/// This is useful any time that the compiler can't determine that some code is unreachable. For
183-
/// example:
184-
///
185-
/// * Match arms with guard conditions.
186-
/// * Loops that dynamically terminate.
187-
/// * Iterators that dynamically terminate.
188-
///
189-
/// # Panics
190-
///
191-
/// This will always panic.
192-
///
193-
/// # Examples
194-
///
195-
/// Match arms:
196-
///
197-
/// ```rust
198-
/// fn foo(x: Option<int>) {
199-
/// match x {
200-
/// Some(n) if n >= 0 => println!("Some(Non-negative)"),
201-
/// Some(n) if n < 0 => println!("Some(Negative)"),
202-
/// Some(_) => unreachable!(), // compile error if commented out
203-
/// None => println!("None")
204-
/// }
205-
/// }
206-
/// ```
207-
///
208-
/// Iterators:
209-
///
210-
/// ```rust
211-
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
212-
/// for i in std::iter::count(0_u32, 1) {
213-
/// if 3*i < i { panic!("u32 overflow"); }
214-
/// if x < 3*i { return i-1; }
215-
/// }
216-
/// unreachable!();
217-
/// }
218-
/// ```
219-
#[macro_export]
220-
#[unstable = "relationship with panic is unclear"]
221-
macro_rules! unreachable {
222-
() => ({
223-
panic!("internal error: entered unreachable code")
224-
});
225-
($msg:expr) => ({
226-
unreachable!("{}", $msg)
227-
});
228-
($fmt:expr, $($arg:tt)*) => ({
229-
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
230-
});
231-
}
232-
233-
/// A standardised placeholder for marking unfinished code. It panics with the
234-
/// message `"not yet implemented"` when executed.
235-
#[macro_export]
236-
#[unstable = "relationship with panic is unclear"]
237-
macro_rules! unimplemented {
238-
() => (panic!("not yet implemented"))
239-
}
240-
24163
/// Use the syntax described in `std::fmt` to create a value of type `String`.
24264
/// See `std::fmt` for more information.
24365
///

0 commit comments

Comments
 (0)