Skip to content

Commit 6e42c2a

Browse files
committed
Add compile_error test for PyClass: Send
1 parent 7a72713 commit 6e42c2a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

tests/test_compile_error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn test_compile_errors() {
66
t.compile_fail("tests/ui/invalid_pyclass_args.rs");
77
t.compile_fail("tests/ui/invalid_pymethod_names.rs");
88
t.compile_fail("tests/ui/missing_clone.rs");
9+
t.compile_fail("tests/ui/pyclass_send.rs");
910
t.compile_fail("tests/ui/reject_generics.rs");
1011
t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs");
1112
// Since the current minimum nightly(2020-01-20) has a different error message,

tests/ui/pyclass_send.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use pyo3::prelude::*;
2+
use std::rc::Rc;
3+
4+
#[pyclass]
5+
struct NotThreadSafe {
6+
data: Rc<i32>
7+
}
8+
9+
fn main() {
10+
let gil = Python::acquire_gil();
11+
let py = gil.python();
12+
13+
let obj = PyCell::new(py, NotThreadSafe { data: Rc::new(5) }).unwrap().to_object(py);
14+
drop(gil);
15+
16+
std::thread::spawn(move || {
17+
let gil = Python::acquire_gil();
18+
let py = gil.python();
19+
20+
// Uh oh, moved Rc to a new thread!
21+
let c: &PyCell<NotThreadSafe> = obj.as_ref(py).downcast().unwrap();
22+
23+
assert_eq!(*c.borrow().data, 5);
24+
}).join().unwrap();
25+
}

tests/ui/pyclass_send.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: `std::rc::Rc<i32>` cannot be sent between threads safely
2+
--> $DIR/pyclass_send.rs:4:1
3+
|
4+
4 | #[pyclass]
5+
| ^^^^^^^^^^ `std::rc::Rc<i32>` cannot be sent between threads safely
6+
|
7+
::: $WORKSPACE/src/pyclass.rs:76:76
8+
|
9+
76 | PyTypeInfo<Layout = PyCell<Self>> + Sized + PyClassAlloc + PyMethods + Send
10+
| ---- required by this bound in `pyo3::pyclass::PyClass`
11+
|
12+
= help: within `NotThreadSafe`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<i32>`
13+
= note: required because it appears within the type `NotThreadSafe`
14+
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)