Skip to content

Commit

Permalink
preserve span of pyo3_path
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored and mejrs committed Mar 3, 2024
1 parent 00c9aed commit 2e0686e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/deprecations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
4 changes: 4 additions & 0 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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>()
Expand Down
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ fn impl_arg_param(
ctx: &Ctx,
) -> Result<TokenStream> {
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
Expand Down
9 changes: 9 additions & 0 deletions pyo3-macros-backend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_proto_pymethods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2e0686e

Please sign in to comment.