Skip to content
Merged
Prev Previous commit
Next Next commit
Simplfy casting as checks
  • Loading branch information
ShaharNaveh committed Jan 30, 2026
commit b4e8dea74c3e4ad7ec412a34bd745503dbb292b4
24 changes: 10 additions & 14 deletions crates/compiler-core/src/bytecode/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ impl InstructionMetadata for Instruction {
Self::YieldValue { .. } => (1, 1),
};

debug_assert!(pushed >= 0);
debug_assert!(popped >= 0);
debug_assert!((0..=i32::MAX).contains(&pushed));
debug_assert!((0..=i32::MAX).contains(&popped));

StackEffect::new(pushed as u32, popped as u32)
}
Expand Down Expand Up @@ -1011,8 +1011,8 @@ impl InstructionMetadata for PseudoInstruction {
Self::StoreFastMaybeNull(_) => (0, 1),
};

debug_assert!(pushed >= 0);
debug_assert!(popped >= 0);
debug_assert!((0..=i32::MAX).contains(&pushed));
debug_assert!((0..=i32::MAX).contains(&popped));

StackEffect::new(pushed as u32, popped as u32)
}
Expand Down Expand Up @@ -1157,6 +1157,11 @@ impl StackEffect {
Self { pushed, popped }
}

/// Get the calculated stack effect as [`i32`].
pub fn effect(self) -> i32 {
self.into()
}

/// Get the pushed count.
pub const fn pushed(self) -> u32 {
self.pushed
Expand All @@ -1166,20 +1171,11 @@ impl StackEffect {
pub const fn popped(self) -> u32 {
self.popped
}

/// Get the calculated stack effect as [`i32`].
pub fn effect(self) -> i32 {
self.into()
}
}

impl From<StackEffect> for i32 {
fn from(effect: StackEffect) -> Self {
// SAFETY: Trust `num_pushed` & `num_popped` impls
unsafe {
Self::try_from(effect.pushed()).unwrap_unchecked()
- Self::try_from(effect.popped()).unwrap_unchecked()
}
(effect.pushed() as i32) - (effect.popped() as i32)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

Expand Down
Loading