Skip to content

Add const methods for oparg enums#7617

Open
ShaharNaveh wants to merge 2 commits intoRustPython:mainfrom
ShaharNaveh:const-oparg-methods
Open

Add const methods for oparg enums#7617
ShaharNaveh wants to merge 2 commits intoRustPython:mainfrom
ShaharNaveh:const-oparg-methods

Conversation

@ShaharNaveh
Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh commented Apr 16, 2026

Until rust-lang/rust#143874 is available for stable rust I believe there's a benefit for providing a const implementation of a trait where possible.

Summary by CodeRabbit

  • Refactor
    • Streamlined value encoding/decoding logic in the compiler core to use explicit conversion helpers, improving consistency and reliability across enum-like bytecode values.
  • Bug Fixes
    • Removed legacy alternative literal encodings; decoding is now strict to declared values and a default "None" value was normalized, preventing ambiguous interpretations.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 8a532544-cefb-40a0-8279-7fe5d1151f8f

📥 Commits

Reviewing files that changed from the base of the PR and between 7702143 and 946800e.

📒 Files selected for processing (1)
  • crates/compiler-core/src/bytecode/oparg.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/compiler-core/src/bytecode/oparg.rs

📝 Walkthrough

Walkthrough

Removed alternative literal encodings from oparg_enum!/impl_oparg_enum!. Added inherent conversion helpers (as_u8, as_u32, try_from_u8, try_from_u32) and routed TryFrom/From impls through them. ConvertValueOparg::None encoding changed from 0 | 255 to 0.

Changes

Cohort / File(s) Summary
Macro refactoring & enum conversions
crates/compiler-core/src/bytecode/oparg.rs
Removed support for alternative enum literal encodings in oparg_enum!/impl_oparg_enum!. Added inherent methods as_u8, as_u32, try_from_u8, try_from_u32. TryFrom<u8>/TryFrom<u32> now delegate to the new helpers (with u8::MAX range check for u32). From<Enum> for u8/u32 use as_u8/as_u32. ConvertValueOparg::None literal changed from `0

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐇 I hopped through code at break of dawn,

trimmed old paths that led me wrong.
Now one clear value sings so bright,
helpers guide it into light.
A tiny hop, a tidy song.

🚥 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 describes the main change: adding const methods to oparg enums, which matches the primary alteration of introducing const conversion helpers (as_u8, as_u32, try_from_u8, try_from_u32).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant