Skip to content

Commit

Permalink
rust: support 1.57
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 7, 2021
1 parent 8a03778 commit 7091512
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ fn impl_arg_param(

Ok(quote_arg_span! {
let #mut_ _tmp: #target_ty = #arg_value_or_default;
#[allow(clippy::needless_option_as_deref)]
let #arg_name = #borrow_tmp;
})
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ fn is_matching_endian(c: u8) -> bool {
}

/// Trait implemented for possible element types of `PyBuffer`.
///
/// # Safety
///
/// This trait must only be implemented for types which represent valid elements of Python buffers.
pub unsafe trait Element: Copy {
/// Gets whether the element specified in the format string is potentially compatible.
/// Alignment and size are checked separately from this function.
Expand Down
4 changes: 4 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ impl IntoPy<Py<PyTuple>> for () {
}

/// Raw level conversion between `*mut ffi::PyObject` and PyO3 types.
///
/// # Safety
///
/// See safety notes on individual functions.
pub unsafe trait FromPyPointer<'p>: Sized {
/// Convert from an arbitrary `PyObject`.
///
Expand Down
4 changes: 4 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ use std::ptr::NonNull;
/// PyO3 is designed in a way that all references to those types are bound
/// to the GIL, which is why you can get a token from all references of those
/// types.
///
/// # Safety
///
/// This trait must only be implemented for types which cannot be accessed without the GIL.
pub unsafe trait PyNativeType: Sized {
/// Returns a GIL marker constrained to the lifetime of this type.
#[inline]
Expand Down
11 changes: 11 additions & 0 deletions src/type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ use std::thread::{self, ThreadId};
/// is of `PyAny`.
///
/// This trait is intended to be used internally.
///
/// # Safety
///
/// This trait must only be implemented for types which represent valid layouts of Python objects.
pub unsafe trait PyLayout<T> {}

/// `T: PySizedLayout<U>` represents that `T` is not a instance of
Expand All @@ -31,6 +35,11 @@ pub trait PySizedLayout<T>: PyLayout<T> + Sized {}
/// - the return value of type_object must always point to the same PyTypeObject instance
///
/// It is safely implemented by the `pyclass` macro.
///
/// # Safety
///
/// Implementations must provide an implementation for `type_object_raw` which infallibly produces a
/// non-null pointer to the corresponding Python type object.
pub unsafe trait PyTypeInfo: Sized {
/// Class name.
const NAME: &'static str;
Expand All @@ -57,6 +66,8 @@ pub unsafe trait PyTypeInfo: Sized {

/// Python object types that have a corresponding type object.
///
/// # Safety
///
/// This trait is marked unsafe because not fulfilling the contract for type_object
/// leads to UB.
///
Expand Down
19 changes: 17 additions & 2 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ fn _test_compile_errors() {
t.compile_fail("tests/ui/invalid_argument_attributes.rs");
t.compile_fail("tests/ui/missing_clone.rs");
t.compile_fail("tests/ui/reject_generics.rs");
t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs");

tests_rust_1_49(&t);
tests_rust_1_54(&t);
tests_rust_1_55(&t);
tests_rust_1_56(&t);
tests_rust_1_57(&t);

#[rustversion::since(1.49)]
fn tests_rust_1_49(t: &trybuild::TestCases) {
Expand All @@ -45,7 +45,6 @@ fn _test_compile_errors() {
#[rustversion::since(1.54)]
fn tests_rust_1_54(t: &trybuild::TestCases) {
t.compile_fail("tests/ui/invalid_frompy_derive.rs");
t.compile_fail("tests/ui/static_ref.rs");
}
#[rustversion::before(1.54)]
fn tests_rust_1_54(_t: &trybuild::TestCases) {}
Expand All @@ -70,4 +69,20 @@ fn _test_compile_errors() {

#[rustversion::before(1.56)]
fn tests_rust_1_56(_t: &trybuild::TestCases) {}

#[rustversion::since(1.57)]
fn tests_rust_1_57(t: &trybuild::TestCases) {
t.compile_fail("tests/ui/invalid_closure.rs");
t.compile_fail("tests/ui/invalid_result_conversion.rs");
t.compile_fail("tests/ui/pyclass_send.rs");

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

#[rustversion::before(1.57)]
fn tests_rust_1_56(_t: &trybuild::TestCases) {
t.compile_fail("tests/ui/static_ref.rs");
t.compile_fail("tests/ui/wrong_aspyref_lifetimes.rs");
}
}
2 changes: 1 addition & 1 deletion tests/ui/static_ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'py`
4 | #[pyfunction]
| ^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 4:1...
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined here...
--> tests/ui/static_ref.rs:4:1
|
4 | #[pyfunction]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/wrong_aspyref_lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0505]: cannot move out of `gil` because it is borrowed
--> tests/ui/wrong_aspyref_lifetimes.rs:7:10
|
6 | let dict: &PyDict = dict.as_ref(gil.python());
| --- borrow of `gil` occurs here
| ------------ borrow of `gil` occurs here
7 | drop(gil);
| ^^^ move out of `gil` occurs here
8 |
9 | let _py: Python = dict.py(); // Obtain a Python<'p> without GIL.
| ---- borrow later used here
| --------- borrow later used here

0 comments on commit 7091512

Please sign in to comment.