Macro for defining opcode & instruction enums#7573
Macro for defining opcode & instruction enums#7573ShaharNaveh wants to merge 6 commits intoRustPython:mainfrom
Conversation
📝 WalkthroughWalkthroughReplaces hand-written Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| #[repr($typ)] |
There was a problem hiding this comment.
I wanted to remove this #[repr(...)], but when doing so I get the following error:
thread 'main' (3429) panicked at crates/vm/src/frame.rs:3790:33: index out of bounds: the len is 5 but the index is 22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
RustPython/crates/vm/src/frame.rs
Lines 3787 to 3791 in d5a90e5
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
crates/compiler-core/src/bytecode/instruction.rs (1)
32-35: Consider derivingPartialEqandEqfor the opcode enum.The
OpcodeandPseudoOpcodeenums only deriveClone,Copy, andDebug. AddingPartialEqandEqwould enable equality comparisons and pattern matching inmatchguards, which may be useful for consumers of these types.💡 Suggested change
- #[derive(Clone, Copy, Debug)] + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum $opcode_name { $($op_name),* }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/compiler-core/src/bytecode/instruction.rs` around lines 32 - 35, The opcode enums only derive Clone, Copy, and Debug; add PartialEq and Eq to the derive list so consumers can compare values and use match guards. Update the macro-generated enum declarations (the $opcode_name expansion and the concrete Opcode and PseudoOpcode enums) to include #[derive(Clone, Copy, Debug, PartialEq, Eq)] so equality comparisons are available for those types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/compiler-core/src/bytecode/instruction.rs`:
- Around line 1303-1308: The StoreFastMaybeNull instruction currently declares
its oparg var_num as NameIdx but should use the local variable index type;
change the oparg type for StoreFastMaybeNull from NameIdx to oparg::VarNum so it
matches the local-store semantics (same as StoreFast) and uses the correct
symbol names var_num and oparg::VarNum in the instruction definition.
---
Nitpick comments:
In `@crates/compiler-core/src/bytecode/instruction.rs`:
- Around line 32-35: The opcode enums only derive Clone, Copy, and Debug; add
PartialEq and Eq to the derive list so consumers can compare values and use
match guards. Update the macro-generated enum declarations (the $opcode_name
expansion and the concrete Opcode and PseudoOpcode enums) to include
#[derive(Clone, Copy, Debug, PartialEq, Eq)] so equality comparisons are
available for those types.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 490f37ff-9d9d-4813-9ac5-a6ddc00d643f
📒 Files selected for processing (2)
crates/compiler-core/src/bytecode.rscrates/compiler-core/src/bytecode/instruction.rs
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/compiler-core/src/bytecode/instruction.rs (1)
32-35: Consider addingPartialEq,Eq, andHashderives to opcode enums.The generated opcode enums (
Opcode,PseudoOpcode) only deriveClone, Copy, Debug. AddingPartialEq,Eq, and optionallyHashwould enable their use in comparisons and as map/set keys, which could be useful for opcode analysis or caching scenarios.♻️ Suggested derive expansion
- #[derive(Clone, Copy, Debug)] + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum $opcode_name {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/compiler-core/src/bytecode/instruction.rs` around lines 32 - 35, The generated opcode enums (the template enum named by $opcode_name, e.g., Opcode and PseudoOpcode) only derive Clone, Copy, Debug; update the derive attribute to also include PartialEq, Eq, and Hash so these enums can be compared and used as map/set keys; modify the derive line in the enum generation template (the block containing #[derive(Clone, Copy, Debug)] and pub enum $opcode_name { ... }) to add PartialEq, Eq, Hash, and ensure both generated enums are adjusted accordingly.
🤖 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/instruction.rs`:
- Around line 32-35: The generated opcode enums (the template enum named by
$opcode_name, e.g., Opcode and PseudoOpcode) only derive Clone, Copy, Debug;
update the derive attribute to also include PartialEq, Eq, and Hash so these
enums can be compared and used as map/set keys; modify the derive line in the
enum generation template (the block containing #[derive(Clone, Copy, Debug)] and
pub enum $opcode_name { ... }) to add PartialEq, Eq, Hash, and ensure both
generated enums are adjusted accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 2966abcb-4020-4bbc-ba78-d346734c5fff
📒 Files selected for processing (1)
crates/compiler-core/src/bytecode/instruction.rs
|
ping @youknowone |
|
This PR is splitting Instruction and Opcode, right? I wasn't sure what's the benefit based on this changes. Could you tell me more? |
yes, exactly. If this approach is good in your opinion I'll make the rest of the code to use the |
This macro will generate an
Instructionand aOpcodeenum, all within rust, no external python scripts with custom config in toml or something.I'd love to hear what you guys think:)
Summary by CodeRabbit