MCPcopy Index your code
hub / github.com/RustPython/RustPython / try_fold_constant_slice

Method try_fold_constant_slice

crates/codegen/src/compile.rs:9505–9530  ·  view source on GitHub ↗

Fold constant slice: if all parts are compile-time constants, emit LOAD_CONST(slice).

(
        &mut self,
        lower: Option<&ast::Expr>,
        upper: Option<&ast::Expr>,
        step: Option<&ast::Expr>,
    )

Source from the content-addressed store, hash-verified

9503
9504 /// Fold constant slice: if all parts are compile-time constants, emit LOAD_CONST(slice).
9505 fn try_fold_constant_slice(
9506 &mut self,
9507 lower: Option<&ast::Expr>,
9508 upper: Option<&ast::Expr>,
9509 step: Option<&ast::Expr>,
9510 ) -> CompileResult<bool> {
9511 let to_const = |expr: Option<&ast::Expr>, this: &mut Self| -> CompileResult<_> {
9512 match expr {
9513 None => Ok(Some(ConstantData::None)),
9514 Some(expr) => this.try_fold_constant_expr(expr),
9515 }
9516 };
9517
9518 let (Some(start), Some(stop), Some(step_val)) = (
9519 to_const(lower, self)?,
9520 to_const(upper, self)?,
9521 to_const(step, self)?,
9522 ) else {
9523 return Ok(false);
9524 };
9525
9526 self.emit_load_const(ConstantData::Slice {
9527 elements: Box::new([start, stop, step_val]),
9528 });
9529 Ok(true)
9530 }
9531
9532 fn emit_return_const(&mut self, constant: ConstantData) {
9533 self.emit_load_const(constant);

Callers 1

compile_expressionMethod · 0.80

Calls 4

newFunction · 0.85
emit_load_constMethod · 0.80
SomeClass · 0.50

Tested by

no test coverage detected