Newtype `var_nums` oparg by ShaharNaveh · Pull Request #7431 · RustPython/RustPython
📝 Walkthrough
Walkthrough
This PR refactors the bytecode instruction type system by introducing a new VarNums type for encoding multiple variable operands and replacing manual bit-shifting logic with a dedicated indexes() method. The changes propagate through instruction variants, display logic, JIT, and VM frame handling to use the new type-safe accessor pattern.
Changes
| Cohort / File(s) | Summary |
|---|---|
Oparg Type System Refactoring crates/compiler-core/src/bytecode/oparg.rs |
Type names reorganized: LoadAttr → VarNums, LoadSuperAttr → LoadAttr, Label → LoadSuperAttr, StoreFastLoadFast → Label. New VarNums impl provides idx_1(), idx_2(), and indexes() accessors replacing separate store_idx/load_idx logic. |
Instruction Variant Updates crates/compiler-core/src/bytecode/instruction.rs |
Several bytecode instructions updated to use Arg<oparg::VarNums> for var_nums fields instead of Arg<u32> or custom types. Display logic for LOAD_FAST_LOAD_FAST, LOAD_FAST_BORROW_LOAD_FAST_BORROW, STORE_FAST_LOAD_FAST, and STORE_FAST_STORE_FAST now calls oparg.indexes() instead of manual bit-shifting. Removed StoreFastLoadFast from public imports. |
Index Extraction Simplification crates/jit/src/instructions.rs, crates/vm/src/frame.rs |
Replaced manual index extraction via bit-shift operations (idx = oparg >> 4; idx = oparg & 0xF) with structured oparg.indexes() calls. Frame handling updated to use var_nums.get(arg).indexes() for loading and storing variable pairs. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
- Newtype
var_numoparg #7400: IntroducesVarNum/VarNumstypes and refactors oparg newtype encoding, directly paralleling the type system changes in this PR. - slice ops and a little bit of peephole #6860: Introduces the fused two-variable instructions (LoadFastLoadFast, StoreFastStoreFast, etc.) that this PR refactors to use the new
VarNums/indexes()API. - Use
num_enumcrate for oparg types #6980: Modifies the oparg type system with new accessor methods and conversion traits that establish the foundation for the centralized index extraction approach.
Suggested reviewers
- youknowone
Poem
🐰 Hop, skip, and a bound—old bit-shifts retire,
Newindexes()dance where opcodes conspire!
VarNums embrace the variables with grace,
No more shifting masks all over the place! ✨
🚥 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 'Newtype var_nums oparg' accurately describes the main change: introducing a new VarNums type that replaces several oparg-like types and consolidates their accessor logic. |
| 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
📝 Coding Plan
- Generate coding plan for human review comments
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.