Skip to content

Commit 77f44d4

Browse files
committed
auto merge of #17894 : steveklabnik/rust/fail_to_panic, r=aturon
This in-progress PR implements #17489. I made the code changes in this commit, next is to go through alllllllll the documentation and fix various things. - Rename column headings as appropriate, `# Panics` for panic conditions and `# Errors` for `Result`s. - clean up usage of words like 'fail' in error messages Anything else to add to the list, @aturon ? I think I should leave the actual functions with names like `slice_or_fail` alone, since you'll get to those in your conventions work? I'm submitting just the code bits now so that we can see it separately, and I also don't want to have to keep re-building rust over and over again if I don't have to 😉 Listing all the bits so I can remember as I go: - [x] compiler-rt - [x] compiletest - [x] doc - [x] driver - [x] etc - [x] grammar - [x] jemalloc - [x] liballoc - [x] libarena - [x] libbacktrace - [x] libcollections - [x] libcore - [x] libcoretest - [x] libdebug - [x] libflate - [x] libfmt_macros - [x] libfourcc - [x] libgetopts - [x] libglob - [x] libgraphviz - [x] libgreen - [x] libhexfloat - [x] liblibc - [x] liblog - [x] libnative - [x] libnum - [x] librand - [x] librbml - [x] libregex - [x] libregex_macros - [x] librlibc - [x] librustc - [x] librustc_back - [x] librustc_llvm - [x] librustdoc - [x] librustrt - [x] libsemver - [x] libserialize - [x] libstd - [x] libsync - [x] libsyntax - [x] libterm - [x] libtest - [x] libtime - [x] libunicode - [x] liburl - [x] libuuid - [x] llvm - [x] rt - [x] test
2 parents 4769bca + 6ac7fc7 commit 77f44d4

File tree

518 files changed

+1776
-1727
lines changed

Some content is hidden

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

518 files changed

+1776
-1727
lines changed

Diff for: src/compiletest/compiletest.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn main() {
4141
let config = parse_config(args);
4242

4343
if config.valgrind_path.is_none() && config.force_valgrind {
44-
fail!("Can't find Valgrind to run Valgrind tests");
44+
panic!("Can't find Valgrind to run Valgrind tests");
4545
}
4646

4747
log_config(&config);
@@ -94,20 +94,20 @@ pub fn parse_config(args: Vec<String> ) -> Config {
9494
let message = format!("Usage: {} [OPTIONS] [TESTNAME...]", argv0);
9595
println!("{}", getopts::usage(message.as_slice(), groups.as_slice()));
9696
println!("");
97-
fail!()
97+
panic!()
9898
}
9999

100100
let matches =
101101
&match getopts::getopts(args_.as_slice(), groups.as_slice()) {
102102
Ok(m) => m,
103-
Err(f) => fail!("{}", f)
103+
Err(f) => panic!("{}", f)
104104
};
105105

106106
if matches.opt_present("h") || matches.opt_present("help") {
107107
let message = format!("Usage: {} [OPTIONS] [TESTNAME...]", argv0);
108108
println!("{}", getopts::usage(message.as_slice(), groups.as_slice()));
109109
println!("");
110-
fail!()
110+
panic!()
111111
}
112112

113113
fn opt_path(m: &getopts::Matches, nm: &str) -> Path {
@@ -120,7 +120,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
120120
Ok(re) => Some(re),
121121
Err(e) => {
122122
println!("failed to parse filter /{}/: {}", s, e);
123-
fail!()
123+
panic!()
124124
}
125125
}
126126
} else {
@@ -263,7 +263,7 @@ pub fn run_tests(config: &Config) {
263263
let res = test::run_tests_console(&opts, tests.into_iter().collect());
264264
match res {
265265
Ok(true) => {}
266-
Ok(false) => fail!("Some tests failed"),
266+
Ok(false) => panic!("Some tests failed"),
267267
Err(e) => {
268268
println!("I/O failure during tests: {}", e);
269269
}

Diff for: src/compiletest/header.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn parse_exec_env(line: &str) -> Option<(String, String)> {
305305
let end = strs.pop().unwrap();
306306
(strs.pop().unwrap(), end)
307307
}
308-
n => fail!("Expected 1 or 2 strings, not {}", n)
308+
n => panic!("Expected 1 or 2 strings, not {}", n)
309309
}
310310
})
311311
}
@@ -350,7 +350,7 @@ pub fn gdb_version_to_int(version_string: &str) -> int {
350350
let components: Vec<&str> = version_string.trim().split('.').collect();
351351

352352
if components.len() != 2 {
353-
fail!("{}", error_string);
353+
panic!("{}", error_string);
354354
}
355355

356356
let major: int = FromStr::from_str(components[0]).expect(error_string);

Diff for: src/compiletest/runtest.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn run(config: Config, testfile: String) {
3939

4040
"arm-linux-androideabi" => {
4141
if !config.adb_device_status {
42-
fail!("android device not available");
42+
panic!("android device not available");
4343
}
4444
}
4545

@@ -316,7 +316,7 @@ actual:\n\
316316
------------------------------------------\n\
317317
\n",
318318
expected, actual);
319-
fail!();
319+
panic!();
320320
}
321321
}
322322

@@ -1453,7 +1453,7 @@ fn maybe_dump_to_stdout(config: &Config, out: &str, err: &str) {
14531453

14541454
fn error(err: &str) { println!("\nerror: {}", err); }
14551455

1456-
fn fatal(err: &str) -> ! { error(err); fail!(); }
1456+
fn fatal(err: &str) -> ! { error(err); panic!(); }
14571457

14581458
fn fatal_proc_rec(err: &str, proc_res: &ProcRes) -> ! {
14591459
print!("\n\
@@ -1471,7 +1471,7 @@ stderr:\n\
14711471
\n",
14721472
err, proc_res.status, proc_res.cmdline, proc_res.stdout,
14731473
proc_res.stderr);
1474-
fail!();
1474+
panic!();
14751475
}
14761476

14771477
fn _arm_exec_compiled_test(config: &Config,

Diff for: src/compiletest/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn get_os(triple: &str) -> &'static str {
3131
return os
3232
}
3333
}
34-
fail!("Cannot determine OS from triple");
34+
panic!("Cannot determine OS from triple");
3535
}
3636

3737
#[cfg(target_os = "windows")]

Diff for: src/doc/complement-design-faq.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ code should need to run is a stack.
9494

9595
`match` being exhaustive has some useful properties. First, if every
9696
possibility is covered by the `match`, adding further variants to the `enum`
97-
in the future will prompt a compilation failure, rather than runtime failure.
97+
in the future will prompt a compilation failure, rather than runtime panic.
9898
Second, it makes cost explicit. In general, only safe way to have a
99-
non-exhaustive match would be to fail the task if nothing is matched, though
99+
non-exhaustive match would be to panic the task if nothing is matched, though
100100
it could fall through if the type of the `match` expression is `()`. This sort
101101
of hidden cost and special casing is against the language's philosophy. It's
102102
easy to ignore certain cases by using the `_` wildcard:

Diff for: src/doc/complement-lang-faq.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ Data values in the language can only be constructed through a fixed set of initi
6565
* There is no global inter-crate namespace; all name management occurs within a crate.
6666
* Using another crate binds the root of _its_ namespace into the user's namespace.
6767

68-
## Why is failure unwinding non-recoverable within a task? Why not try to "catch exceptions"?
68+
## Why is panic unwinding non-recoverable within a task? Why not try to "catch exceptions"?
6969

7070
In short, because too few guarantees could be made about the dynamic environment of the catch block, as well as invariants holding in the unwound heap, to be able to safely resume; we believe that other methods of signalling and logging errors are more appropriate, with tasks playing the role of a "hard" isolation boundary between separate heaps.
7171

7272
Rust provides, instead, three predictable and well-defined options for handling any combination of the three main categories of "catch" logic:
7373

7474
* Failure _logging_ is done by the integrated logging subsystem.
75-
* _Recovery_ after a failure is done by trapping a task failure from _outside_ the task, where other tasks are known to be unaffected.
75+
* _Recovery_ after a panic is done by trapping a task panic from _outside_
76+
the task, where other tasks are known to be unaffected.
7677
* _Cleanup_ of resources is done by RAII-style objects with destructors.
7778

7879
Cleanup through RAII-style destructors is more likely to work than in catch blocks anyways, since it will be better tested (part of the non-error control paths, so executed all the time).

Diff for: src/doc/guide-ffi.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ the stack of the task which is spawned.
191191

192192
Foreign libraries often hand off ownership of resources to the calling code.
193193
When this occurs, we must use Rust's destructors to provide safety and guarantee
194-
the release of these resources (especially in the case of failure).
194+
the release of these resources (especially in the case of panic).
195195

196196
# Callbacks from C code to Rust functions
197197

Diff for: src/doc/guide-macros.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ match x {
240240
// complicated stuff goes here
241241
return result + val;
242242
},
243-
_ => fail!("Didn't get good_2")
243+
_ => panic!("Didn't get good_2")
244244
}
245245
}
246246
_ => return 0 // default value
@@ -284,7 +284,7 @@ macro_rules! biased_match (
284284
biased_match!((x) ~ (Good1(g1, val)) else { return 0 };
285285
binds g1, val )
286286
biased_match!((g1.body) ~ (Good2(result) )
287-
else { fail!("Didn't get good_2") };
287+
else { panic!("Didn't get good_2") };
288288
binds result )
289289
// complicated stuff goes here
290290
return result + val;
@@ -397,7 +397,7 @@ macro_rules! biased_match (
397397
# fn f(x: T1) -> uint {
398398
biased_match!(
399399
(x) ~ (Good1(g1, val)) else { return 0 };
400-
(g1.body) ~ (Good2(result) ) else { fail!("Didn't get Good2") };
400+
(g1.body) ~ (Good2(result) ) else { panic!("Didn't get Good2") };
401401
binds val, result )
402402
// complicated stuff goes here
403403
return result + val;

Diff for: src/doc/guide-tasks.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ relates to the Rust type system, and introduce the fundamental library
88
abstractions for constructing concurrent programs.
99

1010
Tasks provide failure isolation and recovery. When a fatal error occurs in Rust
11-
code as a result of an explicit call to `fail!()`, an assertion failure, or
11+
code as a result of an explicit call to `panic!()`, an assertion failure, or
1212
another invalid operation, the runtime system destroys the entire task. Unlike
1313
in languages such as Java and C++, there is no way to `catch` an exception.
14-
Instead, tasks may monitor each other for failure.
14+
Instead, tasks may monitor each other to see if they panic.
1515

1616
Tasks use Rust's type system to provide strong memory safety guarantees. In
1717
particular, the type system guarantees that tasks cannot induce a data race
@@ -317,19 +317,19 @@ spawn(proc() {
317317
# }
318318
```
319319

320-
# Handling task failure
320+
# Handling task panics
321321

322-
Rust has a built-in mechanism for raising exceptions. The `fail!()` macro
323-
(which can also be written with an error string as an argument: `fail!(
324-
~reason)`) and the `assert!` construct (which effectively calls `fail!()` if a
322+
Rust has a built-in mechanism for raising exceptions. The `panic!()` macro
323+
(which can also be written with an error string as an argument: `panic!(
324+
~reason)`) and the `assert!` construct (which effectively calls `panic!()` if a
325325
boolean expression is false) are both ways to raise exceptions. When a task
326326
raises an exception, the task unwinds its stack—running destructors and
327327
freeing memory along the way—and then exits. Unlike exceptions in C++,
328-
exceptions in Rust are unrecoverable within a single task: once a task fails,
328+
exceptions in Rust are unrecoverable within a single task: once a task panics,
329329
there is no way to "catch" the exception.
330330

331-
While it isn't possible for a task to recover from failure, tasks may notify
332-
each other of failure. The simplest way of handling task failure is with the
331+
While it isn't possible for a task to recover from panicking, tasks may notify
332+
each other if they panic. The simplest way of handling a panic is with the
333333
`try` function, which is similar to `spawn`, but immediately blocks and waits
334334
for the child task to finish. `try` returns a value of type
335335
`Result<T, Box<Any + Send>>`. `Result` is an `enum` type with two variants:
@@ -346,7 +346,7 @@ let result: Result<int, Box<std::any::Any + Send>> = task::try(proc() {
346346
if some_condition() {
347347
calculate_result()
348348
} else {
349-
fail!("oops!");
349+
panic!("oops!");
350350
}
351351
});
352352
assert!(result.is_err());
@@ -355,18 +355,18 @@ assert!(result.is_err());
355355
Unlike `spawn`, the function spawned using `try` may return a value, which
356356
`try` will dutifully propagate back to the caller in a [`Result`] enum. If the
357357
child task terminates successfully, `try` will return an `Ok` result; if the
358-
child task fails, `try` will return an `Error` result.
358+
child task panics, `try` will return an `Error` result.
359359

360360
[`Result`]: std/result/index.html
361361

362-
> *Note:* A failed task does not currently produce a useful error
362+
> *Note:* A panicked task does not currently produce a useful error
363363
> value (`try` always returns `Err(())`). In the
364364
> future, it may be possible for tasks to intercept the value passed to
365-
> `fail!()`.
365+
> `panic!()`.
366366
367-
But not all failures are created equal. In some cases you might need to abort
367+
But not all panics are created equal. In some cases you might need to abort
368368
the entire program (perhaps you're writing an assert which, if it trips,
369369
indicates an unrecoverable logic error); in other cases you might want to
370-
contain the failure at a certain boundary (perhaps a small piece of input from
370+
contain the panic at a certain boundary (perhaps a small piece of input from
371371
the outside world, which you happen to be processing in parallel, is malformed
372372
such that the processing task cannot proceed).

Diff for: src/doc/guide-testing.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ value. To run the tests in a crate, it must be compiled with the
4949
`--test` flag: `rustc myprogram.rs --test -o myprogram-tests`. Running
5050
the resulting executable will run all the tests in the crate. A test
5151
is considered successful if its function returns; if the task running
52-
the test fails, through a call to `fail!`, a failed `assert`, or some
52+
the test fails, through a call to `panic!`, a failed `assert`, or some
5353
other (`assert_eq`, ...) means, then the test fails.
5454

5555
When compiling a crate with the `--test` flag `--cfg test` is also
@@ -77,7 +77,7 @@ test on windows you can write `#[cfg_attr(windows, ignore)]`.
7777

7878
Tests that are intended to fail can be annotated with the
7979
`should_fail` attribute. The test will be run, and if it causes its
80-
task to fail then the test will be counted as successful; otherwise it
80+
task to panic then the test will be counted as successful; otherwise it
8181
will be counted as a failure. For example:
8282

8383
~~~test_harness

Diff for: src/doc/guide-unsafe.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ code:
182182
- implement the `Drop` for resource clean-up via a destructor, and use
183183
RAII (Resource Acquisition Is Initialization). This reduces the need
184184
for any manual memory management by users, and automatically ensures
185-
that clean-up is always run, even when the task fails.
185+
that clean-up is always run, even when the task panics.
186186
- ensure that any data stored behind a raw pointer is destroyed at the
187187
appropriate time.
188188

@@ -462,7 +462,7 @@ fn start(_argc: int, _argv: *const *const u8) -> int {
462462
// provided by libstd.
463463
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
464464
#[lang = "eh_personality"] extern fn eh_personality() {}
465-
#[lang = "fail_fmt"] fn fail_fmt() -> ! { loop {} }
465+
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
466466
# // fn main() {} tricked you, rustdoc!
467467
```
468468

@@ -485,7 +485,7 @@ pub extern fn main(argc: int, argv: *const *const u8) -> int {
485485
486486
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
487487
#[lang = "eh_personality"] extern fn eh_personality() {}
488-
#[lang = "fail_fmt"] fn fail_fmt() -> ! { loop {} }
488+
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
489489
# // fn main() {} tricked you, rustdoc!
490490
```
491491

@@ -504,8 +504,8 @@ The second of these three functions, `eh_personality`, is used by the
504504
failure mechanisms of the compiler. This is often mapped to GCC's
505505
personality function (see the
506506
[libstd implementation](std/rt/unwind/index.html) for more
507-
information), but crates which do not trigger failure can be assured
508-
that this function is never called. The final function, `fail_fmt`, is
507+
information), but crates which do not trigger a panic can be assured
508+
that this function is never called. The final function, `panic_fmt`, is
509509
also used by the failure mechanisms of the compiler.
510510

511511
## Using libcore
@@ -565,8 +565,8 @@ pub extern fn dot_product(a: *const u32, a_len: u32,
565565
return ret;
566566
}
567567
568-
#[lang = "fail_fmt"]
569-
extern fn fail_fmt(args: &core::fmt::Arguments,
568+
#[lang = "panic_fmt"]
569+
extern fn panic_fmt(args: &core::fmt::Arguments,
570570
file: &str,
571571
line: uint) -> ! {
572572
loop {}
@@ -579,9 +579,9 @@ extern fn fail_fmt(args: &core::fmt::Arguments,
579579
```
580580

581581
Note that there is one extra lang item here which differs from the examples
582-
above, `fail_fmt`. This must be defined by consumers of libcore because the
583-
core library declares failure, but it does not define it. The `fail_fmt`
584-
lang item is this crate's definition of failure, and it must be guaranteed to
582+
above, `panic_fmt`. This must be defined by consumers of libcore because the
583+
core library declares panics, but it does not define it. The `panic_fmt`
584+
lang item is this crate's definition of panic, and it must be guaranteed to
585585
never return.
586586

587587
As can be seen in this example, the core library is intended to provide the
@@ -686,7 +686,7 @@ fn main(argc: int, argv: *const *const u8) -> int {
686686
687687
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
688688
#[lang = "eh_personality"] extern fn eh_personality() {}
689-
#[lang = "fail_fmt"] fn fail_fmt() -> ! { loop {} }
689+
#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
690690
```
691691

692692
Note the use of `abort`: the `exchange_malloc` lang item is assumed to

Diff for: src/doc/guide.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -5213,17 +5213,17 @@ immediately.
52135213

52145214
## Success and failure
52155215

5216-
Tasks don't always succeed, they can also fail. A task that wishes to fail
5217-
can call the `fail!` macro, passing a message:
5216+
Tasks don't always succeed, they can also panic. A task that wishes to panic
5217+
can call the `panic!` macro, passing a message:
52185218

52195219
```{rust}
52205220
spawn(proc() {
5221-
fail!("Nope.");
5221+
panic!("Nope.");
52225222
});
52235223
```
52245224

5225-
If a task fails, it is not possible for it to recover. However, it can
5226-
notify other tasks that it has failed. We can do this with `task::try`:
5225+
If a task panics, it is not possible for it to recover. However, it can
5226+
notify other tasks that it has panicked. We can do this with `task::try`:
52275227

52285228
```{rust}
52295229
use std::task;
@@ -5233,14 +5233,14 @@ let result = task::try(proc() {
52335233
if rand::random() {
52345234
println!("OK");
52355235
} else {
5236-
fail!("oops!");
5236+
panic!("oops!");
52375237
}
52385238
});
52395239
```
52405240

5241-
This task will randomly fail or succeed. `task::try` returns a `Result`
5241+
This task will randomly panic or succeed. `task::try` returns a `Result`
52425242
type, so we can handle the response like any other computation that may
5243-
fail.
5243+
panic.
52445244

52455245
# Macros
52465246

0 commit comments

Comments
 (0)