Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile_error test for PyClass: Send #948

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn test_compile_errors() {
t.compile_fail("tests/ui/invalid_pyclass_args.rs");
t.compile_fail("tests/ui/invalid_pymethod_names.rs");
t.compile_fail("tests/ui/missing_clone.rs");
t.compile_fail("tests/ui/pyclass_send.rs");
t.compile_fail("tests/ui/reject_generics.rs");
t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs");
// Since the current minimum nightly(2020-01-20) has a different error message,
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/pyclass_send.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use pyo3::prelude::*;
use std::rc::Rc;

#[pyclass]
struct NotThreadSafe {
data: Rc<i32>
}

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<NotThreadSafe> = obj.as_ref(py).downcast().unwrap();

assert_eq!(*c.borrow().data, 5);
}).join().unwrap();
}
14 changes: 14 additions & 0 deletions tests/ui/pyclass_send.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0277]: `std::rc::Rc<i32>` cannot be sent between threads safely
--> $DIR/pyclass_send.rs:4:1
|
4 | #[pyclass]
| ^^^^^^^^^^ `std::rc::Rc<i32>` cannot be sent between threads safely
|
::: $WORKSPACE/src/pyclass.rs:76:76
|
76 | PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethods + Send
| ---- required by this bound in `pyo3::pyclass::PyClass`
|
= help: within `NotThreadSafe`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<i32>`
= 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)