Skip to content

Commit

Permalink
emit compile errors on macros inside #[pymethods]
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Oct 2, 2023
1 parent f335f42 commit b3fcbd8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/3491.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Emit compile errors instead of ignoring macro invocations inside `#[pymethods]` blocks.
7 changes: 6 additions & 1 deletion pyo3-macros-backend/src/pyimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ pub fn impl_methods(
}
}
}
_ => (),
syn::ImplItem::Macro(m) => bail_spanned!(
m.span() =>
"macros cannot be used in `#[pymethods]` impl blocks\n\
= note: this was previously accepted and ignored"
),
_ => {}
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/invalid_pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,13 @@ impl DuplicateMethod {
fn func_b(&self) {}
}

macro_rules! macro_invocation {
() => {};
}

#[pymethods]
impl MyClass {
macro_invocation!();
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/ui/invalid_pymethods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ error: Python objects are shared, so 'self' cannot be moved out of the Python in
144 | fn method_self_by_value(self) {}
| ^^^^

error: macros cannot be used in `#[pymethods]` impl blocks
= note: this was previously accepted and ignored
--> tests/ui/invalid_pymethods.rs:179:5
|
179 | macro_invocation!();
| ^^^^^^^^^^^^^^^^

error[E0119]: conflicting implementations of trait `pyo3::impl_::pyclass::PyClassNewTextSignature<TwoNew>` for type `pyo3::impl_::pyclass::PyClassImplCollector<TwoNew>`
--> tests/ui/invalid_pymethods.rs:149:1
|
Expand Down

0 comments on commit b3fcbd8

Please sign in to comment.