Skip to content

Commit 2ace80b

Browse files
committed
Cherry-pick relevant parts of 9bd26ee for objc2 v0.6.4
And add a changelog entry for it.
1 parent 6780851 commit 2ace80b

File tree

6 files changed

+75
-15
lines changed

6 files changed

+75
-15
lines changed

crates/objc2/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## Unreleased - YYYY-MM-DD
88

9+
## Added
10+
* Added support for the unstable `darwin_objc` feature.
11+
912

1013
## [0.6.3] - 2025-10-04
1114
[0.6.3]: https://github.com/madsmtm/objc2/compare/objc2-0.6.2...objc2-0.6.3

crates/objc2/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ disable-encoding-assertions = []
6969
# no longer required.
7070
verify = []
7171

72-
# Make the `sel!` macro look up the selector statically.
72+
# Make the `sel!`/`class!` macro look up the item statically.
7373
#
7474
# The plan is to enable this by default, but right now we are uncertain of
7575
# its stability, and it might need significant changes before being fully
@@ -82,6 +82,9 @@ unstable-static-sel-inlined = ["unstable-static-sel"]
8282
unstable-static-class = ["dep:objc2-proc-macros"]
8383
unstable-static-class-inlined = ["unstable-static-class"]
8484

85+
# Augment the above with the nightly `darwin_objc` feature.
86+
unstable-darwin-objc = []
87+
8588
# Uses nightly features to make autorelease pools fully sound
8689
unstable-autoreleasesafe = []
8790

crates/objc2/src/__macro_helpers/common_selectors.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@ use crate::runtime::Sel;
1111

1212
#[inline]
1313
pub fn alloc_sel() -> Sel {
14-
__sel_inner!("alloc\0", "alloc")
14+
__sel_inner!("alloc", "alloc")
1515
}
1616

1717
#[inline]
1818
pub fn init_sel() -> Sel {
19-
__sel_inner!("init\0", "init")
19+
__sel_inner!("init", "init")
2020
}
2121

2222
#[inline]
2323
pub fn new_sel() -> Sel {
24-
__sel_inner!("new\0", "new")
24+
__sel_inner!("new", "new")
2525
}
2626

2727
#[inline]
2828
pub fn dealloc_sel() -> Sel {
29-
__sel_inner!("dealloc\0", "dealloc")
29+
__sel_inner!("dealloc", "dealloc")
3030
}
3131

3232
/// An undocumented selector called by the Objective-C runtime when
3333
/// initializing instance variables.
3434
#[inline]
3535
#[allow(dead_code)] // May be useful in the future
3636
fn cxx_construct_sel() -> Sel {
37-
__sel_inner!(".cxx_construct\0", ".cxx_construct")
37+
__sel_inner!(".cxx_construct", ".cxx_construct")
3838
}
3939

4040
/// Objective-C runtimes call `.cxx_destruct` as part of the final `dealloc`
@@ -71,7 +71,7 @@ fn cxx_construct_sel() -> Sel {
7171
#[inline]
7272
#[allow(dead_code)] // May be useful in the future
7373
fn cxx_destruct_sel() -> Sel {
74-
__sel_inner!(".cxx_destruct\0", ".cxx_destruct")
74+
__sel_inner!(".cxx_destruct", ".cxx_destruct")
7575
}
7676

7777
#[cfg(test)]

crates/objc2/src/__macro_helpers/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub use core::{compile_error, concat, env, module_path, panic, stringify};
1414
// TODO: Use `core::cell::LazyCell`
1515
pub use std::sync::Once;
1616

17+
#[cfg(feature = "unstable-darwin-objc")]
18+
pub use core::os::darwin::objc as core_darwin_objc;
19+
1720
mod cache;
1821
mod class;
1922
mod common_selectors;

crates/objc2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
//! [#203]: https://github.com/madsmtm/objc2/issues/203
105105
106106
#![no_std]
107+
#![cfg_attr(feature = "unstable-darwin-objc", feature(darwin_objc))]
107108
#![cfg_attr(
108109
feature = "unstable-autoreleasesafe",
109110
feature(negative_impls, auto_traits)

crates/objc2/src/macros/mod.rs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,26 @@ macro_rules! __class_inner {
139139
/// - runtime, causing UB (unlikely)
140140
///
141141
/// The `"unstable-static-sel-inlined"` feature is the even more extreme
142-
/// version - it yields the best performance and is closest to real
142+
/// version - it yield better performance and is closer to real
143143
/// Objective-C code, but probably won't work unless your code and its
144144
/// inlining is written in a very certain way.
145145
///
146146
/// Enabling LTO greatly increases the chance that these features work.
147147
///
148+
/// On Apple/Darwin targets, these limitations can be overcome with the
149+
/// `"unstable-darwin-objc"` feature which uses the nightly-only `darwin_objc`
150+
/// language feature. This experimental language feature implements the
151+
/// Objective-C static selector ABI directly in the Rust compiler and should
152+
/// work in more if not all cases. Using `"unstable-darwin-objc"` requires
153+
/// `darwin_objc` to be enabled in every crate that uses this macro, which can
154+
/// be achieved in `objc2` crates by enabling their own
155+
/// `"unstable-darwin-objc"` features and in your own crates by adding
156+
/// `#![feature(darwin_objc)]`.
157+
///
158+
/// See [rust-lang/rust#145496] for the tracking issue for the feature.
159+
///
148160
/// [rust-lang/rust#53929]: https://github.com/rust-lang/rust/issues/53929
161+
/// [rust-lang/rust#145496]: https://github.com/rust-lang/rust/issues/145496
149162
///
150163
///
151164
/// # Examples
@@ -267,9 +280,9 @@ macro_rules! __sel_helper {
267280
// Base-case
268281
{
269282
($($parsed_sel:tt)*)
270-
} => ({
283+
} => {
271284
$crate::__sel_data!($($parsed_sel)*)
272-
});
285+
};
273286
// Single identifier
274287
{
275288
()
@@ -309,7 +322,6 @@ macro_rules! __sel_data {
309322
$crate::__macro_helpers::concat!(
310323
$crate::__macro_helpers::stringify!($first),
311324
$(':', $($($crate::__macro_helpers::stringify!($rest),)? ':',)*)?
312-
'\0',
313325
)
314326
};
315327
}
@@ -323,7 +335,7 @@ macro_rules! __sel_inner {
323335
$crate::__macro_helpers::CachedSel::new();
324336
#[allow(unused_unsafe)]
325337
unsafe {
326-
CACHED_SEL.get($data)
338+
CACHED_SEL.get($crate::__macro_helpers::concat!($data, '\0'))
327339
}
328340
}};
329341
}
@@ -405,7 +417,7 @@ macro_rules! __statics_sel {
405417
($data:expr)
406418
($hash:expr)
407419
} => {
408-
const X: &[$crate::__macro_helpers::u8] = $data.as_bytes();
420+
const X: &[$crate::__macro_helpers::u8] = $crate::__macro_helpers::concat!($data, '\0').as_bytes();
409421

410422
/// Clang marks this with LLVM's `unnamed_addr`.
411423
/// See rust-lang/rust#18297
@@ -559,11 +571,26 @@ macro_rules! __statics_class {
559571
}
560572
}
561573

574+
#[doc(hidden)]
575+
#[macro_export]
576+
#[cfg(all(feature = "unstable-static-sel", feature = "unstable-darwin-objc"))]
577+
macro_rules! __sel_inner {
578+
($data:expr, $_hash:expr) => {{
579+
let ptr = $crate::__macro_helpers::core_darwin_objc::selector!($data);
580+
let ptr = ptr.cast_const().cast::<$crate::__macro_helpers::u8>();
581+
#[allow(unused_unsafe)]
582+
unsafe {
583+
$crate::runtime::Sel::__internal_from_ptr(ptr)
584+
}
585+
}};
586+
}
587+
562588
#[doc(hidden)]
563589
#[macro_export]
564590
#[cfg(all(
565591
feature = "unstable-static-sel",
566-
not(feature = "unstable-static-sel-inlined")
592+
not(feature = "unstable-darwin-objc"),
593+
not(feature = "unstable-static-sel-inlined"),
567594
))]
568595
macro_rules! __sel_inner {
569596
($data:expr, $hash:expr) => {{
@@ -595,7 +622,11 @@ macro_rules! __sel_inner {
595622

596623
#[doc(hidden)]
597624
#[macro_export]
598-
#[cfg(feature = "unstable-static-sel-inlined")]
625+
#[cfg(all(
626+
feature = "unstable-static-sel",
627+
not(feature = "unstable-darwin-objc"),
628+
feature = "unstable-static-sel-inlined",
629+
))]
599630
macro_rules! __sel_inner {
600631
($data:expr, $hash:expr) => {{
601632
$crate::__statics_sel! {
@@ -615,6 +646,24 @@ macro_rules! __sel_inner {
615646
#[macro_export]
616647
#[cfg(all(
617648
feature = "unstable-static-class",
649+
feature = "unstable-darwin-objc",
650+
not(feature = "gnustep-1-7"),
651+
))]
652+
macro_rules! __class_inner {
653+
($name:expr, $_hash:expr) => {{
654+
let ptr = $crate::__macro_helpers::core_darwin_objc::class!($name);
655+
let ptr = ptr.cast_const().cast::<$crate::runtime::AnyClass>();
656+
#[allow(unused_unsafe)]
657+
let r: &'static $crate::runtime::AnyClass = unsafe { &*ptr };
658+
r
659+
}};
660+
}
661+
662+
#[doc(hidden)]
663+
#[macro_export]
664+
#[cfg(all(
665+
feature = "unstable-static-class",
666+
not(feature = "unstable-darwin-objc"),
618667
not(feature = "gnustep-1-7"),
619668
not(feature = "unstable-static-class-inlined")
620669
))]
@@ -639,6 +688,7 @@ macro_rules! __class_inner {
639688
#[macro_export]
640689
#[cfg(all(
641690
feature = "unstable-static-class",
691+
not(feature = "unstable-darwin-objc"),
642692
not(feature = "gnustep-1-7"),
643693
feature = "unstable-static-class-inlined"
644694
))]

0 commit comments

Comments
 (0)