◐ Shell
clean mode source ↗

`#[newtype_oparg]` derive macro by ShaharNaveh · Pull Request #7489 · RustPython/RustPython

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a new procedural-macro crate rustpython-macros providing #[newtype_oparg], updates workspace and compiler-core dependencies to use it, refactors many bytecode oparg enums/newtypes to use the macro-generated implementations, and adjusts VM frame handling for ContainsOp* invert parsing.

Changes

Cohort / File(s) Summary
Workspace Manifest
Cargo.toml
Add workspace dependency rustpython-macros = { path = "crates/macros", version = "0.5.0" }.
Compiler Core Dependencies
crates/compiler-core/Cargo.toml
Add rustpython-macros = { workspace = true } under [dependencies].
New Proc-macro Crate Manifest
crates/macros/Cargo.toml
New crate manifest for rustpython-macros configured as a procedural macro with workspace-sourced metadata and proc-macro deps (proc-macro2, quote, syn).
Proc-macro Entrypoint
crates/macros/src/lib.rs
Add #[proc_macro_attribute] pub fn newtype_oparg(...) that dispatches to enum/struct handlers and emits compile-time diagnostics.
Proc-macro Generator
crates/macros/src/newtype_oparg.rs
Implement macro logic: validate #[oparg(...)] attributes, generate tuple-newtype structs and enums, produce From/TryFrom conversions, Display impls, catch-all handling, and OpArgType impls with diagnostics.
Bytecode Oparg Refactor
crates/compiler-core/src/bytecode/oparg.rs
Replace local oparg_enum!/newtype_oparg! macros with #[newtype_oparg]-annotated enums/structs for many oparg types (e.g., ConvertValueOparg, ResumeType, RaiseKind, IntrinsicFunction1/2, BinaryOperator, Invert, SpecialMethod, CommonConstant, BuildSliceArgCount, and newtypes like ConstIdx, VarNum, VarNums, LoadAttr, LoadSuperAttr, Label); remove manual From/Display impls and adjust discriminants/representations (notably ResumeType::Other(u32) catch-all).
VM Instruction Handling
crates/vm/src/frame.rs
Parse invert flag for Instruction::ContainsOp* from full u32 instead of truncating to u8, preserving fallback to Invert::No on parse failure.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • youknowone
  • coolreader18

Poem

🐰 I hopped through crates with tokens in paw,
I stitched newtypes where old macros once saw,
Enums wear labels, catch-alls hum low,
Bytecode now leaps where the carrots grow,
Compile and hop — a rabbit's small-wow!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: introduction of a #[newtype_oparg] derive macro. It matches the substantial refactoring throughout the codebase where local macros are replaced with this new derive macro.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.