Skip to content

Replacing fail keyword part 2 of 3 #4744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/lib/codemirror-rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CodeMirror.defineMode("rust", function() {
var indentUnit = 4, altIndentUnit = 2;
var valKeywords = {
"if": "if-style", "while": "if-style", "loop": "if-style", "else": "else-style",
"do": "else-style", "return": "else-style", "fail": "else-style",
"do": "else-style", "return": "else-style",
"break": "atom", "cont": "atom", "const": "let", "resource": "fn",
"let": "let", "fn": "fn", "for": "for", "match": "match", "trait": "trait",
"impl": "impl", "type": "type", "enum": "enum", "struct": "atom", "mod": "mod",
Expand Down
37 changes: 12 additions & 25 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ break
const copy
do drop
else enum extern
fail false fn for
false fn for
if impl
let log loop
match mod move mut
Expand Down Expand Up @@ -692,15 +692,15 @@ mod math {
type complex = (f64, f64);
fn sin(f: f64) -> f64 {
...
# fail;
# die!();
}
fn cos(f: f64) -> f64 {
...
# fail;
# die!();
}
fn tan(f: f64) -> f64 {
...
# fail;
# die!();
}
}
~~~~~~~~
Expand Down Expand Up @@ -989,13 +989,13 @@ output slot type would normally be. For example:
~~~~
fn my_err(s: &str) -> ! {
log(info, s);
fail;
die!();
}
~~~~

We call such functions "diverging" because they never return a value to the
caller. Every control path in a diverging function must end with a
[`fail`](#fail-expressions) or a call to another diverging function on every
`fail!()` or a call to another diverging function on every
control path. The `!` annotation does *not* denote a type. Rather, the result
type of a diverging function is a special type called $\bot$ ("bottom") that
unifies with any type. Rust has no syntax for $\bot$.
Expand All @@ -1007,7 +1007,7 @@ were declared without the `!` annotation, the following code would not
typecheck:

~~~~
# fn my_err(s: &str) -> ! { fail }
# fn my_err(s: &str) -> ! { die!() }

fn f(i: int) -> int {
if i == 42 {
Expand Down Expand Up @@ -2291,9 +2291,9 @@ enum List<X> { Nil, Cons(X, @List<X>) }
let x: List<int> = Cons(10, @Cons(11, @Nil));

match x {
Cons(_, @Nil) => fail ~"singleton list",
Cons(_, @Nil) => die!(~"singleton list"),
Cons(*) => return,
Nil => fail ~"empty list"
Nil => die!(~"empty list")
}
~~~~

Expand Down Expand Up @@ -2330,7 +2330,7 @@ match x {
return;
}
_ => {
fail;
die!();
}
}
~~~~
Expand Down Expand Up @@ -2418,23 +2418,10 @@ guard may refer to the variables bound within the pattern they follow.
let message = match maybe_digit {
Some(x) if x < 10 => process_digit(x),
Some(x) => process_other(x),
None => fail
None => die!()
};
~~~~


### Fail expressions

~~~~~~~~{.ebnf .gram}
fail_expr : "fail" expr ? ;
~~~~~~~~

Evaluating a `fail` expression causes a task to enter the *failing* state. In
the *failing* state, a task unwinds its stack, destroying all frames and
running all destructors until it reaches its entry frame, at which point it
halts execution in the *dead* state.


### Return expressions

~~~~~~~~{.ebnf .gram}
Expand Down Expand Up @@ -3154,7 +3141,7 @@ unblock and transition back to *running*.

A task may transition to the *failing* state at any time, due being
killed by some external event or internally, from the evaluation of a
`fail` expression. Once *failing*, a task unwinds its stack and
`fail!()` macro. Once *failing*, a task unwinds its stack and
transitions to the *dead* state. Unwinding the stack of a task is done by
the task itself, on its own control stack. If a value with a destructor is
freed during unwinding, the code for the destructor is run, also on the task's
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorial-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ match x {
// complicated stuff goes here
return result + val;
},
_ => fail ~"Didn't get good_2"
_ => die!(~"Didn't get good_2")
}
}
_ => return 0 // default value
Expand Down Expand Up @@ -260,7 +260,7 @@ macro_rules! biased_match (
biased_match!((x) ~ (good_1(g1, val)) else { return 0 };
binds g1, val )
biased_match!((g1.body) ~ (good_2(result) )
else { fail ~"Didn't get good_2" };
else { die!(~"Didn't get good_2") };
binds result )
// complicated stuff goes here
return result + val;
Expand Down Expand Up @@ -362,7 +362,7 @@ macro_rules! biased_match (
# fn f(x: t1) -> uint {
biased_match!(
(x) ~ (good_1(g1, val)) else { return 0 };
(g1.body) ~ (good_2(result) ) else { fail ~"Didn't get good_2" };
(g1.body) ~ (good_2(result) ) else { die!(~"Didn't get good_2") };
binds val, result )
// complicated stuff goes here
return result + val;
Expand Down
22 changes: 11 additions & 11 deletions doc/tutorial-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cheaper to create than traditional threads, Rust can create hundreds of
thousands of concurrent tasks on a typical 32-bit system.

Tasks provide failure isolation and recovery. When an exception occurs in Rust
code (as a result of an explicit call to `fail`, an assertion failure, or
code (as a result of an explicit call to `fail!()`, an assertion failure, or
another invalid operation), the runtime system destroys the entire
task. Unlike in languages such as Java and C++, there is no way to `catch` an
exception. Instead, tasks may monitor each other for failure.
Expand Down Expand Up @@ -296,9 +296,9 @@ let result = ports.foldl(0, |accum, port| *accum + port.recv() );

# Handling task failure

Rust has a built-in mechanism for raising exceptions. The `fail` construct
(which can also be written with an error string as an argument: `fail
~reason`) and the `assert` construct (which effectively calls `fail` if a
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
freeing memory along the way---and then exits. Unlike exceptions in C++,
Expand All @@ -313,7 +313,7 @@ of all tasks are intertwined: if one fails, so do all the others.
# fn do_some_work() { loop { task::yield() } }
# do task::try {
// Create a child task that fails
do spawn { fail }
do spawn { die!() }

// This will also fail because the task we spawned failed
do_some_work();
Expand All @@ -337,7 +337,7 @@ let result: Result<int, ()> = do task::try {
if some_condition() {
calculate_result()
} else {
fail ~"oops!";
die!(~"oops!");
}
};
assert result.is_err();
Expand All @@ -354,7 +354,7 @@ an `Error` result.
> ***Note:*** A failed task does not currently produce a useful error
> value (`try` always returns `Err(())`). In the
> future, it may be possible for tasks to intercept the value passed to
> `fail`.
> `fail!()`.

TODO: Need discussion of `future_result` in order to make failure
modes useful.
Expand All @@ -377,7 +377,7 @@ either task dies, it kills the other one.
# do task::try {
do task::spawn {
do task::spawn {
fail; // All three tasks will die.
die!(); // All three tasks will die.
}
sleep_forever(); // Will get woken up by force, then fail
}
Expand Down Expand Up @@ -432,7 +432,7 @@ do task::spawn_supervised {
// Intermediate task immediately exits
}
wait_for_a_while();
fail; // Will kill grandchild even if child has already exited
die!(); // Will kill grandchild even if child has already exited
# };
~~~

Expand All @@ -446,10 +446,10 @@ other at all, using `task::spawn_unlinked` for _isolated failure_.
let (time1, time2) = (random(), random());
do task::spawn_unlinked {
sleep_for(time2); // Won't get forced awake
fail;
die!();
}
sleep_for(time1); // Won't get forced awake
fail;
die!();
// It will take MAX(time1,time2) for the program to finish.
# };
~~~
Expand Down
2 changes: 1 addition & 1 deletion src/libcargo/cargo.rc
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub fn parse_source(name: ~str, j: &json::Json) -> @Source {
json::Object(j) => {
let mut url = match j.find(&~"url") {
Some(&json::String(u)) => copy u,
_ => fail ~"needed 'url' field in source"
_ => die!(~"needed 'url' field in source")
};
let method = match j.find(&~"method") {
Some(&json::String(u)) => copy u,
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/dvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<A> DVec<A> {
let mut data = cast::reinterpret_cast(&null::<()>());
data <-> self.data;
let data_ptr: *() = cast::reinterpret_cast(&data);
if data_ptr.is_null() { fail ~"Recursive use of dvec"; }
if data_ptr.is_null() { die!(~"Recursive use of dvec"); }
self.data = move ~[move t];
self.data.push_all_move(move data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub mod linear {
pure fn get(&self, k: &K) -> &self/V {
match self.find(k) {
Some(v) => v,
None => fail fmt!("No entry found for key: %?", k),
None => die!(fmt!("No entry found for key: %?", k)),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ pub fn send<T,Tbuffer>(p: SendPacketBuffered<T,Tbuffer>, payload: T) -> bool {
//unsafe { forget(p); }
return true;
}
Full => fail ~"duplicate send",
Full => die!(~"duplicate send"),
Blocked => {
debug!("waking up task for %?", p_);
let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
Expand Down Expand Up @@ -1020,7 +1020,7 @@ impl<T:Owned,Tbuffer:Owned> SendPacketBuffered<T,Tbuffer> {
//forget(packet);
header
},
None => fail ~"packet already consumed"
None => die!(~"packet already consumed")
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/private/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn test_fail() {
let mut i = 0;
do (|| {
i = 10;
fail;
die!();
}).finally {
assert failing();
assert i == 10;
Expand All @@ -95,4 +95,4 @@ fn test_compact() {
fn but_always_run_this_function() { }
do_some_fallible_work.finally(
but_always_run_this_function);
}
}
6 changes: 3 additions & 3 deletions src/libcore/private/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn test_modify() {
Some(~shared_mutable_state(10))
}
}
_ => fail
_ => die!()
}
}

Expand All @@ -280,7 +280,7 @@ fn test_modify() {
assert *v == 10;
None
},
_ => fail
_ => die!()
}
}

Expand All @@ -291,7 +291,7 @@ fn test_modify() {
Some(~shared_mutable_state(10))
}
}
_ => fail
_ => die!()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/private/weak_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn run_weak_task_service(port: Port<ServiceMsg>) {
// nobody will receive this
shutdown_chan.send(());
}
None => fail
None => die!()
}
}
Shutdown => break
Expand Down Expand Up @@ -195,7 +195,7 @@ fn test_select_stream_and_oneshot() {
do weaken_task |signal| {
match select2i(&port, &signal) {
Left(*) => (),
Right(*) => fail
Right(*) => die!()
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ impl ReprVisitor : TyVisitor {
}

// Type no longer exists, vestigial function.
fn visit_str(&self) -> bool { fail; }
fn visit_str(&self) -> bool { die!(); }

fn visit_estr_box(&self) -> bool {
do self.get::<@str> |s| {
Expand All @@ -616,7 +616,7 @@ impl ReprVisitor : TyVisitor {

// Type no longer exists, vestigial function.
fn visit_estr_fixed(&self, _n: uint, _sz: uint,
_align: uint) -> bool { fail; }
_align: uint) -> bool { die!(); }

fn visit_box(&self, mtbl: uint, inner: *TyDesc) -> bool {
self.writer.write_char('@');
Expand Down Expand Up @@ -652,7 +652,7 @@ impl ReprVisitor : TyVisitor {
}

// Type no longer exists, vestigial function.
fn visit_vec(&self, _mtbl: uint, _inner: *TyDesc) -> bool { fail; }
fn visit_vec(&self, _mtbl: uint, _inner: *TyDesc) -> bool { die!(); }


fn visit_unboxed_vec(&self, mtbl: uint, inner: *TyDesc) -> bool {
Expand Down Expand Up @@ -859,7 +859,7 @@ impl ReprVisitor : TyVisitor {
}

// Type no longer exists, vestigial function.
fn visit_constr(&self, _inner: *TyDesc) -> bool { fail; }
fn visit_constr(&self, _inner: *TyDesc) -> bool { die!(); }

fn visit_closure_ptr(&self, _ck: uint) -> bool { true }
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ fn test_spawn_linked_sup_fail_up() { // child fails; parent fails
can_not_copy: None,
.. b0
};
do b1.spawn { fail; }
do b1.spawn { die!(); }
po.recv(); // We should get punted awake
}
#[test] #[should_fail] #[ignore(cfg(windows))]
Expand All @@ -760,7 +760,7 @@ fn test_spawn_linked_sup_fail_down() { // parent fails; child fails
fn test_spawn_linked_unsup_fail_up() { // child fails; parent fails
let (po, _ch) = stream::<()>();
// Default options are to spawn linked & unsupervised.
do spawn { fail; }
do spawn { die!(); }
po.recv(); // We should get punted awake
}
#[test] #[should_fail] #[ignore(cfg(windows))]
Expand Down
Loading