Skip to content

Commit c021a01

Browse files
authored
Merge pull request #1364 from davidhewitt/pyclass-send-ui-test
pyclass: add !Send compile_error test
2 parents b152fd6 + 22de3b4 commit c021a01

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

tests/test_compile_error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn test_compile_errors() {
2626
fn tests_rust_1_49(t: &trybuild::TestCases) {
2727
t.compile_fail("tests/ui/invalid_frompy_derive.rs");
2828
t.compile_fail("tests/ui/invalid_pymethod_receiver.rs");
29+
t.compile_fail("tests/ui/pyclass_send.rs");
2930

3031
#[cfg(Py_LIMITED_API)]
3132
t.compile_fail("tests/ui/abi3_nativetype_inheritance.rs");

tests/ui/pyclass_send.rs

+25
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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0277]: `Rc<i32>` cannot be sent between threads safely
2+
--> $DIR/pyclass_send.rs:4:1
3+
|
4+
4 | #[pyclass]
5+
| ^^^^^^^^^^ `Rc<i32>` cannot be sent between threads safely
6+
|
7+
::: $WORKSPACE/src/pyclass.rs
8+
|
9+
| pub struct ThreadCheckerStub<T: Send>(PhantomData<T>);
10+
| ---- required by this bound in `ThreadCheckerStub`
11+
|
12+
= help: within `NotThreadSafe`, the trait `Send` is not implemented for `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)