Skip to content

Commit

Permalink
Add compile_error test for PyClass: Send
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jun 12, 2020
1 parent 7a72713 commit 6e42c2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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)

0 comments on commit 6e42c2a

Please sign in to comment.