Skip to content

Commit bbf81ac

Browse files
committed
Allow using C-unwind in stable Rust
Improves the situation in #539.
1 parent 60a655d commit bbf81ac

File tree

8 files changed

+22
-27
lines changed

8 files changed

+22
-27
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ jobs:
182182
key: cargo-${{ github.job }}-${{ matrix.name }}-${{ hashFiles('**/Cargo.lock') }}
183183

184184
- name: cargo check
185-
run: cargo check $PUBLIC_CRATES $FRAMEWORKS_MACOS_14 $INTERESTING_FEATURES
185+
run: cargo check $PUBLIC_CRATES $FRAMEWORKS_MACOS_14 --features=all
186186
env:
187187
RUSTFLAGS: "--codegen=debuginfo=0" # Removed --deny=warnings
188188

crates/objc-sys/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ unstable-winobjc = ["gnustep-1-8"]
5454
# Link to ObjFW
5555
unstable-objfw = []
5656

57-
# Use nightly c_unwind feature
57+
# Uses `extern "C-unwind"` on relevant function declarations.
58+
#
59+
# This raises MSRV to `1.71`.
60+
#
61+
# Warning: Enabling this is a breaking change for consumer crates, as it
62+
# changes the signature of functions.
5863
unstable-c-unwind = []
5964

6065
# Private
@@ -65,6 +70,7 @@ cc = { version = "1.0.80", optional = true }
6570

6671
[package.metadata.docs.rs]
6772
default-target = "aarch64-apple-darwin"
73+
features = ["unstable-c-unwind"]
6874
targets = [
6975
"aarch64-apple-darwin",
7076
"x86_64-apple-darwin",

crates/objc-sys/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@
170170
#![allow(non_snake_case)]
171171
#![allow(missing_debug_implementations)]
172172
#![doc(html_root_url = "https://docs.rs/objc-sys/0.3.5")]
173-
#![cfg_attr(feature = "unstable-c-unwind", feature(c_unwind))]
174173
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide))]
175-
#![cfg_attr(docsrs, doc(cfg_hide(doc)))]
174+
#![cfg_attr(docsrs, doc(cfg_hide(doc, feature = "unstable-c-unwind")))]
176175

177176
// TODO: Remove this and add "no-std" category to Cargo.toml
178177
// Requires a better solution for C-types in `no_std` crates.
@@ -213,10 +212,6 @@ macro_rules! generate_linking_tests {
213212
// Get function pointer to make the linker require the
214213
// symbol to be available.
215214
let f: unsafe extern $abi fn($($(#[$a_m])* $t),*) $(-> $r)? = crate::$name;
216-
// Workaround for https://github.com/rust-lang/rust/pull/92964
217-
#[cfg(feature = "unstable-c-unwind")]
218-
#[allow(clippy::useless_transmute)]
219-
let f: unsafe extern "C" fn() = unsafe { core::mem::transmute(f) };
220215
// Execute side-effect to ensure it is not optimized away.
221216
std::println!("{:p}", f);
222217
}

crates/objc2/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ unstable-static-class-inlined = ["unstable-static-class"]
7676
# Uses nightly features to make autorelease pools fully sound
7777
unstable-autoreleasesafe = []
7878

79-
# Uses the nightly c_unwind feature to make throwing safe
79+
# Uses `extern "C-unwind"` to make method calls that throw safe.
80+
#
81+
# This raises MSRV to `1.71`.
8082
#
8183
# You must manually enable `objc-sys/unstable-c-unwind` to use this.
8284
unstable-c-unwind = []
@@ -137,7 +139,7 @@ harness = false
137139

138140
[package.metadata.docs.rs]
139141
default-target = "aarch64-apple-darwin"
140-
features = ["exception"]
142+
features = ["unstable-c-unwind", "exception"]
141143
targets = [
142144
"aarch64-apple-darwin",
143145
"x86_64-apple-darwin",

crates/objc2/src/__macro_helpers/common_selectors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn cxx_construct_sel() -> Sel {
5656
/// take a selector, unlike every other Objective-C method, see:
5757
/// <https://github.com/apple-oss-distributions/objc4/blob/objc4-906/runtime/objc-class.mm#L457>
5858
///
59-
/// So the signature is `extern "C" fn(*mut AnyObject)`.
59+
/// So the signature is `extern "C-unwind" fn(*mut AnyObject)`.
6060
///
6161
/// This is likely because it's not a real Objective-C method that can be
6262
/// called from userspace / objc_msgSend, and it's more efficient to not pass
@@ -67,8 +67,6 @@ fn cxx_construct_sel() -> Sel {
6767
/// convention, where such an ignored parameter would be allowed on all
6868
/// relevant architectures.
6969
///
70-
/// TODO: Unsure whether "C-unwind" is allowed?
71-
///
7270
/// [gcc-docs]: https://gcc.gnu.org/onlinedocs/gcc/Objective-C-and-Objective-C_002b_002b-Dialect-Options.html#index-fobjc-call-cxx-cdtors
7371
#[inline]
7472
#[allow(dead_code)] // May be useful in the future

crates/objc2/src/exception.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,11 @@ impl RefUnwindSafe for Exception {}
172172
/// Objective-C exception handler like [`catch`] (and specifically not
173173
/// [`catch_unwind`]).
174174
///
175-
/// This also invokes undefined behaviour until `C-unwind` is stabilized, see
176-
/// [RFC-2945] - you can try this out on nightly using the `unstable-c-unwind`
177-
/// feature flag.
175+
/// This also invokes undefined behaviour unless `C-unwind` is used, which it
176+
/// only is if the `unstable-c-unwind` feature flag is enabled (raises MSRV to
177+
/// 1.71).
178178
///
179179
/// [`catch_unwind`]: std::panic::catch_unwind
180-
/// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html
181180
#[inline]
182181
#[cfg(feature = "exception")] // For consistency, not strictly required
183182
pub unsafe fn throw(exception: Retained<Exception>) -> ! {
@@ -274,11 +273,9 @@ unsafe fn try_no_ret<F: FnOnce()>(closure: F) -> Result<(), Option<Retained<Exce
274273
/// The given closure must not panic (e.g. normal Rust unwinding into this
275274
/// causes undefined behaviour).
276275
///
277-
/// Additionally, this unwinds through the closure from Objective-C, which is
278-
/// undefined behaviour until `C-unwind` is stabilized, see [RFC-2945] - you
279-
/// can try this out on nightly using the `unstable-c-unwind` feature flag.
280-
///
281-
/// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html
276+
/// This also invokes undefined behaviour unless `C-unwind` is used, which it
277+
/// only is if the `unstable-c-unwind` feature flag is enabled (raises MSRV to
278+
/// 1.71).
282279
#[cfg(feature = "exception")]
283280
pub unsafe fn catch<R>(
284281
closure: impl FnOnce() -> R + UnwindSafe,

crates/objc2/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
feature = "unstable-autoreleasesafe",
152152
feature(negative_impls, auto_traits)
153153
)]
154-
#![cfg_attr(feature = "unstable-c-unwind", feature(c_unwind))]
155154
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
156155
#![warn(missing_docs)]
157156
#![warn(clippy::missing_errors_doc)]

crates/objc2/src/macros/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,17 +831,15 @@ macro_rules! __class_inner {
831831
/// # Panics
832832
///
833833
/// Panics if the `"catch-all"` feature is enabled and the Objective-C method
834-
/// throws an exception. Exceptions may still cause UB until
835-
/// `extern "C-unwind"` is stable, see [RFC-2945].
834+
/// throws an exception. Exceptions may still cause UB unless you enable the
835+
/// `"unstable-c-unwind"` feature (raises MSRV to 1.71).
836836
///
837837
/// Panics if `debug_assertions` are enabled and the Objective-C method's
838838
/// encoding does not match the encoding of the given arguments and return.
839839
///
840840
/// And panics if the `NSError**` handling functionality described above is
841841
/// used, and the error object was unexpectedly `NULL`.
842842
///
843-
/// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html
844-
///
845843
///
846844
/// # Safety
847845
///

0 commit comments

Comments
 (0)