Skip to content

Commit 2b9b2b6

Browse files
committed
Auto merge of rust-lang#116088 - nbdd0121:unwind, r=<try>
Stabilise `c_unwind` Fix rust-lang#74990 Fix rust-lang#115285 Marking as draft PR for now due to `compiler_builtins` issues r? `@Amanieu`
2 parents 42dfac5 + cff15e8 commit 2b9b2b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+40
-144
lines changed

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ declare_features! (
7979
(accepted, braced_empty_structs, "1.8.0", Some(29720), None),
8080
/// Allows `c"foo"` literals.
8181
(accepted, c_str_literals, "CURRENT_RUSTC_VERSION", Some(105723), None),
82+
/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries and treat `extern "C" fn` as nounwind.
83+
(accepted, c_unwind, "CURRENT_RUSTC_VERSION", Some(74990), None),
8284
/// Allows `#[cfg_attr(predicate, multiple, attributes, here)]`.
8385
(accepted, cfg_attr_multi, "1.33.0", Some(54881), None),
8486
/// Allows the use of `#[cfg(doctest)]`, set when rustdoc is collecting doctests.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,6 @@ declare_features! (
362362
(unstable, async_fn_track_caller, "1.73.0", Some(110011), None),
363363
/// Allows builtin # foo() syntax
364364
(unstable, builtin_syntax, "1.71.0", Some(110680), None),
365-
/// Treat `extern "C"` function as nounwind.
366-
(unstable, c_unwind, "1.52.0", Some(74990), None),
367365
/// Allows using C-variadics.
368366
(unstable, c_variadic, "1.34.0", Some(44930), None),
369367
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.

compiler/rustc_middle/src/ty/layout.rs

+1-35
Original file line numberDiff line numberDiff line change
@@ -1197,37 +1197,6 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) ->
11971197
// ABIs have such an option. Otherwise the only other thing here is Rust
11981198
// itself, and those ABIs are determined by the panic strategy configured
11991199
// for this compilation.
1200-
//
1201-
// Unfortunately at this time there's also another caveat. Rust [RFC
1202-
// 2945][rfc] has been accepted and is in the process of being implemented
1203-
// and stabilized. In this interim state we need to deal with historical
1204-
// rustc behavior as well as plan for future rustc behavior.
1205-
//
1206-
// Historically functions declared with `extern "C"` were marked at the
1207-
// codegen layer as `nounwind`. This happened regardless of `panic=unwind`
1208-
// or not. This is UB for functions in `panic=unwind` mode that then
1209-
// actually panic and unwind. Note that this behavior is true for both
1210-
// externally declared functions as well as Rust-defined function.
1211-
//
1212-
// To fix this UB rustc would like to change in the future to catch unwinds
1213-
// from function calls that may unwind within a Rust-defined `extern "C"`
1214-
// function and forcibly abort the process, thereby respecting the
1215-
// `nounwind` attribute emitted for `extern "C"`. This behavior change isn't
1216-
// ready to roll out, so determining whether or not the `C` family of ABIs
1217-
// unwinds is conditional not only on their definition but also whether the
1218-
// `#![feature(c_unwind)]` feature gate is active.
1219-
//
1220-
// Note that this means that unlike historical compilers rustc now, by
1221-
// default, unconditionally thinks that the `C` ABI may unwind. This will
1222-
// prevent some optimization opportunities, however, so we try to scope this
1223-
// change and only assume that `C` unwinds with `panic=unwind` (as opposed
1224-
// to `panic=abort`).
1225-
//
1226-
// Eventually the check against `c_unwind` here will ideally get removed and
1227-
// this'll be a little cleaner as it'll be a straightforward check of the
1228-
// ABI.
1229-
//
1230-
// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md
12311200
use SpecAbi::*;
12321201
match abi {
12331202
C { unwind }
@@ -1239,10 +1208,7 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) ->
12391208
| Thiscall { unwind }
12401209
| Aapcs { unwind }
12411210
| Win64 { unwind }
1242-
| SysV64 { unwind } => {
1243-
unwind
1244-
|| (!tcx.features().c_unwind && tcx.sess.panic_strategy() == PanicStrategy::Unwind)
1245-
}
1211+
| SysV64 { unwind } => unwind,
12461212
PtxKernel
12471213
| Msp430Interrupt
12481214
| X86Interrupt

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@
167167
//
168168
// Language features:
169169
// tidy-alphabetical-start
170+
#![cfg_attr(bootstrap, feature(c_unwind))]
170171
#![cfg_attr(not(test), feature(coroutine_trait))]
171172
#![cfg_attr(test, feature(panic_update_hook))]
172173
#![cfg_attr(test, feature(test))]
173174
#![feature(allocator_internals)]
174175
#![feature(allow_internal_unstable)]
175176
#![feature(associated_type_bounds)]
176-
#![feature(c_unwind)]
177177
#![feature(cfg_sanitize)]
178178
#![feature(const_mut_refs)]
179179
#![feature(const_precise_live_drops)]

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@
198198
//
199199
// Language features:
200200
// tidy-alphabetical-start
201+
#![cfg_attr(bootstrap, feature(c_unwind))]
201202
#![feature(abi_unadjusted)]
202203
#![feature(adt_const_params)]
203204
#![feature(allow_internal_unsafe)]
204205
#![feature(allow_internal_unstable)]
205206
#![feature(asm_const)]
206207
#![feature(associated_type_bounds)]
207208
#![feature(auto_traits)]
208-
#![feature(c_unwind)]
209209
#![feature(cfg_sanitize)]
210210
#![feature(cfg_target_has_atomic)]
211211
#![feature(cfg_target_has_atomic_equal_alignment)]

library/panic_abort/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![feature(std_internals)]
1414
#![feature(staged_api)]
1515
#![feature(rustc_attrs)]
16-
#![feature(c_unwind)]
16+
#![cfg_attr(bootstrap, feature(c_unwind))]
1717
#![allow(internal_features)]
1818

1919
#[cfg(target_os = "android")]

library/panic_unwind/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#![feature(rustc_attrs)]
2323
#![panic_runtime]
2424
#![feature(panic_runtime)]
25-
#![feature(c_unwind)]
25+
#![cfg_attr(bootstrap, feature(c_unwind))]
2626
// `real_imp` is unused with Miri, so silence warnings.
2727
#![cfg_attr(miri, allow(dead_code))]
2828
#![allow(internal_features)]

library/proc_macro/src/bridge/buffer.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ pub struct Buffer {
1010
data: *mut u8,
1111
len: usize,
1212
capacity: usize,
13-
reserve: extern "C" fn(Buffer, usize) -> Buffer,
14-
drop: extern "C" fn(Buffer),
13+
// HACK(nbdd0121): These should be `extern "C"` but LLVM < 17.0.4 will misoptimise if we do so.
14+
// Remove this when we drop support for them or when we require patches to be present.
15+
reserve: extern "C-unwind" fn(Buffer, usize) -> Buffer,
16+
drop: extern "C-unwind" fn(Buffer),
1517
}
1618

1719
unsafe impl Sync for Buffer {}
@@ -141,13 +143,13 @@ impl From<Vec<u8>> for Buffer {
141143
}
142144
}
143145

144-
extern "C" fn reserve(b: Buffer, additional: usize) -> Buffer {
146+
extern "C-unwind" fn reserve(b: Buffer, additional: usize) -> Buffer {
145147
let mut v = to_vec(b);
146148
v.reserve(additional);
147149
Buffer::from(v)
148150
}
149151

150-
extern "C" fn drop(b: Buffer) {
152+
extern "C-unwind" fn drop(b: Buffer) {
151153
mem::drop(to_vec(b));
152154
}
153155

library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@
265265
//
266266
// Language features:
267267
// tidy-alphabetical-start
268+
#![cfg_attr(bootstrap, feature(c_unwind))]
268269
#![feature(alloc_error_handler)]
269270
#![feature(allocator_internals)]
270271
#![feature(allow_internal_unsafe)]
271272
#![feature(allow_internal_unstable)]
272-
#![feature(c_unwind)]
273273
#![feature(cfg_target_thread_local)]
274274
#![feature(cfi_encoding)]
275275
#![feature(concat_idents)]

library/unwind/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![unstable(feature = "panic_unwind", issue = "32837")]
33
#![feature(link_cfg)]
44
#![feature(staged_api)]
5-
#![feature(c_unwind)]
5+
#![cfg_attr(bootstrap, feature(c_unwind))]
66
#![feature(cfg_target_abi)]
77
#![feature(strict_provenance)]
88
#![cfg_attr(not(target_env = "msvc"), feature(libc))]

src/doc/unstable-book/src/language-features/c-unwind.md

-26
This file was deleted.

src/tools/miri/tests/fail-dep/concurrency/unwind_top_of_stack.rs

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
//! Unwinding past the top frame of a stack is Undefined Behavior.
66
7-
#![feature(c_unwind)]
8-
97
use std::{mem, ptr};
108

119
extern "C-unwind" fn thread_start(_null: *mut libc::c_void) -> *mut libc::c_void {

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@compile-flags: -Zmiri-disable-abi-check
2-
#![feature(c_unwind)]
32

43
#[no_mangle]
54
extern "C-unwind" fn unwind() {

src/tools/miri/tests/fail/function_calls/exported_symbol_bad_unwind2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
55
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
66
//@[definition,both]error-in-other-file: aborted execution
7-
#![feature(rustc_attrs, c_unwind)]
7+
#![feature(rustc_attrs)]
88

99
#[cfg_attr(any(definition, both), rustc_nounwind)]
1010
#[no_mangle]

src/tools/miri/tests/fail/panic/bad_miri_start_panic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@compile-flags: -Zmiri-disable-abi-check
22
// This feature is required to trigger the error using the "C" ABI.
3-
#![feature(c_unwind)]
43

54
extern "C" {
65
fn miri_start_panic(payload: *mut u8) -> !;

src/tools/miri/tests/fail/panic/bad_unwind.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(c_unwind)]
2-
31
//! Unwinding when the caller ABI is "C" (without "-unwind") is UB.
42
53
extern "C-unwind" fn unwind() {

src/tools/miri/tests/fail/terminate-terminator.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// Enable MIR inlining to ensure that `TerminatorKind::UnwindTerminate` is generated
88
// instead of just `UnwindAction::Terminate`.
99

10-
#![feature(c_unwind)]
11-
1210
struct Foo;
1311

1412
impl Drop for Foo {

src/tools/miri/tests/fail/unwind-action-terminate.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//@normalize-stderr-test: "\| +\^+" -> "| ^"
44
//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> ""
55
//@normalize-stderr-test: "\n +at [^\n]+" -> ""
6-
#![feature(c_unwind)]
7-
86
extern "C" fn panic_abort() {
97
panic!()
108
}

src/tools/miri/tests/panic/function_calls/exported_symbol_good_unwind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// found in this form" errors works without `-C prefer-dynamic` (`panic!` calls foreign function
33
// `__rust_start_panic`).
44
// no-prefer-dynamic
5-
#![feature(c_unwind, unboxed_closures)]
5+
#![feature(unboxed_closures)]
66

77
use std::panic;
88

tests/assembly/asm/aarch64-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// assembly-output: emit-asm
2-
// compile-flags: -O
2+
// compile-flags: -O -C panic=abort
33
// compile-flags: --target aarch64-unknown-linux-gnu
44
// needs-llvm-components: aarch64
55

tests/assembly/asm/arm-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// assembly-output: emit-asm
2-
// compile-flags: -O
2+
// compile-flags: -O -C panic=abort
33
// compile-flags: --target armv7-unknown-linux-gnueabihf
44
// compile-flags: -C target-feature=+neon
55
// needs-llvm-components: arm

tests/assembly/asm/x86-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// revisions: x86_64 i686
22
// assembly-output: emit-asm
3-
// compile-flags: -O
3+
// compile-flags: -O -C panic=abort
44
//[x86_64] compile-flags: --target x86_64-unknown-linux-gnu
55
//[x86_64] needs-llvm-components: x86
66
//[i686] compile-flags: --target i686-unknown-linux-gnu

tests/codegen/avr/avr-func-addrspace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
1+
// compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib -C panic=abort
22
// needs-llvm-components: avr
33

44
// This test validates that function pointers can be stored in global variables

tests/codegen/catch-unwind.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// ignore-loongarch64 FIXME
1515

1616
#![crate_type = "lib"]
17-
#![feature(c_unwind)]
1817

1918
extern "C" {
2019
fn bar();

tests/codegen/cffi/c-variadic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#![crate_type = "lib"]
66
#![feature(c_variadic)]
7-
#![feature(c_unwind)]
87
#![no_std]
98
use core::ffi::VaList;
109

tests/codegen/debuginfo-inline-callsite-location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#![crate_type = "lib"]
2121

2222
#[no_mangle]
23-
extern "C" fn add_numbers(x: &Option<i32>, y: &Option<i32>) -> i32 {
23+
extern "C-unwind" fn add_numbers(x: &Option<i32>, y: &Option<i32>) -> i32 {
2424
let x1 = x.unwrap();
2525
let y1 = y.unwrap();
2626

tests/codegen/unwind-abis/aapcs-unwind-abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// needs-llvm-components: arm
22
// compile-flags: --target=armv7-unknown-linux-gnueabihf --crate-type=rlib -Cno-prepopulate-passes
33
#![no_core]
4-
#![feature(no_core, lang_items, c_unwind)]
4+
#![feature(no_core, lang_items)]
55
#[lang="sized"]
66
trait Sized { }
77

tests/codegen/unwind-abis/c-unwind-abi-panic-abort.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// when the code is compiled with `panic=abort`.
55

66
#![crate_type = "lib"]
7-
#![feature(c_unwind)]
87

98
// CHECK: @rust_item_that_can_unwind() unnamed_addr [[ATTR0:#[0-9]+]]
109
#[no_mangle]

tests/codegen/unwind-abis/c-unwind-abi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// to prevent LLVM from inferring the attribute.
77

88
#![crate_type = "lib"]
9-
#![feature(c_unwind)]
109

1110
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
1211
#[no_mangle]

tests/codegen/unwind-abis/cdecl-unwind-abi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// disable optimizations above to prevent LLVM from inferring the attribute.
77

88
#![crate_type = "lib"]
9-
#![feature(c_unwind)]
109

1110
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
1211
#[no_mangle]

tests/codegen/unwind-abis/fastcall-unwind-abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// needs-llvm-components: x86
22
// compile-flags: --target=i686-pc-windows-msvc --crate-type=rlib -Cno-prepopulate-passes
33
#![no_core]
4-
#![feature(no_core, lang_items, c_unwind)]
4+
#![feature(no_core, lang_items)]
55
#[lang="sized"]
66
trait Sized { }
77

tests/codegen/unwind-abis/nounwind-on-stable-panic-unwind.rs

-18
This file was deleted.

tests/codegen/unwind-abis/nounwind.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// ignore-wasm32-bare compiled with panic=abort by default
33

44
#![crate_type = "lib"]
5-
#![feature(c_unwind)]
65

76
// We disable optimizations to prevent LLVM from inferring the attribute.
87

tests/codegen/unwind-abis/stdcall-unwind-abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// needs-llvm-components: x86
22
// compile-flags: --target=i686-pc-windows-msvc --crate-type=rlib -Cno-prepopulate-passes
33
#![no_core]
4-
#![feature(no_core, lang_items, c_unwind)]
4+
#![feature(no_core, lang_items)]
55
#[lang="sized"]
66
trait Sized { }
77

tests/codegen/unwind-abis/system-unwind-abi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// optimizations above to prevent LLVM from inferring the attribute.
77

88
#![crate_type = "lib"]
9-
#![feature(c_unwind)]
109

1110
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0 {
1211
#[no_mangle]

0 commit comments

Comments
 (0)