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

Method expr_constant

crates/codegen/src/compile.rs:9602–9630  ·  view source on GitHub ↗

Evaluate whether an expression is a compile-time constant boolean. Returns Some(true) for truthy constants, Some(false) for falsy constants, None for non-constant expressions. = expr_constant in CPython compile.c

(expr: &ast::Expr)

Source from the content-addressed store, hash-verified

9600 /// None for non-constant expressions.
9601 /// = expr_constant in CPython compile.c
9602 fn expr_constant(expr: &ast::Expr) -> Option<bool> {
9603 match expr {
9604 ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) => Some(*value),
9605 ast::Expr::NoneLiteral(_) => Some(false),
9606 ast::Expr::EllipsisLiteral(_) => Some(true),
9607 ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value {
9608 ast::Number::Int(i) => {
9609 let n: i64 = i.as_i64().unwrap_or(1);
9610 Some(n != 0)
9611 }
9612 ast::Number::Float(f) => Some(*f != 0.0),
9613 ast::Number::Complex { real, imag, .. } => Some(*real != 0.0 || *imag != 0.0),
9614 },
9615 ast::Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => {
9616 Some(!value.to_str().is_empty())
9617 }
9618 ast::Expr::BytesLiteral(ast::ExprBytesLiteral { value, .. }) => {
9619 Some(value.bytes().next().is_some())
9620 }
9621 ast::Expr::Tuple(ast::ExprTuple { elts, .. }) => {
9622 if elts.is_empty() {
9623 Some(false)
9624 } else {
9625 None // non-empty tuples may have side effects in elements
9626 }
9627 }
9628 _ => None,
9629 }
9630 }
9631
9632 fn emit_nop(&mut self) {
9633 emit!(self, Instruction::Nop);

Callers

nothing calls this directly

Calls 5

to_strMethod · 0.80
SomeClass · 0.50
is_emptyMethod · 0.45
nextMethod · 0.45
bytesMethod · 0.45

Tested by

no test coverage detected