Skip to content

Instantly share code, notes, and snippets.

@ngoldbaum
Created November 1, 2024 16:18
Show Gist options
  • Save ngoldbaum/12d01abaf05761b14951a90dfd826461 to your computer and use it in GitHub Desktop.
Save ngoldbaum/12d01abaf05761b14951a90dfd826461 to your computer and use it in GitHub Desktop.
diff --git a/pyo3-macros-backend/src/pyclass.rs b/pyo3-macros-backend/src/pyclass.rs
index 96bfbb9e..ae3082e3 100644
--- a/pyo3-macros-backend/src/pyclass.rs
+++ b/pyo3-macros-backend/src/pyclass.rs
@@ -2317,7 +2317,7 @@ impl<'a> PyClassImplsBuilder<'a> {
cls.span() =>
const _: () = {
use #pyo3_path::impl_::pyclass::*;
- assert_pyclass_sync::<#cls, { IsSync::<#cls>::VALUE }>();
+ assert_pyclass_sync::<#cls>();
};
}
};
diff --git a/src/impl_/pyclass/assertions.rs b/src/impl_/pyclass/assertions.rs
index 14a18c3a..daf2ed53 100644
--- a/src/impl_/pyclass/assertions.rs
+++ b/src/impl_/pyclass/assertions.rs
@@ -6,9 +6,10 @@
///
/// The additional `const IS_SYNC: bool` parameter is used to allow the custom
/// diagnostic to be emitted; if `PyClassSync`
-pub const fn assert_pyclass_sync<T, const IS_SYNC: bool>()
+#[allow(unused)]
+pub const fn assert_pyclass_sync<T>()
where
- T: PyClassSync<IS_SYNC> + Sync,
+ T: PyClassSync + Sync,
{
}
@@ -21,23 +22,9 @@ where
note = "see <TODO INSERT PYO3 GUIDE> for more information",
)
)]
-pub trait PyClassSync<const IS_SYNC: bool>: private::Sealed<IS_SYNC> {}
+pub trait PyClassSync<T: Sync = Self> {}
-mod private {
- pub trait Sealed<const IS_SYNC: bool> {}
- impl<T> Sealed<true> for T {}
- #[cfg(not(diagnostic_namespace))]
- impl<T> Sealed<false> for T {}
-}
-
-// If `true` is passed for the const parameter, then the diagnostic will
-// not be emitted.
-impl<T> PyClassSync<true> for T {}
-
-// Without `diagnostic_namespace`, the trait bound is not useful, so we add
-// an implementation for `false`` to avoid a useless diagnostic.
-#[cfg(not(diagnostic_namespace))]
-impl<T> PyClassSync<false> for T {}
+impl<T> PyClassSync for T where T: Sync {}
mod tests {
#[cfg(feature = "macros")]
@@ -47,6 +34,7 @@ mod tests {
#[crate::pyclass(crate = "crate")]
struct MyClass {}
- assert_pyclass_sync::<MyClass, true>();
+
+ assert_pyclass_sync::<MyClass>();
}
}
diff --git a/tests/ui/pyclass_send.stderr b/tests/ui/pyclass_send.stderr
index 0f09e569..3ef9591f 100644
--- a/tests/ui/pyclass_send.stderr
+++ b/tests/ui/pyclass_send.stderr
@@ -1,21 +1,3 @@
-error[E0277]: the trait `Sync` is not implemented for `NotSyncNotSend`
- --> tests/ui/pyclass_send.rs:5:8
- |
-5 | struct NotSyncNotSend(*mut c_void);
- | ^^^^^^^^^^^^^^ required by `#[pyclass]`
- |
- = help: the trait `PyClassSync<false>` is not implemented for `NotSyncNotSend`
- = note: replace thread-unsafe fields with thread-safe alternatives
- = note: see <TODO INSERT PYO3 GUIDE> for more information
-note: required by a bound in `pyo3::impl_::pyclass::assertions::assert_pyclass_sync`
- --> src/impl_/pyclass/assertions.rs
- |
- | pub const fn assert_pyclass_sync<T, const IS_SYNC: bool>()
- | ------------------- required by a bound in this function
- | where
- | T: PyClassSync<IS_SYNC> + Sync,
- | ^^^^^^^^^^^^^^^^^^^^ required by this bound in `assert_pyclass_sync`
-
error[E0277]: `*mut c_void` cannot be shared between threads safely
--> tests/ui/pyclass_send.rs:5:8
|
@@ -31,11 +13,11 @@ note: required because it appears within the type `NotSyncNotSend`
note: required by a bound in `pyo3::impl_::pyclass::assertions::assert_pyclass_sync`
--> src/impl_/pyclass/assertions.rs
|
- | pub const fn assert_pyclass_sync<T, const IS_SYNC: bool>()
+ | pub const fn assert_pyclass_sync<T>()
| ------------------- required by a bound in this function
| where
- | T: PyClassSync<IS_SYNC> + Sync,
- | ^^^^ required by this bound in `assert_pyclass_sync`
+ | T: PyClassSync + Sync,
+ | ^^^^ required by this bound in `assert_pyclass_sync`
error[E0277]: `*mut c_void` cannot be sent between threads safely
--> tests/ui/pyclass_send.rs:4:1
@@ -58,24 +40,6 @@ note: required by a bound in `PyClassImpl::ThreadChecker`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `PyClassImpl::ThreadChecker`
= note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)
-error[E0277]: the trait `Sync` is not implemented for `SendNotSync`
- --> tests/ui/pyclass_send.rs:8:8
- |
-8 | struct SendNotSync(*mut c_void);
- | ^^^^^^^^^^^ required by `#[pyclass]`
- |
- = help: the trait `PyClassSync<false>` is not implemented for `SendNotSync`
- = note: replace thread-unsafe fields with thread-safe alternatives
- = note: see <TODO INSERT PYO3 GUIDE> for more information
-note: required by a bound in `pyo3::impl_::pyclass::assertions::assert_pyclass_sync`
- --> src/impl_/pyclass/assertions.rs
- |
- | pub const fn assert_pyclass_sync<T, const IS_SYNC: bool>()
- | ------------------- required by a bound in this function
- | where
- | T: PyClassSync<IS_SYNC> + Sync,
- | ^^^^^^^^^^^^^^^^^^^^ required by this bound in `assert_pyclass_sync`
-
error[E0277]: `*mut c_void` cannot be shared between threads safely
--> tests/ui/pyclass_send.rs:8:8
|
@@ -91,11 +55,11 @@ note: required because it appears within the type `SendNotSync`
note: required by a bound in `pyo3::impl_::pyclass::assertions::assert_pyclass_sync`
--> src/impl_/pyclass/assertions.rs
|
- | pub const fn assert_pyclass_sync<T, const IS_SYNC: bool>()
+ | pub const fn assert_pyclass_sync<T>()
| ------------------- required by a bound in this function
| where
- | T: PyClassSync<IS_SYNC> + Sync,
- | ^^^^ required by this bound in `assert_pyclass_sync`
+ | T: PyClassSync + Sync,
+ | ^^^^ required by this bound in `assert_pyclass_sync`
error[E0277]: `*mut c_void` cannot be sent between threads safely
--> tests/ui/pyclass_send.rs:11:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment