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
18 changes: 13 additions & 5 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12288,20 +12288,24 @@ impl<'warnings> Compiler<'warnings> {
&mut self,
expr: &ast::Expr,
) -> CompileResult<Option<ConstantData>> {
// CPython 3.14 ast_preprocess.c::fold_const_match_patterns()
// CPython ast_preprocess.c::fold_const_match_patterns()
// folds only the constant forms needed by match patterns before
// codegen_pattern_value()/codegen_pattern_mapping_key() visit them.
Ok(match expr {
ast::Expr::UnaryOp(ast::ExprUnaryOp {
op: ast::UnaryOp::USub,
op: op @ (ast::UnaryOp::UAdd | ast::UnaryOp::USub),
operand,
..
}) => {
let Some(constant) = self.try_compile_match_pattern_number_constant(operand)?
else {
return Ok(None);
};
Self::try_negate_match_pattern_constant(constant)
if *op == ast::UnaryOp::USub {
Self::try_negate_match_pattern_constant(constant)
} else {
Some(constant)
}
}
ast::Expr::BinOp(ast::ExprBinOp {
left, op, right, ..
Expand All @@ -12326,7 +12330,7 @@ impl<'warnings> Compiler<'warnings> {
return Ok(Some(constant));
}
let ast::Expr::UnaryOp(ast::ExprUnaryOp {
op: ast::UnaryOp::USub,
op: op @ (ast::UnaryOp::UAdd | ast::UnaryOp::USub),
operand,
..
}) = expr
Expand All @@ -12336,7 +12340,11 @@ impl<'warnings> Compiler<'warnings> {
let Some(constant) = self.try_compile_match_pattern_real_constant(operand)? else {
return Ok(None);
};
Ok(Self::try_negate_match_pattern_constant(constant))
Ok(if *op == ast::UnaryOp::USub {
Self::try_negate_match_pattern_constant(constant)
} else {
Some(constant)
})
}

fn try_compile_match_pattern_real_constant(
Expand Down
15 changes: 11 additions & 4 deletions crates/vm/src/stdlib/_ast/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ fn validate_interpolated_elements<'a>(
}

fn ensure_literal_number(expr: &ast::Expr, allow_real: bool, allow_imaginary: bool) -> bool {
if let Some(constant) = ast_constant_value(expr) {
return match constant {
ConstantData::Integer { .. } | ConstantData::Float { .. } => allow_real,
ConstantData::Complex { .. } => allow_imaginary,
_ => false,
};
}
let ast::Expr::NumberLiteral(number) = expr else {
return false;
};
Expand All @@ -200,11 +207,11 @@ fn ensure_literal_number(expr: &ast::Expr, allow_real: bool, allow_imaginary: bo
}
}

fn ensure_literal_negative(expr: &ast::Expr, allow_real: bool, allow_imaginary: bool) -> bool {
fn ensure_literal_signed(expr: &ast::Expr, allow_real: bool, allow_imaginary: bool) -> bool {
let ast::Expr::UnaryOp(unary) = expr else {
return false;
};
if unary.op != ast::UnaryOp::USub {
if !matches!(unary.op, ast::UnaryOp::UAdd | ast::UnaryOp::USub) {
return false;
}
ensure_literal_number(&unary.operand, allow_real, allow_imaginary)
Expand All @@ -218,7 +225,7 @@ fn ensure_literal_complex(expr: &ast::Expr) -> bool {
return false;
}
let real_left = ensure_literal_number(&bin.left, true, false)
|| ensure_literal_negative(&bin.left, true, false);
|| ensure_literal_signed(&bin.left, true, false);
real_left && ensure_literal_number(&bin.right, false, true)
}

Expand All @@ -244,7 +251,7 @@ fn validate_pattern_match_value(vm: &VirtualMachine, expr: &ast::Expr) -> PyResu
Ok(())
}
ast::Expr::Attribute(_) => Ok(()),
ast::Expr::UnaryOp(_) if ensure_literal_negative(expr, true, true) => Ok(()),
ast::Expr::UnaryOp(_) if ensure_literal_signed(expr, true, true) => Ok(()),
ast::Expr::BinOp(_) if ensure_literal_complex(expr) => Ok(()),
ast::Expr::FString(_) | ast::Expr::TString(_) => Ok(()),
ast::Expr::BooleanLiteral(_)
Expand Down
Loading