From 2e0686ef629f8fb2a60557f574f73baacb3a2ad3 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Fri, 1 Mar 2024 09:57:31 +0000 Subject: [PATCH] preserve span of pyo3_path --- pyo3-macros-backend/src/deprecations.rs | 1 + pyo3-macros-backend/src/method.rs | 4 ++++ pyo3-macros-backend/src/params.rs | 1 + pyo3-macros-backend/src/utils.rs | 9 +++++++++ tests/ui/invalid_proto_pymethods.stderr | 2 +- 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pyo3-macros-backend/src/deprecations.rs b/pyo3-macros-backend/src/deprecations.rs index 3aaa4c8c765..3f1f34144f6 100644 --- a/pyo3-macros-backend/src/deprecations.rs +++ b/pyo3-macros-backend/src/deprecations.rs @@ -32,6 +32,7 @@ impl<'ctx> ToTokens for Deprecations<'ctx> { let Self(deprecations, Ctx { pyo3_path }) = self; for (deprecation, span) in deprecations { + let pyo3_path = pyo3_path.to_tokens_spanned(*span); let ident = deprecation.ident(*span); quote_spanned!( *span => diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index 21b0177a9db..1d2d22b236b 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -129,6 +129,7 @@ impl FnType { FnType::FnClass(span) | FnType::FnNewClass(span) => { let py = syn::Ident::new("py", Span::call_site()); let slf: Ident = syn::Ident::new("_slf", Span::call_site()); + let pyo3_path = pyo3_path.to_tokens_spanned(*span); quote_spanned! { *span => #[allow(clippy::useless_conversion)] ::std::convert::Into::into( @@ -140,6 +141,7 @@ impl FnType { FnType::FnModule(span) => { let py = syn::Ident::new("py", Span::call_site()); let slf: Ident = syn::Ident::new("_slf", Span::call_site()); + let pyo3_path = pyo3_path.to_tokens_spanned(*span); quote_spanned! { *span => #[allow(clippy::useless_conversion)] ::std::convert::Into::into( @@ -200,6 +202,7 @@ impl SelfType { syn::Ident::new("extract_pyclass_ref", *span) }; let holder = syn::Ident::new(&format!("holder_{}", holders.len()), *span); + let pyo3_path = pyo3_path.to_tokens_spanned(*span); holders.push(quote_spanned! { *span => #[allow(clippy::let_unit_value)] let mut #holder = #pyo3_path::impl_::extract_argument::FunctionArgumentHolder::INIT; @@ -216,6 +219,7 @@ impl SelfType { ) } SelfType::TryFromBoundRef(span) => { + let pyo3_path = pyo3_path.to_tokens_spanned(*span); error_mode.handle_error( quote_spanned! { *span => #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &#slf).downcast::<#cls>() diff --git a/pyo3-macros-backend/src/params.rs b/pyo3-macros-backend/src/params.rs index 97a52962710..7260362fa43 100644 --- a/pyo3-macros-backend/src/params.rs +++ b/pyo3-macros-backend/src/params.rs @@ -151,6 +151,7 @@ fn impl_arg_param( ctx: &Ctx, ) -> Result { let Ctx { pyo3_path } = ctx; + let pyo3_path = pyo3_path.to_tokens_spanned(arg.ty.span()); // Use this macro inside this function, to ensure that all code generated here is associated // with the function argument diff --git a/pyo3-macros-backend/src/utils.rs b/pyo3-macros-backend/src/utils.rs index 7a94021bb0f..ca32abb42b3 100644 --- a/pyo3-macros-backend/src/utils.rs +++ b/pyo3-macros-backend/src/utils.rs @@ -164,6 +164,15 @@ pub enum PyO3CratePath { Default, } +impl PyO3CratePath { + pub fn to_tokens_spanned(&self, span: Span) -> TokenStream { + match self { + Self::Given(path) => quote::quote_spanned! { span => #path }, + Self::Default => quote::quote_spanned! { span => ::pyo3 }, + } + } +} + impl quote::ToTokens for PyO3CratePath { fn to_tokens(&self, tokens: &mut TokenStream) { match self { diff --git a/tests/ui/invalid_proto_pymethods.stderr b/tests/ui/invalid_proto_pymethods.stderr index 275a6b93c46..c9f6adff3a1 100644 --- a/tests/ui/invalid_proto_pymethods.stderr +++ b/tests/ui/invalid_proto_pymethods.stderr @@ -31,4 +31,4 @@ error[E0592]: duplicate definitions with name `__pymethod___richcmp____` | duplicate definitions for `__pymethod___richcmp____` | other definition for `__pymethod___richcmp____` | - = note: this error originates in the macro `_pyo3::impl_::pyclass::generate_pyclass_richcompare_slot` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info) + = note: this error originates in the macro `::pyo3::impl_::pyclass::generate_pyclass_richcompare_slot` which comes from the expansion of the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)