Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7574,6 +7574,11 @@ impl Compiler {
ast::Expr::Slice(ast::ExprSlice {
lower, upper, step, ..
}) => {
// Try constant slice folding first
if self.try_fold_constant_slice(lower.as_deref(), upper.as_deref(), step.as_deref())
{
return Ok(());
}
Comment on lines +7577 to +7581
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

try_fold_constant_slice is narrower than the rest of the compiler’s constant logic.

Lines 8999-9016 only fold numbers, booleans, and None, and Line 9005 also limits integer folding to i64. But ExprExt::is_constant_slice() already treats string/bytes/ellipsis bounds as constant, so cases like obj["a":"z"] skip the BINARY_SLICE fast path and still never reach the new LOAD_CONST(slice) path. Large integer literals have the same gap. That leaves valid constant slices on the old bytecode path and misses the parity target for this feature.

💡 Suggested fix
-                if self.try_fold_constant_slice(lower.as_deref(), upper.as_deref(), step.as_deref())
-                {
+                if self.try_fold_constant_slice(
+                    lower.as_deref(),
+                    upper.as_deref(),
+                    step.as_deref(),
+                )? {
                     return Ok(());
                 }
-    fn try_fold_constant_slice(
-        &mut self,
-        lower: Option<&ast::Expr>,
-        upper: Option<&ast::Expr>,
-        step: Option<&ast::Expr>,
-    ) -> bool {
-        fn to_const(expr: Option<&ast::Expr>) -> Option<ConstantData> {
-            match expr {
-                None | Some(ast::Expr::NoneLiteral(_)) => Some(ConstantData::None),
-                Some(ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. })) => match value
-                {
-                    ast::Number::Int(i) => Some(ConstantData::Integer {
-                        value: i.as_i64()?.into(),
-                    }),
-                    ast::Number::Float(f) => Some(ConstantData::Float { value: *f }),
-                    ast::Number::Complex { real, imag } => Some(ConstantData::Complex {
-                        value: num_complex::Complex64::new(*real, *imag),
-                    }),
-                },
-                Some(ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. })) => {
-                    Some(ConstantData::Boolean { value: *value })
-                }
-                // Only match Constant_kind nodes (no UnaryOp)
-                _ => None,
-            }
-        }
+    fn try_fold_constant_slice(
+        &mut self,
+        lower: Option<&ast::Expr>,
+        upper: Option<&ast::Expr>,
+        step: Option<&ast::Expr>,
+    ) -> CompileResult<bool> {
+        let mut to_const = |expr: Option<&ast::Expr>| -> CompileResult<Option<ConstantData>> {
+            Ok(match expr {
+                None | Some(ast::Expr::NoneLiteral(_)) => Some(ConstantData::None),
+                Some(ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. })) => {
+                    Some(match value {
+                        ast::Number::Int(i) => ConstantData::Integer {
+                            value: ruff_int_to_bigint(i).map_err(|e| self.error(e))?,
+                        },
+                        ast::Number::Float(f) => ConstantData::Float { value: *f },
+                        ast::Number::Complex { real, imag } => ConstantData::Complex {
+                            value: Complex::new(*real, *imag),
+                        },
+                    })
+                }
+                Some(ast::Expr::StringLiteral(s)) => Some(ConstantData::Str {
+                    value: self.compile_string_value(s),
+                }),
+                Some(ast::Expr::BytesLiteral(b)) => Some(ConstantData::Bytes {
+                    value: b.value.bytes().collect(),
+                }),
+                Some(ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. })) => {
+                    Some(ConstantData::Boolean { value: *value })
+                }
+                Some(ast::Expr::EllipsisLiteral(_)) => Some(ConstantData::Ellipsis),
+                _ => None,
+            })
+        };
 
-        let (Some(start), Some(stop), Some(step_val)) =
-            (to_const(lower), to_const(upper), to_const(step))
+        let (Some(start), Some(stop), Some(step_val)) =
+            (to_const(lower)?, to_const(upper)?, to_const(step)?)
         else {
-            return false;
+            return Ok(false);
         };
 
         self.emit_load_const(ConstantData::Slice {
             elements: Box::new([start, stop, step_val]),
         });
-        true
+        Ok(true)
     }

Also applies to: 8992-9030

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/codegen/src/compile.rs` around lines 7577 - 7581,
try_fold_constant_slice currently only recognizes numbers, booleans and None and
limits integer folding to i64, which is narrower than
ExprExt::is_constant_slice; update try_fold_constant_slice to accept the same
constant kinds that ExprExt::is_constant_slice treats constant (include
string/bytes/ellipsis bounds and arbitrary-size integers, not just i64) so that
patterns like obj["a":"z"] and large integer literals are folded to
LOAD_CONST(slice) and hit the BINARY_SLICE fast path; make the same expansion in
the adjacent constant-folding block that has the i64 restriction so both places
have parity with ExprExt::is_constant_slice.

let mut compile_bound = |bound: Option<&ast::Expr>| match bound {
Some(exp) => self.compile_expression(exp),
None => {
Expand Down Expand Up @@ -8407,6 +8412,24 @@ impl Compiler {
let arg0 = self.varname(".0")?;

let return_none = init_collection.is_none();

// PEP 479: Wrap generator/coroutine body with StopIteration handler
let is_gen_scope = self.current_symbol_table().is_generator || is_async;
let stop_iteration_block = if is_gen_scope {
let handler_block = self.new_block();
emit!(
self,
PseudoInstruction::SetupCleanup {
delta: handler_block
}
);
self.set_no_location();
self.push_fblock(FBlockType::StopIteration, handler_block, handler_block)?;
Some(handler_block)
} else {
None
};

// Create empty object of proper type:
if let Some(init_collection) = init_collection {
self._emit(init_collection, OpArg::new(0), BlockIdx::NULL)
Expand Down Expand Up @@ -8496,6 +8519,23 @@ impl Compiler {

self.emit_return_value();

// Close StopIteration handler and emit handler code
if let Some(handler_block) = stop_iteration_block {
emit!(self, PseudoInstruction::PopBlock);
self.set_no_location();
self.pop_fblock(FBlockType::StopIteration);
self.switch_to_block(handler_block);
emit!(
self,
Instruction::CallIntrinsic1 {
func: oparg::IntrinsicFunction1::StopIterationError
}
);
self.set_no_location();
emit!(self, Instruction::Reraise { depth: 1u32 });
self.set_no_location();
}

let code = self.exit_scope();

self.ctx = prev_ctx;
Expand Down Expand Up @@ -8949,6 +8989,46 @@ impl Compiler {
self.emit_arg(idx, |consti| Instruction::LoadConst { consti })
}

/// Fold constant slice: if all parts are compile-time constants, emit LOAD_CONST(slice).
fn try_fold_constant_slice(
&mut self,
lower: Option<&ast::Expr>,
upper: Option<&ast::Expr>,
step: Option<&ast::Expr>,
) -> bool {
fn to_const(expr: Option<&ast::Expr>) -> Option<ConstantData> {
match expr {
None | Some(ast::Expr::NoneLiteral(_)) => Some(ConstantData::None),
Some(ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. })) => match value
{
ast::Number::Int(i) => Some(ConstantData::Integer {
value: i.as_i64()?.into(),
}),
ast::Number::Float(f) => Some(ConstantData::Float { value: *f }),
ast::Number::Complex { real, imag } => Some(ConstantData::Complex {
value: num_complex::Complex64::new(*real, *imag),
}),
},
Some(ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. })) => {
Some(ConstantData::Boolean { value: *value })
}
// Only match Constant_kind nodes (no UnaryOp)
_ => None,
}
}

let (Some(start), Some(stop), Some(step_val)) =
(to_const(lower), to_const(upper), to_const(step))
else {
return false;
};

self.emit_load_const(ConstantData::Slice {
elements: Box::new([start, stop, step_val]),
});
true
}

fn emit_return_const(&mut self, constant: ConstantData) {
self.emit_load_const(constant);
emit!(self, Instruction::ReturnValue)
Expand Down
Loading
Loading