Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unintentional unsafe_code trigger #4574

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion newsfragments/4396.fixed.md

This file was deleted.

2 changes: 2 additions & 0 deletions newsfragments/4574.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes `#[forbid(unsafe_code)]` regression by reverting #4396.
Fixes unintentional `unsafe_code` trigger by adjusting macro hygiene.
1 change: 0 additions & 1 deletion pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,6 @@ fn impl_pytypeinfo(cls: &syn::Ident, attr: &PyClassArgs, ctx: &Ctx) -> TokenStre
};

quote! {
#[allow(unsafe_code)]
unsafe impl #pyo3_path::type_object::PyTypeInfo for #cls {
const NAME: &'static str = #cls_name;
const MODULE: ::std::option::Option<&'static str> = #module;
Expand Down
15 changes: 9 additions & 6 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,22 +766,27 @@ 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
use #pyo3_path::impl_::pyclass::Probe;

struct Offset;
#[allow(unsafe_code)]
unsafe impl #pyo3_path::impl_::pyclass::OffsetCalculator<#cls, #ty> for Offset {
fn offset() -> usize {
#pyo3_path::impl_::pyclass::class_offset::<#cls>() +
#pyo3_path::impl_::pyclass::offset_of!(#cls, #field)
}
}

#[allow(unsafe_code)]
const GENERATOR: #pyo3_path::impl_::pyclass::PyClassGetterGenerator::<
#cls,
#ty,
Expand All @@ -792,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
6 changes: 1 addition & 5 deletions src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ slot_fragment_trait! {
#[macro_export]
macro_rules! generate_pyclass_getattro_slot {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
attr: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -435,7 +434,6 @@ macro_rules! define_pyclass_setattr_slot {
#[macro_export]
macro_rules! $generate_macro {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
attr: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -552,7 +550,6 @@ macro_rules! define_pyclass_binary_operator_slot {
#[macro_export]
macro_rules! $generate_macro {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
_other: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -745,7 +742,6 @@ slot_fragment_trait! {
#[macro_export]
macro_rules! generate_pyclass_pow_slot {
($cls:ty) => {{
#[allow(unsafe_code)]
unsafe extern "C" fn __wrap(
_slf: *mut $crate::ffi::PyObject,
_other: *mut $crate::ffi::PyObject,
Expand Down Expand Up @@ -870,7 +866,7 @@ macro_rules! generate_pyclass_richcompare_slot {
($cls:ty) => {{
#[allow(unknown_lints, non_local_definitions)]
impl $cls {
#[allow(non_snake_case, unsafe_code)]
#[allow(non_snake_case)]
unsafe extern "C" fn __pymethod___richcmp____(
slf: *mut $crate::ffi::PyObject,
other: *mut $crate::ffi::PyObject,
Expand Down
1 change: 0 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ macro_rules! wrap_pymodule {
#[macro_export]
macro_rules! append_to_inittab {
($module:ident) => {
#[allow(unsafe_code)]
unsafe {
if $crate::ffi::Py_IsInitialized() != 0 {
::std::panic!(
Expand Down
1 change: 0 additions & 1 deletion src/tests/hygiene/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![no_implicit_prelude]
#![allow(dead_code, unused_variables, clippy::unnecessary_wraps)]
#![deny(unsafe_code)]

// The modules in this test are used to check PyO3 macro expansion is hygienic. By locating the test
// inside the crate the global `::pyo3` namespace is not available, so in combination with
Expand Down
1 change: 0 additions & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ macro_rules! pyobject_native_static_type_object(
#[macro_export]
macro_rules! pyobject_native_type_info(
($name:ty, $typeobject:expr, $module:expr $(, #checkfunction=$checkfunction:path)? $(;$generics:ident)*) => {
#[allow(unsafe_code)]
unsafe impl<$($generics,)*> $crate::type_object::PyTypeInfo for $name {
const NAME: &'static str = stringify!($name);
const MODULE: ::std::option::Option<&'static str> = $module;
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)]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, does it make sense to include all the hygiene tests in here?

Something like

use pyo3::*;  // to make this `crate` match PyO3's `crate` used in the hygiene tests

#[path = "../../src/tests/hygiene/mod.rs"]
mod hygiene;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very smart! This can't test the cfg variants in there, but I think that's fine, so I simply allowed unexpected_cfgs for that.

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() {}
Loading