From e04b88d1fcf55ff5dc631423aa158f1fd0c79578 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 2 Dec 2025 13:37:12 +0200 Subject: [PATCH 1/6] Align `f-string` related bytecodes with 3.13 --- crates/codegen/src/compile.rs | 74 ++++++++++-------- crates/compiler-core/src/bytecode.rs | 112 ++++++++++++++++++++++----- crates/vm/src/frame.rs | 25 ++++-- 3 files changed, 152 insertions(+), 59 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index e8dc269dca8..4656fbe75b2 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -21,22 +21,23 @@ use malachite_bigint::BigInt; use num_complex::Complex; use num_traits::{Num, ToPrimitive}; use ruff_python_ast::{ - Alias, Arguments, BoolOp, CmpOp, Comprehension, ConversionFlag, DebugText, Decorator, DictItem, - ExceptHandler, ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, - ExprFString, ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp, - FString, FStringFlags, FStringPart, Identifier, Int, InterpolatedElement, - InterpolatedStringElement, InterpolatedStringElements, Keyword, MatchCase, ModExpression, - ModModule, Operator, Parameters, Pattern, PatternMatchAs, PatternMatchClass, - PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchSingleton, - PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, TypeParam, TypeParamParamSpec, - TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, WithItem, + Alias, Arguments, BoolOp, CmpOp, Comprehension, DebugText, Decorator, DictItem, ExceptHandler, + ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, ExprFString, + ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp, FString, + FStringFlags, FStringPart, Identifier, Int, InterpolatedElement, InterpolatedStringElement, + InterpolatedStringElements, Keyword, MatchCase, ModExpression, ModModule, Operator, Parameters, + Pattern, PatternMatchAs, PatternMatchClass, PatternMatchMapping, PatternMatchOr, + PatternMatchSequence, PatternMatchSingleton, PatternMatchStar, PatternMatchValue, Singleton, + Stmt, StmtExpr, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, + TypeParams, UnaryOp, WithItem, }; use ruff_text_size::{Ranged, TextRange}; use rustpython_compiler_core::{ Mode, OneIndexed, PositionEncoding, SourceFile, SourceLocation, bytecode::{ self, Arg as OpArgMarker, BinaryOperator, BuildSliceArgCount, CodeObject, - ComparisonOperator, ConstantData, Instruction, Invert, OpArg, OpArgType, UnpackExArgs, + ComparisonOperator, ConstantData, ConversionFlag, Instruction, Invert, OpArg, OpArgType, + UnpackExArgs, }, }; use rustpython_wtf8::Wtf8Buf; @@ -5636,7 +5637,12 @@ impl Compiler { } } InterpolatedStringElement::Interpolation(fstring_expr) => { - let mut conversion = fstring_expr.conversion; + let mut conversion = match fstring_expr.conversion { + ruff_python_ast::ConversionFlag::None => ConversionFlag::None, + ruff_python_ast::ConversionFlag::Str => ConversionFlag::Str, + ruff_python_ast::ConversionFlag::Repr => ConversionFlag::Repr, + ruff_python_ast::ConversionFlag::Ascii => ConversionFlag::Ascii, + }; if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text { let range = fstring_expr.expression.range(); @@ -5645,35 +5651,37 @@ impl Compiler { self.emit_load_const(ConstantData::Str { value: text.into() }); element_count += 1; + + // Match CPython behavior: If debug text is present, apply repr conversion. + // if no `format_spec` specified. + // See: https://github.com/python/cpython/blob/f61afca262d3a0aa6a8a501db0b1936c60858e35/Parser/action_helpers.c#L1456 + if matches!( + (conversion, &fstring_expr.format_spec), + (ConversionFlag::None, None) + ) { + conversion = ConversionFlag::Repr; + } } - match &fstring_expr.format_spec { - None => { - self.emit_load_const(ConstantData::Str { - value: Wtf8Buf::new(), - }); - // Match CPython behavior: If debug text is present, apply repr conversion. - // See: https://github.com/python/cpython/blob/f61afca262d3a0aa6a8a501db0b1936c60858e35/Parser/action_helpers.c#L1456 - if conversion == ConversionFlag::None - && fstring_expr.debug_text.is_some() - { - conversion = ConversionFlag::Repr; - } + self.compile_expression(&fstring_expr.expression)?; + + match conversion { + ConversionFlag::None => {} + ConversionFlag::Str | ConversionFlag::Repr | ConversionFlag::Ascii => { + emit!(self, Instruction::ConvertValue { oparg: conversion }) } + } + + match &fstring_expr.format_spec { Some(format_spec) => { self.compile_fstring_elements(flags, &format_spec.elements)?; + + emit!(self, Instruction::FormatWithSpec); + } + None => { + emit!(self, Instruction::FormatSimple); } } - - self.compile_expression(&fstring_expr.expression)?; - - let conversion = match conversion { - ConversionFlag::None => bytecode::ConversionFlag::None, - ConversionFlag::Str => bytecode::ConversionFlag::Str, - ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii, - ConversionFlag::Repr => bytecode::ConversionFlag::Repr, - }; - emit!(self, Instruction::FormatValue { conversion }); } } } diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index 2a0ad7e280a..b52d5dd9c3d 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -12,18 +12,56 @@ use num_complex::Complex64; use rustpython_wtf8::{Wtf8, Wtf8Buf}; use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, num::NonZeroU8, ops::Deref}; +/// Oparg values for [`Instruction::ConvertValue`]. +/// +/// ## See also +/// +/// - [CPython FVC_* flags](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Include/ceval.h#L129-L132) +#[repr(u8)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] -#[repr(i8)] -#[allow(clippy::cast_possible_wrap)] pub enum ConversionFlag { - /// No conversion - None = -1, // CPython uses -1 + /// No conversion flag. + /// + /// ```python + /// f"{x}" + /// f"{x:4}" + /// ``` + None = 0, /// Converts by calling `str()`. - Str = b's' as i8, - /// Converts by calling `ascii()`. - Ascii = b'a' as i8, + /// + /// ```python + /// f"{x!s}" + /// f"{x!s:2}" + /// ``` + Str = 1, /// Converts by calling `repr()`. - Repr = b'r' as i8, + /// + /// ```python + /// f"{x!r}" + /// f"{x!r:2}" + /// ``` + Repr = 2, + /// Converts by calling `ascii()`. + /// + /// ```python + /// f"{x!a}" + /// f"{x!a:2}" + /// ``` + Ascii = 3, +} + +impl fmt::Display for ConversionFlag { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let out = match self { + Self::Str => "1 (str)", + Self::Repr => "2 (repr)", + Self::Ascii => "3 (ascii)", + // We should never reach this. `FVC_NONE` are being handled by `Instruction::FormatSimple` + Self::None => "", + }; + + write!(f, "{out}") + } } /// Resume type for the RESUME instruction @@ -479,18 +517,18 @@ impl fmt::Display for Label { impl OpArgType for ConversionFlag { #[inline] fn from_op_arg(x: u32) -> Option { - match x as u8 { - b's' => Some(Self::Str), - b'a' => Some(Self::Ascii), - b'r' => Some(Self::Repr), - std::u8::MAX => Some(Self::None), - _ => None, - } + Some(match x { + 0 => Self::None, + 1 => Self::Str, + 2 => Self::Repr, + 3 => Self::Ascii, + _ => return None, + }) } #[inline] fn to_op_arg(self) -> u32 { - self as i8 as u8 as u32 + self as u32 } } @@ -777,9 +815,39 @@ pub enum Instruction { UnpackEx { args: Arg, }, - FormatValue { - conversion: Arg, + /// Convert value to a string, depending on `oparg`: + /// + /// ```python + /// value = STACK.pop() + /// result = func(value) + /// STACK.append(result) + /// ``` + /// + /// Used for implementing formatted string literals (f-strings). + ConvertValue { + oparg: Arg, }, + /// Formats the value on top of stack: + /// + /// ```python + /// value = STACK.pop() + /// result = value.__format__("") + /// STACK.append(result) + /// ``` + /// + /// Used for implementing formatted string literals (f-strings). + FormatSimple, + /// Formats the given value with the given format spec: + /// + /// ```python + /// spec = STACK.pop() + /// value = STACK.pop() + /// result = value.__format__(spec) + /// STACK.append(result) + /// ``` + /// + /// Used for implementing formatted string literals (f-strings). + FormatWithSpec, PopException, Reverse { amount: Arg, @@ -1656,6 +1724,9 @@ impl Instruction { CallMethodKeyword { nargs } => -1 - (nargs.get(arg) as i32) - 3 + 1, CallFunctionEx { has_kwargs } => -1 - (has_kwargs.get(arg) as i32) - 1 + 1, CallMethodEx { has_kwargs } => -1 - (has_kwargs.get(arg) as i32) - 3 + 1, + ConvertValue { .. } => 0, + FormatSimple => 0, + FormatWithSpec => -1, LoadMethod { .. } => -1 + 3, ForIter { .. } => { if jump { @@ -1709,7 +1780,6 @@ impl Instruction { let UnpackExArgs { before, after } = args.get(arg); -1 + before as i32 + 1 + after as i32 } - FormatValue { .. } => -1, PopException => 0, Reverse { .. } => 0, GetAwaitable => 0, @@ -1891,10 +1961,12 @@ impl Instruction { SetAdd { i } => w!(SetAdd, i), MapAdd { i } => w!(MapAdd, i), PrintExpr => w!(PrintExpr), + ConvertValue { oparg } => write!(f, "{:pad$}{}", "CONVERT_VALUE", oparg.get(arg)), + FormatSimple => w!(FORMAT_SIMPLE), + FormatWithSpec => w!(FORMAT_WITH_SPEC), LoadBuildClass => w!(LoadBuildClass), UnpackSequence { size } => w!(UnpackSequence, size), UnpackEx { args } => w!(UnpackEx, args), - FormatValue { conversion } => w!(FormatValue, ?conversion), PopException => w!(PopException), Reverse { amount } => w!(Reverse, amount), GetAwaitable => w!(GetAwaitable), diff --git a/crates/vm/src/frame.rs b/crates/vm/src/frame.rs index bc684bc9f68..c1181efee9a 100644 --- a/crates/vm/src/frame.rs +++ b/crates/vm/src/frame.rs @@ -1308,8 +1308,23 @@ impl ExecutingFrame<'_> { let args = args.get(arg); self.execute_unpack_ex(vm, args.before, args.after) } - bytecode::Instruction::FormatValue { conversion } => { - self.format_value(conversion.get(arg), vm) + bytecode::Instruction::ConvertValue { oparg: conversion } => { + self.convert_value(conversion.get(arg), vm) + } + bytecode::Instruction::FormatSimple => { + let value = self.pop_value(); + let formatted = vm.format(&value, vm.ctx.new_str(""))?; + self.push_value(formatted.into()); + + Ok(None) + } + bytecode::Instruction::FormatWithSpec => { + let spec = self.pop_value(); + let value = self.pop_value(); + let formatted = vm.format(&value, spec.downcast::().unwrap())?; + self.push_value(formatted.into()); + + Ok(None) } bytecode::Instruction::PopException => { let block = self.pop_block(); @@ -2237,7 +2252,7 @@ impl ExecutingFrame<'_> { Err(vm.new_value_error(msg)) } - fn format_value( + fn convert_value( &mut self, conversion: bytecode::ConversionFlag, vm: &VirtualMachine, @@ -2251,9 +2266,7 @@ impl ExecutingFrame<'_> { ConversionFlag::None => value, }; - let spec = self.pop_value(); - let formatted = vm.format(&value, spec.downcast::().unwrap())?; - self.push_value(formatted.into()); + self.push_value(value); Ok(None) } From 804588442ba12222439749294a81a006a967b86c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 2 Dec 2025 14:00:16 +0200 Subject: [PATCH 2/6] Resolve name collision --- crates/codegen/src/compile.rs | 38 ++++++++++--------- ...pile__tests__nested_double_async_with.snap | 33 ++++++++-------- crates/compiler-core/src/bytecode.rs | 10 ++--- crates/vm/src/frame.rs | 12 +++--- crates/vm/src/stdlib/ast/other.rs | 10 ++--- 5 files changed, 52 insertions(+), 51 deletions(-) diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 4656fbe75b2..ead5dda57d0 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -21,22 +21,22 @@ use malachite_bigint::BigInt; use num_complex::Complex; use num_traits::{Num, ToPrimitive}; use ruff_python_ast::{ - Alias, Arguments, BoolOp, CmpOp, Comprehension, DebugText, Decorator, DictItem, ExceptHandler, - ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, ExprFString, - ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp, FString, - FStringFlags, FStringPart, Identifier, Int, InterpolatedElement, InterpolatedStringElement, - InterpolatedStringElements, Keyword, MatchCase, ModExpression, ModModule, Operator, Parameters, - Pattern, PatternMatchAs, PatternMatchClass, PatternMatchMapping, PatternMatchOr, - PatternMatchSequence, PatternMatchSingleton, PatternMatchStar, PatternMatchValue, Singleton, - Stmt, StmtExpr, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, - TypeParams, UnaryOp, WithItem, + Alias, Arguments, BoolOp, CmpOp, Comprehension, ConversionFlag, DebugText, Decorator, DictItem, + ExceptHandler, ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, + ExprFString, ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp, + FString, FStringFlags, FStringPart, Identifier, Int, InterpolatedElement, + InterpolatedStringElement, InterpolatedStringElements, Keyword, MatchCase, ModExpression, + ModModule, Operator, Parameters, Pattern, PatternMatchAs, PatternMatchClass, + PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchSingleton, + PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, TypeParam, TypeParamParamSpec, + TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, WithItem, }; use ruff_text_size::{Ranged, TextRange}; use rustpython_compiler_core::{ Mode, OneIndexed, PositionEncoding, SourceFile, SourceLocation, bytecode::{ self, Arg as OpArgMarker, BinaryOperator, BuildSliceArgCount, CodeObject, - ComparisonOperator, ConstantData, ConversionFlag, Instruction, Invert, OpArg, OpArgType, + ComparisonOperator, ConstantData, ConvertValueOparg, Instruction, Invert, OpArg, OpArgType, UnpackExArgs, }, }; @@ -5638,10 +5638,10 @@ impl Compiler { } InterpolatedStringElement::Interpolation(fstring_expr) => { let mut conversion = match fstring_expr.conversion { - ruff_python_ast::ConversionFlag::None => ConversionFlag::None, - ruff_python_ast::ConversionFlag::Str => ConversionFlag::Str, - ruff_python_ast::ConversionFlag::Repr => ConversionFlag::Repr, - ruff_python_ast::ConversionFlag::Ascii => ConversionFlag::Ascii, + ConversionFlag::None => ConvertValueOparg::None, + ConversionFlag::Str => ConvertValueOparg::Str, + ConversionFlag::Repr => ConvertValueOparg::Repr, + ConversionFlag::Ascii => ConvertValueOparg::Ascii, }; if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text { @@ -5657,17 +5657,19 @@ impl Compiler { // See: https://github.com/python/cpython/blob/f61afca262d3a0aa6a8a501db0b1936c60858e35/Parser/action_helpers.c#L1456 if matches!( (conversion, &fstring_expr.format_spec), - (ConversionFlag::None, None) + (ConvertValueOparg::None, None) ) { - conversion = ConversionFlag::Repr; + conversion = ConvertValueOparg::Repr; } } self.compile_expression(&fstring_expr.expression)?; match conversion { - ConversionFlag::None => {} - ConversionFlag::Str | ConversionFlag::Repr | ConversionFlag::Ascii => { + ConvertValueOparg::None => {} + ConvertValueOparg::Str + | ConvertValueOparg::Repr + | ConvertValueOparg::Ascii => { emit!(self, Instruction::ConvertValue { oparg: conversion }) } } diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap index 3042e35f179..435b73a14de 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap @@ -11,7 +11,7 @@ expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyn 6 CallFunctionPositional(1) 7 BuildTuple (2) 8 GetIter - >> 9 ForIter (72) + >> 9 ForIter (71) 10 StoreLocal (2, stop_exc) 2 11 LoadNameAny (3, self) @@ -21,7 +21,7 @@ expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyn 15 CallFunctionPositional(1) 16 LoadConst (("type")) 17 CallMethodKeyword (1) - 18 SetupWith (69) + 18 SetupWith (68) 19 Pop 3 20 SetupExcept (42) @@ -65,23 +65,22 @@ expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyn 53 LoadConst (None) 54 StoreLocal (8, ex) 55 DeleteLocal (8, ex) - 56 Jump (67) + 56 Jump (66) >> 57 Raise (Reraise) 9 >> 58 LoadNameAny (3, self) 59 LoadMethod (10, fail) - 60 LoadConst ("") - 61 LoadNameAny (2, stop_exc) - 62 FormatValue (None) - 63 LoadConst (" was suppressed") - 64 BuildString (2) - 65 CallMethodPositional (1) - 66 Pop + 60 LoadNameAny (2, stop_exc) + 61 FORMAT_SIMPLE + 62 LoadConst (" was suppressed") + 63 BuildString (2) + 64 CallMethodPositional (1) + 65 Pop - 2 >> 67 PopBlock - 68 EnterFinally - >> 69 WithCleanupStart - 70 WithCleanupFinish - 71 Jump (9) - >> 72 PopBlock - 73 ReturnConst (None) + 2 >> 66 PopBlock + 67 EnterFinally + >> 68 WithCleanupStart + 69 WithCleanupFinish + 70 Jump (9) + >> 71 PopBlock + 72 ReturnConst (None) diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index b52d5dd9c3d..252403035c9 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -19,8 +19,8 @@ use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, num::NonZe /// - [CPython FVC_* flags](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Include/ceval.h#L129-L132) #[repr(u8)] #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] -pub enum ConversionFlag { - /// No conversion flag. +pub enum ConvertValueOparg { + /// No conversion. /// /// ```python /// f"{x}" @@ -50,7 +50,7 @@ pub enum ConversionFlag { Ascii = 3, } -impl fmt::Display for ConversionFlag { +impl fmt::Display for ConvertValueOparg { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let out = match self { Self::Str => "1 (str)", @@ -514,7 +514,7 @@ impl fmt::Display for Label { } } -impl OpArgType for ConversionFlag { +impl OpArgType for ConvertValueOparg { #[inline] fn from_op_arg(x: u32) -> Option { Some(match x { @@ -825,7 +825,7 @@ pub enum Instruction { /// /// Used for implementing formatted string literals (f-strings). ConvertValue { - oparg: Arg, + oparg: Arg, }, /// Formats the value on top of stack: /// diff --git a/crates/vm/src/frame.rs b/crates/vm/src/frame.rs index c1181efee9a..3758c2db860 100644 --- a/crates/vm/src/frame.rs +++ b/crates/vm/src/frame.rs @@ -2254,16 +2254,16 @@ impl ExecutingFrame<'_> { fn convert_value( &mut self, - conversion: bytecode::ConversionFlag, + conversion: bytecode::ConvertValueOparg, vm: &VirtualMachine, ) -> FrameResult { - use bytecode::ConversionFlag; + use bytecode::ConvertValueOparg; let value = self.pop_value(); let value = match conversion { - ConversionFlag::Str => value.str(vm)?.into(), - ConversionFlag::Repr => value.repr(vm)?.into(), - ConversionFlag::Ascii => vm.ctx.new_str(builtins::ascii(value, vm)?).into(), - ConversionFlag::None => value, + ConvertValueOparg::Str => value.str(vm)?.into(), + ConvertValueOparg::Repr => value.repr(vm)?.into(), + ConvertValueOparg::Ascii => vm.ctx.new_str(builtins::ascii(value, vm)?).into(), + ConvertValueOparg::None => value, }; self.push_value(value); diff --git a/crates/vm/src/stdlib/ast/other.rs b/crates/vm/src/stdlib/ast/other.rs index 780d7cc7124..cf8a8319749 100644 --- a/crates/vm/src/stdlib/ast/other.rs +++ b/crates/vm/src/stdlib/ast/other.rs @@ -14,12 +14,12 @@ impl Node for ruff::ConversionFlag { ) -> PyResult { i32::try_from_object(vm, object)? .to_u32() - .and_then(bytecode::ConversionFlag::from_op_arg) + .and_then(bytecode::ConvertValueOparg::from_op_arg) .map(|flag| match flag { - bytecode::ConversionFlag::None => Self::None, - bytecode::ConversionFlag::Str => Self::Str, - bytecode::ConversionFlag::Ascii => Self::Ascii, - bytecode::ConversionFlag::Repr => Self::Repr, + bytecode::ConvertValueOparg::None => Self::None, + bytecode::ConvertValueOparg::Str => Self::Str, + bytecode::ConvertValueOparg::Repr => Self::Repr, + bytecode::ConvertValueOparg::Ascii => Self::Ascii, }) .ok_or_else(|| vm.new_value_error("invalid conversion flag")) } From e6d999d4647bc0bd06bbd52795d1ae83557179d0 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 2 Dec 2025 15:18:53 +0200 Subject: [PATCH 3/6] Adjust for ruff return value --- crates/compiler-core/src/bytecode.rs | 38 +++++++++++++++------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index 252403035c9..f1beb4501c3 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -64,6 +64,26 @@ impl fmt::Display for ConvertValueOparg { } } +impl OpArgType for ConvertValueOparg { + #[inline] + fn from_op_arg(x: u32) -> Option { + Some(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, + }) + } + + #[inline] + fn to_op_arg(self) -> u32 { + self as u32 + } +} + /// Resume type for the RESUME instruction #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[repr(u32)] @@ -514,24 +534,6 @@ impl fmt::Display for Label { } } -impl OpArgType for ConvertValueOparg { - #[inline] - fn from_op_arg(x: u32) -> Option { - Some(match x { - 0 => Self::None, - 1 => Self::Str, - 2 => Self::Repr, - 3 => Self::Ascii, - _ => return None, - }) - } - - #[inline] - fn to_op_arg(self) -> u32 { - self as u32 - } -} - op_arg_enum!( /// The kind of Raise that occurred. #[derive(Copy, Clone, Debug, PartialEq, Eq)] From eefeb24bcc8186836b96dfe3bc66bb899544bc76 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 2 Dec 2025 16:11:32 +0200 Subject: [PATCH 4/6] Trigger CI From 32d4eaff3b52573f29e532c85613fb6dd072f14d Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 2 Dec 2025 17:01:19 +0200 Subject: [PATCH 5/6] Fix sorting --- crates/compiler-core/src/bytecode.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index 021da7652b1..609ec4a6322 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -1868,17 +1868,17 @@ impl Instruction { BinaryOp { op } => write!(f, "{:pad$}({})", "BINARY_OP", op.get(arg)), BinarySubscript => w!(BinarySubscript), Break { target } => w!(Break, target), - BuildList { size } => w!(BuildList, size), BuildListFromTuples { size } => w!(BuildListFromTuples, size), - BuildMap { size } => w!(BuildMap, size), + BuildList { size } => w!(BuildList, size), BuildMapForCall { size } => w!(BuildMapForCall, size), - BuildSet { size } => w!(BuildSet, size), + BuildMap { size } => w!(BuildMap, size), BuildSetFromTuples { size } => w!(BuildSetFromTuples, size), + BuildSet { size } => w!(BuildSet, size), BuildSlice { argc } => w!(BuildSlice, ?argc), BuildString { size } => w!(BuildString, size), - BuildTuple { size } => w!(BuildTuple, size), BuildTupleFromIter => w!(BuildTupleFromIter), BuildTupleFromTuples { size } => w!(BuildTupleFromTuples, size), + BuildTuple { size } => w!(BuildTuple, size), CallFunctionEx { has_kwargs } => w!(CallFunctionEx, has_kwargs), CallFunctionKeyword { nargs } => w!(CallFunctionKeyword, nargs), CallFunctionPositional { nargs } => w!(CallFunctionPositional, nargs), @@ -1912,13 +1912,13 @@ impl Instruction { GetIter => w!(GetIter), GetLen => w!(GetLen), ImportFrom { idx } => w!(ImportFrom, name = idx), - ImportName { idx } => w!(ImportName, name = idx), ImportNameless => w!(ImportNameless), + ImportName { idx } => w!(ImportName, name = idx), IsOp(inv) => w!(IS_OP, ?inv), - Jump { target } => w!(Jump, target), JumpIfFalseOrPop { target } => w!(JumpIfFalseOrPop, target), JumpIfNotExcMatch(target) => w!(JUMP_IF_NOT_EXC_MATCH, target), JumpIfTrueOrPop { target } => w!(JumpIfTrueOrPop, target), + Jump { target } => w!(Jump, target), ListAppend { i } => w!(ListAppend, i), LoadAttr { idx } => w!(LoadAttr, name = idx), LoadBuildClass => w!(LoadBuildClass), From c7c1c0b6ba008cb5c68cf8cefee3048a94b63a9a Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 2 Dec 2025 17:18:36 +0200 Subject: [PATCH 6/6] Fix bad merge --- crates/vm/src/frame.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/vm/src/frame.rs b/crates/vm/src/frame.rs index c22c98921df..ed76fe6aab4 100644 --- a/crates/vm/src/frame.rs +++ b/crates/vm/src/frame.rs @@ -2255,7 +2255,7 @@ impl ExecutingFrame<'_> { fn convert_value( &mut self, - conversion: bytecode::ConversionFlag, + conversion: bytecode::ConvertValueOparg, vm: &VirtualMachine, ) -> FrameResult { use bytecode::ConvertValueOparg;