Skip to content

Commit dc4e048

Browse files
committed
---
yaml --- r: 172271 b: refs/heads/master c: 9cc847d h: refs/heads/master i: 172269: f26d35b 172267: 5bb3f23 172263: e1b13c4 172255: 292b9e4 v: v3
1 parent ab84577 commit dc4e048

File tree

8 files changed

+208
-18
lines changed

8 files changed

+208
-18
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: c133b2110b79d7a735032e4af431ee3a1143f3ce
2+
refs/heads/master: 9cc847d8c3cb156a2765a6be962209acd472b8aa
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 5b3cd3900ceda838f5798c30ab96ceb41f962534
55
refs/heads/try: 705b92bdfe33d0d6febdf945340262514e1b3b5c

trunk/src/doc/trpl/macros.md

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

441441
# Scoping and macro import/export
442442

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.
443+
Macros occupy a single global namespace. The interaction with Rust's system of
444+
modules and crates is somewhat complex.
446445

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

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-
455451
If a module has the `macro_use` attribute, its macros are also visible in its
456452
parent module after the child's `mod` item. If the parent also has `macro_use`
457453
then the macros will be visible in the grandparent after the parent's `mod`

trunk/src/libcore/macros.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,15 @@ macro_rules! panic {
4949
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
5050
/// ```
5151
#[macro_export]
52-
#[stable]
5352
macro_rules! assert {
5453
($cond:expr) => (
5554
if !$cond {
5655
panic!(concat!("assertion failed: ", stringify!($cond)))
5756
}
5857
);
59-
($cond:expr, $($arg:tt)+) => (
58+
($cond:expr, $($arg:expr),+) => (
6059
if !$cond {
61-
panic!($($arg)+)
60+
panic!($($arg),+)
6261
}
6362
);
6463
}
@@ -76,7 +75,6 @@ macro_rules! assert {
7675
/// assert_eq!(a, b);
7776
/// ```
7877
#[macro_export]
79-
#[stable]
8078
macro_rules! assert_eq {
8179
($left:expr , $right:expr) => ({
8280
match (&($left), &($right)) {
@@ -118,7 +116,6 @@ macro_rules! assert_eq {
118116
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
119117
/// ```
120118
#[macro_export]
121-
#[stable]
122119
macro_rules! debug_assert {
123120
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
124121
}
@@ -230,7 +227,6 @@ macro_rules! writeln {
230227
/// }
231228
/// ```
232229
#[macro_export]
233-
#[unstable = "relationship with panic is unclear"]
234230
macro_rules! unreachable {
235231
() => ({
236232
panic!("internal error: entered unreachable code")
@@ -246,7 +242,6 @@ macro_rules! unreachable {
246242
/// A standardised placeholder for marking unfinished code. It panics with the
247243
/// message `"not yet implemented"` when executed.
248244
#[macro_export]
249-
#[unstable = "relationship with panic is unclear"]
250245
macro_rules! unimplemented {
251246
() => (panic!("not yet implemented"))
252247
}

trunk/src/libstd/lib.rs

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

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

129128
#[macro_use]

trunk/src/libstd/macros.rs

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,184 @@ 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+
63241
/// Use the syntax described in `std::fmt` to create a value of type `String`.
64242
/// See `std::fmt` for more information.
65243
///

trunk/src/libsyntax/test.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,19 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<ast::ViewItem>) {
439439
let item_ = ast::ItemMod(testmod);
440440

441441
let mod_ident = token::gensym_ident("__test");
442+
let allow_unstable = {
443+
let unstable = P(nospan(ast::MetaWord(InternedString::new("unstable"))));
444+
let allow = P(nospan(ast::MetaList(InternedString::new("allow"),
445+
vec![unstable])));
446+
attr::mk_attr_inner(attr::mk_attr_id(), allow)
447+
};
442448
let item = ast::Item {
443449
ident: mod_ident,
444-
attrs: Vec::new(),
445450
id: ast::DUMMY_NODE_ID,
446451
node: item_,
447452
vis: ast::Public,
448453
span: DUMMY_SP,
454+
attrs: vec![allow_unstable],
449455
};
450456
let reexport = cx.reexport_test_harness_main.as_ref().map(|s| {
451457
// building `use <ident> = __test::main`

trunk/src/rust-installer

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit e577c97b494be2815b215e3042207d6d4b7c5516
1+
Subproject commit 3a37981744a5af2433fed551f742465c78c9af7f
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 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+
// compile-flags: --test
12+
13+
#![deny(unstable)]
14+
15+
#[test]
16+
fn foo() {}

0 commit comments

Comments
 (0)