]> git.lizzy.rs Git - rust.git/commitdiff
Prevent unwinding when `-C panic=abort` is used regardless declared ABI
authorGary Guo <gary@garyguo.net>
Wed, 11 May 2022 22:08:02 +0000 (23:08 +0100)
committerGary Guo <gary@garyguo.net>
Wed, 11 May 2022 23:03:48 +0000 (00:03 +0100)
compiler/rustc_middle/src/ty/layout.rs
src/test/codegen/unwind-abis/c-unwind-abi-panic-abort.rs

index c8055100d30968a5c1c9f0df09fa9d5400b72f38..630a89cc7cbfcef117aa1f6d11b6f7655a7a8dbe 100644 (file)
@@ -2888,6 +2888,14 @@ pub fn fn_can_unwind<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: Option<DefId>, abi: Spe
             return false;
         }
 
+        // With `-C panic=abort`, all non-FFI functions are required to not unwind.
+        //
+        // Note that this is true regardless ABI specified on the function -- a `extern "C-unwind"`
+        // function defined in Rust is also required to abort.
+        if tcx.sess.panic_strategy() == PanicStrategy::Abort && !tcx.is_foreign_item(did) {
+            return false;
+        }
+
         // With -Z panic-in-drop=abort, drop_in_place never unwinds.
         //
         // This is not part of `codegen_fn_attrs` as it can differ between crates
index 398937a04c9236bfade8d7defb02ae9858665700..5334a1c004dbb916062e600c7f0d3ebd086b18a4 100644 (file)
@@ -1,7 +1,7 @@
 // compile-flags: -C panic=abort
 
-// Test that `nounwind` atributes are not applied to `C-unwind` extern functions
-// even when the code is compiled with `panic=abort`.
+// Test that `nounwind` atributes are also applied to extern `C-unwind` Rust functions
+// when the code is compiled with `panic=abort`.
 
 #![crate_type = "lib"]
 #![feature(c_unwind)]
@@ -19,4 +19,4 @@
 // Now, make sure that the LLVM attributes for this functions are correct.  First, make
 // sure that the first item is correctly marked with the `nounwind` attribute:
 //
-// CHECK-NOT: attributes #0 = { {{.*}}nounwind{{.*}} }
+// CHECK: attributes #0 = { {{.*}}nounwind{{.*}} }