fix: prevent infinite loop and emit a clear error when using prefixed pyclass attributes (e.g. vm::pymethod) - #8098
fix: prevent infinite loop and emit a clear error when using prefixed pyclass attributes (e.g. vm::pymethod)#8098youknowone with Copilot wants to merge 4 commits into
vm::pymethod)#8098Conversation
…ributes like vm::pymethod
vm::pymethod)
|
@mapf0000 you are right that current behavior is wrong. Thank you for feedback. @copilot you must add a test that the new code is working correctly. also check mapf0000's suggestion about vm::pymethod. Can Rust proc-macro recognize |
Done — addressed both points (commits cover the latest two changes on top of the original fix): Namespace alias question: proc-macros receive raw token trees before name resolution, so Warning for Test: Added a runtime test in |
vm::pymethod)vm::pymethod)
|
@copilot we all know |
Added a unit test directly in
Commit: the latest commit |
| let result = | ||
| attrs_to_content_items(&[attr], |i, name| (i, name)); |
There was a problem hiding this comment.
[reviewdog-suggester] reported by reviewdog 🐶

cargo checkwould hang indefinitely (100% CPU, no output) whenever an inner py attribute like#[pymethod]was written with a path prefix (e.g.#[vm::pymethod]) inside a#[pyclass]impl block.Root cause
attrs_to_content_itemsincrates/derive-impl/src/pyclass.rsiterated attributes with aPeekableiterator but placediter.next()after acontinue, so it was never reached whenattr.get_ident()returnedNone.get_ident()returnsNonefor multi-segment paths (vm::pymethod) — only single-segment names (pymethod) returnSome. The same attribute was peeked forever.Fix
The loops in
attrs_to_content_itemsare updated to handle multi-segment attribute paths:whileloop (which collects#[cfg]attrs before the first py item) nowbreaks when it encounters a multi-segment path whose last segment is a known py attribute name, instead of silently consuming it.forloop (which processes py items) now emits a clear compile error when it sees such an attribute, pointing the user to the unqualified form.Proc-macros receive raw token trees before name resolution, so
vmin#[vm::pymethod]is just an opaque identifier — the macro cannot know it is an alias forrustpython_vm, and the qualified form can never be supported.Testing
crates/vm/src/lib.rsdefines aTestItemstruct with#[pyclass]/#[pymethod](correct unqualified form), instantiates it, and assertsitem.value() == 42, verifying the macro-generated code is functionally correct.crates/derive-impl/src/pyclass.rsdirectly callsattrs_to_content_itemswith a#[vm::pymethod]attribute (constructed viasyn::parse_quote!) and asserts that an error is returned whose message contains both the full path (vm::pymethod) and the unqualified name (pymethod), confirming the error path is exercised and not silently ignored or looped.