diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/panics | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/panics')
58 files changed, 871 insertions, 0 deletions
diff --git a/src/test/ui/panics/abort-on-panic.rs b/src/test/ui/panics/abort-on-panic.rs new file mode 100644 index 000000000..1f6ad64c0 --- /dev/null +++ b/src/test/ui/panics/abort-on-panic.rs @@ -0,0 +1,98 @@ +// run-pass + +#![allow(unused_must_use)] +#![feature(c_unwind)] +#![feature(panic_always_abort)] +// Since we mark some ABIs as "nounwind" to LLVM, we must make sure that +// we never unwind through them. + +// ignore-emscripten no processes +// ignore-sgx no processes + +use std::io; +use std::io::prelude::*; +use std::process::{exit, Command, Stdio}; +use std::sync::{Arc, Barrier}; +use std::thread; +use std::{env, panic}; + +extern "C" fn panic_in_ffi() { + panic!("Test"); +} + +fn should_have_aborted() { + io::stdout().write(b"This should never be printed.\n"); + let _ = io::stdout().flush(); +} + +fn bomb_out_but_not_abort(msg: &str) { + eprintln!("bombing out: {}", msg); + exit(1); +} + +fn test() { + let _ = panic::catch_unwind(|| { + panic_in_ffi(); + }); + should_have_aborted(); +} + +fn test_always_abort() { + panic::always_abort(); + let _ = panic::catch_unwind(|| { + panic!(); + }); + should_have_aborted(); +} + +fn test_always_abort_thread() { + let barrier = Arc::new(Barrier::new(2)); + let thr = { + let barrier = barrier.clone(); + thread::spawn(move || { + barrier.wait(); + panic!("in thread"); + }) + }; + panic::always_abort(); + barrier.wait(); + let _ = thr.join(); + bomb_out_but_not_abort("joined - but we were supposed to panic!"); +} + +fn main() { + let tests: &[(_, fn())] = &[ + ("test", test), + ("test_always_abort", test_always_abort), + ("test_always_abort_thread", test_always_abort_thread), + ]; + + let args: Vec<String> = env::args().collect(); + if args.len() > 1 { + // This is inside the self-executed command. + for (a, f) in tests { + if &args[1] == a { + return f(); + } + } + bomb_out_but_not_abort("bad test"); + } + + let execute_self_expecting_abort = |arg| { + let mut p = Command::new(&args[0]) + .stdout(Stdio::piped()) + .stdin(Stdio::piped()) + .arg(arg) + .spawn() + .unwrap(); + let status = p.wait().unwrap(); + assert!(!status.success()); + // Any reasonable platform can distinguish a process which + // called exit(1) from one which panicked. + assert_ne!(status.code(), Some(1)); + }; + + for (a, _f) in tests { + execute_self_expecting_abort(a); + } +} diff --git a/src/test/ui/panics/args-panic.rs b/src/test/ui/panics/args-panic.rs new file mode 100644 index 000000000..7636025c2 --- /dev/null +++ b/src/test/ui/panics/args-panic.rs @@ -0,0 +1,11 @@ +// run-fail +// error-pattern:meep +// ignore-emscripten no processes + +fn f(_a: isize, _b: isize, _c: Box<isize>) { + panic!("moop"); +} + +fn main() { + f(1, panic!("meep"), Box::new(42)); +} diff --git a/src/test/ui/panics/default-backtrace-ice.rs b/src/test/ui/panics/default-backtrace-ice.rs new file mode 100644 index 000000000..fd86a3f9d --- /dev/null +++ b/src/test/ui/panics/default-backtrace-ice.rs @@ -0,0 +1,9 @@ +// unset-rustc-env:RUST_BACKTRACE +// compile-flags:-Z treat-err-as-bug=1 +// error-pattern:stack backtrace: +// failure-status:101 +// normalize-stderr-test "note: .*" -> "" +// normalize-stderr-test "thread 'rustc' .*" -> "" +// normalize-stderr-test " .*\n" -> "" + +fn main() { missing_ident; } diff --git a/src/test/ui/panics/default-backtrace-ice.stderr b/src/test/ui/panics/default-backtrace-ice.stderr new file mode 100644 index 000000000..a0025d7e2 --- /dev/null +++ b/src/test/ui/panics/default-backtrace-ice.stderr @@ -0,0 +1,18 @@ +error[E0425]: cannot find value `missing_ident` in this scope +LL | fn main() { missing_ident; } + + +stack backtrace: + +error: internal compiler error: unexpected panic + + + + + + + + + +query stack during panic: +end of query stack diff --git a/src/test/ui/panics/doublepanic.rs b/src/test/ui/panics/doublepanic.rs new file mode 100644 index 000000000..c1fcc875c --- /dev/null +++ b/src/test/ui/panics/doublepanic.rs @@ -0,0 +1,10 @@ +#![allow(unreachable_code)] + +// run-fail +// error-pattern:One +// ignore-emscripten no processes + +fn main() { + panic!("One"); + panic!("Two"); +} diff --git a/src/test/ui/panics/explicit-panic-msg.rs b/src/test/ui/panics/explicit-panic-msg.rs new file mode 100644 index 000000000..9d8035787 --- /dev/null +++ b/src/test/ui/panics/explicit-panic-msg.rs @@ -0,0 +1,15 @@ +#![allow(unused_assignments)] +#![allow(unused_variables)] +#![allow(non_fmt_panics)] + +// run-fail +// error-pattern:wooooo +// ignore-emscripten no processes + +fn main() { + let mut a = 1; + if 1 == 1 { + a = 2; + } + panic!(format!("woooo{}", "o")); +} diff --git a/src/test/ui/panics/explicit-panic.rs b/src/test/ui/panics/explicit-panic.rs new file mode 100644 index 000000000..27c73d349 --- /dev/null +++ b/src/test/ui/panics/explicit-panic.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:explicit +// ignore-emscripten no processes + +fn main() { + panic!(); +} diff --git a/src/test/ui/panics/fmt-panic.rs b/src/test/ui/panics/fmt-panic.rs new file mode 100644 index 000000000..87fb2e6dd --- /dev/null +++ b/src/test/ui/panics/fmt-panic.rs @@ -0,0 +1,8 @@ +// run-fail +// error-pattern:meh +// ignore-emscripten no processes + +fn main() { + let str_var: String = "meh".to_string(); + panic!("{}", str_var); +} diff --git a/src/test/ui/panics/issue-47429-short-backtraces.legacy.run.stderr b/src/test/ui/panics/issue-47429-short-backtraces.legacy.run.stderr new file mode 100644 index 000000000..b6223b937 --- /dev/null +++ b/src/test/ui/panics/issue-47429-short-backtraces.legacy.run.stderr @@ -0,0 +1,5 @@ +thread 'main' panicked at 'explicit panic', $DIR/issue-47429-short-backtraces.rs:22:5 +stack backtrace: + 0: std::panicking::begin_panic + 1: issue_47429_short_backtraces::main +note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/src/test/ui/panics/issue-47429-short-backtraces.rs b/src/test/ui/panics/issue-47429-short-backtraces.rs new file mode 100644 index 000000000..f338ace6b --- /dev/null +++ b/src/test/ui/panics/issue-47429-short-backtraces.rs @@ -0,0 +1,23 @@ +// Regression test for #47429: short backtraces were not terminating correctly + +// compile-flags: -O +// compile-flags:-Cstrip=none +// run-fail +// check-run-results +// exec-env:RUST_BACKTRACE=1 + +// ignore-msvc see #62897 and `backtrace-debuginfo.rs` test +// ignore-android FIXME #17520 +// ignore-openbsd no support for libbacktrace without filename +// ignore-wasm no panic or subprocess support +// ignore-emscripten no panic or subprocess support +// ignore-sgx no subprocess support + +// NOTE(eddyb) output differs between symbol mangling schemes +// revisions: legacy v0 +// [legacy] compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy +// [v0] compile-flags: -Csymbol-mangling-version=v0 + +fn main() { + panic!() +} diff --git a/src/test/ui/panics/issue-47429-short-backtraces.v0.run.stderr b/src/test/ui/panics/issue-47429-short-backtraces.v0.run.stderr new file mode 100644 index 000000000..c2bea4492 --- /dev/null +++ b/src/test/ui/panics/issue-47429-short-backtraces.v0.run.stderr @@ -0,0 +1,5 @@ +thread 'main' panicked at 'explicit panic', $DIR/issue-47429-short-backtraces.rs:22:5 +stack backtrace: + 0: std::panicking::begin_panic::<&str> + 1: issue_47429_short_backtraces::main +note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/src/test/ui/panics/location-detail-panic-no-column.rs b/src/test/ui/panics/location-detail-panic-no-column.rs new file mode 100644 index 000000000..7cf1bb09c --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-column.rs @@ -0,0 +1,8 @@ +// run-fail +// check-run-results +// compile-flags: -Zlocation-detail=line,file +// exec-env:RUST_BACKTRACE=0 + +fn main() { + panic!("column-redacted"); +} diff --git a/src/test/ui/panics/location-detail-panic-no-column.run.stderr b/src/test/ui/panics/location-detail-panic-no-column.run.stderr new file mode 100644 index 000000000..46c9b8448 --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-column.run.stderr @@ -0,0 +1,2 @@ +thread 'main' panicked at 'column-redacted', $DIR/location-detail-panic-no-column.rs:7:0 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/test/ui/panics/location-detail-panic-no-file.rs b/src/test/ui/panics/location-detail-panic-no-file.rs new file mode 100644 index 000000000..9bcbf01d1 --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-file.rs @@ -0,0 +1,8 @@ +// run-fail +// check-run-results +// compile-flags: -Zlocation-detail=line,column +// exec-env:RUST_BACKTRACE=0 + +fn main() { + panic!("file-redacted"); +} diff --git a/src/test/ui/panics/location-detail-panic-no-file.run.stderr b/src/test/ui/panics/location-detail-panic-no-file.run.stderr new file mode 100644 index 000000000..811f93bf3 --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-file.run.stderr @@ -0,0 +1,2 @@ +thread 'main' panicked at 'file-redacted', <redacted>:7:5 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/test/ui/panics/location-detail-panic-no-line.rs b/src/test/ui/panics/location-detail-panic-no-line.rs new file mode 100644 index 000000000..25df092e1 --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-line.rs @@ -0,0 +1,8 @@ +// run-fail +// check-run-results +// compile-flags: -Zlocation-detail=file,column +// exec-env:RUST_BACKTRACE=0 + +fn main() { + panic!("line-redacted"); +} diff --git a/src/test/ui/panics/location-detail-panic-no-line.run.stderr b/src/test/ui/panics/location-detail-panic-no-line.run.stderr new file mode 100644 index 000000000..cc3f1624c --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-line.run.stderr @@ -0,0 +1,2 @@ +thread 'main' panicked at 'line-redacted', $DIR/location-detail-panic-no-line.rs:0:5 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/test/ui/panics/location-detail-panic-no-location-info.rs b/src/test/ui/panics/location-detail-panic-no-location-info.rs new file mode 100644 index 000000000..7b609145b --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-location-info.rs @@ -0,0 +1,8 @@ +// run-fail +// check-run-results +// compile-flags: -Zlocation-detail=none +// exec-env:RUST_BACKTRACE=0 + +fn main() { + panic!("no location info"); +} diff --git a/src/test/ui/panics/location-detail-panic-no-location-info.run.stderr b/src/test/ui/panics/location-detail-panic-no-location-info.run.stderr new file mode 100644 index 000000000..d1c310864 --- /dev/null +++ b/src/test/ui/panics/location-detail-panic-no-location-info.run.stderr @@ -0,0 +1,2 @@ +thread 'main' panicked at 'no location info', <redacted>:0:0 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/test/ui/panics/location-detail-unwrap-no-file.rs b/src/test/ui/panics/location-detail-unwrap-no-file.rs new file mode 100644 index 000000000..5955d9a25 --- /dev/null +++ b/src/test/ui/panics/location-detail-unwrap-no-file.rs @@ -0,0 +1,9 @@ +// run-fail +// check-run-results +// compile-flags: -Copt-level=0 -Zlocation-detail=line,column +// exec-env:RUST_BACKTRACE=0 + +fn main() { + let opt: Option<u32> = None; + opt.unwrap(); +} diff --git a/src/test/ui/panics/location-detail-unwrap-no-file.run.stderr b/src/test/ui/panics/location-detail-unwrap-no-file.run.stderr new file mode 100644 index 000000000..7d8e1d930 --- /dev/null +++ b/src/test/ui/panics/location-detail-unwrap-no-file.run.stderr @@ -0,0 +1,2 @@ +thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', <redacted>:8:9 +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace diff --git a/src/test/ui/panics/main-panic.rs b/src/test/ui/panics/main-panic.rs new file mode 100644 index 000000000..023ab4701 --- /dev/null +++ b/src/test/ui/panics/main-panic.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:thread 'main' panicked at +// ignore-emscripten no processes + +fn main() { + panic!() +} diff --git a/src/test/ui/panics/panic-2021.rs b/src/test/ui/panics/panic-2021.rs new file mode 100644 index 000000000..e606612e1 --- /dev/null +++ b/src/test/ui/panics/panic-2021.rs @@ -0,0 +1,9 @@ +// edition:2021 + +fn main() { + panic!(123); //~ ERROR: format argument must be a string literal + panic!("{}"); //~ ERROR: 1 positional argument in format string + core::panic!("{}"); //~ ERROR: 1 positional argument in format string + assert!(false, 123); //~ ERROR: format argument must be a string literal + assert!(false, "{}"); //~ ERROR: 1 positional argument in format string +} diff --git a/src/test/ui/panics/panic-2021.stderr b/src/test/ui/panics/panic-2021.stderr new file mode 100644 index 000000000..373c1c2c0 --- /dev/null +++ b/src/test/ui/panics/panic-2021.stderr @@ -0,0 +1,42 @@ +error: format argument must be a string literal + --> $DIR/panic-2021.rs:4:12 + | +LL | panic!(123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | panic!("{}", 123); + | +++++ + +error: 1 positional argument in format string, but no arguments were given + --> $DIR/panic-2021.rs:5:13 + | +LL | panic!("{}"); + | ^^ + +error: 1 positional argument in format string, but no arguments were given + --> $DIR/panic-2021.rs:6:19 + | +LL | core::panic!("{}"); + | ^^ + +error: format argument must be a string literal + --> $DIR/panic-2021.rs:7:20 + | +LL | assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | assert!(false, "{}", 123); + | +++++ + +error: 1 positional argument in format string, but no arguments were given + --> $DIR/panic-2021.rs:8:21 + | +LL | assert!(false, "{}"); + | ^^ + +error: aborting due to 5 previous errors + diff --git a/src/test/ui/panics/panic-arg.rs b/src/test/ui/panics/panic-arg.rs new file mode 100644 index 000000000..f7c2dbb09 --- /dev/null +++ b/src/test/ui/panics/panic-arg.rs @@ -0,0 +1,11 @@ +// run-fail +// error-pattern:woe +// ignore-emscripten no processes + +fn f(a: isize) { + println!("{}", a); +} + +fn main() { + f(panic!("woe")); +} diff --git a/src/test/ui/panics/panic-handler-chain-update-hook.rs b/src/test/ui/panics/panic-handler-chain-update-hook.rs new file mode 100644 index 000000000..4dd08ba4a --- /dev/null +++ b/src/test/ui/panics/panic-handler-chain-update-hook.rs @@ -0,0 +1,36 @@ +// run-pass +// needs-unwind +#![allow(stable_features)] + +// ignore-emscripten no threads support + +#![feature(std_panic)] +#![feature(panic_update_hook)] + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::panic; +use std::thread; + +static A: AtomicUsize = AtomicUsize::new(0); +static B: AtomicUsize = AtomicUsize::new(0); +static C: AtomicUsize = AtomicUsize::new(0); + +fn main() { + panic::set_hook(Box::new(|_| { A.fetch_add(1, Ordering::SeqCst); })); + panic::update_hook(|prev, info| { + B.fetch_add(1, Ordering::SeqCst); + prev(info); + }); + panic::update_hook(|prev, info| { + C.fetch_add(1, Ordering::SeqCst); + prev(info); + }); + + let _ = thread::spawn(|| { + panic!(); + }).join(); + + assert_eq!(1, A.load(Ordering::SeqCst)); + assert_eq!(1, B.load(Ordering::SeqCst)); + assert_eq!(1, C.load(Ordering::SeqCst)); +} diff --git a/src/test/ui/panics/panic-handler-chain.rs b/src/test/ui/panics/panic-handler-chain.rs new file mode 100644 index 000000000..73d6e790d --- /dev/null +++ b/src/test/ui/panics/panic-handler-chain.rs @@ -0,0 +1,30 @@ +// run-pass +// needs-unwind +#![allow(stable_features)] + +// ignore-emscripten no threads support + +#![feature(std_panic)] + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::panic; +use std::thread; + +static A: AtomicUsize = AtomicUsize::new(0); +static B: AtomicUsize = AtomicUsize::new(0); + +fn main() { + panic::set_hook(Box::new(|_| { A.fetch_add(1, Ordering::SeqCst); })); + let hook = panic::take_hook(); + panic::set_hook(Box::new(move |info| { + B.fetch_add(1, Ordering::SeqCst); + hook(info); + })); + + let _ = thread::spawn(|| { + panic!(); + }).join(); + + assert_eq!(1, A.load(Ordering::SeqCst)); + assert_eq!(1, B.load(Ordering::SeqCst)); +} diff --git a/src/test/ui/panics/panic-handler-flail-wildly.rs b/src/test/ui/panics/panic-handler-flail-wildly.rs new file mode 100644 index 000000000..679dc7de8 --- /dev/null +++ b/src/test/ui/panics/panic-handler-flail-wildly.rs @@ -0,0 +1,56 @@ +// run-pass +// needs-unwind + +#![allow(stable_features)] +#![allow(unused_must_use)] + +// ignore-emscripten no threads support + +#![feature(std_panic)] + +use std::panic; +use std::thread; + +fn a() { + panic::set_hook(Box::new(|_| println!("hello yes this is a"))); + panic::take_hook(); + panic::set_hook(Box::new(|_| println!("hello yes this is a part 2"))); + panic::take_hook(); +} + +fn b() { + panic::take_hook(); + panic::take_hook(); + panic::take_hook(); + panic::take_hook(); + panic::take_hook(); + panic!(); +} + +fn c() { + panic::set_hook(Box::new(|_| ())); + panic::set_hook(Box::new(|_| ())); + panic::set_hook(Box::new(|_| ())); + panic::set_hook(Box::new(|_| ())); + panic::set_hook(Box::new(|_| ())); + panic::set_hook(Box::new(|_| ())); + panic!(); +} + +fn main() { + for _ in 0..10 { + let mut handles = vec![]; + for _ in 0..10 { + handles.push(thread::spawn(a)); + } + for _ in 0..10 { + handles.push(thread::spawn(b)); + } + for _ in 0..10 { + handles.push(thread::spawn(c)); + } + for handle in handles { + let _ = handle.join(); + } + } +} diff --git a/src/test/ui/panics/panic-handler-set-twice.rs b/src/test/ui/panics/panic-handler-set-twice.rs new file mode 100644 index 000000000..274453020 --- /dev/null +++ b/src/test/ui/panics/panic-handler-set-twice.rs @@ -0,0 +1,25 @@ +// run-pass +// needs-unwind +#![allow(unused_variables)] +#![allow(stable_features)] + +#![feature(std_panic)] + +// ignore-emscripten no threads support + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::panic; +use std::thread; + +static A: AtomicUsize = AtomicUsize::new(0); + +fn main() { + panic::set_hook(Box::new(|_| ())); + panic::set_hook(Box::new(|info| { A.fetch_add(1, Ordering::SeqCst); })); + + let _ = thread::spawn(|| { + panic!(); + }).join(); + + assert_eq!(1, A.load(Ordering::SeqCst)); +} diff --git a/src/test/ui/panics/panic-in-dtor-drops-fields.rs b/src/test/ui/panics/panic-in-dtor-drops-fields.rs new file mode 100644 index 000000000..c0963aa31 --- /dev/null +++ b/src/test/ui/panics/panic-in-dtor-drops-fields.rs @@ -0,0 +1,38 @@ +// run-pass +// needs-unwind +#![allow(dead_code)] +#![allow(non_upper_case_globals)] + +// ignore-emscripten no threads support + +use std::thread; + +static mut dropped: bool = false; + +struct A { + b: B, +} + +struct B { + foo: isize, +} + +impl Drop for A { + fn drop(&mut self) { + panic!() + } +} + +impl Drop for B { + fn drop(&mut self) { + unsafe { dropped = true; } + } +} + +pub fn main() { + let ret = thread::spawn(move|| { + let _a = A { b: B { foo: 3 } }; + }).join(); + assert!(ret.is_err()); + unsafe { assert!(dropped); } +} diff --git a/src/test/ui/panics/panic-macro-any-wrapped.rs b/src/test/ui/panics/panic-macro-any-wrapped.rs new file mode 100644 index 000000000..663bf6713 --- /dev/null +++ b/src/test/ui/panics/panic-macro-any-wrapped.rs @@ -0,0 +1,9 @@ +// run-fail +// error-pattern:panicked at 'Box<dyn Any>' +// ignore-emscripten no processes + +#![allow(non_fmt_panics)] + +fn main() { + panic!(Box::new(612_i64)); +} diff --git a/src/test/ui/panics/panic-macro-any.rs b/src/test/ui/panics/panic-macro-any.rs new file mode 100644 index 000000000..c7df53654 --- /dev/null +++ b/src/test/ui/panics/panic-macro-any.rs @@ -0,0 +1,9 @@ +// run-fail +// error-pattern:panicked at 'Box<dyn Any>' +// ignore-emscripten no processes + +#![allow(non_fmt_panics)] + +fn main() { + panic!(Box::new(413) as Box<dyn std::any::Any + Send>); +} diff --git a/src/test/ui/panics/panic-macro-explicit.rs b/src/test/ui/panics/panic-macro-explicit.rs new file mode 100644 index 000000000..ac4d6f812 --- /dev/null +++ b/src/test/ui/panics/panic-macro-explicit.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:panicked at 'explicit panic' +// ignore-emscripten no processes + +fn main() { + panic!(); +} diff --git a/src/test/ui/panics/panic-macro-fmt.rs b/src/test/ui/panics/panic-macro-fmt.rs new file mode 100644 index 000000000..a755ebc0f --- /dev/null +++ b/src/test/ui/panics/panic-macro-fmt.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:panicked at 'test-fail-fmt 42 rust' +// ignore-emscripten no processes + +fn main() { + panic!("test-fail-fmt {} {}", 42, "rust"); +} diff --git a/src/test/ui/panics/panic-macro-owned.rs b/src/test/ui/panics/panic-macro-owned.rs new file mode 100644 index 000000000..b898fde77 --- /dev/null +++ b/src/test/ui/panics/panic-macro-owned.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:panicked at 'test-fail-owned' +// ignore-emscripten no processes + +fn main() { + panic!("test-fail-owned"); +} diff --git a/src/test/ui/panics/panic-macro-static.rs b/src/test/ui/panics/panic-macro-static.rs new file mode 100644 index 000000000..a1d467cbf --- /dev/null +++ b/src/test/ui/panics/panic-macro-static.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:panicked at 'test-fail-static' +// ignore-emscripten no processes + +fn main() { + panic!("test-fail-static"); +} diff --git a/src/test/ui/panics/panic-main.rs b/src/test/ui/panics/panic-main.rs new file mode 100644 index 000000000..87df7688f --- /dev/null +++ b/src/test/ui/panics/panic-main.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:moop +// ignore-emscripten no processes + +fn main() { + panic!("moop"); +} diff --git a/src/test/ui/panics/panic-parens.rs b/src/test/ui/panics/panic-parens.rs new file mode 100644 index 000000000..59ab54446 --- /dev/null +++ b/src/test/ui/panics/panic-parens.rs @@ -0,0 +1,20 @@ +// Fail macros without arguments need to be disambiguated in +// certain positions + +// run-fail +// error-pattern:oops +// ignore-emscripten no processes + +fn bigpanic() { + while (panic!("oops")) { + if (panic!()) { + match (panic!()) { + () => {} + } + } + } +} + +fn main() { + bigpanic(); +} diff --git a/src/test/ui/panics/panic-recover-propagate.rs b/src/test/ui/panics/panic-recover-propagate.rs new file mode 100644 index 000000000..e110d94b6 --- /dev/null +++ b/src/test/ui/panics/panic-recover-propagate.rs @@ -0,0 +1,27 @@ +// run-pass +// needs-unwind +// ignore-emscripten no threads support + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::panic; +use std::thread; + +static A: AtomicUsize = AtomicUsize::new(0); + +fn main() { + panic::set_hook(Box::new(|_| { + A.fetch_add(1, Ordering::SeqCst); + })); + + let result = thread::spawn(|| { + let result = panic::catch_unwind(|| { + panic!("hi there"); + }); + + panic::resume_unwind(result.unwrap_err()); + }).join(); + + let msg = *result.unwrap_err().downcast::<&'static str>().unwrap(); + assert_eq!("hi there", msg); + assert_eq!(1, A.load(Ordering::SeqCst)); +} diff --git a/src/test/ui/panics/panic-set-handler.rs b/src/test/ui/panics/panic-set-handler.rs new file mode 100644 index 000000000..3c00183e2 --- /dev/null +++ b/src/test/ui/panics/panic-set-handler.rs @@ -0,0 +1,12 @@ +// run-fail +// error-pattern:greetings from the panic handler +// ignore-emscripten no processes + +use std::panic; + +fn main() { + panic::set_hook(Box::new(|i| { + eprintln!("greetings from the panic handler"); + })); + panic!("foobar"); +} diff --git a/src/test/ui/panics/panic-set-unset-handler.rs b/src/test/ui/panics/panic-set-unset-handler.rs new file mode 100644 index 000000000..dde0c72f7 --- /dev/null +++ b/src/test/ui/panics/panic-set-unset-handler.rs @@ -0,0 +1,13 @@ +// run-fail +// error-pattern:thread 'main' panicked at 'foobar' +// ignore-emscripten no processes + +use std::panic; + +fn main() { + panic::set_hook(Box::new(|i| { + eprint!("greetings from the panic handler"); + })); + panic::take_hook(); + panic!("foobar"); +} diff --git a/src/test/ui/panics/panic-short-backtrace-windows-x86_64.rs b/src/test/ui/panics/panic-short-backtrace-windows-x86_64.rs new file mode 100644 index 000000000..39ffe86dd --- /dev/null +++ b/src/test/ui/panics/panic-short-backtrace-windows-x86_64.rs @@ -0,0 +1,53 @@ +// This test has been spuriously failing a lot recently (#92000). +// Ignore it until the underlying issue is fixed. +// ignore-test + +// Regression test for #87481: short backtrace formatting cut off the entire stack trace. + +// Codegen-units is specified here so that we can replicate a typical rustc invocation which +// is not normally limited to 1 CGU. This is important so that the `__rust_begin_short_backtrace` +// and `__rust_end_short_backtrace` symbols are not marked internal to the CGU and thus will be +// named in the symbol table. +// compile-flags: -O -Ccodegen-units=8 + +// run-fail +// check-run-results +// exec-env:RUST_BACKTRACE=1 + +// We need to normalize out frame 5 because without debug info, dbghelp.dll doesn't know where CGU +// internal functions like `main` start or end and so it will return whatever symbol happens +// to be located near the address. +// normalize-stderr-test: "5: .*" -> "5: some Rust fn" + +// Backtraces are pretty broken in general on i686-pc-windows-msvc (#62897). +// only-x86_64-pc-windows-msvc + +fn main() { + a(); +} + +// Make these no_mangle so dbghelp.dll can figure out the symbol names. + +#[no_mangle] +#[inline(never)] +fn a() { + b(); +} + +#[no_mangle] +#[inline(never)] +fn b() { + c(); +} + +#[no_mangle] +#[inline(never)] +fn c() { + d(); +} + +#[no_mangle] +#[inline(never)] +fn d() { + panic!("d was called"); +} diff --git a/src/test/ui/panics/panic-short-backtrace-windows-x86_64.run.stderr b/src/test/ui/panics/panic-short-backtrace-windows-x86_64.run.stderr new file mode 100644 index 000000000..799a8b30e --- /dev/null +++ b/src/test/ui/panics/panic-short-backtrace-windows-x86_64.run.stderr @@ -0,0 +1,9 @@ +thread 'main' panicked at 'd was called', $DIR/panic-short-backtrace-windows-x86_64.rs:48:5 +stack backtrace: + 0: std::panicking::begin_panic + 1: d + 2: c + 3: b + 4: a + 5: some Rust fn +note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/src/test/ui/panics/panic-take-handler-nop.rs b/src/test/ui/panics/panic-take-handler-nop.rs new file mode 100644 index 000000000..41cbac97c --- /dev/null +++ b/src/test/ui/panics/panic-take-handler-nop.rs @@ -0,0 +1,10 @@ +// run-fail +// error-pattern:thread 'main' panicked at 'foobar' +// ignore-emscripten no processes + +use std::panic; + +fn main() { + panic::take_hook(); + panic!("foobar"); +} diff --git a/src/test/ui/panics/panic-task-name-none.rs b/src/test/ui/panics/panic-task-name-none.rs new file mode 100644 index 000000000..4e95fb5bd --- /dev/null +++ b/src/test/ui/panics/panic-task-name-none.rs @@ -0,0 +1,13 @@ +// run-fail +// error-pattern:thread '<unnamed>' panicked at 'test' +// ignore-emscripten Needs threads + +use std::thread; + +fn main() { + let r: Result<(), _> = thread::spawn(move || { + panic!("test"); + }) + .join(); + assert!(r.is_ok()); +} diff --git a/src/test/ui/panics/panic-task-name-owned.rs b/src/test/ui/panics/panic-task-name-owned.rs new file mode 100644 index 000000000..f85be7bb8 --- /dev/null +++ b/src/test/ui/panics/panic-task-name-owned.rs @@ -0,0 +1,18 @@ +// run-fail +// error-pattern:thread 'owned name' panicked at 'test' +// ignore-emscripten Needs threads. + +use std::thread::Builder; + +fn main() { + let r: () = Builder::new() + .name("owned name".to_string()) + .spawn(move || { + panic!("test"); + () + }) + .unwrap() + .join() + .unwrap(); + panic!(); +} diff --git a/src/test/ui/panics/panic.rs b/src/test/ui/panics/panic.rs new file mode 100644 index 000000000..b6227a582 --- /dev/null +++ b/src/test/ui/panics/panic.rs @@ -0,0 +1,7 @@ +// run-fail +// error-pattern:1 == 2 +// ignore-emscripten no processes + +fn main() { + assert!(1 == 2); +} diff --git a/src/test/ui/panics/result-get-panic.rs b/src/test/ui/panics/result-get-panic.rs new file mode 100644 index 000000000..461f30b91 --- /dev/null +++ b/src/test/ui/panics/result-get-panic.rs @@ -0,0 +1,9 @@ +// run-fail +// error-pattern:called `Result::unwrap()` on an `Err` value +// ignore-emscripten no processes + +use std::result::Result::Err; + +fn main() { + println!("{}", Err::<isize, String>("kitty".to_string()).unwrap()); +} diff --git a/src/test/ui/panics/runtime-switch.legacy.run.stderr b/src/test/ui/panics/runtime-switch.legacy.run.stderr new file mode 100644 index 000000000..f282f1883 --- /dev/null +++ b/src/test/ui/panics/runtime-switch.legacy.run.stderr @@ -0,0 +1,5 @@ +thread 'main' panicked at 'explicit panic', $DIR/runtime-switch.rs:25:5 +stack backtrace: + 0: std::panicking::begin_panic + 1: runtime_switch::main +note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/src/test/ui/panics/runtime-switch.rs b/src/test/ui/panics/runtime-switch.rs new file mode 100644 index 000000000..37ef961e2 --- /dev/null +++ b/src/test/ui/panics/runtime-switch.rs @@ -0,0 +1,26 @@ +// Test for std::panic::set_backtrace_style. + +// compile-flags: -O +// compile-flags:-Cstrip=none +// run-fail +// check-run-results +// exec-env:RUST_BACKTRACE=0 + +// ignore-msvc see #62897 and `backtrace-debuginfo.rs` test +// ignore-android FIXME #17520 +// ignore-openbsd no support for libbacktrace without filename +// ignore-wasm no panic or subprocess support +// ignore-emscripten no panic or subprocess support +// ignore-sgx no subprocess support + +// NOTE(eddyb) output differs between symbol mangling schemes +// revisions: legacy v0 +// [legacy] compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy +// [v0] compile-flags: -Csymbol-mangling-version=v0 + +#![feature(panic_backtrace_config)] + +fn main() { + std::panic::set_backtrace_style(std::panic::BacktraceStyle::Short); + panic!() +} diff --git a/src/test/ui/panics/runtime-switch.v0.run.stderr b/src/test/ui/panics/runtime-switch.v0.run.stderr new file mode 100644 index 000000000..7ce9722e5 --- /dev/null +++ b/src/test/ui/panics/runtime-switch.v0.run.stderr @@ -0,0 +1,5 @@ +thread 'main' panicked at 'explicit panic', $DIR/runtime-switch.rs:25:5 +stack backtrace: + 0: std::panicking::begin_panic::<&str> + 1: runtime_switch::main +note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. diff --git a/src/test/ui/panics/test-panic.rs b/src/test/ui/panics/test-panic.rs new file mode 100644 index 000000000..85c9279cd --- /dev/null +++ b/src/test/ui/panics/test-panic.rs @@ -0,0 +1,9 @@ +// run-fail +// check-stdout +// compile-flags: --test +// ignore-emscripten + +#[test] +fn test_foo() { + panic!() +} diff --git a/src/test/ui/panics/test-should-fail-bad-message.rs b/src/test/ui/panics/test-should-fail-bad-message.rs new file mode 100644 index 000000000..701f26776 --- /dev/null +++ b/src/test/ui/panics/test-should-fail-bad-message.rs @@ -0,0 +1,10 @@ +// run-fail +// check-stdout +// compile-flags: --test +// ignore-emscripten + +#[test] +#[should_panic(expected = "foobar")] +fn test_foo() { + panic!("blah") +} diff --git a/src/test/ui/panics/test-should-panic-bad-message.rs b/src/test/ui/panics/test-should-panic-bad-message.rs new file mode 100644 index 000000000..a82c4e144 --- /dev/null +++ b/src/test/ui/panics/test-should-panic-bad-message.rs @@ -0,0 +1,10 @@ +// run-fail +// compile-flags: --test +// check-stdout +// ignore-emscripten no processes + +#[test] +#[should_panic(expected = "foo")] +pub fn test_bar() { + panic!("bar") +} diff --git a/src/test/ui/panics/test-should-panic-no-message.rs b/src/test/ui/panics/test-should-panic-no-message.rs new file mode 100644 index 000000000..13f67a41c --- /dev/null +++ b/src/test/ui/panics/test-should-panic-no-message.rs @@ -0,0 +1,10 @@ +// run-fail +// compile-flags: --test +// check-stdout +// ignore-emscripten no processes + +#[test] +#[should_panic(expected = "foo")] +pub fn test_explicit() { + panic!() +} diff --git a/src/test/ui/panics/unique-panic.rs b/src/test/ui/panics/unique-panic.rs new file mode 100644 index 000000000..ae7911e59 --- /dev/null +++ b/src/test/ui/panics/unique-panic.rs @@ -0,0 +1,10 @@ +// run-fail +// error-pattern: panic +// for some reason, fails to match error string on +// wasm32-unknown-unknown with stripped debuginfo and symbols, +// so don't strip it +// compile-flags:-Cstrip=none + +fn main() { + Box::new(panic!()); +} diff --git a/src/test/ui/panics/while-body-panics.rs b/src/test/ui/panics/while-body-panics.rs new file mode 100644 index 000000000..2c05eb389 --- /dev/null +++ b/src/test/ui/panics/while-body-panics.rs @@ -0,0 +1,14 @@ +#![allow(while_true)] + +// run-fail +// error-pattern:quux +// ignore-emscripten no processes + +fn main() { + let _x: isize = { + while true { + panic!("quux"); + } + 8 + }; +} diff --git a/src/test/ui/panics/while-panic.rs b/src/test/ui/panics/while-panic.rs new file mode 100644 index 000000000..3c6ee8fa3 --- /dev/null +++ b/src/test/ui/panics/while-panic.rs @@ -0,0 +1,14 @@ +#![allow(while_true)] + +// run-fail +// error-pattern:giraffe +// ignore-emscripten no processes + +fn main() { + panic!("{}", { + while true { + panic!("giraffe") + } + "clandestine" + }); +} |