Skip to content

Commit

Permalink
auto merge of #5260 : pcwalton/rust/assert, r=pcwalton
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Mar 8, 2013
2 parents ceeccf8 + 48b14f5 commit 647a94d
Show file tree
Hide file tree
Showing 937 changed files with 9,613 additions and 9,459 deletions.
21 changes: 5 additions & 16 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ grammar as double-quoted strings. Other tokens have exact rules given.
The keywords are the following strings:

~~~~~~~~ {.keyword}
as assert
as
break
const copy
do drop
Expand Down Expand Up @@ -2000,7 +2000,7 @@ let v = ~[1,2,3];
mutate(copy v); // Pass a copy
assert v[0] == 1; // Original was not modified
fail_unless!(v[0] == 1); // Original was not modified
~~~~

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


### Assert expressions

~~~~~~~~{.ebnf .gram}
assert_expr : "assert" expr ;
~~~~~~~~

> **Note:** In future versions of Rust, `assert` will be changed from a full expression to a macro.
An `assert` expression causes the program to fail if its `expr` argument evaluates to `false`.
The failure carries string representation of the false expression.

# Type system

## Types
Expand Down Expand Up @@ -2560,7 +2549,7 @@ An example of a tuple type and its use:
type Pair<'self> = (int,&'self str);
let p: Pair<'static> = (10,"hello");
let (a, b) = p;
assert b != "world";
fail_unless!(b != "world");
~~~~


Expand All @@ -2581,7 +2570,7 @@ An example of a vector type and its use:
~~~~
let v: &[int] = &[7, 5, 3];
let i: int = v[2];
assert (i == 3);
fail_unless!(i == 3);
~~~~

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

Other operations act on box values as single-word-sized address values. For
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn unix_time_in_microseconds() -> u64 {
}
}
# fn main() { assert fmt!("%?", unix_time_in_microseconds()) != ~""; }
# fn main() { fail_unless!(fmt!("%?", unix_time_in_microseconds()) != ~""); }
~~~~

The `#[nolink]` attribute indicates that there's no foreign library to
Expand Down
16 changes: 8 additions & 8 deletions doc/tutorial-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ let result = ports.foldl(0, |accum, port| *accum + port.recv() );

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

Unlike `spawn`, the function spawned using `try` may return a value,
Expand Down Expand Up @@ -401,7 +401,7 @@ do spawn { // Bidirectionally linked
// Wait for the supervised child task to exist.
let message = receiver.recv();
// Kill both it and the parent task.
assert message != 42;
fail_unless!(message != 42);
}
do try { // Unidirectionally linked
sender.send(42);
Expand Down Expand Up @@ -507,13 +507,13 @@ do spawn {
};
from_child.send(22);
assert from_child.recv() == ~"22";
fail_unless!(from_child.recv() == ~"22");
from_child.send(23);
from_child.send(0);
assert from_child.recv() == ~"23";
assert from_child.recv() == ~"0";
fail_unless!(from_child.recv() == ~"23");
fail_unless!(from_child.recv() == ~"0");
# }
~~~~
Expand Down
12 changes: 6 additions & 6 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ expression to the given type.
~~~~
let x: float = 4.0;
let y: uint = x as uint;
assert y == 4u;
fail_unless!(y == 4u);
~~~~

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

As with `match` expressions and `let` bindings, function arguments support
Expand Down Expand Up @@ -1000,7 +1000,7 @@ let x = ~10;
let y = copy x;
let z = *x + *y;
assert z == 20;
fail_unless!(z == 20);
~~~~

When they do not contain any managed boxes, owned boxes can be sent
Expand Down Expand Up @@ -1327,8 +1327,8 @@ and [`core::str`]. Here are some examples.
let crayons = [Almond, AntiqueBrass, Apricot];
// Check the length of the vector
assert crayons.len() == 3;
assert !crayons.is_empty();
fail_unless!(crayons.len() == 3);
fail_unless!(!crayons.is_empty());
// Iterate over a vector, obtaining a pointer to each element
for crayons.each |crayon| {
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/compiletest.rc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn parse_config(args: ~[~str]) -> config {
getopts::optopt(~"logfile"),
getopts::optflag(~"jit")];

assert !args.is_empty();
fail_unless!(!args.is_empty());
let args_ = vec::tail(args);
let matches =
&match getopts::getopts(args_, opts) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn target_env(lib_path: ~str, prog: ~str) -> ~[(~str,~str)] {
let mut env = os::env();

// Make sure we include the aux directory in the path
assert prog.ends_with(~".exe");
fail_unless!(prog.ends_with(~".exe"));
let aux_path = prog.slice(0u, prog.len() - 4u) + ~".libaux";
env = do vec::map(env) |pair| {
Expand Down
45 changes: 26 additions & 19 deletions src/libcore/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ use vec;
/// Code for dealing with @-vectors. This is pretty incomplete, and
/// contains a bunch of duplication from the code for ~-vectors.
#[abi = "cdecl"]
pub extern mod rustrt {
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
++v: **vec::raw::VecRepr,
++n: libc::size_t);
pub mod rustrt {
use libc;
use sys;
use vec;

#[abi = "cdecl"]
#[link_name = "rustrt"]
pub extern {
pub unsafe fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
++v: **vec::raw::VecRepr,
++n: libc::size_t);
}
}

/// Returns the number of elements the vector can hold without reallocating
Expand Down Expand Up @@ -285,31 +292,31 @@ pub fn test() {
}
}

assert seq_range(10, 15) == @[10, 11, 12, 13, 14];
assert from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5];
assert from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14];
fail_unless!(seq_range(10, 15) == @[10, 11, 12, 13, 14]);
fail_unless!(from_fn(5, |x| x+1) == @[1, 2, 3, 4, 5]);
fail_unless!(from_elem(5, 3.14) == @[3.14, 3.14, 3.14, 3.14, 3.14]);
}

#[test]
pub fn append_test() {
assert @[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6];
fail_unless!(@[1,2,3] + @[4,5,6] == @[1,2,3,4,5,6]);
}

#[test]
pub fn test_from_owned() {
assert from_owned::<int>(~[]) == @[];
assert from_owned(~[true]) == @[true];
assert from_owned(~[1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5];
assert from_owned(~[~"abc", ~"123"]) == @[~"abc", ~"123"];
assert from_owned(~[~[42]]) == @[~[42]];
fail_unless!(from_owned::<int>(~[]) == @[]);
fail_unless!(from_owned(~[true]) == @[true]);
fail_unless!(from_owned(~[1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5]);
fail_unless!(from_owned(~[~"abc", ~"123"]) == @[~"abc", ~"123"]);
fail_unless!(from_owned(~[~[42]]) == @[~[42]]);
}
#[test]
pub fn test_from_slice() {
assert from_slice::<int>([]) == @[];
assert from_slice([true]) == @[true];
assert from_slice([1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5];
assert from_slice([@"abc", @"123"]) == @[@"abc", @"123"];
assert from_slice([@[42]]) == @[@[42]];
fail_unless!(from_slice::<int>([]) == @[]);
fail_unless!(from_slice([true]) == @[true]);
fail_unless!(from_slice([1, 2, 3, 4, 5]) == @[1, 2, 3, 4, 5]);
fail_unless!(from_slice([@"abc", @"123"]) == @[@"abc", @"123"]);
fail_unless!(from_slice([@[42]]) == @[@[42]]);
}

8 changes: 4 additions & 4 deletions src/libcore/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ pub fn test_bool_from_str() {
use from_str::FromStr;

do all_values |v| {
assert Some(v) == FromStr::from_str(to_str(v))
fail_unless!(Some(v) == FromStr::from_str(to_str(v)))
}
}

#[test]
pub fn test_bool_to_str() {
assert to_str(false) == ~"false";
assert to_str(true) == ~"true";
fail_unless!(to_str(false) == ~"false");
fail_unless!(to_str(true) == ~"true");
}

#[test]
pub fn test_bool_to_bit() {
do all_values |v| {
assert to_bit(v) == if is_true(v) { 1u8 } else { 0u8 };
fail_unless!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
}
}

Expand Down
23 changes: 13 additions & 10 deletions src/libcore/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[abi = "rust-intrinsic"]
extern mod rusti {
fn forget<T>(-x: T);
fn reinterpret_cast<T, U>(&&e: T) -> U;
pub mod rusti {
#[abi = "rust-intrinsic"]
#[link_name = "rusti"]
pub extern {
fn forget<T>(-x: T);
fn reinterpret_cast<T, U>(&&e: T) -> U;
}
}

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

#[test]
pub fn test_reinterpret_cast() {
assert 1u == unsafe { reinterpret_cast(&1) };
fail_unless!(1u == unsafe { reinterpret_cast(&1) });
}

#[test]
Expand All @@ -120,8 +123,8 @@ pub mod tests {
let ptr: *int = transmute(box); // refcount 2
let _box1: @~str = reinterpret_cast(&ptr);
let _box2: @~str = reinterpret_cast(&ptr);
assert *_box1 == ~"box box box";
assert *_box2 == ~"box box box";
fail_unless!(*_box1 == ~"box box box");
fail_unless!(*_box2 == ~"box box box");
// Will destroy _box1 and _box2. Without the bump, this would
// use-after-free. With too many bumps, it would leak.
}
Expand All @@ -133,15 +136,15 @@ pub mod tests {
unsafe {
let x = @100u8;
let x: *BoxRepr = transmute(x);
assert (*x).data == 100;
fail_unless!((*x).data == 100);
let _x: @int = transmute(x);
}
}
#[test]
pub fn test_transmute2() {
unsafe {
assert ~[76u8, 0u8] == transmute(~"L");
fail_unless!(~[76u8, 0u8] == transmute(~"L"));
}
}
}
8 changes: 4 additions & 4 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub impl<T> Cell<T> {
#[test]
fn test_basic() {
let value_cell = Cell(~10);
assert !value_cell.is_empty();
fail_unless!(!value_cell.is_empty());
let value = value_cell.take();
assert value == ~10;
assert value_cell.is_empty();
fail_unless!(value == ~10);
fail_unless!(value_cell.is_empty());
value_cell.put_back(value);
assert !value_cell.is_empty();
fail_unless!(!value_cell.is_empty());
}

#[test]
Expand Down
Loading

0 comments on commit 647a94d

Please sign in to comment.