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