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

Suggested reviewers

  • youknowone

Poem

🐰 Hop, skip, and a bound—old bit-shifts retire,
New indexes() 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.

❤️ Share

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