]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/panic-in-drop-abort.rs
7b9c41b658ae9d6e68ffd1abee8f22625addd398
[rust.git] / src / test / codegen / panic-in-drop-abort.rs
1 // compile-flags: -Z panic-in-drop=abort -O
2 // ignore-msvc
3
4 // Ensure that unwinding code paths are eliminated from the output after
5 // optimization.
6
7 // This test uses ignore-msvc, because the expected optimization does not happen on targets using
8 // SEH exceptions with the new LLVM pass manager anymore, see
9 // https://github.com/llvm/llvm-project/issues/51311. The core issue is that Rust promises that
10 // the drop_in_place() function can't unwind, but implements it in a way that *can*, because we
11 // currently go out of our way to allow longjmps, which also use the unwinding mechanism on MSVC
12 // targets. We should either forbid longjmps, or not assume nounwind, making this optimization
13 // incompatible with the current behavior of running cleanuppads on longjmp unwinding.
14
15 // CHECK-NOT: {{(call|invoke).*}}should_not_appear_in_output
16
17 #![crate_type = "lib"]
18 use std::any::Any;
19 use std::mem::forget;
20
21 pub struct ExternDrop;
22 impl Drop for ExternDrop {
23     #[inline(always)]
24     fn drop(&mut self) {
25         // This call may potentially unwind.
26         extern "Rust" {
27             fn extern_drop();
28         }
29         unsafe {
30             extern_drop();
31         }
32     }
33 }
34
35 struct AssertNeverDrop;
36 impl Drop for AssertNeverDrop {
37     #[inline(always)]
38     fn drop(&mut self) {
39         // This call should be optimized away as unreachable.
40         extern "C" {
41             fn should_not_appear_in_output();
42         }
43         unsafe {
44             should_not_appear_in_output();
45         }
46     }
47 }
48
49 #[no_mangle]
50 pub fn normal_drop(x: ExternDrop) {
51     let guard = AssertNeverDrop;
52     drop(x);
53     forget(guard);
54 }
55
56 #[no_mangle]
57 pub fn indirect_drop(x: Box<dyn Any>) {
58     let guard = AssertNeverDrop;
59     drop(x);
60     forget(guard);
61 }