Skip to content

Commit

Permalink
fix incorrect spans on ret and py local varianbles in emitted code (
Browse files Browse the repository at this point in the history
#4382)

* fix incorrect spans on `ret` and `py` local varianbles in emitted code

* add newsfragment
  • Loading branch information
Icxolu authored and davidhewitt committed Sep 15, 2024
1 parent 898ef1b commit 4525bc7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions newsfragments/4382.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed hygiene/span issues of emitted code which affected expansion in `macro_rules` context.
7 changes: 4 additions & 3 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,10 @@ impl<'a> FnSpec<'a> {

// We must assign the output_span to the return value of the call,
// but *not* of the call itself otherwise the spans get really weird
let ret_expr = quote! { let ret = #call; };
let ret_var = quote_spanned! {*output_span=> ret };
let return_conversion = quotes::map_result_into_ptr(quotes::ok_wrap(ret_var, ctx), ctx);
let ret_ident = Ident::new("ret", *output_span);
let ret_expr = quote! { let #ret_ident = #call; };
let return_conversion =
quotes::map_result_into_ptr(quotes::ok_wrap(ret_ident.to_token_stream(), ctx), ctx);
quote! {
{
#ret_expr
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ impl<'a> PyClassImplsBuilder<'a> {
if attr.options.extends.is_none() {
quote! {
impl #pyo3_path::IntoPy<#pyo3_path::PyObject> for #cls {
fn into_py(self, py: #pyo3_path::Python) -> #pyo3_path::PyObject {
fn into_py(self, py: #pyo3_path::Python<'_>) -> #pyo3_path::PyObject {
#pyo3_path::IntoPy::into_py(#pyo3_path::Py::new(py, self).unwrap(), py)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pyo3-macros-backend/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ pub(crate) fn map_result_into_ptr(result: TokenStream, ctx: &Ctx) -> TokenStream
output_span,
} = ctx;
let pyo3_path = pyo3_path.to_tokens_spanned(*output_span);
quote_spanned! {*output_span=> #pyo3_path::impl_::wrap::map_result_into_ptr(py, #result) }
let py = syn::Ident::new("py", proc_macro2::Span::call_site());
quote_spanned! {*output_span=> #pyo3_path::impl_::wrap::map_result_into_ptr(#py, #result) }
}
19 changes: 19 additions & 0 deletions src/tests/hygiene/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ fn append_to_inittab() {

crate::append_to_inittab!(module_for_inittab);
}

macro_rules! macro_rules_hygiene {
($name_a:ident, $name_b:ident) => {
#[crate::pyclass(crate = "crate")]
struct $name_a {}

#[crate::pymethods(crate = "crate")]
impl $name_a {
fn finalize(&mut self) -> $name_b {
$name_b {}
}
}

#[crate::pyclass(crate = "crate")]
struct $name_b {}
};
}

macro_rules_hygiene!(MyClass1, MyClass2);

0 comments on commit 4525bc7

Please sign in to comment.