From 3ea960dfe87c67f58e9de3880a8baf0691ec4493 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 30 Aug 2024 12:47:40 -0700 Subject: [PATCH 1/3] Drop "readonly" LLVM attribute from references to opaque C++ types --- src/opaque.rs | 2 ++ tests/ui/opaque_autotraits.stderr | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/opaque.rs b/src/opaque.rs index e0f8ce2c2..93a2adb26 100644 --- a/src/opaque.rs +++ b/src/opaque.rs @@ -1,6 +1,7 @@ #![allow(missing_docs)] use crate::void; +use core::cell::UnsafeCell; use core::marker::{PhantomData, PhantomPinned}; use core::mem; @@ -14,6 +15,7 @@ use core::mem; pub struct Opaque { _private: [*const void; 0], _pinned: PhantomData, + _mutable: UnsafeCell<()>, } const_assert_eq!(0, mem::size_of::()); diff --git a/tests/ui/opaque_autotraits.stderr b/tests/ui/opaque_autotraits.stderr index 0a797b460..9b4e7a49c 100644 --- a/tests/ui/opaque_autotraits.stderr +++ b/tests/ui/opaque_autotraits.stderr @@ -22,6 +22,29 @@ note: required by a bound in `assert_send` 8 | fn assert_send() {} | ^^^^ required by this bound in `assert_send` +error[E0277]: `UnsafeCell<()>` cannot be shared between threads safely + --> tests/ui/opaque_autotraits.rs:14:19 + | +14 | assert_sync::(); + | ^^^^^^^^^^^ `UnsafeCell<()>` cannot be shared between threads safely + | + = help: within `ffi::Opaque`, the trait `Sync` is not implemented for `UnsafeCell<()>`, which is required by `ffi::Opaque: Sync` +note: required because it appears within the type `cxx::private::Opaque` + --> src/opaque.rs + | + | pub struct Opaque { + | ^^^^^^ +note: required because it appears within the type `ffi::Opaque` + --> tests/ui/opaque_autotraits.rs:4:14 + | +4 | type Opaque; + | ^^^^^^ +note: required by a bound in `assert_sync` + --> tests/ui/opaque_autotraits.rs:9:19 + | +9 | fn assert_sync() {} + | ^^^^ required by this bound in `assert_sync` + error[E0277]: `*const cxx::void` cannot be shared between threads safely --> tests/ui/opaque_autotraits.rs:14:19 | From e19c267ee02ae815790919665d6f08b5731fa9ac Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 30 Aug 2024 12:51:51 -0700 Subject: [PATCH 2/3] Reduce verbose !Sync opaque type errors --- src/opaque.rs | 8 +++++++- tests/ui/opaque_autotraits.stderr | 23 ----------------------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/opaque.rs b/src/opaque.rs index 93a2adb26..ccf74f415 100644 --- a/src/opaque.rs +++ b/src/opaque.rs @@ -15,8 +15,14 @@ use core::mem; pub struct Opaque { _private: [*const void; 0], _pinned: PhantomData, - _mutable: UnsafeCell<()>, + _mutable: SyncUnsafeCell<()>, } +// TODO: https://github.com/rust-lang/rust/issues/95439 +#[repr(transparent)] +struct SyncUnsafeCell(UnsafeCell); + +unsafe impl Sync for SyncUnsafeCell {} + const_assert_eq!(0, mem::size_of::()); const_assert_eq!(1, mem::align_of::()); diff --git a/tests/ui/opaque_autotraits.stderr b/tests/ui/opaque_autotraits.stderr index 9b4e7a49c..0a797b460 100644 --- a/tests/ui/opaque_autotraits.stderr +++ b/tests/ui/opaque_autotraits.stderr @@ -22,29 +22,6 @@ note: required by a bound in `assert_send` 8 | fn assert_send() {} | ^^^^ required by this bound in `assert_send` -error[E0277]: `UnsafeCell<()>` cannot be shared between threads safely - --> tests/ui/opaque_autotraits.rs:14:19 - | -14 | assert_sync::(); - | ^^^^^^^^^^^ `UnsafeCell<()>` cannot be shared between threads safely - | - = help: within `ffi::Opaque`, the trait `Sync` is not implemented for `UnsafeCell<()>`, which is required by `ffi::Opaque: Sync` -note: required because it appears within the type `cxx::private::Opaque` - --> src/opaque.rs - | - | pub struct Opaque { - | ^^^^^^ -note: required because it appears within the type `ffi::Opaque` - --> tests/ui/opaque_autotraits.rs:4:14 - | -4 | type Opaque; - | ^^^^^^ -note: required by a bound in `assert_sync` - --> tests/ui/opaque_autotraits.rs:9:19 - | -9 | fn assert_sync() {} - | ^^^^ required by this bound in `assert_sync` - error[E0277]: `*const cxx::void` cannot be shared between threads safely --> tests/ui/opaque_autotraits.rs:14:19 | From 642278c45300445c547e2fb70bb4ab95c127e438 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 30 Aug 2024 13:06:56 -0700 Subject: [PATCH 3/3] Fix compatibility with compilers older than 1.72 Old compilers didn't used to consider `()` acceptable for FFI. error: `extern` block uses type `()`, which is not FFI-safe --> demo/src/main.rs:20:14 | 20 | type BlobstoreClient; | ______________^ 21 | | 22 | | fn new_blobstore_client() -> UniquePtr; 23 | | fn put(&self, parts: &mut MultiBuf) -> u64; | |________________^ not FFI-safe | = help: consider using a struct instead = note: tuples have unspecified layout note: the lint level is defined here --> demo/src/main.rs:1:1 | 1 | #[cxx::bridge(namespace = "org::blobstore")] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: this error originates in the attribute macro `cxx::bridge` (in Nightly builds, run with -Z macro-backtrace for more info) --- src/opaque.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opaque.rs b/src/opaque.rs index ccf74f415..2ca90af43 100644 --- a/src/opaque.rs +++ b/src/opaque.rs @@ -15,7 +15,7 @@ use core::mem; pub struct Opaque { _private: [*const void; 0], _pinned: PhantomData, - _mutable: SyncUnsafeCell<()>, + _mutable: SyncUnsafeCell>, } // TODO: https://github.com/rust-lang/rust/issues/95439