GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Newtype oparg align methods by ShaharNaveh · Pull Request #7403 · RustPython/RustPython

Conversation

@ShaharNaveh

@ShaharNaveh ShaharNaveh commented Mar 12, 2026 •
edited by coderabbitai Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Refactor
    • Internal code structure improvements across multiple modules for better maintainability and consistency.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ShaharNaveh added 2 commits March 12, 2026 12:35
@ShaharNaveh
Align methods for newtype opargs
51ba230
@ShaharNaveh
Remove redundent code
1d79c46
@coderabbitai

coderabbitai Bot commented Mar 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
📝 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 OpArg to 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_num oparg #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.

❤️ Share
  • X
  • Mastodon
  • Reddit
  • LinkedIn

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ShaharNaveh
fix jit
8acdc3e
@ShaharNaveh
ShaharNaveh marked this pull request as ready for review March 12, 2026 12:46
coderabbitai[bot]
coderabbitai Bot reviewed Mar 12, 2026
View reviewed changes

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
crates/vm/src/frame.rs (1)

6835-6846: Deduplicate the FOR_ITER exhaustion target adjustment.

for_iter_jump_on_exhausted() now re-implements the same END_FOR skip logic as for_iter_jump_target(). Reusing the helper would keep this control-flow rule in one place.

♻️ Proposed cleanup
     /// Handle iterator exhaustion in specialized FOR_ITER handlers.
     /// Skips END_FOR if present at target and jumps.
     fn for_iter_jump_on_exhausted(&mut self, target: bytecode::Label) {
-        let target_idx = target.as_usize();
-        let jump_target = if let Some(unit) = self.code.instructions.get(target_idx) {
-            if matches!(
-                unit.op,
-                bytecode::Instruction::EndFor | bytecode::Instruction::InstrumentedEndFor
-            ) {
-                bytecode::Label::new(target.as_u32() + 1)
-            } else {
-                target
-            }
-        } else {
-            target
-        };
-        self.jump(jump_target);
+        self.jump(self.for_iter_jump_target(target));
     }

Also applies to: 8817-8832

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/vm/src/frame.rs` around lines 6835 - 6846, The FOR_ITER exhaustion
target adjustment logic is duplicated between for_iter_jump_target and
for_iter_jump_on_exhausted; refactor so the rule lives in one place by making
for_iter_jump_on_exhausted call for_iter_jump_target (or extract a shared helper
used by both). Locate the duplicate logic in functions for_iter_jump_target and
for_iter_jump_on_exhausted and remove the repeated END_FOR/InstrumentedEndFor
check from for_iter_jump_on_exhausted, delegating to for_iter_jump_target to
perform the END_FOR skip and return the adjusted bytecode::Label.
crates/compiler-core/src/bytecode/oparg.rs (1)

702-705: From<$name> for usize bakes in a pointer-width assumption.

std intentionally does not provide From<u32> for usize; this macro reintroduces the same potentially truncating cast for every oparg wrapper. I'd keep as_usize() as the explicit conversion and only add a trait impl here if you want a checked TryFrom.

Suggested change
-        impl From<$name> for usize {
-            fn from(value: $name) -> Self {
-                value.as_usize()
-            }
-        }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/compiler-core/src/bytecode/oparg.rs` around lines 702 - 705, The impl
From<$name> for usize bakes in a pointer-width assumption and can truncate on
some platforms; remove that impl and keep callers using the explicit as_usize()
method on the wrapper ($name.as_usize()). If you need a fallible/checked
conversion, add a TryFrom<$name> for usize implementation instead (implement
TryFrom::try_from for $name that performs a safe/checked conversion and returns
a Result), but do not reintroduce the infallible From<$name> for usize.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@crates/compiler-core/src/bytecode/oparg.rs`:
- Around line 702-705: The impl From<$name> for usize bakes in a pointer-width
assumption and can truncate on some platforms; remove that impl and keep callers
using the explicit as_usize() method on the wrapper ($name.as_usize()). If you
need a fallible/checked conversion, add a TryFrom<$name> for usize
implementation instead (implement TryFrom::try_from for $name that performs a
safe/checked conversion and returns a Result), but do not reintroduce the
infallible From<$name> for usize.

In `@crates/vm/src/frame.rs`:
- Around line 6835-6846: The FOR_ITER exhaustion target adjustment logic is
duplicated between for_iter_jump_target and for_iter_jump_on_exhausted; refactor
so the rule lives in one place by making for_iter_jump_on_exhausted call
for_iter_jump_target (or extract a shared helper used by both). Locate the
duplicate logic in functions for_iter_jump_target and for_iter_jump_on_exhausted
and remove the repeated END_FOR/InstrumentedEndFor check from
for_iter_jump_on_exhausted, delegating to for_iter_jump_target to perform the
END_FOR skip and return the adjusted bytecode::Label.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: e1a75ba9-493f-4eb0-99ba-b6eefb796523

📥 Commits

Reviewing files that changed from the base of the PR and between 331029e and 8acdc3e.

📒 Files selected for processing (6)
  • crates/codegen/src/ir.rs
  • crates/compiler-core/src/bytecode.rs
  • crates/compiler-core/src/bytecode/instruction.rs
  • crates/compiler-core/src/bytecode/oparg.rs
  • crates/jit/src/instructions.rs
  • crates/vm/src/frame.rs

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
youknowone
youknowone approved these changes Mar 12, 2026
View reviewed changes

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@youknowone
youknowone merged commit bb36783 into RustPython:main Mar 12, 2026
15 checks passed

Uh oh!

There was an error while loading. Please reload this page.

youknowone pushed a commit to youknowone/RustPython that referenced this pull request Mar 19, 2026
@ShaharNaveh @youknowone
Newtype oparg align methods (RustPython#7403)
56967af
* Align methods for newtype opargs
youknowone pushed a commit to youknowone/RustPython that referenced this pull request Mar 22, 2026
@ShaharNaveh
Newtype oparg align methods (RustPython#7403)
3ac5a49
* Align methods for newtype opargs
This was referenced Mar 24, 2026
#[newtype_oparg] derive macro #7489
Closed
Oparg resume depth #7515
Merged
Remove oparg builders #7537
Merged
youknowone pushed a commit that referenced this pull request Sep 16, 2026
@ShaharNaveh
Newtype oparg align methods (#7403)
6c8f19e
* Align methods for newtype opargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@coderabbitai coderabbitai[bot] coderabbitai[bot] left review comments
@youknowone youknowone youknowone approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ShaharNaveh @youknowone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.