Skip to content

Commit

Permalink
fix unintentional unsafe_code trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Sep 24, 2024
1 parent 3f6ab7c commit 0270553
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,14 @@ pub fn impl_py_getter_def(

// TODO: on MSRV 1.77+, we can use `::std::mem::offset_of!` here, and it should
// make it possible for the `MaybeRuntimePyMethodDef` to be a `Static` variant.
let method_def = quote_spanned! {ty.span()=>
let generator = quote_spanned! { ty.span() =>
#pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Runtime(
|| GENERATOR.generate(#python_name, #doc)
)
};
// This is separate so that the unsafe below does not inherit the span and thus does not
// trigger the `unsafe_code` lint
let method_def = quote! {
#cfg_attrs
{
#[allow(unused_imports)] // might not be used if all probes are positve
Expand All @@ -790,9 +797,7 @@ pub fn impl_py_getter_def(
{ #pyo3_path::impl_::pyclass::IsIntoPyObjectRef::<#ty>::VALUE },
{ #pyo3_path::impl_::pyclass::IsIntoPyObject::<#ty>::VALUE },
> = unsafe { #pyo3_path::impl_::pyclass::PyClassGetterGenerator::new() };
#pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Runtime(
|| GENERATOR.generate(#python_name, #doc)
)
#generator
}
};

Expand Down
2 changes: 2 additions & 0 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ fn test_compile_errors() {
#[cfg(any(not(Py_LIMITED_API), Py_3_10))] // to avoid PyFunctionArgument for &str
t.compile_fail("tests/ui/invalid_cancel_handle.rs");
t.pass("tests/ui/pymodule_missing_docs.rs");
#[cfg(not(Py_LIMITED_API))]
t.pass("tests/ui/forbid_unsafe.rs");
#[cfg(all(Py_LIMITED_API, not(feature = "experimental-async")))]
// output changes with async feature
t.compile_fail("tests/ui/abi3_inheritance.rs");
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/forbid_unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![forbid(unsafe_code)]

use pyo3::*;

#[allow(unexpected_cfgs)]
#[path = "../../src/tests/hygiene/mod.rs"]
mod hygiene;

mod gh_4394 {
use pyo3::prelude::*;

#[derive(Eq, Ord, PartialEq, PartialOrd, Clone)]
#[pyclass(get_all)]
pub struct VersionSpecifier {
pub(crate) operator: Operator,
pub(crate) version: Version,
}

#[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Hash, Clone, Copy)]
#[pyo3::pyclass(eq, eq_int)]
pub enum Operator {
Equal,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)]
#[pyclass]
pub struct Version;
}

fn main() {}

0 comments on commit 0270553

Please sign in to comment.