Skip to content

Commit 5e1054d

Browse files
authored
Merge pull request #503 from madsmtm/small-fixes
Small fixes
2 parents 40bdef6 + ad11ebf commit 5e1054d

File tree

13 files changed

+49
-11
lines changed

13 files changed

+49
-11
lines changed

crates/block-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#![warn(clippy::ptr_as_ptr)]
2323
#![warn(clippy::missing_errors_doc)]
2424
#![warn(clippy::missing_panics_doc)]
25+
#![allow(non_camel_case_types)]
2526
// Update in Cargo.toml as well.
2627
#![doc(html_root_url = "https://docs.rs/block-sys/0.2.0")]
2728
#![cfg_attr(feature = "unstable-docsrs", feature(doc_auto_cfg, doc_cfg_hide))]

crates/header-translator/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Make sure you have the same XCode version installed as the one documented in [`c
1414
If you use a different operating system than macOS, or have multiple SDKs installed, you can specify the directory as the first argument:
1515

1616
```console
17-
cargo run --bin header-translator -- /Applications/Xcode_new.app/Contents/Developer
17+
cargo run --bin header-translator -- /Applications/Xcode.app/Contents/Developer
1818
```
1919

2020
Note that you will likely need to use a newer `libclang`, such as [the one distributed with Swift `5.7.2`](https://github.com/apple/llvm-project/tree/swift-5.7.2-RELEASE). You can use a different version as follows (details might vary between operating systems):

crates/icrate/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
305305
unsound before).
306306

307307
### Fixed
308-
* Fixed `NSZone` not being `#[repr(C)]`.
308+
* Fixed `NSZone` not specifying a `#[repr(...)]`.
309309

310310

311311
## objc2::foundation 0.3.0-beta.3 - 2022-09-01
@@ -470,7 +470,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
470470

471471
### Fixed
472472
* Soundness issue with `NSValue`, `NSDictionary`, `NSArray` and
473-
`NSMutableArray` not being `#[repr(C)]`.
473+
`NSMutableArray` not specifying a `#[repr(...)]`.
474474
* **BREAKING**: `NSObject` is no longer `Send` and `Sync` (because its
475475
subclasses may not be).
476476

crates/objc-sys/src/object.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use crate::{objc_class, OpaqueData};
1010
#[repr(C)]
1111
pub struct objc_object {
1212
// `isa` field is deprecated, so we don't expose it here.
13+
//
14+
// Also, we need this to be a zero-sized, so that the compiler doesn't
15+
// assume anything about the layout.
16+
//
1317
// Use `object_getClass` instead.
1418
_priv: [u8; 0],
1519
_p: OpaqueData,

crates/objc2/src/declare/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ pub(crate) mod private {
152152
///
153153
/// This is a sealed trait that is implemented for a lot of `extern "C"`
154154
/// function pointer types.
155-
pub trait MethodImplementation: private::Sealed {
155+
//
156+
// Note: `Sized` is intentionally added to make the trait not object safe.
157+
pub trait MethodImplementation: private::Sealed + Sized {
156158
/// The callee type of the method.
157159
type Callee: RefEncode + ?Sized;
158160
/// The return type of the method.

crates/objc2/src/encode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub unsafe trait RefEncode {
196196
/// [`Encoding::Class`], [`Encoding::Pointer`], [`Encoding::Sel`] or
197197
/// [`Encoding::Unknown`].
198198
///
199+
///
199200
/// # Examples
200201
///
201202
/// This is usually implemented either as an object pointer:

crates/objc2/src/mutability.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
//! (can be done inside [`extern_class!`] and [`declare_class!`]).
1616
//!
1717
//! Note that precious little of Objective-C follows Rust's usual shared xor
18-
//! unique ownership model, most often objects assume interior mutability.
18+
//! unique ownership model, most often objects assume interior mutability, so
19+
//! a safe default is often [`InteriorMutable`], or of you're working with GUI
20+
//! code, [`MainThreadOnly`].
1921
//!
2022
//! [`UnsafeCell`]: core::cell::UnsafeCell
2123
//! [`ClassType::Mutability`]: crate::ClassType::Mutability
@@ -248,6 +250,9 @@ mod private {
248250
impl MutabilityIsMutable for Mutable {}
249251
impl<IS: ?Sized> MutabilityIsMutable for MutableWithImmutableSuperclass<IS> {}
250252

253+
pub trait MutabilityIsMainThreadOnly: Mutability {}
254+
impl MutabilityIsMainThreadOnly for MainThreadOnly {}
255+
251256
// TODO: Trait for objects whose `hash` is guaranteed to never change,
252257
// which allows it to be used as a key in `NSDictionary`.
253258
}
@@ -256,6 +261,8 @@ mod private {
256261
///
257262
/// This is a sealed trait, and should not need to be implemented. Open an
258263
/// issue if you know a use-case where this restrition should be lifted!
264+
//
265+
// Note: `Sized` is intentionally added to make the trait not object safe.
259266
pub trait Mutability: private::Sealed + Sized {}
260267
impl Mutability for Root {}
261268
impl Mutability for Immutable {}
@@ -337,7 +344,10 @@ impl<T: ?Sized + ClassType> IsMutable for T where T::Mutability: private::Mutabi
337344
//
338345
// Note: MainThreadMarker::from relies on this.
339346
pub trait IsMainThreadOnly: ClassType {}
340-
impl<T: ?Sized + ClassType<Mutability = MainThreadOnly>> IsMainThreadOnly for T {}
347+
impl<T: ?Sized + ClassType> IsMainThreadOnly for T where
348+
T::Mutability: private::MutabilityIsMainThreadOnly
349+
{
350+
}
341351

342352
#[cfg(test)]
343353
mod tests {

crates/objc2/src/rc/autorelease.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ mod tests {
551551
pool
552552
}
553553

554+
#[allow(unused)]
555+
fn assert_object_safe(_: &dyn AutoreleaseSafe) {}
556+
554557
#[cfg_attr(
555558
not(feature = "unstable-autoreleasesafe"),
556559
ignore = "only stably ZST when `unstable-autoreleasesafe` is enabled"

crates/objc2/src/rc/weak_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{ffi, Message};
2727
///
2828
/// [`sync::Weak`]: std::sync::Weak
2929
/// [`sync::Arc`]: std::sync::Arc
30-
#[repr(transparent)]
30+
#[repr(transparent)] // This is not a public guarantee
3131
pub struct WeakId<T: ?Sized> {
3232
/// We give the runtime the address to this box, so that it can modify it
3333
/// even if the `WeakId` is moved.

crates/objc2/src/runtime/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ impl fmt::Debug for AnyObject {
12811281
mod tests {
12821282
use alloc::format;
12831283
use alloc::string::ToString;
1284+
use core::mem::size_of;
12841285

12851286
use super::*;
12861287
use crate::test_utils;
@@ -1562,4 +1563,18 @@ mod tests {
15621563
};
15631564
assert_eq!(res, 24);
15641565
}
1566+
1567+
#[test]
1568+
fn test_sizes() {
1569+
assert_eq!(size_of::<Sel>(), size_of::<*const ()>());
1570+
assert_eq!(size_of::<Sel>(), size_of::<Option<Sel>>());
1571+
1572+
// These must be zero-sized until we get extern types, otherwise the
1573+
// optimizer may invalidly assume something about their layout.
1574+
assert_eq!(size_of::<AnyClass>(), 0);
1575+
assert_eq!(size_of::<AnyObject>(), 0);
1576+
assert_eq!(size_of::<AnyProtocol>(), 0);
1577+
assert_eq!(size_of::<Ivar>(), 0);
1578+
assert_eq!(size_of::<Method>(), 0);
1579+
}
15651580
}

0 commit comments

Comments
 (0)