Skip to content

Commit 647a94d

Browse files
committed
auto merge of #5260 : pcwalton/rust/assert, r=pcwalton
r? @brson
2 parents ceeccf8 + 48b14f5 commit 647a94d

File tree

937 files changed

+9613
-9459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

937 files changed

+9613
-9459
lines changed

doc/rust.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ grammar as double-quoted strings. Other tokens have exact rules given.
205205
The keywords are the following strings:
206206

207207
~~~~~~~~ {.keyword}
208-
as assert
208+
as
209209
break
210210
const copy
211211
do drop
@@ -2000,7 +2000,7 @@ let v = ~[1,2,3];
20002000
20012001
mutate(copy v); // Pass a copy
20022002
2003-
assert v[0] == 1; // Original was not modified
2003+
fail_unless!(v[0] == 1); // Original was not modified
20042004
~~~~
20052005

20062006
### Unary move expressions
@@ -2450,17 +2450,6 @@ In the future, logging will move into a library, and will no longer be a core ex
24502450
It is therefore recommended to use the macro forms of logging (`error!`, `debug!`, etc.) to minimize disruption in code that uses logging.
24512451

24522452

2453-
### Assert expressions
2454-
2455-
~~~~~~~~{.ebnf .gram}
2456-
assert_expr : "assert" expr ;
2457-
~~~~~~~~
2458-
2459-
> **Note:** In future versions of Rust, `assert` will be changed from a full expression to a macro.
2460-
2461-
An `assert` expression causes the program to fail if its `expr` argument evaluates to `false`.
2462-
The failure carries string representation of the false expression.
2463-
24642453
# Type system
24652454

24662455
## Types
@@ -2560,7 +2549,7 @@ An example of a tuple type and its use:
25602549
type Pair<'self> = (int,&'self str);
25612550
let p: Pair<'static> = (10,"hello");
25622551
let (a, b) = p;
2563-
assert b != "world";
2552+
fail_unless!(b != "world");
25642553
~~~~
25652554

25662555

@@ -2581,7 +2570,7 @@ An example of a vector type and its use:
25812570
~~~~
25822571
let v: &[int] = &[7, 5, 3];
25832572
let i: int = v[2];
2584-
assert (i == 3);
2573+
fail_unless!(i == 3);
25852574
~~~~
25862575

25872576
All accessible elements of a vector are always initialized, and access to a vector is always bounds-checked.
@@ -2986,7 +2975,7 @@ example of an _implicit dereference_ operation performed on box values:
29862975
~~~~~~~~
29872976
struct Foo { y: int }
29882977
let x = @Foo{y: 10};
2989-
assert x.y == 10;
2978+
fail_unless!(x.y == 10);
29902979
~~~~~~~~
29912980

29922981
Other operations act on box values as single-word-sized address values. For

doc/tutorial-ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn unix_time_in_microseconds() -> u64 {
239239
}
240240
}
241241
242-
# fn main() { assert fmt!("%?", unix_time_in_microseconds()) != ~""; }
242+
# fn main() { fail_unless!(fmt!("%?", unix_time_in_microseconds()) != ~""); }
243243
~~~~
244244

245245
The `#[nolink]` attribute indicates that there's no foreign library to

doc/tutorial-tasks.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ let result = ports.foldl(0, |accum, port| *accum + port.recv() );
297297

298298
Rust has a built-in mechanism for raising exceptions. The `fail!()` macro
299299
(which can also be written with an error string as an argument: `fail!(
300-
~reason)`) and the `assert` construct (which effectively calls `fail!()` if a
301-
boolean expression is false) are both ways to raise exceptions. When a task
302-
raises an exception the task unwinds its stack---running destructors and
300+
~reason)`) and the `fail_unless!` construct (which effectively calls `fail!()`
301+
if a boolean expression is false) are both ways to raise exceptions. When a
302+
task raises an exception the task unwinds its stack---running destructors and
303303
freeing memory along the way---and then exits. Unlike exceptions in C++,
304304
exceptions in Rust are unrecoverable within a single task: once a task fails,
305305
there is no way to "catch" the exception.
@@ -339,7 +339,7 @@ let result: Result<int, ()> = do task::try {
339339
fail!(~"oops!");
340340
}
341341
};
342-
assert result.is_err();
342+
fail_unless!(result.is_err());
343343
~~~
344344

345345
Unlike `spawn`, the function spawned using `try` may return a value,
@@ -401,7 +401,7 @@ do spawn { // Bidirectionally linked
401401
// Wait for the supervised child task to exist.
402402
let message = receiver.recv();
403403
// Kill both it and the parent task.
404-
assert message != 42;
404+
fail_unless!(message != 42);
405405
}
406406
do try { // Unidirectionally linked
407407
sender.send(42);
@@ -507,13 +507,13 @@ do spawn {
507507
};
508508
509509
from_child.send(22);
510-
assert from_child.recv() == ~"22";
510+
fail_unless!(from_child.recv() == ~"22");
511511
512512
from_child.send(23);
513513
from_child.send(0);
514514
515-
assert from_child.recv() == ~"23";
516-
assert from_child.recv() == ~"0";
515+
fail_unless!(from_child.recv() == ~"23");
516+
fail_unless!(from_child.recv() == ~"0");
517517
518518
# }
519519
~~~~

doc/tutorial.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ expression to the given type.
381381
~~~~
382382
let x: float = 4.0;
383383
let y: uint = x as uint;
384-
assert y == 4u;
384+
fail_unless!(y == 4u);
385385
~~~~
386386

387387
## Syntax extensions
@@ -849,8 +849,8 @@ Ending the function with a semicolon like so is equivalent to returning `()`.
849849
fn line(a: int, b: int, x: int) -> int { a * x + b }
850850
fn oops(a: int, b: int, x: int) -> () { a * x + b; }
851851
852-
assert 8 == line(5, 3, 1);
853-
assert () == oops(5, 3, 1);
852+
fail_unless!(8 == line(5, 3, 1));
853+
fail_unless!(() == oops(5, 3, 1));
854854
~~~~
855855

856856
As with `match` expressions and `let` bindings, function arguments support
@@ -1000,7 +1000,7 @@ let x = ~10;
10001000
let y = copy x;
10011001
10021002
let z = *x + *y;
1003-
assert z == 20;
1003+
fail_unless!(z == 20);
10041004
~~~~
10051005

10061006
When they do not contain any managed boxes, owned boxes can be sent
@@ -1327,8 +1327,8 @@ and [`core::str`]. Here are some examples.
13271327
let crayons = [Almond, AntiqueBrass, Apricot];
13281328
13291329
// Check the length of the vector
1330-
assert crayons.len() == 3;
1331-
assert !crayons.is_empty();
1330+
fail_unless!(crayons.len() == 3);
1331+
fail_unless!(!crayons.is_empty());
13321332
13331333
// Iterate over a vector, obtaining a pointer to each element
13341334
for crayons.each |crayon| {

src/compiletest/compiletest.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn parse_config(args: ~[~str]) -> config {
6363
getopts::optopt(~"logfile"),
6464
getopts::optflag(~"jit")];
6565

66-
assert !args.is_empty();
66+
fail_unless!(!args.is_empty());
6767
let args_ = vec::tail(args);
6868
let matches =
6969
&match getopts::getopts(args_, opts) {

src/compiletest/procsrv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn target_env(lib_path: ~str, prog: ~str) -> ~[(~str,~str)] {
2525
let mut env = os::env();
2626

2727
// Make sure we include the aux directory in the path
28-
assert prog.ends_with(~".exe");
28+
fail_unless!(prog.ends_with(~".exe"));
2929
let aux_path = prog.slice(0u, prog.len() - 4u) + ~".libaux";
3030
3131
env = do vec::map(env) |pair| {

src/libcore/at_vec.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ use vec;
2323
/// Code for dealing with @-vectors. This is pretty incomplete, and
2424
/// contains a bunch of duplication from the code for ~-vectors.
2525
26-
#[abi = "cdecl"]
27-
pub extern mod rustrt {
28-
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
29-
++v: **vec::raw::VecRepr,
30-
++n: libc::size_t);
26+
pub mod rustrt {
27+
use libc;
28+
use sys;
29+
use vec;
30+
31+
#[abi = "cdecl"]
32+
#[link_name = "rustrt"]
33+
pub extern {
34+
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
35+
++v: **vec::raw::VecRepr,
36+
++n: libc::size_t);
37+
}
3138
}
3239

3340
/// Returns the number of elements the vector can hold without reallocating
@@ -285,31 +292,31 @@ pub fn test() {
285292
}
286293
}
287294

288-
assert seq_range(10, 15) == @[10, 11, 12, 13, 14];
289-
assert from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5];
290-
assert from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14];
295+
fail_unless!(seq_range(10, 15) == @[10, 11, 12, 13, 14]);
296+
fail_unless!(from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5]);
297+
fail_unless!(from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14]);
291298
}
292299

293300
#[test]
294301
pub fn append_test() {
295-
assert @[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6];
302+
fail_unless!(@[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6]);
296303
}
297304

298305
#[test]
299306
pub fn test_from_owned() {
300-
assert from_owned::<int>(~[]) == @[];
301-
assert from_owned(~[true]) == @[true];
302-
assert from_owned(~[1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5];
303-
assert from_owned(~[~"abc", ~"123"]) == @[~"abc", ~"123"];
304-
assert from_owned(~[~[42]]) == @[~[42]];
307+
fail_unless!(from_owned::<int>(~[]) == @[]);
308+
fail_unless!(from_owned(~[true]) == @[true]);
309+
fail_unless!(from_owned(~[1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5]);
310+
fail_unless!(from_owned(~[~"abc", ~"123"]) == @[~"abc", ~"123"]);
311+
fail_unless!(from_owned(~[~[42]]) == @[~[42]]);
305312
}
306313
307314
#[test]
308315
pub fn test_from_slice() {
309-
assert from_slice::<int>([]) == @[];
310-
assert from_slice([true]) == @[true];
311-
assert from_slice([1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5];
312-
assert from_slice([@"abc", @"123"]) == @[@"abc", @"123"];
313-
assert from_slice([@[42]]) == @[@[42]];
316+
fail_unless!(from_slice::<int>([]) == @[]);
317+
fail_unless!(from_slice([true]) == @[true]);
318+
fail_unless!(from_slice([1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5]);
319+
fail_unless!(from_slice([@"abc", @"123"]) == @[@"abc", @"123"]);
320+
fail_unless!(from_slice([@[42]]) == @[@[42]]);
314321
}
315322

src/libcore/bool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ pub fn test_bool_from_str() {
8686
use from_str::FromStr;
8787

8888
do all_values |v| {
89-
assert Some(v) == FromStr::from_str(to_str(v))
89+
fail_unless!(Some(v) == FromStr::from_str(to_str(v)))
9090
}
9191
}
9292

9393
#[test]
9494
pub fn test_bool_to_str() {
95-
assert to_str(false) == ~"false";
96-
assert to_str(true) == ~"true";
95+
fail_unless!(to_str(false) == ~"false");
96+
fail_unless!(to_str(true) == ~"true");
9797
}
9898

9999
#[test]
100100
pub fn test_bool_to_bit() {
101101
do all_values |v| {
102-
assert to_bit(v) == if is_true(v) { 1u8 } else { 0u8 };
102+
fail_unless!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
103103
}
104104
}
105105

src/libcore/cast.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[abi = "rust-intrinsic"]
12-
extern mod rusti {
13-
fn forget<T>(-x: T);
14-
fn reinterpret_cast<T, U>(&&e: T) -> U;
11+
pub mod rusti {
12+
#[abi = "rust-intrinsic"]
13+
#[link_name = "rusti"]
14+
pub extern {
15+
fn forget<T>(-x: T);
16+
fn reinterpret_cast<T, U>(&&e: T) -> U;
17+
}
1518
}
1619

1720
/// Casts the value at `src` to U. The two types must have the same length.
@@ -45,7 +48,7 @@ pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
4548
*
4649
* # Example
4750
*
48-
* assert transmute("L") == ~[76u8, 0u8];
51+
* fail_unless!(transmute("L") == ~[76u8, 0u8]);
4952
*/
5053
#[inline(always)]
5154
pub unsafe fn transmute<L, G>(thing: L) -> G {
@@ -109,7 +112,7 @@ pub mod tests {
109112

110113
#[test]
111114
pub fn test_reinterpret_cast() {
112-
assert 1u == unsafe { reinterpret_cast(&1) };
115+
fail_unless!(1u == unsafe { reinterpret_cast(&1) });
113116
}
114117

115118
#[test]
@@ -120,8 +123,8 @@ pub mod tests {
120123
let ptr: *int = transmute(box); // refcount 2
121124
let _box1: @~str = reinterpret_cast(&ptr);
122125
let _box2: @~str = reinterpret_cast(&ptr);
123-
assert *_box1 == ~"box box box";
124-
assert *_box2 == ~"box box box";
126+
fail_unless!(*_box1 == ~"box box box");
127+
fail_unless!(*_box2 == ~"box box box");
125128
// Will destroy _box1 and _box2. Without the bump, this would
126129
// use-after-free. With too many bumps, it would leak.
127130
}
@@ -133,15 +136,15 @@ pub mod tests {
133136
unsafe {
134137
let x = @100u8;
135138
let x: *BoxRepr = transmute(x);
136-
assert (*x).data == 100;
139+
fail_unless!((*x).data == 100);
137140
let _x: @int = transmute(x);
138141
}
139142
}
140143
141144
#[test]
142145
pub fn test_transmute2() {
143146
unsafe {
144-
assert ~[76u8, 0u8] == transmute(~"L");
147+
fail_unless!(~[76u8, 0u8] == transmute(~"L"));
145148
}
146149
}
147150
}

src/libcore/cell.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ pub impl<T> Cell<T> {
6565
#[test]
6666
fn test_basic() {
6767
let value_cell = Cell(~10);
68-
assert !value_cell.is_empty();
68+
fail_unless!(!value_cell.is_empty());
6969
let value = value_cell.take();
70-
assert value == ~10;
71-
assert value_cell.is_empty();
70+
fail_unless!(value == ~10);
71+
fail_unless!(value_cell.is_empty());
7272
value_cell.put_back(value);
73-
assert !value_cell.is_empty();
73+
fail_unless!(!value_cell.is_empty());
7474
}
7575

7676
#[test]

0 commit comments

Comments
 (0)