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

Method try_fold_constant_expr

crates/codegen/src/compile.rs:9465–9497  ·  view source on GitHub ↗
(&mut self, expr: &ast::Expr)

Source from the content-addressed store, hash-verified

9463 }
9464
9465 fn try_fold_constant_expr(&mut self, expr: &ast::Expr) -> CompileResult<Option<ConstantData>> {
9466 Ok(Some(match expr {
9467 ast::Expr::NumberLiteral(num) => match &num.value {
9468 ast::Number::Int(int) => ConstantData::Integer {
9469 value: ruff_int_to_bigint(int).map_err(|e| self.error(e))?,
9470 },
9471 ast::Number::Float(f) => ConstantData::Float { value: *f },
9472 ast::Number::Complex { real, imag } => ConstantData::Complex {
9473 value: Complex::new(*real, *imag),
9474 },
9475 },
9476 ast::Expr::StringLiteral(s) => ConstantData::Str {
9477 value: self.compile_string_value(s),
9478 },
9479 ast::Expr::BytesLiteral(b) => ConstantData::Bytes {
9480 value: b.value.bytes().collect(),
9481 },
9482 ast::Expr::BooleanLiteral(b) => ConstantData::Boolean { value: b.value },
9483 ast::Expr::NoneLiteral(_) => ConstantData::None,
9484 ast::Expr::EllipsisLiteral(_) => ConstantData::Ellipsis,
9485 ast::Expr::Tuple(ast::ExprTuple { elts, .. }) => {
9486 let mut elements = Vec::with_capacity(elts.len());
9487 for elt in elts {
9488 let Some(constant) = self.try_fold_constant_expr(elt)? else {
9489 return Ok(None);
9490 };
9491 elements.push(constant);
9492 }
9493 ConstantData::Tuple { elements }
9494 }
9495 _ => return Ok(None),
9496 }))
9497 }
9498
9499 fn emit_load_const(&mut self, constant: ConstantData) {
9500 let idx = self.arg_constant(constant);

Callers 3

compile_statementMethod · 0.80

Calls 9

ruff_int_to_bigintFunction · 0.85
newFunction · 0.85
compile_string_valueMethod · 0.80
collectMethod · 0.80
SomeClass · 0.50
errorMethod · 0.45
bytesMethod · 0.45
lenMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected