Newtype oparg align methods by ShaharNaveh · Pull Request #7403 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This PR refactors the Label oparg type from a tuple struct with direct field access to a newtype with constructor and accessor methods. Multiple oparg types (Label, StoreFastLoadFast, ConstIdx, VarNum, LoadAttr, LoadSuperAttr) are reimplemented using a macro-driven newtype pattern. Call sites across codegen, compiler-core, jit, and vm are systematically updated to use Label::new() constructors and as_u32()/as_usize() accessors instead of tuple field access.
Changes
| Cohort / File(s) | Summary |
|---|---|
Oparg Type Refactoring crates/compiler-core/src/bytecode/oparg.rs |
Introduces newtype_oparg macro to generate uniform newtypes for oparg types. Replaces ad-hoc Label/StoreFastLoadFast implementations with macro-generated versions. Adds ConstIdx, VarNum, LoadAttr, LoadSuperAttr newtypes and LoadSuperAttrBuilder. All types now include constructor, conversion, and Display impls via macro. |
Label Constructor Method Addition crates/compiler-core/src/bytecode.rs |
Adds Label::new(u32) constructor in CodeObject::display_inner call site. |
Label API Updates - Codegen & JIT crates/codegen/src/ir.rs, crates/jit/src/instructions.rs |
Replace Label tuple construction Label(value) with Label::new(value) and field access .0 with as_u32() / as_usize() accessors in jump target calculations. |
Label API Updates - VM crates/vm/src/frame.rs |
Systematically update all Label construction and access patterns across jump operations, exception handling, and iterator instructions to use Label::new() constructors and as_u32() / as_usize() accessors. |
Instruction Formatting crates/compiler-core/src/bytecode/instruction.rs |
Adjust fmt_dis macro to format argument without explicit u32 conversion in display output. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
- Move
OpArgto its own file #6703: Moves/defines Label in oparg module; this PR updates call sites with new Label::new/as_u32 API to match that refactor. - Newtype
var_numoparg #7400: Introduces VarNum newtype using similar macro-based oparg machinery; both PRs establish the newtype pattern for opargs. - Rework exception compiler #6909: Modifies Label oparg representation and updates call sites handling Arg<Label>; related refactor of same type's API.
Suggested reviewers
- youknowone
Poem
🐰 A Label once lived tuple-free,
With constructor methods, oh what glee!
As_u32() reads its heart so true,
A macro-born newtype, shiny and new!
From codegen to VM, changes all align,
A refactor so neat, it's divine! ✨
🚥 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 accurately reflects the main refactoring work: converting oparg types to newtypes with a uniform constructor/accessor method interface. |
| 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
- Post copyable unit tests in a comment
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.
Comment @coderabbitai help to get the list of available commands and usage tips.