]> git.lizzy.rs Git - rust.git/blob - tests/codegen/panic-in-drop-abort.rs
Rollup merge of #107700 - jyn514:tools-builder, r=Mark-Simulacrum
[rust.git] / tests / 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.
10
11 // CHECK-NOT: {{(call|invoke).*}}should_not_appear_in_output
12
13 #![crate_type = "lib"]
14 use std::any::Any;
15 use std::mem::forget;
16
17 pub struct ExternDrop;
18 impl Drop for ExternDrop {
19     #[inline(always)]
20     fn drop(&mut self) {
21         // This call may potentially unwind.
22         extern "Rust" {
23             fn extern_drop();
24         }
25         unsafe {
26             extern_drop();
27         }
28     }
29 }
30
31 struct AssertNeverDrop;
32 impl Drop for AssertNeverDrop {
33     #[inline(always)]
34     fn drop(&mut self) {
35         // This call should be optimized away as unreachable.
36         extern "C" {
37             fn should_not_appear_in_output();
38         }
39         unsafe {
40             should_not_appear_in_output();
41         }
42     }
43 }
44
45 #[no_mangle]
46 pub fn normal_drop(x: ExternDrop) {
47     let guard = AssertNeverDrop;
48     drop(x);
49     forget(guard);
50 }
51
52 #[no_mangle]
53 pub fn indirect_drop(x: Box<dyn Any>) {
54     let guard = AssertNeverDrop;
55     drop(x);
56     forget(guard);
57 }