diff --git a/tests/test_compile_error.rs b/tests/test_compile_error.rs index 99f52e71..e7928a29 100644 --- a/tests/test_compile_error.rs +++ b/tests/test_compile_error.rs @@ -26,6 +26,7 @@ fn test_compile_errors() { fn tests_rust_1_49(t: &trybuild::TestCases) { t.compile_fail("tests/ui/invalid_frompy_derive.rs"); t.compile_fail("tests/ui/invalid_pymethod_receiver.rs"); + t.compile_fail("tests/ui/pyclass_send.rs"); #[cfg(Py_LIMITED_API)] t.compile_fail("tests/ui/abi3_nativetype_inheritance.rs"); diff --git a/tests/ui/pyclass_send.rs b/tests/ui/pyclass_send.rs new file mode 100644 index 00000000..a46622b7 --- /dev/null +++ b/tests/ui/pyclass_send.rs @@ -0,0 +1,25 @@ +use pyo3::prelude::*; +use std::rc::Rc; + +#[pyclass] +struct NotThreadSafe { + data: Rc +} + +fn main() { + let gil = Python::acquire_gil(); + let py = gil.python(); + + let obj = PyCell::new(py, NotThreadSafe { data: Rc::new(5) }).unwrap().to_object(py); + drop(gil); + + std::thread::spawn(move || { + let gil = Python::acquire_gil(); + let py = gil.python(); + + // Uh oh, moved Rc to a new thread! + let c: &PyCell = obj.as_ref(py).downcast().unwrap(); + + assert_eq!(*c.borrow().data, 5); + }).join().unwrap(); +} diff --git a/tests/ui/pyclass_send.stderr b/tests/ui/pyclass_send.stderr new file mode 100644 index 00000000..135f53b0 --- /dev/null +++ b/tests/ui/pyclass_send.stderr @@ -0,0 +1,14 @@ +error[E0277]: `Rc` cannot be sent between threads safely + --> $DIR/pyclass_send.rs:4:1 + | +4 | #[pyclass] + | ^^^^^^^^^^ `Rc` cannot be sent between threads safely + | + ::: $WORKSPACE/src/pyclass.rs + | + | pub struct ThreadCheckerStub(PhantomData); + | ---- required by this bound in `ThreadCheckerStub` + | + = help: within `NotThreadSafe`, the trait `Send` is not implemented for `Rc` + = note: required because it appears within the type `NotThreadSafe` + = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)