Skip to content

Commit 4cf8343

Browse files
committed
test built-in panic catching
1 parent b91383b commit 4cf8343

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

tests/run-pass/catch_panic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// ignore-windows: Unwind panicking does not currently work on Windows
2+
#![allow(const_err)]
3+
24
use std::panic::{catch_unwind, AssertUnwindSafe};
35
use std::cell::Cell;
46

@@ -44,15 +46,21 @@ fn main() {
4446
prev(panic_info)
4547
}));
4648

49+
// Std panics
4750
test(|_old_val| std::panic!("Hello from panic: std"));
4851
test(|old_val| std::panic!(format!("Hello from panic: {:?}", old_val)));
4952
test(|old_val| std::panic!("Hello from panic: {:?}", old_val));
5053
test(|_old_val| std::panic!(1337));
5154

55+
// Core panics
5256
test(|_old_val| core::panic!("Hello from panic: core"));
5357
test(|old_val| core::panic!(&format!("Hello from panic: {:?}", old_val)));
5458
test(|old_val| core::panic!("Hello from panic: {:?}", old_val));
5559

60+
// Built-in panics
61+
test(|_old_val| { let _val = [0, 1, 2][4]; loop {} });
62+
test(|_old_val| { let _val = 1/0; loop {} });
63+
5664
// Cleanup: reset to default hook.
5765
drop(std::panic::take_hook());
5866

tests/run-pass/catch_panic.stderr

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
thread 'main' panicked at 'Hello from panic: std', $DIR/catch_panic.rs:47:21
1+
thread 'main' panicked at 'Hello from panic: std', $DIR/catch_panic.rs:50:21
22
Caught panic message (&str): Hello from panic: std
3-
thread 'main' panicked at 'Hello from panic: 1', $DIR/catch_panic.rs:48:20
3+
thread 'main' panicked at 'Hello from panic: 1', $DIR/catch_panic.rs:51:20
44
Caught panic message (String): Hello from panic: 1
5-
thread 'main' panicked at 'Hello from panic: 2', $DIR/catch_panic.rs:49:20
5+
thread 'main' panicked at 'Hello from panic: 2', $DIR/catch_panic.rs:52:20
66
Caught panic message (String): Hello from panic: 2
7-
thread 'main' panicked at 'Box<Any>', $DIR/catch_panic.rs:50:21
7+
thread 'main' panicked at 'Box<Any>', $DIR/catch_panic.rs:53:21
88
Failed get caught panic message.
9-
thread 'main' panicked at 'Hello from panic: core', $DIR/catch_panic.rs:52:21
9+
thread 'main' panicked at 'Hello from panic: core', $DIR/catch_panic.rs:56:21
1010
Caught panic message (String): Hello from panic: core
11-
thread 'main' panicked at 'Hello from panic: 5', $DIR/catch_panic.rs:53:20
11+
thread 'main' panicked at 'Hello from panic: 5', $DIR/catch_panic.rs:57:20
1212
Caught panic message (String): Hello from panic: 5
13-
thread 'main' panicked at 'Hello from panic: 6', $DIR/catch_panic.rs:54:20
13+
thread 'main' panicked at 'Hello from panic: 6', $DIR/catch_panic.rs:58:20
1414
Caught panic message (String): Hello from panic: 6
15+
thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4', $DIR/catch_panic.rs:61:34
16+
Caught panic message (String): index out of bounds: the len is 3 but the index is 4
17+
thread 'main' panicked at 'attempt to divide by zero', $DIR/catch_panic.rs:62:34
18+
Caught panic message (String): attempt to divide by zero
1519
Success!

0 commit comments

Comments
 (0)