From 21e7584468273cc42c0b1ab2dd552c15812d2d8b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 31 Jan 2026 15:45:59 +0200 Subject: [PATCH] Make `to_oparg` to return a `Result` --- .../compiler-core/src/bytecode/instruction.rs | 2 +- crates/compiler-core/src/bytecode/oparg.rs | 45 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index 449357fd246..525b87bfe55 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -1240,7 +1240,7 @@ impl Arg { } #[inline(always)] - pub fn try_get(self, arg: OpArg) -> Option { + pub fn try_get(self, arg: OpArg) -> Result { T::from_op_arg(arg.0) } diff --git a/crates/compiler-core/src/bytecode/oparg.rs b/crates/compiler-core/src/bytecode/oparg.rs index c5cf342d074..724fd6fcd10 100644 --- a/crates/compiler-core/src/bytecode/oparg.rs +++ b/crates/compiler-core/src/bytecode/oparg.rs @@ -2,10 +2,13 @@ use bitflags::bitflags; use core::{fmt, num::NonZeroU8}; -use crate::bytecode::{CodeUnit, instruction::Instruction}; +use crate::{ + bytecode::{CodeUnit, instruction::Instruction}, + marshal::MarshalError, +}; pub trait OpArgType: Copy { - fn from_op_arg(x: u32) -> Option; + fn from_op_arg(x: u32) -> Result; fn to_op_arg(self) -> u32; } @@ -158,15 +161,15 @@ impl fmt::Display for ConvertValueOparg { impl OpArgType for ConvertValueOparg { #[inline] - fn from_op_arg(x: u32) -> Option { - Some(match x { + fn from_op_arg(x: u32) -> Result { + Ok(match x { // Ruff `ConversionFlag::None` is `-1i8`, // when its converted to `u8` its value is `u8::MAX` 0 | 255 => Self::None, 1 => Self::Str, 2 => Self::Repr, 3 => Self::Ascii, - _ => return None, + _ => return Err(MarshalError::InvalidBytecode), }) } @@ -188,8 +191,8 @@ pub enum ResumeType { impl OpArgType for u32 { #[inline(always)] - fn from_op_arg(x: u32) -> Option { - Some(x) + fn from_op_arg(x: u32) -> Result { + Ok(x) } #[inline(always)] @@ -200,8 +203,8 @@ impl OpArgType for u32 { impl OpArgType for bool { #[inline(always)] - fn from_op_arg(x: u32) -> Option { - Some(x != 0) + fn from_op_arg(x: u32) -> Result { + Ok(x != 0) } #[inline(always)] @@ -217,10 +220,10 @@ macro_rules! op_arg_enum_impl { self as u32 } - fn from_op_arg(x: u32) -> Option { - Some(match u8::try_from(x).ok()? { + fn from_op_arg(x: u32) -> Result { + Ok(match u8::try_from(x).map_err(|_| MarshalError::InvalidBytecode)? { $($value => Self::$var,)* - _ => return None, + _ => return Err(MarshalError::InvalidBytecode), }) } } @@ -248,8 +251,8 @@ pub struct Label(pub u32); impl OpArgType for Label { #[inline(always)] - fn from_op_arg(x: u32) -> Option { - Some(Self(x)) + fn from_op_arg(x: u32) -> Result { + Ok(Self(x)) } #[inline(always)] @@ -341,8 +344,8 @@ bitflags! { impl OpArgType for MakeFunctionFlags { #[inline(always)] - fn from_op_arg(x: u32) -> Option { - Self::from_bits(x as u8) + fn from_op_arg(x: u32) -> Result { + Self::from_bits(x as u8).ok_or(MarshalError::InvalidBytecode) } #[inline(always)] @@ -601,11 +604,11 @@ pub enum BuildSliceArgCount { impl OpArgType for BuildSliceArgCount { #[inline(always)] - fn from_op_arg(x: u32) -> Option { - Some(match x { + fn from_op_arg(x: u32) -> Result { + Ok(match x { 2 => Self::Two, 3 => Self::Three, - _ => return None, + _ => return Err(MarshalError::InvalidBytecode), }) } @@ -636,9 +639,9 @@ pub struct UnpackExArgs { impl OpArgType for UnpackExArgs { #[inline(always)] - fn from_op_arg(x: u32) -> Option { + fn from_op_arg(x: u32) -> Result { let [before, after, ..] = x.to_le_bytes(); - Some(Self { before, after }) + Ok(Self { before, after }) } #[inline(always)]