Skip to content

Commit 0270553

Browse files
committed
fix unintentional unsafe_code trigger
1 parent 3f6ab7c commit 0270553

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

pyo3-macros-backend/src/pymethod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,14 @@ pub fn impl_py_getter_def(
766766

767767
// TODO: on MSRV 1.77+, we can use `::std::mem::offset_of!` here, and it should
768768
// make it possible for the `MaybeRuntimePyMethodDef` to be a `Static` variant.
769-
let method_def = quote_spanned! {ty.span()=>
769+
let generator = quote_spanned! { ty.span() =>
770+
#pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Runtime(
771+
|| GENERATOR.generate(#python_name, #doc)
772+
)
773+
};
774+
// This is separate so that the unsafe below does not inherit the span and thus does not
775+
// trigger the `unsafe_code` lint
776+
let method_def = quote! {
770777
#cfg_attrs
771778
{
772779
#[allow(unused_imports)] // might not be used if all probes are positve
@@ -790,9 +797,7 @@ pub fn impl_py_getter_def(
790797
{ #pyo3_path::impl_::pyclass::IsIntoPyObjectRef::<#ty>::VALUE },
791798
{ #pyo3_path::impl_::pyclass::IsIntoPyObject::<#ty>::VALUE },
792799
> = unsafe { #pyo3_path::impl_::pyclass::PyClassGetterGenerator::new() };
793-
#pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Runtime(
794-
|| GENERATOR.generate(#python_name, #doc)
795-
)
800+
#generator
796801
}
797802
};
798803

tests/test_compile_error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn test_compile_errors() {
5454
#[cfg(any(not(Py_LIMITED_API), Py_3_10))] // to avoid PyFunctionArgument for &str
5555
t.compile_fail("tests/ui/invalid_cancel_handle.rs");
5656
t.pass("tests/ui/pymodule_missing_docs.rs");
57+
#[cfg(not(Py_LIMITED_API))]
58+
t.pass("tests/ui/forbid_unsafe.rs");
5759
#[cfg(all(Py_LIMITED_API, not(feature = "experimental-async")))]
5860
// output changes with async feature
5961
t.compile_fail("tests/ui/abi3_inheritance.rs");

tests/ui/forbid_unsafe.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![forbid(unsafe_code)]
2+
3+
use pyo3::*;
4+
5+
#[allow(unexpected_cfgs)]
6+
#[path = "../../src/tests/hygiene/mod.rs"]
7+
mod hygiene;
8+
9+
mod gh_4394 {
10+
use pyo3::prelude::*;
11+
12+
#[derive(Eq, Ord, PartialEq, PartialOrd, Clone)]
13+
#[pyclass(get_all)]
14+
pub struct VersionSpecifier {
15+
pub(crate) operator: Operator,
16+
pub(crate) version: Version,
17+
}
18+
19+
#[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash, Clone, Copy)]
20+
#[pyo3::pyclass(eq, eq_int)]
21+
pub enum Operator {
22+
Equal,
23+
}
24+
25+
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)]
26+
#[pyclass]
27+
pub struct Version;
28+
}
29+
30+
fn main() {}

0 commit comments

Comments
 (0)