Skip to content

Commit

Permalink
silence non-local-definitions nightly lint (#3901)
Browse files Browse the repository at this point in the history
* silence non-local-definitions nightly lint

* add newsfragment

* add FIXMEs for `non_local_definitions`

* also allow `non_local_definitions` in doctests
  • Loading branch information
davidhewitt authored Feb 26, 2024
1 parent 404161c commit 8e2219b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions newsfragments/3901.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `non_local_definitions` lint warning triggered by many PyO3 macros.
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {

let ident = &tokens.ident;
Ok(quote!(
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate as _pyo3;
use _pyo3::prelude::PyAnyMethods;
Expand Down
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
// this avoids complications around the fact that the generated module has a different scope
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pymodule] is
// inside a function body)
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate::impl_::pymodule as impl_;

Expand Down
6 changes: 6 additions & 0 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ fn impl_class(
.impl_all()?;

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate as _pyo3;

Expand Down Expand Up @@ -783,6 +785,8 @@ fn impl_simple_enum(
.impl_all()?;

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate as _pyo3;

Expand Down Expand Up @@ -917,6 +921,8 @@ fn impl_complex_enum(
}

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate as _pyo3;

Expand Down
3 changes: 3 additions & 0 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ pub fn impl_wrap_pyfunction(
// this avoids complications around the fact that the generated module has a different scope
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pyfunction] is
// inside a function body)
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate as _pyo3;

impl #name::MakeDef {
const DEF: #krate::impl_::pyfunction::PyMethodDef = #methoddef;
}
Expand Down
2 changes: 2 additions & 0 deletions pyo3-macros-backend/src/pyimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ pub fn impl_methods(
};

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #krate as _pyo3;

Expand Down
23 changes: 12 additions & 11 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,18 @@ impl SlotFragmentDef {
)?;
let ret_ty = ret_ty.ffi_type();
Ok(quote! {
impl #cls {
unsafe fn #wrapper_ident(
py: _pyo3::Python,
_raw_slf: *mut _pyo3::ffi::PyObject,
#(#arg_idents: #arg_types),*
) -> _pyo3::PyResult<#ret_ty> {
let _slf = _raw_slf;
#( #holders )*
#body
}
}

impl _pyo3::impl_::pyclass::#fragment_trait<#cls> for _pyo3::impl_::pyclass::PyClassImplCollector<#cls> {

#[inline]
Expand All @@ -1250,17 +1262,6 @@ impl SlotFragmentDef {
_raw_slf: *mut _pyo3::ffi::PyObject,
#(#arg_idents: #arg_types),*
) -> _pyo3::PyResult<#ret_ty> {
impl #cls {
unsafe fn #wrapper_ident(
py: _pyo3::Python,
_raw_slf: *mut _pyo3::ffi::PyObject,
#(#arg_idents: #arg_types),*
) -> _pyo3::PyResult<#ret_ty> {
let _slf = _raw_slf;
#( #holders )*
#body
}
}
#cls::#wrapper_ident(py, _raw_slf, #(#arg_idents),*)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use std::os::raw::c_char;
#[macro_export]
macro_rules! impl_exception_boilerplate {
($name: ident) => {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
impl ::std::convert::From<&$name> for $crate::PyErr {
#[inline]
fn from(err: &$name) -> $crate::PyErr {
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
rust_2021_prelude_collisions,
warnings
),
allow(unused_variables, unused_assignments, unused_extern_crates)
allow(
unused_variables,
unused_assignments,
unused_extern_crates,
// FIXME https://github.com/rust-lang/rust/issues/121621#issuecomment-1965156376
unknown_lints,
non_local_definitions,
)
)))]

//! Rust bindings to the Python interpreter.
Expand Down
8 changes: 8 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,17 @@ macro_rules! pyobject_native_type_named (
}
}

// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
impl<$($generics,)*> $crate::IntoPy<$crate::Py<$name>> for &'_ $name {
#[inline]
fn into_py(self, py: $crate::Python<'_>) -> $crate::Py<$name> {
unsafe { $crate::Py::from_borrowed_ptr(py, self.as_ptr()) }
}
}

// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
impl<$($generics,)*> ::std::convert::From<&'_ $name> for $crate::Py<$name> {
#[inline]
fn from(other: &$name) -> Self {
Expand All @@ -203,6 +207,8 @@ macro_rules! pyobject_native_type_named (
}
}

// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
impl<'a, $($generics,)*> ::std::convert::From<&'a $name> for &'a $crate::PyAny {
fn from(ob: &'a $name) -> Self {
unsafe{&*(ob as *const $name as *const $crate::PyAny)}
Expand Down Expand Up @@ -252,6 +258,8 @@ macro_rules! pyobject_native_type_info(
#[macro_export]
macro_rules! pyobject_native_type_extract {
($name:ty $(;$generics:ident)*) => {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
impl<'py, $($generics,)*> $crate::FromPyObject<'py> for &'py $name {
#[inline]
fn extract_bound(obj: &$crate::Bound<'py, $crate::PyAny>) -> $crate::PyResult<Self> {
Expand Down

0 comments on commit 8e2219b

Please sign in to comment.